blob: 2332f2a38822c4db85d7c4601c8be9ce48654c21 [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);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, 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 paste_option_changed(void);
70static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000073 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000075 static void
76set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000080 // Find default value for 'shell' option.
81 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000082 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010083#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000084 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010085 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +000087 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020088#if defined(MSWIN)
89 {
90 // For MS-Windows put the path in quotes instead of escaping spaces.
91 char_u *cmd;
92 size_t len;
93
94 if (vim_strchr(p, ' ') != NULL)
95 {
96 len = STRLEN(p) + 3; // two quotes and a trailing NUL
97 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +020098 if (cmd != NULL)
99 {
100 vim_snprintf((char *)cmd, len, "\"%s\"", p);
101 set_string_default("sh", cmd);
102 vim_free(cmd);
103 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200104 }
105 else
106 set_string_default("sh", p);
107 }
108#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100109 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200110#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000111}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000113/*
114 * Set the default for 'backupskip' to include environment variables for
115 * temp files.
116 */
117 static void
118set_init_default_backupskip(void)
119{
120 int opt_idx;
121 long_u n;
122 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100123#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000124 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100125#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000126 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100127#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000128 int len;
129 garray_T ga;
130 int mustfree;
131 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200132
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000133 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000135 ga_init2(&ga, 1, 100);
136 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
137 {
138 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100143# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000144 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100147#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 p = vim_getenv((char_u *)names[n], &mustfree);
149 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000151 // First time count the NUL, otherwise count the ','.
152 len = (int)STRLEN(p) + 3;
153 item = alloc(len);
154 STRCPY(item, p);
155 add_pathsep(item);
156 STRCAT(item, "*");
157 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
158 == NULL
159 && ga_grow(&ga, len) == OK)
160 {
161 if (ga.ga_len > 0)
162 STRCAT(ga.ga_data, ",");
163 STRCAT(ga.ga_data, item);
164 ga.ga_len += len;
165 }
166 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 if (mustfree)
169 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000171 if (ga.ga_data != NULL)
172 {
173 set_string_default("bsk", ga.ga_data);
174 vim_free(ga.ga_data);
175 }
176}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000178/*
179 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
180 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
181 */
182 static void
183set_init_default_maxmemtot(void)
184{
185 int opt_idx;
186 long_u n;
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000189 if (opt_idx < 0)
190 return;
191
192#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
193 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {
ichizok02560422022-04-05 14:18:44 +0100196#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000197 // Use amount of memory available at this moment.
198 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100199#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000200 // Use amount of memory available to Vim.
201 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100202#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000203 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
206 opt_idx = findoption((char_u *)"maxmem");
207 if (opt_idx >= 0)
208 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000210 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
211 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000212#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000216}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000218/*
219 * Initialize the 'cdpath' option to a default value.
220 */
221 static void
222set_init_default_cdpath(void)
223{
224 int opt_idx;
225 char_u *cdpath;
226 char_u *buf;
227 int i;
228 int j;
229 int mustfree = FALSE;
230
231 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
232 if (cdpath == NULL)
233 return;
234
235 buf = alloc((STRLEN(cdpath) << 1) + 2);
236 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000238 buf[0] = ','; // start with ",", current dir first
239 j = 1;
240 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000242 if (vim_ispathlistsep(cdpath[i]))
243 buf[j++] = ',';
244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000246 if (cdpath[i] == ' ' || cdpath[i] == ',')
247 buf[j++] = '\\';
248 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 }
250 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000251 buf[j] = NUL;
252 opt_idx = findoption((char_u *)"cdpath");
253 if (opt_idx >= 0)
254 {
255 options[opt_idx].def_val[VI_DEFAULT] = buf;
256 options[opt_idx].flags |= P_DEF_ALLOCED;
257 }
258 else
259 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000261 if (mustfree)
262 vim_free(cdpath);
263}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000265/*
266 * Initialize the 'printencoding' option to a default value.
267 */
268 static void
269set_init_default_printencoding(void)
270{
ichizok02560422022-04-05 14:18:44 +0100271#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000272 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100273 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100275# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000276 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100277# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000278 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100279# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000280 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100281# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000282 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000284 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000286}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287
288#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000289/*
290 * Initialize the 'printexpr' option to a default value.
291 */
292 static void
293set_init_default_printexpr(void)
294{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100295 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100297# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000298 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100299# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
301
ichizok02560422022-04-05 14:18:44 +0100302# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# endif
305 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000306}
307#endif
308
309#ifdef UNIX
310/*
311 * Force restricted-mode on for "nologin" or "false" $SHELL
312 */
313 static void
314set_init_restricted_mode(void)
315{
316 char_u *p;
317
318 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000319 if (p == NULL)
320 return;
321 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
322 restricted = TRUE;
323 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000324}
325#endif
326
327#ifdef CLEAN_RUNTIMEPATH
328/*
329 * When Vim is started with the "--clean" argument, set the default value
330 * for the 'runtimepath' and 'packpath' options.
331 */
332 static void
333set_init_clean_rtp(void)
334{
335 int opt_idx;
336
337 opt_idx = findoption((char_u *)"runtimepath");
338 if (opt_idx >= 0)
339 {
340 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
341 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
342 }
343 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000344 if (opt_idx < 0)
345 return;
346 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
347 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000348}
349#endif
350
351/*
352 * Expand environment variables and things like "~" for the defaults.
353 * If option_expand() returns non-NULL the variable is expanded. This can
354 * only happen for non-indirect options.
355 * Also set the default to the expanded value, so ":set" does not list
356 * them.
357 * Don't set the P_ALLOCED flag, because we don't want to free the
358 * default.
359 */
360 static void
361set_init_expand_env(void)
362{
363 int opt_idx;
364 char_u *p;
365
366 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
367 {
368 if ((options[opt_idx].flags & P_GETTEXT)
369 && options[opt_idx].var != NULL)
370 p = (char_u *)_(*(char **)options[opt_idx].var);
371 else
372 p = option_expand(opt_idx, NULL);
373 if (p != NULL && (p = vim_strsave(p)) != NULL)
374 {
375 *(char_u **)options[opt_idx].var = p;
376 // VIMEXP
377 // Defaults for all expanded options are currently the same for Vi
378 // and Vim. When this changes, add some code here! Also need to
379 // split P_DEF_ALLOCED in two.
380 if (options[opt_idx].flags & P_DEF_ALLOCED)
381 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
382 options[opt_idx].def_val[VI_DEFAULT] = p;
383 options[opt_idx].flags |= P_DEF_ALLOCED;
384 }
385 }
386}
387
388/*
389 * Initialize the 'LANG' environment variable to a default value.
390 */
391 static void
392set_init_lang_env(void)
393{
394#if defined(MSWIN) && defined(FEAT_GETTEXT)
395 // If $LANG isn't set, try to get a good value for it. This makes the
396 // right language be used automatically. Don't do this for English.
397 if (mch_getenv((char_u *)"LANG") == NULL)
398 {
399 char buf[20];
400 long_u n;
401
402 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
403 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
404 // only the first two.
405 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
406 (LPTSTR)buf, 20);
407 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
408 {
409 // There are a few exceptions (probably more)
410 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
411 STRCPY(buf, "zh_TW");
412 else if (STRNICMP(buf, "chs", 3) == 0
413 || STRNICMP(buf, "zhc", 3) == 0)
414 STRCPY(buf, "zh_CN");
415 else if (STRNICMP(buf, "jp", 2) == 0)
416 STRCPY(buf, "ja");
417 else
418 buf[2] = NUL; // truncate to two-letter code
419 vim_setenv((char_u *)"LANG", (char_u *)buf);
420 }
421 }
422#elif defined(MACOS_CONVERT)
423 // Moved to os_mac_conv.c to avoid dependency problems.
424 mac_lang_init();
425#endif
426}
427
428/*
429 * Initialize the 'encoding' option to a default value.
430 */
431 static void
432set_init_default_encoding(void)
433{
434 char_u *p;
435 int opt_idx;
436
437# ifdef MSWIN
438 // MS-Windows has builtin support for conversion to and from Unicode, using
439 // "utf-8" for 'encoding' should work best for most users.
440 p = vim_strsave((char_u *)ENC_DFLT);
441# else
442 // enc_locale() will try to find the encoding of the current locale.
443 // This works best for properly configured systems, old and new.
444 p = enc_locale();
445# endif
446 if (p == NULL)
447 return;
448
449 // Try setting 'encoding' and check if the value is valid.
450 // If not, go back to the default encoding.
451 char_u *save_enc = p_enc;
452 p_enc = p;
453 if (STRCMP(p_enc, "gb18030") == 0)
454 {
455 // We don't support "gb18030", but "cp936" is a good substitute
456 // for practical purposes, thus use that. It's not an alias to
457 // still support conversion between gb18030 and utf-8.
458 p_enc = vim_strsave((char_u *)"cp936");
459 vim_free(p);
460 }
461 if (mb_init() == NULL)
462 {
463 opt_idx = findoption((char_u *)"encoding");
464 if (opt_idx >= 0)
465 {
466 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
467 options[opt_idx].flags |= P_DEF_ALLOCED;
468 }
469
470#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
471 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
472 {
473 // Adjust the default for 'isprint' and 'iskeyword' to match
474 // latin1. Also set the defaults for when 'nocompatible' is
475 // set.
476 set_string_option_direct((char_u *)"isp", -1,
477 ISP_LATIN1, OPT_FREE, SID_NONE);
478 set_string_option_direct((char_u *)"isk", -1,
479 ISK_LATIN1, OPT_FREE, SID_NONE);
480 opt_idx = findoption((char_u *)"isp");
481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
483 opt_idx = findoption((char_u *)"isk");
484 if (opt_idx >= 0)
485 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
486 (void)init_chartab();
487 }
488#endif
489
490#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
491 // Win32 console: When GetACP() returns a different value from
492 // GetConsoleCP() set 'termencoding'.
493 if (
494# ifdef VIMDLL
495 (!gui.in_use && !gui.starting) &&
496# endif
497 GetACP() != GetConsoleCP())
498 {
499 char buf[50];
500
501 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
502 // Use an alternative value.
503 if (GetConsoleCP() == 0)
504 sprintf(buf, "cp%ld", (long)GetACP());
505 else
506 sprintf(buf, "cp%ld", (long)GetConsoleCP());
507 p_tenc = vim_strsave((char_u *)buf);
508 if (p_tenc != NULL)
509 {
510 opt_idx = findoption((char_u *)"termencoding");
511 if (opt_idx >= 0)
512 {
513 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
514 options[opt_idx].flags |= P_DEF_ALLOCED;
515 }
516 convert_setup(&input_conv, p_tenc, p_enc);
517 convert_setup(&output_conv, p_enc, p_tenc);
518 }
519 else
520 p_tenc = empty_option;
521 }
522#endif
523#if defined(MSWIN)
524 // $HOME may have characters in active code page.
525 init_homedir();
526#endif
527 }
528 else
529 {
530 vim_free(p_enc);
531 p_enc = save_enc;
532 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000533}
534
535/*
536 * Initialize the options, first part.
537 *
538 * Called only once from main(), just after creating the first buffer.
539 * If "clean_arg" is TRUE Vim was started with --clean.
540 */
541 void
542set_init_1(int clean_arg)
543{
544#ifdef FEAT_LANGMAP
545 langmap_init();
546#endif
547
548 // Be Vi compatible by default
549 p_cp = TRUE;
550
551 // Use POSIX compatibility when $VIM_POSIX is set.
552 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
553 {
554 set_string_default("cpo", (char_u *)CPO_ALL);
555 set_string_default("shm", (char_u *)SHM_POSIX);
556 }
557
558 set_init_default_shell();
559 set_init_default_backupskip();
560 set_init_default_maxmemtot();
561 set_init_default_cdpath();
562 set_init_default_printencoding();
563#ifdef FEAT_POSTSCRIPT
564 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#endif
566
567 /*
568 * Set all the options (except the terminal options) to their default
569 * value. Also set the global value for local options.
570 */
571 set_options_default(0);
572
matveytadbb1bf2022-02-01 17:26:12 +0000573#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000574 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000575#endif
576
Bram Moolenaar07268702018-03-01 21:57:32 +0100577#ifdef CLEAN_RUNTIMEPATH
578 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000579 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#ifdef FEAT_GUI
583 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100584 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
586
587 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100589 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 check_buf_options(curbuf);
591 check_win_options(curwin);
592 check_options();
593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 didset_options();
596
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000597#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // Use the current chartab for the generic chartab. This is not in
599 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000600 init_spell_chartab();
601#endif
602
K.Takatace3189d2023-02-15 19:13:43 +0000603 set_init_default_encoding();
604
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000605 // Expand environment variables and things like "~" for the defaults.
606 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100608 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // Detect use of mlterm.
612 // Mlterm is a terminal emulator akin to xterm that has some special
613 // abilities (bidi namely).
614 // NOTE: mlterm's author is being asked to 'set' a variable
615 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100617 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
619
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200620 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000622 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 set_helplang_default(get_mess_lang());
627#endif
628}
629
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200630static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
631
632/*
633 * Set the "fileencodings" option to the default value for when 'encoding' is
634 * utf-8.
635 */
636 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000637set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200638{
639 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
640 OPT_FREE, 0);
641}
642
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643/*
644 * Set an option to its default value.
645 * This does not take care of side effects!
646 */
647 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100648set_option_default(
649 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100650 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
651 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100653 char_u *varp; // pointer to variable for current option
654 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000656 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
658
659 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
660 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100661 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
663 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
664 if (flags & P_STRING)
665 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200666 // 'fencs' default value depends on 'encoding'
667 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
668 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 // Use set_string_option_direct() for local options to handle
670 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200671 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200672 set_string_option_direct(NULL, opt_idx,
673 options[opt_idx].def_val[dvi], opt_flags, 0);
674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200676 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
677 free_string_option(*(char_u **)(varp));
678 *(char_u **)varp = options[opt_idx].def_val[dvi];
679 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681 }
682 else if (flags & P_NUM)
683 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000684 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 win_comp_scroll(curwin);
686 else
687 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100688 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
689
690 if ((long *)varp == &curwin->w_p_so
691 || (long *)varp == &curwin->w_p_siso)
692 // 'scrolloff' and 'sidescrolloff' local values have a
693 // different default value than the global default.
694 *(long *)varp = -1;
695 else
696 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100697 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (both)
699 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100700 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
702 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100703 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100705 // the cast to long is required for Manx C, long_i is needed for
706 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000707 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000708#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100709 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000710 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
711 *(int *)varp = FALSE;
712#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (both)
715 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
716 *(int *)varp;
717 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000720 flagsp = insecure_flag(opt_idx, opt_flags);
721 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 }
723
724#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200725 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#endif
727}
728
729/*
730 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200731 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100734set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000739 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaardac13472019-09-16 21:06:21 +0200741 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200742 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200743 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100744 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200745# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200746 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200747 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200748# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100749 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 set_option_default(i, opt_flags, p_cp);
751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100752 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000753 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200755 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756}
757
758/*
759 * Set the Vi-default value of a string option.
760 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100761 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100763 static void
764set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 char_u *p;
767 int opt_idx;
768
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100769 if (escape && vim_strchr(val, ' ') != NULL)
770 p = vim_strsave_escaped(val, (char_u *)" ");
771 else
772 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000773 if (p == NULL) // we don't want a NULL
774 return;
775
776 opt_idx = findoption((char_u *)name);
777 if (opt_idx < 0)
778 return;
779
780 if (options[opt_idx].flags & P_DEF_ALLOCED)
781 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
782 options[opt_idx].def_val[VI_DEFAULT] = p;
783 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100786 void
787set_string_default(char *name, char_u *val)
788{
789 set_string_default_esc(name, val, FALSE);
790}
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200793 * For an option value that contains comma separated items, find "newval" in
794 * "origval". Return NULL if not found.
795 */
796 static char_u *
797find_dup_item(char_u *origval, char_u *newval, long_u flags)
798{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200799 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200800 size_t newlen;
801 char_u *s;
802
803 if (origval == NULL)
804 return NULL;
805
806 newlen = STRLEN(newval);
807 for (s = origval; *s != NUL; ++s)
808 {
809 if ((!(flags & P_COMMA)
810 || s == origval
811 || (s[-1] == ',' && !(bs & 1)))
812 && STRNCMP(s, newval, newlen) == 0
813 && (!(flags & P_COMMA)
814 || s[newlen] == ','
815 || s[newlen] == NUL))
816 return s;
817 // Count backslashes. Only a comma with an even number of backslashes
818 // or a single backslash preceded by a comma before it is recognized as
819 // a separator.
820 if ((s > origval + 1
821 && s[-1] == '\\'
822 && s[-2] != ',')
823 || (s == origval + 1
824 && s[-1] == '\\'))
825 ++bs;
826 else
827 bs = 0;
828 }
829 return NULL;
830}
831
832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Set the Vi-default value of a number option.
834 * Used for 'lines' and 'columns'.
835 */
836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000839 int opt_idx;
840
841 opt_idx = findoption((char_u *)name);
842 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000843 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844}
845
Dominique Pelle748b3082022-01-08 12:41:16 +0000846#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200847/*
848 * Set all window-local and buffer-local options to the Vim default.
849 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200850 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200851 */
852 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200853set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200854{
855 win_T *save_curwin = curwin;
856 int i;
857
858 curwin = wp;
859 curbuf = curwin->w_buffer;
860 block_autocmds();
861
Bram Moolenaardac13472019-09-16 21:06:21 +0200862 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200863 {
864 struct vimoption *p = &(options[i]);
865 char_u *varp = get_varp_scope(p, OPT_LOCAL);
866
867 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200868 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200869 && !(options[i].flags & P_NODEFAULT)
870 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200871 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200872 }
873
874 unblock_autocmds();
875 curwin = save_curwin;
876 curbuf = curwin->w_buffer;
877}
Dominique Pelle748b3082022-01-08 12:41:16 +0000878#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200879
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000880#if defined(EXITFREE) || defined(PROTO)
881/*
882 * Free all options.
883 */
884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100885free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000886{
887 int i;
888
Bram Moolenaardac13472019-09-16 21:06:21 +0200889 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000890 {
891 if (options[i].indir == PV_NONE)
892 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100894 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000895 free_string_option(*(char_u **)options[i].var);
896 if (options[i].flags & P_DEF_ALLOCED)
897 free_string_option(options[i].def_val[VI_DEFAULT]);
898 }
899 else if (options[i].var != VAR_WIN
900 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200902 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000903 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000904 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000905 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000906}
907#endif
908
909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910/*
911 * Initialize the options, part two: After getting Rows and Columns and
912 * setting 'term'.
913 */
914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100915set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000917 int idx;
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100920 * 'scroll' defaults to half the window height. The stored default is zero,
921 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000923 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000924 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000925 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 comp_col();
927
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000928 /*
929 * 'window' is only for backwards compatibility with Vi.
930 * Default is Rows - 1.
931 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000932 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000933 p_window = Rows - 1;
934 set_number_default("window", Rows - 1);
935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100936 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100937#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000938 /*
939 * If 'background' wasn't set by the user, try guessing the value,
940 * depending on the terminal name. Only need to check for terminals
941 * with a dark background, that can handle color.
942 */
943 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000944 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
945 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000947 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // don't mark it as set, when starting the GUI it may be
949 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000950 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000953
954#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100955 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000956#endif
957#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000959#endif
960#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Initialize the options, part three: After reading the .vimrc
967 */
968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100969set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100971#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972/*
973 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
974 * This is done after other initializations, where 'shell' might have been
975 * set, but only if they have not been set before.
976 */
977 char_u *p;
978 int idx_srr;
979 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100980# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 int idx_sp;
982 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000986 if (idx_srr < 0)
987 do_srr = FALSE;
988 else
989 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000992 if (idx_sp < 0)
993 do_sp = FALSE;
994 else
995 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100996# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200997 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (p != NULL)
999 {
1000 /*
1001 * Default for p_sp is "| tee", for p_srr is ">".
1002 * For known shells it is changed here to include stderr.
1003 */
1004 if ( fnamecmp(p, "csh") == 0
1005 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001006# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 || fnamecmp(p, "csh.exe") == 0
1008 || fnamecmp(p, "tcsh.exe") == 0
1009# endif
1010 )
1011 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001012# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (do_sp)
1014 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001015# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001017# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1021 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (do_srr)
1024 {
1025 p_srr = (char_u *)">&";
1026 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1027 }
1028 }
Mike Williams12795022021-06-28 20:53:58 +02001029# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001030 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1031 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001032 else if ( fnamecmp(p, "powershell") == 0
1033 || fnamecmp(p, "powershell.exe") == 0
1034 )
1035 {
1036# if defined(FEAT_QUICKFIX)
1037 if (do_sp)
1038 {
1039 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1040 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1041 }
1042# endif
1043 if (do_srr)
1044 {
1045 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1046 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1047 }
1048 }
1049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 else
Natanael Copa56318362021-05-06 18:46:35 +02001051 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if ( fnamecmp(p, "sh") == 0
1053 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001054 || fnamecmp(p, "mksh") == 0
1055 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001057 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001059 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001060 || fnamecmp(p, "ash") == 0
1061 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001062 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001063# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 || fnamecmp(p, "cmd") == 0
1065 || fnamecmp(p, "sh.exe") == 0
1066 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001067 || fnamecmp(p, "mksh.exe") == 0
1068 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001070 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 || fnamecmp(p, "bash.exe") == 0
1072 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001073 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001074 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001076 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001078# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (do_sp)
1080 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001081# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001083# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001084 if (fnamecmp(p, "pwsh") == 0)
1085 p_sp = (char_u *)">%s 2>&1";
1086 else
1087 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1090 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (do_srr)
1093 {
1094 p_srr = (char_u *)">%s 2>&1";
1095 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1096 }
1097 }
1098 vim_free(p);
1099 }
1100#endif
1101
Bram Moolenaar4f974752019-02-17 17:44:42 +01001102#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001104 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1105 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001107 * set, but only if they have not been set before.
1108 * Default values depend on shell (cmd.exe is default shell):
1109 *
1110 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001111 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001112 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001113 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001114 * "sh" like shells - "-c" "\""
1115 *
1116 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 */
Mike Williams12795022021-06-28 20:53:58 +02001118 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1119 {
1120 int idx_opt;
1121
1122 idx_opt = findoption((char_u *)"shcf");
1123 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1124 {
1125 p_shcf = (char_u*)"-Command";
1126 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1127 }
1128
1129 idx_opt = findoption((char_u *)"sxq");
1130 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1131 {
1132 p_sxq = (char_u*)"\"";
1133 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1134 }
1135 }
1136 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
1138 int idx3;
1139
1140 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001141 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
1143 p_shcf = (char_u *)"-c";
1144 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1145 }
1146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001147 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001149 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 p_sxq = (char_u *)"\"";
1152 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001155 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1156 {
1157 int idx3;
1158
1159 /*
1160 * cmd.exe on Windows will strip the first and last double quote given
1161 * on the command line, e.g. most of the time things like:
1162 * cmd /c "my path/to/echo" "my args to echo"
1163 * become:
1164 * my path/to/echo" "my args to echo
1165 * when executed.
1166 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001167 * To avoid this, set shellxquote to surround the command in
1168 * parenthesis. This appears to make most commands work, without
1169 * breaking commands that worked previously, such as
1170 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001171 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001172 idx3 = findoption((char_u *)"sxq");
1173 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1174 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001175 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001176 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1177 }
1178
Bram Moolenaara64ba222012-02-12 23:23:31 +01001179 idx3 = findoption((char_u *)"shcf");
1180 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1181 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001182 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001183 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1184 }
1185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#endif
1187
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001188 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001189 {
1190 int idx_ffs = findoption((char_u *)"ffs");
1191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001192 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001193 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1194 set_fileformat(default_fileformat(), OPT_LOCAL);
1195 }
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198}
1199
1200#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1201/*
1202 * When 'helplang' is still at its default value, set it to "lang".
1203 * Only the first two characters of "lang" are used.
1204 */
1205 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001206set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207{
1208 int idx;
1209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 return;
1212 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001213 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1214 return;
1215
1216 if (options[idx].flags & P_ALLOCED)
1217 free_string_option(p_hlg);
1218 p_hlg = vim_strsave(lang);
1219 if (p_hlg == NULL)
1220 p_hlg = empty_option;
1221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001223 // zh_CN becomes "cn", zh_TW becomes "tw"
1224 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001225 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001226 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1227 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001228 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001229 // any C like setting, such as C.UTF-8, becomes "en"
1230 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1231 {
1232 p_hlg[0] = 'e';
1233 p_hlg[1] = 'n';
1234 }
1235 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001237 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238}
1239#endif
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241/*
1242 * 'title' and 'icon' only default to true if they have not been set or reset
1243 * in .vimrc and we can read the old value.
1244 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1245 * they can be reset. This reduces startup time when using X on a remote
1246 * machine.
1247 */
1248 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001249set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250{
1251 int idx1;
1252 long val;
1253
1254 /*
1255 * If GUI is (going to be) used, we can always set the window title and
1256 * icon name. Saves a bit of time, because the X11 display server does
1257 * not need to be contacted.
1258 */
1259 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001260 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262#ifdef FEAT_GUI
1263 if (gui.starting || gui.in_use)
1264 val = TRUE;
1265 else
1266#endif
1267 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001268 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 p_title = val;
1270 }
1271 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001272 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001274 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001276
1277#ifdef FEAT_GUI
1278 if (gui.starting || gui.in_use)
1279 val = TRUE;
1280 else
1281#endif
1282 val = mch_can_restore_icon();
1283 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1284 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001287 void
1288ex_set(exarg_T *eap)
1289{
1290 int flags = 0;
1291
1292 if (eap->cmdidx == CMD_setlocal)
1293 flags = OPT_LOCAL;
1294 else if (eap->cmdidx == CMD_setglobal)
1295 flags = OPT_GLOBAL;
1296#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001297 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001298 ex_options(eap);
1299 else
1300#endif
1301 {
1302 if (eap->forceit)
1303 flags |= OPT_ONECOLUMN;
1304 (void)do_set(eap->arg, flags);
1305 }
1306}
1307
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001308/*
1309 * :set operator types
1310 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001311typedef enum {
1312 OP_NONE = 0,
1313 OP_ADDING, // "opt+=arg"
1314 OP_PREPENDING, // "opt^=arg"
1315 OP_REMOVING, // "opt-=arg"
1316} set_op_T;
1317
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001318typedef enum {
1319 PREFIX_NO = 0, // "no" prefix
1320 PREFIX_NONE, // no prefix
1321 PREFIX_INV, // "inv" prefix
1322} set_prefix_T;
1323
1324/*
1325 * Return the prefix type for the option name in *argp.
1326 */
1327 static set_prefix_T
1328get_option_prefix(char_u **argp)
1329{
1330 int prefix = PREFIX_NONE;
1331 char_u *arg = *argp;
1332
1333 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1334 {
1335 prefix = PREFIX_NO;
1336 arg += 2;
1337 }
1338 else if (STRNCMP(arg, "inv", 3) == 0)
1339 {
1340 prefix = PREFIX_INV;
1341 arg += 3;
1342 }
1343
1344 *argp = arg;
1345 return prefix;
1346}
1347
1348/*
1349 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1350 * and the option name length in "*lenp". For a <t_xx> option, return the key
1351 * number in "*keyp".
1352 *
1353 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1354 * otherwise returns OK.
1355 */
1356 static int
1357parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1358{
1359 int key = 0;
1360 int len;
1361 int opt_idx;
1362
1363 if (*arg == '<')
1364 {
1365 opt_idx = -1;
1366 // look out for <t_>;>
1367 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1368 len = 5;
1369 else
1370 {
1371 len = 1;
1372 while (arg[len] != NUL && arg[len] != '>')
1373 ++len;
1374 }
1375 if (arg[len] != '>')
1376 return FAIL;
1377
1378 arg[len] = NUL; // put NUL after name
1379 if (arg[1] == 't' && arg[2] == '_') // could be term code
1380 opt_idx = findoption(arg + 1);
1381 arg[len++] = '>'; // restore '>'
1382 if (opt_idx == -1)
1383 key = find_key_option(arg + 1, TRUE);
1384 }
1385 else
1386 {
1387 int nextchar; // next non-white char after option name
1388
1389 len = 0;
1390 /*
1391 * The two characters after "t_" may not be alphanumeric.
1392 */
1393 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1394 len = 4;
1395 else
1396 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1397 ++len;
1398 nextchar = arg[len];
1399 arg[len] = NUL; // put NUL after name
1400 opt_idx = findoption(arg);
1401 arg[len] = nextchar; // restore nextchar
1402 if (opt_idx == -1)
1403 key = find_key_option(arg, FALSE);
1404 }
1405
1406 *keyp = key;
1407 *lenp = len;
1408 *opt_idxp = opt_idx;
1409
1410 return OK;
1411}
1412
1413/*
1414 * Get the option operator (+=, ^=, -=).
1415 */
1416 static set_op_T
1417get_opt_op(char_u *arg)
1418{
1419 set_op_T op = OP_NONE;
1420
1421 if (*arg != NUL && *(arg + 1) == '=')
1422 {
1423 if (*arg == '+')
1424 op = OP_ADDING; // "+="
1425 else if (*arg == '^')
1426 op = OP_PREPENDING; // "^="
1427 else if (*arg == '-')
1428 op = OP_REMOVING; // "-="
1429 }
1430
1431 return op;
1432}
1433
1434/*
1435 * Validate whether the value of the option in "opt_idx" can be changed.
1436 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1437 * if it can be changed.
1438 */
1439 static int
1440validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1441{
1442 // Skip all options that are not window-local (used when showing
1443 // an already loaded buffer in a window).
1444 if ((opt_flags & OPT_WINONLY)
1445 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1446 return FAIL;
1447
1448 // Skip all options that are window-local (used for :vimgrep).
1449 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1450 && options[opt_idx].var == VAR_WIN)
1451 return FAIL;
1452
1453 // Disallow changing some options from modelines.
1454 if (opt_flags & OPT_MODELINE)
1455 {
1456 if (flags & (P_SECURE | P_NO_ML))
1457 {
1458 *errmsg = e_not_allowed_in_modeline;
1459 return FAIL;
1460 }
1461 if ((flags & P_MLE) && !p_mle)
1462 {
1463 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1464 return FAIL;
1465 }
1466#ifdef FEAT_DIFF
1467 // In diff mode some options are overruled. This avoids that
1468 // 'foldmethod' becomes "marker" instead of "diff" and that
1469 // "wrap" gets set.
1470 if (curwin->w_p_diff
1471 && opt_idx >= 0 // shut up coverity warning
1472 && (
1473# ifdef FEAT_FOLDING
1474 options[opt_idx].indir == PV_FDM ||
1475# endif
1476 options[opt_idx].indir == PV_WRAP))
1477 return FAIL;
1478#endif
1479 }
1480
1481#ifdef HAVE_SANDBOX
1482 // Disallow changing some options in the sandbox
1483 if (sandbox != 0 && (flags & P_SECURE))
1484 {
1485 *errmsg = e_not_allowed_in_sandbox;
1486 return FAIL;
1487 }
1488#endif
1489
1490 return OK;
1491}
1492
Bram Moolenaar47403942022-09-21 21:12:53 +01001493/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001494 * Get the Vim/Vi default value for a string option.
1495 */
1496 static char_u *
1497stropt_get_default_val(
1498 int opt_idx,
1499 char_u *varp,
1500 int flags,
1501 int cp_val)
1502{
1503 char_u *newval;
1504
1505 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1506 ? VI_DEFAULT : VIM_DEFAULT];
1507 if ((char_u **)varp == &p_bg)
1508 {
1509 // guess the value of 'background'
1510#ifdef FEAT_GUI
1511 if (gui.in_use)
1512 newval = gui_bg_default();
1513 else
1514#endif
1515 newval = term_bg_default();
1516 }
1517 else if ((char_u **)varp == &p_fencs && enc_utf8)
1518 newval = fencs_utf8_default;
1519
1520 // expand environment variables and ~ since the default value was
1521 // already expanded, only required when an environment variable was set
1522 // later
1523 if (newval == NULL)
1524 newval = empty_option;
1525 else
1526 {
1527 char_u *s = option_expand(opt_idx, newval);
1528 if (s == NULL)
1529 s = newval;
1530 newval = vim_strsave(s);
1531 }
1532
1533 return newval;
1534}
1535
1536/*
1537 * Convert the 'backspace' option number value to a string: for adding,
1538 * prepending and removing string.
1539 */
1540 static void
1541opt_backspace_nr2str(
1542 char_u *varp,
1543 char_u **origval_p,
1544 char_u **origval_l_p,
1545 char_u **origval_g_p,
1546 char_u **oldval_p)
1547{
1548 int i = getdigits((char_u **)varp);
1549
1550 switch (i)
1551 {
1552 case 0:
1553 *(char_u **)varp = empty_option;
1554 break;
1555 case 1:
1556 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1557 break;
1558 case 2:
1559 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1560 break;
1561 case 3:
1562 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1563 break;
1564 }
1565 vim_free(*oldval_p);
1566 if (*origval_p == *oldval_p)
1567 *origval_p = *(char_u **)varp;
1568 if (*origval_l_p == *oldval_p)
1569 *origval_l_p = *(char_u **)varp;
1570 if (*origval_g_p == *oldval_p)
1571 *origval_g_p = *(char_u **)varp;
1572 *oldval_p = *(char_u **)varp;
1573}
1574
1575/*
1576 * Convert the 'whichwrap' option number value to a string, for backwards
1577 * compatibility with Vim 3.0.
1578 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1579 */
1580 static char_u *
1581opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1582{
1583 *whichwrap = NUL;
1584 int i = getdigits(argp);
1585 if (i & 1)
1586 STRCAT(whichwrap, "b,");
1587 if (i & 2)
1588 STRCAT(whichwrap, "s,");
1589 if (i & 4)
1590 STRCAT(whichwrap, "h,l,");
1591 if (i & 8)
1592 STRCAT(whichwrap, "<,>,");
1593 if (i & 16)
1594 STRCAT(whichwrap, "[,],");
1595 if (*whichwrap != NUL) // remove trailing ,
1596 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1597
1598 return whichwrap;
1599}
1600
1601/*
1602 * Copy the new string value into allocated memory for the option.
1603 * Can't use set_string_option_direct(), because we need to remove the
1604 * backslashes.
1605 */
1606 static char_u *
1607stropt_copy_value(
1608 char_u *origval,
1609 char_u **argp,
1610 set_op_T op,
1611 int flags UNUSED)
1612{
1613 char_u *arg = *argp;
1614 unsigned newlen;
1615 char_u *newval;
1616 char_u *s = NULL;
1617
1618 // get a bit too much
1619 newlen = (unsigned)STRLEN(arg) + 1;
1620 if (op != OP_NONE)
1621 newlen += (unsigned)STRLEN(origval) + 1;
1622 newval = alloc(newlen);
1623 if (newval == NULL) // out of mem, don't change
1624 return NULL;
1625 s = newval;
1626
1627 // Copy the string, skip over escaped chars.
1628 // For MS-DOS and WIN32 backslashes before normal file name characters
1629 // are not removed, and keep backslash at start, for "\\machine\path",
1630 // but do remove it for "\\\\machine\\path".
1631 // The reverse is found in ExpandOldSetting().
1632 while (*arg != NUL && !VIM_ISWHITE(*arg))
1633 {
1634 int i;
1635
1636 if (*arg == '\\' && arg[1] != NUL
1637#ifdef BACKSLASH_IN_FILENAME
1638 && !((flags & P_EXPAND)
1639 && vim_isfilec(arg[1])
1640 && !VIM_ISWHITE(arg[1])
1641 && (arg[1] != '\\'
1642 || (s == newval && arg[2] != '\\')))
1643#endif
1644 )
1645 ++arg; // remove backslash
1646 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1647 {
1648 // copy multibyte char
1649 mch_memmove(s, arg, (size_t)i);
1650 arg += i;
1651 s += i;
1652 }
1653 else
1654 *s++ = *arg++;
1655 }
1656 *s = NUL;
1657
1658 *argp = arg;
1659 return newval;
1660}
1661
1662/*
1663 * Expand environment variables and ~ in string option value 'newval'.
1664 */
1665 static char_u *
1666stropt_expand_envvar(
1667 int opt_idx,
1668 char_u *origval,
1669 char_u *newval,
1670 set_op_T op)
1671{
1672 char_u *s = option_expand(opt_idx, newval);
1673 if (s == NULL)
1674 return newval;
1675
1676 vim_free(newval);
1677 unsigned newlen = (unsigned)STRLEN(s) + 1;
1678 if (op != OP_NONE)
1679 newlen += (unsigned)STRLEN(origval) + 1;
1680
1681 newval = alloc(newlen);
1682 if (newval == NULL)
1683 return NULL;
1684
1685 STRCPY(newval, s);
1686
1687 return newval;
1688}
1689
1690/*
1691 * Concatenate the original and new values of a string option, adding a "," if
1692 * needed.
1693 */
1694 static void
1695stropt_concat_with_comma(
1696 char_u *origval,
1697 char_u *newval,
1698 set_op_T op,
1699 int flags)
1700{
1701 int len = 0;
1702
1703 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1704 if (op == OP_ADDING)
1705 {
1706 len = (int)STRLEN(origval);
1707 // strip a trailing comma, would get 2
1708 if (comma && len > 1
1709 && (flags & P_ONECOMMA) == P_ONECOMMA
1710 && origval[len - 1] == ','
1711 && origval[len - 2] != '\\')
1712 len--;
1713 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1714 mch_memmove(newval, origval, (size_t)len);
1715 }
1716 else
1717 {
1718 len = (int)STRLEN(newval);
1719 STRMOVE(newval + len + comma, origval);
1720 }
1721 if (comma)
1722 newval[len] = ',';
1723}
1724
1725/*
1726 * Remove a value from a string option. Copy string option value in "origval"
1727 * to "newval" and then remove the string "strval" of length "len".
1728 */
1729 static void
1730stropt_remove_val(
1731 char_u *origval,
1732 char_u *newval,
1733 int flags,
1734 char_u *strval,
1735 int len)
1736{
1737 // Remove newval[] from origval[]. (Note: "len" has been set above
1738 // and is used here).
1739 STRCPY(newval, origval);
1740 if (*strval)
1741 {
1742 // may need to remove a comma
1743 if (flags & P_COMMA)
1744 {
1745 if (strval == origval)
1746 {
1747 // include comma after string
1748 if (strval[len] == ',')
1749 ++len;
1750 }
1751 else
1752 {
1753 // include comma before string
1754 --strval;
1755 ++len;
1756 }
1757 }
1758 STRMOVE(newval + (strval - origval), strval + len);
1759 }
1760}
1761
1762/*
1763 * Remove flags that appear twice in the string option value 'newval'.
1764 */
1765 static void
1766stropt_remove_dupflags(char_u *newval, int flags)
1767{
1768 char_u *s = newval;
1769
1770 // Remove flags that appear twice.
1771 while (*s)
1772 {
1773 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1774 if (flags & P_ONECOMMA)
1775 {
1776 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1777 {
1778 // Remove the duplicated value and the next comma.
1779 STRMOVE(s, s + 2);
1780 continue;
1781 }
1782 }
1783 else
1784 {
1785 if ((!(flags & P_COMMA) || *s != ',')
1786 && vim_strchr(s + 1, *s) != NULL)
1787 {
1788 STRMOVE(s, s + 1);
1789 continue;
1790 }
1791 }
1792 ++s;
1793 }
1794}
1795
1796/*
1797 * Get the string value specified for a ":set" command. The following set
1798 * options are supported:
1799 * set {opt}&
1800 * set {opt}<
1801 * set {opt}={val}
1802 * set {opt}:{val}
1803 */
1804 static char_u *
1805stropt_get_newval(
1806 int nextchar,
1807 int opt_idx,
1808 char_u **argp,
1809 char_u *varp,
1810 char_u **origval_arg,
1811 char_u **origval_l_arg,
1812 char_u **origval_g_arg,
1813 char_u **oldval_arg,
1814 set_op_T *op_arg,
1815 int flags,
1816 int cp_val)
1817{
1818 char_u *arg = *argp;
1819 char_u *origval = *origval_arg;
1820 char_u *origval_l = *origval_l_arg;
1821 char_u *origval_g = *origval_g_arg;
1822 char_u *oldval = *oldval_arg;
1823 set_op_T op = *op_arg;
1824 char_u *save_arg = NULL;
1825 char_u *newval;
1826 char_u *s = NULL;
1827 char_u whichwrap[80];
1828
1829 if (nextchar == '&') // set to default val
1830 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1831 else if (nextchar == '<') // set to global val
1832 newval = vim_strsave(*(char_u **)get_varp_scope(
1833 &(options[opt_idx]), OPT_GLOBAL));
1834 else
1835 {
1836 ++arg; // jump to after the '=' or ':'
1837
1838 // Set 'keywordprg' to ":help" if an empty
1839 // value was passed to :set by the user.
1840 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1841 {
1842 save_arg = arg;
1843 arg = (char_u *)":help";
1844 }
1845 // Convert 'backspace' number to string
1846 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1847 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1848 &oldval);
1849 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1850 {
1851 // Convert 'whichwrap' number to string, for backwards
1852 // compatibility with Vim 3.0.
1853 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1854 save_arg = arg;
1855 arg = t;
1856 }
1857 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1858 // version 3.0
1859 else if (*arg == '>' && (varp == (char_u *)&p_dir
1860 || varp == (char_u *)&p_bdir))
1861 ++arg;
1862
1863 // Copy the new string into allocated memory.
1864 newval = stropt_copy_value(origval, &arg, op, flags);
1865 if (newval == NULL)
1866 goto done;
1867
1868 // Expand environment variables and ~.
1869 // Don't do it when adding without inserting a comma.
1870 if (op == OP_NONE || (flags & P_COMMA))
1871 {
1872 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1873 if (newval == NULL)
1874 goto done;
1875 }
1876
1877 // locate newval[] in origval[] when removing it and when adding to
1878 // avoid duplicates
1879 int len = 0;
1880 if (op == OP_REMOVING || (flags & P_NODUP))
1881 {
1882 len = (int)STRLEN(newval);
1883 s = find_dup_item(origval, newval, flags);
1884
1885 // do not add if already there
1886 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1887 {
1888 op = OP_NONE;
1889 STRCPY(newval, origval);
1890 }
1891
1892 // if no duplicate, move pointer to end of original value
1893 if (s == NULL)
1894 s = origval + (int)STRLEN(origval);
1895 }
1896
1897 // concatenate the two strings; add a ',' if needed
1898 if (op == OP_ADDING || op == OP_PREPENDING)
1899 stropt_concat_with_comma(origval, newval, op, flags);
1900 else if (op == OP_REMOVING)
1901 // Remove newval[] from origval[]. (Note: "len" has been set above
1902 // and is used here).
1903 stropt_remove_val(origval, newval, flags, s, len);
1904
1905 if (flags & P_FLAGLIST)
1906 // Remove flags that appear twice.
1907 stropt_remove_dupflags(newval, flags);
1908 }
1909
1910done:
1911 if (save_arg != NULL)
1912 arg = save_arg; // arg was temporarily changed, restore it
1913 *argp = arg;
1914 *origval_arg = origval;
1915 *origval_l_arg = origval_l;
1916 *origval_g_arg = origval_g;
1917 *oldval_arg = oldval;
1918 *op_arg = op;
1919
1920 return newval;
1921}
1922
1923/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001924 * Part of do_set() for string options.
1925 * Returns FAIL on failure, do not process further options.
1926 */
1927 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001928do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001929 int opt_idx,
1930 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001931 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001932 int nextchar,
1933 set_op_T op_arg,
1934 int flags,
1935 int cp_val,
1936 char_u *varp_arg,
1937 char *errbuf,
1938 int *value_checked,
1939 char **errmsg)
1940{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001941 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001942 set_op_T op = op_arg;
1943 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001944 char_u *oldval = NULL; // previous value if *varp
1945 char_u *newval;
1946 char_u *origval = NULL;
1947 char_u *origval_l = NULL;
1948 char_u *origval_g = NULL;
1949#if defined(FEAT_EVAL)
1950 char_u *saved_origval = NULL;
1951 char_u *saved_origval_l = NULL;
1952 char_u *saved_origval_g = NULL;
1953 char_u *saved_newval = NULL;
1954#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001955
1956 // When using ":set opt=val" for a global option
1957 // with a local value the local value will be
1958 // reset, use the global value here.
1959 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1960 && ((int)options[opt_idx].indir & PV_BOTH))
1961 varp = options[opt_idx].var;
1962
1963 // The old value is kept until we are sure that the new value is valid.
1964 oldval = *(char_u **)varp;
1965
1966 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1967 {
1968 origval_l = *(char_u **)get_varp_scope(
1969 &(options[opt_idx]), OPT_LOCAL);
1970 origval_g = *(char_u **)get_varp_scope(
1971 &(options[opt_idx]), OPT_GLOBAL);
1972
1973 // A global-local string option might have an empty option as value to
1974 // indicate that the global value should be used.
1975 if (((int)options[opt_idx].indir & PV_BOTH)
1976 && origval_l == empty_option)
1977 origval_l = origval_g;
1978 }
1979
1980 // When setting the local value of a global option, the old value may be
1981 // the global value.
1982 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1983 origval = *(char_u **)get_varp(&options[opt_idx]);
1984 else
1985 origval = oldval;
1986
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001987 // Get the new value for the option
1988 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1989 &origval_l, &origval_g, &oldval, &op, flags,
1990 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001991
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001992 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001993 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001994 if (newval == NULL)
1995 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001996
1997#if defined(FEAT_EVAL)
1998 if (!starting
1999# ifdef FEAT_CRYPT
2000 && options[opt_idx].indir != PV_KEY
2001# endif
2002 && origval != NULL && newval != NULL)
2003 {
2004 // origval may be freed by did_set_string_option(), make a copy.
2005 saved_origval = vim_strsave(origval);
2006 // newval (and varp) may become invalid if the buffer is closed by
2007 // autocommands.
2008 saved_newval = vim_strsave(newval);
2009 if (origval_l != NULL)
2010 saved_origval_l = vim_strsave(origval_l);
2011 if (origval_g != NULL)
2012 saved_origval_g = vim_strsave(origval_g);
2013 }
2014#endif
2015
2016 {
2017 long_u *p = insecure_flag(opt_idx, opt_flags);
2018 int secure_saved = secure;
2019
2020 // When an option is set in the sandbox, from a modeline or in secure
2021 // mode, then deal with side effects in secure mode. Also when the
2022 // value was set with the P_INSECURE flag and is not completely
2023 // replaced.
2024 if ((opt_flags & OPT_MODELINE)
2025#ifdef HAVE_SANDBOX
2026 || sandbox != 0
2027#endif
2028 || (op != OP_NONE && (*p & P_INSECURE)))
2029 secure = 1;
2030
2031 // Handle side effects, and set the global value for ":set" on local
2032 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2033 // be triggered that can cause havoc.
2034 *errmsg = did_set_string_option(
2035 opt_idx, (char_u **)varp, oldval, errbuf,
2036 opt_flags, value_checked);
2037
2038 secure = secure_saved;
2039 }
2040
2041#if defined(FEAT_EVAL)
2042 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002043 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002044 saved_origval_l, saved_origval_g, saved_newval);
2045 vim_free(saved_origval);
2046 vim_free(saved_origval_l);
2047 vim_free(saved_origval_g);
2048 vim_free(saved_newval);
2049#endif
2050
Bram Moolenaar6f981142022-09-22 12:48:58 +01002051 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002052 return *errmsg == NULL ? OK : FAIL;
2053}
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002056 * Set a boolean option.
2057 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002058 */
2059 static char *
2060do_set_option_bool(
2061 int opt_idx,
2062 int opt_flags,
2063 set_prefix_T prefix,
2064 long_u flags,
2065 char_u *varp,
2066 int nextchar,
2067 int afterchar,
2068 int cp_val)
2069
2070{
2071 varnumber_T value;
2072
2073 if (nextchar == '=' || nextchar == ':')
2074 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002075 if (opt_idx < 0 || varp == NULL)
2076 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002077
2078 /*
2079 * ":set opt!": invert
2080 * ":set opt&": reset to default value
2081 * ":set opt<": reset to global value
2082 */
2083 if (nextchar == '!')
2084 value = *(int *)(varp) ^ 1;
2085 else if (nextchar == '&')
2086 value = (int)(long)(long_i)options[opt_idx].def_val[
2087 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2088 else if (nextchar == '<')
2089 {
2090 // For 'autoread' -1 means to use global value.
2091 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2092 value = -1;
2093 else
2094 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2095 }
2096 else
2097 {
2098 /*
2099 * ":set invopt": invert
2100 * ":set opt" or ":set noopt": set or reset
2101 */
2102 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2103 return e_trailing_characters;
2104 if (prefix == PREFIX_INV)
2105 value = *(int *)(varp) ^ 1;
2106 else
2107 value = prefix == PREFIX_NO ? 0 : 1;
2108 }
2109
2110 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2111}
2112
2113/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002114 * Set a numeric option.
2115 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002116 */
2117 static char *
2118do_set_option_numeric(
2119 int opt_idx,
2120 int opt_flags,
2121 char_u **argp,
2122 int nextchar,
2123 set_op_T op,
2124 long_u flags,
2125 int cp_val,
2126 char_u *varp,
2127 char *errbuf,
2128 size_t errbuflen)
2129{
2130 char_u *arg = *argp;
2131 varnumber_T value;
2132 int i;
2133 char *errmsg = NULL;
2134
Bram Moolenaar546933f2023-02-06 16:40:49 +00002135 if (opt_idx < 0 || varp == NULL)
2136 return NULL; // "cannot happen"
2137 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002138 /*
2139 * Different ways to set a number option:
2140 * & set to default value
2141 * < set to global value
2142 * <xx> accept special key codes for 'wildchar'
2143 * c accept any non-digit for 'wildchar'
2144 * [-]0-9 set number
2145 * other error
2146 */
2147 ++arg;
2148 if (nextchar == '&')
2149 value = (long)(long_i)options[opt_idx].def_val[
2150 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2151 else if (nextchar == '<')
2152 {
2153 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2154 // use the global value.
2155 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2156 value = NO_LOCAL_UNDOLEVEL;
2157 else
2158 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2159 }
2160 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2161 && (*arg == '<'
2162 || *arg == '^'
2163 || (*arg != NUL
2164 && (!arg[1] || VIM_ISWHITE(arg[1]))
2165 && !VIM_ISDIGIT(*arg))))
2166 {
2167 value = string_to_key(arg, FALSE);
2168 if (value == 0 && (long *)varp != &p_wcm)
2169 {
2170 errmsg = e_invalid_argument;
2171 goto skip;
2172 }
2173 }
2174 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2175 {
2176 // Allow negative (for 'undolevels'), octal and hex numbers.
2177 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
2178 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2179 {
2180 errmsg = e_number_required_after_equal;
2181 goto skip;
2182 }
2183 }
2184 else
2185 {
2186 errmsg = e_number_required_after_equal;
2187 goto skip;
2188 }
2189
2190 if (op == OP_ADDING)
2191 value = *(long *)varp + value;
2192 else if (op == OP_PREPENDING)
2193 value = *(long *)varp * value;
2194 else if (op == OP_REMOVING)
2195 value = *(long *)varp - value;
2196
2197 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2198 opt_flags);
2199
2200skip:
2201 *argp = arg;
2202 return errmsg;
2203}
2204
2205/*
2206 * Set a key code (t_xx) option
2207 */
2208 static char *
2209do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2210{
2211 char_u *arg = *argp;
2212 char_u *p;
2213
2214 if (nextchar == '&')
2215 {
2216 if (add_termcap_entry(key_name, TRUE) == FAIL)
2217 return e_not_found_in_termcap;
2218 }
2219 else
2220 {
2221 ++arg; // jump to after the '=' or ':'
2222 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2223 if (*p == '\\' && p[1] != NUL)
2224 ++p;
2225 nextchar = *p;
2226 *p = NUL;
2227 add_termcode(key_name, arg, FALSE);
2228 *p = nextchar;
2229 }
2230 if (full_screen)
2231 ttest(FALSE);
2232 redraw_all_later(UPD_CLEAR);
2233
2234 *argp = arg;
2235 return NULL;
2236}
2237
2238/*
2239 * Set an option to a new value.
2240 */
2241 static char *
2242do_set_option_value(
2243 int opt_idx,
2244 int opt_flags,
2245 char_u **argp,
2246 set_prefix_T prefix,
2247 set_op_T op,
2248 long_u flags,
2249 char_u *varp,
2250 char_u *key_name,
2251 int nextchar,
2252 int afterchar,
2253 int cp_val,
2254 int *stopopteval,
2255 char *errbuf,
2256 size_t errbuflen)
2257{
2258 int value_checked = FALSE;
2259 char *errmsg = NULL;
2260 char_u *arg = *argp;
2261
2262 if (flags & P_BOOL)
2263 {
2264 // boolean option
2265 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2266 nextchar, afterchar, cp_val);
2267 if (errmsg != NULL)
2268 goto skip;
2269 }
2270 else
2271 {
2272 // numeric or string option
2273 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2274 || prefix != PREFIX_NONE)
2275 {
2276 errmsg = e_invalid_argument;
2277 goto skip;
2278 }
2279
2280 if (flags & P_NUM)
2281 {
2282 // numeric option
2283 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2284 op, flags, cp_val, varp,
2285 errbuf, errbuflen);
2286 if (errmsg != NULL)
2287 goto skip;
2288 }
2289 else if (opt_idx >= 0)
2290 {
2291 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002292 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2293 flags, cp_val, varp, errbuf,
2294 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002295 {
2296 if (errmsg != NULL)
2297 goto skip;
2298 *stopopteval = TRUE;
2299 goto skip;
2300 }
2301 }
2302 else
2303 {
2304 // key code option
2305 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2306 if (errmsg != NULL)
2307 goto skip;
2308 }
2309 }
2310
2311 if (opt_idx >= 0)
2312 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2313
2314skip:
2315 *argp = arg;
2316 return errmsg;
2317}
2318
2319/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002320 * Set an option to a new value.
2321 * Return NULL if OK, return an untranslated error message when something is
2322 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2323 */
2324 static char *
2325do_set_option(
2326 int opt_flags,
2327 char_u **argp,
2328 char_u *arg_start,
2329 char_u **startarg,
2330 int *did_show,
2331 int *stopopteval,
2332 char *errbuf,
2333 size_t errbuflen)
2334{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002335 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002336 char_u *arg;
2337 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2338 set_op_T op;
2339 long_u flags; // flags for current option
2340 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002341 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002342 int nextchar; // next non-white char after option name
2343 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002344 char *errmsg = NULL;
2345 int key;
2346 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002347
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002348 prefix = get_option_prefix(argp);
2349 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002350
2351 // find end of name
2352 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002353 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2354 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002355
2356 // remember character after option name
2357 afterchar = arg[len];
2358
2359 if (in_vim9script())
2360 {
2361 char_u *p = skipwhite(arg + len);
2362
2363 // disallow white space before =val, +=val, -=val, ^=val
2364 if (p > arg + len && (p[0] == '='
2365 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2366 && p[1] == '=')))
2367 {
2368 errmsg = e_no_white_space_allowed_between_option_and;
2369 arg = p;
2370 *startarg = p;
2371 goto skip;
2372 }
2373 }
2374 else
2375 // skip white space, allow ":set ai ?", ":set hlsearch !"
2376 while (VIM_ISWHITE(arg[len]))
2377 ++len;
2378
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002379 op = get_opt_op(arg + len);
2380 if (op != OP_NONE)
2381 len++;
2382
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002383 nextchar = arg[len];
2384
2385 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2386 {
2387 if (in_vim9script() && arg > arg_start
2388 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2389 errmsg = e_no_white_space_allowed_between_option_and;
2390 else
2391 errmsg = e_unknown_option;
2392 goto skip;
2393 }
2394
2395 if (opt_idx >= 0)
2396 {
2397 if (options[opt_idx].var == NULL) // hidden option: skip
2398 {
2399 // Only give an error message when requesting the value of
2400 // a hidden option, ignore setting it.
2401 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2402 && (!(options[opt_idx].flags & P_BOOL)
2403 || nextchar == '?'))
2404 errmsg = e_option_not_supported;
2405 goto skip;
2406 }
2407
2408 flags = options[opt_idx].flags;
2409 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2410 }
2411 else
2412 {
2413 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002414 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002415 if (key < 0)
2416 {
2417 key_name[0] = KEY2TERMCAP0(key);
2418 key_name[1] = KEY2TERMCAP1(key);
2419 }
2420 else
2421 {
2422 key_name[0] = KS_KEY;
2423 key_name[1] = (key & 0xff);
2424 }
2425 }
2426
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002427 // Make sure the option value can be changed.
2428 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002429 goto skip;
2430
Bram Moolenaar40b48722023-02-05 17:04:50 +00002431 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002432 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2433 {
2434 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002435 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2436 {
2437 if (arg[3] == 'm') // "opt&vim": set to Vim default
2438 {
2439 cp_val = FALSE;
2440 arg += 3;
2441 }
2442 else // "opt&vi": set to Vi default
2443 {
2444 cp_val = TRUE;
2445 arg += 2;
2446 }
2447 }
2448 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2449 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2450 {
2451 errmsg = e_trailing_characters;
2452 goto skip;
2453 }
2454 }
2455
2456 /*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002457 * Allow '=' and ':' for historical reasons (MSDOS command.com).
2458 * Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002459 */
2460 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002461 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002462 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2463 && !(flags & P_BOOL)))
2464 {
2465 /*
2466 * print value
2467 */
2468 if (*did_show)
2469 msg_putchar('\n'); // cursor below last one
2470 else
2471 {
2472 gotocmdline(TRUE); // cursor at status line
2473 *did_show = TRUE; // remember that we did a line
2474 }
2475 if (opt_idx >= 0)
2476 {
2477 showoneopt(&options[opt_idx], opt_flags);
2478#ifdef FEAT_EVAL
2479 if (p_verbose > 0)
2480 {
2481 // Mention where the option was last set.
2482 if (varp == options[opt_idx].var)
2483 last_set_msg(options[opt_idx].script_ctx);
2484 else if ((int)options[opt_idx].indir & PV_WIN)
2485 last_set_msg(curwin->w_p_script_ctx[
2486 (int)options[opt_idx].indir & PV_MASK]);
2487 else if ((int)options[opt_idx].indir & PV_BUF)
2488 last_set_msg(curbuf->b_p_script_ctx[
2489 (int)options[opt_idx].indir & PV_MASK]);
2490 }
2491#endif
2492 }
2493 else
2494 {
2495 char_u *p;
2496
2497 p = find_termcode(key_name);
2498 if (p == NULL)
2499 {
2500 errmsg = e_key_code_not_set;
2501 goto skip;
2502 }
2503 else
2504 (void)show_one_termcode(key_name, p, TRUE);
2505 }
2506 if (nextchar != '?'
2507 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2508 errmsg = e_trailing_characters;
2509 }
2510 else
2511 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002512 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2513 flags, varp, key_name, nextchar,
2514 afterchar, cp_val, stopopteval, errbuf,
2515 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002516 }
2517
2518skip:
2519 *argp = arg;
2520 return errmsg;
2521}
2522
2523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Parse 'arg' for option settings.
2525 *
2526 * 'arg' may be IObuff, but only when no errors can be present and option
2527 * does not need to be expanded with option_expand().
2528 * "opt_flags":
2529 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002530 * OPT_GLOBAL for ":setglobal"
2531 * OPT_LOCAL for ":setlocal" and a modeline
2532 * OPT_MODELINE for a modeline
2533 * OPT_WINONLY to only set window-local options
2534 * OPT_NOWIN to skip setting window-local options
2535 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002537 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 */
2539 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002540do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002541 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002542 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002544 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002546 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 if (*arg == NUL)
2549 {
2550 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 did_show = TRUE;
2552 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002555 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002557 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002558 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /*
2561 * ":set all" show all options.
2562 * ":set all&" set all options to their default value.
2563 */
2564 arg += 3;
2565 if (*arg == '&')
2566 {
2567 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002568 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002570 didset_options();
2571 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002572 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 did_show = TRUE;
2578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002580 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
2582 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002583 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002584 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 arg += 7;
2586 }
2587 else
2588 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002589 int stopopteval = FALSE;
2590 char *errmsg = NULL;
2591 char errbuf[80];
2592 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002594 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2595 &did_show, &stopopteval, errbuf,
2596 sizeof(errbuf));
2597 if (stopopteval)
2598 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 /*
2601 * Advance to next argument.
2602 * - skip until a blank found, taking care of backslashes
2603 * - skip blanks
2604 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2605 */
2606 for (i = 0; i < 2 ; ++i)
2607 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002608 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (*arg++ == '\\' && *arg != NUL)
2610 ++arg;
2611 arg = skipwhite(arg);
2612 if (*arg != '=')
2613 break;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002616 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002618 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2619 i = (int)STRLEN(IObuff) + 2;
2620 if (i + (arg - startarg) < IOSIZE)
2621 {
2622 // append the argument with the error
2623 STRCAT(IObuff, ": ");
2624 mch_memmove(IObuff + i, startarg, (arg - startarg));
2625 IObuff[i + (arg - startarg)] = NUL;
2626 }
2627 // make sure all characters are printable
2628 trans_characters(IObuff, IOSIZE);
2629
2630 ++no_wait_return; // wait_return() done later
2631 emsg((char *)IObuff); // show error highlighted
2632 --no_wait_return;
2633
2634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637
2638 arg = skipwhite(arg);
2639 }
2640
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641theend:
2642 if (silent_mode && did_show)
2643 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002644 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002645 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002646 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002647 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002648 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002649 out_flush();
2650 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002651 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002652 }
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return OK;
2655}
2656
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002657/*
2658 * Call this when an option has been given a new value through a user command.
2659 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2660 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002662did_set_option(
2663 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002664 int opt_flags, // possibly with OPT_MODELINE
2665 int new_value, // value was replaced completely
2666 int value_checked) // value was checked to be safe, no need to set the
2667 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002668{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002669 long_u *p;
2670
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002671 options[opt_idx].flags |= P_WAS_SET;
2672
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002673 // When an option is set in the sandbox, from a modeline or in secure mode
2674 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2675 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002676 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002677 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002678#ifdef HAVE_SANDBOX
2679 || sandbox != 0
2680#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002681 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002682 *p = *p | P_INSECURE;
2683 else if (new_value)
2684 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002685}
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687/*
2688 * Convert a key name or string into a key value.
2689 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002690 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002692 int
2693string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002696 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (*arg == '^')
2698 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002699 if (multi_byte)
2700 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 return *arg;
2702}
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704/*
2705 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2706 * maketitle() to create and display it.
2707 * When switching the title or icon off, call mch_restore_title() to get
2708 * the old value back.
2709 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002710 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002711did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 if (starting != NO_SCREEN
2714#ifdef FEAT_GUI
2715 && !gui.starting
2716#endif
2717 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721/*
2722 * set_options_bin - called when 'bin' changes value.
2723 */
2724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002725set_options_bin(
2726 int oldval,
2727 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730 /*
2731 * The option values that are changed when 'bin' changes are
2732 * copied when 'bin is set and restored when 'bin' is reset.
2733 */
2734 if (newval)
2735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738 if (!(opt_flags & OPT_GLOBAL))
2739 {
2740 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2741 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2742 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2743 curbuf->b_p_et_nobin = curbuf->b_p_et;
2744 }
2745 if (!(opt_flags & OPT_LOCAL))
2746 {
2747 p_tw_nobin = p_tw;
2748 p_wm_nobin = p_wm;
2749 p_ml_nobin = p_ml;
2750 p_et_nobin = p_et;
2751 }
2752 }
2753
2754 if (!(opt_flags & OPT_GLOBAL))
2755 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 curbuf->b_p_tw = 0; // no automatic line wrap
2757 curbuf->b_p_wm = 0; // no automatic line wrap
2758 curbuf->b_p_ml = 0; // no modelines
2759 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 if (!(opt_flags & OPT_LOCAL))
2762 {
2763 p_tw = 0;
2764 p_wm = 0;
2765 p_ml = FALSE;
2766 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 if (!(opt_flags & OPT_GLOBAL))
2773 {
2774 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2775 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2776 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2777 curbuf->b_p_et = curbuf->b_p_et_nobin;
2778 }
2779 if (!(opt_flags & OPT_LOCAL))
2780 {
2781 p_tw = p_tw_nobin;
2782 p_wm = p_wm_nobin;
2783 p_ml = p_ml_nobin;
2784 p_et = p_et_nobin;
2785 }
2786 }
2787}
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789/*
2790 * Expand environment variables for some string options.
2791 * These string options cannot be indirect!
2792 * If "val" is NULL expand the current value of the option.
2793 * Return pointer to NameBuff, or NULL when not expanded.
2794 */
2795 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002796option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002798 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2800 return NULL;
2801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002802 // If val is longer than MAXPATHL no meaningful expansion can be done,
2803 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (val != NULL && STRLEN(val) > MAXPATHL)
2805 return NULL;
2806
2807 if (val == NULL)
2808 val = *(char_u **)options[opt_idx].var;
2809
2810 /*
2811 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2812 * Escape spaces when expanding 'tags', they are used to separate file
2813 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002814 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 */
2816 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002817 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002818#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002819 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2820#endif
2821 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002822 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 return NULL;
2824
2825 return NameBuff;
2826}
2827
2828/*
2829 * After setting various option values: recompute variables that depend on
2830 * option values.
2831 */
2832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002833didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 (void)init_chartab();
2837
Bram Moolenaardac13472019-09-16 21:06:21 +02002838 didset_string_options();
2839
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002840#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002841 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002842 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002843 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002844 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002845#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002846 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002848#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002850 fill_breakat_flags();
2851#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002852 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002853}
2854
2855/*
2856 * More side effects of setting options.
2857 */
2858 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002859didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002860{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002862 (void)highlight_changed();
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002865 check_opt_wim();
2866
Bram Moolenaareed9d462021-02-15 20:38:25 +01002867 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002868 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002871 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002872
2873#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002875 (void)check_clipboard_option();
2876#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002877#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002878 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002879 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002880 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002881 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883}
2884
2885/*
2886 * Check for string options that are NULL (normally only termcap options).
2887 */
2888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 int opt_idx;
2892
2893 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2894 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2895 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2896}
2897
2898/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002899 * Return the option index found by a pointer into term_strings[].
2900 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002902 int
2903get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002905 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2908 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002909 return opt_idx;
2910 return -1; // cannot happen: didn't find it!
2911}
2912
2913/*
2914 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2915 * Return the option index or -1 if not found.
2916 */
2917 int
2918set_term_option_alloced(char_u **p)
2919{
2920 int opt_idx = get_term_opt_idx(p);
2921
2922 if (opt_idx >= 0)
2923 options[opt_idx].flags |= P_ALLOCED;
2924 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925}
2926
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002927#if defined(FEAT_EVAL) || defined(PROTO)
2928/*
2929 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2930 * Return FALSE when it wasn't.
2931 * Return -1 for an unknown option.
2932 */
2933 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002934was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002935{
2936 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002937 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002938
2939 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002940 {
2941 flagp = insecure_flag(idx, opt_flags);
2942 return (*flagp & P_INSECURE) != 0;
2943 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002944 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002945 return -1;
2946}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002947
2948/*
2949 * Get a pointer to the flags used for the P_INSECURE flag of option
2950 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002951 * NOTE: Caller must make sure that "curwin" is set to the window from which
2952 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002953 */
2954 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002955insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002956{
2957 if (opt_flags & OPT_LOCAL)
2958 switch ((int)options[opt_idx].indir)
2959 {
2960#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002961 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002962#endif
2963#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002964# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002965 case PV_FDE: return &curwin->w_p_fde_flags;
2966 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002967# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002968# ifdef FEAT_BEVAL
2969 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2970# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002971 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002972 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002973# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002974 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002975# endif
2976#endif
2977 }
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002980 return &options[opt_idx].flags;
2981}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002982#endif
2983
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984/*
2985 * Redraw the window title and/or tab page text later.
2986 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002987void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002988{
2989 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002990 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002991}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002994 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2995 * characters or characters in "allowed".
2996 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002997 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002998valid_name(char_u *val, char *allowed)
2999{
3000 char_u *s;
3001
3002 for (s = val; *s != NUL; ++s)
3003 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3004 return FALSE;
3005 return TRUE;
3006}
3007
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003008#if defined(FEAT_EVAL) || defined(PROTO)
3009/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003010 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003011 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003012 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003013 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003014set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003015{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003016 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3017 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003018 sctx_T new_script_ctx = script_ctx;
3019
Bram Moolenaar51258742020-05-03 17:19:33 +02003020 // Modeline already has the line number set.
3021 if (!(opt_flags & OPT_MODELINE))
3022 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // Remember where the option was set. For local options need to do that
3025 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003027 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003028 if (both || (opt_flags & OPT_LOCAL))
3029 {
3030 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003031 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003032 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003033 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003034 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003035 if (both)
3036 // also setting the "all buffers" value
3037 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3038 new_script_ctx;
3039 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003040 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003041}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003042
3043/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003044 * Get the script context of global option "name".
3045 *
3046 */
3047 sctx_T *
3048get_option_sctx(char *name)
3049{
3050 int idx = findoption((char_u *)name);
3051
3052 if (idx >= 0)
3053 return &options[idx].script_ctx;
3054 siemsg("no such option: %s", name);
3055 return NULL;
3056}
3057
3058/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003059 * Set the script_ctx for a termcap option.
3060 * "name" must be the two character code, e.g. "RV".
3061 * When "name" is NULL use "opt_idx".
3062 */
3063 void
3064set_term_option_sctx_idx(char *name, int opt_idx)
3065{
3066 char_u buf[5];
3067 int idx;
3068
3069 if (name == NULL)
3070 idx = opt_idx;
3071 else
3072 {
3073 buf[0] = 't';
3074 buf[1] = '_';
3075 buf[2] = name[0];
3076 buf[3] = name[1];
3077 buf[4] = 0;
3078 idx = findoption(buf);
3079 }
3080 if (idx >= 0)
3081 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3082}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003083#endif
3084
Bram Moolenaarf4140482020-02-15 23:06:45 +01003085#if defined(FEAT_EVAL)
3086/*
3087 * Apply the OptionSet autocommand.
3088 */
3089 static void
3090apply_optionset_autocmd(
3091 int opt_idx,
3092 long opt_flags,
3093 long oldval,
3094 long oldval_g,
3095 long newval,
3096 char *errmsg)
3097{
3098 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3099
3100 // Don't do this while starting up, failure or recursively.
3101 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3102 return;
3103
3104 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3105 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3106 oldval_g);
3107 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3108 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3109 (opt_flags & OPT_LOCAL) ? "local" : "global");
3110 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3111 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3112 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3113 if (opt_flags & OPT_LOCAL)
3114 {
3115 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3116 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3117 }
3118 if (opt_flags & OPT_GLOBAL)
3119 {
3120 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3121 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3122 }
3123 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3124 {
3125 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3126 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3127 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3128 }
3129 if (opt_flags & OPT_MODELINE)
3130 {
3131 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3132 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3133 }
3134 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3135 NULL, FALSE, NULL);
3136 reset_v_option_vars();
3137}
3138#endif
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140/*
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003141 * Process the updated 'compatible' option value.
3142 */
3143 static void
3144did_set_compatible(void)
3145{
3146 compatible_set();
3147}
3148
3149#ifdef FEAT_LANGMAP
3150/*
3151 * Process the updated 'langremap' option value.
3152 */
3153 static void
3154did_set_langremap(void)
3155{
3156 // 'langremap' -> !'langnoremap'
3157 p_lnr = !p_lrm;
3158}
3159
3160/*
3161 * Process the updated 'langnoremap' option value.
3162 */
3163 static void
3164did_set_langnoremap(void)
3165{
3166 // 'langnoremap' -> !'langremap'
3167 p_lrm = !p_lnr;
3168}
3169#endif
3170
3171#ifdef FEAT_PERSISTENT_UNDO
3172/*
3173 * Process the updated 'undofile' option value.
3174 */
3175 static void
3176did_set_undofile(int opt_flags)
3177{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003178 // Only take action when the option was set.
3179 if (!curbuf->b_p_udf && !p_udf)
3180 return;
3181
3182 // When reset we do not delete the undo file, the option may be set again
3183 // without making any changes in between.
3184 char_u hash[UNDO_HASH_SIZE];
3185 buf_T *save_curbuf = curbuf;
3186
3187 FOR_ALL_BUFFERS(curbuf)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003188 {
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003189 // When 'undofile' is set globally: for every buffer, otherwise
3190 // only for the current buffer: Try to read in the undofile,
3191 // if one exists, the buffer wasn't changed and the buffer was
3192 // loaded
3193 if ((curbuf == save_curbuf
3194 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
3195 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003196 {
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003197#ifdef FEAT_CRYPT
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003198 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
3199 continue;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003200#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003201 u_compute_hash(hash);
3202 u_read_undo(NULL, hash, curbuf->b_fname);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003203 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003204 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003205 curbuf = save_curbuf;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003206}
3207#endif
3208
3209/*
3210 * Process the updated 'readonly' option value.
3211 */
3212 static void
3213did_set_readonly(int opt_flags)
3214{
3215 // when 'readonly' is reset globally, also reset readonlymode
3216 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
3217 readonlymode = FALSE;
3218
3219 // when 'readonly' is set may give W10 again
3220 if (curbuf->b_p_ro)
3221 curbuf->b_did_warn = FALSE;
3222
3223 redraw_titles();
3224}
3225
3226#ifdef FEAT_GUI
3227/*
3228 * Process the updated 'mousehide' option value.
3229 */
3230 static void
3231did_set_mousehide(void)
3232{
3233 if (!p_mh)
3234 gui_mch_mousehide(FALSE);
3235}
3236#endif
3237
3238/*
3239 * Process the updated 'modifiable' option value.
3240 */
3241 static char *
3242did_set_modifiable(int *doskip UNUSED)
3243{
3244 // when 'modifiable' is changed, redraw the window title
3245
3246# ifdef FEAT_TERMINAL
3247 // Cannot set 'modifiable' when in Terminal mode.
3248 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3249 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3250 {
3251 curbuf->b_p_ma = FALSE;
3252 *doskip = TRUE;
3253 return e_cannot_make_terminal_with_running_job_modifiable;
3254 }
3255# endif
3256 redraw_titles();
3257
3258 return NULL;
3259}
3260
3261/*
3262 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3263 * option value.
3264 */
3265 static void
3266did_set_eof_eol_fixeol_bomb(void)
3267{
3268 // redraw the window title and tab page text
3269 redraw_titles();
3270}
3271
3272/*
3273 * Process the updated 'binary' option value.
3274 */
3275 static void
3276did_set_binary(int opt_flags, long old_value)
3277{
3278 // when 'bin' is set also set some other options
3279 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
3280 redraw_titles();
3281}
3282
3283/*
3284 * Process the updated 'buflisted' option value.
3285 */
3286 static void
3287did_set_buflisted(long old_value)
3288{
3289 // when 'buflisted' changes, trigger autocommands
3290 if (old_value != curbuf->b_p_bl)
3291 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3292 NULL, NULL, TRUE, curbuf);
3293}
3294
3295/*
3296 * Process the updated 'swapfile' option value.
3297 */
3298 static void
3299did_set_swapfile(void)
3300{
3301 // when 'swf' is set, create swapfile, when reset remove swapfile
3302 if (curbuf->b_p_swf && p_uc)
3303 ml_open_file(curbuf); // create the swap file
3304 else
3305 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3306 // buf->b_p_swf
3307 mf_close_file(curbuf, TRUE); // remove the swap file
3308}
3309
3310/*
3311 * Process the updated 'terse' option value.
3312 */
3313 static void
3314did_set_terse(void)
3315{
3316 char_u *p;
3317
3318 // when 'terse' is set change 'shortmess'
3319 p = vim_strchr(p_shm, SHM_SEARCH);
3320
3321 // insert 's' in p_shm
3322 if (p_terse && p == NULL)
3323 {
3324 STRCPY(IObuff, p_shm);
3325 STRCAT(IObuff, "s");
3326 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
3327 }
3328 // remove 's' from p_shm
3329 else if (!p_terse && p != NULL)
3330 STRMOVE(p, p + 1);
3331}
3332
3333/*
3334 * Process the updated 'paste' option value.
3335 */
3336 static void
3337did_set_paste(void)
3338{
3339 // when 'paste' is set or reset also change other options
3340 paste_option_changed();
3341}
3342
3343/*
3344 * Process the updated 'insertmode' option value.
3345 */
3346 static void
3347did_set_insertmode(long old_value)
3348{
3349 // when 'insertmode' is set from an autocommand need to do work here
3350 if (p_im)
3351 {
3352 if ((State & MODE_INSERT) == 0)
3353 need_start_insertmode = TRUE;
3354 stop_insert_mode = FALSE;
3355 }
3356 // only reset if it was set previously
3357 else if (old_value)
3358 {
3359 need_start_insertmode = FALSE;
3360 stop_insert_mode = TRUE;
3361 if (restart_edit != 0 && mode_displayed)
3362 clear_cmdline = TRUE; // remove "(insert)"
3363 restart_edit = 0;
3364 }
3365}
3366
3367/*
3368 * Process the updated 'ignorecase' option value.
3369 */
3370 static void
3371did_set_ignorecase(void)
3372{
3373 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3374 if (p_hls)
3375 redraw_all_later(UPD_SOME_VALID);
3376}
3377
3378#ifdef FEAT_SEARCH_EXTRA
3379/*
3380 * Process the updated 'hlsearch' option value.
3381 */
3382 static void
3383did_set_hlsearch(void)
3384{
3385 // when 'hlsearch' is set or reset: reset no_hlsearch
3386 set_no_hlsearch(FALSE);
3387}
3388#endif
3389
3390/*
3391 * Process the updated 'scrollbind' option value.
3392 */
3393 static void
3394did_set_scrollbind(void)
3395{
3396 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3397 // at the end of normal_cmd()
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003398 if (!curwin->w_p_scb)
3399 return;
3400 do_check_scrollbind(FALSE);
3401 curwin->w_scbind_pos = curwin->w_topline;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003402}
3403
3404#ifdef FEAT_QUICKFIX
3405/*
3406 * Process the updated 'previewwindow' option value.
3407 */
3408 static char *
3409did_set_previewwindow(int *doskip)
3410{
3411 if (!curwin->w_p_pvw)
3412 return NULL;
3413
3414 // There can be only one window with 'previewwindow' set.
3415 win_T *win;
3416
3417 FOR_ALL_WINDOWS(win)
3418 if (win->w_p_pvw && win != curwin)
3419 {
3420 curwin->w_p_pvw = FALSE;
3421 *doskip = TRUE;
3422 return e_preview_window_already_exists;
3423 }
3424
3425 return NULL;
3426}
3427#endif
3428
3429/*
3430 * Process the updated 'smoothscroll' option value.
3431 */
3432 static void
3433did_set_smoothscroll(void)
3434{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003435 if (curwin->w_p_sms)
3436 return;
3437 curwin->w_skipcol = 0;
3438 changed_line_abv_curs();
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003439}
3440
3441/*
3442 * Process the updated 'textmode' option value.
3443 */
3444 static void
3445did_set_textmode(int opt_flags)
3446{
3447 // when 'textmode' is set or reset also change 'fileformat'
3448 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3449}
3450
3451/*
3452 * Process the updated 'textauto' option value.
3453 */
3454 static void
3455did_set_textauto(int opt_flags)
3456{
3457 // when 'textauto' is set or reset also change 'fileformats'
3458 set_string_option_direct((char_u *)"ffs", -1,
3459 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
3460 OPT_FREE | opt_flags, 0);
3461}
3462
3463/*
3464 * Process the updated 'lisp' option value.
3465 */
3466 static void
3467did_set_lisp(void)
3468{
3469 // When 'lisp' option changes include/exclude '-' in keyword characters.
3470 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3471}
3472
3473/*
3474 * Process the updated 'title' or the 'icon' option value.
3475 */
3476 static void
3477did_set_title_icon(void)
3478{
3479 // when 'title' changed, may need to change the title; same for 'icon'
3480 did_set_title();
3481}
3482
3483/*
3484 * Process the updated 'modified' option value.
3485 */
3486 static void
3487did_set_modified(long value)
3488{
3489 if (!value)
3490 save_file_ff(curbuf); // Buffer is unchanged
3491 redraw_titles();
3492 modified_was_set = value;
3493}
3494
3495#ifdef BACKSLASH_IN_FILENAME
3496/*
3497 * Process the updated 'shellslash' option value.
3498 */
3499 static void
3500did_set_shellslash(void)
3501{
3502 if (p_ssl)
3503 {
3504 psepc = '/';
3505 psepcN = '\\';
3506 pseps[0] = '/';
3507 }
3508 else
3509 {
3510 psepc = '\\';
3511 psepcN = '/';
3512 pseps[0] = '\\';
3513 }
3514
3515 // need to adjust the file name arguments and buffer names.
3516 buflist_slash_adjust();
3517 alist_slash_adjust();
3518# ifdef FEAT_EVAL
3519 scriptnames_slash_adjust();
3520# endif
3521}
3522#endif
3523
3524/*
3525 * Process the updated 'wrap' option value.
3526 */
3527 static void
3528did_set_wrap(void)
3529{
3530 // If 'wrap' is set, set w_leftcol to zero.
3531 if (curwin->w_p_wrap)
3532 curwin->w_leftcol = 0;
3533}
3534
3535/*
3536 * Process the updated 'equalalways' option value.
3537 */
3538 static void
3539did_set_equalalways(long old_value)
3540{
3541 if (p_ea && !old_value)
3542 win_equal(curwin, FALSE, 0);
3543}
3544
3545/*
3546 * Process the updated 'weirdinvert' option value.
3547 */
3548 static void
3549did_set_weirdinvert(long old_value)
3550{
3551 // When 'weirdinvert' changed, set/reset 't_xs'.
3552 // Then set 'weirdinvert' according to value of 't_xs'.
3553 if (p_wiv && !old_value)
3554 T_XS = (char_u *)"y";
3555 else if (!p_wiv && old_value)
3556 T_XS = empty_option;
3557 p_wiv = (*T_XS != NUL);
3558}
3559
3560#ifdef FEAT_BEVAL_GUI
3561/*
3562 * Process the updated 'ballooneval' option value.
3563 */
3564 static void
3565did_set_ballooneval(long old_value)
3566{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003567 if (balloonEvalForTerm)
3568 return;
3569 if (p_beval && !old_value)
3570 gui_mch_enable_beval_area(balloonEval);
3571 else if (!p_beval && old_value)
3572 gui_mch_disable_beval_area(balloonEval);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003573}
3574#endif
3575
3576#ifdef FEAT_BEVAL_TERM
3577/*
3578 * Process the updated 'balloonevalterm' option value.
3579 */
3580 static void
3581did_set_balloonevalterm(void)
3582{
3583 mch_bevalterm_changed();
3584}
3585#endif
3586
3587#ifdef FEAT_AUTOCHDIR
3588/*
3589 * Process the updated 'autochdir' option value.
3590 */
3591 static void
3592did_set_autochdir(void)
3593{
3594 // Change directories when the 'acd' option is set now.
3595 DO_AUTOCHDIR;
3596}
3597#endif
3598
3599#ifdef FEAT_DIFF
3600/*
3601 * Process the updated 'diff' option value.
3602 */
3603 static void
3604did_set_diff(void)
3605{
3606 // May add or remove the buffer from the list of diff buffers.
3607 diff_buf_adjust(curwin);
3608# ifdef FEAT_FOLDING
3609 if (foldmethodIsDiff(curwin))
3610 foldUpdateAll(curwin);
3611# endif
3612}
3613#endif
3614
3615#ifdef HAVE_INPUT_METHOD
3616/*
3617 * Process the updated 'imdisable' option value.
3618 */
3619 static void
3620did_set_imdisable(void)
3621{
3622 // Only de-activate it here, it will be enabled when changing mode.
3623 if (p_imdisable)
3624 im_set_active(FALSE);
3625 else if (State & MODE_INSERT)
3626 // When the option is set from an autocommand, it may need to take
3627 // effect right away.
3628 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3629}
3630#endif
3631
3632#ifdef FEAT_SPELL
3633/*
3634 * Process the updated 'spell' option value.
3635 */
3636 static char *
3637did_set_spell(void)
3638{
3639 if (curwin->w_p_spell)
3640 return did_set_spelllang(curwin);
3641
3642 return NULL;
3643}
3644#endif
3645
3646#ifdef FEAT_ARABIC
3647/*
3648 * Process the updated 'arabic' option value.
3649 */
3650 static char *
3651did_set_arabic(void)
3652{
3653 char *errmsg = NULL;
3654
3655 if (curwin->w_p_arab)
3656 {
3657 /*
3658 * 'arabic' is set, handle various sub-settings.
3659 */
3660 if (!p_tbidi)
3661 {
3662 // set rightleft mode
3663 if (!curwin->w_p_rl)
3664 {
3665 curwin->w_p_rl = TRUE;
3666 changed_window_setting();
3667 }
3668
3669 // Enable Arabic shaping (major part of what Arabic requires)
3670 if (!p_arshape)
3671 {
3672 p_arshape = TRUE;
3673 redraw_later_clear();
3674 }
3675 }
3676
3677 // Arabic requires a utf-8 encoding, inform the user if it's not
3678 // set.
3679 if (STRCMP(p_enc, "utf-8") != 0)
3680 {
3681 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3682
3683 msg_source(HL_ATTR(HLF_W));
3684 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3685#ifdef FEAT_EVAL
3686 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3687#endif
3688 }
3689
3690 // set 'delcombine'
3691 p_deco = TRUE;
3692
3693# ifdef FEAT_KEYMAP
3694 // Force-set the necessary keymap for arabic
3695 errmsg = set_option_value((char_u *)"keymap",
3696 0L, (char_u *)"arabic", OPT_LOCAL);
3697# endif
3698 }
3699 else
3700 {
3701 /*
3702 * 'arabic' is reset, handle various sub-settings.
3703 */
3704 if (!p_tbidi)
3705 {
3706 // reset rightleft mode
3707 if (curwin->w_p_rl)
3708 {
3709 curwin->w_p_rl = FALSE;
3710 changed_window_setting();
3711 }
3712
3713 // 'arabicshape' isn't reset, it is a global option and
3714 // another window may still need it "on".
3715 }
3716
3717 // 'delcombine' isn't reset, it is a global option and another
3718 // window may still want it "on".
3719
3720# ifdef FEAT_KEYMAP
3721 // Revert to the default keymap
3722 curbuf->b_p_iminsert = B_IMODE_NONE;
3723 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3724# endif
3725 }
3726
3727 return errmsg;
3728}
3729#endif
3730
3731#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3732/*
3733 * Process the updated 'number' or 'relativenumber' option value.
3734 */
3735 static void
3736did_set_number_relativenumber(char_u *varp)
3737{
3738 if (gui.in_use
3739 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3740 && curbuf->b_signlist != NULL)
3741 {
3742 // If the 'number' or 'relativenumber' options are modified and
3743 // 'signcolumn' is set to 'number', then clear the screen for a full
3744 // refresh. Otherwise the sign icons are not displayed properly in the
3745 // number column. If the 'number' option is set and only the
3746 // 'relativenumber' option is toggled, then don't refresh the screen
3747 // (optimization).
3748 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3749 redraw_all_later(UPD_CLEAR);
3750 }
3751}
3752#endif
3753
3754#ifdef FEAT_TERMGUICOLORS
3755 static char *
3756did_set_termguicolors(int *doskip UNUSED)
3757{
3758# ifdef FEAT_VTP
3759 // Do not turn on 'tgc' when 24-bit colors are not supported.
3760 if (
3761# ifdef VIMDLL
3762 !gui.in_use && !gui.starting &&
3763# endif
3764 !has_vtp_working())
3765 {
3766 p_tgc = 0;
3767 *doskip = TRUE;
3768 return e_24_bit_colors_are_not_supported_on_this_environment;
3769 }
3770 if (is_term_win32())
3771 swap_tcap();
3772# endif
3773# ifdef FEAT_GUI
3774 if (!gui.in_use && !gui.starting)
3775# endif
3776 highlight_gui_started();
3777# ifdef FEAT_VTP
3778 // reset t_Co
3779 if (is_term_win32())
3780 {
3781 control_console_color_rgb();
3782 set_termname(T_NAME);
3783 init_highlight(TRUE, FALSE);
3784 }
3785# endif
3786# ifdef FEAT_TERMINAL
3787 term_update_colors_all();
3788 term_update_palette_all();
3789 term_update_wincolor_all();
3790# endif
3791
3792 return NULL;
3793}
3794#endif
3795
3796/*
3797 * When some boolean options are changed, need to take some action.
3798 */
3799 static char *
3800did_set_bool_option(
3801 char_u *varp,
3802 int opt_flags,
3803 long value,
3804 long old_value,
3805 int *doskip)
3806{
3807 char *errmsg = NULL;
3808
3809 if ((int *)varp == &p_cp) // 'compatible'
3810 did_set_compatible();
3811#ifdef FEAT_LANGMAP
3812 else if ((int *)varp == &p_lrm) // 'langremap'
3813 did_set_langremap();
3814 else if ((int *)varp == &p_lnr) // 'langnoremap'
3815 did_set_langnoremap();
3816#endif
3817#ifdef FEAT_PERSISTENT_UNDO
3818 else if ((int *)varp == &curbuf->b_p_udf // buffer local 'undofile'
3819 || (int *)varp == &p_udf) // 'undofile'
3820 did_set_undofile(opt_flags);
3821#endif
3822 else if ((int *)varp == &curbuf->b_p_ro) // 'readonly'
3823 did_set_readonly(opt_flags);
3824#ifdef FEAT_GUI
3825 else if ((int *)varp == &p_mh) // 'mousehide'
3826 did_set_mousehide();
3827#endif
3828 else if ((int *)varp == &curbuf->b_p_ma)
3829 errmsg = did_set_modifiable(doskip); // 'modifiable'
3830 else if ((int *)varp == &curbuf->b_p_eof // 'endoffile'
3831 || (int *)varp == &curbuf->b_p_eol // 'endofline'
3832 || (int *)varp == &curbuf->b_p_fixeol // 'fixendofline'
3833 || (int *)varp == &curbuf->b_p_bomb) // 'bomb'
3834 did_set_eof_eol_fixeol_bomb();
3835 else if ((int *)varp == &curbuf->b_p_bin) // 'binary'
3836 did_set_binary(opt_flags, old_value);
3837 else if ((int *)varp == &curbuf->b_p_bl) // 'buflisted'
3838 did_set_buflisted(old_value);
3839 else if ((int *)varp == &curbuf->b_p_swf) // 'swapfile'
3840 did_set_swapfile();
3841 else if ((int *)varp == &p_terse) // 'terse'
3842 did_set_terse();
3843 else if ((int *)varp == &p_paste) // 'paste'
3844 did_set_paste();
3845 else if ((int *)varp == &p_im) // 'insertmode'
3846 did_set_insertmode(old_value);
3847 else if ((int *)varp == &p_ic) // 'ignorecase'
3848 did_set_ignorecase();
3849#ifdef FEAT_SEARCH_EXTRA
3850 else if ((int *)varp == &p_hls) // 'hlsearch'
3851 did_set_hlsearch();
3852#endif
3853 else if ((int *)varp == &curwin->w_p_scb) // 'scrollbind'
3854 did_set_scrollbind();
3855#ifdef FEAT_QUICKFIX
3856 else if ((int *)varp == &curwin->w_p_pvw) // 'previewwindow'
3857 errmsg = did_set_previewwindow(doskip);
3858#endif
3859 else if ((int *)varp == &curwin->w_p_sms) // 'smoothscroll'
3860 did_set_smoothscroll();
3861 else if ((int *)varp == &curbuf->b_p_tx) // 'textmode'
3862 did_set_textmode(opt_flags);
3863 else if ((int *)varp == &p_ta) // 'textauto'
3864 did_set_textauto(opt_flags);
3865 else if (varp == (char_u *)&(curbuf->b_p_lisp)) // 'lisp'
3866 did_set_lisp();
3867 else if ( (int *)varp == &p_title // 'title'
3868 || (int *)varp == &p_icon) // 'icon'
3869 did_set_title_icon();
3870 else if ((int *)varp == &curbuf->b_changed) // 'modified'
3871 did_set_modified(value);
3872#ifdef BACKSLASH_IN_FILENAME
3873 else if ((int *)varp == &p_ssl) // 'shellslash'
3874 did_set_shellslash();
3875#endif
3876 else if ((int *)varp == &curwin->w_p_wrap) // 'wrap'
3877 did_set_wrap();
3878 else if ((int *)varp == &p_ea) // 'equalalways'
3879 did_set_equalalways(old_value);
3880 else if ((int *)varp == &p_wiv) // weirdinvert'
3881 did_set_weirdinvert(old_value);
3882#ifdef FEAT_BEVAL_GUI
3883 else if ((int *)varp == &p_beval) // 'ballooneval'
3884 did_set_ballooneval(old_value);
3885#endif
3886#ifdef FEAT_BEVAL_TERM
3887 else if ((int *)varp == &p_bevalterm) // 'balloonevalterm'
3888 did_set_balloonevalterm();
3889#endif
3890#ifdef FEAT_AUTOCHDIR
3891 else if ((int *)varp == &p_acd) // 'autochdir'
3892 did_set_autochdir();
3893#endif
3894#ifdef FEAT_DIFF
3895 else if ((int *)varp == &curwin->w_p_diff) // 'diff'
3896 did_set_diff();
3897#endif
3898#ifdef HAVE_INPUT_METHOD
3899 else if ((int *)varp == &p_imdisable) // 'imdisable'
3900 did_set_imdisable();
3901#endif
3902#ifdef FEAT_SPELL
3903 else if ((int *)varp == &curwin->w_p_spell) // 'spell'
3904 errmsg = did_set_spell();
3905#endif
3906#ifdef FEAT_ARABIC
3907 else if ((int *)varp == &curwin->w_p_arab) // 'arabic'
3908 errmsg = did_set_arabic();
3909#endif
3910#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3911 else if ( (int *)varp == &curwin->w_p_nu // 'number'
3912 || (int *)varp == &curwin->w_p_rnu) // 'relativenumber'
3913 did_set_number_relativenumber(varp);
3914#endif
3915#ifdef FEAT_TERMGUICOLORS
3916 else if ((int *)varp == &p_tgc) // 'termguicolors'
3917 errmsg = did_set_termguicolors(doskip);
3918#endif
3919
3920 return errmsg;
3921}
3922
3923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 * Set the value of a boolean option, and take care of side effects.
3925 * Returns NULL for success, or an error message for an error.
3926 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003927 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003928set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003929 int opt_idx, // index in options[] table
3930 char_u *varp, // pointer to the option variable
3931 int value, // new value
3932 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003935#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003936 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003937#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003938 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003940 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 if ((secure
3942#ifdef HAVE_SANDBOX
3943 || sandbox != 0
3944#endif
3945 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003946 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003948#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003949 // Save the global value before changing anything. This is needed as for
3950 // a global-only option setting the "local value" in fact sets the global
3951 // value (since there is only one value).
3952 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3953 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
3954 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003955#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003957 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003959 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003960 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961#endif
3962
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003963#ifdef FEAT_GUI
3964 need_mouse_correct = TRUE;
3965#endif
3966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003967 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3969 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
3970
3971 /*
3972 * Handle side effects of changing a bool option.
3973 */
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003974 int doskip = FALSE;
3975 errmsg = did_set_bool_option(varp, opt_flags, value, old_value, &doskip);
3976 if (doskip)
3977 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003979 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 options[opt_idx].flags |= P_WAS_SET;
3982
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003983#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003984 apply_optionset_autocmd(opt_idx, opt_flags,
3985 (long)(old_value ? TRUE : FALSE),
3986 (long)(old_global_value ? TRUE : FALSE),
3987 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003988#endif
3989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003990 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003991 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003992 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003993 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003994
3995 if ((opt_flags & OPT_NO_REDRAW) == 0)
3996 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003998 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999}
4000
4001/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004002 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004004 static char *
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004005did_set_winheight_helpheight(long *pp, char *errmsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004007 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004009 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004010 p_wh = 1;
4011 }
4012 if (p_wmh > p_wh)
4013 {
4014 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4015 p_wh = p_wmh;
4016 }
4017 if (p_hh < 0)
4018 {
4019 errmsg = e_argument_must_be_positive;
4020 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004023 // Change window height NOW
4024 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004026 if (pp == &p_wh && curwin->w_height < p_wh)
4027 win_setheight((int)p_wh);
4028 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4029 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004032 return errmsg;
4033}
4034
4035/*
4036 * Process the new 'winminheight' option value.
4037 */
4038 static char *
4039did_set_winminheight(char *errmsg)
4040{
4041 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004043 errmsg = e_argument_must_be_positive;
4044 p_wmh = 0;
4045 }
4046 if (p_wmh > p_wh)
4047 {
4048 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4049 p_wmh = p_wh;
4050 }
4051 win_setminheight();
4052
4053 return errmsg;
4054}
4055
4056/*
4057 * Process the new 'winwidth' option value.
4058 */
4059 static char *
4060did_set_winwidth(char *errmsg)
4061{
4062 if (p_wiw < 1)
4063 {
4064 errmsg = e_argument_must_be_positive;
4065 p_wiw = 1;
4066 }
4067 if (p_wmw > p_wiw)
4068 {
4069 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4070 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004072
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004073 // Change window width NOW
4074 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4075 win_setwidth((int)p_wiw);
4076
4077 return errmsg;
4078}
4079
4080/*
4081 * Process the new 'winminwidth' option value.
4082 */
4083 static char *
4084did_set_winminwidth(char *errmsg)
4085{
4086 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004087 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004088 errmsg = e_argument_must_be_positive;
4089 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004090 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004091 if (p_wmw > p_wiw)
4092 {
4093 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4094 p_wmw = p_wiw;
4095 }
4096 win_setminwidth();
4097
4098 return errmsg;
4099}
4100
4101/*
4102 * Process the new 'laststatus' option value.
4103 */
4104 static void
4105did_set_laststatus(void)
4106{
4107 last_status(FALSE); // (re)set last window status line
4108}
4109
4110/*
4111 * Process the new 'showtabline' option value.
4112 */
4113 static void
4114did_set_showtabline(void)
4115{
4116 shell_new_rows(); // recompute window positions and heights
4117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004120/*
4121 * Process the new 'linespace' option value.
4122 */
4123 static void
4124did_set_linespace(void)
4125{
4126 // Recompute gui.char_height and resize the Vim window to keep the
4127 // same number of lines.
4128 if (gui.in_use && gui_mch_adjust_charheight() == OK)
4129 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
4130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#endif
4132
4133#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004134/*
4135 * Process the new 'foldlevel' option value.
4136 */
4137 static void
4138did_set_foldlevel(void)
4139{
4140 if (curwin->w_p_fdl < 0)
4141 curwin->w_p_fdl = 0;
4142 newFoldLevel();
4143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004145/*
4146 * Process the new 'foldminlines' option value.
4147 */
4148 static void
4149did_set_foldminlines(void)
4150{
4151 foldUpdateAll(curwin);
4152}
4153
4154/*
4155 * Process the new 'foldnestmax' option value.
4156 */
4157 static void
4158did_set_foldnestmax(void)
4159{
4160 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 foldUpdateAll(curwin);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004162}
4163
4164/*
4165 * Process the new 'foldcolumn' option value.
4166 */
4167 static char *
4168did_set_foldcolumn(char *errmsg)
4169{
4170 if (curwin->w_p_fdc < 0)
4171 {
4172 errmsg = e_argument_must_be_positive;
4173 curwin->w_p_fdc = 0;
4174 }
4175 else if (curwin->w_p_fdc > 12)
4176 {
4177 errmsg = e_invalid_argument;
4178 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004181 return errmsg;
4182}
4183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004185/*
4186 * Process the new 'shiftwidth' or the 'tabstop' option value.
4187 */
4188 static void
4189did_set_shiftwidth_tabstop(long *pp)
4190{
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004191#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004192 if (foldmethodIsIndent(curwin))
4193 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004194#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004195 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4196 // parse 'cinoptions'.
4197 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4198 parse_cino(curbuf);
4199}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004201/*
4202 * Process the new 'maxcombine' option value.
4203 */
4204 static void
4205did_set_maxcombine(void)
4206{
4207 if (p_mco > MAX_MCO)
4208 p_mco = MAX_MCO;
4209 else if (p_mco < 0)
4210 p_mco = 0;
4211 screenclear(); // will re-allocate the screen
4212}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004213
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004214/*
4215 * Process the new 'iminsert' option value.
4216 */
4217 static char *
4218did_set_iminsert(char *errmsg)
4219{
4220 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004222 errmsg = e_invalid_argument;
4223 curbuf->b_p_iminsert = B_IMODE_NONE;
4224 }
4225 p_iminsert = curbuf->b_p_iminsert;
4226 if (termcap_active) // don't do this in the alternate screen
4227 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004228#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004229 // Show/unshow value of 'keymap' in status lines.
4230 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004232
4233 return errmsg;
4234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004236#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004237/*
4238 * Process the new 'imstyle' option value.
4239 */
4240 static char *
4241did_set_imstyle(char *errmsg)
4242{
4243 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4244 errmsg = e_invalid_argument;
4245
4246 return errmsg;
4247}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004248#endif
4249
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004250/*
4251 * Process the new 'window' option value.
4252 */
4253 static void
4254did_set_window(void)
4255{
4256 if (p_window < 1)
4257 p_window = 1;
4258 else if (p_window >= Rows)
4259 p_window = Rows - 1;
4260}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004261
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004262/*
4263 * Process the new 'imsearch' option value.
4264 */
4265 static char *
4266did_set_imsearch(char *errmsg)
4267{
4268 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004270 errmsg = e_invalid_argument;
4271 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004273 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004275 return errmsg;
4276}
4277
4278/*
4279 * Process the new 'titlelen' option value.
4280 */
4281 static char *
4282did_set_titlelen(long old_value, char *errmsg)
4283{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004284 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004285 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004287 errmsg = e_argument_must_be_positive;
4288 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004290 if (starting != NO_SCREEN && old_value != p_titlelen)
4291 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004293 return errmsg;
4294}
4295
4296/*
4297 * Process the new 'cmdheight' option value.
4298 */
4299 static char *
4300did_set_cmdheight(long old_value, char *errmsg)
4301{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004302 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004303 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004305 errmsg = e_argument_must_be_positive;
4306 p_ch = 1;
4307 }
4308 if (p_ch > Rows - min_rows() + 1)
4309 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004311 // Only compute the new window layout when startup has been
4312 // completed. Otherwise the frame sizes may be wrong.
4313 if ((p_ch != old_value
4314 || tabline_height() + topframe->fr_height != Rows - p_ch)
4315 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004317 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004319 )
4320 command_height();
4321
4322 return errmsg;
4323}
4324
4325/*
4326 * Process the new 'updatecount' option value.
4327 */
4328 static char *
4329did_set_updatecount(long old_value, char *errmsg)
4330{
4331 // when 'updatecount' changes from zero to non-zero, open swap files
4332 if (p_uc < 0)
4333 {
4334 errmsg = e_argument_must_be_positive;
4335 p_uc = 100;
4336 }
4337 if (p_uc && !old_value)
4338 ml_open_files();
4339
4340 return errmsg;
4341}
4342
4343#ifdef FEAT_CONCEAL
4344/*
4345 * Process the new 'conceallevel' option value.
4346 */
4347 static char *
4348did_set_conceallevel(char *errmsg)
4349{
4350 if (curwin->w_p_cole < 0)
4351 {
4352 errmsg = e_argument_must_be_positive;
4353 curwin->w_p_cole = 0;
4354 }
4355 else if (curwin->w_p_cole > 3)
4356 {
4357 errmsg = e_invalid_argument;
4358 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004361 return errmsg;
4362}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004365#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004366/*
4367 * Process the new 'pyxversion' option value.
4368 */
4369 static char *
4370did_set_pyxversion(char *errmsg)
4371{
4372 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4373 errmsg = e_invalid_argument;
4374
4375 return errmsg;
4376}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004377#endif
4378
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004379/*
4380 * Process the new global 'undolevels' option value.
4381 */
4382 static void
4383did_set_global_undolevels(long value, long old_value)
4384{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004385 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004386
4387 // use the old value, otherwise u_sync() may not work properly
4388 p_ul = old_value;
4389 u_sync(TRUE);
4390 p_ul = value;
4391}
4392
4393/*
4394 * Process the new buffer local 'undolevels' option value.
4395 */
4396 static void
4397did_set_buflocal_undolevels(long value, long old_value)
4398{
4399 // use the old value, otherwise u_sync() may not work properly
4400 curbuf->b_p_ul = old_value;
4401 u_sync(TRUE);
4402 curbuf->b_p_ul = value;
4403}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004405#ifdef FEAT_LINEBREAK
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004406/*
4407 * Process the new 'numberwidth' option value.
4408 */
4409 static char *
4410did_set_numberwidth(char *errmsg)
4411{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004412 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004413 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004414 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004415 errmsg = e_argument_must_be_positive;
4416 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004417 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004418 if (curwin->w_p_nuw > 20)
4419 {
4420 errmsg = e_invalid_argument;
4421 curwin->w_p_nuw = 20;
4422 }
4423 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4424
4425 return errmsg;
4426}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004427#endif
4428
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004429/*
4430 * Process the new 'textwidth' option value.
4431 */
4432 static char *
4433did_set_textwidth(char *errmsg)
4434{
4435 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004436 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004437 errmsg = e_argument_must_be_positive;
4438 curbuf->b_p_tw = 0;
4439 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004440#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004441 {
4442 win_T *wp;
4443 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004444
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004445 FOR_ALL_TAB_WINDOWS(tp, wp)
4446 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004447 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004448#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004449
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004450 return errmsg;
4451}
4452
4453/*
4454 * When some number options are changed, need to take some action.
4455 */
4456 static char *
4457did_set_num_option(long *pp, long value, long old_value, char *errmsg)
4458{
4459 if (pp == &p_wh // 'winheight'
4460 || pp == &p_hh) // 'helpheight'
4461 errmsg = did_set_winheight_helpheight(pp, errmsg);
4462 else if (pp == &p_wmh) // 'winminheight'
4463 errmsg = did_set_winminheight(errmsg);
4464 else if (pp == &p_wiw) // 'winwidth'
4465 errmsg = did_set_winwidth(errmsg);
4466 else if (pp == &p_wmw) // 'winminwidth'
4467 errmsg = did_set_winminwidth(errmsg);
4468 else if (pp == &p_ls)
4469 did_set_laststatus(); // 'laststatus'
4470 else if (pp == &p_stal)
4471 did_set_showtabline(); // 'showtabline'
4472#ifdef FEAT_GUI
4473 else if (pp == &p_linespace) // 'linespace'
4474 did_set_linespace();
4475#endif
4476#ifdef FEAT_FOLDING
4477 else if (pp == &curwin->w_p_fdl) // 'foldlevel'
4478 did_set_foldlevel();
4479 else if (pp == &curwin->w_p_fml) // 'foldminlines'
4480 did_set_foldminlines();
4481 else if (pp == &curwin->w_p_fdn) // 'foldnestmax'
4482 did_set_foldnestmax();
4483 else if (pp == &curwin->w_p_fdc) // 'foldcolumn'
4484 errmsg = did_set_foldcolumn(errmsg);
4485#endif // FEAT_FOLDING
4486 else if ( pp == &curbuf->b_p_sw // 'shiftwidth'
4487 || pp == &curbuf->b_p_ts) // 'tabstop'
4488 did_set_shiftwidth_tabstop(pp);
4489 else if (pp == &p_mco) // 'maxcombine'
4490 did_set_maxcombine();
4491 else if (pp == &curbuf->b_p_iminsert) // 'iminsert'
4492 errmsg = did_set_iminsert(errmsg);
4493#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4494 else if (pp == &p_imst) // 'imstyle'
4495 errmsg = did_set_imstyle(errmsg);
4496#endif
4497 else if (pp == &p_window) // 'window'
4498 did_set_window();
4499 else if (pp == &curbuf->b_p_imsearch) // 'imsearch'
4500 errmsg = did_set_imsearch(errmsg);
4501 else if (pp == &p_titlelen) // 'titlelen'
4502 errmsg = did_set_titlelen(old_value, errmsg);
4503 else if (pp == &p_ch) // 'cmdheight'
4504 errmsg = did_set_cmdheight(old_value, errmsg);
4505 else if (pp == &p_uc) // 'updatecount'
4506 errmsg = did_set_updatecount(old_value, errmsg);
4507#ifdef FEAT_CONCEAL
4508 else if (pp == &curwin->w_p_cole) // 'conceallevel'
4509 errmsg = did_set_conceallevel(errmsg);
4510#endif
4511#ifdef MZSCHEME_GUI_THREADS
4512 else if (pp == &p_mzq) // 'mzquantum'
4513 mzvim_reset_timer();
4514#endif
4515#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
4516 else if (pp == &p_pyx) // 'pyxversion'
4517 errmsg = did_set_pyxversion(errmsg);
4518#endif
4519 else if (pp == &p_ul) // global 'undolevels'
4520 did_set_global_undolevels(value, old_value);
4521 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4522 did_set_buflocal_undolevels(value, old_value);
4523#ifdef FEAT_LINEBREAK
4524 else if (pp == &curwin->w_p_nuw) // 'numberwidth'
4525 errmsg = did_set_numberwidth(errmsg);
4526#endif
4527 else if (pp == &curbuf->b_p_tw) // 'textwidth'
4528 errmsg = did_set_textwidth(errmsg);
4529
4530 return errmsg;
4531}
4532
4533/*
4534 * Check the bounds of numeric options.
4535 */
4536 static char *
4537check_num_option_bounds(
4538 long *pp,
4539 long old_value,
4540 long old_Rows,
4541 long old_Columns,
4542 char *errbuf,
4543 size_t errbuflen,
4544 char *errmsg)
4545{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (Rows < min_rows() && full_screen)
4547 {
4548 if (errbuf != NULL)
4549 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004550 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004551 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 errmsg = errbuf;
4553 }
4554 Rows = min_rows();
4555 }
4556 if (Columns < MIN_COLUMNS && full_screen)
4557 {
4558 if (errbuf != NULL)
4559 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004560 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004561 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 errmsg = errbuf;
4563 }
4564 Columns = MIN_COLUMNS;
4565 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004566 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 /*
4569 * If the screen (shell) height has been changed, assume it is the
4570 * physical screenheight.
4571 */
4572 if (old_Rows != Rows || old_Columns != Columns)
4573 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004574 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (updating_screen)
4576 *pp = old_value;
4577 else if (full_screen
4578#ifdef FEAT_GUI
4579 && !gui.starting
4580#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004581 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 set_shellsize((int)Columns, (int)Rows, TRUE);
4583 else
4584 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004585 // Postpone the resizing; check the size and cmdline position for
4586 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 check_shellsize();
4588 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4589 cmdline_row = Rows - p_ch;
4590 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004591 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004592 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 if (curbuf->b_p_ts <= 0)
4596 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004597 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 curbuf->b_p_ts = 8;
4599 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004600 else if (curbuf->b_p_ts > TABSTOP_MAX)
4601 {
4602 errmsg = e_invalid_argument;
4603 curbuf->b_p_ts = 8;
4604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (p_tm < 0)
4606 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004607 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 p_tm = 0;
4609 }
4610 if ((curwin->w_p_scr <= 0
4611 || (curwin->w_p_scr > curwin->w_height
4612 && curwin->w_height > 0))
4613 && full_screen)
4614 {
4615 if (pp == &(curwin->w_p_scr))
4616 {
4617 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004618 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 win_comp_scroll(curwin);
4620 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004621 // If 'scroll' became invalid because of a side effect silently adjust
4622 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 else if (curwin->w_p_scr <= 0)
4624 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004625 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 curwin->w_p_scr = curwin->w_height;
4627 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004628 if (p_hi < 0)
4629 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004630 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004631 p_hi = 0;
4632 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004633 else if (p_hi > 10000)
4634 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004635 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004636 p_hi = 10000;
4637 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638 if (p_re < 0 || p_re > 2)
4639 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004640 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004641 p_re = 0;
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (p_report < 0)
4644 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004645 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 p_report = 1;
4647 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004648 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004650 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 p_sj = Rows / 2;
4652 else
4653 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004654 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 p_sj = 1;
4656 }
4657 }
4658 if (p_so < 0 && full_screen)
4659 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004660 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 p_so = 0;
4662 }
4663 if (p_siso < 0 && full_screen)
4664 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004665 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 p_siso = 0;
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (p_cwh < 1)
4669 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004670 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 p_cwh = 1;
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (p_ut < 0)
4674 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004675 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 p_ut = 2000;
4677 }
4678 if (p_ss < 0)
4679 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004680 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 p_ss = 0;
4682 }
4683
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004684 return errmsg;
4685}
4686
4687/*
4688 * Set the value of a number option, and take care of side effects.
4689 * Returns NULL for success, or an error message for an error.
4690 */
4691 static char *
4692set_num_option(
4693 int opt_idx, // index in options[] table
4694 char_u *varp, // pointer to the option variable
4695 long value, // new value
4696 char *errbuf, // buffer for error messages
4697 size_t errbuflen, // length of "errbuf"
4698 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4699 // OPT_MODELINE, etc.
4700{
4701 char *errmsg = NULL;
4702 long old_value = *(long *)varp;
4703#if defined(FEAT_EVAL)
4704 long old_global_value = 0; // only used when setting a local and
4705 // global option
4706#endif
4707 long old_Rows = Rows; // remember old Rows
4708 long old_Columns = Columns; // remember old Columns
4709 long *pp = (long *)varp;
4710
4711 // Disallow changing some options from secure mode.
4712 if ((secure
4713#ifdef HAVE_SANDBOX
4714 || sandbox != 0
4715#endif
4716 ) && (options[opt_idx].flags & P_SECURE))
4717 return e_not_allowed_here;
4718
4719#if defined(FEAT_EVAL)
4720 // Save the global value before changing anything. This is needed as for
4721 // a global-only option setting the "local value" in fact sets the global
4722 // value (since there is only one value).
4723 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4724 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4725 OPT_GLOBAL);
4726#endif
4727
4728 *pp = value;
4729#ifdef FEAT_EVAL
4730 // Remember where the option was set.
4731 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4732#endif
4733#ifdef FEAT_GUI
4734 need_mouse_correct = TRUE;
4735#endif
4736
4737 if (curbuf->b_p_sw < 0)
4738 {
4739 errmsg = e_argument_must_be_positive;
4740#ifdef FEAT_VARTABS
4741 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4742 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4743 ? tabstop_first(curbuf->b_p_vts_array)
4744 : curbuf->b_p_ts;
4745#else
4746 curbuf->b_p_sw = curbuf->b_p_ts;
4747#endif
4748 }
4749
4750 /*
4751 * Number options that need some action when changed
4752 */
4753 errmsg = did_set_num_option(pp, value, old_value, errmsg);
4754
4755 /*
4756 * Check the bounds for numeric options here
4757 */
4758 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4759 errbuf, errbuflen, errmsg);
4760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4763 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4764
4765 options[opt_idx].flags |= P_WAS_SET;
4766
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004767#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004768 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4769 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004770#endif
4771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004772 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004773 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004774 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004775 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004776 if ((opt_flags & OPT_NO_REDRAW) == 0)
4777 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 return errmsg;
4780}
4781
4782/*
4783 * Called after an option changed: check if something needs to be redrawn.
4784 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004786check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004789 int doclear = (flags & P_RCLR) == P_RCLR;
4790 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004792 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
4795 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4796 changed_window_setting();
4797 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004798 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004799 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004800 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004801 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004802 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004804 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805}
4806
4807/*
4808 * Find index for option 'arg'.
4809 * Return -1 if not found.
4810 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004811 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004812findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 int opt_idx;
4815 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004816 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 int is_term_opt;
4818
4819 /*
4820 * For first call: Initialize the quick-access table.
4821 * It contains the index for the first option that starts with a certain
4822 * letter. There are 26 letters, plus the first "t_" option.
4823 */
4824 if (quick_tab[1] == 0)
4825 {
4826 p = options[0].fullname;
4827 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4828 {
4829 if (s[0] != p[0])
4830 {
4831 if (s[0] == 't' && s[1] == '_')
4832 quick_tab[26] = opt_idx;
4833 else
4834 quick_tab[CharOrdLow(s[0])] = opt_idx;
4835 }
4836 p = s;
4837 }
4838 }
4839
4840 /*
4841 * Check for name starting with an illegal character.
4842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 return -1;
4845
4846 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4847 if (is_term_opt)
4848 opt_idx = quick_tab[26];
4849 else
4850 opt_idx = quick_tab[CharOrdLow(arg[0])];
4851 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004853 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 break;
4855 }
4856 if (s == NULL && !is_term_opt)
4857 {
4858 opt_idx = quick_tab[CharOrdLow(arg[0])];
4859 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4860 {
4861 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004862 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 break;
4864 s = NULL;
4865 }
4866 }
4867 if (s == NULL)
4868 opt_idx = -1;
4869 return opt_idx;
4870}
4871
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004872#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4873 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874/*
4875 * Get the value for an option.
4876 *
4877 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004878 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004879 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004880 * String option: gov_string, *stringval gets allocated string.
4881 * Hidden Number option: gov_hidden_number.
4882 * Hidden Toggle option: gov_hidden_bool.
4883 * Hidden String option: gov_hidden_string.
4884 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004885 *
4886 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004888 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004889get_option_value(
4890 char_u *name,
4891 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004892 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004893 int *flagsp,
4894 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 int opt_idx;
4897 char_u *varp;
4898
4899 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004900 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004901 {
4902 int key;
4903
4904 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004905 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004906 {
4907 char_u key_name[2];
4908 char_u *p;
4909
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004910 if (flagsp != NULL)
4911 *flagsp = 0; // terminal option has no flags
4912
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004913 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004914 if (key < 0)
4915 {
4916 key_name[0] = KEY2TERMCAP0(key);
4917 key_name[1] = KEY2TERMCAP1(key);
4918 }
4919 else
4920 {
4921 key_name[0] = KS_KEY;
4922 key_name[1] = (key & 0xff);
4923 }
4924 p = find_termcode(key_name);
4925 if (p != NULL)
4926 {
4927 if (stringval != NULL)
4928 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004929 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004930 }
4931 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004932 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004935 varp = get_varp_scope(&(options[opt_idx]), scope);
4936
4937 if (flagsp != NULL)
4938 // Return the P_xxxx option flags.
4939 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 if (options[opt_idx].flags & P_STRING)
4942 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004943 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004944 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (stringval != NULL)
4946 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004947 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004948 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4949 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004951 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004952 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004953 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004956 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 *stringval = vim_strsave(*(char_u **)(varp));
4958 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004959 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004962 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004963 return (options[opt_idx].flags & P_NUM)
4964 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (options[opt_idx].flags & P_NUM)
4966 *numval = *(long *)varp;
4967 else
4968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004969 // Special case: 'modified' is b_changed, but we also want to consider
4970 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if ((int *)varp == &curbuf->b_changed)
4972 *numval = curbufIsChanged();
4973 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004974 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004976 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977}
4978#endif
4979
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004980#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004981/*
4982 * Returns the option attributes and its value. Unlike the above function it
4983 * will return either global value or local value of the option depending on
4984 * what was requested, but it will never return global value if it was
4985 * requested to return local one and vice versa. Neither it will return
4986 * buffer-local value if it was requested to return window-local one.
4987 *
4988 * Pretends that option is absent if it is not present in the requested scope
4989 * (i.e. has no global, window-local or buffer-local value depending on
4990 * opt_type). Uses
4991 *
4992 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004993 * 0 hidden or unknown option, also option that does not have requested
4994 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004995 * see SOPT_* in vim.h for other flags
4996 *
4997 * Possible opt_type values: see SREQ_* in vim.h
4998 */
4999 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005000get_option_value_strict(
5001 char_u *name,
5002 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005004 int opt_type,
5005 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005006{
5007 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005008 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005009 struct vimoption *p;
5010 int r = 0;
5011
5012 opt_idx = findoption(name);
5013 if (opt_idx < 0)
5014 return 0;
5015
5016 p = &(options[opt_idx]);
5017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005019 if (p->var == NULL)
5020 return 0;
5021
5022 if (p->flags & P_BOOL)
5023 r |= SOPT_BOOL;
5024 else if (p->flags & P_NUM)
5025 r |= SOPT_NUM;
5026 else if (p->flags & P_STRING)
5027 r |= SOPT_STRING;
5028
5029 if (p->indir == PV_NONE)
5030 {
5031 if (opt_type == SREQ_GLOBAL)
5032 r |= SOPT_GLOBAL;
5033 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005034 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005035 }
5036 else
5037 {
5038 if (p->indir & PV_BOTH)
5039 r |= SOPT_GLOBAL;
5040 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005041 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005042
5043 if (p->indir & PV_WIN)
5044 {
5045 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005046 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005047 else
5048 r |= SOPT_WIN;
5049 }
5050 else if (p->indir & PV_BUF)
5051 {
5052 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005053 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005054 else
5055 r |= SOPT_BUF;
5056 }
5057 }
5058
5059 if (stringval == NULL)
5060 return r;
5061
5062 if (opt_type == SREQ_GLOBAL)
5063 varp = p->var;
5064 else
5065 {
5066 if (opt_type == SREQ_BUF)
5067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005068 // Special case: 'modified' is b_changed, but we also want to
5069 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005070 if (p->indir == PV_MOD)
5071 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005072 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073 varp = NULL;
5074 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005075# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005076 else if (p->indir == PV_KEY)
5077 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005078 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005079 *stringval = NULL;
5080 varp = NULL;
5081 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005082# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083 else
5084 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005085 buf_T *save_curbuf = curbuf;
5086
5087 // only getting a pointer, no need to use aucmd_prepbuf()
5088 curbuf = (buf_T *)from;
5089 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005090 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005091 curbuf = save_curbuf;
5092 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005093 }
5094 }
5095 else if (opt_type == SREQ_WIN)
5096 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005097 win_T *save_curwin = curwin;
5098
5099 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005100 curbuf = curwin->w_buffer;
5101 varp = get_varp(p);
5102 curwin = save_curwin;
5103 curbuf = curwin->w_buffer;
5104 }
5105 if (varp == p->var)
5106 return (r | SOPT_UNSET);
5107 }
5108
5109 if (varp != NULL)
5110 {
5111 if (p->flags & P_STRING)
5112 *stringval = vim_strsave(*(char_u **)(varp));
5113 else if (p->flags & P_NUM)
5114 *numval = *(long *) varp;
5115 else
5116 *numval = *(int *)varp;
5117 }
5118
5119 return r;
5120}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005121
5122/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005123 * Iterate over options. First argument is a pointer to a pointer to a
5124 * structure inside options[] array, second is option type like in the above
5125 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005126 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005127 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005128 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005129 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005130 * first argument to NULL.
5131 *
5132 * Returns full option name for current option on each call.
5133 */
5134 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005135option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005136{
5137 struct vimoption *ret = NULL;
5138 do
5139 {
5140 if (*option == NULL)
5141 *option = (void *) options;
5142 else if (((struct vimoption *) (*option))->fullname == NULL)
5143 {
5144 *option = NULL;
5145 return NULL;
5146 }
5147 else
5148 *option = (void *) (((struct vimoption *) (*option)) + 1);
5149
5150 ret = ((struct vimoption *) (*option));
5151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005152 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005153 if (ret->var == NULL)
5154 {
5155 ret = NULL;
5156 continue;
5157 }
5158
5159 switch (opt_type)
5160 {
5161 case SREQ_GLOBAL:
5162 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5163 ret = NULL;
5164 break;
5165 case SREQ_BUF:
5166 if (!(ret->indir & PV_BUF))
5167 ret = NULL;
5168 break;
5169 case SREQ_WIN:
5170 if (!(ret->indir & PV_WIN))
5171 ret = NULL;
5172 break;
5173 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005174 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005175 return NULL;
5176 }
5177 }
5178 while (ret == NULL);
5179
5180 return (char_u *)ret->fullname;
5181}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005182#endif
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005185 * Return the flags for the option at 'opt_idx'.
5186 */
5187 long_u
5188get_option_flags(int opt_idx)
5189{
5190 return options[opt_idx].flags;
5191}
5192
5193/*
5194 * Set a flag for the option at 'opt_idx'.
5195 */
5196 void
5197set_option_flag(int opt_idx, long_u flag)
5198{
5199 options[opt_idx].flags |= flag;
5200}
5201
5202/*
5203 * Clear a flag for the option at 'opt_idx'.
5204 */
5205 void
5206clear_option_flag(int opt_idx, long_u flag)
5207{
5208 options[opt_idx].flags &= ~flag;
5209}
5210
5211/*
5212 * Returns TRUE if the option at 'opt_idx' is a global option
5213 */
5214 int
5215is_global_option(int opt_idx)
5216{
5217 return options[opt_idx].indir == PV_NONE;
5218}
5219
5220/*
5221 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5222 * local value.
5223 */
5224 int
5225is_global_local_option(int opt_idx)
5226{
5227 return options[opt_idx].indir & PV_BOTH;
5228}
5229
5230/*
5231 * Returns TRUE if the option at 'opt_idx' is a window-local option
5232 */
5233 int
5234is_window_local_option(int opt_idx)
5235{
5236 return options[opt_idx].var == VAR_WIN;
5237}
5238
5239/*
5240 * Returns TRUE if the option at 'opt_idx' is a hidden option
5241 */
5242 int
5243is_hidden_option(int opt_idx)
5244{
5245 return options[opt_idx].var == NULL;
5246}
5247
5248#if defined(FEAT_CRYPT) || defined(PROTO)
5249/*
5250 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5251 */
5252 int
5253is_crypt_key_option(int opt_idx)
5254{
5255 return options[opt_idx].indir == PV_KEY;
5256}
5257#endif
5258
5259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 * Set the value of option "name".
5261 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005262 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005263 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005265 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005266set_option_value(
5267 char_u *name,
5268 long number,
5269 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005270 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 int opt_idx;
5273 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005274 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005275 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005278 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005279 {
5280 int key;
5281
5282 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005283 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005284 {
5285 char_u key_name[2];
5286
5287 if (key < 0)
5288 {
5289 key_name[0] = KEY2TERMCAP0(key);
5290 key_name[1] = KEY2TERMCAP1(key);
5291 }
5292 else
5293 {
5294 key_name[0] = KS_KEY;
5295 key_name[1] = (key & 0xff);
5296 }
5297 add_termcode(key_name, string, FALSE);
5298 if (full_screen)
5299 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005300 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005301 return NULL;
5302 }
5303
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005304 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 else
5307 {
5308 flags = options[opt_idx].flags;
5309#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005310 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005312 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005313 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005314 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005317 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005318 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005319
5320 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5321 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005323 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005325 int idx;
5326
5327 // Either we are given a string or we are setting option
5328 // to zero.
5329 for (idx = 0; string[idx] == '0'; ++idx)
5330 ;
5331 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005332 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005333 // There's another character after zeros or the string
5334 // is empty. In both cases, we are trying to set a
5335 // num option using a string.
5336 semsg(_(e_number_required_after_str_equal_str),
5337 name, string);
5338 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005339
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005342 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005343 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005344 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005345 errbuf, sizeof(errbuf), opt_flags);
5346 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005347 else
5348 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005352 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353}
5354
5355/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005356 * Call set_option_value() and when an error is returned report it.
5357 */
5358 void
5359set_option_value_give_err(
5360 char_u *name,
5361 long number,
5362 char_u *string,
5363 int opt_flags) // OPT_LOCAL or 0 (both)
5364{
5365 char *errmsg = set_option_value(name, number, string, opt_flags);
5366
5367 if (errmsg != NULL)
5368 emsg(_(errmsg));
5369}
5370
5371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 * Get the terminal code for a terminal option.
5373 * Returns NULL when not found.
5374 */
5375 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005376get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 int opt_idx;
5379 char_u *varp;
5380
5381 if (tname[0] != 't' || tname[1] != '_' ||
5382 tname[2] == NUL || tname[3] == NUL)
5383 return NULL;
5384 if ((opt_idx = findoption(tname)) >= 0)
5385 {
5386 varp = get_varp(&(options[opt_idx]));
5387 if (varp != NULL)
5388 varp = *(char_u **)(varp);
5389 return varp;
5390 }
5391 return find_termcode(tname + 2);
5392}
5393
5394 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005395get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
5397 int i;
5398
5399 i = findoption((char_u *)"hl");
5400 if (i >= 0)
5401 return options[i].def_val[VI_DEFAULT];
5402 return (char_u *)NULL;
5403}
5404
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005405 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005406get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005407{
5408 int i;
5409
5410 i = findoption((char_u *)"enc");
5411 if (i >= 0)
5412 return options[i].def_val[VI_DEFAULT];
5413 return (char_u *)NULL;
5414}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005415
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005416#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005417 int
5418is_option_allocated(char *name)
5419{
5420 int idx = findoption((char_u *)name);
5421
5422 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5423}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005424#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005425
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005426#if defined(FEAT_EVAL) || defined(PROTO)
5427/*
5428 * Return TRUE if "name" is a string option.
5429 * Returns FALSE if option "name" does not exist.
5430 */
5431 int
5432is_string_option(char_u *name)
5433{
5434 int idx = findoption(name);
5435
5436 return idx >= 0 && (options[idx].flags & P_STRING);
5437}
5438#endif
5439
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440/*
5441 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005442 * When "has_lt" is true there is a '<' before "*arg_arg".
5443 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 */
5445 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005446find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005448 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005450 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
5452 /*
5453 * Don't use get_special_key_code() for t_xx, we don't want it to call
5454 * add_termcap_entry().
5455 */
5456 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5457 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005458 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005460 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005462 key = find_special_key(&arg, &modifiers,
5463 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005464 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 key = 0;
5466 }
5467 return key;
5468}
5469
5470/*
5471 * if 'all' == 0: show changed options
5472 * if 'all' == 1: show all normal options
5473 * if 'all' == 2: show all terminal options
5474 */
5475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005476showoptions(
5477 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005478 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 struct vimoption *p;
5481 int col;
5482 int isterm;
5483 char_u *varp;
5484 struct vimoption **items;
5485 int item_count;
5486 int run;
5487 int row, rows;
5488 int cols;
5489 int i;
5490 int len;
5491
5492#define INC 20
5493#define GAP 3
5494
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005495 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 if (items == NULL)
5497 return;
5498
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005499 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005501 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005503 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005505 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005507 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005510 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 * 1. display the short items
5512 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005513 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 */
5515 for (run = 1; run <= 2 && !got_int; ++run)
5516 {
5517 /*
5518 * collect the items in items[]
5519 */
5520 item_count = 0;
5521 for (p = &options[0]; p->fullname != NULL; p++)
5522 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005523 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005524 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005525 continue;
5526
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 varp = NULL;
5528 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005529 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 if (p->indir != PV_NONE && !isterm)
5532 varp = get_varp_scope(p, opt_flags);
5533 }
5534 else
5535 varp = get_varp(p);
5536 if (varp != NULL
5537 && ((all == 2 && isterm)
5538 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005539 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005541 if (opt_flags & OPT_ONECOLUMN)
5542 len = Columns;
5543 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005544 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 else
5546 {
5547 option_value2string(p, opt_flags);
5548 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5549 }
5550 if ((len <= INC - GAP && run == 1) ||
5551 (len > INC - GAP && run == 2))
5552 items[item_count++] = p;
5553 }
5554 }
5555
5556 /*
5557 * display the items
5558 */
5559 if (run == 1)
5560 {
5561 cols = (Columns + GAP - 3) / INC;
5562 if (cols == 0)
5563 cols = 1;
5564 rows = (item_count + cols - 1) / cols;
5565 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 rows = item_count;
5568 for (row = 0; row < rows && !got_int; ++row)
5569 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005570 msg_putchar('\n'); // go to next line
5571 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 break;
5573 col = 0;
5574 for (i = row; i < item_count; i += rows)
5575 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005576 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 showoneopt(items[i], opt_flags);
5578 col += INC;
5579 }
5580 out_flush();
5581 ui_breakcheck();
5582 }
5583 }
5584 vim_free(items);
5585}
5586
5587/*
5588 * Return TRUE if option "p" has its default value.
5589 */
5590 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005591optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 int dvi;
5594
5595 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005596 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005597 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005599 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005601 // the cast to long is required for Manx C, long_i is
5602 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005603 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5606}
5607
5608/*
5609 * showoneopt: show the value of one option
5610 * must not be called with a hidden option!
5611 */
5612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005613showoneopt(
5614 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005617 char_u *varp;
5618 int save_silent = silent_mode;
5619
5620 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623 varp = get_varp_scope(p, opt_flags);
5624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005625 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5627 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005628 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005630 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005632 msg_puts(" ");
5633 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 if (!(p->flags & P_BOOL))
5635 {
5636 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005637 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 option_value2string(p, opt_flags);
5639 msg_outtrans(NameBuff);
5640 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005641
5642 silent_mode = save_silent;
5643 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644}
5645
5646/*
5647 * Write modified options as ":set" commands to a file.
5648 *
5649 * There are three values for "opt_flags":
5650 * OPT_GLOBAL: Write global option values and fresh values of
5651 * buffer-local options (used for start of a session
5652 * file).
5653 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5654 * curwin (used for a vimrc file).
5655 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5656 * and local values for window-local options of
5657 * curwin. Local values are also written when at the
5658 * default value, because a modeline or autocommand
5659 * may have set them when doing ":edit file" and the
5660 * user has set them back at the default or fresh
5661 * value.
5662 * When "local_only" is TRUE, don't write fresh
5663 * values, only local values (for ":mkview").
5664 * (fresh value = value used for a new buffer or window for a local option).
5665 *
5666 * Return FAIL on error, OK otherwise.
5667 */
5668 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005669makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005672 char_u *varp; // currently used value
5673 char_u *varp_fresh; // local value
5674 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 char *cmd;
5676 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005677 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
5679 /*
5680 * The options that don't have a default (terminal name, columns, lines)
5681 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005682 * Do the loop over "options[]" twice: once for options with the
5683 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005685 for (pri = 1; pri >= 0; --pri)
5686 {
5687 for (p = &options[0]; !istermoption(p); p++)
5688 if (!(p->flags & P_NO_MKRC)
5689 && !istermoption(p)
5690 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005692 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5694 continue;
5695
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005696 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5697 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5699 continue;
5700
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005701 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005703 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 continue;
5705
Bram Moolenaard23b7142021-04-17 21:04:34 +02005706 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5707 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005708 continue;
5709
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 round = 2;
5711 if (p->indir != PV_NONE)
5712 {
5713 if (p->var == VAR_WIN)
5714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005715 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (!(opt_flags & OPT_LOCAL))
5717 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005718 // When fresh value of window-local option is not at the
5719 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5721 {
5722 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005723 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
5725 round = 1;
5726 varp_local = varp;
5727 varp = varp_fresh;
5728 }
5729 }
5730 }
5731 }
5732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005733 // Round 1: fresh value for window-local options.
5734 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 for ( ; round <= 2; varp = varp_local, ++round)
5736 {
5737 if (round == 1 || (opt_flags & OPT_GLOBAL))
5738 cmd = "set";
5739 else
5740 cmd = "setlocal";
5741
5742 if (p->flags & P_BOOL)
5743 {
5744 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5745 return FAIL;
5746 }
5747 else if (p->flags & P_NUM)
5748 {
5749 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5750 return FAIL;
5751 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005752 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005754 int do_endif = FALSE;
5755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005756 // Don't set 'syntax' and 'filetype' again if the value is
5757 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005758 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005759#if defined(FEAT_SYN_HL)
5760 p->indir == PV_SYN ||
5761#endif
5762 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {
5764 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5765 *(char_u **)(varp)) < 0
5766 || put_eol(fd) < 0)
5767 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005768 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005771 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005773 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (put_line(fd, "endif") == FAIL)
5776 return FAIL;
5777 }
5778 }
5779 }
5780 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 return OK;
5783}
5784
5785#if defined(FEAT_FOLDING) || defined(PROTO)
5786/*
5787 * Generate set commands for the local fold options only. Used when
5788 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5789 */
5790 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005791makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005793 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005795 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 == FAIL
5797# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005798 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005800 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 == FAIL
5802 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5803 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5804 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5805 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5806 )
5807 return FAIL;
5808
5809 return OK;
5810}
5811#endif
5812
5813 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005814put_setstring(
5815 FILE *fd,
5816 char *cmd,
5817 char *name,
5818 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005819 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
5821 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005822 char_u *buf = NULL;
5823 char_u *part = NULL;
5824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5827 return FAIL;
5828 if (*valuep != NULL)
5829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005830 // Output 'pastetoggle' as key names. For other
5831 // options some characters have to be escaped with
5832 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (valuep == &p_pt)
5834 {
5835 s = *valuep;
5836 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005837 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 return FAIL;
5839 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005840 // expand the option value, replace $HOME by ~
5841 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005843 int size = (int)STRLEN(*valuep) + 1;
5844
5845 // replace home directory in the whole option value into "buf"
5846 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005847 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005848 goto fail;
5849 home_replace(NULL, *valuep, buf, size, FALSE);
5850
5851 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005852 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005853 // can be expanded when read back.
5854 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5855 && vim_strchr(*valuep, ',') != NULL)
5856 {
5857 part = alloc(size);
5858 if (part == NULL)
5859 goto fail;
5860
5861 // write line break to clear the option, e.g. ':set rtp='
5862 if (put_eol(fd) == FAIL)
5863 goto fail;
5864
5865 p = buf;
5866 while (*p != NUL)
5867 {
5868 // for each comma separated option part, append value to
5869 // the option, :set rtp+=value
5870 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5871 goto fail;
5872 (void)copy_option_part(&p, part, size, ",");
5873 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5874 goto fail;
5875 }
5876 vim_free(buf);
5877 vim_free(part);
5878 return OK;
5879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005881 {
5882 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005884 }
5885 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
5887 else if (put_escstr(fd, *valuep, 2) == FAIL)
5888 return FAIL;
5889 }
5890 if (put_eol(fd) < 0)
5891 return FAIL;
5892 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005893fail:
5894 vim_free(buf);
5895 vim_free(part);
5896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898
5899 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005900put_setnum(
5901 FILE *fd,
5902 char *cmd,
5903 char *name,
5904 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
5906 long wc;
5907
5908 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5909 return FAIL;
5910 if (wc_use_keyname((char_u *)valuep, &wc))
5911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005912 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5914 return FAIL;
5915 }
5916 else if (fprintf(fd, "%ld", *valuep) < 0)
5917 return FAIL;
5918 if (put_eol(fd) < 0)
5919 return FAIL;
5920 return OK;
5921}
5922
5923 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005924put_setbool(
5925 FILE *fd,
5926 char *cmd,
5927 char *name,
5928 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005930 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005931 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5933 || put_eol(fd) < 0)
5934 return FAIL;
5935 return OK;
5936}
5937
5938/*
5939 * Clear all the terminal options.
5940 * If the option has been allocated, free the memory.
5941 * Terminal options are never hidden or indirect.
5942 */
5943 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005944clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 /*
5947 * Reset a few things before clearing the old options. This may cause
5948 * outputting a few things that the terminal doesn't understand, but the
5949 * screen will be cleared later, so this is OK.
5950 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005951 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005952 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005954 // When starting the GUI close the display opened for the clipboard.
5955 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (gui.starting)
5957 clear_xterm_clip();
5958#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005959 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005961 free_termoptions();
5962}
5963
5964 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005965free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005966{
5967 struct vimoption *p;
5968
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005969 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 if (istermoption(p))
5971 {
5972 if (p->flags & P_ALLOCED)
5973 free_string_option(*(char_u **)(p->var));
5974 if (p->flags & P_DEF_ALLOCED)
5975 free_string_option(p->def_val[VI_DEFAULT]);
5976 *(char_u **)(p->var) = empty_option;
5977 p->def_val[VI_DEFAULT] = empty_option;
5978 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005979#ifdef FEAT_EVAL
5980 // remember where the option was cleared
5981 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 }
5984 clear_termcodes();
5985}
5986
5987/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005988 * Free the string for one term option, if it was allocated.
5989 * Set the string to empty_option and clear allocated flag.
5990 * "var" points to the option value.
5991 */
5992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005993free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005994{
5995 struct vimoption *p;
5996
5997 for (p = &options[0]; p->fullname != NULL; p++)
5998 if (p->var == var)
5999 {
6000 if (p->flags & P_ALLOCED)
6001 free_string_option(*(char_u **)(p->var));
6002 *(char_u **)(p->var) = empty_option;
6003 p->flags &= ~P_ALLOCED;
6004 break;
6005 }
6006}
6007
6008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 * Set the terminal option defaults to the current value.
6010 * Used after setting the terminal name.
6011 */
6012 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006013set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 struct vimoption *p;
6016
6017 for (p = &options[0]; p->fullname != NULL; p++)
6018 {
6019 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6020 {
6021 if (p->flags & P_DEF_ALLOCED)
6022 {
6023 free_string_option(p->def_val[VI_DEFAULT]);
6024 p->flags &= ~P_DEF_ALLOCED;
6025 }
6026 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6027 if (p->flags & P_ALLOCED)
6028 {
6029 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006030 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032 }
6033 }
6034}
6035
6036/*
6037 * return TRUE if 'p' starts with 't_'
6038 */
6039 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006040istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6043}
6044
Bram Moolenaardac13472019-09-16 21:06:21 +02006045/*
6046 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6047 */
6048 int
6049istermoption_idx(int opt_idx)
6050{
6051 return istermoption(&options[opt_idx]);
6052}
6053
Bram Moolenaar113e1072019-01-20 15:30:40 +01006054#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006056 * Unset local option value, similar to ":set opt<".
6057 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006058 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006059unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006060{
6061 struct vimoption *p;
6062 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006063 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006064
6065 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006066 if (opt_idx < 0)
6067 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006068 p = &(options[opt_idx]);
6069
6070 switch ((int)p->indir)
6071 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006072 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006073 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006074 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006075 break;
6076 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006077 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006078 break;
6079 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006080 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006081 break;
6082 case PV_AR:
6083 buf->b_p_ar = -1;
6084 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006085 case PV_BKC:
6086 clear_string_option(&buf->b_p_bkc);
6087 buf->b_bkc_flags = 0;
6088 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006089 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006090 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006091 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006092 case PV_TC:
6093 clear_string_option(&buf->b_p_tc);
6094 buf->b_tc_flags = 0;
6095 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006096 case PV_SISO:
6097 curwin->w_p_siso = -1;
6098 break;
6099 case PV_SO:
6100 curwin->w_p_so = -1;
6101 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006102# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006103 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006104 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006105 break;
6106 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006107 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006108 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006109# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006110 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006111 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006112 break;
6113 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006114 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006115 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006116# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006117 case PV_TSRFU:
6118 clear_string_option(&buf->b_p_tsrfu);
6119 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006120# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006121 case PV_FP:
6122 clear_string_option(&buf->b_p_fp);
6123 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006124# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006125 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006126 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006127 break;
6128 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006129 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006130 break;
6131 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006132 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006133 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006134# endif
6135# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006136 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006137 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006138 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006139# endif
6140# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006141 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006142 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006143 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006144# endif
6145# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006146 case PV_SBR:
6147 clear_string_option(&((win_T *)from)->w_p_sbr);
6148 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006149# endif
6150# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006151 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006152 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006153 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006154# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006155 case PV_UL:
6156 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6157 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006158 case PV_LW:
6159 clear_string_option(&buf->b_p_lw);
6160 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006161 case PV_MENC:
6162 clear_string_option(&buf->b_p_menc);
6163 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006164 case PV_LCS:
6165 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006166 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006167 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006168 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006169 case PV_FCS:
6170 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006171 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006172 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006173 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006174 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006175 clear_string_option(&((win_T *)from)->w_p_ve);
6176 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006177 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006178 }
6179}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006180#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006181
6182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006184 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 */
6186 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006187get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006189 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
6191 if (p->var == VAR_WIN)
6192 return (char_u *)GLOBAL_WO(get_varp(p));
6193 return p->var;
6194 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006195 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 switch ((int)p->indir)
6198 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006199 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006201 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6202 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6203 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006205 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6206 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6207 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006208 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006209 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006210 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006211 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6212 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006214 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6215 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006217 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6218 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006219#ifdef FEAT_COMPL_FUNC
6220 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6221#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006222#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6223 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6224#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006225#if defined(FEAT_CRYPT)
6226 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6227#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006228#ifdef FEAT_LINEBREAK
6229 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6230#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006231#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006232 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006233#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006234 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006235 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006236 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006237 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006238 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006239 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006240 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006243 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
6245 return get_varp(p);
6246}
6247
6248/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006249 * Get pointer to option variable at 'opt_idx', depending on local or global
6250 * scope.
6251 */
6252 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006253get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006254{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006255 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006256}
6257
6258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 * Get pointer to option variable.
6260 */
6261 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006262get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006264 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (p->var == NULL)
6266 return NULL;
6267
6268 switch ((int)p->indir)
6269 {
6270 case PV_NONE: return p->var;
6271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006272 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006273 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006275 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006277 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006279 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006281 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006283 case PV_TC: return *curbuf->b_p_tc != NUL
6284 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006285 case PV_BKC: return *curbuf->b_p_bkc != NUL
6286 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006287 case PV_SISO: return curwin->w_p_siso >= 0
6288 ? (char_u *)&(curwin->w_p_siso) : p->var;
6289 case PV_SO: return curwin->w_p_so >= 0
6290 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006292 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006294 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6296#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006297 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006299 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006301#ifdef FEAT_COMPL_FUNC
6302 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6303 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6304#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006305 case PV_FP: return *curbuf->b_p_fp != NUL
6306 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006308 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006310 case PV_GP: return *curbuf->b_p_gp != NUL
6311 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6312 case PV_MP: return *curbuf->b_p_mp != NUL
6313 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006315#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6316 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6317 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6318#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006319#if defined(FEAT_CRYPT)
6320 case PV_CM: return *curbuf->b_p_cm != NUL
6321 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6322#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006323#ifdef FEAT_LINEBREAK
6324 case PV_SBR: return *curwin->w_p_sbr != NUL
6325 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6326#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006327#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006328 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006329 ? (char_u *)&(curwin->w_p_stl) : p->var;
6330#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006331 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6332 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006333 case PV_LW: return *curbuf->b_p_lw != NUL
6334 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006335 case PV_MENC: return *curbuf->b_p_menc != NUL
6336 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337#ifdef FEAT_ARABIC
6338 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6339#endif
6340 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006341 case PV_LCS: return *curwin->w_p_lcs != NUL
6342 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006343 case PV_FCS: return *curwin->w_p_fcs != NUL
6344 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006345 case PV_VE: return *curwin->w_p_ve != NUL
6346 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006347#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006348 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6349#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006350#ifdef FEAT_SYN_HL
6351 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6352 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006353 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006354 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356#ifdef FEAT_DIFF
6357 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6358#endif
6359#ifdef FEAT_FOLDING
6360 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6361 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6362 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6363 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6364 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6365 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6366 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6367# ifdef FEAT_EVAL
6368 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6369 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6370# endif
6371 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6372#endif
6373 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006374 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006375#ifdef FEAT_LINEBREAK
6376 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006379 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006380#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6382#endif
6383#ifdef FEAT_RIGHTLEFT
6384 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6385 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6386#endif
6387 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006388 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6390#ifdef FEAT_LINEBREAK
6391 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006392 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6393 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006395 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006397 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006398#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006399 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6400 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6401#endif
6402#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006403 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6404 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6405 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6409 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6412 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6414 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6416 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6417 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006418 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421#ifdef FEAT_FOLDING
6422 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006425#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006426 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006428#ifdef FEAT_COMPL_FUNC
6429 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006430 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006431#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006432#ifdef FEAT_EVAL
6433 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6434#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006435 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006437 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006443 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6445 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6446 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6447 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6448#ifdef FEAT_FIND_ID
6449# ifdef FEAT_EVAL
6450 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6451# endif
6452#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006453#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6455 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6456#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006457#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006458 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460#ifdef FEAT_CRYPT
6461 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006464 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6466 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6467 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6468 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6469 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006471 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6478#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006479 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006480 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6481#endif
6482#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006483 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6484 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6485 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006486 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487#endif
6488 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6489 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6490 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6491 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006492#ifdef FEAT_PERSISTENT_UNDO
6493 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6496#ifdef FEAT_KEYMAP
6497 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6498#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006499#ifdef FEAT_SIGNS
6500 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6501#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006502#ifdef FEAT_VARTABS
6503 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6504 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6505#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006506 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006508 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 return (char_u *)&(curbuf->b_p_wm);
6510}
6511
6512/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006513 * Return a pointer to the variable for option at 'opt_idx'
6514 */
6515 char_u *
6516get_option_var(int opt_idx)
6517{
6518 return options[opt_idx].var;
6519}
6520
Dominique Pelle748b3082022-01-08 12:41:16 +00006521#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006522/*
6523 * Return the full name of the option at 'opt_idx'
6524 */
6525 char_u *
6526get_option_fullname(int opt_idx)
6527{
6528 return (char_u *)options[opt_idx].fullname;
6529}
Dominique Pelle748b3082022-01-08 12:41:16 +00006530#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006531
6532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 * Get the value of 'equalprg', either the buffer-local one or the global one.
6534 */
6535 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006536get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 if (*curbuf->b_p_ep == NUL)
6539 return p_ep;
6540 return curbuf->b_p_ep;
6541}
6542
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543/*
6544 * Copy options from one window to another.
6545 * Used when splitting a window.
6546 */
6547 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006548win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6551 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006552 after_copy_winopt(wp_to);
6553}
6554
6555/*
6556 * After copying window options: update variables depending on options.
6557 */
6558 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006559after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006560{
6561#ifdef FEAT_LINEBREAK
6562 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006563#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006564#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006565 fill_culopt_flags(NULL, wp);
6566 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006567#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006568 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6569 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006570}
6571
6572 static char_u *
6573copy_option_val(char_u *val)
6574{
6575 if (val == empty_option)
6576 return empty_option; // no need to allocate memory
6577 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580/*
6581 * Copy the options from one winopt_T to another.
6582 * Doesn't free the old option values in "to", use clear_winopt() for that.
6583 * The 'scroll' option is not copied, because it depends on the window height.
6584 * The 'previewwindow' option is reset, there can be only one preview window.
6585 */
6586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006587copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
6589#ifdef FEAT_ARABIC
6590 to->wo_arab = from->wo_arab;
6591#endif
6592 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006593 to->wo_lcs = copy_option_val(from->wo_lcs);
6594 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006596 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006597 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006598 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006599#ifdef FEAT_LINEBREAK
6600 to->wo_nuw = from->wo_nuw;
6601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602#ifdef FEAT_RIGHTLEFT
6603 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006604 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006606#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006607 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006608#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006609#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006610 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006613#ifdef FEAT_DIFF
6614 to->wo_wrap_save = from->wo_wrap_save;
6615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616#ifdef FEAT_LINEBREAK
6617 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006618 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006619 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006621 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006623 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006624 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006625 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006626 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006627#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006628 to->wo_spell = from->wo_spell;
6629#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006630#ifdef FEAT_SYN_HL
6631 to->wo_cuc = from->wo_cuc;
6632 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006633 to->wo_culopt = copy_option_val(from->wo_culopt);
6634 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636#ifdef FEAT_DIFF
6637 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006638 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006640#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006641 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006642 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006643#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006644#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006645 to->wo_twk = copy_option_val(from->wo_twk);
6646 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648#ifdef FEAT_FOLDING
6649 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006650 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006652 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006653 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 to->wo_fml = from->wo_fml;
6655 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006656 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006657 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006658 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006659 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 to->wo_fdn = from->wo_fdn;
6661# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006662 to->wo_fde = copy_option_val(from->wo_fde);
6663 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006665 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006667#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006668 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006669#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006670
6671#ifdef FEAT_EVAL
6672 // Copy the script context so that we know where the value was last set.
6673 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6674 sizeof(to->wo_script_ctx));
6675#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006676 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677}
6678
6679/*
6680 * Check string options in a window for a NULL value.
6681 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006683check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 check_winopt(&win->w_onebuf_opt);
6686 check_winopt(&win->w_allbuf_opt);
6687}
6688
6689/*
6690 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6691 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006693check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695#ifdef FEAT_FOLDING
6696 check_string_option(&wop->wo_fdi);
6697 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006698 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699# ifdef FEAT_EVAL
6700 check_string_option(&wop->wo_fde);
6701 check_string_option(&wop->wo_fdt);
6702# endif
6703 check_string_option(&wop->wo_fmr);
6704#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006705#ifdef FEAT_SIGNS
6706 check_string_option(&wop->wo_scl);
6707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708#ifdef FEAT_RIGHTLEFT
6709 check_string_option(&wop->wo_rlc);
6710#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006711#ifdef FEAT_LINEBREAK
6712 check_string_option(&wop->wo_sbr);
6713#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006714#ifdef FEAT_STL_OPT
6715 check_string_option(&wop->wo_stl);
6716#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006717#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006718 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006719 check_string_option(&wop->wo_cc);
6720#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006721#ifdef FEAT_CONCEAL
6722 check_string_option(&wop->wo_cocu);
6723#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006724#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006725 check_string_option(&wop->wo_twk);
6726 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006727#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006728#ifdef FEAT_LINEBREAK
6729 check_string_option(&wop->wo_briopt);
6730#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006731 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006732 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006733 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006734 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735}
6736
6737/*
6738 * Free the allocated memory inside a winopt_T.
6739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006741clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743#ifdef FEAT_FOLDING
6744 clear_string_option(&wop->wo_fdi);
6745 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006746 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747# ifdef FEAT_EVAL
6748 clear_string_option(&wop->wo_fde);
6749 clear_string_option(&wop->wo_fdt);
6750# endif
6751 clear_string_option(&wop->wo_fmr);
6752#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006753#ifdef FEAT_SIGNS
6754 clear_string_option(&wop->wo_scl);
6755#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006756#ifdef FEAT_LINEBREAK
6757 clear_string_option(&wop->wo_briopt);
6758#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006759 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760#ifdef FEAT_RIGHTLEFT
6761 clear_string_option(&wop->wo_rlc);
6762#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006763#ifdef FEAT_LINEBREAK
6764 clear_string_option(&wop->wo_sbr);
6765#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006766#ifdef FEAT_STL_OPT
6767 clear_string_option(&wop->wo_stl);
6768#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006769#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006770 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006771 clear_string_option(&wop->wo_cc);
6772#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006773#ifdef FEAT_CONCEAL
6774 clear_string_option(&wop->wo_cocu);
6775#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006776#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006777 clear_string_option(&wop->wo_twk);
6778 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006779#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006780 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006781 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006782 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783}
6784
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006785#ifdef FEAT_EVAL
6786// Index into the options table for a buffer-local option enum.
6787static int buf_opt_idx[BV_COUNT];
6788# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6789
6790/*
6791 * Initialize buf_opt_idx[] if not done already.
6792 */
6793 static void
6794init_buf_opt_idx(void)
6795{
6796 static int did_init_buf_opt_idx = FALSE;
6797 int i;
6798
6799 if (did_init_buf_opt_idx)
6800 return;
6801 did_init_buf_opt_idx = TRUE;
6802 for (i = 0; !istermoption_idx(i); i++)
6803 if (options[i].indir & PV_BUF)
6804 buf_opt_idx[options[i].indir & PV_MASK] = i;
6805}
6806#else
6807# define COPY_OPT_SCTX(buf, bv)
6808#endif
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810/*
6811 * Copy global option values to local options for one buffer.
6812 * Used when creating a new buffer and sometimes when entering a buffer.
6813 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006814 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6816 * appropriate.
6817 * BCO_NOHELP Don't copy the values to a help buffer.
6818 */
6819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006820buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006823 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 int dont_do_help;
6825 int did_isk = FALSE;
6826
6827 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 * Skip this when the option defaults have not been set yet. Happens when
6829 * main() allocates the first buffer.
6830 */
6831 if (p_cpo != NULL)
6832 {
6833 /*
6834 * Always copy when entering and 'cpo' contains 'S'.
6835 * Don't copy when already initialized.
6836 * Don't copy when 'cpo' contains 's' and not entering.
6837 * 'S' BCO_ENTER initialized 's' should_copy
6838 * yes yes X X TRUE
6839 * yes no yes X FALSE
6840 * no X yes X FALSE
6841 * X no no yes FALSE
6842 * X no no no TRUE
6843 * no yes no X TRUE
6844 */
6845 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6846 && (buf->b_p_initialized
6847 || (!(flags & BCO_ENTER)
6848 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6849 should_copy = FALSE;
6850
6851 if (should_copy || (flags & BCO_ALWAYS))
6852 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006853#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006854 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006855 init_buf_opt_idx();
6856#endif
6857 // Don't copy the options specific to a help buffer when
6858 // BCO_NOHELP is given or the options were initialized already
6859 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6861 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006862 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 save_p_isk = buf->b_p_isk;
6865 buf->b_p_isk = NULL;
6866 }
6867 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006868 * Always free the allocated strings. If not already initialized,
6869 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 */
6871 if (!buf->b_p_initialized)
6872 {
6873 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006874 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006877 switch (*p_ffs)
6878 {
6879 case 'm':
6880 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6881 case 'd':
6882 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6883 case 'u':
6884 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6885 default:
6886 buf->b_p_ff = vim_strsave(p_ff);
6887 }
6888 if (buf->b_p_ff != NULL)
6889 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 buf->b_p_bh = empty_option;
6891 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893 else
6894 free_buf_options(buf, FALSE);
6895
6896 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006897 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 buf->b_p_ai_nopaste = p_ai_nopaste;
6899 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006900 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006902 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 buf->b_p_tw_nopaste = p_tw_nopaste;
6904 buf->b_p_tw_nobin = p_tw_nobin;
6905 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006906 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 buf->b_p_wm_nopaste = p_wm_nopaste;
6908 buf->b_p_wm_nobin = p_wm_nobin;
6909 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006910 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006911 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006912 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006913 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006914 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006916 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006918 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006920 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 buf->b_p_ml_nobin = p_ml_nobin;
6922 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006923 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006924 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006925 buf->b_p_swf = FALSE;
6926 else
6927 {
6928 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006929 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006932 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006933#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006934 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006935 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006937#ifdef FEAT_COMPL_FUNC
6938 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006939 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006940 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006941 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006942 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006943 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006944#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006945#ifdef FEAT_EVAL
6946 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006947 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006948 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006951 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006953#ifdef FEAT_VARTABS
6954 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006955 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006956 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006957 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006958 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006959 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006960 buf->b_p_vsts_nopaste = p_vsts_nopaste
6961 ? vim_strsave(p_vsts_nopaste) : NULL;
6962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006966 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967#ifdef FEAT_FOLDING
6968 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970#endif
6971 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006972 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006973 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006974 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006975 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6976 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006978 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006980 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006982 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006984 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006985
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006987 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006989 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006991 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006992 buf->b_p_cinsd = vim_strsave(p_cinsd);
6993 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006994 buf->b_p_lop = vim_strsave(p_lop);
6995 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006996
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006997 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007000 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007002 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007004 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007006 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007008 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007009 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007010 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007011#endif
7012#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007013 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007015 (void)compile_cap_prog(&buf->b_s);
7016 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007017 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007018 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007019 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007020 buf->b_s.b_p_spo = vim_strsave(p_spo);
7021 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007023#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007025 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007027 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007029 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007030#if defined(FEAT_EVAL)
7031 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007032 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034#ifdef FEAT_CRYPT
7035 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007036 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007039 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040#ifdef FEAT_KEYMAP
7041 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007042 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 buf->b_kmap_state |= KEYMAP_INIT;
7044#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007045#ifdef FEAT_TERMINAL
7046 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007047 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007048#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007049 // This isn't really an option, but copying the langmap and IME
7050 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007052 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007054 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 // options that are normally global but also have a local value
7057 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007059 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007060 buf->b_p_bkc = empty_option;
7061 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062#ifdef FEAT_QUICKFIX
7063 buf->b_p_gp = empty_option;
7064 buf->b_p_mp = empty_option;
7065 buf->b_p_efm = empty_option;
7066#endif
7067 buf->b_p_ep = empty_option;
7068 buf->b_p_kp = empty_option;
7069 buf->b_p_path = empty_option;
7070 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007071 buf->b_p_tc = empty_option;
7072 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#ifdef FEAT_FIND_ID
7074 buf->b_p_def = empty_option;
7075 buf->b_p_inc = empty_option;
7076# ifdef FEAT_EVAL
7077 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007078 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079# endif
7080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 buf->b_p_dict = empty_option;
7082 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007083#ifdef FEAT_COMPL_FUNC
7084 buf->b_p_tsrfu = empty_option;
7085#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007086 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007087 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007088#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7089 buf->b_p_bexpr = empty_option;
7090#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007091#if defined(FEAT_CRYPT)
7092 buf->b_p_cm = empty_option;
7093#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007094#ifdef FEAT_PERSISTENT_UNDO
7095 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007096 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007097#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007098 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007099 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
7101 /*
7102 * Don't copy the options set by ex_help(), use the saved values,
7103 * when going from a help buffer to a non-help buffer.
7104 * Don't touch these at all when BCO_NOHELP is used and going from
7105 * or to a help buffer.
7106 */
7107 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007110#ifdef FEAT_VARTABS
7111 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007112 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007113 else
7114 buf->b_p_vts_array = NULL;
7115#endif
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 else
7118 {
7119 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007120 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 did_isk = TRUE;
7122 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007123 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007124#ifdef FEAT_VARTABS
7125 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007126 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007127 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007128 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007129 else
7130 buf->b_p_vts_array = NULL;
7131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (buf->b_p_bt[0] == 'h')
7134 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007136 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
7138 }
7139
7140 /*
7141 * When the options should be copied (ignoring BCO_ALWAYS), set the
7142 * flag that indicates that the options have been initialized.
7143 */
7144 if (should_copy)
7145 buf->b_p_initialized = TRUE;
7146 }
7147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (did_isk)
7150 (void)buf_init_chartab(buf, FALSE);
7151}
7152
7153/*
7154 * Reset the 'modifiable' option and its default value.
7155 */
7156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007157reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 int opt_idx;
7160
7161 curbuf->b_p_ma = FALSE;
7162 p_ma = FALSE;
7163 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007164 if (opt_idx >= 0)
7165 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167
7168/*
7169 * Set the global value for 'iminsert' to the local value.
7170 */
7171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007172set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 p_iminsert = curbuf->b_p_iminsert;
7175}
7176
7177/*
7178 * Set the global value for 'imsearch' to the local value.
7179 */
7180 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007181set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182{
7183 p_imsearch = curbuf->b_p_imsearch;
7184}
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186static int expand_option_idx = -1;
7187static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7188static int expand_option_flags = 0;
7189
7190 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007191set_context_in_set_cmd(
7192 expand_T *xp,
7193 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007194 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007197 long_u flags = 0; // init for GCC
7198 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 char_u *p;
7200 char_u *s;
7201 int is_term_option = FALSE;
7202 int key;
7203
7204 expand_option_flags = opt_flags;
7205
7206 xp->xp_context = EXPAND_SETTINGS;
7207 if (*arg == NUL)
7208 {
7209 xp->xp_pattern = arg;
7210 return;
7211 }
7212 p = arg + STRLEN(arg) - 1;
7213 if (*p == ' ' && *(p - 1) != '\\')
7214 {
7215 xp->xp_pattern = p + 1;
7216 return;
7217 }
7218 while (p > arg)
7219 {
7220 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007221 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 if (*p == ' ' || *p == ',')
7223 {
7224 while (s > arg && *(s - 1) == '\\')
7225 --s;
7226 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007227 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (*p == ' ' && ((p - s) & 1) == 0)
7229 {
7230 ++p;
7231 break;
7232 }
7233 --p;
7234 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007235 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
7237 xp->xp_context = EXPAND_BOOL_SETTINGS;
7238 p += 2;
7239 }
7240 if (STRNCMP(p, "inv", 3) == 0)
7241 {
7242 xp->xp_context = EXPAND_BOOL_SETTINGS;
7243 p += 3;
7244 }
7245 xp->xp_pattern = arg = p;
7246 if (*arg == '<')
7247 {
7248 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007249 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 return;
7251 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007252 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
7254 xp->xp_context = EXPAND_NOTHING;
7255 return;
7256 }
7257 nextchar = *++p;
7258 is_term_option = TRUE;
7259 expand_option_name[2] = KEY2TERMCAP0(key);
7260 expand_option_name[3] = KEY2TERMCAP1(key);
7261 }
7262 else
7263 {
7264 if (p[0] == 't' && p[1] == '_')
7265 {
7266 p += 2;
7267 if (*p != NUL)
7268 ++p;
7269 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007270 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 nextchar = *++p;
7272 is_term_option = TRUE;
7273 expand_option_name[2] = p[-2];
7274 expand_option_name[3] = p[-1];
7275 }
7276 else
7277 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007278 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7280 p++;
7281 if (*p == NUL)
7282 return;
7283 nextchar = *p;
7284 *p = NUL;
7285 opt_idx = findoption(arg);
7286 *p = nextchar;
7287 if (opt_idx == -1 || options[opt_idx].var == NULL)
7288 {
7289 xp->xp_context = EXPAND_NOTHING;
7290 return;
7291 }
7292 flags = options[opt_idx].flags;
7293 if (flags & P_BOOL)
7294 {
7295 xp->xp_context = EXPAND_NOTHING;
7296 return;
7297 }
7298 }
7299 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007300 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7302 {
7303 ++p;
7304 nextchar = '=';
7305 }
7306 if ((nextchar != '=' && nextchar != ':')
7307 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7308 {
7309 xp->xp_context = EXPAND_UNSUCCESSFUL;
7310 return;
7311 }
7312 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7313 {
7314 xp->xp_context = EXPAND_OLD_SETTING;
7315 if (is_term_option)
7316 expand_option_idx = -1;
7317 else
7318 expand_option_idx = opt_idx;
7319 xp->xp_pattern = p + 1;
7320 return;
7321 }
7322 xp->xp_context = EXPAND_NOTHING;
7323 if (is_term_option || (flags & P_NUM))
7324 return;
7325
7326 xp->xp_pattern = p + 1;
7327
7328 if (flags & P_EXPAND)
7329 {
7330 p = options[opt_idx].var;
7331 if (p == (char_u *)&p_bdir
7332 || p == (char_u *)&p_dir
7333 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007334 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337#ifdef FEAT_SESSION
7338 || p == (char_u *)&p_vdir
7339#endif
7340 )
7341 {
7342 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007343 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 xp->xp_backslash = XP_BS_THREE;
7345 else
7346 xp->xp_backslash = XP_BS_ONE;
7347 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007348 else if (p == (char_u *)&p_ft)
7349 {
7350 xp->xp_context = EXPAND_FILETYPE;
7351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 else
7353 {
7354 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 if (p == (char_u *)&p_tags)
7357 xp->xp_backslash = XP_BS_THREE;
7358 else
7359 xp->xp_backslash = XP_BS_ONE;
7360 }
7361 }
7362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007363 // For an option that is a list of file names, find the start of the
7364 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7366 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007367 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 if (*p == ' ' || *p == ',')
7369 {
7370 s = p;
7371 while (s > xp->xp_pattern && *(s - 1) == '\\')
7372 --s;
7373 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7374 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7375 {
7376 xp->xp_pattern = p + 1;
7377 break;
7378 }
7379 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007380
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007381#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007382 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007383 if (options[opt_idx].var == (char_u *)&p_sps
7384 && STRNCMP(p, "file:", 5) == 0)
7385 {
7386 xp->xp_pattern = p + 5;
7387 break;
7388 }
7389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391}
7392
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007393/*
7394 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7395 *
7396 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7397 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7398 *
7399 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7400 * regular expression 'regmatch', then stores the match in matches[idx] and
7401 * returns TRUE.
7402 *
7403 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7404 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7405 *
7406 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7407 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7408 */
7409 static int
7410match_str(
7411 char_u *str,
7412 regmatch_T *regmatch,
7413 char_u **matches,
7414 int idx,
7415 int test_only,
7416 int fuzzy,
7417 char_u *fuzzystr,
7418 fuzmatch_str_T *fuzmatch)
7419{
7420 if (!fuzzy)
7421 {
7422 if (vim_regexec(regmatch, str, (colnr_T)0))
7423 {
7424 if (!test_only)
7425 matches[idx] = vim_strsave(str);
7426 return TRUE;
7427 }
7428 }
7429 else
7430 {
7431 int score;
7432
7433 score = fuzzy_match_str(str, fuzzystr);
7434 if (score != 0)
7435 {
7436 if (!test_only)
7437 {
7438 fuzmatch[idx].idx = idx;
7439 fuzmatch[idx].str = vim_strsave(str);
7440 fuzmatch[idx].score = score;
7441 }
7442
7443 return TRUE;
7444 }
7445 }
7446
7447 return FALSE;
7448}
7449
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007451ExpandSettings(
7452 expand_T *xp,
7453 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007454 char_u *fuzzystr,
7455 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007456 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007457 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007459 int num_normal = 0; // Nr of matching non-term-code settings
7460 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 int opt_idx;
7462 int match;
7463 int count = 0;
7464 char_u *str;
7465 int loop;
7466 int is_term_opt;
7467 char_u name_buf[MAX_KEY_NAME_LEN];
7468 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007469 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007470 int fuzzy;
7471 fuzmatch_str_T *fuzmatch = NULL;
7472
Christian Brabandtcb747892022-05-08 21:10:56 +01007473 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007475 // do this loop twice:
7476 // loop == 0: count the number of matching options
7477 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 for (loop = 0; loop <= 1; ++loop)
7479 {
7480 regmatch->rm_ic = ic;
7481 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7482 {
K.Takataeeec2542021-06-02 13:28:16 +02007483 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007484 {
7485 if (match_str((char_u *)names[match], regmatch, *matches,
7486 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 {
7488 if (loop == 0)
7489 num_normal++;
7490 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007491 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
7495 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7496 opt_idx++)
7497 {
7498 if (options[opt_idx].var == NULL)
7499 continue;
7500 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7501 && !(options[opt_idx].flags & P_BOOL))
7502 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007503 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 if (is_term_opt && num_normal > 0)
7505 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007506
7507 if (match_str(str, regmatch, *matches, count, (loop == 0),
7508 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
7510 if (loop == 0)
7511 {
7512 if (is_term_opt)
7513 num_term++;
7514 else
7515 num_normal++;
7516 }
7517 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007518 count++;
7519 }
7520 else if (!fuzzy && options[opt_idx].shortname != NULL
7521 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007522 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007523 {
7524 // Compare against the abbreviated option name (for regular
7525 // expression match). Fuzzy matching (previous if) already
7526 // matches against both the expanded and abbreviated names.
7527 if (loop == 0)
7528 {
7529 if (is_term_opt)
7530 num_term++;
7531 else
7532 num_normal++;
7533 }
7534 else
7535 (*matches)[count++] = vim_strsave(str);
7536 }
7537 else if (is_term_opt)
7538 {
7539 name_buf[0] = '<';
7540 name_buf[1] = 't';
7541 name_buf[2] = '_';
7542 name_buf[3] = str[2];
7543 name_buf[4] = str[3];
7544 name_buf[5] = '>';
7545 name_buf[6] = NUL;
7546
7547 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7548 fuzzy, fuzzystr, fuzmatch))
7549 {
7550 if (loop == 0)
7551 num_term++;
7552 else
7553 count++;
7554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
7556 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007557
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 /*
7559 * Check terminal key codes, these are not in the option table
7560 */
7561 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7562 {
7563 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7564 {
7565 if (!isprint(str[0]) || !isprint(str[1]))
7566 continue;
7567
7568 name_buf[0] = 't';
7569 name_buf[1] = '_';
7570 name_buf[2] = str[0];
7571 name_buf[3] = str[1];
7572 name_buf[4] = NUL;
7573
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007574 if (match_str(name_buf, regmatch, *matches, count,
7575 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7576 {
7577 if (loop == 0)
7578 num_term++;
7579 else
7580 count++;
7581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 else
7583 {
7584 name_buf[0] = '<';
7585 name_buf[1] = 't';
7586 name_buf[2] = '_';
7587 name_buf[3] = str[0];
7588 name_buf[4] = str[1];
7589 name_buf[5] = '>';
7590 name_buf[6] = NUL;
7591
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007592 if (match_str(name_buf, regmatch, *matches, count,
7593 (loop == 0), fuzzy, fuzzystr,
7594 fuzmatch))
7595 {
7596 if (loop == 0)
7597 num_term++;
7598 else
7599 count++;
7600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 }
7602 }
7603
7604 /*
7605 * Check special key names.
7606 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007607 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7609 {
7610 name_buf[0] = '<';
7611 STRCPY(name_buf + 1, str);
7612 STRCAT(name_buf, ">");
7613
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007614 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7615 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {
7617 if (loop == 0)
7618 num_term++;
7619 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007620 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622 }
7623 }
7624 if (loop == 0)
7625 {
7626 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007627 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007629 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 else
7631 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007632 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007634 *matches = ALLOC_MULT(char_u *, *numMatches);
7635 if (*matches == NULL)
7636 {
7637 *matches = (char_u **)"";
7638 return FAIL;
7639 }
7640 }
7641 else
7642 {
7643 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7644 if (fuzmatch == NULL)
7645 {
7646 *matches = (char_u **)"";
7647 return FAIL;
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650 }
7651 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007652
7653 if (fuzzy &&
7654 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7655 return FAIL;
7656
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 return OK;
7658}
7659
7660 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007661ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007663 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 char_u *buf;
7665
zeertzjqb0d45ec2023-01-25 15:04:22 +00007666 *numMatches = 0;
7667 *matches = ALLOC_ONE(char_u *);
7668 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 return FAIL;
7670
7671 /*
7672 * For a terminal key code expand_option_idx is < 0.
7673 */
7674 if (expand_option_idx < 0)
7675 {
7676 var = find_termcode(expand_option_name + 2);
7677 if (var == NULL)
7678 expand_option_idx = findoption(expand_option_name);
7679 }
7680
7681 if (expand_option_idx >= 0)
7682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007683 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 option_value2string(&options[expand_option_idx], expand_option_flags);
7685 var = NameBuff;
7686 }
7687 else if (var == NULL)
7688 var = (char_u *)"";
7689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007690 // A backslash is required before some characters. This is the reverse of
7691 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 buf = vim_strsave_escaped(var, escape_chars);
7693
7694 if (buf == NULL)
7695 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007696 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 return FAIL;
7698 }
7699
7700#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007701 // For MS-Windows et al. we don't double backslashes at the start and
7702 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007703 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 if (var[0] == '\\' && var[1] == '\\'
7705 && expand_option_idx >= 0
7706 && (options[expand_option_idx].flags & P_EXPAND)
7707 && vim_isfilec(var[2])
7708 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007709 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710#endif
7711
zeertzjqb0d45ec2023-01-25 15:04:22 +00007712 *matches[0] = buf;
7713 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 return OK;
7715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
7717/*
7718 * Get the value for the numeric or string option *opp in a nice format into
7719 * NameBuff[]. Must not be called with a hidden option!
7720 */
7721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007722option_value2string(
7723 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007724 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 char_u *varp;
7727
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007728 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729
7730 if (opp->flags & P_NUM)
7731 {
7732 long wc = 0;
7733
7734 if (wc_use_keyname(varp, &wc))
7735 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7736 else if (wc != 0)
7737 STRCPY(NameBuff, transchar((int)wc));
7738 else
7739 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7740 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007741 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {
7743 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007744 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 NameBuff[0] = NUL;
7746#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007747 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007748 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 STRCPY(NameBuff, "*****");
7750#endif
7751 else if (opp->flags & P_EXPAND)
7752 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007753 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 else if ((char_u **)opp->var == &p_pt)
7755 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7756 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007757 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 }
7759}
7760
7761/*
7762 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7763 * printed as a keyname.
7764 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7765 */
7766 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007767wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7770 {
7771 *wcp = *(long *)varp;
7772 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7773 return TRUE;
7774 }
7775 return FALSE;
7776}
7777
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 * Return TRUE if "x" is present in 'shortmess' option, or
7780 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7781 */
7782 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007783shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007785 return p_shm != NULL &&
7786 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 || (vim_strchr(p_shm, 'a') != NULL
7788 && vim_strchr((char_u *)SHM_A, x) != NULL));
7789}
7790
7791/*
7792 * paste_option_changed() - Called after p_paste was set or reset.
7793 */
7794 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007795paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 static int old_p_paste = FALSE;
7798 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007799 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#ifdef FEAT_RIGHTLEFT
7802 static int save_ri = 0;
7803 static int save_hkmap = 0;
7804#endif
7805 buf_T *buf;
7806
7807 if (p_paste)
7808 {
7809 /*
7810 * Paste switched from off to on.
7811 * Save the current values, so they can be restored later.
7812 */
7813 if (!old_p_paste)
7814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007815 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007816 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {
7818 buf->b_p_tw_nopaste = buf->b_p_tw;
7819 buf->b_p_wm_nopaste = buf->b_p_wm;
7820 buf->b_p_sts_nopaste = buf->b_p_sts;
7821 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007822 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007823#ifdef FEAT_VARTABS
7824 if (buf->b_p_vsts_nopaste)
7825 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007826 buf->b_p_vsts_nopaste =
7827 buf->b_p_vsts && buf->b_p_vsts != empty_option
7828 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
7831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007832 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007834 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836#ifdef FEAT_RIGHTLEFT
7837 save_ri = p_ri;
7838 save_hkmap = p_hkmap;
7839#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007840 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007841 p_ai_nopaste = p_ai;
7842 p_et_nopaste = p_et;
7843 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 p_tw_nopaste = p_tw;
7845 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007846#ifdef FEAT_VARTABS
7847 if (p_vsts_nopaste)
7848 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007849 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7850 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
7853
7854 /*
7855 * Always set the option values, also when 'paste' is set when it is
7856 * already on.
7857 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007858 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007859 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007861 buf->b_p_tw = 0; // textwidth is 0
7862 buf->b_p_wm = 0; // wrapmargin is 0
7863 buf->b_p_sts = 0; // softtabstop is 0
7864 buf->b_p_ai = 0; // no auto-indent
7865 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007866#ifdef FEAT_VARTABS
7867 if (buf->b_p_vsts)
7868 free_string_option(buf->b_p_vsts);
7869 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007870 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007874 // set global options
7875 p_sm = 0; // no showmatch
7876 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007878 status_redraw_all(); // redraw to remove the ruler
7879 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007881 p_ri = 0; // no reverse insert
7882 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007884 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 p_tw = 0;
7886 p_wm = 0;
7887 p_sts = 0;
7888 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007889#ifdef FEAT_VARTABS
7890 if (p_vsts)
7891 free_string_option(p_vsts);
7892 p_vsts = empty_option;
7893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895
7896 /*
7897 * Paste switched from on to off: Restore saved values.
7898 */
7899 else if (old_p_paste)
7900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007901 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007902 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {
7904 buf->b_p_tw = buf->b_p_tw_nopaste;
7905 buf->b_p_wm = buf->b_p_wm_nopaste;
7906 buf->b_p_sts = buf->b_p_sts_nopaste;
7907 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007908 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007909#ifdef FEAT_VARTABS
7910 if (buf->b_p_vsts)
7911 free_string_option(buf->b_p_vsts);
7912 buf->b_p_vsts = buf->b_p_vsts_nopaste
7913 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007914 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007915 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007916 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007917 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007918 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
7921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007922 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007924 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007926 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928#ifdef FEAT_RIGHTLEFT
7929 p_ri = save_ri;
7930 p_hkmap = save_hkmap;
7931#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007932 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007933 p_ai = p_ai_nopaste;
7934 p_et = p_et_nopaste;
7935 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 p_tw = p_tw_nopaste;
7937 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007938#ifdef FEAT_VARTABS
7939 if (p_vsts)
7940 free_string_option(p_vsts);
7941 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 }
7944
7945 old_p_paste = p_paste;
7946}
7947
7948/*
7949 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7950 *
7951 * Reset 'compatible' and set the values for options that didn't get set yet
7952 * to the Vim defaults.
7953 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007954 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 */
7956 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007957vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007959 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007960 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007961 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962
7963 if (!option_was_set((char_u *)"cp"))
7964 {
7965 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007966 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7968 set_option_default(opt_idx, OPT_FREE, FALSE);
7969 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007970 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007972
7973 if (fname != NULL)
7974 {
7975 p = vim_getenv(envname, &dofree);
7976 if (p == NULL)
7977 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007978 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007979 p = FullName_save(fname, FALSE);
7980 if (p != NULL)
7981 {
7982 vim_setenv(envname, p);
7983 vim_free(p);
7984 }
7985 }
7986 else if (dofree)
7987 vim_free(p);
7988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989}
7990
7991/*
7992 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7993 */
7994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007995change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007997 int opt_idx;
7998
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (p_cp != on)
8000 {
8001 p_cp = on;
8002 compatible_set();
8003 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008004 opt_idx = findoption((char_u *)"cp");
8005 if (opt_idx >= 0)
8006 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007}
8008
8009/*
8010 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008011 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 */
8013 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008014option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015{
8016 int idx;
8017
8018 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008019 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 return FALSE;
8021 if (options[idx].flags & P_WAS_SET)
8022 return TRUE;
8023 return FALSE;
8024}
8025
8026/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008027 * Reset the flag indicating option "name" was set.
8028 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008029 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008030reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008031{
8032 int idx = findoption(name);
8033
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008034 if (idx < 0)
8035 return FAIL;
8036
8037 options[idx].flags &= ~P_WAS_SET;
8038 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008039}
8040
8041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 * compatible_set() - Called when 'compatible' has been set or unset.
8043 *
8044 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8045 * flag) to a Vi compatible value.
8046 * When 'compatible' is unset: Set all options that have a different default
8047 * for Vim (without the P_VI_DEF flag) to that default.
8048 */
8049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008050compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
8052 int opt_idx;
8053
Bram Moolenaardac13472019-09-16 21:06:21 +02008054 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8056 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8057 set_option_default(opt_idx, OPT_FREE, p_cp);
8058 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008059 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
Bram Moolenaardac13472019-09-16 21:06:21 +02008062#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064/*
8065 * fill_breakat_flags() -- called when 'breakat' changes value.
8066 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008067 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008068fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008070 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 int i;
8072
8073 for (i = 0; i < 256; i++)
8074 breakat_flags[i] = FALSE;
8075
8076 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008077 for (p = p_breakat; *p; p++)
8078 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080#endif
8081
8082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 * Check if backspacing over something is allowed.
8084 */
8085 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008086can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008087 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008089#ifdef FEAT_JOB_CHANNEL
8090 if (what == BS_START && bt_prompt(curbuf))
8091 return FALSE;
8092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 switch (*p_bs)
8094 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008095 case '3': return TRUE;
8096 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 case '1': return (what != BS_START);
8098 case '0': return FALSE;
8099 }
8100 return vim_strchr(p_bs, what) != NULL;
8101}
8102
8103/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008104 * Return the effective 'scrolloff' value for the current window, using the
8105 * global value when appropriate.
8106 */
8107 long
8108get_scrolloff_value(void)
8109{
8110 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8111}
8112
8113/*
8114 * Return the effective 'sidescrolloff' value for the current window, using the
8115 * global value when appropriate.
8116 */
8117 long
8118get_sidescrolloff_value(void)
8119{
8120 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8121}
8122
8123/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008124 * Get the local or global value of 'backupcopy'.
8125 */
8126 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008127get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008128{
8129 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8130}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008131
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008132#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008133/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008134 * Get the local or global value of 'formatlistpat'.
8135 */
8136 char_u *
8137get_flp_value(buf_T *buf)
8138{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008139 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8140 return p_flp;
8141 return buf->b_p_flp;
8142}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008143#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008144
8145/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008146 * Get the local or global value of the 'virtualedit' flags.
8147 */
8148 unsigned int
8149get_ve_flags(void)
8150{
Gary Johnson51ad8502021-08-03 18:33:08 +02008151 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8152 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008153}
8154
Bram Moolenaaree857022019-11-09 23:26:40 +01008155#if defined(FEAT_LINEBREAK) || defined(PROTO)
8156/*
8157 * Get the local or global value of 'showbreak'.
8158 */
8159 char_u *
8160get_showbreak_value(win_T *win)
8161{
8162 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8163 return p_sbr;
8164 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8165 return empty_option;
8166 return win->w_p_sbr;
8167}
8168#endif
8169
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008170#if defined(FEAT_EVAL) || defined(PROTO)
8171/*
8172 * Get window or buffer local options.
8173 */
8174 dict_T *
8175get_winbuf_options(int bufopt)
8176{
8177 dict_T *d;
8178 int opt_idx;
8179
8180 d = dict_alloc();
8181 if (d == NULL)
8182 return NULL;
8183
Bram Moolenaardac13472019-09-16 21:06:21 +02008184 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008185 {
8186 struct vimoption *opt = &options[opt_idx];
8187
8188 if ((bufopt && (opt->indir & PV_BUF))
8189 || (!bufopt && (opt->indir & PV_WIN)))
8190 {
8191 char_u *varp = get_varp(opt);
8192
8193 if (varp != NULL)
8194 {
8195 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008196 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008197 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008198 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008199 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008200 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008201 }
8202 }
8203 }
8204
8205 return d;
8206}
8207#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008208
Bram Moolenaardac13472019-09-16 21:06:21 +02008209#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008210/*
8211 * This is called when 'culopt' is changed
8212 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008213 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008214fill_culopt_flags(char_u *val, win_T *wp)
8215{
8216 char_u *p;
8217 char_u culopt_flags_new = 0;
8218
8219 if (val == NULL)
8220 p = wp->w_p_culopt;
8221 else
8222 p = val;
8223 while (*p != NUL)
8224 {
8225 if (STRNCMP(p, "line", 4) == 0)
8226 {
8227 p += 4;
8228 culopt_flags_new |= CULOPT_LINE;
8229 }
8230 else if (STRNCMP(p, "both", 4) == 0)
8231 {
8232 p += 4;
8233 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8234 }
8235 else if (STRNCMP(p, "number", 6) == 0)
8236 {
8237 p += 6;
8238 culopt_flags_new |= CULOPT_NBR;
8239 }
8240 else if (STRNCMP(p, "screenline", 10) == 0)
8241 {
8242 p += 10;
8243 culopt_flags_new |= CULOPT_SCRLINE;
8244 }
8245
8246 if (*p != ',' && *p != NUL)
8247 return FAIL;
8248 if (*p == ',')
8249 ++p;
8250 }
8251
8252 // Can't have both "line" and "screenline".
8253 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8254 return FAIL;
8255 wp->w_p_culopt_flags = culopt_flags_new;
8256
8257 return OK;
8258}
8259#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008260
8261/*
8262 * Get the value of 'magic' adjusted for Vim9 script.
8263 */
8264 int
8265magic_isset(void)
8266{
8267 switch (magic_overruled)
8268 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008269 case OPTION_MAGIC_ON: return TRUE;
8270 case OPTION_MAGIC_OFF: return FALSE;
8271 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008272 }
8273#ifdef FEAT_EVAL
8274 if (in_vim9script())
8275 return TRUE;
8276#endif
8277 return p_magic;
8278}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008279
8280/*
8281 * Set the callback function value for an option that accepts a function name,
8282 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8283 * Returns OK if the option is successfully set to a function, otherwise
8284 * returns FAIL.
8285 */
8286 int
8287option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8288{
8289#ifdef FEAT_EVAL
8290 typval_T *tv;
8291 callback_T cb;
8292
8293 if (optval == NULL || *optval == NUL)
8294 {
8295 free_callback(optcb);
8296 return OK;
8297 }
8298
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008299 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008300 || (STRNCMP(optval, "function(", 9) == 0)
8301 || (STRNCMP(optval, "funcref(", 8) == 0))
8302 // Lambda expression or a funcref
8303 tv = eval_expr(optval, NULL);
8304 else
8305 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008306 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008307 if (tv == NULL)
8308 return FAIL;
8309
8310 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008311 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008312 {
8313 free_tv(tv);
8314 return FAIL;
8315 }
8316
8317 free_callback(optcb);
8318 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008319 if (cb.cb_free_name)
8320 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008321 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008322
8323 // when using Vim9 style "import.funcname" it needs to be expanded to
8324 // "import#funcname".
8325 expand_autload_callback(optcb);
8326
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008327 return OK;
8328#else
8329 return FAIL;
8330#endif
8331}