blob: eb0b9d313a736a198b39951a9ecc8a2413894712 [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};
Foxe Chenb90c2392025-06-27 21:10:35 +020047// Note: Keep this in sync with get_clipmethod()
48static char *(p_cpm_values[]) = {"wayland", "x11", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020049#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020050#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020051static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
52 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010053 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020054 # endif
55 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020056#endif
57static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020058#ifdef FEAT_SYN_HL
59// Note: Keep this in sync with fill_culopt_flags()
60static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
61#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020062static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020063static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020064#ifdef FEAT_FOLDING
65static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
66 "quickfix", "search", "tag", "insert",
67 "undo", "jump", NULL};
68#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020069// Note: Keep this in sync with match_keyprotocol()
70static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
71#ifdef FEAT_PROP_POPUP
72// Note: Keep this in sync with parse_popup_option()
73static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
74static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
75static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
76#endif
77#if defined(FEAT_SPELL)
78// Note: Keep this in sync with spell_check_sps()
79static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
80#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020081#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020082// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020083static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
84 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020085 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
86 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020087#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010088// Keep in sync with SWB_ flags in option.h
89static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010090static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020091static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020092// Keep in sync with TCL_ flags in option.h
93static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020094#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
95static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
96#endif
97#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
98static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
99#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200100#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200101static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
102#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +0200103static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200104// Note: Keep this in sync with check_opt_wim()
Girish Palya2bacc3e2025-03-02 22:55:57 +0100105static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
Girish Palya93c2d5b2025-07-08 21:29:02 +0200106static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", "exacttext", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200107#ifdef FEAT_WAK
108static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
109#endif
110static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
111static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
112static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
113static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
114#ifdef FEAT_BROWSE
115static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
116#endif
117static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
118static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
119static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
120static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
121static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200122static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200123#ifdef FEAT_FOLDING
124static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
125# ifdef FEAT_DIFF
126 "diff",
127# endif
128 NULL};
129static char *(p_fcl_values[]) = {"all", NULL};
130#endif
glepnirf31cfa22025-03-06 21:59:13 +0100131static char *(p_cfc_values[]) = {"keyword", "files", "whole_line", NULL};
Girish Palyab1565882025-04-15 20:16:00 +0200132static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", "nearest", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200133#ifdef BACKSLASH_IN_FILENAME
134static char *(p_csl_values[]) = {"slash", "backslash", NULL};
135#endif
136#ifdef FEAT_SIGNS
137static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
138#endif
139#if defined(MSWIN) && defined(FEAT_TERMINAL)
140static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
141#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000142static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000143static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200144
145static int check_opt_strings(char_u *val, char **values, int list);
146static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
147
148/*
149 * After setting various option values: recompute variables that depend on
150 * option values.
151 */
152 void
153didset_string_options(void)
154{
155 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
156 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
157 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
glepnirf31cfa22025-03-06 21:59:13 +0100158 (void)opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200159 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200160#ifdef FEAT_SESSION
161 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
162 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
163#endif
164#ifdef FEAT_FOLDING
165 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
166#endif
167 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200168 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200169 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
170 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200171#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200172 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
173#endif
174#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
175 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
176#endif
177#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
178 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
179#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100180 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200181 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200182}
183
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100184#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200185/*
186 * Trigger the OptionSet autocommand.
187 * "opt_idx" is the index of the option being set.
188 * "opt_flags" can be OPT_LOCAL etc.
189 * "oldval" the old value
190 * "oldval_l" the old local value (only non-NULL if global and local value
191 * are set)
192 * "oldval_g" the old global value (only non-NULL if global and local value
193 * are set)
194 * "newval" the new value
195 */
196 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000197trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200198 int opt_idx,
199 int opt_flags,
200 char_u *oldval,
201 char_u *oldval_l,
202 char_u *oldval_g,
203 char_u *newval)
204{
205 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000206 if (oldval == NULL || newval == NULL
207 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
208 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200209
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000210 char_u buf_type[7];
211
212 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200213 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000214 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
215 set_vim_var_string(VV_OPTION_NEW, newval, -1);
216 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
217 if (opt_flags & OPT_LOCAL)
218 {
219 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
220 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200221 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000222 if (opt_flags & OPT_GLOBAL)
223 {
224 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
225 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
226 }
227 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
228 {
229 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
230 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
231 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
232 }
233 if (opt_flags & OPT_MODELINE)
234 {
235 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
236 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
237 }
238 apply_autocmds(EVENT_OPTIONSET,
239 get_option_fullname(opt_idx), NULL, FALSE,
240 NULL);
241 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200242}
243#endif
244
245 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100246illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200247{
248 if (errbuf == NULL)
249 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100250 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100251 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200252 return errbuf;
253}
254
255/*
256 * Check string options in a buffer for NULL value.
257 */
258 void
259check_buf_options(buf_T *buf)
260{
261 check_string_option(&buf->b_p_bh);
262 check_string_option(&buf->b_p_bt);
263 check_string_option(&buf->b_p_fenc);
264 check_string_option(&buf->b_p_ff);
265#ifdef FEAT_FIND_ID
266 check_string_option(&buf->b_p_def);
267 check_string_option(&buf->b_p_inc);
268# ifdef FEAT_EVAL
269 check_string_option(&buf->b_p_inex);
270# endif
271#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100272#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200273 check_string_option(&buf->b_p_inde);
274 check_string_option(&buf->b_p_indk);
275#endif
276#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
277 check_string_option(&buf->b_p_bexpr);
278#endif
279#if defined(FEAT_CRYPT)
280 check_string_option(&buf->b_p_cm);
281#endif
282 check_string_option(&buf->b_p_fp);
283#if defined(FEAT_EVAL)
284 check_string_option(&buf->b_p_fex);
285#endif
286#ifdef FEAT_CRYPT
287 check_string_option(&buf->b_p_key);
288#endif
289 check_string_option(&buf->b_p_kp);
290 check_string_option(&buf->b_p_mps);
291 check_string_option(&buf->b_p_fo);
292 check_string_option(&buf->b_p_flp);
293 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200294 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200295#ifdef FEAT_FOLDING
296 check_string_option(&buf->b_p_cms);
297#endif
298 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200299 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200300#ifdef FEAT_SYN_HL
301 check_string_option(&buf->b_p_syn);
302 check_string_option(&buf->b_s.b_syn_isk);
303#endif
304#ifdef FEAT_SPELL
305 check_string_option(&buf->b_s.b_p_spc);
306 check_string_option(&buf->b_s.b_p_spf);
307 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200308 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200310 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200311 check_string_option(&buf->b_p_cink);
312 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100313 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200314 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100315 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200316 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200317 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200318 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200319 check_string_option(&buf->b_p_cpt);
glepnirbcd59952025-04-24 21:48:35 +0200320 check_string_option(&buf->b_p_ise);
Bram Moolenaardac13472019-09-16 21:06:21 +0200321#ifdef FEAT_COMPL_FUNC
322 check_string_option(&buf->b_p_cfu);
323 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100324 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200325#endif
326#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100327 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200328 check_string_option(&buf->b_p_tfu);
329#endif
330#ifdef FEAT_KEYMAP
331 check_string_option(&buf->b_p_keymap);
332#endif
333#ifdef FEAT_QUICKFIX
glepnir7b9eb632025-05-16 19:49:23 +0200334 check_string_option(&buf->b_p_gefm);
Bram Moolenaardac13472019-09-16 21:06:21 +0200335 check_string_option(&buf->b_p_gp);
336 check_string_option(&buf->b_p_mp);
337 check_string_option(&buf->b_p_efm);
338#endif
339 check_string_option(&buf->b_p_ep);
340 check_string_option(&buf->b_p_path);
341 check_string_option(&buf->b_p_tags);
342 check_string_option(&buf->b_p_tc);
343 check_string_option(&buf->b_p_dict);
344 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200345 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200346 check_string_option(&buf->b_p_bkc);
347 check_string_option(&buf->b_p_menc);
348#ifdef FEAT_VARTABS
349 check_string_option(&buf->b_p_vsts);
350 check_string_option(&buf->b_p_vts);
351#endif
352}
353
354/*
355 * Free the string allocated for an option.
356 * Checks for the string being empty_option. This may happen if we're out of
357 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
358 * check_options().
359 * Does NOT check for P_ALLOCED flag!
360 */
361 void
362free_string_option(char_u *p)
363{
364 if (p != empty_option)
365 vim_free(p);
366}
367
368 void
369clear_string_option(char_u **pp)
370{
371 if (*pp != empty_option)
372 vim_free(*pp);
373 *pp = empty_option;
374}
375
376 void
377check_string_option(char_u **pp)
378{
379 if (*pp == NULL)
380 *pp = empty_option;
381}
382
383/*
384 * Set global value for string option when it's a local option.
385 */
386 static void
387set_string_option_global(
388 int opt_idx, // option index
389 char_u **varp) // pointer to option variable
390{
391 char_u **p, *s;
392
393 // the global value is always allocated
394 if (is_window_local_option(opt_idx))
395 p = (char_u **)GLOBAL_WO(varp);
396 else
397 p = (char_u **)get_option_var(opt_idx);
398 if (!is_global_option(opt_idx)
399 && p != varp
400 && (s = vim_strsave(*varp)) != NULL)
401 {
402 free_string_option(*p);
403 *p = s;
404 }
405}
406
407/*
408 * Set a string option to a new value (without checking the effect).
409 * The string is copied into allocated memory.
410 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
411 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
412 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
413 * "set_sid".
414 */
415 void
416set_string_option_direct(
417 char_u *name,
418 int opt_idx,
419 char_u *val,
420 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
421 int set_sid UNUSED)
422{
423 char_u *s;
424 char_u **varp;
425 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
426 int idx = opt_idx;
427
428 if (idx == -1) // use name
429 {
430 idx = findoption(name);
431 if (idx < 0) // not found (should not happen)
432 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000433 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100434 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200435 return;
436 }
437 }
438
439 if (is_hidden_option(idx)) // can't set hidden option
440 return;
441
442 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000443 if (s == NULL)
444 return;
445
446 varp = (char_u **)get_option_varp_scope(idx,
447 both ? OPT_LOCAL : opt_flags);
448 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
449 free_string_option(*varp);
450 *varp = s;
451
452 // For buffer/window local option may also set the global value.
453 if (both)
454 set_string_option_global(idx, varp);
455
456 set_option_flag(idx, P_ALLOCED);
457
458 // When setting both values of a global option with a local value,
459 // make the local value empty, so that the global value is used.
460 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200461 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000462 free_string_option(*varp);
463 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200464 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000465# ifdef FEAT_EVAL
466 if (set_sid != SID_NONE)
467 {
468 sctx_T script_ctx;
469
470 if (set_sid == 0)
471 script_ctx = current_sctx;
472 else
473 {
474 script_ctx.sc_sid = set_sid;
475 script_ctx.sc_seq = 0;
476 script_ctx.sc_lnum = 0;
477 script_ctx.sc_version = 1;
478 }
479 set_option_sctx_idx(idx, opt_flags, script_ctx);
480 }
481# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200482}
483
Dominique Pellee764d1b2023-03-12 21:20:59 +0000484#if defined(FEAT_PROP_POPUP) || \
485 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200486/*
487 * Like set_string_option_direct(), but for a window-local option in "wp".
488 * Blocks autocommands to avoid the old curwin becoming invalid.
489 */
490 void
491set_string_option_direct_in_win(
492 win_T *wp,
493 char_u *name,
494 int opt_idx,
495 char_u *val,
496 int opt_flags,
497 int set_sid)
498{
499 win_T *save_curwin = curwin;
500
501 block_autocmds();
502 curwin = wp;
503 curbuf = curwin->w_buffer;
504 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
505 curwin = save_curwin;
506 curbuf = curwin->w_buffer;
507 unblock_autocmds();
508}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000509#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200510
Dominique Pelle748b3082022-01-08 12:41:16 +0000511#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200512/*
513 * Like set_string_option_direct(), but for a buffer-local option in "buf".
514 * Blocks autocommands to avoid the old curbuf becoming invalid.
515 */
516 void
517set_string_option_direct_in_buf(
518 buf_T *buf,
519 char_u *name,
520 int opt_idx,
521 char_u *val,
522 int opt_flags,
523 int set_sid)
524{
525 buf_T *save_curbuf = curbuf;
526
527 block_autocmds();
528 curbuf = buf;
529 curwin->w_buffer = curbuf;
530 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
531 curbuf = save_curbuf;
532 curwin->w_buffer = curbuf;
533 unblock_autocmds();
534}
Dominique Pelle748b3082022-01-08 12:41:16 +0000535#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200536
537/*
538 * Set a string option to a new value, and handle the effects.
539 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100540 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200541 */
542 char *
543set_string_option(
544 int opt_idx,
545 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000546 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100547 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100548 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200549{
550 char_u *s;
551 char_u **varp;
552 char_u *oldval;
553#if defined(FEAT_EVAL)
554 char_u *oldval_l = NULL;
555 char_u *oldval_g = NULL;
556 char_u *saved_oldval = NULL;
557 char_u *saved_oldval_l = NULL;
558 char_u *saved_oldval_g = NULL;
559 char_u *saved_newval = NULL;
560#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100561 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200562 int value_checked = FALSE;
563
564 if (is_hidden_option(opt_idx)) // don't set hidden option
565 return NULL;
566
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100567 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000568 if (s == NULL)
569 return NULL;
570
571 varp = (char_u **)get_option_varp_scope(opt_idx,
572 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
573 ? (is_global_local_option(opt_idx)
574 ? OPT_GLOBAL : OPT_LOCAL)
575 : opt_flags);
576 oldval = *varp;
577#if defined(FEAT_EVAL)
578 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200579 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000580 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
581 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200582 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000583#endif
584 *varp = s;
585
586#if defined(FEAT_EVAL)
587 if (!starting
588# ifdef FEAT_CRYPT
589 && !is_crypt_key_option(opt_idx)
590# endif
591 )
592 {
593 if (oldval_l != NULL)
594 saved_oldval_l = vim_strsave(oldval_l);
595 if (oldval_g != NULL)
596 saved_oldval_g = vim_strsave(oldval_g);
597 saved_oldval = vim_strsave(oldval);
598 saved_newval = vim_strsave(s);
599 }
600#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000601 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100602 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000603 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
604
605#if defined(FEAT_EVAL)
606 // call autocommand after handling side effects
607 if (errmsg == NULL)
608 trigger_optionset_string(opt_idx, opt_flags,
609 saved_oldval, saved_oldval_l,
610 saved_oldval_g, saved_newval);
611 vim_free(saved_oldval);
612 vim_free(saved_oldval_l);
613 vim_free(saved_oldval_g);
614 vim_free(saved_newval);
615#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100616 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200617}
618
619/*
620 * Return TRUE if "val" is a valid 'filetype' name.
621 * Also used for 'syntax' and 'keymap'.
622 */
623 static int
624valid_filetype(char_u *val)
625{
626 return valid_name(val, ".-_");
627}
628
629#ifdef FEAT_STL_OPT
630/*
631 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100632 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200633 */
634 static char *
635check_stl_option(char_u *s)
636{
Bram Moolenaardac13472019-09-16 21:06:21 +0200637 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100638 static char errbuf[ERR_BUFLEN];
639 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200640
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100641 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200642 {
643 // Check for valid keys after % sequences
644 while (*s && *s != '%')
645 s++;
646 if (!*s)
647 break;
648 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000649 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200650 {
651 s++;
652 continue;
653 }
654 if (*s == ')')
655 {
656 s++;
657 if (--groupdepth < 0)
658 break;
659 continue;
660 }
661 if (*s == '-')
662 s++;
663 while (VIM_ISDIGIT(*s))
664 s++;
665 if (*s == STL_USER_HL)
666 continue;
667 if (*s == '.')
668 {
669 s++;
670 while (*s && VIM_ISDIGIT(*s))
671 s++;
672 }
673 if (*s == '(')
674 {
675 groupdepth++;
676 continue;
677 }
678 if (vim_strchr(STL_ALL, *s) == NULL)
679 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100680 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200681 }
682 if (*s == '{')
683 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100684 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200685
zeertzjq5dc294a2022-04-15 13:17:57 +0100686 if (reevaluate && *++s == '}')
687 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100688 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200689 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200690 s++;
691 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100692 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200693 }
694 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200695 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100696 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200697 return NULL;
698}
699#endif
700
701/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000702 * Check for a "normal" directory or file name in some options. Disallow a
703 * path separator (slash and/or backslash), wildcards and characters that are
704 * often illegal in a file name. Be more permissive if "secure" is off.
705 */
706 static int
707check_illegal_path_names(int opt_idx, char_u **varp)
708{
709 return (((get_option_flags(opt_idx) & P_NFNAME)
710 && vim_strpbrk(*varp, (char_u *)(secure
711 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
712 || ((get_option_flags(opt_idx) & P_NDNAME)
713 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
714}
715
716/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000717 * An option that accepts a list of flags is changed.
718 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000719 */
720 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000721did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000722{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000723 if (opt_strings_flags(val, values, flagp, list) == FAIL)
724 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000725
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000726 return NULL;
727}
728
729/*
730 * An option that accepts a list of string values is changed.
731 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
732 */
733 static char *
734did_set_opt_strings(char_u *val, char **values, int list)
735{
736 return did_set_opt_flags(val, values, NULL, list);
737}
738
739/*
740 * An option which is a list of flags is set. Valid values are in 'flags'.
741 */
742 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100743did_set_option_listflag(
744 char_u *val,
745 char_u *flags,
746 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100747 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000748{
749 char_u *s;
750
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000751 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000752 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100753 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000754
755 return NULL;
756}
757
758/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200759 * Expand an option that accepts a list of fixed string values with known
760 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200761 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200762 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200763expand_set_opt_string(
764 optexpand_T *args,
765 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200766 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200767 int *numMatches,
768 char_u ***matches)
769{
770 char_u *p;
771 regmatch_T *regmatch = args->oe_regmatch;
772 int include_orig_val = args->oe_include_orig_val;
773 char_u *option_val = args->oe_opt_value;
774
775 // Assume numValues is small since they are fixed enums, so just allocate
776 // upfront instead of needing two passes to calculate output size.
777 *matches = ALLOC_MULT(char_u *, numValues + 1);
778 if (*matches == NULL)
779 return FAIL;
780
781 int count = 0;
782
783 if (include_orig_val && *option_val != NUL)
784 {
785 p = vim_strsave(option_val);
786 if (p == NULL)
787 {
788 VIM_CLEAR(*matches);
789 return FAIL;
790 }
791 (*matches)[count++] = p;
792 }
793
794 for (char **val = values; *val != NULL; val++)
795 {
796 if (include_orig_val && *option_val != NUL)
797 {
798 if (STRCMP((char_u*)*val, option_val) == 0)
799 continue;
800 }
801 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
802 {
803 p = vim_strsave((char_u*)*val);
804 if (p == NULL)
805 {
806 if (count == 0)
807 {
808 VIM_CLEAR(*matches);
809 return FAIL;
810 }
811 else
812 break;
813 }
814 (*matches)[count++] = p;
815 }
816 }
817 if (count == 0)
818 {
819 VIM_CLEAR(*matches);
820 return FAIL;
821 }
822 *numMatches = count;
823 return OK;
824}
825
826static char_u *set_opt_callback_orig_option = NULL;
827static char_u *((*set_opt_callback_func)(expand_T *, int));
828
829/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200830 * Callback used by expand_set_opt_generic to also include the original value
831 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200832 */
833 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200834expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200835{
836 if (idx == 0)
837 {
838 if (set_opt_callback_orig_option != NULL)
839 return set_opt_callback_orig_option;
840 else
841 return (char_u *)""; // empty strings are ignored
842 }
843 return set_opt_callback_func(xp, idx - 1);
844}
845
846/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200847 * Expand an option with a callback that iterates through a list of possible
848 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200849 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200850 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200851expand_set_opt_generic(
852 optexpand_T *args,
853 char_u *((*func)(expand_T *, int)),
854 int *numMatches,
855 char_u ***matches)
856{
857 int ret;
858
859 set_opt_callback_orig_option = args->oe_include_orig_val ?
860 args->oe_opt_value : NULL;
861 set_opt_callback_func = func;
862
863 ret = ExpandGeneric(
864 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
865 args->oe_xp,
866 args->oe_regmatch,
867 matches,
868 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200869 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200870 FALSE);
871
872 set_opt_callback_orig_option = NULL;
873 set_opt_callback_func = NULL;
874 return ret;
875}
876
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200877# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200878static garray_T *expand_cb_ga;
879static optexpand_T *expand_cb_args;
880
881/*
882 * Callback provided to a function in expand_set_opt_callback. Will perform
883 * regex matching against the value and add to the list.
884 *
885 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
886 * caller should terminate the enumeration.
887 */
888 static int
889expand_set_opt_callback_cb(char_u *val)
890{
891 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
892 expand_T *xp = expand_cb_args->oe_xp;
893 garray_T *ga = expand_cb_ga;
894 char_u *str;
895
896 if (val == NULL || *val == NUL)
897 return OK;
898
899 if (xp->xp_pattern[0] != NUL &&
900 !vim_regexec(regmatch, val, (colnr_T)0))
901 return OK;
902
903 str = vim_strsave_escaped(val, (char_u *)" \t\\");
904
905 if (str == NULL)
906 return FAIL;
907
908 if (ga_grow(ga, 1) == FAIL)
909 {
910 vim_free(str);
911 return FAIL;
912 }
913
914 ((char_u **)ga->ga_data)[ga->ga_len] = str;
915 ++ga->ga_len;
916 return OK;
917}
918
919/*
920 * Expand an option with a provided function that takes a callback. The
921 * function will enumerate through all options and call the callback to add it
922 * to the list.
923 *
924 * "func" is the enumerator function that will generate the list of options.
925 * "func_params" is a single parameter that will be passed to func.
926 */
927 static int
928expand_set_opt_callback(
929 optexpand_T *args,
930 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
931 void *func_params,
932 int *numMatches,
933 char_u ***matches)
934{
935 garray_T ga;
936 int include_orig_val = args->oe_include_orig_val;
937 char_u *option_val = args->oe_opt_value;
938
939 ga_init2(&ga, sizeof(char *), 30);
940
941 if (include_orig_val && *option_val != NUL)
942 {
943 char_u *p = vim_strsave(option_val);
944 if (p == NULL)
945 return FAIL;
946 if (ga_grow(&ga, 1) == FAIL)
947 {
948 vim_free(p);
949 return FAIL;
950 }
951 ((char_u **)ga.ga_data)[ga.ga_len] = p;
952 ++ga.ga_len;
953 }
954
955 expand_cb_ga = &ga;
956 expand_cb_args = args;
957
958 func(args, func_params, expand_set_opt_callback_cb);
959
960 expand_cb_ga = NULL;
961 expand_cb_args = NULL;
962
963 *matches = ga.ga_data;
964 *numMatches = ga.ga_len;
965 return OK;
966}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200967#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200968
969/*
970 * Expand an option which is a list of flags.
971 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200972 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200973expand_set_opt_listflag(
974 optexpand_T *args,
975 char_u *flags,
976 int *numMatches,
977 char_u ***matches)
978{
979 char_u *p;
980 char_u *option_val = args->oe_opt_value;
981 char_u *cmdline_val = args->oe_set_arg;
982 int append = args->oe_append;
983 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
984
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200985 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200986
987 // Assume we only have small number of flags, so just allocate max size.
988 *matches = ALLOC_MULT(char_u *, num_flags + 1);
989 if (*matches == NULL)
990 return FAIL;
991
992 int count = 0;
993
994 if (include_orig_val)
995 {
996 p = vim_strsave(option_val);
997 if (p == NULL)
998 {
999 VIM_CLEAR(*matches);
1000 return FAIL;
1001 }
1002 (*matches)[count++] = p;
1003 }
1004
1005 for (char_u *flag = flags; *flag != NUL; flag++)
1006 {
1007 if (append && vim_strchr(option_val, *flag) != NULL)
1008 continue;
1009
1010 if (vim_strchr(cmdline_val, *flag) == NULL)
1011 {
1012 if (include_orig_val
1013 && option_val[1] == NUL
1014 && *flag == option_val[0])
1015 {
1016 // This value is already used as the first choice as it's the
1017 // existing flag. Just skip it to avoid duplicate.
1018 continue;
1019 }
1020 p = vim_strnsave(flag, 1);
1021 if (p == NULL)
1022 {
1023 if (count == 0)
1024 {
1025 VIM_CLEAR(*matches);
1026 return FAIL;
1027 }
1028 else
1029 break;
1030 }
1031 (*matches)[count++] = p;
1032 }
1033 }
1034
1035 if (count == 0)
1036 {
1037 VIM_CLEAR(*matches);
1038 return FAIL;
1039 }
1040 *numMatches = count;
1041 return OK;
1042}
1043
1044/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001045 * The 'ambiwidth' option is changed.
1046 */
1047 char *
1048did_set_ambiwidth(optset_T *args UNUSED)
1049{
1050 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1051 return e_invalid_argument;
1052
1053 return check_chars_options();
1054}
1055
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001056 int
1057expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1058{
1059 return expand_set_opt_string(
1060 args,
1061 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001062 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001063 numMatches,
1064 matches);
1065}
1066
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001067/*
1068 * The 'background' option is changed.
1069 */
1070 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001071did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001072{
1073 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1074 return e_invalid_argument;
1075
Gregory Anders83ad2722024-01-03 19:48:51 +01001076 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1077 // Value was not changed
1078 return NULL;
1079
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001080#ifdef FEAT_EVAL
1081 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001082#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001083
1084 init_highlight(FALSE, FALSE);
1085
1086#ifdef FEAT_EVAL
1087 if (dark != (*p_bg == 'd')
1088 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001089 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001090 // The color scheme must have set 'background' back to another
1091 // value, that's not what we want here. Disable the color
1092 // scheme and set the colors again.
1093 do_unlet((char_u *)"g:colors_name", TRUE);
1094 free_string_option(p_bg);
1095 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1096 check_string_option(&p_bg);
1097 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001098 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001099#endif
1100#ifdef FEAT_TERMINAL
1101 term_update_colors_all();
1102#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001103
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001104 return NULL;
1105}
1106
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001107 int
1108expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1109{
1110 return expand_set_opt_string(
1111 args,
1112 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001113 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001114 numMatches,
1115 matches);
1116}
1117
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001118/*
1119 * The 'backspace' option is changed.
1120 */
1121 char *
1122did_set_backspace(optset_T *args UNUSED)
1123{
1124 if (VIM_ISDIGIT(*p_bs))
1125 {
1126 if (*p_bs > '3' || p_bs[1] != NUL)
1127 return e_invalid_argument;
1128 }
1129 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1130 return e_invalid_argument;
1131
1132 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001133}
1134
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001135 int
1136expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1137{
1138 return expand_set_opt_string(
1139 args,
1140 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001141 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001142 numMatches,
1143 matches);
1144}
1145
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001146/*
1147 * The 'backupcopy' option is changed.
1148 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001149 char *
1150did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001151{
1152 char_u *bkc = p_bkc;
1153 unsigned int *flags = &bkc_flags;
1154 char *errmsg = NULL;
1155
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001156 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001157 {
1158 bkc = curbuf->b_p_bkc;
1159 flags = &curbuf->b_bkc_flags;
1160 }
zeertzjq46dcd842024-11-03 09:10:50 +01001161 else if (!(args->os_flags & OPT_GLOBAL))
1162 // When using :set, clear the local flags.
1163 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001164
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001165 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001166 // make the local value empty: use the global value
1167 *flags = 0;
1168 else
1169 {
1170 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1171 errmsg = e_invalid_argument;
1172 if ((((int)*flags & BKC_AUTO) != 0)
1173 + (((int)*flags & BKC_YES) != 0)
1174 + (((int)*flags & BKC_NO) != 0) != 1)
1175 {
1176 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001177 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1178 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001179 errmsg = e_invalid_argument;
1180 }
1181 }
1182
1183 return errmsg;
1184}
1185
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001186 int
1187expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1188{
1189 return expand_set_opt_string(
1190 args,
1191 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001192 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001193 numMatches,
1194 matches);
1195}
1196
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001197/*
1198 * The 'backupext' or the 'patchmode' option is changed.
1199 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001200 char *
1201did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001202{
1203 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1204 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1205 return e_backupext_and_patchmode_are_equal;
1206
1207 return NULL;
1208}
1209
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001210/*
1211 * The 'belloff' option is changed.
1212 */
1213 char *
1214did_set_belloff(optset_T *args UNUSED)
1215{
1216 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1217}
1218
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001219 int
1220expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1221{
1222 return expand_set_opt_string(
1223 args,
1224 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001225 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001226 numMatches,
1227 matches);
1228}
1229
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001230#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001231/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001232 * The 'breakat' option is changed.
1233 */
1234 char *
1235did_set_breakat(optset_T *args UNUSED)
1236{
1237 char_u *p;
1238 int i;
1239
1240 for (i = 0; i < 256; i++)
1241 breakat_flags[i] = FALSE;
1242
1243 if (p_breakat != NULL)
1244 for (p = p_breakat; *p; p++)
1245 breakat_flags[*p] = TRUE;
1246
1247 return NULL;
1248}
1249
1250/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001251 * The 'breakindentopt' option is changed.
1252 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001253 char *
Millyb38700a2024-10-22 22:59:39 +02001254did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001255{
Millyb38700a2024-10-22 22:59:39 +02001256 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001257
Millyb38700a2024-10-22 22:59:39 +02001258 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1259 == FAIL)
1260 return e_invalid_argument;
1261
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001262 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001263 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001264 redraw_all_later(UPD_NOT_VALID);
1265
Millyb38700a2024-10-22 22:59:39 +02001266 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001267}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001268
1269 int
1270expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1271{
1272 return expand_set_opt_string(
1273 args,
1274 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001275 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001276 numMatches,
1277 matches);
1278}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001279#endif
1280
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001281#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001282/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001283 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001284 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001285 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001286did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001287{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001288 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1289 && !mch_isdir(p_bsdir))
1290 return e_invalid_argument;
1291
1292 return NULL;
1293}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001294
1295 int
1296expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1297{
1298 return expand_set_opt_string(
1299 args,
1300 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001301 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001302 numMatches,
1303 matches);
1304}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001305#endif
1306
1307/*
1308 * The 'bufhidden' option is changed.
1309 */
1310 char *
1311did_set_bufhidden(optset_T *args UNUSED)
1312{
1313 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1314}
1315
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001316 int
1317expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1318{
1319 return expand_set_opt_string(
1320 args,
1321 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001322 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001323 numMatches,
1324 matches);
1325}
1326
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001327/*
1328 * The 'buftype' option is changed.
1329 */
1330 char *
1331did_set_buftype(optset_T *args UNUSED)
1332{
1333 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1334 return e_invalid_argument;
1335
1336 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001337 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001338 curwin->w_redr_status = TRUE;
1339 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001340 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001341 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1342 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001343
1344 return NULL;
1345}
1346
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001347 int
1348expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1349{
1350 return expand_set_opt_string(
1351 args,
1352 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001353 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001354 numMatches,
1355 matches);
1356}
1357
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001358/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001359 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001360 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001361 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001362did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001363{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001364 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1365}
1366
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001367 int
1368expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1369{
1370 return expand_set_opt_string(
1371 args,
1372 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001373 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001374 numMatches,
1375 matches);
1376}
1377
1378#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1379 int
1380expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1381{
1382 return expand_set_opt_string(
1383 args,
1384 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001385 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001386 numMatches,
1387 matches);
1388}
Foxe Chenb90c2392025-06-27 21:10:35 +02001389
1390 char *
1391did_set_clipmethod(optset_T *args UNUSED)
1392{
1393 return choose_clipmethod();
1394}
1395
1396 int
1397expand_set_clipmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1398{
1399 return expand_set_opt_string(
1400 args,
1401 p_cpm_values,
1402 ARRAY_LENGTH(p_cpm_values) - 1,
1403 numMatches,
1404 matches);
1405}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001406#endif
1407
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001408/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001409 * The global 'listchars' or 'fillchars' option is changed.
1410 */
1411 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001412did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1413 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001414{
1415 char *errmsg = NULL;
1416 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1417
1418 // only apply the global value to "curwin" when it does not have a
1419 // local value
1420 if (opt_lcs)
1421 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001422 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1423 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001424 else
1425 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001426 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1427 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001428 if (errmsg != NULL)
1429 return errmsg;
1430
1431 tabpage_T *tp;
1432 win_T *wp;
1433
1434 // If the current window is set to use the global
1435 // 'listchars'/'fillchars' value, clear the window-local value.
1436 if (!(opt_flags & OPT_GLOBAL))
1437 clear_string_option(local_ptr);
1438 FOR_ALL_TAB_WINDOWS(tp, wp)
1439 {
1440 // If the current window has a local value need to apply it
1441 // again, it was changed when setting the global value.
1442 // If no error was returned above, we don't expect an error
1443 // here, so ignore the return value.
1444 if (opt_lcs)
1445 {
1446 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001447 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001448 }
1449 else
1450 {
1451 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001452 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001453 }
1454 }
1455
1456 redraw_all_later(UPD_NOT_VALID);
1457
1458 return NULL;
1459}
1460
1461/*
1462 * The 'fillchars' option or the 'listchars' option is changed.
1463 */
1464 char *
1465did_set_chars_option(optset_T *args)
1466{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001467 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001468 char *errmsg = NULL;
1469
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001470 if ( varp == &p_lcs // global 'listchars'
1471 || varp == &p_fcs) // global 'fillchars'
1472 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001473 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001474 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001475 errmsg = set_listchars_option(curwin, *varp, TRUE,
1476 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001477 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001478 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1479 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001480
1481 return errmsg;
1482}
1483
1484/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001485 * Expand 'fillchars' or 'listchars' option value.
1486 */
1487 int
1488expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1489{
1490 char_u **varp = (char_u **)args->oe_varp;
1491 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1492 return expand_set_opt_generic(
1493 args,
1494 is_lcs ? get_listchars_name : get_fillchars_name,
1495 numMatches,
1496 matches);
1497}
1498
1499/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001500 * The 'cinoptions' option is changed.
1501 */
1502 char *
1503did_set_cinoptions(optset_T *args UNUSED)
1504{
1505 // TODO: recognize errors
1506 parse_cino(curbuf);
1507
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001508 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001509}
1510
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001511#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001512/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001513 * The 'colorcolumn' option is changed.
1514 */
1515 char *
Millya441a3e2024-10-22 22:43:01 +02001516did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001517{
Millya441a3e2024-10-22 22:43:01 +02001518 char_u **varp = (char_u **)args->os_varp;
1519
1520 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001521}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001522#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001523
1524/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001525 * The 'comments' option is changed.
1526 */
1527 char *
1528did_set_comments(optset_T *args)
1529{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001530 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001531 char_u *s;
1532 char *errmsg = NULL;
1533
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001534 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001535 {
1536 while (*s && *s != ':')
1537 {
1538 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1539 && !VIM_ISDIGIT(*s) && *s != '-')
1540 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001541 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001542 break;
1543 }
1544 ++s;
1545 }
1546 if (*s++ == NUL)
1547 errmsg = e_missing_colon;
1548 else if (*s == ',' || *s == NUL)
1549 errmsg = e_zero_length_string;
1550 if (errmsg != NULL)
1551 break;
1552 while (*s && *s != ',')
1553 {
1554 if (*s == '\\' && s[1] != NUL)
1555 ++s;
1556 ++s;
1557 }
1558 s = skip_to_option_part(s);
1559 }
1560
1561 return errmsg;
1562}
1563
1564#if defined(FEAT_FOLDING) || defined(PROTO)
1565/*
1566 * The 'commentstring' option is changed.
1567 */
1568 char *
1569did_set_commentstring(optset_T *args)
1570{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001571 char_u **varp = (char_u **)args->os_varp;
1572
1573 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001574 return e_commentstring_must_be_empty_or_contain_str;
1575
1576 return NULL;
1577}
1578#endif
1579
1580/*
Girish Palyacbe53192025-04-14 22:13:15 +02001581 * Check if value for 'complete' is valid when 'complete' option is changed.
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001582 */
1583 char *
1584did_set_complete(optset_T *args)
1585{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001586 char_u **varp = (char_u **)args->os_varp;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001587 char_u *p, *t;
Girish Palyacbe53192025-04-14 22:13:15 +02001588 char_u buffer[LSIZE];
1589 char_u *buf_ptr;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001590 char_u char_before = NUL;
Girish Palyacbe53192025-04-14 22:13:15 +02001591 int escape;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001592
Girish Palyacbe53192025-04-14 22:13:15 +02001593 for (p = *varp; *p; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001594 {
Girish Palyacbe53192025-04-14 22:13:15 +02001595 vim_memset(buffer, 0, LSIZE);
1596 buf_ptr = buffer;
1597 escape = 0;
1598
1599 // Extract substring while handling escaped commas
1600 while (*p && (*p != ',' || escape) && buf_ptr < (buffer + LSIZE - 1))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001601 {
Girish Palyacbe53192025-04-14 22:13:15 +02001602 if (*p == '\\' && *(p + 1) == ',')
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001603 {
Girish Palyacbe53192025-04-14 22:13:15 +02001604 escape = 1; // Mark escape mode
1605 p++; // Skip '\'
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001606 }
1607 else
1608 {
Girish Palyacbe53192025-04-14 22:13:15 +02001609 escape = 0;
1610 *buf_ptr++ = *p;
1611 }
1612 p++;
1613 }
1614 *buf_ptr = NUL;
1615
Girish Palya14f6da52025-05-26 19:04:25 +02001616 if (vim_strchr((char_u *)".wbuksid]tUFo", *buffer) == NULL)
Girish Palyacbe53192025-04-14 22:13:15 +02001617 return illegal_char(args->os_errbuf, args->os_errbuflen, *buffer);
1618
Girish Palya14f6da52025-05-26 19:04:25 +02001619 if (vim_strchr((char_u *)"ksF", *buffer) == NULL && *(buffer + 1) != NUL
Girish Palya0ac1eb32025-04-16 20:18:33 +02001620 && *(buffer + 1) != '^')
1621 char_before = *buffer;
1622 else
1623 {
1624 // Test for a number after '^'
1625 if ((t = vim_strchr(buffer, '^')) != NULL)
1626 {
1627 *t++ = NUL;
1628 if (!*t)
1629 char_before = '^';
1630 else
1631 {
1632 for (; *t; t++)
1633 {
1634 if (!vim_isdigit(*t))
1635 {
1636 char_before = '^';
1637 break;
1638 }
1639 }
1640 }
1641 }
1642 }
1643 if (char_before != NUL)
Girish Palyacbe53192025-04-14 22:13:15 +02001644 {
1645 if (args->os_errbuf)
1646 {
1647 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Girish Palya0ac1eb32025-04-16 20:18:33 +02001648 _(e_illegal_character_after_chr), char_before);
Girish Palyacbe53192025-04-14 22:13:15 +02001649 return args->os_errbuf;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001650 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02001651 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001652 }
Girish Palyacbe53192025-04-14 22:13:15 +02001653 // Skip comma and spaces
1654 while (*p == ',' || *p == ' ')
1655 p++;
1656 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001657 return NULL;
1658}
1659
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001660 int
1661expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1662{
1663 static char *(p_cpt_values[]) = {
Girish Palya14f6da52025-05-26 19:04:25 +02001664 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U", "F", "o",
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001665 NULL};
1666 return expand_set_opt_string(
1667 args,
1668 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001669 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001670 numMatches,
1671 matches);
1672}
1673
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001674/*
1675 * The 'completeopt' option is changed.
1676 */
1677 char *
1678did_set_completeopt(optset_T *args UNUSED)
1679{
zeertzjq529b9ad2024-06-05 20:27:06 +02001680 char_u *cot = p_cot;
1681 unsigned *flags = &cot_flags;
1682
1683 if (args->os_flags & OPT_LOCAL)
1684 {
1685 cot = curbuf->b_p_cot;
1686 flags = &curbuf->b_cot_flags;
1687 }
zeertzjq46dcd842024-11-03 09:10:50 +01001688 else if (!(args->os_flags & OPT_GLOBAL))
1689 // When using :set, clear the local flags.
1690 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001691
1692 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001693 return e_invalid_argument;
1694
zeertzjq529b9ad2024-06-05 20:27:06 +02001695 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1696 return e_invalid_argument;
1697
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001698 return NULL;
1699}
1700
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001701 int
1702expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1703{
1704 return expand_set_opt_string(
1705 args,
1706 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001707 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001708 numMatches,
1709 matches);
1710}
1711
glepnir6a89c942024-10-01 20:32:12 +02001712/*
glepnirf31cfa22025-03-06 21:59:13 +01001713 * The 'completefuzzycollect' option is changed.
1714 */
1715 char *
1716did_set_completefuzzycollect(optset_T *args UNUSED)
1717{
1718 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1719 return e_invalid_argument;
1720 return NULL;
1721}
1722
zeertzjq53d59ec2025-03-07 19:09:09 +01001723 int
1724expand_set_completefuzzycollect(
1725 optexpand_T *args,
1726 int *numMatches,
1727 char_u ***matches)
1728{
1729 return expand_set_opt_string(
1730 args,
1731 p_cfc_values,
1732 ARRAY_LENGTH(p_cfc_values) - 1,
1733 numMatches,
1734 matches);
1735}
1736
glepnirf31cfa22025-03-06 21:59:13 +01001737/*
glepnir6a89c942024-10-01 20:32:12 +02001738 * The 'completeitemalign' option is changed.
1739 */
1740 char *
1741did_set_completeitemalign(optset_T *args UNUSED)
1742{
1743 char_u *p = p_cia;
1744 unsigned new_cia_flags = 0;
1745 int seen[3] = { FALSE, FALSE, FALSE };
1746 int count = 0;
1747 char_u buf[10];
1748
1749 while (*p)
1750 {
1751 copy_option_part(&p, buf, sizeof(buf), ",");
1752 if (count >= 3)
1753 return e_invalid_argument;
1754
1755 if (STRCMP(buf, "abbr") == 0)
1756 {
1757 if (seen[CPT_ABBR])
1758 return e_invalid_argument;
1759 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1760 seen[CPT_ABBR] = TRUE;
1761 count++;
1762 }
1763 else if (STRCMP(buf, "kind") == 0)
1764 {
1765 if (seen[CPT_KIND])
1766 return e_invalid_argument;
1767 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1768 seen[CPT_KIND] = TRUE;
1769 count++;
1770 }
1771 else if (STRCMP(buf, "menu") == 0)
1772 {
1773 if (seen[CPT_MENU])
1774 return e_invalid_argument;
1775 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1776 seen[CPT_MENU] = TRUE;
1777 count++;
1778 }
1779 else
1780 return e_invalid_argument;
1781 }
1782 if (new_cia_flags == 0 || count != 3)
1783 return e_invalid_argument;
1784
1785 cia_flags = new_cia_flags;
1786 return NULL;
1787}
1788
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001789#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1790/*
1791 * The 'completepopup' option is changed.
1792 */
1793 char *
1794did_set_completepopup(optset_T *args UNUSED)
1795{
1796 if (parse_completepopup(NULL) == FAIL)
1797 return e_invalid_argument;
1798
1799 popup_close_info();
1800 return NULL;
1801}
1802#endif
1803
1804#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1805/*
1806 * The 'completeslash' option is changed.
1807 */
1808 char *
1809did_set_completeslash(optset_T *args UNUSED)
1810{
1811 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1812 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1813 return e_invalid_argument;
1814
1815 return NULL;
1816}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001817
1818 int
1819expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1820{
1821 return expand_set_opt_string(
1822 args,
1823 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001824 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001825 numMatches,
1826 matches);
1827}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001828#endif
1829
1830#if defined(FEAT_CONCEAL) || defined(PROTO)
1831/*
1832 * The 'concealcursor' option is changed.
1833 */
1834 char *
1835did_set_concealcursor(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 *)COCU_ALL, args->os_errbuf,
1840 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001841}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001842
1843 int
1844expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1845{
1846 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1847}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001848#endif
1849
1850/*
1851 * The 'cpoptions' option is changed.
1852 */
1853 char *
1854did_set_cpoptions(optset_T *args)
1855{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001856 char_u **varp = (char_u **)args->os_varp;
1857
Christian Brabandtb39b2402023-11-29 11:34:05 +01001858 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1859 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001860}
1861
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001862 int
1863expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1864{
1865 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1866}
1867
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001868#if defined(FEAT_CRYPT) || defined(PROTO)
1869/*
1870 * The 'cryptkey' option is changed.
1871 */
1872 char *
1873did_set_cryptkey(optset_T *args)
1874{
1875 // Make sure the ":set" command doesn't show the new value in the
1876 // history.
1877 remove_key_from_history();
1878
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001879 if (args->os_op != OP_NONE)
1880 // Don't allow set+=/-=/^= as they can allow for substring guessing
1881 return e_invalid_argument;
1882
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001883 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1884 {
1885 // Need to update the swapfile.
1886 ml_set_crypt_key(curbuf, args->os_oldval.string,
1887 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1888 changed_internal();
1889 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001890# ifdef FEAT_SODIUM
1891 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02001892 crypt_sodium_lock_key(args->os_newval.string);
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001893# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001894
1895 return NULL;
1896}
1897
1898/*
1899 * The 'cryptmethod' option is changed.
1900 */
1901 char *
1902did_set_cryptmethod(optset_T *args)
1903{
1904 char_u *p;
1905 char_u *s;
1906
1907 if (args->os_flags & OPT_LOCAL)
1908 p = curbuf->b_p_cm;
1909 else
1910 p = p_cm;
1911 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1912 return e_invalid_argument;
1913 else if (crypt_self_test() == FAIL)
1914 return e_invalid_argument;
1915
1916 // When setting the global value to empty, make it "zip".
1917 if (*p_cm == NUL)
1918 {
1919 free_string_option(p_cm);
1920 p_cm = vim_strsave((char_u *)"zip");
1921 }
1922 // When using ":set cm=name" the local value is going to be empty.
1923 // Do that here, otherwise the crypt functions will still use the
1924 // local value.
1925 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1926 {
1927 free_string_option(curbuf->b_p_cm);
1928 curbuf->b_p_cm = empty_option;
1929 }
1930
1931 // Need to update the swapfile when the effective method changed.
1932 // Set "s" to the effective old value, "p" to the effective new
1933 // method and compare.
1934 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1935 s = p_cm; // was previously using the global value
1936 else
1937 s = args->os_oldval.string;
1938 if (*curbuf->b_p_cm == NUL)
1939 p = p_cm; // is now using the global value
1940 else
1941 p = curbuf->b_p_cm;
1942 if (STRCMP(s, p) != 0)
1943 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1944
1945 // If the global value changes need to update the swapfile for all
1946 // buffers using that value.
1947 if ((args->os_flags & OPT_GLOBAL)
1948 && STRCMP(p_cm, args->os_oldval.string) != 0)
1949 {
1950 buf_T *buf;
1951
1952 FOR_ALL_BUFFERS(buf)
1953 if (buf != curbuf && *buf->b_p_cm == NUL)
1954 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1955 }
1956 return NULL;
1957}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001958
1959 int
1960expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1961{
1962 return expand_set_opt_string(
1963 args,
1964 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001965 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001966 numMatches,
1967 matches);
1968}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001969#endif
1970
1971#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1972/*
1973 * The 'cscopequickfix' option is changed.
1974 */
1975 char *
1976did_set_cscopequickfix(optset_T *args UNUSED)
1977{
1978 char_u *p;
1979
1980 if (p_csqf == NULL)
1981 return NULL;
1982
1983 p = p_csqf;
1984 while (*p != NUL)
1985 {
1986 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1987 || p[1] == NUL
1988 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1989 || (p[2] != NUL && p[2] != ','))
1990 return e_invalid_argument;
1991 else if (p[2] == NUL)
1992 break;
1993 else
1994 p += 3;
1995 }
1996
1997 return NULL;
1998}
1999#endif
2000
2001#if defined(FEAT_SYN_HL) || defined(PROTO)
2002/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002003 * The 'cursorlineopt' option is changed.
2004 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002005 char *
2006did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002007{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002008 char_u **varp = (char_u **)args->os_varp;
2009
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002010 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002011 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002012 return e_invalid_argument;
2013
2014 return NULL;
2015}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002016
2017 int
2018expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
2019{
2020 return expand_set_opt_string(
2021 args,
2022 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002023 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002024 numMatches,
2025 matches);
2026}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002027#endif
2028
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002029/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002030 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002031 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002032 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002033did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002034{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002035 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002036}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002037
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002038 int
2039expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
2040{
2041 return expand_set_opt_string(
2042 args,
2043 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002044 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002045 numMatches,
2046 matches);
2047}
2048
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002049#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002050/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002051 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002052 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002053 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002054did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002055{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002056 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002057 return e_invalid_argument;
2058
2059 return NULL;
2060}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002061
2062 int
2063expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2064{
2065 expand_T *xp = args->oe_xp;
2066
2067 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2068 {
2069 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002070 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002071 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2072 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2073 {
2074 return expand_set_opt_string(
2075 args,
2076 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002077 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002078 numMatches,
2079 matches);
2080 }
Yee Cheng Chin9943d472025-03-26 19:41:02 +01002081 // Within "inline:", we have a subgroup of possible options.
2082 int inline_len = (int)STRLEN("inline:");
2083 if (xp->xp_pattern - args->oe_set_arg >= inline_len &&
2084 STRNCMP(xp->xp_pattern - inline_len, "inline:", inline_len) == 0)
2085 {
2086 return expand_set_opt_string(
2087 args,
2088 p_dip_inline_values,
2089 ARRAY_LENGTH(p_dip_inline_values) - 1,
2090 numMatches,
2091 matches);
2092 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002093 return FAIL;
2094 }
2095
2096 return expand_set_opt_string(
2097 args,
2098 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002099 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002100 numMatches,
2101 matches);
2102}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002103#endif
2104
2105/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002106 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002107 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002108 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002109did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002110{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002111 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002112 return e_invalid_argument;
2113
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002114 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002115 return NULL;
2116}
2117
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002118 int
2119expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2120{
2121 return expand_set_opt_string(
2122 args,
2123 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002124 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002125 numMatches,
2126 matches);
2127}
2128
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002129/*
2130 * The 'eadirection' option is changed.
2131 */
2132 char *
2133did_set_eadirection(optset_T *args UNUSED)
2134{
2135 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2136}
2137
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002138 int
2139expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2140{
2141 return expand_set_opt_string(
2142 args,
2143 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002144 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002145 numMatches,
2146 matches);
2147}
2148
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002149/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002150 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2151 * options is changed.
2152 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002153 char *
2154did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002155{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002156 char_u **varp = (char_u **)args->os_varp;
2157 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002158 char *errmsg = NULL;
2159 char_u *p;
2160
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002161 // Get the global option to compare with, otherwise we would have to check
2162 // two values for all local options.
2163 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2164
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002165 if (gvarp == &p_fenc)
2166 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002167 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002168 errmsg = e_cannot_make_changes_modifiable_is_off;
2169 else if (vim_strchr(*varp, ',') != NULL)
2170 // No comma allowed in 'fileencoding'; catches confusing it
2171 // with 'fileencodings'.
2172 errmsg = e_invalid_argument;
2173 else
2174 {
2175 // May show a "+" in the title now.
2176 redraw_titles();
2177 // Add 'fileencoding' to the swap file.
2178 ml_setflags(curbuf);
2179 }
2180 }
2181 if (errmsg == NULL)
2182 {
2183 // canonize the value, so that STRCMP() can be used on it
2184 p = enc_canonize(*varp);
2185 if (p != NULL)
2186 {
2187 vim_free(*varp);
2188 *varp = p;
2189 }
2190 if (varp == &p_enc)
2191 {
2192 errmsg = mb_init();
2193 redraw_titles();
2194 }
2195 }
2196
2197#if defined(FEAT_GUI_GTK)
2198 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2199 {
2200 // GTK uses only a single encoding, and that is UTF-8.
2201 if (STRCMP(p_tenc, "utf-8") != 0)
2202 errmsg = e_cannot_be_changed_in_gtk_GUI;
2203 }
2204#endif
2205
2206 if (errmsg == NULL)
2207 {
2208#ifdef FEAT_KEYMAP
2209 // When 'keymap' is used and 'encoding' changes, reload the keymap
2210 // (with another encoding).
2211 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2212 (void)keymap_init();
2213#endif
2214
2215 // When 'termencoding' is not empty and 'encoding' changes or when
2216 // 'termencoding' changes, need to setup for keyboard input and
2217 // display output conversion.
2218 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2219 {
2220 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2221 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2222 {
2223 semsg(_(e_cannot_convert_between_str_and_str),
2224 p_tenc, p_enc);
2225 errmsg = e_invalid_argument;
2226 }
2227 }
2228
2229#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002230 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002231 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002232 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002233 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002234 init_vimdir();
2235 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002236#endif
2237 }
2238
2239 return errmsg;
2240}
2241
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002242 int
2243expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2244{
2245 return expand_set_opt_generic(
2246 args,
2247 get_encoding_name,
2248 numMatches,
2249 matches);
2250}
2251
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002252/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002253 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002254 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002255 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002256did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002257{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002258 char_u **varp = (char_u **)args->os_varp;
2259
2260 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002261 return e_invalid_argument;
2262 return NULL;
2263}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002264
Luuk van Baalb7147f82025-02-08 18:52:39 +01002265static int expand_eiw = FALSE;
2266
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002267 static char_u *
2268get_eventignore_name(expand_T *xp, int idx)
2269{
Luuk van Baal8cc6d8b2025-05-31 12:10:31 +02002270 int subtract = *xp->xp_pattern == '-';
Luuk van Baalb7147f82025-02-08 18:52:39 +01002271 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002272 // all event names.
Luuk van Baal8cc6d8b2025-05-31 12:10:31 +02002273 if (!subtract && idx == 0)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002274 return (char_u *)"all";
Luuk van Baal8cc6d8b2025-05-31 12:10:31 +02002275
2276 char_u *name = get_event_name_no_group(xp, idx - 1 + subtract, expand_eiw);
2277 if (name == NULL)
2278 return NULL;
2279
2280 sprintf((char *)IObuff, "%s%s", subtract ? "-" : "", name);
2281 return IObuff;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002282}
2283
2284 int
2285expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2286{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002287 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002288 return expand_set_opt_generic(
2289 args,
2290 get_eventignore_name,
2291 numMatches,
2292 matches);
2293}
2294
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295/*
2296 * The 'fileformat' option is changed.
2297 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002298 char *
2299did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002300{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002301 char_u **varp = (char_u **)args->os_varp;
2302
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002303 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002304 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002305 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002306 return e_invalid_argument;
2307
2308 // may also change 'textmode'
2309 if (get_fileformat(curbuf) == EOL_DOS)
2310 curbuf->b_p_tx = TRUE;
2311 else
2312 curbuf->b_p_tx = FALSE;
2313 redraw_titles();
2314 // update flag in swap file
2315 ml_setflags(curbuf);
2316 // Redraw needed when switching to/from "mac": a CR in the text
2317 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002318 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002319 redraw_curbuf_later(UPD_NOT_VALID);
2320
2321 return NULL;
2322}
2323
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002324 int
2325expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2326{
2327 return expand_set_opt_string(
2328 args,
2329 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002330 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002331 numMatches,
2332 matches);
2333}
2334
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002335/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002336 * Function given to ExpandGeneric() to obtain the possible arguments of the
2337 * fileformat options.
2338 */
2339 char_u *
2340get_fileformat_name(expand_T *xp UNUSED, int idx)
2341{
2342 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2343 return NULL;
2344
2345 return (char_u*)p_ff_values[idx];
2346}
2347
2348/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002349 * The 'fileformats' option is changed.
2350 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002351 char *
2352did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002353{
2354 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2355 return e_invalid_argument;
2356
2357 // also change 'textauto'
2358 if (*p_ffs == NUL)
2359 p_ta = FALSE;
2360 else
2361 p_ta = TRUE;
2362
2363 return NULL;
2364}
2365
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002366/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002367 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002368 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002369 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002370did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002371{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002372 char_u **varp = (char_u **)args->os_varp;
2373
2374 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002375 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002376
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002377 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002378
2379 // Since we check the value, there is no need to set P_INSECURE,
2380 // even when the value comes from a modeline.
2381 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002382
2383 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002384}
2385
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002386#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002387/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002388 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002389 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002390 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002391did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002392{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002393 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2394}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002395
2396 int
2397expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2398{
2399 return expand_set_opt_string(
2400 args,
2401 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002402 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002403 numMatches,
2404 matches);
2405}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002406#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002407
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002408#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2409/*
2410 * The 'foldexpr' option is changed.
2411 */
2412 char *
2413did_set_foldexpr(optset_T *args)
2414{
2415 (void)did_set_optexpr(args);
2416 if (foldmethodIsExpr(curwin))
2417 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002418 return NULL;
2419}
2420#endif
2421
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002422#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002423/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002424 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002425 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002426 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002427did_set_foldignore(optset_T *args UNUSED)
2428{
2429 if (foldmethodIsIndent(curwin))
2430 foldUpdateAll(curwin);
2431 return NULL;
2432}
2433
2434/*
2435 * The 'foldmarker' option is changed.
2436 */
2437 char *
2438did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002439{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002440 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002441 char_u *p;
2442
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002443 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002444 if (p == NULL)
2445 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002446 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002447 return e_invalid_argument;
2448 else if (foldmethodIsMarker(curwin))
2449 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002450
2451 return NULL;
2452}
2453
2454/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002455 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002456 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002457 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002458did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002459{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002460 char_u **varp = (char_u **)args->os_varp;
2461
Milly142cad12024-10-22 22:11:51 +02002462 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002463 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002464
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002465 foldUpdateAll(curwin);
2466 if (foldmethodIsDiff(curwin))
2467 newFoldLevel();
2468 return NULL;
2469}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002470
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002471 int
2472expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2473{
2474 return expand_set_opt_string(
2475 args,
2476 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002477 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002478 numMatches,
2479 matches);
2480}
2481
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002482/*
2483 * The 'foldopen' option is changed.
2484 */
2485 char *
2486did_set_foldopen(optset_T *args UNUSED)
2487{
2488 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2489}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002490
2491 int
2492expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2493{
2494 return expand_set_opt_string(
2495 args,
2496 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002497 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002498 numMatches,
2499 matches);
2500}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002501#endif
2502
2503/*
2504 * The 'formatoptions' option is changed.
2505 */
2506 char *
2507did_set_formatoptions(optset_T *args)
2508{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002509 char_u **varp = (char_u **)args->os_varp;
2510
Christian Brabandtb39b2402023-11-29 11:34:05 +01002511 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2512 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002513}
2514
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002515 int
2516expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2517{
2518 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2519}
2520
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002521#if defined(CURSOR_SHAPE) || defined(PROTO)
2522/*
2523 * The 'guicursor' option is changed.
2524 */
2525 char *
2526did_set_guicursor(optset_T *args UNUSED)
2527{
2528 return parse_shape_opt(SHAPE_CURSOR);
2529}
2530#endif
2531
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002532#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002533/*
2534 * The 'guifont' option is changed.
2535 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002536 char *
2537did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002538{
2539 char_u *p;
2540 char *errmsg = NULL;
2541
2542 if (gui.in_use)
2543 {
2544 p = p_guifont;
2545# if defined(FEAT_GUI_GTK)
2546 // Put up a font dialog and let the user select a new value.
2547 // If this is cancelled go back to the old value but don't
2548 // give an error message.
2549 if (STRCMP(p, "*") == 0)
2550 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002551 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002552 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002553 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002554 }
2555# endif
2556 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2557 {
2558# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2559 if (STRCMP(p_guifont, "*") == 0)
2560 {
2561 // Dialog was cancelled: Keep the old value without giving
2562 // an error message.
2563 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002564 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002565 }
2566 else
2567# endif
2568 errmsg = e_invalid_fonts;
2569 }
2570 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002571
2572 return errmsg;
2573}
2574
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002575/*
2576 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2577 * specific behaviors.
2578 */
2579 int
2580expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2581{
2582 if (!gui.in_use)
2583 return FAIL;
2584
2585# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2586 char_u **varp = (char_u **)args->oe_varp;
2587 int wide = (varp == &p_guifontwide);
2588
2589 return expand_set_opt_callback(
2590 args, gui_mch_expand_font, &wide, numMatches, matches);
2591# else
2592 return FAIL;
2593# endif
2594}
2595
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002596# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002597/*
2598 * The 'guifontset' option is changed.
2599 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002600 char *
2601did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002602{
2603 char *errmsg = NULL;
2604
2605 if (STRCMP(p_guifontset, "*") == 0)
2606 errmsg = e_cant_select_fontset;
2607 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2608 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002609
2610 return errmsg;
2611}
2612# endif
2613
2614/*
2615 * The 'guifontwide' option is changed.
2616 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002617 char *
2618did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002619{
2620 char *errmsg = NULL;
2621
2622 if (STRCMP(p_guifontwide, "*") == 0)
2623 errmsg = e_cant_select_wide_font;
2624 else if (gui_get_wide_font() == FAIL)
2625 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002626
2627 return errmsg;
2628}
2629#endif
2630
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002631#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002632/*
2633 * The 'guiligatures' option is changed.
2634 */
2635 char *
2636did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002637{
2638 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002639 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002640}
2641#endif
2642
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002643#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002644/*
2645 * The 'guioptions' option is changed.
2646 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002647 char *
2648did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002649{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002650 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002651 char *errmsg;
2652
Christian Brabandtb39b2402023-11-29 11:34:05 +01002653 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2654 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002655 if (errmsg != NULL)
2656 return errmsg;
2657
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002658 gui_init_which_components(args->os_oldval.string);
2659 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002660}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002661
2662 int
2663expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2664{
2665 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2666}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002667#endif
2668
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002669#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002670/*
2671 * The 'guitablabel' option is changed.
2672 */
2673 char *
2674did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002675{
2676 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002677 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002678}
2679#endif
2680
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002681/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002682 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002683 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002684 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002685did_set_helpfile(optset_T *args UNUSED)
2686{
2687 // May compute new values for $VIM and $VIMRUNTIME
2688 if (didset_vim)
2689 vim_unsetenv_ext((char_u *)"VIM");
2690 if (didset_vimruntime)
2691 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2692 return NULL;
2693}
2694
2695#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2696/*
2697 * The 'helplang' option is changed.
2698 */
2699 char *
2700did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002701{
2702 char *errmsg = NULL;
2703
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002704 // Check for "", "ab", "ab,cd", etc.
2705 for (char_u *s = p_hlg; *s != NUL; s += 3)
2706 {
2707 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2708 {
2709 errmsg = e_invalid_argument;
2710 break;
2711 }
2712 if (s[2] == NUL)
2713 break;
2714 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002715
2716 return errmsg;
2717}
2718#endif
2719
2720/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002721 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002722 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002723 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002724did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002725{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002726 if (highlight_changed() == FAIL)
2727 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002728
2729 return NULL;
2730}
2731
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002732/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002733 * Expand 'highlight' option.
2734 */
2735 int
2736expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2737{
2738 char_u *p;
2739 expand_T *xp = args->oe_xp;
2740 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002741 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002742 int count = 0;
2743
2744 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2745 {
2746 // Right after a ':', meaning we just return all highlight names.
2747 return expand_set_opt_generic(
2748 args,
2749 get_highlight_name,
2750 numMatches,
2751 matches);
2752 }
2753
2754 if (*xp->xp_pattern == NUL)
2755 {
2756 // At beginning of a comma-separated list. Return the specific list of
2757 // supported occasions.
2758 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2759 if (*matches == NULL)
2760 return FAIL;
2761
2762 // We still want to return the full option if it's requested.
2763 if (args->oe_include_orig_val)
2764 {
2765 p = vim_strsave(args->oe_opt_value);
2766 if (p == NULL)
2767 {
2768 VIM_CLEAR(*matches);
2769 return FAIL;
2770 }
2771 (*matches)[count++] = p;
2772 }
2773
2774 for (i = 0; i < HLF_COUNT; i++)
2775 {
2776 p = vim_strnsave(&hl_flags[i], 1);
2777 if (p == NULL)
2778 {
2779 if (count == 0)
2780 {
2781 VIM_CLEAR(*matches);
2782 return FAIL;
2783 }
2784 else
2785 break;
2786 }
2787 (*matches)[count++] = p;
2788 }
2789
2790 if (count == 0)
2791 {
2792 VIM_CLEAR(*matches);
2793 return FAIL;
2794 }
2795 *numMatches = count;
2796 return OK;
2797 }
2798
2799 // We are after the initial character (which indicates the occasion). We
2800 // already made sure we are not matching after a ':' above, so now we want
2801 // to match against display mode modifiers.
2802 // Since the xp_pattern starts from the beginning, we need to include it in
2803 // the returned match.
2804
2805 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002806 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002807 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002808 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002809
2810 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2811 if (*matches == NULL)
2812 return FAIL;
2813
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002814 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002815
2816 for (i = 0; i < num_hl_modes; i++)
2817 {
2818 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002819 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2820 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002821 continue;
2822
2823 // ':' only works by itself, not with other flags.
2824 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2825 continue;
2826
2827 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2828 if (p == NULL)
2829 {
2830 if (i == 0)
2831 {
2832 VIM_CLEAR(*matches);
2833 return FAIL;
2834 }
2835 else
2836 break;
2837 }
2838 p[pattern_len] = p_hl_mode_values[i];
2839 p[pattern_len + 1] = NUL;
2840 (*matches)[count++] = p;
2841 }
2842 if (count == 0)
2843 {
2844 VIM_CLEAR(*matches);
2845 return FAIL;
2846 }
2847 *numMatches = count;
2848
2849 return OK;
2850}
2851
2852/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002853 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002854 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002855 static char *
2856parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002857{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002858#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002859 char_u **varp = (char_u **)args->os_varp;
2860
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002861 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002862 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002863 stl_syntax |= flagval;
2864 else
2865 stl_syntax &= ~flagval;
2866#endif
2867 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002868
2869 return NULL;
2870}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002871
2872/*
2873 * The 'iconstring' option is changed.
2874 */
2875 char *
2876did_set_iconstring(optset_T *args)
2877{
2878 int flagval = 0;
2879
2880#ifdef FEAT_STL_OPT
2881 flagval = STL_IN_ICON;
2882#endif
2883
2884 return parse_titleiconstring(args, flagval);
2885}
2886
2887#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2888/*
2889 * The 'imactivatekey' option is changed.
2890 */
2891 char *
2892did_set_imactivatekey(optset_T *args UNUSED)
2893{
2894 if (!im_xim_isvalid_imactivate())
2895 return e_invalid_argument;
2896 return NULL;
2897}
2898#endif
2899
2900/*
glepnirbcd59952025-04-24 21:48:35 +02002901 * The 'isexpand' option is changed.
2902 */
2903 char *
2904did_set_isexpand(optset_T *args)
2905{
2906 char_u *ise = p_ise;
2907 char_u *p;
2908 int last_was_comma = FALSE;
2909
2910 if (args->os_flags & OPT_LOCAL)
2911 ise = curbuf->b_p_ise;
2912
2913 for (p = ise; *p != NUL;)
2914 {
2915 if (*p == '\\' && p[1] == ',')
2916 {
2917 p += 2;
2918 last_was_comma = FALSE;
2919 continue;
2920 }
2921
2922 if (*p == ',')
2923 {
2924 if (last_was_comma)
2925 return e_invalid_argument;
2926 last_was_comma = TRUE;
2927 p++;
2928 continue;
2929 }
2930
2931 last_was_comma = FALSE;
2932 MB_PTR_ADV(p);
2933 }
2934
2935 if (last_was_comma)
2936 return e_invalid_argument;
2937
2938 return NULL;
2939}
2940
2941
2942/*
Milly5e7a6a42024-10-22 22:27:19 +02002943 * The 'iskeyword' option is changed.
2944 */
2945 char *
2946did_set_iskeyword(optset_T *args)
2947{
2948 char_u **varp = (char_u **)args->os_varp;
2949
2950 if (varp == &p_isk) // only check for global-value
2951 {
2952 if (check_isopt(*varp) == FAIL)
2953 return e_invalid_argument;
2954 }
2955 else // fallthrough for local-value
2956 return did_set_isopt(args);
2957
2958 return NULL;
2959}
2960
2961/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002962 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2963 * changed.
2964 */
2965 char *
2966did_set_isopt(optset_T *args)
2967{
Milly5e7a6a42024-10-22 22:27:19 +02002968 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002969 // If the new option is invalid, use old value.
2970 // 'lisp' option: refill g_chartab[] for '-' char.
2971 if (init_chartab() == FAIL)
2972 {
2973 args->os_restore_chartab = TRUE;// need to restore the chartab.
2974 return e_invalid_argument; // error in value
2975 }
2976
2977 return NULL;
2978}
2979
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002980/*
2981 * The 'jumpoptions' option is changed.
2982 */
2983 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002984did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002985{
2986 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2987 return e_invalid_argument;
2988
2989 return NULL;
2990}
2991
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002992 int
2993expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2994{
2995 return expand_set_opt_string(
2996 args,
2997 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002998 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002999 numMatches,
3000 matches);
3001}
3002
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003003#if defined(FEAT_KEYMAP) || defined(PROTO)
3004/*
3005 * The 'keymap' option is changed.
3006 */
3007 char *
3008did_set_keymap(optset_T *args)
3009{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003010 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003011 char *errmsg = NULL;
3012
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003013 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003014 errmsg = e_invalid_argument;
3015 else
3016 {
3017 int secure_save = secure;
3018
3019 // Reset the secure flag, since the value of 'keymap' has
3020 // been checked to be safe.
3021 secure = 0;
3022
3023 // load or unload key mapping tables
3024 errmsg = keymap_init();
3025
3026 secure = secure_save;
3027
3028 // Since we check the value, there is no need to set P_INSECURE,
3029 // even when the value comes from a modeline.
3030 args->os_value_checked = TRUE;
3031 }
3032
3033 if (errmsg == NULL)
3034 {
3035 if (*curbuf->b_p_keymap != NUL)
3036 {
3037 // Installed a new keymap, switch on using it.
3038 curbuf->b_p_iminsert = B_IMODE_LMAP;
3039 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
3040 curbuf->b_p_imsearch = B_IMODE_LMAP;
3041 }
3042 else
3043 {
3044 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
3045 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3046 curbuf->b_p_iminsert = B_IMODE_NONE;
3047 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
3048 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3049 }
3050 if ((args->os_flags & OPT_LOCAL) == 0)
3051 {
3052 set_iminsert_global();
3053 set_imsearch_global();
3054 }
3055 status_redraw_curbuf();
3056 }
3057
3058 return errmsg;
3059}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003060#endif
3061
3062/*
3063 * The 'keymodel' option is changed.
3064 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003065 char *
3066did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003067{
3068 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
3069 return e_invalid_argument;
3070
3071 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
3072 km_startsel = (vim_strchr(p_km, 'a') != NULL);
3073 return NULL;
3074}
3075
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003076 int
3077expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
3078{
3079 return expand_set_opt_string(
3080 args,
3081 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003082 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003083 numMatches,
3084 matches);
3085}
3086
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003087/*
3088 * The 'keyprotocol' option is changed.
3089 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003090 char *
3091did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003092{
Gregory Anders3695d0e2023-09-29 20:17:20 +02003093 char_u *term = T_NAME;
3094 keyprot_T kpc = match_keyprotocol(term);
3095 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003096 return e_invalid_argument;
3097
Gregory Anders3695d0e2023-09-29 20:17:20 +02003098 apply_keyprotocol(term, kpc);
3099
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003100 return NULL;
3101}
3102
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003103 int
3104expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
3105{
3106 expand_T *xp = args->oe_xp;
3107 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3108 {
3109 // 'keyprotocol' only has well-defined terms for completion for the
3110 // protocol part after the colon.
3111 return expand_set_opt_string(
3112 args,
3113 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003114 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003115 numMatches,
3116 matches);
3117 }
3118 // Use expand_set_opt_string instead of returning FAIL so that we can
3119 // include the original value if args->oe_include_orig_val is set.
3120 static char *(empty[]) = {NULL};
3121 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3122}
3123
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003124/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003125 * The 'lispoptions' option is changed.
3126 */
3127 char *
3128did_set_lispoptions(optset_T *args)
3129{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003130 char_u **varp = (char_u **)args->os_varp;
3131
3132 if (**varp != NUL
3133 && STRCMP(*varp, "expr:0") != 0
3134 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003135 return e_invalid_argument;
3136
3137 return NULL;
3138}
3139
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003140 int
3141expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3142{
3143 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3144 return expand_set_opt_string(
3145 args,
3146 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003147 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003148 numMatches,
3149 matches);
3150}
3151
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003152/*
3153 * The 'matchpairs' option is changed.
3154 */
3155 char *
3156did_set_matchpairs(optset_T *args)
3157{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003158 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003159 char_u *p;
3160
3161 if (has_mbyte)
3162 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003163 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003164 {
3165 int x2 = -1;
3166 int x3 = -1;
3167
3168 p += mb_ptr2len(p);
3169 if (*p != NUL)
3170 x2 = *p++;
3171 if (*p != NUL)
3172 {
3173 x3 = mb_ptr2char(p);
3174 p += mb_ptr2len(p);
3175 }
3176 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3177 return e_invalid_argument;
3178 if (*p == NUL)
3179 break;
3180 }
3181 }
3182 else
3183 {
3184 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003185 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003186 {
3187 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3188 return e_invalid_argument;
3189 if (p[3] == NUL)
3190 break;
3191 }
3192 }
3193
3194 return NULL;
3195}
3196
Christian Brabandt51d4d842024-12-06 17:26:25 +01003197/*
3198 * Process the updated 'messagesopt' option value.
3199 */
3200 char *
3201did_set_messagesopt(optset_T *args UNUSED)
3202{
3203 if (messagesopt_changed() == FAIL)
3204 return e_invalid_argument;
3205
3206 return NULL;
3207}
3208
3209 int
3210expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3211{
zeertzjq8cc43da2024-12-07 16:09:08 +01003212 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003213 return expand_set_opt_string(
3214 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003215 p_mopt_values,
3216 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003217 numMatches,
3218 matches);
3219}
3220
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003221#if defined(FEAT_SPELL) || defined(PROTO)
3222/*
3223 * The 'mkspellmem' option is changed.
3224 */
3225 char *
3226did_set_mkspellmem(optset_T *args UNUSED)
3227{
3228 if (spell_check_msm() != OK)
3229 return e_invalid_argument;
3230
3231 return NULL;
3232}
3233#endif
3234
3235/*
3236 * The 'mouse' option is changed.
3237 */
3238 char *
3239did_set_mouse(optset_T *args)
3240{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003241 char_u **varp = (char_u **)args->os_varp;
3242
Christian Brabandtb39b2402023-11-29 11:34:05 +01003243 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3244 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003245}
3246
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003247 int
3248expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3249{
3250 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3251}
3252
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003253/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003254 * The 'mousemodel' option is changed.
3255 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003256 char *
3257did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003258{
3259 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3260 return e_invalid_argument;
3261#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003262 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003263 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3264 // to create or delete the popup menus.
3265 gui_motif_update_mousemodel(root_menu);
3266#endif
3267
3268 return NULL;
3269}
3270
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003271 int
3272expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3273{
3274 return expand_set_opt_string(
3275 args,
3276 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003277 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003278 numMatches,
3279 matches);
3280}
3281
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003282#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3283 char *
3284did_set_mouseshape(optset_T *args UNUSED)
3285{
3286 char *errmsg = NULL;
3287
3288 errmsg = parse_shape_opt(SHAPE_MOUSE);
3289 update_mouseshape(-1);
3290
3291 return errmsg;
3292}
3293#endif
3294
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003295/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003296 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003297 */
3298 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003299did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003300{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003301 char_u **varp = (char_u **)args->os_varp;
3302
3303 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003304}
3305
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003306 int
3307expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3308{
3309 return expand_set_opt_string(
3310 args,
3311 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003312 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003313 numMatches,
3314 matches);
3315}
3316
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003317#if defined(FEAT_EVAL) || defined(PROTO)
3318/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003319 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003320 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3321 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003322 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003323 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003324did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003325{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003326 char_u **varp = (char_u **)args->os_varp;
3327
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003328 // If the option value starts with <SID> or s:, then replace that with
3329 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003330 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003331 if (name != NULL)
3332 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003333 free_string_option(*varp);
3334 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003335 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003336
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003337 return NULL;
3338}
3339#endif
3340
3341/*
3342 * The 'pastetoggle' option is changed.
3343 */
3344 char *
3345did_set_pastetoggle(optset_T *args UNUSED)
3346{
3347 char_u *p;
3348
3349 // translate key codes like in a mapping
3350 if (*p_pt)
3351 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003352 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003353 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3354 if (p != NULL)
3355 {
3356 free_string_option(p_pt);
3357 p_pt = p;
3358 }
3359 }
3360
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003361 return NULL;
3362}
3363
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003364#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3365/*
3366 * The 'previewpopup' option is changed.
3367 */
3368 char *
3369did_set_previewpopup(optset_T *args UNUSED)
3370{
3371 if (parse_previewpopup(NULL) == FAIL)
3372 return e_invalid_argument;
3373
3374 return NULL;
3375}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003376
3377 int
3378expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3379{
3380 expand_T *xp = args->oe_xp;
3381
3382 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3383 {
3384 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003385 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003386 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3387 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3388 {
3389 return expand_set_opt_string(
3390 args,
3391 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003392 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003393 numMatches,
3394 matches);
3395 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003396 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003397 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3398 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3399 {
3400 return expand_set_opt_string(
3401 args,
3402 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003403 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003404 numMatches,
3405 matches);
3406 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003407 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003408 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3409 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3410 {
3411 // Return the list of all highlight names
3412 return expand_set_opt_generic(
3413 args,
3414 get_highlight_name,
3415 numMatches,
3416 matches);
3417 }
3418 return FAIL;
3419 }
3420
3421 return expand_set_opt_string(
3422 args,
3423 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003424 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003425 numMatches,
3426 matches);
3427}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003428#endif
3429
3430#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3431/*
3432 * The 'printencoding' option is changed.
3433 */
3434 char *
3435did_set_printencoding(optset_T *args UNUSED)
3436{
3437 char_u *s, *p;
3438
3439 // Canonize 'printencoding' if VIM standard one
3440 p = enc_canonize(p_penc);
3441 if (p != NULL)
3442 {
3443 vim_free(p_penc);
3444 p_penc = p;
3445 }
3446 else
3447 {
3448 // Ensure lower case and '-' for '_'
3449 for (s = p_penc; *s != NUL; s++)
3450 {
3451 if (*s == '_')
3452 *s = '-';
3453 else
3454 *s = TOLOWER_ASC(*s);
3455 }
3456 }
3457
3458 return NULL;
3459}
3460#endif
3461
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003462#if defined(FEAT_PRINTER) || defined(PROTO)
3463
3464 static char_u *
3465get_printoptions_names(expand_T *xp UNUSED, int idx)
3466{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003467 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003468 return NULL;
3469 return (char_u*)printer_opts[idx].name;
3470}
3471
3472/*
3473 * Expand 'printoptions' option
3474 */
3475 int
3476expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3477{
3478 return expand_set_opt_generic(
3479 args,
3480 get_printoptions_names,
3481 numMatches,
3482 matches);
3483}
3484#endif
3485
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003486#if defined(FEAT_STL_OPT) || defined(PROTO)
3487/*
3488 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3489 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3490 */
3491 static char *
3492parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3493{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003494 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003495 char_u *s;
3496 char *errmsg = NULL;
3497 int wid;
3498
3499 if (rulerformat) // reset ru_wid first
3500 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003501 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003502 if (rulerformat && *s == '%')
3503 {
3504 // set ru_wid if 'ruf' starts with "%99("
3505 if (*++s == '-') // ignore a '-'
3506 s++;
3507 wid = getdigits(&s);
3508 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3509 ru_wid = wid;
3510 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003511 {
3512 // Validate the flags in 'rulerformat' only if it doesn't point to
3513 // a custom function ("%!" flag).
3514 if ((*varp)[1] != '!')
3515 errmsg = check_stl_option(p_ruf);
3516 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003517 }
3518 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3519 else if (rulerformat || s[0] != '%' || s[1] != '!')
3520 errmsg = check_stl_option(s);
3521 if (rulerformat && errmsg == NULL)
3522 comp_col();
3523
3524 return errmsg;
3525}
3526#endif
3527
3528#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3529/*
3530 * The 'renderoptions' option is changed.
3531 */
3532 char *
3533did_set_renderoptions(optset_T *args UNUSED)
3534{
3535 if (!gui_mch_set_rendering_options(p_rop))
3536 return e_invalid_argument;
3537
3538 return NULL;
3539}
3540#endif
3541
3542#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3543/*
3544 * The 'rightleftcmd' option is changed.
3545 */
3546 char *
3547did_set_rightleftcmd(optset_T *args)
3548{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003549 char_u **varp = (char_u **)args->os_varp;
3550
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003551 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003552 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003553 return e_invalid_argument;
3554
3555 return NULL;
3556}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003557
3558 int
3559expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3560{
3561 static char *(p_rlc_values[]) = {"search", NULL};
3562 return expand_set_opt_string(
3563 args,
3564 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003565 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003566 numMatches,
3567 matches);
3568}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003569#endif
3570
3571#if defined(FEAT_STL_OPT) || defined(PROTO)
3572/*
3573 * The 'rulerformat' option is changed.
3574 */
3575 char *
3576did_set_rulerformat(optset_T *args)
3577{
3578 return parse_statustabline_rulerformat(args, TRUE);
3579}
3580#endif
3581
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003582#if defined(FEAT_TABPANEL)
3583/*
3584 * Process the new 'tabpanelopt' option value.
3585 */
3586 char *
John Marriotta9657c82025-06-12 21:37:18 +02003587did_set_tabpanelopt(optset_T *args UNUSED)
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003588{
3589 if (tabpanelopt_changed() == FAIL)
3590 return e_invalid_argument;
3591
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003592 return NULL;
3593}
3594
3595 int
3596expand_set_tabpanelopt(optexpand_T *args, int *numMatches, char_u ***matches)
3597{
Hirohito Higashi598bbb12025-05-22 22:41:05 +02003598 expand_T *xp = args->oe_xp;
3599
3600 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3601 {
3602 // Within "align:", we have a subgroup of possible options.
3603 int align_len = (int)STRLEN("align:");
3604 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3605 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3606 {
3607 return expand_set_opt_string(
3608 args,
3609 p_tplo_align_values,
3610 ARRAY_LENGTH(p_tplo_align_values) - 1,
3611 numMatches,
3612 matches);
3613 }
3614 return FAIL;
3615 }
3616
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003617 return expand_set_opt_string(
3618 args,
Hirohito Higashi598bbb12025-05-22 22:41:05 +02003619 p_tplo_values,
3620 ARRAY_LENGTH(p_tplo_values) - 1,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003621 numMatches,
3622 matches);
3623}
3624#endif
3625
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003626/*
3627 * The 'scrollopt' option is changed.
3628 */
3629 char *
3630did_set_scrollopt(optset_T *args UNUSED)
3631{
3632 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3633}
3634
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003635 int
3636expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3637{
3638 return expand_set_opt_string(
3639 args,
3640 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003641 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003642 numMatches,
3643 matches);
3644}
3645
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003646/*
Foxe Chenb90c2392025-06-27 21:10:35 +02003647 * The 'wlseat' option is changed
3648 */
3649 char *
3650did_set_wlseat(optset_T *args UNUSED)
3651{
3652#ifdef FEAT_WAYLAND_CLIPBOARD
Hirohito Higashi73b96502025-06-28 18:18:21 +02003653 // If there isn't any seat named 'wlseat', then let the Wayland clipboard be
Foxe Chenb90c2392025-06-27 21:10:35 +02003654 // unavailable. Ignore errors returned.
3655 wayland_cb_reload();
3656#endif
3657
3658 return NULL;
3659}
3660
3661/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003662 * The 'selection' option is changed.
3663 */
3664 char *
3665did_set_selection(optset_T *args UNUSED)
3666{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003667 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003668 return e_invalid_argument;
3669
3670 return NULL;
3671}
3672
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003673 int
3674expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3675{
3676 return expand_set_opt_string(
3677 args,
3678 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003679 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003680 numMatches,
3681 matches);
3682}
3683
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003684/*
3685 * The 'selectmode' option is changed.
3686 */
3687 char *
3688did_set_selectmode(optset_T *args UNUSED)
3689{
3690 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3691}
3692
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003693 int
3694expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3695{
3696 return expand_set_opt_string(
3697 args,
3698 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003699 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003700 numMatches,
3701 matches);
3702}
3703
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003704#if defined(FEAT_SESSION) || defined(PROTO)
3705/*
3706 * The 'sessionoptions' option is changed.
3707 */
3708 char *
3709did_set_sessionoptions(optset_T *args)
3710{
3711 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3712 return e_invalid_argument;
3713 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3714 {
3715 // Don't allow both "sesdir" and "curdir".
3716 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3717 &ssop_flags, TRUE);
3718 return e_invalid_argument;
3719 }
3720
3721 return NULL;
3722}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003723
3724 int
3725expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3726{
3727 return expand_set_opt_string(
3728 args,
3729 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003730 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003731 numMatches,
3732 matches);
3733}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003734#endif
3735
3736/*
3737 * The 'shortmess' option is changed.
3738 */
3739 char *
3740did_set_shortmess(optset_T *args)
3741{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003742 char_u **varp = (char_u **)args->os_varp;
3743
Christian Brabandtb39b2402023-11-29 11:34:05 +01003744 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3745 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003746}
3747
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003748 int
3749expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3750{
3751 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3752}
3753
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003754#if defined(FEAT_LINEBREAK) || defined(PROTO)
3755/*
3756 * The 'showbreak' option is changed.
3757 */
3758 char *
3759did_set_showbreak(optset_T *args)
3760{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003761 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003762 char_u *s;
3763
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003764 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003765 {
3766 if (ptr2cells(s) != 1)
3767 return e_showbreak_contains_unprintable_or_wide_character;
3768 MB_PTR_ADV(s);
3769 }
3770
3771 return NULL;
3772}
3773#endif
3774
3775/*
3776 * The 'showcmdloc' option is changed.
3777 */
3778 char *
3779did_set_showcmdloc(optset_T *args UNUSED)
3780{
zeertzjqc27fcf42024-03-01 23:01:43 +01003781 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3782
3783 if (errmsg == NULL)
3784 comp_col();
3785
3786 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003787}
3788
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003789 int
3790expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3791{
3792 return expand_set_opt_string(
3793 args,
3794 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003795 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003796 numMatches,
3797 matches);
3798}
3799
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003800#if defined(FEAT_SIGNS) || defined(PROTO)
3801/*
3802 * The 'signcolumn' option is changed.
3803 */
3804 char *
3805did_set_signcolumn(optset_T *args)
3806{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003807 char_u **varp = (char_u **)args->os_varp;
3808
3809 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003810 return e_invalid_argument;
3811 // When changing the 'signcolumn' to or from 'number', recompute the
3812 // width of the number column if 'number' or 'relativenumber' is set.
3813 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3814 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3815 && (curwin->w_p_nu || curwin->w_p_rnu))
3816 curwin->w_nrwidth_line_count = 0;
3817
3818 return NULL;
3819}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003820
3821 int
3822expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3823{
3824 return expand_set_opt_string(
3825 args,
3826 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003827 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003828 numMatches,
3829 matches);
3830}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003831#endif
3832
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003833#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003834/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003835 * The 'spellcapcheck' option is changed.
3836 */
3837 char *
3838did_set_spellcapcheck(optset_T *args UNUSED)
3839{
3840 // compile the regexp program.
3841 return compile_cap_prog(curwin->w_s);
3842}
3843
3844/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003845 * The 'spellfile' option is changed.
3846 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003847 char *
3848did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003849{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003850 char_u **varp = (char_u **)args->os_varp;
3851
3852 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003853 return e_invalid_argument;
3854
3855 // If there is a window for this buffer in which 'spell' is set load the
3856 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003857 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003858}
3859
3860/*
3861 * The 'spell' option is changed.
3862 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003863 char *
3864did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003865{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003866 char_u **varp = (char_u **)args->os_varp;
3867
3868 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003869 return e_invalid_argument;
3870
3871 // If there is a window for this buffer in which 'spell' is set load the
3872 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003873 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003874}
3875
3876/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003877 * The 'spelloptions' option is changed.
3878 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003879 char *
3880did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003881{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003882 char_u **varp = (char_u **)args->os_varp;
3883
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003884 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003885 return e_invalid_argument;
3886
3887 return NULL;
3888}
3889
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003890 int
3891expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3892{
3893 static char *(p_spo_values[]) = {"camel", NULL};
3894 return expand_set_opt_string(
3895 args,
3896 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003897 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003898 numMatches,
3899 matches);
3900}
3901
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003902/*
3903 * The 'spellsuggest' option is changed.
3904 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003905 char *
3906did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003907{
3908 if (spell_check_sps() != OK)
3909 return e_invalid_argument;
3910
3911 return NULL;
3912}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003913
3914 int
3915expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3916{
3917 return expand_set_opt_string(
3918 args,
3919 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003920 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003921 numMatches,
3922 matches);
3923}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003924#endif
3925
3926/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003927 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003928 */
3929 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003930did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003931{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003932 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003933}
3934
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003935 int
3936expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3937{
3938 return expand_set_opt_string(
3939 args,
3940 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003941 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003942 numMatches,
3943 matches);
3944}
3945
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003946#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003947/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003948 * The 'statusline' option is changed.
3949 */
3950 char *
3951did_set_statusline(optset_T *args)
3952{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003953 return parse_statustabline_rulerformat(args, FALSE);
3954}
3955#endif
3956
3957/*
3958 * The 'swapsync' option is changed.
3959 */
3960 char *
3961did_set_swapsync(optset_T *args UNUSED)
3962{
3963 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003964}
3965
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003966 int
3967expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3968{
3969 return expand_set_opt_string(
3970 args,
3971 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003972 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003973 numMatches,
3974 matches);
3975}
3976
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003977/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003978 * The 'switchbuf' option is changed.
3979 */
3980 char *
3981did_set_switchbuf(optset_T *args UNUSED)
3982{
3983 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3984}
3985
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003986 int
3987expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3988{
3989 return expand_set_opt_string(
3990 args,
3991 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003992 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003993 numMatches,
3994 matches);
3995}
3996
LemonBoy5247b0b2024-07-12 19:30:58 +02003997/*
3998 * The 'tabclose' option is changed.
3999 */
4000 char *
4001did_set_tabclose(optset_T *args UNUSED)
4002{
4003 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
4004}
4005
4006 int
4007expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
4008{
4009 return expand_set_opt_string(
4010 args,
4011 p_tcl_values,
4012 ARRAY_LENGTH(p_tcl_values) - 1,
4013 numMatches,
4014 matches);
4015}
4016
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004017#if defined(FEAT_STL_OPT) || defined(PROTO)
4018/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004019 * The 'tabline' option is changed.
4020 */
4021 char *
4022did_set_tabline(optset_T *args)
4023{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004024 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004025}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004026#endif
4027
4028/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004029 * The 'tagcase' option is changed.
4030 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004031 char *
4032did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004033{
4034 unsigned int *flags;
4035 char_u *p;
4036
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004037 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004038 {
4039 p = curbuf->b_p_tc;
4040 flags = &curbuf->b_tc_flags;
4041 }
4042 else
4043 {
4044 p = p_tc;
4045 flags = &tc_flags;
4046 }
4047
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004048 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004049 // make the local value empty: use the global value
4050 *flags = 0;
4051 else if (*p == NUL
4052 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
4053 return e_invalid_argument;
4054
4055 return NULL;
4056}
4057
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004058 int
4059expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
4060{
4061 return expand_set_opt_string(
4062 args,
4063 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004064 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004065 numMatches,
4066 matches);
4067}
4068
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004069/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004070 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004071 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004072 char *
4073did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004074{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004075 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004076 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004077#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004078 if (gui.in_use)
4079 return e_cannot_change_term_in_GUI;
4080 if (term_is_gui(T_NAME))
4081 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004082#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004083 if (set_termname(T_NAME) == FAIL)
4084 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004085
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004086 // Screen colors may have changed.
4087 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004088
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004089 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004090}
4091
4092/*
4093 * Some terminal option (t_xxx) is changed
4094 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004095 char *
4096did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004097{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004098 char_u **varp = (char_u **)args->os_varp;
4099
4100 if (!full_screen)
4101 return NULL;
4102
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004103 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
4104 if (varp == &T_CCO)
4105 {
4106 int colors = atoi((char *)T_CCO);
4107
4108 // Only reinitialize colors if t_Co value has really changed to
4109 // avoid expensive reload of colorscheme if t_Co is set to the
4110 // same value multiple times.
4111 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004112 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004113 t_colors = colors;
4114 if (t_colors <= 1)
4115 {
4116 vim_free(T_CCO);
4117 T_CCO = empty_option;
4118 }
4119#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
4120 if (is_term_win32())
4121 {
4122 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004123 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004124 }
4125#endif
4126 // We now have a different color setup, initialize it again.
4127 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128 }
4129 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004130 ttest(FALSE);
4131 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004132 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004133 out_str(T_ME);
4134 redraw_later(UPD_CLEAR);
4135#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4136 // Since t_me has been set, this probably means that the user
4137 // wants to use this as default colors. Need to reset default
4138 // background/foreground colors.
4139# ifdef VIMDLL
4140 if (!gui.in_use && !gui.starting)
4141# endif
4142 mch_set_normal_colors();
4143#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004144 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004145 if (varp == &T_BE && termcap_active)
4146 {
4147 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004148
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004149 if (*T_BE == NUL)
4150 // When clearing t_BE we assume the user no longer wants
4151 // bracketed paste, thus disable it by writing t_BD.
4152 out_str(T_BD);
4153 else
4154 out_str(T_BE);
4155 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004156
4157 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004158}
4159
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004160#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004161/*
4162 * The 'termwinkey' option is changed.
4163 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004164 char *
Milly94606f72024-10-22 22:07:52 +02004165did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004166{
Milly94606f72024-10-22 22:07:52 +02004167 char_u **varp = (char_u **)args->os_varp;
4168
4169 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004170 return e_invalid_argument;
4171
4172 return NULL;
4173}
4174
4175/*
4176 * The 'termwinsize' option is changed.
4177 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004178 char *
Milly8be10aa2024-10-22 22:01:46 +02004179did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004180{
Milly8be10aa2024-10-22 22:01:46 +02004181 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004182 char_u *p;
4183
Milly8be10aa2024-10-22 22:01:46 +02004184 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004185 return NULL;
4186
Milly8be10aa2024-10-22 22:01:46 +02004187 p = skipdigits(*varp);
4188 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004189 return e_invalid_argument;
4190
4191 return NULL;
4192}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004193
4194# if defined(MSWIN) || defined(PROTO)
4195/*
4196 * The 'termwintype' option is changed.
4197 */
4198 char *
4199did_set_termwintype(optset_T *args UNUSED)
4200{
4201 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4202}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004203
4204 int
4205expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4206{
4207 return expand_set_opt_string(
4208 args,
4209 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004210 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004211 numMatches,
4212 matches);
4213}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004214# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004215#endif
4216
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004217/*
4218 * The 'titlestring' option is changed.
4219 */
4220 char *
4221did_set_titlestring(optset_T *args)
4222{
4223 int flagval = 0;
4224
4225#ifdef FEAT_STL_OPT
4226 flagval = STL_IN_TITLE;
4227#endif
4228 return parse_titleiconstring(args, flagval);
4229}
4230
4231#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4232/*
4233 * The 'toolbar' option is changed.
4234 */
4235 char *
4236did_set_toolbar(optset_T *args UNUSED)
4237{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004238 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4239 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004240 return e_invalid_argument;
4241
4242 out_flush();
4243 gui_mch_show_toolbar((toolbar_flags &
4244 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4245 return NULL;
4246}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004247
4248 int
4249expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4250{
4251 return expand_set_opt_string(
4252 args,
4253 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004254 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004255 numMatches,
4256 matches);
4257}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004258#endif
4259
4260#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4261/*
4262 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4263 */
4264 char *
4265did_set_toolbariconsize(optset_T *args UNUSED)
4266{
4267 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4268 return e_invalid_argument;
4269
4270 out_flush();
4271 gui_mch_show_toolbar((toolbar_flags &
4272 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4273 return NULL;
4274}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004275
4276 int
4277expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4278{
4279 return expand_set_opt_string(
4280 args,
4281 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004282 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004283 numMatches,
4284 matches);
4285}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004286#endif
4287
4288#if defined(UNIX) || defined(VMS) || defined(PROTO)
4289/*
4290 * The 'ttymouse' option is changed.
4291 */
4292 char *
4293did_set_ttymouse(optset_T *args UNUSED)
4294{
4295 char *errmsg = NULL;
4296
4297 // Switch the mouse off before changing the escape sequences used for
4298 // that.
4299 mch_setmouse(FALSE);
4300 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4301 errmsg = e_invalid_argument;
4302 else
4303 check_mouse_termcode();
4304 if (termcap_active)
4305 setmouse(); // may switch it on again
4306
4307 return errmsg;
4308}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004309
4310 int
4311expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4312{
4313 return expand_set_opt_string(
4314 args,
4315 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004316 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004317 numMatches,
4318 matches);
4319}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004320#endif
4321
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004322#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004323/*
4324 * The 'varsofttabstop' option is changed.
4325 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004326 char *
4327did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004328{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004329 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004330 char_u *cp;
4331
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004332 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004333 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004334 else
4335 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004336 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004337 {
4338 if (vim_isdigit(*cp))
4339 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004340 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004341 continue;
4342 return e_invalid_argument;
4343 }
4344
4345 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004346 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004347 {
4348 if (oldarray)
4349 vim_free(oldarray);
4350 }
4351 else
4352 return e_invalid_argument;
4353 }
4354
4355 return NULL;
4356}
4357
4358/*
4359 * The 'vartabstop' option is changed.
4360 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004361 char *
4362did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004363{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004364 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004365 char_u *cp;
4366
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004367 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004368 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004369 else
4370 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004371 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004372 {
4373 if (vim_isdigit(*cp))
4374 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004375 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004376 continue;
4377 return e_invalid_argument;
4378 }
4379
4380 int *oldarray = curbuf->b_p_vts_array;
4381
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004382 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004383 {
4384 vim_free(oldarray);
4385# ifdef FEAT_FOLDING
4386 if (foldmethodIsIndent(curwin))
4387 foldUpdateAll(curwin);
4388# endif
4389 }
4390 else
4391 return e_invalid_argument;
4392 }
4393
4394 return NULL;
4395}
4396#endif
4397
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004398/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004399 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004400 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004401 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004402did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004403{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004404 verbose_stop();
4405 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004406 return e_invalid_argument;
4407
4408 return NULL;
4409}
4410
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004411#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004412/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004413 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004414 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004415 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004416did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004417{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004418 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004419}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004420#endif
4421
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004422#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004423/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004424 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004425 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004426 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004427did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004428{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004429 char_u *s;
4430 char *errmsg = NULL;
4431
4432 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004433 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004434 // Check it's a valid character
4435 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4436 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004437 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004438 break;
4439 }
4440 if (*s == 'n') // name is always last one
4441 break;
4442 else if (*s == 'r') // skip until next ','
4443 {
4444 while (*++s && *s != ',')
4445 ;
4446 }
4447 else if (*s == '%')
4448 {
4449 // optional number
4450 while (vim_isdigit(*++s))
4451 ;
4452 }
4453 else if (*s == '!' || *s == 'h' || *s == 'c')
4454 ++s; // no extra chars
4455 else // must have a number
4456 {
4457 while (vim_isdigit(*++s))
4458 ;
4459
4460 if (!VIM_ISDIGIT(*(s - 1)))
4461 {
4462 if (args->os_errbuf != NULL)
4463 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004464 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004465 _(e_missing_number_after_angle_str_angle),
4466 transchar_byte(*(s - 1)));
4467 errmsg = args->os_errbuf;
4468 }
4469 else
4470 errmsg = "";
4471 break;
4472 }
4473 }
4474 if (*s == ',')
4475 ++s;
4476 else if (*s)
4477 {
4478 if (args->os_errbuf != NULL)
4479 errmsg = e_missing_comma;
4480 else
4481 errmsg = "";
4482 break;
4483 }
4484 }
4485 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4486 errmsg = e_must_specify_a_value;
4487
4488 return errmsg;
4489}
4490#endif
4491
4492/*
4493 * The 'virtualedit' option is changed.
4494 */
4495 char *
4496did_set_virtualedit(optset_T *args)
4497{
4498 char_u *ve = p_ve;
4499 unsigned int *flags = &ve_flags;
4500
4501 if (args->os_flags & OPT_LOCAL)
4502 {
4503 ve = curwin->w_p_ve;
4504 flags = &curwin->w_ve_flags;
4505 }
4506
4507 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4508 // make the local value empty: use the global value
4509 *flags = 0;
4510 else
4511 {
4512 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4513 return e_invalid_argument;
4514 else if (STRCMP(ve, args->os_oldval.string) != 0)
4515 {
4516 // Recompute cursor position in case the new 've' setting
4517 // changes something.
4518 validate_virtcol();
4519 coladvance(curwin->w_virtcol);
4520 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004521 }
4522
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004523 return NULL;
4524}
4525
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004526 int
4527expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4528{
4529 return expand_set_opt_string(
4530 args,
4531 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004532 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004533 numMatches,
4534 matches);
4535}
4536
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004537/*
4538 * The 'whichwrap' option is changed.
4539 */
4540 char *
4541did_set_whichwrap(optset_T *args)
4542{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004543 char_u **varp = (char_u **)args->os_varp;
4544
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004545 // Add ',' to the list flags because 'whichwrap' is a flag
4546 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004547 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4548 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004549}
4550
4551 int
4552expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4553{
4554 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004555}
4556
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004557/*
4558 * The 'wildmode' option is changed.
4559 */
4560 char *
4561did_set_wildmode(optset_T *args UNUSED)
4562{
4563 if (check_opt_wim() == FAIL)
4564 return e_invalid_argument;
4565 return NULL;
4566}
4567
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004568 int
4569expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4570{
4571 return expand_set_opt_string(
4572 args,
4573 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004574 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004575 numMatches,
4576 matches);
4577}
4578
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004579/*
4580 * The 'wildoptions' option is changed.
4581 */
4582 char *
4583did_set_wildoptions(optset_T *args UNUSED)
4584{
4585 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4586}
4587
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004588 int
4589expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4590{
4591 return expand_set_opt_string(
4592 args,
4593 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004594 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004595 numMatches,
4596 matches);
4597}
4598
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004599#if defined(FEAT_WAK) || defined(PROTO)
4600/*
4601 * The 'winaltkeys' option is changed.
4602 */
4603 char *
4604did_set_winaltkeys(optset_T *args UNUSED)
4605{
4606 char *errmsg = NULL;
4607
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004608 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004609 errmsg = e_invalid_argument;
4610# ifdef FEAT_MENU
4611# if defined(FEAT_GUI_MOTIF)
4612 else if (gui.in_use)
4613 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4614# elif defined(FEAT_GUI_GTK)
4615 else if (gui.in_use)
4616 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4617# endif
4618# endif
4619 return errmsg;
4620}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004621
4622 int
4623expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4624{
4625 return expand_set_opt_string(
4626 args,
4627 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004628 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004629 numMatches,
4630 matches);
4631}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004632#endif
4633
4634/*
4635 * The 'wincolor' option is changed.
4636 */
4637 char *
4638did_set_wincolor(optset_T *args UNUSED)
4639{
4640#ifdef FEAT_TERMINAL
4641 term_update_wincolor(curwin);
4642#endif
4643 return NULL;
4644}
4645
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004646 int
4647expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4648{
4649 return expand_set_opt_generic(
4650 args,
4651 get_highlight_name,
4652 numMatches,
4653 matches);
4654}
4655
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004656#ifdef FEAT_SYN_HL
4657/*
4658 * When the 'syntax' option is set, load the syntax of that name.
4659 */
4660 static void
4661do_syntax_autocmd(int value_changed)
4662{
4663 static int syn_recursive = 0;
4664
4665 ++syn_recursive;
4666 // Only pass TRUE for "force" when the value changed or not used
4667 // recursively, to avoid endless recurrence.
4668 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4669 value_changed || syn_recursive == 1, curbuf);
4670 curbuf->b_flags |= BF_SYN_SET;
4671 --syn_recursive;
4672}
4673#endif
4674
4675/*
4676 * When the 'filetype' option is set, trigger the FileType autocommand.
4677 */
4678 static void
4679do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4680{
4681 // Skip this when called from a modeline and the filetype was already set
4682 // to this value.
4683 if ((opt_flags & OPT_MODELINE) && !value_changed)
4684 return;
4685
4686 static int ft_recursive = 0;
4687 int secure_save = secure;
4688
4689 // Reset the secure flag, since the value of 'filetype' has
4690 // been checked to be safe.
4691 secure = 0;
4692
4693 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004694 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004695 // Only pass TRUE for "force" when the value changed or not
4696 // used recursively, to avoid endless recurrence.
4697 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4698 value_changed || ft_recursive == 1, curbuf);
4699 --ft_recursive;
4700 // Just in case the old "curbuf" is now invalid.
4701 if (varp != &(curbuf->b_p_ft))
4702 varp = NULL;
4703
4704 secure = secure_save;
4705}
4706
4707#ifdef FEAT_SPELL
4708/*
4709 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4710 * 'runtimepath'.
4711 */
4712 static void
4713do_spelllang_source(void)
4714{
4715 char_u fname[200];
4716 char_u *p;
4717 char_u *q = curwin->w_s->b_p_spl;
4718
4719 // Skip the first name if it is "cjk".
4720 if (STRNCMP(q, "cjk,", 4) == 0)
4721 q += 4;
4722
4723 // They could set 'spellcapcheck' depending on the language. Use the first
4724 // name in 'spelllang' up to '_region' or '.encoding'.
4725 for (p = q; *p != NUL; ++p)
4726 if (!ASCII_ISALNUM(*p) && *p != '-')
4727 break;
4728 if (p > q)
4729 {
4730 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4731 (int)(p - q), q);
4732 source_runtime(fname, DIP_ALL);
4733 }
4734}
4735#endif
4736
4737/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004738 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004739 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004740 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004741 */
4742 char *
4743did_set_string_option(
4744 int opt_idx, // index in options[] table
4745 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004746 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004747 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004748 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004749 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004750 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004751 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004752 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004753 // need to set P_INSECURE
4754{
4755 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004756 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004757 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004758 optset_T args;
4759
4760 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4761 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4762 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4763 if (varp == &T_NAME)
4764 {
4765 opt_idx = findoption((char_u *)"term");
4766 if (opt_idx >= 0)
4767 {
4768 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4769 did_set_cb = get_option_did_set_cb(opt_idx);
4770 }
4771 }
4772
4773 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004774
Bram Moolenaardac13472019-09-16 21:06:21 +02004775 // Disallow changing some options from secure mode
4776 if ((secure
4777#ifdef HAVE_SANDBOX
4778 || sandbox != 0
4779#endif
4780 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004781 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004782 // Check for a "normal" directory or file name in some options.
4783 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004784 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004785 else if (did_set_cb != NULL)
4786 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004787 args.os_varp = (char_u *)varp;
4788 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004789 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004790 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004791 args.os_oldval.string = oldval;
4792 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004793 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004794 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004795 // Invoke the option specific callback function to validate and apply
4796 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004797 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004798
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004799 // The 'keymap', 'filetype' and 'syntax' option callback functions
4800 // may change the os_value_checked field.
4801 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004802 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004803
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004804 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004805 if (errmsg != NULL)
4806 {
zeertzjqf6782732022-07-27 18:26:03 +01004807 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004808 *varp = oldval;
4809 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004810 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004811 (void)init_chartab();
4812 if (varp == &p_hl)
4813 (void)highlight_changed();
4814 }
4815 else
4816 {
4817#ifdef FEAT_EVAL
4818 // Remember where the option was set.
4819 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4820#endif
4821 // Free string options that are in allocated memory.
4822 // Use "free_oldval", because recursiveness may change the flags under
4823 // our fingers (esp. init_highlight()).
4824 if (free_oldval)
4825 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004826 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004827
4828 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4829 && is_global_local_option(opt_idx))
4830 {
4831 // global option with local value set to use global value; free
4832 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004833 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004834 free_string_option(*(char_u **)p);
4835 *(char_u **)p = empty_option;
4836 }
4837
4838 // May set global value for local option.
4839 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4840 set_string_option_global(opt_idx, varp);
4841
4842 // Trigger the autocommand only after setting the flags.
4843#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004844 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004845 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004846#endif
4847 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004848 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004849#ifdef FEAT_SPELL
4850 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004851 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004852#endif
4853 }
4854
Bram Moolenaardac13472019-09-16 21:06:21 +02004855 if (varp == &p_mouse)
4856 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004857 if (*p_mouse == NUL)
4858 mch_setmouse(FALSE); // switch mouse off
4859 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004860 setmouse(); // in case 'mouse' changed
4861 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004862
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004863 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004864 {
4865 export_myvimdir();
4866#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004867 update_package_paths_in_lua();
4868#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004869 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004870
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004871#if defined(FEAT_LINEBREAK)
4872 // Changing Formatlistpattern when briopt includes the list setting:
4873 // redraw
4874 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4875 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004876 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004877#endif
4878
Bram Moolenaardac13472019-09-16 21:06:21 +02004879 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004880 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4881 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004882 curwin->w_set_curswant = TRUE;
4883
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004884 if ((opt_flags & OPT_NO_REDRAW) == 0)
4885 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004886#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004887 // set when changing an option that only requires a redraw in the GUI
4888 int redraw_gui_only = FALSE;
4889
4890 if (varp == &p_go // 'guioptions'
4891 || varp == &p_guifont // 'guifont'
4892# ifdef FEAT_GUI_TABLINE
4893 || varp == &p_gtl // 'guitablabel'
4894 || varp == &p_gtt // 'guitabtooltip'
4895# endif
4896# ifdef FEAT_XFONTSET
4897 || varp == &p_guifontset // 'guifontset'
4898# endif
4899 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004900# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004901 || varp == &p_guiligatures // 'guiligatures'
4902# endif
4903 )
4904 redraw_gui_only = TRUE;
4905
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004906 // check redraw when it's not a GUI option or the GUI is active.
4907 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004908#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004909 check_redraw(get_option_flags(opt_idx));
4910 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004911
4912#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004913 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004914 {
4915 set_termname((char_u *)"win32");
4916 init_highlight(TRUE, FALSE);
4917 }
4918#endif
4919
4920 return errmsg;
4921}
4922
4923/*
4924 * Check an option that can be a range of string values.
4925 *
4926 * Return OK for correct value, FAIL otherwise.
4927 * Empty is always OK.
4928 */
4929 static int
4930check_opt_strings(
4931 char_u *val,
4932 char **values,
4933 int list) // when TRUE: accept a list of values
4934{
4935 return opt_strings_flags(val, values, NULL, list);
4936}
4937
4938/*
4939 * Handle an option that can be a range of string values.
4940 * Set a flag in "*flagp" for each string present.
4941 *
4942 * Return OK for correct value, FAIL otherwise.
4943 * Empty is always OK.
4944 */
4945 static int
4946opt_strings_flags(
4947 char_u *val, // new value
4948 char **values, // array of valid string values
4949 unsigned *flagp,
4950 int list) // when TRUE: accept a list of values
4951{
4952 int i;
4953 int len;
4954 unsigned new_flags = 0;
4955
4956 while (*val)
4957 {
4958 for (i = 0; ; ++i)
4959 {
4960 if (values[i] == NULL) // val not found in values[]
4961 return FAIL;
4962
4963 len = (int)STRLEN(values[i]);
4964 if (STRNCMP(values[i], val, len) == 0
4965 && ((list && val[len] == ',') || val[len] == NUL))
4966 {
4967 val += len + (val[len] == ',');
4968 new_flags |= (1 << i);
4969 break; // check next item in val list
4970 }
4971 }
4972 }
4973 if (flagp != NULL)
4974 *flagp = new_flags;
4975
4976 return OK;
4977}
4978
4979/*
4980 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4981 */
4982 int
4983check_ff_value(char_u *p)
4984{
4985 return check_opt_strings(p, p_ff_values, FALSE);
4986}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004987
4988/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004989 * Save the actual shortmess Flags and clear them temporarily to avoid that
4990 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004991 *
4992 * Caller must make sure to first call save_clear_shm_value() and then
4993 * restore_shm_value() exactly the same number of times.
4994 */
4995 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004996save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004997{
4998 if (STRLEN(p_shm) >= SHM_LEN)
4999 {
5000 iemsg(e_internal_error_shortmess_too_long);
5001 return;
5002 }
5003
5004 if (++set_shm_recursive == 1)
5005 {
5006 STRCPY(shm_buf, p_shm);
5007 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
5008 }
5009}
5010
5011/*
5012 * Restore the shortmess Flags set from the save_clear_shm_value() function.
5013 */
5014 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00005015restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00005016{
5017 if (--set_shm_recursive == 0)
5018 {
5019 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
5020 vim_memset(shm_buf, 0, SHM_LEN);
5021 }
5022}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01005023
5024/*
5025 * Export the environment variable $MYVIMDIR to the first item in runtimepath
5026 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01005027 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01005028export_myvimdir()
5029{
5030 int dofree = FALSE;
5031 char_u *p;
5032 char_u *q = p_rtp;
5033 char_u *buf = alloc(MAXPATHL);
5034
5035 if (buf == NULL)
5036 return;
5037
5038 (void)copy_option_part(&q, buf, MAXPATHL, ",");
5039
5040 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
5041
5042 if (p == NULL || STRCMP(p, buf) != 0)
5043 {
5044 add_pathsep(buf);
5045#ifdef MSWIN
5046 // normalize path separators
5047 for (q = buf; *q != NUL; q++)
5048 if (*q == '/')
5049 *q = '\\';
5050#endif
5051 vim_setenv((char_u *)"MYVIMDIR", buf);
5052 }
5053 if (dofree)
5054 vim_free(p);
5055 vim_free(buf);
5056}