blob: e82a79b6b0755b214f1f974a43f43dc58c729c0a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
John Marriott95dacbb2024-09-10 21:25:14 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, size_t newvallen, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Christian Brabandt757593c2023-08-22 21:44:10 +020071#if defined(FEAT_EVAL) || defined(PROTO)
72static char *(p_bin_dep_opts[]) = {"textwidth", "wrapmargin", "modeline", "expandtab", NULL};
73static char *(p_paste_dep_opts[]) = {"autoindent", "expandtab", "ruler", "showmatch", "smarttab",
74 "softtabstop", "textwidth", "wrapmargin",
75#ifdef FEAT_RIGHTLEFT
76 "hkmap", "revins",
77#endif
78#ifdef FEAT_VARTABS
79 "varsofttabstop",
80#endif
81 NULL};
82static void didset_options_sctx(int opt_flags, char **buf);
83#endif
84
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000087 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000089 static void
90set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
92 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000094 // Find default value for 'shell' option.
95 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010097#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010099 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200102#if defined(MSWIN)
103 {
104 // For MS-Windows put the path in quotes instead of escaping spaces.
105 char_u *cmd;
106 size_t len;
107
108 if (vim_strchr(p, ' ') != NULL)
109 {
110 len = STRLEN(p) + 3; // two quotes and a trailing NUL
111 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200112 if (cmd != NULL)
113 {
114 vim_snprintf((char *)cmd, len, "\"%s\"", p);
115 set_string_default("sh", cmd);
116 vim_free(cmd);
117 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200118 }
119 else
120 set_string_default("sh", p);
121 }
122#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100123 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200124#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127/*
128 * Set the default for 'backupskip' to include environment variables for
129 * temp files.
130 */
131 static void
132set_init_default_backupskip(void)
133{
134 int opt_idx;
John Marriott95dacbb2024-09-10 21:25:14 +0200135 int i;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000136 char_u *p;
John Marriott95dacbb2024-09-10 21:25:14 +0200137 int plen;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100138#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000139 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100140#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000141 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100142#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000143 garray_T ga;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200144
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000147 ga_init2(&ga, 1, 100);
John Marriott95dacbb2024-09-10 21:25:14 +0200148 for (i = 0; i < (int)ARRAY_LENGTH(names); ++i)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000149 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000150 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100151#ifdef UNIX
John Marriott95dacbb2024-09-10 21:25:14 +0200152 if (*names[i] == NUL)
153 {
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100154# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000155 p = (char_u *)"/private/tmp";
John Marriott95dacbb2024-09-10 21:25:14 +0200156 plen = (int)STRLEN_LITERAL("/private/tmp");
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100157# else
John Marriott95dacbb2024-09-10 21:25:14 +0200158 p = (char_u *)"/tmp";
159 plen = (int)STRLEN_LITERAL("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160# endif
John Marriott95dacbb2024-09-10 21:25:14 +0200161 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000162 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100163#endif
John Marriott95dacbb2024-09-10 21:25:14 +0200164 {
165 p = vim_getenv((char_u *)names[i], &mustfree);
166 plen = 0; // will be calculated below
167 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 {
John Marriott95dacbb2024-09-10 21:25:14 +0200170 char_u *item;
171 size_t itemsize;
172 int has_trailing_path_sep = FALSE;
173
174 if (plen == 0)
175 {
176 // the value was retrieved from the environment
177 plen = (int)STRLEN(p);
178 // does the value include a trailing path separator?
179 if (after_pathsep(p, p + plen))
180 has_trailing_path_sep = TRUE;
181 }
182
183 // item size needs to be large enough to include "/*" and a trailing NUL
184 // note: the value (and therefore plen) may already include a path separator
185 itemsize = plen + (has_trailing_path_sep ? 0 : 1) + 2;
186 item = alloc(itemsize);
Bram Moolenaar14338022023-03-15 22:05:44 +0000187 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000188 {
zeertzjq8feed3a2024-09-29 10:37:47 +0200189 // add a preceding comma as a separator after the first item
John Marriott95dacbb2024-09-10 21:25:14 +0200190 size_t itemseplen = (ga.ga_len == 0) ? 0 : 1;
191 size_t itemlen;
192
193 itemlen = vim_snprintf((char *)item, itemsize, "%s%s*", p, (has_trailing_path_sep) ? "" : PATHSEPSTR);
194
195 if (find_dup_item(ga.ga_data, item, itemlen, options[opt_idx].flags) == NULL
196 && ga_grow(&ga, itemseplen + itemlen + 1) == OK)
Bram Moolenaar14338022023-03-15 22:05:44 +0000197 {
John Marriott95dacbb2024-09-10 21:25:14 +0200198 ga.ga_len += vim_snprintf((char *)ga.ga_data + ga.ga_len,
199 itemseplen + itemlen + 1,
200 "%s%s", (itemseplen > 0) ? "," : "", item);
Bram Moolenaar14338022023-03-15 22:05:44 +0000201 }
202 vim_free(item);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000205 if (mustfree)
206 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000208 if (ga.ga_data != NULL)
209 {
210 set_string_default("bsk", ga.ga_data);
211 vim_free(ga.ga_data);
212 }
213}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000215/*
216 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
217 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
218 */
219 static void
220set_init_default_maxmemtot(void)
221{
222 int opt_idx;
223 long_u n;
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000226 if (opt_idx < 0)
227 return;
228
229#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
230 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 {
ichizok02560422022-04-05 14:18:44 +0100233#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000234 // Use amount of memory available at this moment.
235 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100236#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000237 // Use amount of memory available to Vim.
238 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100239#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000240 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000242 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
243 opt_idx = findoption((char_u *)"maxmem");
244 if (opt_idx >= 0)
245 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000246#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000247 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
248 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000249#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000250 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000253}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000255/*
256 * Initialize the 'cdpath' option to a default value.
257 */
258 static void
259set_init_default_cdpath(void)
260{
261 int opt_idx;
262 char_u *cdpath;
263 char_u *buf;
264 int i;
265 int j;
266 int mustfree = FALSE;
267
268 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
269 if (cdpath == NULL)
270 return;
271
272 buf = alloc((STRLEN(cdpath) << 1) + 2);
273 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000275 buf[0] = ','; // start with ",", current dir first
276 j = 1;
277 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000279 if (vim_ispathlistsep(cdpath[i]))
280 buf[j++] = ',';
281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000283 if (cdpath[i] == ' ' || cdpath[i] == ',')
284 buf[j++] = '\\';
285 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 }
287 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288 buf[j] = NUL;
289 opt_idx = findoption((char_u *)"cdpath");
290 if (opt_idx >= 0)
291 {
292 options[opt_idx].def_val[VI_DEFAULT] = buf;
293 options[opt_idx].flags |= P_DEF_ALLOCED;
294 }
295 else
296 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000298 if (mustfree)
299 vim_free(cdpath);
300}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000302/*
303 * Initialize the 'printencoding' option to a default value.
304 */
305 static void
306set_init_default_printencoding(void)
307{
ichizok02560422022-04-05 14:18:44 +0100308#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000309 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100310 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100312# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000313 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100314# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000315 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100316# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000317 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100318# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000319 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000321 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000323}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
325#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000326/*
327 * Initialize the 'printexpr' option to a default value.
328 */
329 static void
330set_init_default_printexpr(void)
331{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100332 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100334# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000335 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100336# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
338
ichizok02560422022-04-05 14:18:44 +0100339# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341# endif
342 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000343}
344#endif
345
346#ifdef UNIX
347/*
348 * Force restricted-mode on for "nologin" or "false" $SHELL
349 */
350 static void
351set_init_restricted_mode(void)
352{
353 char_u *p;
354
355 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000356 if (p == NULL)
357 return;
358 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
359 restricted = TRUE;
360 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000361}
362#endif
363
364#ifdef CLEAN_RUNTIMEPATH
365/*
366 * When Vim is started with the "--clean" argument, set the default value
367 * for the 'runtimepath' and 'packpath' options.
368 */
369 static void
370set_init_clean_rtp(void)
371{
372 int opt_idx;
373
374 opt_idx = findoption((char_u *)"runtimepath");
375 if (opt_idx >= 0)
376 {
377 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
378 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
379 }
380 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000381 if (opt_idx < 0)
382 return;
383 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
384 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000385}
386#endif
387
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200388#ifdef UNIX
389/*
390 * Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only
391 * vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'.
392 * In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used
393 * as a fallback as is defined in the XDG base dir specification:
394 * <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
395 */
396 static void
397set_init_xdg_rtp(void)
398{
399 int opt_idx;
400 int has_xdg_env = TRUE;
401 int should_free_xdg_dir = FALSE;
402 char_u *vimrc1 = NULL;
403 char_u *vimrc2 = NULL;
404 char_u *xdg_dir = NULL;
405 char_u *xdg_rtp = NULL;
406 char_u *vimrc_xdg = NULL;
407
Christian Brabandtb09fa352024-04-21 14:52:20 +0200408 // initialize chartab, so we can expand $HOME
409 (void)init_chartab();
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200410 vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE);
411 vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2);
412
413 xdg_dir = mch_getenv("XDG_CONFIG_HOME");
414 if (!xdg_dir)
415 {
416 xdg_dir = expand_env_save((char_u *)"~/.config");
417 should_free_xdg_dir = TRUE;
418 has_xdg_env = FALSE;
419 }
420 vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE);
421
422 if (file_is_readable(vimrc1) || file_is_readable(vimrc2) ||
423 !file_is_readable(vimrc_xdg))
424 goto theend;
425
426 xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH
427 : (char_u *)XDG_RUNTIMEPATH_FB;
428
429 if ((opt_idx = findoption((char_u *)"runtimepath")) < 0)
430 goto theend;
431
432 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
433 p_rtp = xdg_rtp;
434
435 if ((opt_idx = findoption((char_u *)"packpath")) < 0)
436 goto theend;
437
438 options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
439 p_pp = xdg_rtp;
440
Christian Brabandtc3e6e392024-05-04 09:48:15 +0200441#if defined(XDG_VDIR) && defined(FEAT_SESSION)
442 if ((opt_idx = findoption((char_u *)"viewdir")) < 0)
443 goto theend;
444
445 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)XDG_VDIR;
446 p_vdir = (char_u *)XDG_VDIR;
447#endif
448
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200449theend:
450 vim_free(vimrc1);
451 vim_free(vimrc2);
452 vim_free(vimrc_xdg);
453 if (should_free_xdg_dir)
454 vim_free(xdg_dir);
455}
456#endif
457
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000458/*
459 * Expand environment variables and things like "~" for the defaults.
460 * If option_expand() returns non-NULL the variable is expanded. This can
461 * only happen for non-indirect options.
462 * Also set the default to the expanded value, so ":set" does not list
463 * them.
464 * Don't set the P_ALLOCED flag, because we don't want to free the
465 * default.
466 */
467 static void
468set_init_expand_env(void)
469{
470 int opt_idx;
471 char_u *p;
472
473 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
474 {
475 if ((options[opt_idx].flags & P_GETTEXT)
476 && options[opt_idx].var != NULL)
477 p = (char_u *)_(*(char **)options[opt_idx].var);
478 else
479 p = option_expand(opt_idx, NULL);
480 if (p != NULL && (p = vim_strsave(p)) != NULL)
481 {
482 *(char_u **)options[opt_idx].var = p;
483 // VIMEXP
484 // Defaults for all expanded options are currently the same for Vi
485 // and Vim. When this changes, add some code here! Also need to
486 // split P_DEF_ALLOCED in two.
487 if (options[opt_idx].flags & P_DEF_ALLOCED)
488 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
489 options[opt_idx].def_val[VI_DEFAULT] = p;
490 options[opt_idx].flags |= P_DEF_ALLOCED;
491 }
492 }
493}
494
495/*
496 * Initialize the 'LANG' environment variable to a default value.
497 */
498 static void
499set_init_lang_env(void)
500{
501#if defined(MSWIN) && defined(FEAT_GETTEXT)
502 // If $LANG isn't set, try to get a good value for it. This makes the
503 // right language be used automatically. Don't do this for English.
504 if (mch_getenv((char_u *)"LANG") == NULL)
505 {
506 char buf[20];
507 long_u n;
508
509 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
510 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
511 // only the first two.
512 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
513 (LPTSTR)buf, 20);
514 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
515 {
516 // There are a few exceptions (probably more)
517 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
518 STRCPY(buf, "zh_TW");
519 else if (STRNICMP(buf, "chs", 3) == 0
520 || STRNICMP(buf, "zhc", 3) == 0)
521 STRCPY(buf, "zh_CN");
522 else if (STRNICMP(buf, "jp", 2) == 0)
523 STRCPY(buf, "ja");
524 else
525 buf[2] = NUL; // truncate to two-letter code
526 vim_setenv((char_u *)"LANG", (char_u *)buf);
527 }
528 }
529#elif defined(MACOS_CONVERT)
530 // Moved to os_mac_conv.c to avoid dependency problems.
531 mac_lang_init();
532#endif
533}
534
535/*
536 * Initialize the 'encoding' option to a default value.
537 */
538 static void
539set_init_default_encoding(void)
540{
541 char_u *p;
542 int opt_idx;
543
Igor Todorovski497e5282024-01-12 17:59:18 +0100544# if defined(MSWIN) || defined(__MVS__)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000545 // MS-Windows has builtin support for conversion to and from Unicode, using
546 // "utf-8" for 'encoding' should work best for most users.
Igor Todorovski497e5282024-01-12 17:59:18 +0100547 // z/OS built should default to UTF-8 mode as setlocale does not respect utf-8 environment variable locales
John Marriott95dacbb2024-09-10 21:25:14 +0200548 p = vim_strnsave((char_u *)ENC_DFLT, STRLEN_LITERAL(ENC_DFLT));
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000549# else
550 // enc_locale() will try to find the encoding of the current locale.
551 // This works best for properly configured systems, old and new.
552 p = enc_locale();
553# endif
554 if (p == NULL)
555 return;
556
557 // Try setting 'encoding' and check if the value is valid.
558 // If not, go back to the default encoding.
559 char_u *save_enc = p_enc;
560 p_enc = p;
561 if (STRCMP(p_enc, "gb18030") == 0)
562 {
563 // We don't support "gb18030", but "cp936" is a good substitute
564 // for practical purposes, thus use that. It's not an alias to
565 // still support conversion between gb18030 and utf-8.
John Marriott95dacbb2024-09-10 21:25:14 +0200566 p_enc = vim_strnsave((char_u *)"cp936", STRLEN_LITERAL("cp936"));
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000567 vim_free(p);
568 }
569 if (mb_init() == NULL)
570 {
571 opt_idx = findoption((char_u *)"encoding");
572 if (opt_idx >= 0)
573 {
574 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
575 options[opt_idx].flags |= P_DEF_ALLOCED;
576 }
577
578#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
579 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
580 {
581 // Adjust the default for 'isprint' and 'iskeyword' to match
582 // latin1. Also set the defaults for when 'nocompatible' is
583 // set.
584 set_string_option_direct((char_u *)"isp", -1,
585 ISP_LATIN1, OPT_FREE, SID_NONE);
586 set_string_option_direct((char_u *)"isk", -1,
587 ISK_LATIN1, OPT_FREE, SID_NONE);
588 opt_idx = findoption((char_u *)"isp");
589 if (opt_idx >= 0)
590 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
591 opt_idx = findoption((char_u *)"isk");
592 if (opt_idx >= 0)
593 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
594 (void)init_chartab();
595 }
596#endif
597
598#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
599 // Win32 console: When GetACP() returns a different value from
600 // GetConsoleCP() set 'termencoding'.
601 if (
602# ifdef VIMDLL
603 (!gui.in_use && !gui.starting) &&
604# endif
605 GetACP() != GetConsoleCP())
606 {
607 char buf[50];
John Marriott95dacbb2024-09-10 21:25:14 +0200608 size_t buflen;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000609
610 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
611 // Use an alternative value.
612 if (GetConsoleCP() == 0)
John Marriott95dacbb2024-09-10 21:25:14 +0200613 buflen = vim_snprintf(buf, sizeof(buf), "cp%ld", (long)GetACP());
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000614 else
John Marriott95dacbb2024-09-10 21:25:14 +0200615 buflen = vim_snprintf(buf, sizeof(buf), "cp%ld", (long)GetConsoleCP());
616 p_tenc = vim_strnsave((char_u *)buf, buflen);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000617 if (p_tenc != NULL)
618 {
619 opt_idx = findoption((char_u *)"termencoding");
620 if (opt_idx >= 0)
621 {
622 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
623 options[opt_idx].flags |= P_DEF_ALLOCED;
624 }
625 convert_setup(&input_conv, p_tenc, p_enc);
626 convert_setup(&output_conv, p_enc, p_tenc);
627 }
628 else
629 p_tenc = empty_option;
630 }
631#endif
632#if defined(MSWIN)
633 // $HOME may have characters in active code page.
634 init_homedir();
635#endif
636 }
637 else
638 {
639 vim_free(p_enc);
640 p_enc = save_enc;
641 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000642}
643
644/*
645 * Initialize the options, first part.
646 *
647 * Called only once from main(), just after creating the first buffer.
648 * If "clean_arg" is TRUE Vim was started with --clean.
649 */
650 void
651set_init_1(int clean_arg)
652{
653#ifdef FEAT_LANGMAP
654 langmap_init();
655#endif
656
657 // Be Vi compatible by default
658 p_cp = TRUE;
659
660 // Use POSIX compatibility when $VIM_POSIX is set.
661 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
662 {
663 set_string_default("cpo", (char_u *)CPO_ALL);
664 set_string_default("shm", (char_u *)SHM_POSIX);
665 }
666
667 set_init_default_shell();
668 set_init_default_backupskip();
669 set_init_default_maxmemtot();
670 set_init_default_cdpath();
671 set_init_default_printencoding();
672#ifdef FEAT_POSTSCRIPT
673 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#endif
675
676 /*
677 * Set all the options (except the terminal options) to their default
678 * value. Also set the global value for local options.
679 */
680 set_options_default(0);
681
matveytadbb1bf2022-02-01 17:26:12 +0000682#ifdef UNIX
Luca Saccarolac9df1fb2024-04-14 22:53:22 +0200683 set_init_xdg_rtp();
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000684 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000685#endif
686
Bram Moolenaar07268702018-03-01 21:57:32 +0100687#ifdef CLEAN_RUNTIMEPATH
688 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000689 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100690#endif
691
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#ifdef FEAT_GUI
693 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100694 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#endif
696
697 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100698 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100699 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 check_buf_options(curbuf);
701 check_win_options(curwin);
702 check_options();
703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 didset_options();
706
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000707#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100708 // Use the current chartab for the generic chartab. This is not in
709 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000710 init_spell_chartab();
711#endif
712
K.Takatace3189d2023-02-15 19:13:43 +0000713 set_init_default_encoding();
714
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000715 // Expand environment variables and things like "~" for the defaults.
716 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100718 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // Detect use of mlterm.
722 // Mlterm is a terminal emulator akin to xterm that has some special
723 // abilities (bidi namely).
724 // NOTE: mlterm's author is being asked to 'set' a variable
725 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100727 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#endif
729
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200730 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000732 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 set_helplang_default(get_mess_lang());
737#endif
738}
739
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200740static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
741
742/*
743 * Set the "fileencodings" option to the default value for when 'encoding' is
744 * utf-8.
745 */
746 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000747set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200748{
749 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
750 OPT_FREE, 0);
751}
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753/*
754 * Set an option to its default value.
755 * This does not take care of side effects!
756 */
757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100758set_option_default(
759 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100760 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
761 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100763 char_u *varp; // pointer to variable for current option
764 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000766 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
768
769 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
770 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {
773 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
774 if (flags & P_STRING)
775 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200776 // 'fencs' default value depends on 'encoding'
777 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
778 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100779 // Use set_string_option_direct() for local options to handle
780 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200781 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200782 set_string_option_direct(NULL, opt_idx,
783 options[opt_idx].def_val[dvi], opt_flags, 0);
784 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200786 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
787 free_string_option(*(char_u **)(varp));
788 *(char_u **)varp = options[opt_idx].def_val[dvi];
789 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 }
792 else if (flags & P_NUM)
793 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000794 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 win_comp_scroll(curwin);
796 else
797 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100798 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
799
800 if ((long *)varp == &curwin->w_p_so
801 || (long *)varp == &curwin->w_p_siso)
802 // 'scrolloff' and 'sidescrolloff' local values have a
803 // different default value than the global default.
804 *(long *)varp = -1;
805 else
806 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (both)
809 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100810 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100815 // the cast to long is required for Manx C, long_i is needed for
816 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000817 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000818#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000820 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
821 *(int *)varp = FALSE;
822#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100823 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (both)
825 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
826 *(int *)varp;
827 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000830 flagsp = insecure_flag(opt_idx, opt_flags);
831 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833
834#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200835 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#endif
837}
838
839/*
840 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200841 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 */
843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100844set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846{
847 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000849 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaardac13472019-09-16 21:06:21 +0200851 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200852 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200853 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100854 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200855# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200856 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200857 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200858# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100859 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 set_option_default(i, opt_flags, p_cp);
861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000863 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200865 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866}
867
868/*
869 * Set the Vi-default value of a string option.
870 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100871 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100873 static void
874set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
876 char_u *p;
877 int opt_idx;
878
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100879 if (escape && vim_strchr(val, ' ') != NULL)
880 p = vim_strsave_escaped(val, (char_u *)" ");
881 else
882 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000883 if (p == NULL) // we don't want a NULL
884 return;
885
886 opt_idx = findoption((char_u *)name);
887 if (opt_idx < 0)
Christian Brabandt29269a72024-04-16 22:44:31 +0200888 {
889 vim_free(p);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000890 return;
Christian Brabandt29269a72024-04-16 22:44:31 +0200891 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000892
893 if (options[opt_idx].flags & P_DEF_ALLOCED)
894 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
895 options[opt_idx].def_val[VI_DEFAULT] = p;
896 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897}
898
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100899 void
900set_string_default(char *name, char_u *val)
901{
902 set_string_default_esc(name, val, FALSE);
903}
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200906 * For an option value that contains comma separated items, find "newval" in
907 * "origval". Return NULL if not found.
908 */
909 static char_u *
John Marriott95dacbb2024-09-10 21:25:14 +0200910find_dup_item(char_u *origval, char_u *newval, size_t newvallen, long_u flags)
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200911{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200912 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200913 char_u *s;
914
915 if (origval == NULL)
916 return NULL;
917
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200918 for (s = origval; *s != NUL; ++s)
919 {
920 if ((!(flags & P_COMMA)
921 || s == origval
922 || (s[-1] == ',' && !(bs & 1)))
John Marriott95dacbb2024-09-10 21:25:14 +0200923 && STRNCMP(s, newval, newvallen) == 0
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200924 && (!(flags & P_COMMA)
John Marriott95dacbb2024-09-10 21:25:14 +0200925 || s[newvallen] == ','
926 || s[newvallen] == NUL))
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200927 return s;
928 // Count backslashes. Only a comma with an even number of backslashes
929 // or a single backslash preceded by a comma before it is recognized as
930 // a separator.
931 if ((s > origval + 1
932 && s[-1] == '\\'
933 && s[-2] != ',')
934 || (s == origval + 1
935 && s[-1] == '\\'))
936 ++bs;
937 else
938 bs = 0;
939 }
940 return NULL;
941}
942
943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 * Set the Vi-default value of a number option.
945 * Used for 'lines' and 'columns'.
946 */
947 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100948set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000950 int opt_idx;
951
952 opt_idx = findoption((char_u *)name);
953 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000954 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955}
956
Dominique Pelle748b3082022-01-08 12:41:16 +0000957#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200958/*
959 * Set all window-local and buffer-local options to the Vim default.
960 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200961 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200962 */
963 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200964set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200965{
966 win_T *save_curwin = curwin;
967 int i;
968
969 curwin = wp;
970 curbuf = curwin->w_buffer;
971 block_autocmds();
972
Bram Moolenaardac13472019-09-16 21:06:21 +0200973 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200974 {
975 struct vimoption *p = &(options[i]);
976 char_u *varp = get_varp_scope(p, OPT_LOCAL);
977
978 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200979 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200980 && !(options[i].flags & P_NODEFAULT)
981 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200982 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200983 }
984
985 unblock_autocmds();
986 curwin = save_curwin;
987 curbuf = curwin->w_buffer;
988}
Dominique Pelle748b3082022-01-08 12:41:16 +0000989#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200990
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000991#if defined(EXITFREE) || defined(PROTO)
992/*
993 * Free all options.
994 */
995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100996free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000997{
998 int i;
999
Bram Moolenaardac13472019-09-16 21:06:21 +02001000 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001001 {
1002 if (options[i].indir == PV_NONE)
1003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001004 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +01001005 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001006 free_string_option(*(char_u **)options[i].var);
1007 if (options[i].flags & P_DEF_ALLOCED)
1008 free_string_option(options[i].def_val[VI_DEFAULT]);
1009 }
1010 else if (options[i].var != VAR_WIN
1011 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +02001013 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001014 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00001015 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00001016 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001017}
1018#endif
1019
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021/*
1022 * Initialize the options, part two: After getting Rows and Columns and
1023 * setting 'term'.
1024 */
1025 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001026set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001028 int idx;
1029
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001030 // 'scroll' defaults to half the window height. The stored default is zero,
1031 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001032 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001033 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001034 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 comp_col();
1036
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001037 // 'window' is only for backwards compatibility with Vi.
1038 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +00001039 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001040 p_window = Rows - 1;
1041 set_number_default("window", Rows - 1);
1042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001043 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +01001044#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001045 // If 'background' wasn't set by the user, try guessing the value,
1046 // depending on the terminal name. Only need to check for terminals
1047 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001048 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001049 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
1050 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001052 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // don't mark it as set, when starting the GUI it may be
1054 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +00001055 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001058
1059#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001060 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001061#endif
1062#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001063 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +00001064#endif
1065#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001066 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +00001067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068}
1069
1070/*
1071 * Initialize the options, part three: After reading the .vimrc
1072 */
1073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001074set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar4f974752019-02-17 17:44:42 +01001076#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077/*
1078 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
1079 * This is done after other initializations, where 'shell' might have been
1080 * set, but only if they have not been set before.
1081 */
1082 char_u *p;
1083 int idx_srr;
1084 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001085# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 int idx_sp;
1087 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001091 if (idx_srr < 0)
1092 do_srr = FALSE;
1093 else
1094 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001095# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001097 if (idx_sp < 0)
1098 do_sp = FALSE;
1099 else
1100 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001101# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001102 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (p != NULL)
1104 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001105 // Default for p_sp is "| tee", for p_srr is ">".
1106 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if ( fnamecmp(p, "csh") == 0
1108 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001109# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 || fnamecmp(p, "csh.exe") == 0
1111 || fnamecmp(p, "tcsh.exe") == 0
1112# endif
1113 )
1114 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001115# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (do_sp)
1117 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001118# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001120# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1124 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (do_srr)
1127 {
1128 p_srr = (char_u *)">&";
1129 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1130 }
1131 }
Mike Williams12795022021-06-28 20:53:58 +02001132# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001133 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1134 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001135 else if ( fnamecmp(p, "powershell") == 0
1136 || fnamecmp(p, "powershell.exe") == 0
1137 )
1138 {
1139# if defined(FEAT_QUICKFIX)
1140 if (do_sp)
1141 {
1142 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1143 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1144 }
1145# endif
1146 if (do_srr)
1147 {
1148 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1149 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1150 }
1151 }
1152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 else
Natanael Copa56318362021-05-06 18:46:35 +02001154 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if ( fnamecmp(p, "sh") == 0
1156 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001157 || fnamecmp(p, "mksh") == 0
1158 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001160 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001162 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001163 || fnamecmp(p, "ash") == 0
1164 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001165 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001166# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 || fnamecmp(p, "cmd") == 0
1168 || fnamecmp(p, "sh.exe") == 0
1169 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001170 || fnamecmp(p, "mksh.exe") == 0
1171 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001173 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 || fnamecmp(p, "bash.exe") == 0
1175 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001176 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001177 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001179 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001181# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (do_sp)
1183 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001184# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001186# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001187 if (fnamecmp(p, "pwsh") == 0)
1188 p_sp = (char_u *)">%s 2>&1";
1189 else
1190 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1193 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (do_srr)
1196 {
1197 p_srr = (char_u *)">%s 2>&1";
1198 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1199 }
1200 }
1201 vim_free(p);
1202 }
1203#endif
1204
Bram Moolenaar4f974752019-02-17 17:44:42 +01001205#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001207 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1208 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001210 * set, but only if they have not been set before.
1211 * Default values depend on shell (cmd.exe is default shell):
1212 *
1213 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001214 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001215 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001216 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001217 * "sh" like shells - "-c" "\""
1218 *
1219 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Mike Williams12795022021-06-28 20:53:58 +02001221 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1222 {
1223 int idx_opt;
1224
1225 idx_opt = findoption((char_u *)"shcf");
1226 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1227 {
1228 p_shcf = (char_u*)"-Command";
1229 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1230 }
1231
1232 idx_opt = findoption((char_u *)"sxq");
1233 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1234 {
1235 p_sxq = (char_u*)"\"";
1236 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1237 }
1238 }
1239 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
1241 int idx3;
1242
1243 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001244 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 p_shcf = (char_u *)"-c";
1247 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1248 }
1249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001252 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
1254 p_sxq = (char_u *)"\"";
1255 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001258 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1259 {
1260 int idx3;
1261
1262 /*
1263 * cmd.exe on Windows will strip the first and last double quote given
1264 * on the command line, e.g. most of the time things like:
1265 * cmd /c "my path/to/echo" "my args to echo"
1266 * become:
1267 * my path/to/echo" "my args to echo
1268 * when executed.
1269 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001270 * To avoid this, set shellxquote to surround the command in
1271 * parenthesis. This appears to make most commands work, without
1272 * breaking commands that worked previously, such as
1273 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001274 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001275 idx3 = findoption((char_u *)"sxq");
1276 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1277 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001278 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001279 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1280 }
1281
Bram Moolenaara64ba222012-02-12 23:23:31 +01001282 idx3 = findoption((char_u *)"shcf");
1283 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1284 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001285 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001286 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1287 }
1288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289#endif
1290
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001291 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001292 {
1293 int idx_ffs = findoption((char_u *)"ffs");
1294
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001295 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001296 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1297 set_fileformat(default_fileformat(), OPT_LOCAL);
1298 }
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
1303#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1304/*
1305 * When 'helplang' is still at its default value, set it to "lang".
1306 * Only the first two characters of "lang" are used.
1307 */
1308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001309set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 int idx;
John Marriott95dacbb2024-09-10 21:25:14 +02001312 size_t langlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
John Marriott95dacbb2024-09-10 21:25:14 +02001314 if (lang == NULL) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 return;
John Marriott95dacbb2024-09-10 21:25:14 +02001316
1317 langlen = STRLEN(lang);
1318 if (langlen < 2) // safety check
1319 return;
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001322 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1323 return;
1324
1325 if (options[idx].flags & P_ALLOCED)
1326 free_string_option(p_hlg);
John Marriott95dacbb2024-09-10 21:25:14 +02001327 p_hlg = vim_strnsave(lang, langlen);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001328 if (p_hlg == NULL)
1329 p_hlg = empty_option;
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001332 // zh_CN becomes "cn", zh_TW becomes "tw"
John Marriott95dacbb2024-09-10 21:25:14 +02001333 if (STRNICMP(p_hlg, "zh_", 3) == 0 && langlen >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001334 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001335 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1336 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001337 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001338 // any C like setting, such as C.UTF-8, becomes "en"
John Marriott95dacbb2024-09-10 21:25:14 +02001339 else if (langlen >= 1 && *p_hlg == 'C')
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001340 {
1341 p_hlg[0] = 'e';
1342 p_hlg[1] = 'n';
1343 }
1344 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001346 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347}
1348#endif
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350/*
1351 * 'title' and 'icon' only default to true if they have not been set or reset
1352 * in .vimrc and we can read the old value.
1353 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1354 * they can be reset. This reduces startup time when using X on a remote
1355 * machine.
1356 */
1357 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001358set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359{
1360 int idx1;
1361 long val;
1362
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001363 // If GUI is (going to be) used, we can always set the window title and
1364 // icon name. Saves a bit of time, because the X11 display server does
1365 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001367 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369#ifdef FEAT_GUI
1370 if (gui.starting || gui.in_use)
1371 val = TRUE;
1372 else
1373#endif
1374 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001375 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 p_title = val;
1377 }
1378 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001379 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001381 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001383
1384#ifdef FEAT_GUI
1385 if (gui.starting || gui.in_use)
1386 val = TRUE;
1387 else
1388#endif
1389 val = mch_can_restore_icon();
1390 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1391 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001394 void
1395ex_set(exarg_T *eap)
1396{
1397 int flags = 0;
1398
1399 if (eap->cmdidx == CMD_setlocal)
1400 flags = OPT_LOCAL;
1401 else if (eap->cmdidx == CMD_setglobal)
1402 flags = OPT_GLOBAL;
1403#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001404 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001405 ex_options(eap);
1406 else
1407#endif
1408 {
1409 if (eap->forceit)
1410 flags |= OPT_ONECOLUMN;
1411 (void)do_set(eap->arg, flags);
1412 }
1413}
1414
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001415/*
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001416 * :set boolean option prefix
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001417 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001418typedef enum {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001419 PREFIX_NO = 0, // "no" prefix
1420 PREFIX_NONE, // no prefix
1421 PREFIX_INV, // "inv" prefix
1422} set_prefix_T;
1423
1424/*
1425 * Return the prefix type for the option name in *argp.
1426 */
1427 static set_prefix_T
1428get_option_prefix(char_u **argp)
1429{
1430 int prefix = PREFIX_NONE;
1431 char_u *arg = *argp;
1432
1433 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1434 {
1435 prefix = PREFIX_NO;
1436 arg += 2;
1437 }
1438 else if (STRNCMP(arg, "inv", 3) == 0)
1439 {
1440 prefix = PREFIX_INV;
1441 arg += 3;
1442 }
1443
1444 *argp = arg;
1445 return prefix;
1446}
1447
1448/*
1449 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1450 * and the option name length in "*lenp". For a <t_xx> option, return the key
1451 * number in "*keyp".
1452 *
1453 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1454 * otherwise returns OK.
1455 */
1456 static int
1457parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1458{
1459 int key = 0;
1460 int len;
1461 int opt_idx;
1462
1463 if (*arg == '<')
1464 {
1465 opt_idx = -1;
1466 // look out for <t_>;>
1467 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1468 len = 5;
1469 else
1470 {
1471 len = 1;
1472 while (arg[len] != NUL && arg[len] != '>')
1473 ++len;
1474 }
1475 if (arg[len] != '>')
1476 return FAIL;
1477
1478 arg[len] = NUL; // put NUL after name
1479 if (arg[1] == 't' && arg[2] == '_') // could be term code
1480 opt_idx = findoption(arg + 1);
1481 arg[len++] = '>'; // restore '>'
1482 if (opt_idx == -1)
1483 key = find_key_option(arg + 1, TRUE);
1484 }
1485 else
1486 {
1487 int nextchar; // next non-white char after option name
1488
1489 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001490 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001491 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1492 len = 4;
1493 else
1494 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1495 ++len;
1496 nextchar = arg[len];
1497 arg[len] = NUL; // put NUL after name
1498 opt_idx = findoption(arg);
1499 arg[len] = nextchar; // restore nextchar
1500 if (opt_idx == -1)
1501 key = find_key_option(arg, FALSE);
1502 }
1503
1504 *keyp = key;
1505 *lenp = len;
1506 *opt_idxp = opt_idx;
1507
1508 return OK;
1509}
1510
1511/*
1512 * Get the option operator (+=, ^=, -=).
1513 */
1514 static set_op_T
1515get_opt_op(char_u *arg)
1516{
1517 set_op_T op = OP_NONE;
1518
1519 if (*arg != NUL && *(arg + 1) == '=')
1520 {
1521 if (*arg == '+')
1522 op = OP_ADDING; // "+="
1523 else if (*arg == '^')
1524 op = OP_PREPENDING; // "^="
1525 else if (*arg == '-')
1526 op = OP_REMOVING; // "-="
1527 }
1528
1529 return op;
1530}
1531
1532/*
1533 * Validate whether the value of the option in "opt_idx" can be changed.
1534 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1535 * if it can be changed.
1536 */
1537 static int
1538validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1539{
1540 // Skip all options that are not window-local (used when showing
1541 // an already loaded buffer in a window).
1542 if ((opt_flags & OPT_WINONLY)
1543 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1544 return FAIL;
1545
1546 // Skip all options that are window-local (used for :vimgrep).
1547 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1548 && options[opt_idx].var == VAR_WIN)
1549 return FAIL;
1550
1551 // Disallow changing some options from modelines.
1552 if (opt_flags & OPT_MODELINE)
1553 {
1554 if (flags & (P_SECURE | P_NO_ML))
1555 {
1556 *errmsg = e_not_allowed_in_modeline;
1557 return FAIL;
1558 }
1559 if ((flags & P_MLE) && !p_mle)
1560 {
1561 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1562 return FAIL;
1563 }
1564#ifdef FEAT_DIFF
1565 // In diff mode some options are overruled. This avoids that
1566 // 'foldmethod' becomes "marker" instead of "diff" and that
1567 // "wrap" gets set.
1568 if (curwin->w_p_diff
1569 && opt_idx >= 0 // shut up coverity warning
1570 && (
1571# ifdef FEAT_FOLDING
1572 options[opt_idx].indir == PV_FDM ||
1573# endif
1574 options[opt_idx].indir == PV_WRAP))
1575 return FAIL;
1576#endif
1577 }
1578
1579#ifdef HAVE_SANDBOX
1580 // Disallow changing some options in the sandbox
1581 if (sandbox != 0 && (flags & P_SECURE))
1582 {
1583 *errmsg = e_not_allowed_in_sandbox;
1584 return FAIL;
1585 }
1586#endif
1587
1588 return OK;
1589}
1590
Bram Moolenaar47403942022-09-21 21:12:53 +01001591/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001592 * Get the Vim/Vi default value for a string option.
1593 */
1594 static char_u *
1595stropt_get_default_val(
1596 int opt_idx,
1597 char_u *varp,
1598 int flags,
1599 int cp_val)
1600{
1601 char_u *newval;
1602
1603 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1604 ? VI_DEFAULT : VIM_DEFAULT];
1605 if ((char_u **)varp == &p_bg)
1606 {
1607 // guess the value of 'background'
1608#ifdef FEAT_GUI
1609 if (gui.in_use)
1610 newval = gui_bg_default();
1611 else
1612#endif
1613 newval = term_bg_default();
1614 }
1615 else if ((char_u **)varp == &p_fencs && enc_utf8)
1616 newval = fencs_utf8_default;
1617
1618 // expand environment variables and ~ since the default value was
1619 // already expanded, only required when an environment variable was set
1620 // later
1621 if (newval == NULL)
1622 newval = empty_option;
1623 else
1624 {
1625 char_u *s = option_expand(opt_idx, newval);
1626 if (s == NULL)
1627 s = newval;
1628 newval = vim_strsave(s);
1629 }
1630
1631 return newval;
1632}
1633
1634/*
1635 * Convert the 'backspace' option number value to a string: for adding,
1636 * prepending and removing string.
1637 */
1638 static void
1639opt_backspace_nr2str(
1640 char_u *varp,
1641 char_u **origval_p,
1642 char_u **origval_l_p,
1643 char_u **origval_g_p,
1644 char_u **oldval_p)
1645{
1646 int i = getdigits((char_u **)varp);
1647
1648 switch (i)
1649 {
1650 case 0:
1651 *(char_u **)varp = empty_option;
1652 break;
1653 case 1:
John Marriott95dacbb2024-09-10 21:25:14 +02001654 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol", STRLEN_LITERAL("indent,eol"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001655 break;
1656 case 2:
John Marriott95dacbb2024-09-10 21:25:14 +02001657 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol,start", STRLEN_LITERAL("indent,eol,start"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001658 break;
1659 case 3:
John Marriott95dacbb2024-09-10 21:25:14 +02001660 *(char_u **)varp = vim_strnsave((char_u *)"indent,eol,nostop", STRLEN_LITERAL("indent,eol,nostop"));
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001661 break;
1662 }
1663 vim_free(*oldval_p);
1664 if (*origval_p == *oldval_p)
1665 *origval_p = *(char_u **)varp;
1666 if (*origval_l_p == *oldval_p)
1667 *origval_l_p = *(char_u **)varp;
1668 if (*origval_g_p == *oldval_p)
1669 *origval_g_p = *(char_u **)varp;
1670 *oldval_p = *(char_u **)varp;
1671}
1672
1673/*
1674 * Convert the 'whichwrap' option number value to a string, for backwards
1675 * compatibility with Vim 3.0.
1676 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1677 */
1678 static char_u *
1679opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1680{
John Marriott95dacbb2024-09-10 21:25:14 +02001681 size_t len = 0;
1682
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001683 *whichwrap = NUL;
1684 int i = getdigits(argp);
1685 if (i & 1)
John Marriott95dacbb2024-09-10 21:25:14 +02001686 {
1687 STRCPY(whichwrap, "b,");
1688 len += 2;
1689 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001690 if (i & 2)
John Marriott95dacbb2024-09-10 21:25:14 +02001691 {
1692 STRCPY(whichwrap + len, "s,");
1693 len += 2;
1694 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001695 if (i & 4)
John Marriott95dacbb2024-09-10 21:25:14 +02001696 {
1697 STRCPY(whichwrap + len, "h,l,");
1698 len += 4;
1699 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001700 if (i & 8)
John Marriott95dacbb2024-09-10 21:25:14 +02001701 {
1702 STRCPY(whichwrap + len, "<,>,");
1703 len += 4;
1704 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001705 if (i & 16)
John Marriott95dacbb2024-09-10 21:25:14 +02001706 {
1707 STRCPY(whichwrap + len, "[,],");
1708 len += 4;
1709 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001710 if (*whichwrap != NUL) // remove trailing ,
John Marriott95dacbb2024-09-10 21:25:14 +02001711 whichwrap[len - 1] = NUL;
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001712
1713 return whichwrap;
1714}
1715
1716/*
1717 * Copy the new string value into allocated memory for the option.
1718 * Can't use set_string_option_direct(), because we need to remove the
1719 * backslashes.
1720 */
1721 static char_u *
1722stropt_copy_value(
1723 char_u *origval,
1724 char_u **argp,
1725 set_op_T op,
1726 int flags UNUSED)
1727{
1728 char_u *arg = *argp;
1729 unsigned newlen;
1730 char_u *newval;
1731 char_u *s = NULL;
1732
1733 // get a bit too much
1734 newlen = (unsigned)STRLEN(arg) + 1;
1735 if (op != OP_NONE)
1736 newlen += (unsigned)STRLEN(origval) + 1;
1737 newval = alloc(newlen);
1738 if (newval == NULL) // out of mem, don't change
1739 return NULL;
1740 s = newval;
1741
1742 // Copy the string, skip over escaped chars.
1743 // For MS-DOS and WIN32 backslashes before normal file name characters
1744 // are not removed, and keep backslash at start, for "\\machine\path",
1745 // but do remove it for "\\\\machine\\path".
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001746 // The reverse is found in escape_option_str_cmdline().
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001747 while (*arg != NUL && !VIM_ISWHITE(*arg))
1748 {
1749 int i;
1750
1751 if (*arg == '\\' && arg[1] != NUL
1752#ifdef BACKSLASH_IN_FILENAME
1753 && !((flags & P_EXPAND)
1754 && vim_isfilec(arg[1])
1755 && !VIM_ISWHITE(arg[1])
1756 && (arg[1] != '\\'
1757 || (s == newval && arg[2] != '\\')))
1758#endif
1759 )
1760 ++arg; // remove backslash
1761 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1762 {
1763 // copy multibyte char
1764 mch_memmove(s, arg, (size_t)i);
1765 arg += i;
1766 s += i;
1767 }
1768 else
1769 *s++ = *arg++;
1770 }
1771 *s = NUL;
1772
1773 *argp = arg;
1774 return newval;
1775}
1776
1777/*
1778 * Expand environment variables and ~ in string option value 'newval'.
1779 */
1780 static char_u *
1781stropt_expand_envvar(
1782 int opt_idx,
1783 char_u *origval,
1784 char_u *newval,
1785 set_op_T op)
1786{
1787 char_u *s = option_expand(opt_idx, newval);
1788 if (s == NULL)
1789 return newval;
1790
1791 vim_free(newval);
1792 unsigned newlen = (unsigned)STRLEN(s) + 1;
1793 if (op != OP_NONE)
1794 newlen += (unsigned)STRLEN(origval) + 1;
1795
1796 newval = alloc(newlen);
1797 if (newval == NULL)
1798 return NULL;
1799
1800 STRCPY(newval, s);
1801
1802 return newval;
1803}
1804
1805/*
1806 * Concatenate the original and new values of a string option, adding a "," if
1807 * needed.
1808 */
1809 static void
1810stropt_concat_with_comma(
1811 char_u *origval,
1812 char_u *newval,
1813 set_op_T op,
1814 int flags)
1815{
1816 int len = 0;
1817
1818 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1819 if (op == OP_ADDING)
1820 {
1821 len = (int)STRLEN(origval);
1822 // strip a trailing comma, would get 2
1823 if (comma && len > 1
1824 && (flags & P_ONECOMMA) == P_ONECOMMA
1825 && origval[len - 1] == ','
1826 && origval[len - 2] != '\\')
1827 len--;
1828 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1829 mch_memmove(newval, origval, (size_t)len);
1830 }
1831 else
1832 {
1833 len = (int)STRLEN(newval);
1834 STRMOVE(newval + len + comma, origval);
1835 }
1836 if (comma)
1837 newval[len] = ',';
1838}
1839
1840/*
1841 * Remove a value from a string option. Copy string option value in "origval"
1842 * to "newval" and then remove the string "strval" of length "len".
1843 */
1844 static void
1845stropt_remove_val(
1846 char_u *origval,
1847 char_u *newval,
1848 int flags,
1849 char_u *strval,
1850 int len)
1851{
1852 // Remove newval[] from origval[]. (Note: "len" has been set above
1853 // and is used here).
1854 STRCPY(newval, origval);
1855 if (*strval)
1856 {
1857 // may need to remove a comma
1858 if (flags & P_COMMA)
1859 {
1860 if (strval == origval)
1861 {
1862 // include comma after string
1863 if (strval[len] == ',')
1864 ++len;
1865 }
1866 else
1867 {
1868 // include comma before string
1869 --strval;
1870 ++len;
1871 }
1872 }
1873 STRMOVE(newval + (strval - origval), strval + len);
1874 }
1875}
1876
1877/*
1878 * Remove flags that appear twice in the string option value 'newval'.
1879 */
1880 static void
1881stropt_remove_dupflags(char_u *newval, int flags)
1882{
1883 char_u *s = newval;
1884
1885 // Remove flags that appear twice.
1886 while (*s)
1887 {
1888 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1889 if (flags & P_ONECOMMA)
1890 {
1891 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1892 {
1893 // Remove the duplicated value and the next comma.
1894 STRMOVE(s, s + 2);
1895 continue;
1896 }
1897 }
1898 else
1899 {
1900 if ((!(flags & P_COMMA) || *s != ',')
1901 && vim_strchr(s + 1, *s) != NULL)
1902 {
1903 STRMOVE(s, s + 1);
1904 continue;
1905 }
1906 }
1907 ++s;
1908 }
1909}
1910
1911/*
1912 * Get the string value specified for a ":set" command. The following set
1913 * options are supported:
1914 * set {opt}&
1915 * set {opt}<
1916 * set {opt}={val}
1917 * set {opt}:{val}
1918 */
1919 static char_u *
1920stropt_get_newval(
1921 int nextchar,
1922 int opt_idx,
1923 char_u **argp,
1924 char_u *varp,
1925 char_u **origval_arg,
1926 char_u **origval_l_arg,
1927 char_u **origval_g_arg,
1928 char_u **oldval_arg,
1929 set_op_T *op_arg,
1930 int flags,
1931 int cp_val)
1932{
1933 char_u *arg = *argp;
1934 char_u *origval = *origval_arg;
1935 char_u *origval_l = *origval_l_arg;
1936 char_u *origval_g = *origval_g_arg;
1937 char_u *oldval = *oldval_arg;
1938 set_op_T op = *op_arg;
1939 char_u *save_arg = NULL;
1940 char_u *newval;
1941 char_u *s = NULL;
1942 char_u whichwrap[80];
1943
1944 if (nextchar == '&') // set to default val
1945 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1946 else if (nextchar == '<') // set to global val
1947 newval = vim_strsave(*(char_u **)get_varp_scope(
1948 &(options[opt_idx]), OPT_GLOBAL));
1949 else
1950 {
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001951 ++arg; // jump to after the '=' or ':'
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001952
1953 // Set 'keywordprg' to ":help" if an empty
1954 // value was passed to :set by the user.
1955 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1956 {
1957 save_arg = arg;
1958 arg = (char_u *)":help";
1959 }
1960 // Convert 'backspace' number to string
1961 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1962 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1963 &oldval);
1964 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1965 {
1966 // Convert 'whichwrap' number to string, for backwards
1967 // compatibility with Vim 3.0.
1968 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1969 save_arg = arg;
1970 arg = t;
1971 }
1972 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1973 // version 3.0
1974 else if (*arg == '>' && (varp == (char_u *)&p_dir
1975 || varp == (char_u *)&p_bdir))
1976 ++arg;
1977
1978 // Copy the new string into allocated memory.
1979 newval = stropt_copy_value(origval, &arg, op, flags);
1980 if (newval == NULL)
1981 goto done;
1982
1983 // Expand environment variables and ~.
1984 // Don't do it when adding without inserting a comma.
1985 if (op == OP_NONE || (flags & P_COMMA))
1986 {
1987 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1988 if (newval == NULL)
1989 goto done;
1990 }
1991
1992 // locate newval[] in origval[] when removing it and when adding to
1993 // avoid duplicates
1994 int len = 0;
1995 if (op == OP_REMOVING || (flags & P_NODUP))
1996 {
1997 len = (int)STRLEN(newval);
John Marriott95dacbb2024-09-10 21:25:14 +02001998 s = find_dup_item(origval, newval, len, flags);
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001999
2000 // do not add if already there
2001 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
2002 {
2003 op = OP_NONE;
2004 STRCPY(newval, origval);
2005 }
2006
2007 // if no duplicate, move pointer to end of original value
2008 if (s == NULL)
2009 s = origval + (int)STRLEN(origval);
2010 }
2011
2012 // concatenate the two strings; add a ',' if needed
2013 if (op == OP_ADDING || op == OP_PREPENDING)
2014 stropt_concat_with_comma(origval, newval, op, flags);
2015 else if (op == OP_REMOVING)
2016 // Remove newval[] from origval[]. (Note: "len" has been set above
2017 // and is used here).
2018 stropt_remove_val(origval, newval, flags, s, len);
2019
2020 if (flags & P_FLAGLIST)
2021 // Remove flags that appear twice.
2022 stropt_remove_dupflags(newval, flags);
2023 }
2024
2025done:
2026 if (save_arg != NULL)
2027 arg = save_arg; // arg was temporarily changed, restore it
2028 *argp = arg;
2029 *origval_arg = origval;
2030 *origval_l_arg = origval_l;
2031 *origval_g_arg = origval_g;
2032 *oldval_arg = oldval;
2033 *op_arg = op;
2034
2035 return newval;
2036}
2037
2038/*
Bram Moolenaar47403942022-09-21 21:12:53 +01002039 * Part of do_set() for string options.
2040 * Returns FAIL on failure, do not process further options.
2041 */
2042 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002043do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01002044 int opt_idx,
2045 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01002046 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01002047 int nextchar,
2048 set_op_T op_arg,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02002049 long_u flags,
Bram Moolenaar47403942022-09-21 21:12:53 +01002050 int cp_val,
2051 char_u *varp_arg,
2052 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +01002053 size_t errbuflen,
Bram Moolenaar47403942022-09-21 21:12:53 +01002054 int *value_checked,
2055 char **errmsg)
2056{
Bram Moolenaar6f981142022-09-22 12:48:58 +01002057 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01002058 set_op_T op = op_arg;
2059 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002060 char_u *oldval = NULL; // previous value if *varp
2061 char_u *newval;
2062 char_u *origval = NULL;
2063 char_u *origval_l = NULL;
2064 char_u *origval_g = NULL;
2065#if defined(FEAT_EVAL)
2066 char_u *saved_origval = NULL;
2067 char_u *saved_origval_l = NULL;
2068 char_u *saved_origval_g = NULL;
2069 char_u *saved_newval = NULL;
2070#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01002071
2072 // When using ":set opt=val" for a global option
2073 // with a local value the local value will be
2074 // reset, use the global value here.
2075 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2076 && ((int)options[opt_idx].indir & PV_BOTH))
2077 varp = options[opt_idx].var;
2078
2079 // The old value is kept until we are sure that the new value is valid.
2080 oldval = *(char_u **)varp;
2081
2082 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2083 {
2084 origval_l = *(char_u **)get_varp_scope(
2085 &(options[opt_idx]), OPT_LOCAL);
2086 origval_g = *(char_u **)get_varp_scope(
2087 &(options[opt_idx]), OPT_GLOBAL);
2088
2089 // A global-local string option might have an empty option as value to
2090 // indicate that the global value should be used.
2091 if (((int)options[opt_idx].indir & PV_BOTH)
2092 && origval_l == empty_option)
2093 origval_l = origval_g;
2094 }
2095
2096 // When setting the local value of a global option, the old value may be
2097 // the global value.
2098 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
2099 origval = *(char_u **)get_varp(&options[opt_idx]);
2100 else
2101 origval = oldval;
2102
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002103 // Get the new value for the option
2104 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
2105 &origval_l, &origval_g, &oldval, &op, flags,
2106 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01002107
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002108 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01002109 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00002110 if (newval == NULL)
2111 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01002112
2113#if defined(FEAT_EVAL)
2114 if (!starting
2115# ifdef FEAT_CRYPT
2116 && options[opt_idx].indir != PV_KEY
2117# endif
2118 && origval != NULL && newval != NULL)
2119 {
2120 // origval may be freed by did_set_string_option(), make a copy.
2121 saved_origval = vim_strsave(origval);
2122 // newval (and varp) may become invalid if the buffer is closed by
2123 // autocommands.
2124 saved_newval = vim_strsave(newval);
2125 if (origval_l != NULL)
2126 saved_origval_l = vim_strsave(origval_l);
2127 if (origval_g != NULL)
2128 saved_origval_g = vim_strsave(origval_g);
2129 }
2130#endif
2131
2132 {
2133 long_u *p = insecure_flag(opt_idx, opt_flags);
2134 int secure_saved = secure;
2135
2136 // When an option is set in the sandbox, from a modeline or in secure
2137 // mode, then deal with side effects in secure mode. Also when the
2138 // value was set with the P_INSECURE flag and is not completely
2139 // replaced.
2140 if ((opt_flags & OPT_MODELINE)
2141#ifdef HAVE_SANDBOX
2142 || sandbox != 0
2143#endif
2144 || (op != OP_NONE && (*p & P_INSECURE)))
2145 secure = 1;
2146
2147 // Handle side effects, and set the global value for ":set" on local
2148 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2149 // be triggered that can cause havoc.
2150 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002151 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002152 errbuflen, opt_flags, op, value_checked);
Bram Moolenaar47403942022-09-21 21:12:53 +01002153
2154 secure = secure_saved;
2155 }
2156
2157#if defined(FEAT_EVAL)
2158 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002159 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002160 saved_origval_l, saved_origval_g, saved_newval);
2161 vim_free(saved_origval);
2162 vim_free(saved_origval_l);
2163 vim_free(saved_origval_g);
2164 vim_free(saved_newval);
2165#endif
2166
Bram Moolenaar6f981142022-09-22 12:48:58 +01002167 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002168 return *errmsg == NULL ? OK : FAIL;
2169}
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002172 * Set a boolean option.
2173 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002174 */
2175 static char *
2176do_set_option_bool(
2177 int opt_idx,
2178 int opt_flags,
2179 set_prefix_T prefix,
2180 long_u flags,
2181 char_u *varp,
2182 int nextchar,
2183 int afterchar,
2184 int cp_val)
2185
2186{
2187 varnumber_T value;
2188
2189 if (nextchar == '=' || nextchar == ':')
2190 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002191 if (opt_idx < 0 || varp == NULL)
2192 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002193
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002194 // ":set opt!": invert
2195 // ":set opt&": reset to default value
2196 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002197 if (nextchar == '!')
2198 value = *(int *)(varp) ^ 1;
2199 else if (nextchar == '&')
2200 value = (int)(long)(long_i)options[opt_idx].def_val[
2201 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2202 else if (nextchar == '<')
2203 {
2204 // For 'autoread' -1 means to use global value.
2205 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2206 value = -1;
2207 else
2208 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2209 }
2210 else
2211 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002212 // ":set invopt": invert
2213 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002214 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2215 return e_trailing_characters;
2216 if (prefix == PREFIX_INV)
2217 value = *(int *)(varp) ^ 1;
2218 else
2219 value = prefix == PREFIX_NO ? 0 : 1;
2220 }
2221
2222 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2223}
2224
2225/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002226 * Set a numeric option.
2227 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002228 */
2229 static char *
2230do_set_option_numeric(
2231 int opt_idx,
2232 int opt_flags,
2233 char_u **argp,
2234 int nextchar,
2235 set_op_T op,
2236 long_u flags,
2237 int cp_val,
2238 char_u *varp,
2239 char *errbuf,
2240 size_t errbuflen)
2241{
2242 char_u *arg = *argp;
2243 varnumber_T value;
2244 int i;
2245 char *errmsg = NULL;
2246
Bram Moolenaar546933f2023-02-06 16:40:49 +00002247 if (opt_idx < 0 || varp == NULL)
2248 return NULL; // "cannot happen"
2249 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002250 /*
2251 * Different ways to set a number option:
2252 * & set to default value
2253 * < set to global value
Milly40c6bab2024-10-04 20:41:14 +02002254 * <xx> accept special key codes for 'wildchar' or 'wildcharm'
2255 * ^x accept ctrl key codes for 'wildchar' or 'wildcharm'
2256 * c accept any non-digit for 'wildchar' or 'wildcharm'
2257 * [-]0-9 set number
2258 * other error
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002259 */
2260 ++arg;
2261 if (nextchar == '&')
2262 value = (long)(long_i)options[opt_idx].def_val[
2263 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2264 else if (nextchar == '<')
2265 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002266 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002267 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002268 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002269 else if (opt_flags == OPT_LOCAL
2270 && ((long *)varp == &curwin->w_p_siso
2271 || (long *)varp == &curwin->w_p_so))
2272 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2273 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002274 else
2275 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2276 }
2277 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2278 && (*arg == '<'
2279 || *arg == '^'
2280 || (*arg != NUL
2281 && (!arg[1] || VIM_ISWHITE(arg[1]))
2282 && !VIM_ISDIGIT(*arg))))
2283 {
2284 value = string_to_key(arg, FALSE);
Milly40c6bab2024-10-04 20:41:14 +02002285 if (value == 0)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002286 {
2287 errmsg = e_invalid_argument;
2288 goto skip;
2289 }
2290 }
2291 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2292 {
2293 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002294 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002295 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2296 {
2297 errmsg = e_number_required_after_equal;
2298 goto skip;
2299 }
2300 }
2301 else
2302 {
2303 errmsg = e_number_required_after_equal;
2304 goto skip;
2305 }
2306
2307 if (op == OP_ADDING)
2308 value = *(long *)varp + value;
2309 else if (op == OP_PREPENDING)
2310 value = *(long *)varp * value;
2311 else if (op == OP_REMOVING)
2312 value = *(long *)varp - value;
2313
2314 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2315 opt_flags);
2316
2317skip:
2318 *argp = arg;
2319 return errmsg;
2320}
2321
2322/*
2323 * Set a key code (t_xx) option
2324 */
2325 static char *
2326do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2327{
2328 char_u *arg = *argp;
2329 char_u *p;
2330
2331 if (nextchar == '&')
2332 {
2333 if (add_termcap_entry(key_name, TRUE) == FAIL)
2334 return e_not_found_in_termcap;
2335 }
2336 else
2337 {
2338 ++arg; // jump to after the '=' or ':'
2339 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2340 if (*p == '\\' && p[1] != NUL)
2341 ++p;
2342 nextchar = *p;
2343 *p = NUL;
2344 add_termcode(key_name, arg, FALSE);
2345 *p = nextchar;
2346 }
2347 if (full_screen)
2348 ttest(FALSE);
2349 redraw_all_later(UPD_CLEAR);
2350
2351 *argp = arg;
2352 return NULL;
2353}
2354
2355/*
2356 * Set an option to a new value.
2357 */
2358 static char *
2359do_set_option_value(
2360 int opt_idx,
2361 int opt_flags,
2362 char_u **argp,
2363 set_prefix_T prefix,
2364 set_op_T op,
2365 long_u flags,
2366 char_u *varp,
2367 char_u *key_name,
2368 int nextchar,
2369 int afterchar,
2370 int cp_val,
2371 int *stopopteval,
2372 char *errbuf,
2373 size_t errbuflen)
2374{
2375 int value_checked = FALSE;
2376 char *errmsg = NULL;
2377 char_u *arg = *argp;
2378
2379 if (flags & P_BOOL)
2380 {
2381 // boolean option
2382 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2383 nextchar, afterchar, cp_val);
2384 if (errmsg != NULL)
2385 goto skip;
2386 }
2387 else
2388 {
2389 // numeric or string option
2390 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2391 || prefix != PREFIX_NONE)
2392 {
2393 errmsg = e_invalid_argument;
2394 goto skip;
2395 }
2396
2397 if (flags & P_NUM)
2398 {
2399 // numeric option
2400 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2401 op, flags, cp_val, varp,
2402 errbuf, errbuflen);
2403 if (errmsg != NULL)
2404 goto skip;
2405 }
2406 else if (opt_idx >= 0)
2407 {
2408 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002409 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002410 flags, cp_val, varp, errbuf, errbuflen,
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002411 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002412 {
2413 if (errmsg != NULL)
2414 goto skip;
2415 *stopopteval = TRUE;
2416 goto skip;
2417 }
2418 }
2419 else
2420 {
2421 // key code option
2422 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2423 if (errmsg != NULL)
2424 goto skip;
2425 }
2426 }
2427
2428 if (opt_idx >= 0)
2429 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2430
2431skip:
2432 *argp = arg;
2433 return errmsg;
2434}
2435
2436/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002437 * Set an option to a new value.
2438 * Return NULL if OK, return an untranslated error message when something is
2439 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2440 */
2441 static char *
2442do_set_option(
2443 int opt_flags,
2444 char_u **argp,
2445 char_u *arg_start,
2446 char_u **startarg,
2447 int *did_show,
2448 int *stopopteval,
2449 char *errbuf,
2450 size_t errbuflen)
2451{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002452 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002453 char_u *arg;
2454 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2455 set_op_T op;
2456 long_u flags; // flags for current option
2457 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002458 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002459 int nextchar; // next non-white char after option name
2460 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002461 char *errmsg = NULL;
2462 int key;
2463 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002464
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002465 prefix = get_option_prefix(argp);
2466 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002467
2468 // find end of name
2469 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002470 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2471 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002472
2473 // remember character after option name
2474 afterchar = arg[len];
2475
2476 if (in_vim9script())
2477 {
2478 char_u *p = skipwhite(arg + len);
2479
2480 // disallow white space before =val, +=val, -=val, ^=val
2481 if (p > arg + len && (p[0] == '='
2482 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2483 && p[1] == '=')))
2484 {
2485 errmsg = e_no_white_space_allowed_between_option_and;
2486 arg = p;
2487 *startarg = p;
2488 goto skip;
2489 }
2490 }
2491 else
2492 // skip white space, allow ":set ai ?", ":set hlsearch !"
2493 while (VIM_ISWHITE(arg[len]))
2494 ++len;
2495
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002496 op = get_opt_op(arg + len);
2497 if (op != OP_NONE)
2498 len++;
2499
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002500 nextchar = arg[len];
2501
2502 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2503 {
2504 if (in_vim9script() && arg > arg_start
2505 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2506 errmsg = e_no_white_space_allowed_between_option_and;
2507 else
2508 errmsg = e_unknown_option;
2509 goto skip;
2510 }
2511
2512 if (opt_idx >= 0)
2513 {
2514 if (options[opt_idx].var == NULL) // hidden option: skip
2515 {
2516 // Only give an error message when requesting the value of
2517 // a hidden option, ignore setting it.
2518 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2519 && (!(options[opt_idx].flags & P_BOOL)
2520 || nextchar == '?'))
2521 errmsg = e_option_not_supported;
2522 goto skip;
2523 }
2524
2525 flags = options[opt_idx].flags;
2526 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2527 }
2528 else
2529 {
2530 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002531 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002532 if (key < 0)
2533 {
2534 key_name[0] = KEY2TERMCAP0(key);
2535 key_name[1] = KEY2TERMCAP1(key);
2536 }
2537 else
2538 {
2539 key_name[0] = KS_KEY;
2540 key_name[1] = (key & 0xff);
2541 }
2542 }
2543
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002544 // Make sure the option value can be changed.
2545 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002546 goto skip;
2547
Bram Moolenaar40b48722023-02-05 17:04:50 +00002548 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002549 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2550 {
2551 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002552 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2553 {
2554 if (arg[3] == 'm') // "opt&vim": set to Vim default
2555 {
2556 cp_val = FALSE;
2557 arg += 3;
2558 }
2559 else // "opt&vi": set to Vi default
2560 {
2561 cp_val = TRUE;
2562 arg += 2;
2563 }
2564 }
2565 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2566 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2567 {
2568 errmsg = e_trailing_characters;
2569 goto skip;
2570 }
2571 }
2572
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002573 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2574 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002575 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002576 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002577 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2578 && !(flags & P_BOOL)))
2579 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002580 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002581 if (*did_show)
2582 msg_putchar('\n'); // cursor below last one
2583 else
2584 {
2585 gotocmdline(TRUE); // cursor at status line
2586 *did_show = TRUE; // remember that we did a line
2587 }
2588 if (opt_idx >= 0)
2589 {
2590 showoneopt(&options[opt_idx], opt_flags);
2591#ifdef FEAT_EVAL
2592 if (p_verbose > 0)
2593 {
2594 // Mention where the option was last set.
2595 if (varp == options[opt_idx].var)
2596 last_set_msg(options[opt_idx].script_ctx);
2597 else if ((int)options[opt_idx].indir & PV_WIN)
2598 last_set_msg(curwin->w_p_script_ctx[
2599 (int)options[opt_idx].indir & PV_MASK]);
2600 else if ((int)options[opt_idx].indir & PV_BUF)
2601 last_set_msg(curbuf->b_p_script_ctx[
2602 (int)options[opt_idx].indir & PV_MASK]);
2603 }
2604#endif
2605 }
2606 else
2607 {
2608 char_u *p;
2609
2610 p = find_termcode(key_name);
2611 if (p == NULL)
2612 {
2613 errmsg = e_key_code_not_set;
2614 goto skip;
2615 }
2616 else
2617 (void)show_one_termcode(key_name, p, TRUE);
2618 }
2619 if (nextchar != '?'
2620 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2621 errmsg = e_trailing_characters;
2622 }
2623 else
2624 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002625 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2626 flags, varp, key_name, nextchar,
2627 afterchar, cp_val, stopopteval, errbuf,
2628 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002629 }
2630
2631skip:
2632 *argp = arg;
2633 return errmsg;
2634}
2635
2636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 * Parse 'arg' for option settings.
2638 *
2639 * 'arg' may be IObuff, but only when no errors can be present and option
2640 * does not need to be expanded with option_expand().
2641 * "opt_flags":
2642 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002643 * OPT_GLOBAL for ":setglobal"
2644 * OPT_LOCAL for ":setlocal" and a modeline
2645 * OPT_MODELINE for a modeline
2646 * OPT_WINONLY to only set window-local options
2647 * OPT_NOWIN to skip setting window-local options
2648 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002650 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 */
2652 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002653do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002654 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002655 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002657 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 if (*arg == NUL)
2662 {
2663 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002664 did_show = TRUE;
2665 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002668 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002670 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002671 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002673 // ":set all" show all options.
2674 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 arg += 3;
2676 if (*arg == '&')
2677 {
2678 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002679 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002681 didset_options();
2682 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002683 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002688 did_show = TRUE;
2689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002691 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002694 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002695 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 arg += 7;
2697 }
2698 else
2699 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002700 int stopopteval = FALSE;
2701 char *errmsg = NULL;
Christian Brabandtb39b2402023-11-29 11:34:05 +01002702 char errbuf[ERR_BUFLEN];
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002703 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002705 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2706 &did_show, &stopopteval, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +01002707 ERR_BUFLEN);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002708 if (stopopteval)
2709 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002711 // Advance to next argument.
2712 // - skip until a blank found, taking care of backslashes
2713 // - skip blanks
2714 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 for (i = 0; i < 2 ; ++i)
2716 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002717 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (*arg++ == '\\' && *arg != NUL)
2719 ++arg;
2720 arg = skipwhite(arg);
2721 if (*arg != '=')
2722 break;
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002725 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
John Marriott95dacbb2024-09-10 21:25:14 +02002727 i = vim_snprintf((char *)IObuff, IOSIZE, "%s", (char_u *)_(errmsg)) + 2;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002728 if (i + (arg - startarg) < IOSIZE)
2729 {
2730 // append the argument with the error
John Marriott95dacbb2024-09-10 21:25:14 +02002731 STRCPY(IObuff + i - 2, ": ");
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002732 mch_memmove(IObuff + i, startarg, (arg - startarg));
2733 IObuff[i + (arg - startarg)] = NUL;
2734 }
2735 // make sure all characters are printable
2736 trans_characters(IObuff, IOSIZE);
2737
2738 ++no_wait_return; // wait_return() done later
2739 emsg((char *)IObuff); // show error highlighted
2740 --no_wait_return;
2741
2742 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
2745
2746 arg = skipwhite(arg);
2747 }
2748
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749theend:
2750 if (silent_mode && did_show)
2751 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002755 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757 out_flush();
2758 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 }
2761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 return OK;
2763}
2764
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002765/*
2766 * Call this when an option has been given a new value through a user command.
2767 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2768 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002770did_set_option(
2771 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002772 int opt_flags, // possibly with OPT_MODELINE
2773 int new_value, // value was replaced completely
2774 int value_checked) // value was checked to be safe, no need to set the
2775 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002776{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002777 long_u *p;
2778
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002779 options[opt_idx].flags |= P_WAS_SET;
2780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // When an option is set in the sandbox, from a modeline or in secure mode
2782 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2783 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002784 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002785 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002786#ifdef HAVE_SANDBOX
2787 || sandbox != 0
2788#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002789 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002790 *p = *p | P_INSECURE;
2791 else if (new_value)
2792 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002793}
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795/*
2796 * Convert a key name or string into a key value.
Millya9c6f902024-10-06 16:47:02 +02002797 * Used for 'cedit', 'termwinkey', 'wildchar' and 'wildcharm' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002798 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002800 int
2801string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
Millya9c6f902024-10-06 16:47:02 +02002803 if (*arg == '<' && arg[1])
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002804 return find_key_option(arg + 1, TRUE);
Millya9c6f902024-10-06 16:47:02 +02002805 if (*arg == '^' && arg[1])
2806 {
2807 int key = Ctrl_chr(arg[1]);
2808 if (key == 0) // ^@ is <Nul>
2809 key = K_ZERO;
2810 return key;
2811 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002812 if (multi_byte)
2813 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return *arg;
2815}
2816
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817/*
2818 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2819 * maketitle() to create and display it.
2820 * When switching the title or icon off, call mch_restore_title() to get
2821 * the old value back.
2822 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002823 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002824did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
2826 if (starting != NO_SCREEN
2827#ifdef FEAT_GUI
2828 && !gui.starting
2829#endif
2830 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834/*
2835 * set_options_bin - called when 'bin' changes value.
2836 */
2837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002838set_options_bin(
2839 int oldval,
2840 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002843 // The option values that are changed when 'bin' changes are
2844 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (newval)
2846 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 if (!(opt_flags & OPT_GLOBAL))
2850 {
2851 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2852 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2853 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2854 curbuf->b_p_et_nobin = curbuf->b_p_et;
2855 }
2856 if (!(opt_flags & OPT_LOCAL))
2857 {
2858 p_tw_nobin = p_tw;
2859 p_wm_nobin = p_wm;
2860 p_ml_nobin = p_ml;
2861 p_et_nobin = p_et;
2862 }
2863 }
2864
2865 if (!(opt_flags & OPT_GLOBAL))
2866 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 curbuf->b_p_tw = 0; // no automatic line wrap
2868 curbuf->b_p_wm = 0; // no automatic line wrap
2869 curbuf->b_p_ml = 0; // no modelines
2870 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872 if (!(opt_flags & OPT_LOCAL))
2873 {
2874 p_tw = 0;
2875 p_wm = 0;
2876 p_ml = FALSE;
2877 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
2880 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
2883 if (!(opt_flags & OPT_GLOBAL))
2884 {
2885 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2886 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2887 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2888 curbuf->b_p_et = curbuf->b_p_et_nobin;
2889 }
2890 if (!(opt_flags & OPT_LOCAL))
2891 {
2892 p_tw = p_tw_nobin;
2893 p_wm = p_wm_nobin;
2894 p_ml = p_ml_nobin;
2895 p_et = p_et_nobin;
2896 }
2897 }
Christian Brabandt757593c2023-08-22 21:44:10 +02002898#if defined(FEAT_EVAL) || defined(PROTO)
2899 // Remember where the dependent option were reset
2900 didset_options_sctx(opt_flags, p_bin_dep_opts);
2901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902}
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904/*
2905 * Expand environment variables for some string options.
2906 * These string options cannot be indirect!
2907 * If "val" is NULL expand the current value of the option.
2908 * Return pointer to NameBuff, or NULL when not expanded.
2909 */
2910 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002911option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002913 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2915 return NULL;
2916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 // If val is longer than MAXPATHL no meaningful expansion can be done,
2918 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (val != NULL && STRLEN(val) > MAXPATHL)
2920 return NULL;
2921
2922 if (val == NULL)
2923 val = *(char_u **)options[opt_idx].var;
2924
2925 /*
2926 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2927 * Escape spaces when expanding 'tags', they are used to separate file
2928 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002929 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 */
2931 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002932 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002933#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002934 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2935#endif
2936 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 return NULL;
2939
2940 return NameBuff;
2941}
2942
2943/*
2944 * After setting various option values: recompute variables that depend on
2945 * option values.
2946 */
2947 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002948didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 (void)init_chartab();
2952
Bram Moolenaardac13472019-09-16 21:06:21 +02002953 didset_string_options();
2954
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002955#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002956 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002957 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002958 (void)compile_cap_prog(curwin->w_s);
Milly322ad0c2024-10-14 20:21:48 +02002959 (void)did_set_spell_option();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002960#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002961 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002962 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002963#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002965 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002966#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002967 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002968}
2969
2970/*
2971 * More side effects of setting options.
2972 */
2973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002974didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002975{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002977 (void)highlight_changed();
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002980 check_opt_wim();
2981
Bram Moolenaareed9d462021-02-15 20:38:25 +01002982 // Parse default for 'listchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002983 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE, NULL, 0);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // Parse default for 'fillchars'.
zeertzjq6a8d2e12024-01-17 20:54:49 +01002986 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002987
2988#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002990 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002991#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002992#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002993 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002994 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002995 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002996 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998}
2999
3000/*
3001 * Check for string options that are NULL (normally only termcap options).
3002 */
3003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003004check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 int opt_idx;
3007
3008 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
3009 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
3010 check_string_option((char_u **)get_varp(&(options[opt_idx])));
3011}
3012
3013/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003014 * Return the option index found by a pointer into term_strings[].
3015 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003017 int
3018get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003020 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
3023 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003024 return opt_idx;
3025 return -1; // cannot happen: didn't find it!
3026}
3027
3028/*
3029 * Mark a terminal option as allocated, found by a pointer into term_strings[].
3030 * Return the option index or -1 if not found.
3031 */
3032 int
3033set_term_option_alloced(char_u **p)
3034{
3035 int opt_idx = get_term_opt_idx(p);
3036
3037 if (opt_idx >= 0)
3038 options[opt_idx].flags |= P_ALLOCED;
3039 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040}
3041
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003042#if defined(FEAT_EVAL) || defined(PROTO)
3043/*
3044 * Return TRUE when option "opt" was set from a modeline or in secure mode.
3045 * Return FALSE when it wasn't.
3046 * Return -1 for an unknown option.
3047 */
3048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003049was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003050{
3051 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003052 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003053
3054 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003055 {
3056 flagp = insecure_flag(idx, opt_flags);
3057 return (*flagp & P_INSECURE) != 0;
3058 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01003059 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003060 return -1;
3061}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003062
3063/*
3064 * Get a pointer to the flags used for the P_INSECURE flag of option
3065 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01003066 * NOTE: Caller must make sure that "curwin" is set to the window from which
3067 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003068 */
3069 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003071{
3072 if (opt_flags & OPT_LOCAL)
3073 switch ((int)options[opt_idx].indir)
3074 {
3075#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003076 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003077#endif
3078#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00003079# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003080 case PV_FDE: return &curwin->w_p_fde_flags;
3081 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00003082# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003083# ifdef FEAT_BEVAL
3084 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
3085# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003086 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003087 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003088# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003089 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003090# endif
3091#endif
3092 }
3093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003095 return &options[opt_idx].flags;
3096}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003097#endif
3098
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003099/*
3100 * Redraw the window title and/or tab page text later.
3101 */
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02003102 void
3103redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003104{
3105 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003106 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003107}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003110 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
3111 * characters or characters in "allowed".
3112 */
Bram Moolenaare677df82019-09-02 22:31:11 +02003113 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02003114valid_name(char_u *val, char *allowed)
3115{
3116 char_u *s;
3117
3118 for (s = val; *s != NUL; ++s)
3119 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3120 return FALSE;
3121 return TRUE;
3122}
3123
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003124#if defined(FEAT_EVAL) || defined(PROTO)
3125/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003126 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003127 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003128 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003129 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003130set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003131{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003132 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3133 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003134 sctx_T new_script_ctx = script_ctx;
3135
Bram Moolenaar51258742020-05-03 17:19:33 +02003136 // Modeline already has the line number set.
3137 if (!(opt_flags & OPT_MODELINE))
3138 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140 // Remember where the option was set. For local options need to do that
3141 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003142 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003143 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003144 if (both || (opt_flags & OPT_LOCAL))
3145 {
3146 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003147 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003148 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003149 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003150 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003151 if (both)
3152 // also setting the "all buffers" value
3153 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3154 new_script_ctx;
3155 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003156 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003157}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003158
3159/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003160 * Get the script context of global option "name".
3161 *
3162 */
3163 sctx_T *
3164get_option_sctx(char *name)
3165{
3166 int idx = findoption((char_u *)name);
3167
3168 if (idx >= 0)
3169 return &options[idx].script_ctx;
3170 siemsg("no such option: %s", name);
3171 return NULL;
3172}
3173
3174/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003175 * Set the script_ctx for a termcap option.
3176 * "name" must be the two character code, e.g. "RV".
3177 * When "name" is NULL use "opt_idx".
3178 */
3179 void
3180set_term_option_sctx_idx(char *name, int opt_idx)
3181{
3182 char_u buf[5];
3183 int idx;
3184
3185 if (name == NULL)
3186 idx = opt_idx;
3187 else
3188 {
3189 buf[0] = 't';
3190 buf[1] = '_';
3191 buf[2] = name[0];
3192 buf[3] = name[1];
3193 buf[4] = 0;
3194 idx = findoption(buf);
3195 }
3196 if (idx >= 0)
3197 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3198}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003199#endif
3200
Bram Moolenaarf4140482020-02-15 23:06:45 +01003201#if defined(FEAT_EVAL)
3202/*
3203 * Apply the OptionSet autocommand.
3204 */
3205 static void
3206apply_optionset_autocmd(
3207 int opt_idx,
3208 long opt_flags,
3209 long oldval,
3210 long oldval_g,
3211 long newval,
3212 char *errmsg)
3213{
3214 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3215
3216 // Don't do this while starting up, failure or recursively.
3217 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3218 return;
3219
3220 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3221 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3222 oldval_g);
3223 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3224 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3225 (opt_flags & OPT_LOCAL) ? "local" : "global");
3226 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3227 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3228 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3229 if (opt_flags & OPT_LOCAL)
3230 {
3231 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3232 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3233 }
3234 if (opt_flags & OPT_GLOBAL)
3235 {
3236 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3237 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3238 }
3239 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3240 {
3241 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3242 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3243 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3244 }
3245 if (opt_flags & OPT_MODELINE)
3246 {
3247 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3248 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3249 }
3250 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3251 NULL, FALSE, NULL);
3252 reset_v_option_vars();
3253}
3254#endif
3255
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003256#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003257/*
3258 * Process the updated 'arabic' option value.
3259 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003260 char *
3261did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003262{
3263 char *errmsg = NULL;
3264
3265 if (curwin->w_p_arab)
3266 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003267 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003268 if (!p_tbidi)
3269 {
3270 // set rightleft mode
3271 if (!curwin->w_p_rl)
3272 {
3273 curwin->w_p_rl = TRUE;
3274 changed_window_setting();
3275 }
3276
3277 // Enable Arabic shaping (major part of what Arabic requires)
3278 if (!p_arshape)
3279 {
3280 p_arshape = TRUE;
3281 redraw_later_clear();
3282 }
3283 }
3284
3285 // Arabic requires a utf-8 encoding, inform the user if it's not
3286 // set.
3287 if (STRCMP(p_enc, "utf-8") != 0)
3288 {
3289 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3290
3291 msg_source(HL_ATTR(HLF_W));
3292 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3293#ifdef FEAT_EVAL
3294 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3295#endif
3296 }
3297
3298 // set 'delcombine'
3299 p_deco = TRUE;
3300
3301# ifdef FEAT_KEYMAP
3302 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003303 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3304 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003305# endif
3306 }
3307 else
3308 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003309 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003310 if (!p_tbidi)
3311 {
3312 // reset rightleft mode
3313 if (curwin->w_p_rl)
3314 {
3315 curwin->w_p_rl = FALSE;
3316 changed_window_setting();
3317 }
3318
3319 // 'arabicshape' isn't reset, it is a global option and
3320 // another window may still need it "on".
3321 }
3322
3323 // 'delcombine' isn't reset, it is a global option and another
3324 // window may still want it "on".
3325
3326# ifdef FEAT_KEYMAP
3327 // Revert to the default keymap
3328 curbuf->b_p_iminsert = B_IMODE_NONE;
3329 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3330# endif
3331 }
3332
3333 return errmsg;
3334}
3335#endif
3336
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003337#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3338/*
3339 * Process the updated 'autochdir' option value.
3340 */
3341 char *
3342did_set_autochdir(optset_T *args UNUSED)
3343{
3344 // Change directories when the 'acd' option is set now.
3345 DO_AUTOCHDIR;
3346 return NULL;
3347}
3348#endif
3349
3350#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3351/*
3352 * Process the updated 'ballooneval' option value.
3353 */
3354 char *
3355did_set_ballooneval(optset_T *args)
3356{
3357 if (balloonEvalForTerm)
3358 return NULL;
3359
3360 if (p_beval && !args->os_oldval.boolean)
3361 gui_mch_enable_beval_area(balloonEval);
3362 else if (!p_beval && args->os_oldval.boolean)
3363 gui_mch_disable_beval_area(balloonEval);
3364
3365 return NULL;
3366}
3367#endif
3368
3369#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3370/*
3371 * Process the updated 'balloonevalterm' option value.
3372 */
3373 char *
3374did_set_balloonevalterm(optset_T *args UNUSED)
3375{
3376 mch_bevalterm_changed();
3377 return NULL;
3378}
3379#endif
3380
3381/*
3382 * Process the updated 'binary' option value.
3383 */
3384 char *
3385did_set_binary(optset_T *args)
3386{
3387 // when 'bin' is set also set some other options
3388 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3389 redraw_titles();
3390
3391 return NULL;
3392}
3393
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003394/*
3395 * Process the updated 'buflisted' option value.
3396 */
3397 char *
3398did_set_buflisted(optset_T *args)
3399{
3400 // when 'buflisted' changes, trigger autocommands
3401 if (args->os_oldval.boolean != curbuf->b_p_bl)
3402 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3403 NULL, NULL, TRUE, curbuf);
3404 return NULL;
3405}
3406
3407/*
3408 * Process the new 'cmdheight' option value.
3409 */
3410 char *
3411did_set_cmdheight(optset_T *args)
3412{
3413 long old_value = args->os_oldval.number;
3414 char *errmsg = NULL;
3415
3416 // if p_ch changed value, change the command line height
3417 if (p_ch < 1)
3418 {
3419 errmsg = e_argument_must_be_positive;
3420 p_ch = 1;
3421 }
3422 if (p_ch > Rows - min_rows() + 1)
3423 p_ch = Rows - min_rows() + 1;
3424
3425 // Only compute the new window layout when startup has been
3426 // completed. Otherwise the frame sizes may be wrong.
3427 if ((p_ch != old_value
3428 || tabline_height() + topframe->fr_height != Rows - p_ch)
3429 && full_screen
3430#ifdef FEAT_GUI
3431 && !gui.starting
3432#endif
3433 )
3434 command_height();
3435
3436 return errmsg;
3437}
3438
3439/*
3440 * Process the updated 'compatible' option value.
3441 */
3442 char *
3443did_set_compatible(optset_T *args UNUSED)
3444{
3445 compatible_set();
3446 return NULL;
3447}
3448
3449#if defined(FEAT_CONCEAL) || defined(PROTO)
3450/*
3451 * Process the new 'conceallevel' option value.
3452 */
3453 char *
3454did_set_conceallevel(optset_T *args UNUSED)
3455{
3456 char *errmsg = NULL;
3457
3458 if (curwin->w_p_cole < 0)
3459 {
3460 errmsg = e_argument_must_be_positive;
3461 curwin->w_p_cole = 0;
3462 }
3463 else if (curwin->w_p_cole > 3)
3464 {
3465 errmsg = e_invalid_argument;
3466 curwin->w_p_cole = 3;
3467 }
3468
3469 return errmsg;
3470}
3471#endif
3472
3473#if defined(FEAT_DIFF) || defined(PROTO)
3474/*
3475 * Process the updated 'diff' option value.
3476 */
3477 char *
3478did_set_diff(optset_T *args UNUSED)
3479{
3480 // May add or remove the buffer from the list of diff buffers.
3481 diff_buf_adjust(curwin);
3482# ifdef FEAT_FOLDING
3483 if (foldmethodIsDiff(curwin))
3484 foldUpdateAll(curwin);
3485# endif
3486 return NULL;
3487}
3488#endif
3489
3490/*
3491 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3492 * option value.
3493 */
3494 char *
3495did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3496{
3497 // redraw the window title and tab page text
3498 redraw_titles();
3499 return NULL;
3500}
3501
3502/*
3503 * Process the updated 'equalalways' option value.
3504 */
3505 char *
3506did_set_equalalways(optset_T *args)
3507{
3508 if (p_ea && !args->os_oldval.boolean)
3509 win_equal(curwin, FALSE, 0);
3510
3511 return NULL;
3512}
3513
3514#if defined(FEAT_FOLDING) || defined(PROTO)
3515/*
3516 * Process the new 'foldcolumn' option value.
3517 */
3518 char *
3519did_set_foldcolumn(optset_T *args UNUSED)
3520{
3521 char *errmsg = NULL;
3522
3523 if (curwin->w_p_fdc < 0)
3524 {
3525 errmsg = e_argument_must_be_positive;
3526 curwin->w_p_fdc = 0;
3527 }
3528 else if (curwin->w_p_fdc > 12)
3529 {
3530 errmsg = e_invalid_argument;
3531 curwin->w_p_fdc = 12;
3532 }
3533
3534 return errmsg;
3535}
3536
3537/*
3538 * Process the new 'foldlevel' option value.
3539 */
3540 char *
3541did_set_foldlevel(optset_T *args UNUSED)
3542{
3543 if (curwin->w_p_fdl < 0)
3544 curwin->w_p_fdl = 0;
3545 newFoldLevel();
3546 return NULL;
3547}
3548
3549/*
3550 * Process the new 'foldminlines' option value.
3551 */
3552 char *
3553did_set_foldminlines(optset_T *args UNUSED)
3554{
3555 foldUpdateAll(curwin);
3556 return NULL;
3557}
3558
3559/*
3560 * Process the new 'foldnestmax' option value.
3561 */
3562 char *
3563did_set_foldnestmax(optset_T *args UNUSED)
3564{
3565 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3566 foldUpdateAll(curwin);
3567 return NULL;
3568}
3569#endif
3570
3571#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3572/*
3573 * Process the updated 'hlsearch' option value.
3574 */
3575 char *
3576did_set_hlsearch(optset_T *args UNUSED)
3577{
3578 // when 'hlsearch' is set or reset: reset no_hlsearch
3579 set_no_hlsearch(FALSE);
3580 return NULL;
3581}
3582#endif
3583
3584/*
3585 * Process the updated 'ignorecase' option value.
3586 */
3587 char *
3588did_set_ignorecase(optset_T *args UNUSED)
3589{
3590 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3591 if (p_hls)
3592 redraw_all_later(UPD_SOME_VALID);
3593 return NULL;
3594}
3595
3596#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3597/*
3598 * Process the updated 'imdisable' option value.
3599 */
3600 char *
3601did_set_imdisable(optset_T *args UNUSED)
3602{
3603 // Only de-activate it here, it will be enabled when changing mode.
3604 if (p_imdisable)
3605 im_set_active(FALSE);
3606 else if (State & MODE_INSERT)
3607 // When the option is set from an autocommand, it may need to take
3608 // effect right away.
3609 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3610 return NULL;
3611}
3612#endif
3613
3614/*
3615 * Process the new 'iminsert' option value.
3616 */
3617 char *
3618did_set_iminsert(optset_T *args UNUSED)
3619{
3620 char *errmsg = NULL;
3621
3622 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3623 {
3624 errmsg = e_invalid_argument;
3625 curbuf->b_p_iminsert = B_IMODE_NONE;
3626 }
3627 p_iminsert = curbuf->b_p_iminsert;
3628 if (termcap_active) // don't do this in the alternate screen
3629 showmode();
3630#if defined(FEAT_KEYMAP)
3631 // Show/unshow value of 'keymap' in status lines.
3632 status_redraw_curbuf();
3633#endif
3634
3635 return errmsg;
3636}
3637
3638/*
3639 * Process the new 'imsearch' option value.
3640 */
3641 char *
3642did_set_imsearch(optset_T *args UNUSED)
3643{
3644 char *errmsg = NULL;
3645
3646 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3647 {
3648 errmsg = e_invalid_argument;
3649 curbuf->b_p_imsearch = B_IMODE_NONE;
3650 }
3651 p_imsearch = curbuf->b_p_imsearch;
3652
3653 return errmsg;
3654}
3655
3656#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3657/*
3658 * Process the new 'imstyle' option value.
3659 */
3660 char *
3661did_set_imstyle(optset_T *args UNUSED)
3662{
3663 char *errmsg = NULL;
3664
3665 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3666 errmsg = e_invalid_argument;
3667
3668 return errmsg;
3669}
3670#endif
3671
3672/*
3673 * Process the updated 'insertmode' option value.
3674 */
3675 char *
3676did_set_insertmode(optset_T *args)
3677{
3678 // when 'insertmode' is set from an autocommand need to do work here
3679 if (p_im)
3680 {
3681 if ((State & MODE_INSERT) == 0)
3682 need_start_insertmode = TRUE;
3683 stop_insert_mode = FALSE;
3684 }
3685 // only reset if it was set previously
3686 else if (args->os_oldval.boolean)
3687 {
3688 need_start_insertmode = FALSE;
3689 stop_insert_mode = TRUE;
3690 if (restart_edit != 0 && mode_displayed)
3691 clear_cmdline = TRUE; // remove "(insert)"
3692 restart_edit = 0;
3693 }
3694
3695 return NULL;
3696}
3697
3698#if defined(FEAT_LANGMAP) || defined(PROTO)
3699/*
3700 * Process the updated 'langnoremap' option value.
3701 */
3702 char *
3703did_set_langnoremap(optset_T *args UNUSED)
3704{
3705 // 'langnoremap' -> !'langremap'
3706 p_lrm = !p_lnr;
3707 return NULL;
3708}
3709
3710/*
3711 * Process the updated 'langremap' option value.
3712 */
3713 char *
3714did_set_langremap(optset_T *args UNUSED)
3715{
3716 // 'langremap' -> !'langnoremap'
3717 p_lnr = !p_lrm;
3718 return NULL;
3719}
3720#endif
3721
3722/*
3723 * Process the new 'laststatus' option value.
3724 */
3725 char *
3726did_set_laststatus(optset_T *args UNUSED)
3727{
3728 last_status(FALSE); // (re)set last window status line
3729 return NULL;
3730}
3731
3732#if defined(FEAT_GUI) || defined(PROTO)
3733/*
3734 * Process the new 'linespace' option value.
3735 */
3736 char *
3737did_set_linespace(optset_T *args UNUSED)
3738{
3739 // Recompute gui.char_height and resize the Vim window to keep the
3740 // same number of lines.
3741 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3742 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3743 return NULL;
3744}
3745#endif
3746
3747/*
3748 * Process the updated 'lisp' option value.
3749 */
3750 char *
3751did_set_lisp(optset_T *args UNUSED)
3752{
3753 // When 'lisp' option changes include/exclude '-' in keyword characters.
3754 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3755 return NULL;
3756}
3757
3758/*
3759 * Process the new 'maxcombine' option value.
3760 */
3761 char *
3762did_set_maxcombine(optset_T *args UNUSED)
3763{
3764 if (p_mco > MAX_MCO)
3765 p_mco = MAX_MCO;
3766 else if (p_mco < 0)
3767 p_mco = 0;
3768 screenclear(); // will re-allocate the screen
3769 return NULL;
3770}
3771
3772/*
3773 * Process the updated 'modifiable' option value.
3774 */
3775 char *
3776did_set_modifiable(optset_T *args UNUSED)
3777{
3778 // when 'modifiable' is changed, redraw the window title
3779
3780# ifdef FEAT_TERMINAL
3781 // Cannot set 'modifiable' when in Terminal mode.
3782 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3783 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3784 {
3785 curbuf->b_p_ma = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003786 return e_cannot_make_terminal_with_running_job_modifiable;
3787 }
3788# endif
3789 redraw_titles();
3790
3791 return NULL;
3792}
3793
3794/*
3795 * Process the updated 'modified' option value.
3796 */
3797 char *
3798did_set_modified(optset_T *args)
3799{
3800 if (!args->os_newval.boolean)
3801 save_file_ff(curbuf); // Buffer is unchanged
3802 redraw_titles();
zeertzjq5bf6c212024-03-31 18:41:27 +02003803 curbuf->b_modified_was_set = args->os_newval.boolean;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003804 return NULL;
3805}
3806
3807#if defined(FEAT_GUI) || defined(PROTO)
3808/*
3809 * Process the updated 'mousehide' option value.
3810 */
3811 char *
3812did_set_mousehide(optset_T *args UNUSED)
3813{
3814 if (!p_mh)
3815 gui_mch_mousehide(FALSE);
3816 return NULL;
3817}
3818#endif
3819
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003820/*
3821 * Process the updated 'number' or 'relativenumber' option value.
3822 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003823 char *
3824did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003825{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003826#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003827 if (gui.in_use
3828 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3829 && curbuf->b_signlist != NULL)
3830 {
3831 // If the 'number' or 'relativenumber' options are modified and
3832 // 'signcolumn' is set to 'number', then clear the screen for a full
3833 // refresh. Otherwise the sign icons are not displayed properly in the
3834 // number column. If the 'number' option is set and only the
3835 // 'relativenumber' option is toggled, then don't refresh the screen
3836 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003837 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003838 redraw_all_later(UPD_CLEAR);
3839 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003840#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003841 return NULL;
3842}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003843
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003844#if defined(FEAT_LINEBREAK) || defined(PROTO)
3845/*
3846 * Process the new 'numberwidth' option value.
3847 */
3848 char *
3849did_set_numberwidth(optset_T *args UNUSED)
3850{
3851 char *errmsg = NULL;
3852
3853 // 'numberwidth' must be positive
3854 if (curwin->w_p_nuw < 1)
3855 {
3856 errmsg = e_argument_must_be_positive;
3857 curwin->w_p_nuw = 1;
3858 }
3859 if (curwin->w_p_nuw > 20)
3860 {
3861 errmsg = e_invalid_argument;
3862 curwin->w_p_nuw = 20;
3863 }
3864 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3865
3866 return errmsg;
3867}
3868#endif
3869
3870/*
3871 * Process the updated 'paste' option value. Called after p_paste was set or
3872 * reset. When 'paste' is set or reset also change other options.
3873 */
3874 char *
3875did_set_paste(optset_T *args UNUSED)
3876{
3877 static int old_p_paste = FALSE;
3878 static int save_sm = 0;
3879 static int save_sta = 0;
3880 static int save_ru = 0;
3881#ifdef FEAT_RIGHTLEFT
3882 static int save_ri = 0;
3883 static int save_hkmap = 0;
3884#endif
3885 buf_T *buf;
3886
3887 if (p_paste)
3888 {
3889 // Paste switched from off to on.
3890 // Save the current values, so they can be restored later.
3891 if (!old_p_paste)
3892 {
3893 // save options for each buffer
3894 FOR_ALL_BUFFERS(buf)
3895 {
3896 buf->b_p_tw_nopaste = buf->b_p_tw;
3897 buf->b_p_wm_nopaste = buf->b_p_wm;
3898 buf->b_p_sts_nopaste = buf->b_p_sts;
3899 buf->b_p_ai_nopaste = buf->b_p_ai;
3900 buf->b_p_et_nopaste = buf->b_p_et;
3901#ifdef FEAT_VARTABS
3902 if (buf->b_p_vsts_nopaste)
3903 vim_free(buf->b_p_vsts_nopaste);
3904 buf->b_p_vsts_nopaste =
3905 buf->b_p_vsts && buf->b_p_vsts != empty_option
3906 ? vim_strsave(buf->b_p_vsts) : NULL;
3907#endif
3908 }
3909
3910 // save global options
3911 save_sm = p_sm;
3912 save_sta = p_sta;
3913 save_ru = p_ru;
3914#ifdef FEAT_RIGHTLEFT
3915 save_ri = p_ri;
3916 save_hkmap = p_hkmap;
3917#endif
3918 // save global values for local buffer options
3919 p_ai_nopaste = p_ai;
3920 p_et_nopaste = p_et;
3921 p_sts_nopaste = p_sts;
3922 p_tw_nopaste = p_tw;
3923 p_wm_nopaste = p_wm;
3924#ifdef FEAT_VARTABS
3925 if (p_vsts_nopaste)
3926 vim_free(p_vsts_nopaste);
3927 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3928 ? vim_strsave(p_vsts) : NULL;
3929#endif
3930 }
3931
3932 // Always set the option values, also when 'paste' is set when it is
3933 // already on. Set options for each buffer.
3934 FOR_ALL_BUFFERS(buf)
3935 {
3936 buf->b_p_tw = 0; // textwidth is 0
3937 buf->b_p_wm = 0; // wrapmargin is 0
3938 buf->b_p_sts = 0; // softtabstop is 0
3939 buf->b_p_ai = 0; // no auto-indent
3940 buf->b_p_et = 0; // no expandtab
3941#ifdef FEAT_VARTABS
3942 if (buf->b_p_vsts)
3943 free_string_option(buf->b_p_vsts);
3944 buf->b_p_vsts = empty_option;
3945 VIM_CLEAR(buf->b_p_vsts_array);
3946#endif
3947 }
3948
3949 // set global options
3950 p_sm = 0; // no showmatch
3951 p_sta = 0; // no smarttab
3952 if (p_ru)
3953 status_redraw_all(); // redraw to remove the ruler
3954 p_ru = 0; // no ruler
3955#ifdef FEAT_RIGHTLEFT
3956 p_ri = 0; // no reverse insert
3957 p_hkmap = 0; // no Hebrew keyboard
3958#endif
3959 // set global values for local buffer options
3960 p_tw = 0;
3961 p_wm = 0;
3962 p_sts = 0;
3963 p_ai = 0;
Christian Brabandt757593c2023-08-22 21:44:10 +02003964 p_et = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003965#ifdef FEAT_VARTABS
3966 if (p_vsts)
3967 free_string_option(p_vsts);
3968 p_vsts = empty_option;
3969#endif
3970 }
3971
3972 // Paste switched from on to off: Restore saved values.
3973 else if (old_p_paste)
3974 {
3975 // restore options for each buffer
3976 FOR_ALL_BUFFERS(buf)
3977 {
3978 buf->b_p_tw = buf->b_p_tw_nopaste;
3979 buf->b_p_wm = buf->b_p_wm_nopaste;
3980 buf->b_p_sts = buf->b_p_sts_nopaste;
3981 buf->b_p_ai = buf->b_p_ai_nopaste;
3982 buf->b_p_et = buf->b_p_et_nopaste;
3983#ifdef FEAT_VARTABS
3984 if (buf->b_p_vsts)
3985 free_string_option(buf->b_p_vsts);
3986 buf->b_p_vsts = buf->b_p_vsts_nopaste
3987 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3988 vim_free(buf->b_p_vsts_array);
3989 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3990 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3991 else
3992 buf->b_p_vsts_array = NULL;
3993#endif
3994 }
3995
3996 // restore global options
3997 p_sm = save_sm;
3998 p_sta = save_sta;
3999 if (p_ru != save_ru)
4000 status_redraw_all(); // redraw to draw the ruler
4001 p_ru = save_ru;
4002#ifdef FEAT_RIGHTLEFT
4003 p_ri = save_ri;
4004 p_hkmap = save_hkmap;
4005#endif
4006 // set global values for local buffer options
4007 p_ai = p_ai_nopaste;
4008 p_et = p_et_nopaste;
4009 p_sts = p_sts_nopaste;
4010 p_tw = p_tw_nopaste;
4011 p_wm = p_wm_nopaste;
4012#ifdef FEAT_VARTABS
4013 if (p_vsts)
4014 free_string_option(p_vsts);
4015 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
4016#endif
4017 }
4018
4019 old_p_paste = p_paste;
4020
Christian Brabandt757593c2023-08-22 21:44:10 +02004021#if defined(FEAT_EVAL) || defined(PROTO)
4022 // Remember where the dependent options were reset
4023 didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts);
4024#endif
4025
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004026 return NULL;
4027}
4028
4029
4030#ifdef FEAT_QUICKFIX
4031/*
4032 * Process the updated 'previewwindow' option value.
4033 */
4034 char *
Yegappan Lakshmanan3ee25962023-12-04 20:31:14 +01004035did_set_previewwindow(optset_T *args UNUSED)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004036{
4037 if (!curwin->w_p_pvw)
4038 return NULL;
4039
4040 // There can be only one window with 'previewwindow' set.
4041 win_T *win;
4042
4043 FOR_ALL_WINDOWS(win)
4044 if (win->w_p_pvw && win != curwin)
4045 {
4046 curwin->w_p_pvw = FALSE;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004047 return e_preview_window_already_exists;
4048 }
4049
4050 return NULL;
4051}
4052#endif
4053
4054#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
4055/*
4056 * Process the new 'pyxversion' option value.
4057 */
4058 char *
4059did_set_pyxversion(optset_T *args UNUSED)
4060{
4061 char *errmsg = NULL;
4062
4063 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4064 errmsg = e_invalid_argument;
4065
4066 return errmsg;
4067}
4068#endif
4069
4070/*
4071 * Process the updated 'readonly' option value.
4072 */
4073 char *
4074did_set_readonly(optset_T *args)
4075{
4076 // when 'readonly' is reset globally, also reset readonlymode
4077 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
4078 readonlymode = FALSE;
4079
4080 // when 'readonly' is set may give W10 again
4081 if (curbuf->b_p_ro)
4082 curbuf->b_did_warn = FALSE;
4083
4084 redraw_titles();
4085
4086 return NULL;
4087}
4088
4089/*
4090 * Process the updated 'scrollbind' option value.
4091 */
4092 char *
4093did_set_scrollbind(optset_T *args UNUSED)
4094{
4095 // when 'scrollbind' is set: snapshot the current position to avoid a jump
4096 // at the end of normal_cmd()
4097 if (!curwin->w_p_scb)
4098 return NULL;
4099
4100 do_check_scrollbind(FALSE);
4101 curwin->w_scbind_pos = curwin->w_topline;
4102 return NULL;
4103}
4104
4105#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
4106/*
4107 * Process the updated 'shellslash' option value.
4108 */
4109 char *
4110did_set_shellslash(optset_T *args UNUSED)
4111{
4112 if (p_ssl)
4113 {
4114 psepc = '/';
4115 psepcN = '\\';
4116 pseps[0] = '/';
4117 }
4118 else
4119 {
4120 psepc = '\\';
4121 psepcN = '/';
4122 pseps[0] = '\\';
4123 }
4124
4125 // need to adjust the file name arguments and buffer names.
4126 buflist_slash_adjust();
4127 alist_slash_adjust();
4128# ifdef FEAT_EVAL
4129 scriptnames_slash_adjust();
4130# endif
4131 return NULL;
4132}
4133#endif
4134
4135/*
4136 * Process the new 'shiftwidth' or the 'tabstop' option value.
4137 */
4138 char *
4139did_set_shiftwidth_tabstop(optset_T *args)
4140{
4141 long *pp = (long *)args->os_varp;
4142 char *errmsg = NULL;
4143
4144 if (curbuf->b_p_sw < 0)
4145 {
4146 errmsg = e_argument_must_be_positive;
4147#ifdef FEAT_VARTABS
4148 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4149 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4150 ? tabstop_first(curbuf->b_p_vts_array)
4151 : curbuf->b_p_ts;
4152#else
4153 curbuf->b_p_sw = curbuf->b_p_ts;
4154#endif
4155 }
4156
4157#ifdef FEAT_FOLDING
4158 if (foldmethodIsIndent(curwin))
4159 foldUpdateAll(curwin);
4160#endif
4161 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4162 // parse 'cinoptions'.
4163 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4164 parse_cino(curbuf);
4165
4166 return errmsg;
4167}
4168
4169/*
4170 * Process the new 'showtabline' option value.
4171 */
4172 char *
4173did_set_showtabline(optset_T *args UNUSED)
4174{
4175 shell_new_rows(); // recompute window positions and heights
4176 return NULL;
4177}
4178
4179/*
4180 * Process the updated 'smoothscroll' option value.
4181 */
4182 char *
4183did_set_smoothscroll(optset_T *args UNUSED)
4184{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004185 if (!curwin->w_p_sms)
4186 curwin->w_skipcol = 0;
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004187
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004188 return NULL;
4189}
4190
4191#if defined(FEAT_SPELL) || defined(PROTO)
4192/*
4193 * Process the updated 'spell' option value.
4194 */
4195 char *
4196did_set_spell(optset_T *args UNUSED)
4197{
4198 if (curwin->w_p_spell)
4199 return parse_spelllang(curwin);
4200
4201 return NULL;
4202}
4203#endif
4204
4205/*
4206 * Process the updated 'swapfile' option value.
4207 */
4208 char *
4209did_set_swapfile(optset_T *args UNUSED)
4210{
4211 // when 'swf' is set, create swapfile, when reset remove swapfile
4212 if (curbuf->b_p_swf && p_uc)
4213 ml_open_file(curbuf); // create the swap file
4214 else
4215 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4216 // buf->b_p_swf
4217 mf_close_file(curbuf, TRUE); // remove the swap file
4218 return NULL;
4219}
4220
4221#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004222 char *
4223did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004224{
4225# ifdef FEAT_VTP
4226 // Do not turn on 'tgc' when 24-bit colors are not supported.
4227 if (
4228# ifdef VIMDLL
4229 !gui.in_use && !gui.starting &&
4230# endif
4231 !has_vtp_working())
4232 {
4233 p_tgc = 0;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004234 return e_24_bit_colors_are_not_supported_on_this_environment;
4235 }
4236 if (is_term_win32())
4237 swap_tcap();
4238# endif
4239# ifdef FEAT_GUI
4240 if (!gui.in_use && !gui.starting)
4241# endif
4242 highlight_gui_started();
4243# ifdef FEAT_VTP
4244 // reset t_Co
4245 if (is_term_win32())
4246 {
4247 control_console_color_rgb();
4248 set_termname(T_NAME);
4249 init_highlight(TRUE, FALSE);
4250 }
4251# endif
4252# ifdef FEAT_TERMINAL
4253 term_update_colors_all();
4254 term_update_palette_all();
4255 term_update_wincolor_all();
4256# endif
4257
4258 return NULL;
4259}
4260#endif
4261
Milly6d5f4a02024-10-22 23:17:45 +02004262#if defined(FEAT_TERMINAL) || defined(PROTO)
4263/*
4264 * Process the updated 'termwinscroll' option value.
4265 */
4266 char *
4267did_set_termwinscroll(optset_T *args)
4268{
4269 long *pp = (long *)args->os_varp;
4270 char *errmsg = NULL;
4271
4272 if (*pp < 1)
4273 {
4274 errmsg = e_argument_must_be_positive;
4275 *pp = 1;
4276 }
4277
4278 return errmsg;
4279}
4280#endif
4281
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004282/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004283 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004285 char *
4286did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004288 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004290 // when 'terse' is set change 'shortmess'
4291 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004293 // insert 's' in p_shm
4294 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004295 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004296 STRCPY(IObuff, p_shm);
4297 STRCAT(IObuff, "s");
4298 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004299 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004300 // remove 's' from p_shm
4301 else if (!p_terse && p != NULL)
4302 STRMOVE(p, p + 1);
4303 return NULL;
4304}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004306/*
4307 * Process the updated 'textauto' option value.
4308 */
4309 char *
4310did_set_textauto(optset_T *args)
4311{
4312 // when 'textauto' is set or reset also change 'fileformats'
4313 set_string_option_direct((char_u *)"ffs", -1,
4314 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4315 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004316
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004317 return NULL;
4318}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004320/*
4321 * Process the updated 'textmode' option value.
4322 */
4323 char *
4324did_set_textmode(optset_T *args)
4325{
4326 // when 'textmode' is set or reset also change 'fileformat'
4327 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4328
4329 return NULL;
4330}
4331
4332/*
4333 * Process the new 'textwidth' option value.
4334 */
4335 char *
4336did_set_textwidth(optset_T *args UNUSED)
4337{
4338 char *errmsg = NULL;
4339
4340 if (curbuf->b_p_tw < 0)
4341 {
4342 errmsg = e_argument_must_be_positive;
4343 curbuf->b_p_tw = 0;
4344 }
4345#ifdef FEAT_SYN_HL
4346 {
4347 win_T *wp;
4348 tabpage_T *tp;
4349
4350 FOR_ALL_TAB_WINDOWS(tp, wp)
Millya441a3e2024-10-22 22:43:01 +02004351 check_colorcolumn(NULL, wp);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004352 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004353#endif
4354
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004355 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356}
4357
4358/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004359 * Process the updated 'title' or the 'icon' option value.
4360 */
4361 char *
4362did_set_title_icon(optset_T *args UNUSED)
4363{
4364 // when 'title' changed, may need to change the title; same for 'icon'
4365 did_set_title();
4366 return NULL;
4367}
4368
4369/*
4370 * Process the new 'titlelen' option value.
4371 */
4372 char *
4373did_set_titlelen(optset_T *args)
4374{
4375 long old_value = args->os_oldval.number;
4376 char *errmsg = NULL;
4377
4378 // if 'titlelen' has changed, redraw the title
4379 if (p_titlelen < 0)
4380 {
4381 errmsg = e_argument_must_be_positive;
4382 p_titlelen = 85;
4383 }
4384 if (starting != NO_SCREEN && old_value != p_titlelen)
4385 need_maketitle = TRUE;
4386
4387 return errmsg;
4388}
4389
4390#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4391/*
4392 * Process the updated 'undofile' option value.
4393 */
4394 char *
4395did_set_undofile(optset_T *args)
4396{
4397 // Only take action when the option was set.
4398 if (!curbuf->b_p_udf && !p_udf)
4399 return NULL;
4400
4401 // When reset we do not delete the undo file, the option may be set again
4402 // without making any changes in between.
4403 char_u hash[UNDO_HASH_SIZE];
4404 buf_T *save_curbuf = curbuf;
4405
4406 FOR_ALL_BUFFERS(curbuf)
4407 {
4408 // When 'undofile' is set globally: for every buffer, otherwise
4409 // only for the current buffer: Try to read in the undofile,
4410 // if one exists, the buffer wasn't changed and the buffer was
4411 // loaded
4412 if ((curbuf == save_curbuf
4413 || (args->os_flags & OPT_GLOBAL)
4414 || args->os_flags == 0)
4415 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4416 {
4417#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004418 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004419 continue;
4420#endif
4421 u_compute_hash(hash);
4422 u_read_undo(NULL, hash, curbuf->b_fname);
4423 }
4424 }
4425 curbuf = save_curbuf;
4426
4427 return NULL;
4428}
4429#endif
4430
4431/*
4432 * Process the new global 'undolevels' option value.
4433 */
4434 static void
4435update_global_undolevels(long value, long old_value)
4436{
4437 // sync undo before 'undolevels' changes
4438
4439 // use the old value, otherwise u_sync() may not work properly
4440 p_ul = old_value;
4441 u_sync(TRUE);
4442 p_ul = value;
4443}
4444
4445/*
4446 * Process the new buffer local 'undolevels' option value.
4447 */
4448 static void
4449update_buflocal_undolevels(long value, long old_value)
4450{
4451 // use the old value, otherwise u_sync() may not work properly
4452 curbuf->b_p_ul = old_value;
4453 u_sync(TRUE);
4454 curbuf->b_p_ul = value;
4455}
4456
4457/*
4458 * Process the new 'undolevels' option value.
4459 */
4460 char *
4461did_set_undolevels(optset_T *args)
4462{
4463 long *pp = (long *)args->os_varp;
4464
4465 if (pp == &p_ul) // global 'undolevels'
4466 update_global_undolevels(args->os_newval.number,
4467 args->os_oldval.number);
4468 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4469 update_buflocal_undolevels(args->os_newval.number,
4470 args->os_oldval.number);
4471
4472 return NULL;
4473}
4474
4475/*
4476 * Process the new 'updatecount' option value.
4477 */
4478 char *
4479did_set_updatecount(optset_T *args)
4480{
4481 long old_value = args->os_oldval.number;
4482 char *errmsg = NULL;
4483
4484 // when 'updatecount' changes from zero to non-zero, open swap files
4485 if (p_uc < 0)
4486 {
4487 errmsg = e_argument_must_be_positive;
4488 p_uc = 100;
4489 }
4490 if (p_uc && !old_value)
4491 ml_open_files();
4492
4493 return errmsg;
4494}
4495
4496/*
4497 * Process the updated 'weirdinvert' option value.
4498 */
4499 char *
4500did_set_weirdinvert(optset_T *args)
4501{
4502 // When 'weirdinvert' changed, set/reset 't_xs'.
4503 // Then set 'weirdinvert' according to value of 't_xs'.
4504 if (p_wiv && !args->os_oldval.boolean)
4505 T_XS = (char_u *)"y";
4506 else if (!p_wiv && args->os_oldval.boolean)
4507 T_XS = empty_option;
4508 p_wiv = (*T_XS != NUL);
4509
4510 return NULL;
4511}
4512
4513/*
Yee Cheng Chin8f4fb002023-10-17 10:06:56 +02004514 * Process the new 'wildchar' / 'wildcharm' option value.
4515 */
4516 char *
4517did_set_wildchar(optset_T *args)
4518{
4519 long c = *(long *)args->os_varp;
4520
4521 // Don't allow key values that wouldn't work as wildchar.
4522 if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER)
4523 return e_invalid_argument;
4524
4525 return NULL;
4526}
4527
4528/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004529 * Process the new 'window' option value.
4530 */
4531 char *
4532did_set_window(optset_T *args UNUSED)
4533{
4534 if (p_window < 1)
4535 p_window = 1;
4536 else if (p_window >= Rows)
4537 p_window = Rows - 1;
4538 return NULL;
4539}
4540
4541/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004542 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004544 char *
4545did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004547 long *pp = (long *)args->os_varp;
4548 char *errmsg = NULL;
4549
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004550 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004552 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004553 p_wh = 1;
4554 }
4555 if (p_wmh > p_wh)
4556 {
4557 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4558 p_wh = p_wmh;
4559 }
4560 if (p_hh < 0)
4561 {
4562 errmsg = e_argument_must_be_positive;
4563 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004566 // Change window height NOW
4567 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004569 if (pp == &p_wh && curwin->w_height < p_wh)
4570 win_setheight((int)p_wh);
4571 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4572 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004575 return errmsg;
4576}
4577
4578/*
4579 * Process the new 'winminheight' option value.
4580 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004581 char *
4582did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004583{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004584 char *errmsg = NULL;
4585
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004586 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004588 errmsg = e_argument_must_be_positive;
4589 p_wmh = 0;
4590 }
4591 if (p_wmh > p_wh)
4592 {
4593 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4594 p_wmh = p_wh;
4595 }
4596 win_setminheight();
4597
4598 return errmsg;
4599}
4600
4601/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004602 * Process the new 'winminwidth' option value.
4603 */
4604 char *
4605did_set_winminwidth(optset_T *args UNUSED)
4606{
4607 char *errmsg = NULL;
4608
4609 if (p_wmw < 0)
4610 {
4611 errmsg = e_argument_must_be_positive;
4612 p_wmw = 0;
4613 }
4614 if (p_wmw > p_wiw)
4615 {
4616 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4617 p_wmw = p_wiw;
4618 }
4619 win_setminwidth();
4620
4621 return errmsg;
4622}
4623
4624/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004625 * Process the new 'winwidth' option value.
4626 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004627 char *
4628did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004629{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004630 char *errmsg = NULL;
4631
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004632 if (p_wiw < 1)
4633 {
4634 errmsg = e_argument_must_be_positive;
4635 p_wiw = 1;
4636 }
4637 if (p_wmw > p_wiw)
4638 {
4639 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4640 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004642
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004643 // Change window width NOW
4644 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4645 win_setwidth((int)p_wiw);
4646
4647 return errmsg;
4648}
4649
4650/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004651 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004652 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004653 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004654did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004655{
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004656 // Set w_leftcol or w_skipcol to zero.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004657 if (curwin->w_p_wrap)
4658 curwin->w_leftcol = 0;
Luuk van Baal1bf1bf52023-10-28 21:43:31 +02004659 else
4660 curwin->w_skipcol = 0;
4661
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004662 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004663}
4664
4665/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004666 * Set the value of a boolean option, and take care of side effects.
4667 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004668 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004669 static char *
4670set_bool_option(
4671 int opt_idx, // index in options[] table
4672 char_u *varp, // pointer to the option variable
4673 int value, // new value
4674 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004675{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004676 int old_value = *(int *)varp;
4677#if defined(FEAT_EVAL)
4678 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004680 char *errmsg = NULL;
4681
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004682 // Disallow changing some options from secure mode
4683 if ((secure
4684#ifdef HAVE_SANDBOX
4685 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004686#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004687 ) && (options[opt_idx].flags & P_SECURE))
4688 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004689
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004690#if defined(FEAT_EVAL)
4691 // Save the global value before changing anything. This is needed as for
4692 // a global-only option setting the "local value" in fact sets the global
4693 // value (since there is only one value).
4694 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4695 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4696 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004698
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004699 *(int *)varp = value; // set the new value
4700#ifdef FEAT_EVAL
4701 // Remember where the option was set.
4702 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004706 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004709 // May set global value for local option.
4710 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4711 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004712
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004713 // Handle side effects of changing a bool option.
4714 if (options[opt_idx].opt_did_set_cb != NULL)
4715 {
4716 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004717
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004718 CLEAR_FIELD(args);
4719 args.os_varp = varp;
4720 args.os_flags = opt_flags;
4721 args.os_oldval.boolean = old_value;
4722 args.os_newval.boolean = value;
4723 args.os_errbuf = NULL;
4724 errmsg = options[opt_idx].opt_did_set_cb(&args);
zeertzjqdeba02d2023-11-02 21:01:19 +01004725 if (errmsg != NULL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004726 return errmsg;
4727 }
4728
4729 // after handling side effects, call autocommand
4730
4731 options[opt_idx].flags |= P_WAS_SET;
4732
4733#if defined(FEAT_EVAL)
4734 apply_optionset_autocmd(opt_idx, opt_flags,
4735 (long)(old_value ? TRUE : FALSE),
4736 (long)(old_global_value ? TRUE : FALSE),
4737 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004738#endif
4739
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004740 comp_col(); // in case 'ruler' or 'showcmd' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004741
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004742 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004743 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4744 && (options[opt_idx].flags & P_HLONLY) == 0)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004745 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004746
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004747 if ((opt_flags & OPT_NO_REDRAW) == 0)
4748 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004749
4750 return errmsg;
4751}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004752
4753/*
4754 * Check the bounds of numeric options.
4755 */
4756 static char *
4757check_num_option_bounds(
4758 long *pp,
4759 long old_value,
4760 long old_Rows,
4761 long old_Columns,
4762 char *errbuf,
4763 size_t errbuflen,
4764 char *errmsg)
4765{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (Rows < min_rows() && full_screen)
4767 {
4768 if (errbuf != NULL)
4769 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004770 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004771 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 errmsg = errbuf;
4773 }
4774 Rows = min_rows();
4775 }
4776 if (Columns < MIN_COLUMNS && full_screen)
4777 {
4778 if (errbuf != NULL)
4779 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004780 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004781 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 errmsg = errbuf;
4783 }
4784 Columns = MIN_COLUMNS;
4785 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004786 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004788 // If the screen (shell) height has been changed, assume it is the
4789 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (old_Rows != Rows || old_Columns != Columns)
4791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004792 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (updating_screen)
4794 *pp = old_value;
4795 else if (full_screen
4796#ifdef FEAT_GUI
4797 && !gui.starting
4798#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004799 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 set_shellsize((int)Columns, (int)Rows, TRUE);
4801 else
4802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 // Postpone the resizing; check the size and cmdline position for
4804 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 check_shellsize();
4806 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4807 cmdline_row = Rows - p_ch;
4808 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004809 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004810 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (curbuf->b_p_ts <= 0)
4814 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004815 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 curbuf->b_p_ts = 8;
4817 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004818 else if (curbuf->b_p_ts > TABSTOP_MAX)
4819 {
4820 errmsg = e_invalid_argument;
4821 curbuf->b_p_ts = 8;
4822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (p_tm < 0)
4824 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004825 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 p_tm = 0;
4827 }
4828 if ((curwin->w_p_scr <= 0
4829 || (curwin->w_p_scr > curwin->w_height
4830 && curwin->w_height > 0))
4831 && full_screen)
4832 {
4833 if (pp == &(curwin->w_p_scr))
4834 {
4835 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004836 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 win_comp_scroll(curwin);
4838 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004839 // If 'scroll' became invalid because of a side effect silently adjust
4840 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 else if (curwin->w_p_scr <= 0)
4842 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004843 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 curwin->w_p_scr = curwin->w_height;
4845 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004846 if (p_hi < 0)
4847 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004848 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004849 p_hi = 0;
4850 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004851 else if (p_hi > 10000)
4852 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004853 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004854 p_hi = 10000;
4855 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004856 if (p_re < 0 || p_re > 2)
4857 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004858 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004859 p_re = 0;
4860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (p_report < 0)
4862 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004863 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 p_report = 1;
4865 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004866 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 p_sj = Rows / 2;
4870 else
4871 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004872 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 p_sj = 1;
4874 }
4875 }
4876 if (p_so < 0 && full_screen)
4877 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004878 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 p_so = 0;
4880 }
4881 if (p_siso < 0 && full_screen)
4882 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004883 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 p_siso = 0;
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (p_cwh < 1)
4887 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004888 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 p_cwh = 1;
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (p_ut < 0)
4892 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004893 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 p_ut = 2000;
4895 }
4896 if (p_ss < 0)
4897 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004898 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 p_ss = 0;
4900 }
4901
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004902 return errmsg;
4903}
4904
4905/*
4906 * Set the value of a number option, and take care of side effects.
4907 * Returns NULL for success, or an error message for an error.
4908 */
4909 static char *
4910set_num_option(
4911 int opt_idx, // index in options[] table
4912 char_u *varp, // pointer to the option variable
4913 long value, // new value
4914 char *errbuf, // buffer for error messages
4915 size_t errbuflen, // length of "errbuf"
4916 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4917 // OPT_MODELINE, etc.
4918{
4919 char *errmsg = NULL;
4920 long old_value = *(long *)varp;
4921#if defined(FEAT_EVAL)
4922 long old_global_value = 0; // only used when setting a local and
4923 // global option
4924#endif
4925 long old_Rows = Rows; // remember old Rows
4926 long old_Columns = Columns; // remember old Columns
4927 long *pp = (long *)varp;
4928
4929 // Disallow changing some options from secure mode.
4930 if ((secure
4931#ifdef HAVE_SANDBOX
4932 || sandbox != 0
4933#endif
4934 ) && (options[opt_idx].flags & P_SECURE))
4935 return e_not_allowed_here;
4936
4937#if defined(FEAT_EVAL)
4938 // Save the global value before changing anything. This is needed as for
4939 // a global-only option setting the "local value" in fact sets the global
4940 // value (since there is only one value).
4941 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4942 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4943 OPT_GLOBAL);
4944#endif
4945
4946 *pp = value;
4947#ifdef FEAT_EVAL
4948 // Remember where the option was set.
4949 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4950#endif
4951#ifdef FEAT_GUI
4952 need_mouse_correct = TRUE;
4953#endif
4954
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004955 // Invoke the option specific callback function to validate and apply the
4956 // new value.
4957 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004958 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004959 optset_T args;
4960
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004961 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004962 args.os_varp = varp;
4963 args.os_flags = opt_flags;
4964 args.os_oldval.number = old_value;
4965 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004966 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004967 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004968 }
4969
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004970 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004971 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4972 errbuf, errbuflen, errmsg);
4973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004974 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4976 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4977
4978 options[opt_idx].flags |= P_WAS_SET;
4979
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004980#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004981 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4982 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004983#endif
4984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004985 comp_col(); // in case 'columns' or 'ls' changed
zeertzjqfcaed6a2024-02-18 09:33:54 +01004986
Bram Moolenaar913077c2012-03-28 19:59:04 +02004987 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004988 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0
4989 && (options[opt_idx].flags & P_HLONLY) == 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004990 curwin->w_set_curswant = TRUE;
zeertzjqfcaed6a2024-02-18 09:33:54 +01004991
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004992 if ((opt_flags & OPT_NO_REDRAW) == 0)
4993 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 return errmsg;
4996}
4997
4998/*
4999 * Called after an option changed: check if something needs to be redrawn.
5000 */
Bram Moolenaardac13472019-09-16 21:06:21 +02005001 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005002check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005004 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005005 int doclear = (flags & P_RCLR) == P_RCLR;
5006 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005008 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
5011 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
zeertzjqfcaed6a2024-02-18 09:33:54 +01005012 {
5013 if (flags & P_HLONLY)
5014 redraw_later(UPD_NOT_VALID);
5015 else
5016 changed_window_setting();
5017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005019 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005020 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005021 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005023 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024}
5025
5026/*
5027 * Find index for option 'arg'.
5028 * Return -1 if not found.
5029 */
Bram Moolenaardac13472019-09-16 21:06:21 +02005030 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005031findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
5033 int opt_idx;
5034 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005035 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 int is_term_opt;
5037
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005038 // For first call: Initialize the quick-access table.
5039 // It contains the index for the first option that starts with a certain
5040 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (quick_tab[1] == 0)
5042 {
5043 p = options[0].fullname;
5044 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
5045 {
5046 if (s[0] != p[0])
5047 {
5048 if (s[0] == 't' && s[1] == '_')
5049 quick_tab[26] = opt_idx;
5050 else
5051 quick_tab[CharOrdLow(s[0])] = opt_idx;
5052 }
5053 p = s;
5054 }
5055 }
5056
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005057 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 return -1;
5060
5061 is_term_opt = (arg[0] == 't' && arg[1] == '_');
5062 if (is_term_opt)
5063 opt_idx = quick_tab[26];
5064 else
5065 opt_idx = quick_tab[CharOrdLow(arg[0])];
zeertzjqf48558e2023-12-08 21:34:31 +01005066 for (; (s = options[opt_idx].fullname) != NULL && s[0] == arg[0]; opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005068 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 break;
5070 }
zeertzjqf48558e2023-12-08 21:34:31 +01005071 if (s != NULL && s[0] != arg[0])
5072 s = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (s == NULL && !is_term_opt)
5074 {
5075 opt_idx = quick_tab[CharOrdLow(arg[0])];
5076 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
5077 {
5078 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005079 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 break;
5081 s = NULL;
5082 }
5083 }
5084 if (s == NULL)
5085 opt_idx = -1;
5086 return opt_idx;
5087}
5088
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005089#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
5090 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091/*
5092 * Get the value for an option.
5093 *
5094 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005095 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01005096 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005097 * String option: gov_string, *stringval gets allocated string.
5098 * Hidden Number option: gov_hidden_number.
5099 * Hidden Toggle option: gov_hidden_bool.
5100 * Hidden String option: gov_hidden_string.
5101 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005102 *
5103 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005105 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01005106get_option_value(
5107 char_u *name,
5108 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005109 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005110 int *flagsp,
5111 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112{
5113 int opt_idx;
5114 char_u *varp;
5115
5116 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005117 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01005118 {
5119 int key;
5120
5121 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005122 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005123 {
5124 char_u key_name[2];
5125 char_u *p;
5126
Bram Moolenaar10c75c42021-12-28 20:53:30 +00005127 if (flagsp != NULL)
5128 *flagsp = 0; // terminal option has no flags
5129
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005130 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01005131 if (key < 0)
5132 {
5133 key_name[0] = KEY2TERMCAP0(key);
5134 key_name[1] = KEY2TERMCAP1(key);
5135 }
5136 else
5137 {
5138 key_name[0] = KS_KEY;
5139 key_name[1] = (key & 0xff);
5140 }
5141 p = find_termcode(key_name);
5142 if (p != NULL)
5143 {
5144 if (stringval != NULL)
5145 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005146 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01005147 }
5148 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005149 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01005150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005152 varp = get_varp_scope(&(options[opt_idx]), scope);
5153
5154 if (flagsp != NULL)
5155 // Return the P_xxxx option flags.
5156 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 if (options[opt_idx].flags & P_STRING)
5159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005160 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005161 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 if (stringval != NULL)
5163 {
zeertzjq51f0bc32022-05-09 13:33:39 +01005164 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005165 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5166 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005168 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005169 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005170 && **(char_u **)(varp) != NUL)
John Marriott95dacbb2024-09-10 21:25:14 +02005171 *stringval = vim_strnsave((char_u *)"*****", STRLEN_LITERAL("*****"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 *stringval = vim_strsave(*(char_u **)(varp));
5175 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005176 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005179 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005180 return (options[opt_idx].flags & P_NUM)
5181 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (options[opt_idx].flags & P_NUM)
5183 *numval = *(long *)varp;
5184 else
5185 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005186 // Special case: 'modified' is b_changed, but we also want to consider
5187 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if ((int *)varp == &curbuf->b_changed)
5189 *numval = curbufIsChanged();
5190 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005191 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005193 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194}
5195#endif
5196
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005197#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005198/*
5199 * Returns the option attributes and its value. Unlike the above function it
5200 * will return either global value or local value of the option depending on
5201 * what was requested, but it will never return global value if it was
5202 * requested to return local one and vice versa. Neither it will return
5203 * buffer-local value if it was requested to return window-local one.
5204 *
5205 * Pretends that option is absent if it is not present in the requested scope
5206 * (i.e. has no global, window-local or buffer-local value depending on
5207 * opt_type). Uses
5208 *
5209 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005210 * 0 hidden or unknown option, also option that does not have requested
5211 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005212 * see SOPT_* in vim.h for other flags
5213 *
5214 * Possible opt_type values: see SREQ_* in vim.h
5215 */
5216 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005217get_option_value_strict(
5218 char_u *name,
5219 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005220 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005221 int opt_type,
5222 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005223{
5224 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005225 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005226 struct vimoption *p;
5227 int r = 0;
5228
5229 opt_idx = findoption(name);
5230 if (opt_idx < 0)
5231 return 0;
5232
5233 p = &(options[opt_idx]);
5234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005235 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005236 if (p->var == NULL)
5237 return 0;
5238
5239 if (p->flags & P_BOOL)
5240 r |= SOPT_BOOL;
5241 else if (p->flags & P_NUM)
5242 r |= SOPT_NUM;
5243 else if (p->flags & P_STRING)
5244 r |= SOPT_STRING;
5245
5246 if (p->indir == PV_NONE)
5247 {
5248 if (opt_type == SREQ_GLOBAL)
5249 r |= SOPT_GLOBAL;
5250 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005251 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005252 }
5253 else
5254 {
5255 if (p->indir & PV_BOTH)
5256 r |= SOPT_GLOBAL;
5257 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005258 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005259
5260 if (p->indir & PV_WIN)
5261 {
5262 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005263 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005264 else
5265 r |= SOPT_WIN;
5266 }
5267 else if (p->indir & PV_BUF)
5268 {
5269 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005270 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005271 else
5272 r |= SOPT_BUF;
5273 }
5274 }
5275
5276 if (stringval == NULL)
5277 return r;
5278
5279 if (opt_type == SREQ_GLOBAL)
5280 varp = p->var;
5281 else
5282 {
5283 if (opt_type == SREQ_BUF)
5284 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005285 // Special case: 'modified' is b_changed, but we also want to
5286 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005287 if (p->indir == PV_MOD)
5288 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005289 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005290 varp = NULL;
5291 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005292# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005293 else if (p->indir == PV_KEY)
5294 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005295 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005296 *stringval = NULL;
5297 varp = NULL;
5298 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005299# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005300 else
5301 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005302 buf_T *save_curbuf = curbuf;
5303
5304 // only getting a pointer, no need to use aucmd_prepbuf()
5305 curbuf = (buf_T *)from;
5306 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005307 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005308 curbuf = save_curbuf;
5309 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005310 }
5311 }
5312 else if (opt_type == SREQ_WIN)
5313 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005314 win_T *save_curwin = curwin;
5315
5316 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005317 curbuf = curwin->w_buffer;
5318 varp = get_varp(p);
5319 curwin = save_curwin;
5320 curbuf = curwin->w_buffer;
5321 }
5322 if (varp == p->var)
5323 return (r | SOPT_UNSET);
5324 }
5325
5326 if (varp != NULL)
5327 {
5328 if (p->flags & P_STRING)
5329 *stringval = vim_strsave(*(char_u **)(varp));
5330 else if (p->flags & P_NUM)
5331 *numval = *(long *) varp;
5332 else
5333 *numval = *(int *)varp;
5334 }
5335
5336 return r;
5337}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005338
5339/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005340 * Iterate over options. First argument is a pointer to a pointer to a
5341 * structure inside options[] array, second is option type like in the above
5342 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005343 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005344 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005345 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005346 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005347 * first argument to NULL.
5348 *
5349 * Returns full option name for current option on each call.
5350 */
5351 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005353{
5354 struct vimoption *ret = NULL;
5355 do
5356 {
5357 if (*option == NULL)
5358 *option = (void *) options;
5359 else if (((struct vimoption *) (*option))->fullname == NULL)
5360 {
5361 *option = NULL;
5362 return NULL;
5363 }
5364 else
5365 *option = (void *) (((struct vimoption *) (*option)) + 1);
5366
5367 ret = ((struct vimoption *) (*option));
5368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005369 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005370 if (ret->var == NULL)
5371 {
5372 ret = NULL;
5373 continue;
5374 }
5375
5376 switch (opt_type)
5377 {
5378 case SREQ_GLOBAL:
5379 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5380 ret = NULL;
5381 break;
5382 case SREQ_BUF:
5383 if (!(ret->indir & PV_BUF))
5384 ret = NULL;
5385 break;
5386 case SREQ_WIN:
5387 if (!(ret->indir & PV_WIN))
5388 ret = NULL;
5389 break;
5390 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005391 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005392 return NULL;
5393 }
5394 }
5395 while (ret == NULL);
5396
5397 return (char_u *)ret->fullname;
5398}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005399#endif
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005402 * Return the flags for the option at 'opt_idx'.
5403 */
5404 long_u
5405get_option_flags(int opt_idx)
5406{
5407 return options[opt_idx].flags;
5408}
5409
5410/*
5411 * Set a flag for the option at 'opt_idx'.
5412 */
5413 void
5414set_option_flag(int opt_idx, long_u flag)
5415{
5416 options[opt_idx].flags |= flag;
5417}
5418
5419/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005420 * Returns TRUE if the option at 'opt_idx' is a global option
5421 */
5422 int
5423is_global_option(int opt_idx)
5424{
5425 return options[opt_idx].indir == PV_NONE;
5426}
5427
5428/*
5429 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5430 * local value.
5431 */
5432 int
5433is_global_local_option(int opt_idx)
5434{
5435 return options[opt_idx].indir & PV_BOTH;
5436}
5437
5438/*
5439 * Returns TRUE if the option at 'opt_idx' is a window-local option
5440 */
5441 int
5442is_window_local_option(int opt_idx)
5443{
5444 return options[opt_idx].var == VAR_WIN;
5445}
5446
5447/*
5448 * Returns TRUE if the option at 'opt_idx' is a hidden option
5449 */
5450 int
5451is_hidden_option(int opt_idx)
5452{
5453 return options[opt_idx].var == NULL;
5454}
5455
5456#if defined(FEAT_CRYPT) || defined(PROTO)
5457/*
5458 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5459 */
5460 int
5461is_crypt_key_option(int opt_idx)
5462{
5463 return options[opt_idx].indir == PV_KEY;
5464}
5465#endif
5466
5467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 * Set the value of option "name".
5469 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005470 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005471 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005473 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474set_option_value(
5475 char_u *name,
5476 long number,
5477 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005478 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 int opt_idx;
5481 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005482 long_u flags;
Christian Brabandtb39b2402023-11-29 11:34:05 +01005483 static char errbuf[ERR_BUFLEN];
5484 int errbuflen = ERR_BUFLEN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005487 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005488 {
5489 int key;
5490
5491 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005492 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005493 {
5494 char_u key_name[2];
5495
5496 if (key < 0)
5497 {
5498 key_name[0] = KEY2TERMCAP0(key);
5499 key_name[1] = KEY2TERMCAP1(key);
5500 }
5501 else
5502 {
5503 key_name[0] = KS_KEY;
5504 key_name[1] = (key & 0xff);
5505 }
5506 add_termcode(key_name, string, FALSE);
5507 if (full_screen)
5508 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005509 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005510 return NULL;
5511 }
5512
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005513 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 else
5516 {
5517 flags = options[opt_idx].flags;
5518#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005519 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005521 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005522 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005523 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005526 if (flags & P_STRING)
Christian Brabandtb39b2402023-11-29 11:34:05 +01005527 return set_string_option(opt_idx, string, opt_flags, errbuf, errbuflen);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005528
5529 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5530 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005532 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005534 int idx;
5535
5536 // Either we are given a string or we are setting option
5537 // to zero.
5538 for (idx = 0; string[idx] == '0'; ++idx)
5539 ;
5540 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005541 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005542 // There's another character after zeros or the string
5543 // is empty. In both cases, we are trying to set a
5544 // num option using a string.
5545 semsg(_(e_number_required_after_str_equal_str),
5546 name, string);
5547 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005548
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005551 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005552 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005553 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005554 errbuf, sizeof(errbuf), opt_flags);
5555 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005556 else
5557 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005561 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563
5564/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005565 * Call set_option_value() and when an error is returned report it.
5566 */
5567 void
5568set_option_value_give_err(
5569 char_u *name,
5570 long number,
5571 char_u *string,
5572 int opt_flags) // OPT_LOCAL or 0 (both)
5573{
5574 char *errmsg = set_option_value(name, number, string, opt_flags);
5575
5576 if (errmsg != NULL)
5577 emsg(_(errmsg));
5578}
5579
5580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 * Get the terminal code for a terminal option.
5582 * Returns NULL when not found.
5583 */
5584 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005585get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 int opt_idx;
5588 char_u *varp;
5589
5590 if (tname[0] != 't' || tname[1] != '_' ||
5591 tname[2] == NUL || tname[3] == NUL)
5592 return NULL;
5593 if ((opt_idx = findoption(tname)) >= 0)
5594 {
5595 varp = get_varp(&(options[opt_idx]));
5596 if (varp != NULL)
5597 varp = *(char_u **)(varp);
5598 return varp;
5599 }
5600 return find_termcode(tname + 2);
5601}
5602
5603 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005604get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 int i;
5607
5608 i = findoption((char_u *)"hl");
5609 if (i >= 0)
5610 return options[i].def_val[VI_DEFAULT];
5611 return (char_u *)NULL;
5612}
5613
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005614 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005615get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005616{
5617 int i;
5618
5619 i = findoption((char_u *)"enc");
5620 if (i >= 0)
5621 return options[i].def_val[VI_DEFAULT];
5622 return (char_u *)NULL;
5623}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005624
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005625#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005626 int
5627is_option_allocated(char *name)
5628{
5629 int idx = findoption((char_u *)name);
5630
5631 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5632}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005633#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635/*
5636 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005637 * When "has_lt" is true there is a '<' before "*arg_arg".
5638 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 */
5640 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005641find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005643 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005645 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005647 // Don't use get_special_key_code() for t_xx, we don't want it to call
5648 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
Millya9c6f902024-10-06 16:47:02 +02005650 {
5651 if (!has_lt || arg[4] == '>')
5652 key = TERMCAP2KEY(arg[2], arg[3]);
5653 }
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005654 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005656 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005658 key = find_special_key(&arg, &modifiers,
5659 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005660 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 key = 0;
5662 }
5663 return key;
5664}
5665
5666/*
5667 * if 'all' == 0: show changed options
5668 * if 'all' == 1: show all normal options
5669 * if 'all' == 2: show all terminal options
5670 */
5671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005672showoptions(
5673 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005674 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 struct vimoption *p;
5677 int col;
5678 int isterm;
5679 char_u *varp;
5680 struct vimoption **items;
5681 int item_count;
5682 int run;
5683 int row, rows;
5684 int cols;
5685 int i;
5686 int len;
5687
5688#define INC 20
5689#define GAP 3
5690
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005691 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (items == NULL)
5693 return;
5694
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005695 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005697 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005699 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005701 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005703 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005705 // Do the loop two times:
5706 // 1. display the short items
5707 // 2. display the long items (only strings and numbers)
5708 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 for (run = 1; run <= 2 && !got_int; ++run)
5710 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005711 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 item_count = 0;
5713 for (p = &options[0]; p->fullname != NULL; p++)
5714 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005715 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005716 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005717 continue;
5718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 varp = NULL;
5720 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005721 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 if (p->indir != PV_NONE && !isterm)
5724 varp = get_varp_scope(p, opt_flags);
5725 }
5726 else
5727 varp = get_varp(p);
5728 if (varp != NULL
5729 && ((all == 2 && isterm)
5730 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005731 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005733 if (opt_flags & OPT_ONECOLUMN)
5734 len = Columns;
5735 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005736 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 else
5738 {
5739 option_value2string(p, opt_flags);
5740 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5741 }
5742 if ((len <= INC - GAP && run == 1) ||
5743 (len > INC - GAP && run == 2))
5744 items[item_count++] = p;
5745 }
5746 }
5747
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005748 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (run == 1)
5750 {
5751 cols = (Columns + GAP - 3) / INC;
5752 if (cols == 0)
5753 cols = 1;
5754 rows = (item_count + cols - 1) / cols;
5755 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005756 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 rows = item_count;
5758 for (row = 0; row < rows && !got_int; ++row)
5759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005760 msg_putchar('\n'); // go to next line
5761 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 break;
5763 col = 0;
5764 for (i = row; i < item_count; i += rows)
5765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005766 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 showoneopt(items[i], opt_flags);
5768 col += INC;
5769 }
5770 out_flush();
5771 ui_breakcheck();
5772 }
5773 }
5774 vim_free(items);
5775}
5776
5777/*
5778 * Return TRUE if option "p" has its default value.
5779 */
5780 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005781optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 int dvi;
5784
5785 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005786 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005787 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005789 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005791 // the cast to long is required for Manx C, long_i is
5792 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005793 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005794 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5796}
5797
5798/*
5799 * showoneopt: show the value of one option
5800 * must not be called with a hidden option!
5801 */
5802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005803showoneopt(
5804 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005805 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005807 char_u *varp;
5808 int save_silent = silent_mode;
5809
5810 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005811 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
5813 varp = get_varp_scope(p, opt_flags);
5814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005815 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5817 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005818 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005820 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005822 msg_puts(" ");
5823 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (!(p->flags & P_BOOL))
5825 {
5826 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005827 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 option_value2string(p, opt_flags);
5829 msg_outtrans(NameBuff);
5830 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005831
5832 silent_mode = save_silent;
5833 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834}
5835
5836/*
5837 * Write modified options as ":set" commands to a file.
5838 *
5839 * There are three values for "opt_flags":
5840 * OPT_GLOBAL: Write global option values and fresh values of
5841 * buffer-local options (used for start of a session
5842 * file).
5843 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5844 * curwin (used for a vimrc file).
5845 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5846 * and local values for window-local options of
5847 * curwin. Local values are also written when at the
5848 * default value, because a modeline or autocommand
5849 * may have set them when doing ":edit file" and the
5850 * user has set them back at the default or fresh
5851 * value.
5852 * When "local_only" is TRUE, don't write fresh
5853 * values, only local values (for ":mkview").
5854 * (fresh value = value used for a new buffer or window for a local option).
5855 *
5856 * Return FAIL on error, OK otherwise.
5857 */
5858 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005859makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005862 char_u *varp; // currently used value
5863 char_u *varp_fresh; // local value
5864 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 char *cmd;
5866 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005867 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869 /*
5870 * The options that don't have a default (terminal name, columns, lines)
5871 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005872 * Do the loop over "options[]" twice: once for options with the
5873 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005875 for (pri = 1; pri >= 0; --pri)
5876 {
5877 for (p = &options[0]; !istermoption(p); p++)
5878 if (!(p->flags & P_NO_MKRC)
5879 && !istermoption(p)
5880 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005882 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5884 continue;
5885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005886 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5887 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5889 continue;
5890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005891 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005893 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 continue;
5895
Bram Moolenaard23b7142021-04-17 21:04:34 +02005896 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5897 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005898 continue;
5899
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 round = 2;
5901 if (p->indir != PV_NONE)
5902 {
5903 if (p->var == VAR_WIN)
5904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005905 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (!(opt_flags & OPT_LOCAL))
5907 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005908 // When fresh value of window-local option is not at the
5909 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5911 {
5912 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005913 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
5915 round = 1;
5916 varp_local = varp;
5917 varp = varp_fresh;
5918 }
5919 }
5920 }
5921 }
5922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005923 // Round 1: fresh value for window-local options.
5924 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 for ( ; round <= 2; varp = varp_local, ++round)
5926 {
5927 if (round == 1 || (opt_flags & OPT_GLOBAL))
5928 cmd = "set";
5929 else
5930 cmd = "setlocal";
5931
5932 if (p->flags & P_BOOL)
5933 {
5934 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5935 return FAIL;
5936 }
5937 else if (p->flags & P_NUM)
5938 {
5939 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5940 return FAIL;
5941 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005942 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005944 int do_endif = FALSE;
5945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005946 // Don't set 'syntax' and 'filetype' again if the value is
5947 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005948 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005949#if defined(FEAT_SYN_HL)
5950 p->indir == PV_SYN ||
5951#endif
5952 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5955 *(char_u **)(varp)) < 0
5956 || put_eol(fd) < 0)
5957 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005958 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 }
5960 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005961 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005963 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
5965 if (put_line(fd, "endif") == FAIL)
5966 return FAIL;
5967 }
5968 }
5969 }
5970 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 return OK;
5973}
5974
5975#if defined(FEAT_FOLDING) || defined(PROTO)
5976/*
5977 * Generate set commands for the local fold options only. Used when
5978 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5979 */
5980 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005981makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005983 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005985 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 == FAIL
5987# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005988 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005990 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 == FAIL
5992 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5993 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5994 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5995 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5996 )
5997 return FAIL;
5998
5999 return OK;
6000}
6001#endif
6002
6003 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006004put_setstring(
6005 FILE *fd,
6006 char *cmd,
6007 char *name,
6008 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006009 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006012 char_u *buf = NULL;
6013 char_u *part = NULL;
6014 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
6016 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6017 return FAIL;
6018 if (*valuep != NULL)
6019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006020 // Output 'pastetoggle' as key names. For other
6021 // options some characters have to be escaped with
6022 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 if (valuep == &p_pt)
6024 {
6025 s = *valuep;
6026 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01006027 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 return FAIL;
6029 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006030 // expand the option value, replace $HOME by ~
6031 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006033 int size = (int)STRLEN(*valuep) + 1;
6034
6035 // replace home directory in the whole option value into "buf"
6036 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02006037 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006038 goto fail;
6039 home_replace(NULL, *valuep, buf, size, FALSE);
6040
6041 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01006042 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006043 // can be expanded when read back.
6044 if (size >= MAXPATHL && (flags & P_COMMA) != 0
6045 && vim_strchr(*valuep, ',') != NULL)
6046 {
6047 part = alloc(size);
6048 if (part == NULL)
6049 goto fail;
6050
6051 // write line break to clear the option, e.g. ':set rtp='
6052 if (put_eol(fd) == FAIL)
6053 goto fail;
6054
6055 p = buf;
6056 while (*p != NUL)
6057 {
6058 // for each comma separated option part, append value to
6059 // the option, :set rtp+=value
6060 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
6061 goto fail;
6062 (void)copy_option_part(&p, part, size, ",");
6063 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
6064 goto fail;
6065 }
6066 vim_free(buf);
6067 vim_free(part);
6068 return OK;
6069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02006071 {
6072 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02006074 }
6075 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077 else if (put_escstr(fd, *valuep, 2) == FAIL)
6078 return FAIL;
6079 }
6080 if (put_eol(fd) < 0)
6081 return FAIL;
6082 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01006083fail:
6084 vim_free(buf);
6085 vim_free(part);
6086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087}
6088
6089 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006090put_setnum(
6091 FILE *fd,
6092 char *cmd,
6093 char *name,
6094 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 long wc;
6097
6098 if (fprintf(fd, "%s %s=", cmd, name) < 0)
6099 return FAIL;
6100 if (wc_use_keyname((char_u *)valuep, &wc))
6101 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006102 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
6104 return FAIL;
6105 }
6106 else if (fprintf(fd, "%ld", *valuep) < 0)
6107 return FAIL;
6108 if (put_eol(fd) < 0)
6109 return FAIL;
6110 return OK;
6111}
6112
6113 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006114put_setbool(
6115 FILE *fd,
6116 char *cmd,
6117 char *name,
6118 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006120 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00006121 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
6123 || put_eol(fd) < 0)
6124 return FAIL;
6125 return OK;
6126}
6127
6128/*
6129 * Clear all the terminal options.
6130 * If the option has been allocated, free the memory.
6131 * Terminal options are never hidden or indirect.
6132 */
6133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006134clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006136 // Reset a few things before clearing the old options. This may cause
6137 // outputting a few things that the terminal doesn't understand, but the
6138 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006139 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006140 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006142 // When starting the GUI close the display opened for the clipboard.
6143 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (gui.starting)
6145 clear_xterm_clip();
6146#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006147 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006149 free_termoptions();
6150}
6151
6152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006153free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006154{
6155 struct vimoption *p;
6156
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006157 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (istermoption(p))
6159 {
6160 if (p->flags & P_ALLOCED)
6161 free_string_option(*(char_u **)(p->var));
6162 if (p->flags & P_DEF_ALLOCED)
6163 free_string_option(p->def_val[VI_DEFAULT]);
6164 *(char_u **)(p->var) = empty_option;
6165 p->def_val[VI_DEFAULT] = empty_option;
6166 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006167#ifdef FEAT_EVAL
6168 // remember where the option was cleared
6169 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 }
6172 clear_termcodes();
6173}
6174
6175/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006176 * Free the string for one term option, if it was allocated.
6177 * Set the string to empty_option and clear allocated flag.
6178 * "var" points to the option value.
6179 */
6180 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006181free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006182{
6183 struct vimoption *p;
6184
6185 for (p = &options[0]; p->fullname != NULL; p++)
6186 if (p->var == var)
6187 {
6188 if (p->flags & P_ALLOCED)
6189 free_string_option(*(char_u **)(p->var));
6190 *(char_u **)(p->var) = empty_option;
6191 p->flags &= ~P_ALLOCED;
6192 break;
6193 }
6194}
6195
6196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 * Set the terminal option defaults to the current value.
6198 * Used after setting the terminal name.
6199 */
6200 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006201set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 struct vimoption *p;
6204
6205 for (p = &options[0]; p->fullname != NULL; p++)
6206 {
6207 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6208 {
6209 if (p->flags & P_DEF_ALLOCED)
6210 {
6211 free_string_option(p->def_val[VI_DEFAULT]);
6212 p->flags &= ~P_DEF_ALLOCED;
6213 }
6214 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6215 if (p->flags & P_ALLOCED)
6216 {
6217 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006218 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220 }
6221 }
6222}
6223
6224/*
6225 * return TRUE if 'p' starts with 't_'
6226 */
6227 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006228istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6231}
6232
Bram Moolenaardac13472019-09-16 21:06:21 +02006233/*
6234 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6235 */
6236 int
6237istermoption_idx(int opt_idx)
6238{
6239 return istermoption(&options[opt_idx]);
6240}
6241
Bram Moolenaar113e1072019-01-20 15:30:40 +01006242#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006244 * Unset local option value, similar to ":set opt<".
6245 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006247unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006248{
6249 struct vimoption *p;
6250 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006251 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006252
6253 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006254 if (opt_idx < 0)
6255 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006256 p = &(options[opt_idx]);
6257
6258 switch ((int)p->indir)
6259 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006260 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006261 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006262 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006263 break;
6264 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006265 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006266 break;
6267 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006268 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006269 break;
6270 case PV_AR:
6271 buf->b_p_ar = -1;
6272 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006273 case PV_BKC:
6274 clear_string_option(&buf->b_p_bkc);
6275 buf->b_bkc_flags = 0;
6276 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006277 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006278 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006279 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006280 case PV_TC:
6281 clear_string_option(&buf->b_p_tc);
6282 buf->b_tc_flags = 0;
6283 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006284 case PV_SISO:
6285 curwin->w_p_siso = -1;
6286 break;
6287 case PV_SO:
6288 curwin->w_p_so = -1;
6289 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006290# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006291 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006292 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006293 break;
6294 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006295 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006296 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006297# endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006298 case PV_COT:
6299 clear_string_option(&buf->b_p_cot);
6300 buf->b_cot_flags = 0;
6301 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006302 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006303 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006304 break;
6305 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006306 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006307 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006308# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006309 case PV_TSRFU:
6310 clear_string_option(&buf->b_p_tsrfu);
6311 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006312# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006313 case PV_FP:
6314 clear_string_option(&buf->b_p_fp);
6315 break;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006316# ifdef FEAT_EVAL
6317 case PV_FEXPR:
6318 clear_string_option(&buf->b_p_fexpr);
6319 break;
6320# endif
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006321# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006322 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006323 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006324 break;
6325 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006326 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006327 break;
6328 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006329 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006330 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006331# endif
6332# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006333 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006334 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006335 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006336# endif
6337# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006338 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006339 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006340 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006341# endif
6342# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006343 case PV_SBR:
6344 clear_string_option(&((win_T *)from)->w_p_sbr);
6345 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006346# endif
6347# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006348 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006349 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006350 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006351# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006352 case PV_UL:
6353 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6354 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006355 case PV_LW:
6356 clear_string_option(&buf->b_p_lw);
6357 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006358 case PV_MENC:
6359 clear_string_option(&buf->b_p_menc);
6360 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006361 case PV_LCS:
6362 clear_string_option(&((win_T *)from)->w_p_lcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006363 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE,
6364 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006365 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006366 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006367 case PV_FCS:
6368 clear_string_option(&((win_T *)from)->w_p_fcs);
zeertzjq6a8d2e12024-01-17 20:54:49 +01006369 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE,
6370 NULL, 0);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006371 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006372 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006373 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006374 clear_string_option(&((win_T *)from)->w_p_ve);
6375 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006376 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006377 }
6378}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006379#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006380
6381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006383 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 */
6385 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006386get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006388 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 {
6390 if (p->var == VAR_WIN)
6391 return (char_u *)GLOBAL_WO(get_varp(p));
6392 return p->var;
6393 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006394 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 {
6396 switch ((int)p->indir)
6397 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006398 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006399#ifdef FEAT_EVAL
6400 case PV_FEXPR: return (char_u *)&(curbuf->b_p_fexpr);
6401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006403 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6404 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6405 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006407 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6408 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6409 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006410 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006411 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006412 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006413 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6414 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006416 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6417 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006419 case PV_COT: return (char_u *)&(curbuf->b_p_cot);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006420 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6421 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006422#ifdef FEAT_COMPL_FUNC
6423 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6424#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006425#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6426 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6427#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006428#if defined(FEAT_CRYPT)
6429 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6430#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006431#ifdef FEAT_LINEBREAK
6432 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6433#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006434#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006435 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006436#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006437 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006438 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006439 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006440 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006441 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006442 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006443 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006444
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006446 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
6448 return get_varp(p);
6449}
6450
6451/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006452 * Get pointer to option variable at 'opt_idx', depending on local or global
6453 * scope.
6454 */
6455 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006456get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006457{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006458 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006459}
6460
6461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 * Get pointer to option variable.
6463 */
6464 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006465get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (p->var == NULL)
6469 return NULL;
6470
6471 switch ((int)p->indir)
6472 {
6473 case PV_NONE: return p->var;
6474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006475 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006476 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006478 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006480 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006482 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006484 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006486 case PV_TC: return *curbuf->b_p_tc != NUL
6487 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006488 case PV_BKC: return *curbuf->b_p_bkc != NUL
6489 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006490 case PV_SISO: return curwin->w_p_siso >= 0
6491 ? (char_u *)&(curwin->w_p_siso) : p->var;
6492 case PV_SO: return curwin->w_p_so >= 0
6493 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006495 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006497 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6499#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02006500 case PV_COT: return *curbuf->b_p_cot != NUL
6501 ? (char_u *)&(curbuf->b_p_cot) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006502 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006504 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006506#ifdef FEAT_COMPL_FUNC
6507 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6508 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6509#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006510 case PV_FP: return *curbuf->b_p_fp != NUL
6511 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006512#ifdef FEAT_EVAL
6513 case PV_FEXPR: return *curbuf->b_p_fexpr != NUL
6514 ? (char_u *)&curbuf->b_p_fexpr : p->var;
6515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006517 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006519 case PV_GP: return *curbuf->b_p_gp != NUL
6520 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6521 case PV_MP: return *curbuf->b_p_mp != NUL
6522 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006524#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6525 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6526 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6527#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006528#if defined(FEAT_CRYPT)
6529 case PV_CM: return *curbuf->b_p_cm != NUL
6530 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6531#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006532#ifdef FEAT_LINEBREAK
6533 case PV_SBR: return *curwin->w_p_sbr != NUL
6534 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6535#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006536#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006537 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006538 ? (char_u *)&(curwin->w_p_stl) : p->var;
6539#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006540 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6541 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006542 case PV_LW: return *curbuf->b_p_lw != NUL
6543 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006544 case PV_MENC: return *curbuf->b_p_menc != NUL
6545 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546#ifdef FEAT_ARABIC
6547 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6548#endif
6549 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006550 case PV_LCS: return *curwin->w_p_lcs != NUL
6551 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006552 case PV_FCS: return *curwin->w_p_fcs != NUL
6553 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006554 case PV_VE: return *curwin->w_p_ve != NUL
6555 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006556#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006557 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6558#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006559#ifdef FEAT_SYN_HL
6560 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6561 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006562 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006563 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565#ifdef FEAT_DIFF
6566 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6567#endif
6568#ifdef FEAT_FOLDING
6569 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6570 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6571 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6572 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6573 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6574 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6575 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6576# ifdef FEAT_EVAL
6577 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6578 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6579# endif
6580 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6581#endif
6582 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006583 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006584#ifdef FEAT_LINEBREAK
6585 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6586#endif
Colin Kennedy21570352024-03-03 16:16:47 +01006587 case PV_WFB: return (char_u *)&(curwin->w_p_wfb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006589 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006590#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6592#endif
6593#ifdef FEAT_RIGHTLEFT
6594 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6595 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6596#endif
6597 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006598 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6600#ifdef FEAT_LINEBREAK
6601 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006602 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6603 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006605 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006607 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006608#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006609 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6610 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6611#endif
6612#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006613 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6614 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6615 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6619 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6622 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6624 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6626 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6627 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006628 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631#ifdef FEAT_FOLDING
6632 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006635#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006636 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006638#ifdef FEAT_COMPL_FUNC
6639 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006640 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006641#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006642#ifdef FEAT_EVAL
6643 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6644#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006645 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006647 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006653 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6655 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6656 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6657 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6658#ifdef FEAT_FIND_ID
6659# ifdef FEAT_EVAL
6660 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6661# endif
6662#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006663#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6665 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6666#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006667#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006668 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670#ifdef FEAT_CRYPT
6671 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006674 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6676 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6677 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6678 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6679 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006681 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6688#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006689 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006690 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6691#endif
6692#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006693 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6694 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6695 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006696 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697#endif
6698 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6699 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6700 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6701 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006702#ifdef FEAT_PERSISTENT_UNDO
6703 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6706#ifdef FEAT_KEYMAP
6707 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6708#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006709#ifdef FEAT_SIGNS
6710 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6711#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006712#ifdef FEAT_VARTABS
6713 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6714 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6715#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006716 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006718 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 return (char_u *)&(curbuf->b_p_wm);
6720}
6721
6722/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006723 * Return a pointer to the variable for option at 'opt_idx'
6724 */
6725 char_u *
6726get_option_var(int opt_idx)
6727{
6728 return options[opt_idx].var;
6729}
6730
Dominique Pelle748b3082022-01-08 12:41:16 +00006731#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006732/*
6733 * Return the full name of the option at 'opt_idx'
6734 */
6735 char_u *
6736get_option_fullname(int opt_idx)
6737{
6738 return (char_u *)options[opt_idx].fullname;
6739}
Dominique Pelle748b3082022-01-08 12:41:16 +00006740#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006741
6742/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006743 * Return the did_set callback function for the option at 'opt_idx'
6744 */
6745 opt_did_set_cb_T
6746get_option_did_set_cb(int opt_idx)
6747{
6748 return options[opt_idx].opt_did_set_cb;
6749}
6750
6751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 * Get the value of 'equalprg', either the buffer-local one or the global one.
6753 */
6754 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006755get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 if (*curbuf->b_p_ep == NUL)
6758 return p_ep;
6759 return curbuf->b_p_ep;
6760}
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762/*
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02006763 * Get the value of 'findexpr', either the buffer-local one or the global one.
6764 */
6765 char_u *
6766get_findexpr(void)
6767{
6768#ifdef FEAT_EVAL
6769 if (*curbuf->b_p_fexpr == NUL)
6770 return p_fexpr;
6771 return curbuf->b_p_fexpr;
6772#else
6773 return (char_u *)"";
6774#endif
6775}
6776
6777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 * Copy options from one window to another.
6779 * Used when splitting a window.
6780 */
6781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006782win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6785 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006786 after_copy_winopt(wp_to);
6787}
6788
6789/*
6790 * After copying window options: update variables depending on options.
6791 */
6792 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006793after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006794{
Christian Brabandtb065a102024-10-05 17:30:22 +02006795 // Set w_leftcol or w_skipcol to zero.
6796 if (wp->w_p_wrap)
6797 wp->w_leftcol = 0;
6798 else
6799 wp->w_skipcol = 0;
Bram Moolenaar010ee962019-09-25 20:37:36 +02006800#ifdef FEAT_LINEBREAK
Millyb38700a2024-10-22 22:59:39 +02006801 briopt_check(NULL, wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006802#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006803#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006804 fill_culopt_flags(NULL, wp);
Millya441a3e2024-10-22 22:43:01 +02006805 check_colorcolumn(NULL, wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006806#endif
zeertzjq6a8d2e12024-01-17 20:54:49 +01006807 set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
6808 set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006809}
6810
6811 static char_u *
6812copy_option_val(char_u *val)
6813{
6814 if (val == empty_option)
6815 return empty_option; // no need to allocate memory
6816 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819/*
6820 * Copy the options from one winopt_T to another.
6821 * Doesn't free the old option values in "to", use clear_winopt() for that.
6822 * The 'scroll' option is not copied, because it depends on the window height.
6823 * The 'previewwindow' option is reset, there can be only one preview window.
6824 */
6825 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006826copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828#ifdef FEAT_ARABIC
6829 to->wo_arab = from->wo_arab;
6830#endif
6831 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006832 to->wo_lcs = copy_option_val(from->wo_lcs);
6833 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006835 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006836 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006837 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006838#ifdef FEAT_LINEBREAK
6839 to->wo_nuw = from->wo_nuw;
6840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841#ifdef FEAT_RIGHTLEFT
6842 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006843 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006845#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006846 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006847#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006848#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006849 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006852#ifdef FEAT_DIFF
6853 to->wo_wrap_save = from->wo_wrap_save;
6854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855#ifdef FEAT_LINEBREAK
6856 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006857 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006858 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006860 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006862 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006863 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006864 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006865 to->wo_crb_save = from->wo_crb_save;
Christian Brabandt4a8eb6e2023-08-13 19:43:42 +02006866 to->wo_siso = from->wo_siso;
6867 to->wo_so = from->wo_so;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006868#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006869 to->wo_spell = from->wo_spell;
6870#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006871#ifdef FEAT_SYN_HL
6872 to->wo_cuc = from->wo_cuc;
6873 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006874 to->wo_culopt = copy_option_val(from->wo_culopt);
6875 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877#ifdef FEAT_DIFF
6878 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006879 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006881#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006882 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006883 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006884#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006885#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006886 to->wo_twk = copy_option_val(from->wo_twk);
6887 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889#ifdef FEAT_FOLDING
6890 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006891 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006893 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006894 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 to->wo_fml = from->wo_fml;
6896 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006897 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006898 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006899 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006900 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 to->wo_fdn = from->wo_fdn;
6902# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006903 to->wo_fde = copy_option_val(from->wo_fde);
6904 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006906 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006908#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006909 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006910#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006911
6912#ifdef FEAT_EVAL
6913 // Copy the script context so that we know where the value was last set.
6914 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6915 sizeof(to->wo_script_ctx));
6916#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006917 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918}
6919
6920/*
6921 * Check string options in a window for a NULL value.
6922 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006923 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006924check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 check_winopt(&win->w_onebuf_opt);
6927 check_winopt(&win->w_allbuf_opt);
6928}
6929
6930/*
6931 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6932 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006933 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006934check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936#ifdef FEAT_FOLDING
6937 check_string_option(&wop->wo_fdi);
6938 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006939 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940# ifdef FEAT_EVAL
6941 check_string_option(&wop->wo_fde);
6942 check_string_option(&wop->wo_fdt);
6943# endif
6944 check_string_option(&wop->wo_fmr);
6945#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006946#ifdef FEAT_SIGNS
6947 check_string_option(&wop->wo_scl);
6948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949#ifdef FEAT_RIGHTLEFT
6950 check_string_option(&wop->wo_rlc);
6951#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006952#ifdef FEAT_LINEBREAK
6953 check_string_option(&wop->wo_sbr);
6954#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006955#ifdef FEAT_STL_OPT
6956 check_string_option(&wop->wo_stl);
6957#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006958#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006959 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006960 check_string_option(&wop->wo_cc);
6961#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006962#ifdef FEAT_CONCEAL
6963 check_string_option(&wop->wo_cocu);
6964#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006965#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006966 check_string_option(&wop->wo_twk);
6967 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006968#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006969#ifdef FEAT_LINEBREAK
6970 check_string_option(&wop->wo_briopt);
6971#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006972 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006973 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006974 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006975 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976}
6977
6978/*
6979 * Free the allocated memory inside a winopt_T.
6980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006982clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984#ifdef FEAT_FOLDING
6985 clear_string_option(&wop->wo_fdi);
6986 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006987 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988# ifdef FEAT_EVAL
6989 clear_string_option(&wop->wo_fde);
6990 clear_string_option(&wop->wo_fdt);
6991# endif
6992 clear_string_option(&wop->wo_fmr);
6993#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006994#ifdef FEAT_SIGNS
6995 clear_string_option(&wop->wo_scl);
6996#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006997#ifdef FEAT_LINEBREAK
6998 clear_string_option(&wop->wo_briopt);
6999#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007000 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#ifdef FEAT_RIGHTLEFT
7002 clear_string_option(&wop->wo_rlc);
7003#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01007004#ifdef FEAT_LINEBREAK
7005 clear_string_option(&wop->wo_sbr);
7006#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007007#ifdef FEAT_STL_OPT
7008 clear_string_option(&wop->wo_stl);
7009#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02007010#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02007011 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02007012 clear_string_option(&wop->wo_cc);
7013#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007014#ifdef FEAT_CONCEAL
7015 clear_string_option(&wop->wo_cocu);
7016#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007017#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007018 clear_string_option(&wop->wo_twk);
7019 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007020#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01007021 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01007022 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02007023 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024}
7025
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007026#ifdef FEAT_EVAL
7027// Index into the options table for a buffer-local option enum.
7028static int buf_opt_idx[BV_COUNT];
7029# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
7030
7031/*
7032 * Initialize buf_opt_idx[] if not done already.
7033 */
7034 static void
7035init_buf_opt_idx(void)
7036{
7037 static int did_init_buf_opt_idx = FALSE;
7038 int i;
7039
7040 if (did_init_buf_opt_idx)
7041 return;
7042 did_init_buf_opt_idx = TRUE;
7043 for (i = 0; !istermoption_idx(i); i++)
7044 if (options[i].indir & PV_BUF)
7045 buf_opt_idx[options[i].indir & PV_MASK] = i;
7046}
7047#else
7048# define COPY_OPT_SCTX(buf, bv)
7049#endif
7050
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051/*
7052 * Copy global option values to local options for one buffer.
7053 * Used when creating a new buffer and sometimes when entering a buffer.
7054 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007055 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
7057 * appropriate.
7058 * BCO_NOHELP Don't copy the values to a help buffer.
7059 */
7060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007061buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 int dont_do_help;
7066 int did_isk = FALSE;
7067
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007068 // Skip this when the option defaults have not been set yet. Happens when
7069 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (p_cpo != NULL)
7071 {
7072 /*
7073 * Always copy when entering and 'cpo' contains 'S'.
7074 * Don't copy when already initialized.
7075 * Don't copy when 'cpo' contains 's' and not entering.
7076 * 'S' BCO_ENTER initialized 's' should_copy
7077 * yes yes X X TRUE
7078 * yes no yes X FALSE
7079 * no X yes X FALSE
7080 * X no no yes FALSE
7081 * X no no no TRUE
7082 * no yes no X TRUE
7083 */
7084 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
7085 && (buf->b_p_initialized
7086 || (!(flags & BCO_ENTER)
7087 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
7088 should_copy = FALSE;
7089
7090 if (should_copy || (flags & BCO_ALWAYS))
7091 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007092#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02007093 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007094 init_buf_opt_idx();
7095#endif
7096 // Don't copy the options specific to a help buffer when
7097 // BCO_NOHELP is given or the options were initialized already
7098 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
7100 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007101 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
7103 save_p_isk = buf->b_p_isk;
7104 buf->b_p_isk = NULL;
7105 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007106 // Always free the allocated strings. If not already initialized,
7107 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 if (!buf->b_p_initialized)
7109 {
7110 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007111 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02007114 switch (*p_ffs)
7115 {
7116 case 'm':
7117 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
7118 case 'd':
7119 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
7120 case 'u':
7121 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
7122 default:
7123 buf->b_p_ff = vim_strsave(p_ff);
7124 }
7125 if (buf->b_p_ff != NULL)
7126 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 buf->b_p_bh = empty_option;
7128 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
7130 else
7131 free_buf_options(buf, FALSE);
7132
7133 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007134 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 buf->b_p_ai_nopaste = p_ai_nopaste;
7136 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007137 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007139 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 buf->b_p_tw_nopaste = p_tw_nopaste;
7141 buf->b_p_tw_nobin = p_tw_nobin;
7142 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007143 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 buf->b_p_wm_nopaste = p_wm_nopaste;
7145 buf->b_p_wm_nobin = p_wm_nobin;
7146 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007147 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00007148 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007149 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02007150 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007151 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007153 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007155 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007157 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 buf->b_p_ml_nobin = p_ml_nobin;
7159 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007160 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02007161 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007162 buf->b_p_swf = FALSE;
7163 else
7164 {
7165 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00007166 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007169 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007170#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02007171 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007172 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007174#ifdef FEAT_COMPL_FUNC
7175 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007176 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007177 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007178 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007179 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007180 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007181#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007182#ifdef FEAT_EVAL
7183 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007184 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00007185 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007188 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007190#ifdef FEAT_VARTABS
7191 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007192 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007193 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007194 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007195 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007196 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007197 buf->b_p_vsts_nopaste = p_vsts_nopaste
7198 ? vim_strsave(p_vsts_nopaste) : NULL;
7199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007201 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007203 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204#ifdef FEAT_FOLDING
7205 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007206 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207#endif
7208 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007209 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007210 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007211 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007212 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7213 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007215 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007217 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007219 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007221 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007224 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007226 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007228 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007229 buf->b_p_cinsd = vim_strsave(p_cinsd);
7230 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007231 buf->b_p_lop = vim_strsave(p_lop);
7232 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007233
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007234 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007237 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007239 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007241 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007243 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007245 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007246 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007247 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007248#endif
7249#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007250 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007251 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007252 (void)compile_cap_prog(&buf->b_s);
7253 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007254 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007255 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007256 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007257 buf->b_s.b_p_spo = vim_strsave(p_spo);
7258 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007260#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007262 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007264 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007266 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007267#if defined(FEAT_EVAL)
7268 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007269 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271#ifdef FEAT_CRYPT
7272 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007273 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007276 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277#ifdef FEAT_KEYMAP
7278 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007279 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 buf->b_kmap_state |= KEYMAP_INIT;
7281#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007282#ifdef FEAT_TERMINAL
7283 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007284 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007285#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007286 // This isn't really an option, but copying the langmap and IME
7287 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007289 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007291 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007293 // options that are normally global but also have a local value
7294 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007296 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007297 buf->b_p_bkc = empty_option;
7298 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299#ifdef FEAT_QUICKFIX
7300 buf->b_p_gp = empty_option;
7301 buf->b_p_mp = empty_option;
7302 buf->b_p_efm = empty_option;
7303#endif
7304 buf->b_p_ep = empty_option;
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02007305#if defined(FEAT_EVAL)
7306 buf->b_p_fexpr = vim_strsave(p_fexpr);
7307 COPY_OPT_SCTX(buf, BV_FEXPR);
7308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 buf->b_p_kp = empty_option;
7310 buf->b_p_path = empty_option;
7311 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007312 buf->b_p_tc = empty_option;
7313 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314#ifdef FEAT_FIND_ID
7315 buf->b_p_def = empty_option;
7316 buf->b_p_inc = empty_option;
7317# ifdef FEAT_EVAL
7318 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007319 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320# endif
7321#endif
zeertzjq529b9ad2024-06-05 20:27:06 +02007322 buf->b_p_cot = empty_option;
7323 buf->b_cot_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 buf->b_p_dict = empty_option;
7325 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007326#ifdef FEAT_COMPL_FUNC
7327 buf->b_p_tsrfu = empty_option;
7328#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007329 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007330 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007331#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7332 buf->b_p_bexpr = empty_option;
7333#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007334#if defined(FEAT_CRYPT)
7335 buf->b_p_cm = empty_option;
7336#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007337#ifdef FEAT_PERSISTENT_UNDO
7338 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007339 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007340#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007341 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007342 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007344 // Don't copy the options set by ex_help(), use the saved values,
7345 // when going from a help buffer to a non-help buffer.
7346 // Don't touch these at all when BCO_NOHELP is used and going from
7347 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007351#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007352 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007353 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007354 else
7355 buf->b_p_vts_array = NULL;
7356#endif
7357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 else
7359 {
7360 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007361 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 did_isk = TRUE;
7363 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007364 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007365#ifdef FEAT_VARTABS
7366 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007367 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007368 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007369 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007370 else
7371 buf->b_p_vts_array = NULL;
7372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 if (buf->b_p_bt[0] == 'h')
7375 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007377 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
7379 }
7380
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007381 // When the options should be copied (ignoring BCO_ALWAYS), set the
7382 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 if (should_copy)
7384 buf->b_p_initialized = TRUE;
7385 }
7386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007387 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 if (did_isk)
7389 (void)buf_init_chartab(buf, FALSE);
7390}
7391
7392/*
7393 * Reset the 'modifiable' option and its default value.
7394 */
7395 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007396reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 int opt_idx;
7399
7400 curbuf->b_p_ma = FALSE;
7401 p_ma = FALSE;
7402 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007403 if (opt_idx >= 0)
7404 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405}
7406
7407/*
7408 * Set the global value for 'iminsert' to the local value.
7409 */
7410 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007411set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
7413 p_iminsert = curbuf->b_p_iminsert;
7414}
7415
7416/*
7417 * Set the global value for 'imsearch' to the local value.
7418 */
7419 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007420set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 p_imsearch = curbuf->b_p_imsearch;
7423}
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425static int expand_option_idx = -1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007426static int expand_option_start_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7428static int expand_option_flags = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007429static int expand_option_append = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007432set_context_in_set_cmd(
7433 expand_T *xp,
7434 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007435 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
7437 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007438 long_u flags = 0; // init for GCC
7439 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 char_u *p;
7441 char_u *s;
7442 int is_term_option = FALSE;
7443 int key;
John Marriott95dacbb2024-09-10 21:25:14 +02007444 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
7446 expand_option_flags = opt_flags;
7447
7448 xp->xp_context = EXPAND_SETTINGS;
7449 if (*arg == NUL)
7450 {
7451 xp->xp_pattern = arg;
7452 return;
7453 }
John Marriott95dacbb2024-09-10 21:25:14 +02007454 argend = arg + STRLEN(arg);
7455 p = argend - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (*p == ' ' && *(p - 1) != '\\')
7457 {
7458 xp->xp_pattern = p + 1;
7459 return;
7460 }
7461 while (p > arg)
7462 {
7463 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007464 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (*p == ' ' || *p == ',')
7466 {
7467 while (s > arg && *(s - 1) == '\\')
7468 --s;
7469 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007470 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (*p == ' ' && ((p - s) & 1) == 0)
7472 {
7473 ++p;
7474 break;
7475 }
7476 --p;
7477 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007478 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
7480 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007481 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 p += 2;
7483 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007484 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
7486 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007487 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 p += 3;
7489 }
7490 xp->xp_pattern = arg = p;
7491 if (*arg == '<')
7492 {
7493 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007494 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return;
7496 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007497 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {
7499 xp->xp_context = EXPAND_NOTHING;
7500 return;
7501 }
7502 nextchar = *++p;
7503 is_term_option = TRUE;
7504 expand_option_name[2] = KEY2TERMCAP0(key);
7505 expand_option_name[3] = KEY2TERMCAP1(key);
7506 }
7507 else
7508 {
7509 if (p[0] == 't' && p[1] == '_')
7510 {
7511 p += 2;
7512 if (*p != NUL)
7513 ++p;
7514 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007515 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 nextchar = *++p;
7517 is_term_option = TRUE;
7518 expand_option_name[2] = p[-2];
7519 expand_option_name[3] = p[-1];
7520 }
7521 else
7522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007523 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7525 p++;
7526 if (*p == NUL)
7527 return;
7528 nextchar = *p;
7529 *p = NUL;
7530 opt_idx = findoption(arg);
7531 *p = nextchar;
7532 if (opt_idx == -1 || options[opt_idx].var == NULL)
7533 {
7534 xp->xp_context = EXPAND_NOTHING;
7535 return;
7536 }
7537 flags = options[opt_idx].flags;
7538 if (flags & P_BOOL)
7539 {
7540 xp->xp_context = EXPAND_NOTHING;
7541 return;
7542 }
7543 }
7544 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007545 // handle "-=" and "+="
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007546 expand_option_append = FALSE;
7547 int expand_option_subtract = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7549 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007550 if (nextchar == '-')
7551 expand_option_subtract = TRUE;
7552 if (nextchar == '+' || nextchar == '^')
7553 expand_option_append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 ++p;
7555 nextchar = '=';
7556 }
7557 if ((nextchar != '=' && nextchar != ':')
7558 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7559 {
7560 xp->xp_context = EXPAND_UNSUCCESSFUL;
7561 return;
7562 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007563
7564 // Below are for handling expanding a specific option's value after the '='
7565 // or ':'
7566
7567 if (is_term_option)
7568 expand_option_idx = -1;
7569 else
7570 expand_option_idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02007572 if (!is_term_option)
7573 {
7574 if (options[opt_idx].flags & P_NO_CMD_EXPAND)
7575 {
7576 xp->xp_context=EXPAND_UNSUCCESSFUL;
7577 return;
7578 }
7579 }
7580
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 xp->xp_pattern = p + 1;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007582 expand_option_start_col = (int)(p + 1 - xp->xp_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007584 // Certain options currently have special case handling to reuse the
7585 // expansion logic with other commands.
Doug Kearns6dfdff32023-08-27 18:48:51 +02007586#ifdef FEAT_SYN_HL
7587 if (options[opt_idx].var == (char_u *)&p_syn)
7588 {
7589 xp->xp_context = EXPAND_OWNSYNTAX;
7590 return;
7591 }
7592#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007593 if (options[opt_idx].var == (char_u *)&p_ft)
7594 {
7595 xp->xp_context = EXPAND_FILETYPE;
7596 return;
7597 }
Doug Kearns81642d92024-01-04 22:37:44 +01007598#ifdef FEAT_KEYMAP
7599 if (options[opt_idx].var == (char_u *)&p_keymap)
7600 {
7601 xp->xp_context = EXPAND_KEYMAP;
7602 return;
7603 }
7604#endif
Doug Kearns6dfdff32023-08-27 18:48:51 +02007605
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007606 // Now pick. If the option has a custom expander, use that. Otherwise, just
7607 // fill with the existing option value.
7608 if (expand_option_subtract)
7609 {
7610 xp->xp_context = EXPAND_SETTING_SUBTRACT;
7611 return;
7612 }
7613 else if (expand_option_idx >= 0 &&
7614 options[expand_option_idx].opt_expand_cb != NULL)
7615 {
7616 xp->xp_context = EXPAND_STRING_SETTING;
7617 }
7618 else if (*xp->xp_pattern == NUL)
7619 {
7620 xp->xp_context = EXPAND_OLD_SETTING;
7621 return;
7622 }
7623 else
7624 xp->xp_context = EXPAND_NOTHING;
7625
7626 if (is_term_option || (flags & P_NUM))
7627 return;
7628
7629 // Only string options below
7630
7631 // Options that have P_EXPAND are considered to all use file/dir expansion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (flags & P_EXPAND)
7633 {
7634 p = options[opt_idx].var;
7635 if (p == (char_u *)&p_bdir
7636 || p == (char_u *)&p_dir
7637 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007638 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641#ifdef FEAT_SESSION
7642 || p == (char_u *)&p_vdir
7643#endif
7644 )
7645 {
7646 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007647 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 xp->xp_backslash = XP_BS_THREE;
7649 else
7650 xp->xp_backslash = XP_BS_ONE;
7651 }
7652 else
7653 {
7654 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007655 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 if (p == (char_u *)&p_tags)
7657 xp->xp_backslash = XP_BS_THREE;
7658 else
7659 xp->xp_backslash = XP_BS_ONE;
7660 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02007661 if (flags & P_COMMA)
7662 xp->xp_backslash |= XP_BS_COMMA;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007665 // For an option that is a list of file names, or comma/colon-separated
7666 // values, split it by the delimiter and find the start of the current
7667 // pattern, while accounting for backslash-escaped space/commas/colons.
7668 // Triple-backslashed escaped file names (e.g. 'path') can also be
7669 // delimited by space.
7670 if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {
John Marriott95dacbb2024-09-10 21:25:14 +02007672 for (p = argend - 1; p >= xp->xp_pattern; --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007674 // count number of backslashes before ' ' or ',' or ':'
7675 if (*p == ' ' || *p == ',' ||
7676 (*p == ':' && (flags & P_COLON)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007678 s = p;
7679 while (s > xp->xp_pattern && *(s - 1) == '\\')
7680 --s;
Yee Cheng Chin54844852023-10-09 18:12:31 +02007681 if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3))
7682#if defined(BACKSLASH_IN_FILENAME)
7683 || (*p == ',' && (flags & P_COMMA) && (p - s) < 1)
7684#else
7685 || (*p == ',' && (flags & P_COMMA) && (p - s) < 2)
7686#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007687 || (*p == ':' && (flags & P_COLON)))
7688 {
7689 xp->xp_pattern = p + 1;
7690 break;
7691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693 }
7694 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007695
7696 // An option that is a list of single-character flags should always start
7697 // at the end as we don't complete words.
7698 if (flags & P_FLAGLIST)
John Marriott95dacbb2024-09-10 21:25:14 +02007699 xp->xp_pattern = argend;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007700
7701 // Some options can either be using file/dir expansions, or custom value
7702 // expansion depending on what the user typed. Unfortunately we have to
7703 // manually handle it here to make sure we have the correct xp_context set.
7704#ifdef FEAT_SPELL
7705 if (options[opt_idx].var == (char_u *)&p_sps)
7706 {
7707 if (STRNCMP(xp->xp_pattern, "file:", 5) == 0)
7708 {
7709 xp->xp_pattern += 5;
7710 return;
7711 }
7712 else if (options[expand_option_idx].opt_expand_cb != NULL)
7713 {
7714 xp->xp_context = EXPAND_STRING_SETTING;
7715 }
7716 }
7717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
7719
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007720/*
7721 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7722 *
7723 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7724 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7725 *
7726 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7727 * regular expression 'regmatch', then stores the match in matches[idx] and
7728 * returns TRUE.
7729 *
7730 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7731 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7732 *
7733 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7734 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7735 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02007736 static int
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007737match_str(
7738 char_u *str,
7739 regmatch_T *regmatch,
7740 char_u **matches,
7741 int idx,
7742 int test_only,
7743 int fuzzy,
7744 char_u *fuzzystr,
7745 fuzmatch_str_T *fuzmatch)
7746{
7747 if (!fuzzy)
7748 {
7749 if (vim_regexec(regmatch, str, (colnr_T)0))
7750 {
7751 if (!test_only)
7752 matches[idx] = vim_strsave(str);
7753 return TRUE;
7754 }
7755 }
7756 else
7757 {
7758 int score;
7759
7760 score = fuzzy_match_str(str, fuzzystr);
7761 if (score != 0)
7762 {
7763 if (!test_only)
7764 {
7765 fuzmatch[idx].idx = idx;
7766 fuzmatch[idx].str = vim_strsave(str);
7767 fuzmatch[idx].score = score;
7768 }
7769
7770 return TRUE;
7771 }
7772 }
7773
7774 return FALSE;
7775}
7776
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007778ExpandSettings(
7779 expand_T *xp,
7780 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007781 char_u *fuzzystr,
7782 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007783 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007784 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007786 int num_normal = 0; // Nr of matching non-term-code settings
7787 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 int opt_idx;
7789 int match;
7790 int count = 0;
7791 char_u *str;
7792 int loop;
7793 int is_term_opt;
7794 char_u name_buf[MAX_KEY_NAME_LEN];
7795 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007796 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007797 int fuzzy;
7798 fuzmatch_str_T *fuzmatch = NULL;
7799
Christian Brabandtcb747892022-05-08 21:10:56 +01007800 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007802 // do this loop twice:
7803 // loop == 0: count the number of matching options
7804 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 for (loop = 0; loop <= 1; ++loop)
7806 {
7807 regmatch->rm_ic = ic;
7808 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7809 {
K.Takataeeec2542021-06-02 13:28:16 +02007810 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007811 {
7812 if (match_str((char_u *)names[match], regmatch, *matches,
7813 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {
7815 if (loop == 0)
7816 num_normal++;
7817 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007818 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 }
7822 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7823 opt_idx++)
7824 {
7825 if (options[opt_idx].var == NULL)
7826 continue;
7827 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007828 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007830 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 if (is_term_opt && num_normal > 0)
7832 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007833
7834 if (match_str(str, regmatch, *matches, count, (loop == 0),
7835 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {
7837 if (loop == 0)
7838 {
7839 if (is_term_opt)
7840 num_term++;
7841 else
7842 num_normal++;
7843 }
7844 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007845 count++;
7846 }
7847 else if (!fuzzy && options[opt_idx].shortname != NULL
7848 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007849 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007850 {
7851 // Compare against the abbreviated option name (for regular
7852 // expression match). Fuzzy matching (previous if) already
7853 // matches against both the expanded and abbreviated names.
7854 if (loop == 0)
7855 {
7856 if (is_term_opt)
7857 num_term++;
7858 else
7859 num_normal++;
7860 }
7861 else
7862 (*matches)[count++] = vim_strsave(str);
7863 }
7864 else if (is_term_opt)
7865 {
7866 name_buf[0] = '<';
7867 name_buf[1] = 't';
7868 name_buf[2] = '_';
7869 name_buf[3] = str[2];
7870 name_buf[4] = str[3];
7871 name_buf[5] = '>';
7872 name_buf[6] = NUL;
7873
7874 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7875 fuzzy, fuzzystr, fuzmatch))
7876 {
7877 if (loop == 0)
7878 num_term++;
7879 else
7880 count++;
7881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007884
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007885 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7887 {
7888 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7889 {
Keith Thompson184f71c2024-01-04 21:19:04 +01007890 if (!SAFE_isprint(str[0]) || !SAFE_isprint(str[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 continue;
7892
7893 name_buf[0] = 't';
7894 name_buf[1] = '_';
7895 name_buf[2] = str[0];
7896 name_buf[3] = str[1];
7897 name_buf[4] = NUL;
7898
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007899 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007900 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007901 {
7902 if (loop == 0)
7903 num_term++;
7904 else
7905 count++;
7906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 else
7908 {
7909 name_buf[0] = '<';
7910 name_buf[1] = 't';
7911 name_buf[2] = '_';
7912 name_buf[3] = str[0];
7913 name_buf[4] = str[1];
7914 name_buf[5] = '>';
7915 name_buf[6] = NUL;
7916
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007917 if (match_str(name_buf, regmatch, *matches, count,
7918 (loop == 0), fuzzy, fuzzystr,
7919 fuzmatch))
7920 {
7921 if (loop == 0)
7922 num_term++;
7923 else
7924 count++;
7925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927 }
7928
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007929 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007930 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7932 {
7933 name_buf[0] = '<';
7934 STRCPY(name_buf + 1, str);
7935 STRCAT(name_buf, ">");
7936
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007937 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7938 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {
7940 if (loop == 0)
7941 num_term++;
7942 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007943 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 }
7945 }
7946 }
7947 if (loop == 0)
7948 {
7949 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007950 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007952 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 else
7954 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007955 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007957 *matches = ALLOC_MULT(char_u *, *numMatches);
7958 if (*matches == NULL)
7959 {
7960 *matches = (char_u **)"";
7961 return FAIL;
7962 }
7963 }
7964 else
7965 {
7966 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7967 if (fuzmatch == NULL)
7968 {
7969 *matches = (char_u **)"";
7970 return FAIL;
7971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973 }
7974 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007975
7976 if (fuzzy &&
7977 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7978 return FAIL;
7979
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 return OK;
7981}
7982
Yee Cheng Chin900894b2023-09-29 20:42:32 +02007983// Escape an option value that can be used on the command-line with :set.
7984// Caller needs to free the returned string, unless NULL is returned.
7985 static char_u*
7986escape_option_str_cmdline(char_u *var)
7987{
7988 char_u *buf;
7989
7990 // A backslash is required before some characters. This is the reverse of
7991 // what happens in do_set().
7992 buf = vim_strsave_escaped(var, escape_chars);
7993 if (buf == NULL)
7994 return NULL;
7995
7996#ifdef BACKSLASH_IN_FILENAME
7997 // For MS-Windows et al. we don't double backslashes at the start and
7998 // before a file name character.
7999 // The reverse is found at stropt_copy_value().
8000 for (var = buf; *var != NUL; MB_PTR_ADV(var))
8001 if (var[0] == '\\' && var[1] == '\\'
8002 && expand_option_idx >= 0
8003 && (options[expand_option_idx].flags & P_EXPAND)
8004 && vim_isfilec(var[2])
8005 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
8006 STRMOVE(var, var + 1);
8007#endif
8008 return buf;
8009}
8010
8011/*
8012 * Expansion handler for :set= when we just want to fill in with the existing value.
8013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00008015ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008017 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 char_u *buf;
8019
zeertzjqb0d45ec2023-01-25 15:04:22 +00008020 *numMatches = 0;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008021 *matches = ALLOC_MULT(char_u *, 1);
zeertzjqb0d45ec2023-01-25 15:04:22 +00008022 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 return FAIL;
8024
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00008025 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 if (expand_option_idx < 0)
8027 {
8028 var = find_termcode(expand_option_name + 2);
8029 if (var == NULL)
8030 expand_option_idx = findoption(expand_option_name);
8031 }
8032
8033 if (expand_option_idx >= 0)
8034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008035 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 option_value2string(&options[expand_option_idx], expand_option_flags);
8037 var = NameBuff;
8038 }
8039 else if (var == NULL)
8040 var = (char_u *)"";
8041
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008042 buf = escape_option_str_cmdline(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (buf == NULL)
8044 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00008045 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 return FAIL;
8047 }
8048
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008049 (*matches)[0] = buf;
zeertzjqb0d45ec2023-01-25 15:04:22 +00008050 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 return OK;
8052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
8054/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008055 * Expansion handler for :set=/:set+= when the option has a custom expansion handler.
8056 */
8057 int
8058ExpandStringSetting(
8059 expand_T *xp,
8060 regmatch_T *regmatch,
8061 int *numMatches,
8062 char_u ***matches)
8063{
8064 char_u *var = NULL; // init for GCC
8065 char_u *buf;
8066
8067 if (expand_option_idx < 0 ||
8068 options[expand_option_idx].opt_expand_cb == NULL)
8069 {
8070 // Not supposed to reach this. This function is only for options with
8071 // custom expansion callbacks.
8072 return FAIL;
8073 }
8074
8075 optexpand_T args;
8076 args.oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags);
8077 args.oe_append = expand_option_append;
8078 args.oe_regmatch = regmatch;
8079 args.oe_xp = xp;
8080 args.oe_set_arg = xp->xp_line + expand_option_start_col;
8081 args.oe_include_orig_val =
8082 !expand_option_append &&
8083 (*args.oe_set_arg == NUL);
8084
8085 // Retrieve the existing value, but escape it as a reverse of setting it.
8086 // We technically only need to do this when oe_append or
8087 // oe_include_orig_val is true.
8088 option_value2string(&options[expand_option_idx], expand_option_flags);
8089 var = NameBuff;
8090 buf = escape_option_str_cmdline(var);
8091 if (buf == NULL)
8092 return FAIL;
8093
8094 args.oe_opt_value = buf;
8095
8096 int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches);
8097
8098 vim_free(buf);
8099 return num_ret;
8100}
8101
8102/*
8103 * Expansion handler for :set-=
8104 */
8105 int
8106ExpandSettingSubtract(
8107 expand_T *xp,
8108 regmatch_T *regmatch,
8109 int *numMatches,
8110 char_u ***matches)
8111{
8112 if (expand_option_idx < 0)
8113 // term option
8114 return ExpandOldSetting(numMatches, matches);
8115
8116 char_u *option_val = *(char_u**)get_option_varp_scope(
8117 expand_option_idx, expand_option_flags);
8118
8119 long_u option_flags = options[expand_option_idx].flags;
8120
8121 if (option_flags & P_NUM)
8122 return ExpandOldSetting(numMatches, matches);
8123 else if (option_flags & P_COMMA)
8124 {
8125 // Split the option by comma, then present each option to the user if
8126 // it matches the pattern.
8127 // This condition needs to go first, because 'whichwrap' has both
8128 // P_COMMA and P_FLAGLIST.
8129 garray_T ga;
8130
8131 char_u *item;
8132 char_u *option_copy;
8133 char_u *next_val;
8134 char_u *comma;
8135
8136 if (*option_val == NUL)
8137 return FAIL;
8138
8139 // Make a copy as we need to inject null characters destructively.
8140 option_copy = vim_strsave(option_val);
8141 if (option_copy == NULL)
8142 return FAIL;
8143 next_val = option_copy;
8144
8145 ga_init2(&ga, sizeof(char_u *), 10);
8146
8147 do
8148 {
8149 item = next_val;
8150 comma = vim_strchr(next_val, ',');
8151 while (comma != NULL && comma != next_val && *(comma - 1) == '\\')
8152 {
8153 // "\," is interpreted as a literal comma rather than option
8154 // separator when reading options in copy_option_part(). Skip
8155 // it.
8156 comma = vim_strchr(comma + 1, ',');
8157 }
8158 if (comma != NULL)
8159 {
8160 *comma = NUL; // null-terminate this value, required by later functions
8161 next_val = comma + 1;
8162 }
8163 else
8164 next_val = NULL;
8165
8166 if (*item == NUL)
8167 // empty value, don't add to list
8168 continue;
8169
8170 if (!vim_regexec(regmatch, item, (colnr_T)0))
8171 continue;
8172
8173 char_u *buf = escape_option_str_cmdline(item);
8174 if (buf == NULL)
8175 {
8176 vim_free(option_copy);
8177 ga_clear_strings(&ga);
8178 return FAIL;
8179 }
8180 if (ga_add_string(&ga, buf) != OK)
8181 {
8182 vim_free(buf);
8183 break;
8184 }
8185 } while (next_val != NULL);
8186
8187 vim_free(option_copy);
8188
8189 *matches = ga.ga_data;
8190 *numMatches = ga.ga_len;
8191 return OK;
8192 }
8193 else if (option_flags & P_FLAGLIST)
8194 {
8195 // Only present the flags that are set on the option as the other flags
8196 // are not meaningful to do set-= on.
8197
8198 if (*xp->xp_pattern != NUL)
8199 {
8200 // Don't suggest anything if cmdline is non-empty. Vim's set-=
8201 // behavior requires consecutive strings and it's usually
Viktor Szépedbf749b2023-10-16 09:53:37 +02008202 // unintuitive to users if they try to subtract multiple flags at
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008203 // once.
8204 return FAIL;
8205 }
8206
Yee Cheng Chin6d113472023-10-02 21:38:39 +02008207 size_t num_flags = STRLEN(option_val);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008208 if (num_flags == 0)
8209 return FAIL;
8210
8211 *matches = ALLOC_MULT(char_u *, num_flags + 1);
8212 if (*matches == NULL)
8213 return FAIL;
8214
8215 int count = 0;
8216 char_u *p;
8217
John Marriott95dacbb2024-09-10 21:25:14 +02008218 p = vim_strnsave(option_val, num_flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008219 if (p == NULL)
8220 {
8221 VIM_CLEAR(*matches);
8222 return FAIL;
8223 }
8224 (*matches)[count++] = p;
8225
8226 if (num_flags > 1)
8227 {
8228 // If more than one flags, split the flags up and expose each
8229 // character as individual choice.
8230 for (char_u *flag = option_val; *flag != NUL; flag++)
8231 {
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +02008232 p = vim_strnsave(flag, 1);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008233 if (p == NULL)
8234 break;
8235 (*matches)[count++] = p;
8236 }
8237 }
8238
8239 *numMatches = count;
8240 return OK;
8241 }
8242
8243 return ExpandOldSetting(numMatches, matches);
8244}
8245
8246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 * Get the value for the numeric or string option *opp in a nice format into
8248 * NameBuff[]. Must not be called with a hidden option!
8249 */
8250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008251option_value2string(
8252 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008253 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254{
8255 char_u *varp;
8256
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00008257 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258
8259 if (opp->flags & P_NUM)
8260 {
8261 long wc = 0;
8262
8263 if (wc_use_keyname(varp, &wc))
8264 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8265 else if (wc != 0)
8266 STRCPY(NameBuff, transchar((int)wc));
8267 else
8268 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8269 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008270 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
8272 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008273 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 NameBuff[0] = NUL;
8275#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008276 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008277 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 STRCPY(NameBuff, "*****");
8279#endif
8280 else if (opp->flags & P_EXPAND)
8281 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008282 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 else if ((char_u **)opp->var == &p_pt)
8284 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8285 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008286 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 }
8288}
8289
8290/*
8291 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8292 * printed as a keyname.
8293 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8294 */
8295 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008296wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8299 {
8300 *wcp = *(long *)varp;
8301 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8302 return TRUE;
8303 }
8304 return FALSE;
8305}
8306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 * Return TRUE if "x" is present in 'shortmess' option, or
8309 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8310 */
8311 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008312shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008314 return p_shm != NULL &&
8315 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 || (vim_strchr(p_shm, 'a') != NULL
8317 && vim_strchr((char_u *)SHM_A, x) != NULL));
8318}
8319
8320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8322 *
8323 * Reset 'compatible' and set the values for options that didn't get set yet
8324 * to the Vim defaults.
8325 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008326 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 */
8328 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008329vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008331 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008332 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008333 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334
8335 if (!option_was_set((char_u *)"cp"))
8336 {
8337 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02008338 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8340 set_option_default(opt_idx, OPT_FREE, FALSE);
8341 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008342 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008344
8345 if (fname != NULL)
8346 {
8347 p = vim_getenv(envname, &dofree);
8348 if (p == NULL)
8349 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008350 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008351 p = FullName_save(fname, FALSE);
8352 if (p != NULL)
8353 {
8354 vim_setenv(envname, p);
Christian Brabandt4e7249a2024-09-05 17:46:19 +02008355 if (vim_getenv((char_u *)"MYVIMDIR", &dofree) == NULL)
8356 {
8357 size_t usedlen = 0;
8358 int len = 0;
8359 char_u *fbuf = NULL;
8360
8361 if (STRNCMP(gettail(fname), ".vimrc", 6) == 0)
8362 {
8363 len = STRLEN(p) - 2;
8364 p[len] = '/';
8365 }
8366 else if (STRNCMP(gettail(fname), ".gvimrc", 7) == 0)
8367 {
8368 len = STRLEN(p);
8369 char_u *buf = alloc(len);
8370 if (buf != NULL)
8371 {
8372 mch_memmove(buf, fname, len - 7);
8373 mch_memmove(buf + len - 7, (char_u *)".vim/", 5);
8374 len -= 2; // decrement len, so that we can set len+1 = NUL below
8375 vim_free(p);
8376 p = buf;
8377 }
8378 }
8379#ifdef MSWIN
8380 else if (STRNCMP(gettail(fname), "_vimrc", 6) == 0)
8381 {
8382 len = STRLEN(p) + 4; // remove _vimrc, add vimfiles/
8383 char_u *buf = alloc(len);
8384 if (buf != NULL)
8385 {
8386 mch_memmove(buf, fname, len - 10);
8387 mch_memmove(buf + len - 10, (char_u *)"vimfiles\\", 9);
8388 len -= 2; // decrement len, so that we can set len+1 = NUL below
8389 vim_free(p);
8390 p = buf;
8391 }
8392 }
8393#endif
8394 else
8395 (void)modify_fname((char_u *)":h", FALSE, &usedlen, &p, &fbuf, &len);
8396
8397 if (p != NULL)
8398 {
8399 // keep the directory separator
8400 p[len + 1] = NUL;
8401 vim_setenv((char_u *)"MYVIMDIR", p);
8402 }
8403 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008404 vim_free(p);
8405 }
8406 }
8407 else if (dofree)
8408 vim_free(p);
8409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410}
8411
8412/*
8413 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
8414 */
8415 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008416change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008418 int opt_idx;
8419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 if (p_cp != on)
8421 {
8422 p_cp = on;
8423 compatible_set();
8424 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008425 opt_idx = findoption((char_u *)"cp");
8426 if (opt_idx >= 0)
8427 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428}
8429
8430/*
8431 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008432 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 */
8434 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008435option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437 int idx;
8438
8439 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008440 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 return FALSE;
8442 if (options[idx].flags & P_WAS_SET)
8443 return TRUE;
8444 return FALSE;
8445}
8446
8447/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008448 * Reset the flag indicating option "name" was set.
8449 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008450 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008451reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008452{
8453 int idx = findoption(name);
8454
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008455 if (idx < 0)
8456 return FAIL;
8457
8458 options[idx].flags &= ~P_WAS_SET;
8459 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008460}
8461
8462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 * compatible_set() - Called when 'compatible' has been set or unset.
8464 *
8465 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8466 * flag) to a Vi compatible value.
8467 * When 'compatible' is unset: Set all options that have a different default
8468 * for Vim (without the P_VI_DEF flag) to that default.
8469 */
8470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008471compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472{
8473 int opt_idx;
8474
Bram Moolenaardac13472019-09-16 21:06:21 +02008475 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8477 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8478 set_option_default(opt_idx, OPT_FREE, p_cp);
8479 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008480 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481}
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 * Check if backspacing over something is allowed.
8485 */
8486 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008487can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008488 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008490#ifdef FEAT_JOB_CHANNEL
8491 if (what == BS_START && bt_prompt(curbuf))
8492 return FALSE;
8493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 switch (*p_bs)
8495 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008496 case '3': return TRUE;
8497 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 case '1': return (what != BS_START);
8499 case '0': return FALSE;
8500 }
8501 return vim_strchr(p_bs, what) != NULL;
8502}
8503
8504/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008505 * Return the effective 'scrolloff' value for the current window, using the
8506 * global value when appropriate.
8507 */
8508 long
8509get_scrolloff_value(void)
8510{
8511 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8512}
8513
8514/*
8515 * Return the effective 'sidescrolloff' value for the current window, using the
8516 * global value when appropriate.
8517 */
8518 long
8519get_sidescrolloff_value(void)
8520{
8521 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8522}
8523
8524/*
zeertzjqaa925ee2024-06-09 18:24:05 +02008525 * Get the local or global value of 'backupcopy' flags.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008526 */
8527 unsigned int
zeertzjqaa925ee2024-06-09 18:24:05 +02008528get_bkc_flags(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008529{
8530 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8531}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008532
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008533#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008534/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008535 * Get the local or global value of 'formatlistpat'.
8536 */
8537 char_u *
8538get_flp_value(buf_T *buf)
8539{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008540 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8541 return p_flp;
8542 return buf->b_p_flp;
8543}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008544#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008545
8546/*
zeertzjqaa925ee2024-06-09 18:24:05 +02008547 * Get the local or global value of 'virtualedit' flags.
Gary Johnson53ba05b2021-07-26 22:19:10 +02008548 */
8549 unsigned int
8550get_ve_flags(void)
8551{
Gary Johnson51ad8502021-08-03 18:33:08 +02008552 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8553 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008554}
8555
Bram Moolenaaree857022019-11-09 23:26:40 +01008556#if defined(FEAT_LINEBREAK) || defined(PROTO)
8557/*
8558 * Get the local or global value of 'showbreak'.
8559 */
8560 char_u *
8561get_showbreak_value(win_T *win)
8562{
8563 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8564 return p_sbr;
8565 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8566 return empty_option;
8567 return win->w_p_sbr;
8568}
8569#endif
8570
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008571#if defined(FEAT_EVAL) || defined(PROTO)
8572/*
8573 * Get window or buffer local options.
8574 */
8575 dict_T *
8576get_winbuf_options(int bufopt)
8577{
8578 dict_T *d;
8579 int opt_idx;
8580
8581 d = dict_alloc();
8582 if (d == NULL)
8583 return NULL;
8584
Bram Moolenaardac13472019-09-16 21:06:21 +02008585 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008586 {
8587 struct vimoption *opt = &options[opt_idx];
8588
8589 if ((bufopt && (opt->indir & PV_BUF))
8590 || (!bufopt && (opt->indir & PV_WIN)))
8591 {
8592 char_u *varp = get_varp(opt);
8593
8594 if (varp != NULL)
8595 {
8596 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008597 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008598 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008599 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008600 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008601 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008602 }
8603 }
8604 }
8605
8606 return d;
8607}
8608#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008609
Bram Moolenaardac13472019-09-16 21:06:21 +02008610#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008611/*
8612 * This is called when 'culopt' is changed
8613 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008614 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008615fill_culopt_flags(char_u *val, win_T *wp)
8616{
8617 char_u *p;
8618 char_u culopt_flags_new = 0;
8619
8620 if (val == NULL)
8621 p = wp->w_p_culopt;
8622 else
8623 p = val;
8624 while (*p != NUL)
8625 {
Yee Cheng Chin900894b2023-09-29 20:42:32 +02008626 // Note: Keep this in sync with p_culopt_values.
Bram Moolenaar017ba072019-09-14 21:01:23 +02008627 if (STRNCMP(p, "line", 4) == 0)
8628 {
8629 p += 4;
8630 culopt_flags_new |= CULOPT_LINE;
8631 }
8632 else if (STRNCMP(p, "both", 4) == 0)
8633 {
8634 p += 4;
8635 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8636 }
8637 else if (STRNCMP(p, "number", 6) == 0)
8638 {
8639 p += 6;
8640 culopt_flags_new |= CULOPT_NBR;
8641 }
8642 else if (STRNCMP(p, "screenline", 10) == 0)
8643 {
8644 p += 10;
8645 culopt_flags_new |= CULOPT_SCRLINE;
8646 }
8647
8648 if (*p != ',' && *p != NUL)
8649 return FAIL;
8650 if (*p == ',')
8651 ++p;
8652 }
8653
8654 // Can't have both "line" and "screenline".
8655 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8656 return FAIL;
8657 wp->w_p_culopt_flags = culopt_flags_new;
8658
8659 return OK;
8660}
8661#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008662
8663/*
8664 * Get the value of 'magic' adjusted for Vim9 script.
8665 */
8666 int
8667magic_isset(void)
8668{
8669 switch (magic_overruled)
8670 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008671 case OPTION_MAGIC_ON: return TRUE;
8672 case OPTION_MAGIC_OFF: return FALSE;
8673 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008674 }
8675#ifdef FEAT_EVAL
8676 if (in_vim9script())
8677 return TRUE;
8678#endif
8679 return p_magic;
8680}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008681
8682/*
8683 * Set the callback function value for an option that accepts a function name,
8684 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8685 * Returns OK if the option is successfully set to a function, otherwise
8686 * returns FAIL.
8687 */
8688 int
8689option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8690{
8691#ifdef FEAT_EVAL
8692 typval_T *tv;
8693 callback_T cb;
8694
8695 if (optval == NULL || *optval == NUL)
8696 {
8697 free_callback(optcb);
8698 return OK;
8699 }
8700
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008701 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008702 || (STRNCMP(optval, "function(", 9) == 0)
8703 || (STRNCMP(optval, "funcref(", 8) == 0))
8704 // Lambda expression or a funcref
8705 tv = eval_expr(optval, NULL);
8706 else
8707 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008708 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008709 if (tv == NULL)
8710 return FAIL;
8711
8712 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008713 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008714 {
8715 free_tv(tv);
8716 return FAIL;
8717 }
8718
8719 free_callback(optcb);
8720 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008721 if (cb.cb_free_name)
8722 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008723 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008724
8725 // when using Vim9 style "import.funcname" it needs to be expanded to
8726 // "import#funcname".
8727 expand_autload_callback(optcb);
8728
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008729 return OK;
8730#else
8731 return FAIL;
8732#endif
8733}
Christian Brabandt757593c2023-08-22 21:44:10 +02008734
8735#if defined(FEAT_EVAL) || defined(PROTO)
8736 static void
8737didset_options_sctx(int opt_flags, char **buf)
8738{
8739 for (int i = 0; ; ++i)
8740 {
8741 if (buf[i] == NULL)
8742 break;
8743
8744 int idx = findoption((char_u *)buf[i]);
8745 if (idx >= 0)
8746 set_option_sctx_idx(idx, opt_flags, current_sctx);
8747 }
8748}
8749#endif