blob: c2f0028671a7658f8be154f8f993f317a13a2307 [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");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000189 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000191#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
192 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
193#endif
194 {
ichizok02560422022-04-05 14:18:44 +0100195#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100196 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200197 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100198#elif defined(HAVE_TOTAL_MEM)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100199 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000200 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100201#else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000202 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000205 opt_idx = findoption((char_u *)"maxmem");
206 if (opt_idx >= 0)
207 {
208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100209 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
210 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000211#endif
212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
213 }
214 }
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();
319 if (p != NULL)
320 {
321 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
322 restricted = TRUE;
323 vim_free(p);
324 }
325}
326#endif
327
328#ifdef CLEAN_RUNTIMEPATH
329/*
330 * When Vim is started with the "--clean" argument, set the default value
331 * for the 'runtimepath' and 'packpath' options.
332 */
333 static void
334set_init_clean_rtp(void)
335{
336 int opt_idx;
337
338 opt_idx = findoption((char_u *)"runtimepath");
339 if (opt_idx >= 0)
340 {
341 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
342 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
343 }
344 opt_idx = findoption((char_u *)"packpath");
345 if (opt_idx >= 0)
346 {
347 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
348 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
349 }
350}
351#endif
352
353/*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
362 static void
363set_init_expand_env(void)
364{
365 int opt_idx;
366 char_u *p;
367
368 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
369 {
370 if ((options[opt_idx].flags & P_GETTEXT)
371 && options[opt_idx].var != NULL)
372 p = (char_u *)_(*(char **)options[opt_idx].var);
373 else
374 p = option_expand(opt_idx, NULL);
375 if (p != NULL && (p = vim_strsave(p)) != NULL)
376 {
377 *(char_u **)options[opt_idx].var = p;
378 // VIMEXP
379 // Defaults for all expanded options are currently the same for Vi
380 // and Vim. When this changes, add some code here! Also need to
381 // split P_DEF_ALLOCED in two.
382 if (options[opt_idx].flags & P_DEF_ALLOCED)
383 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
384 options[opt_idx].def_val[VI_DEFAULT] = p;
385 options[opt_idx].flags |= P_DEF_ALLOCED;
386 }
387 }
388}
389
390/*
391 * Initialize the 'LANG' environment variable to a default value.
392 */
393 static void
394set_init_lang_env(void)
395{
396#if defined(MSWIN) && defined(FEAT_GETTEXT)
397 // If $LANG isn't set, try to get a good value for it. This makes the
398 // right language be used automatically. Don't do this for English.
399 if (mch_getenv((char_u *)"LANG") == NULL)
400 {
401 char buf[20];
402 long_u n;
403
404 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
405 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
406 // only the first two.
407 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
408 (LPTSTR)buf, 20);
409 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
410 {
411 // There are a few exceptions (probably more)
412 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
413 STRCPY(buf, "zh_TW");
414 else if (STRNICMP(buf, "chs", 3) == 0
415 || STRNICMP(buf, "zhc", 3) == 0)
416 STRCPY(buf, "zh_CN");
417 else if (STRNICMP(buf, "jp", 2) == 0)
418 STRCPY(buf, "ja");
419 else
420 buf[2] = NUL; // truncate to two-letter code
421 vim_setenv((char_u *)"LANG", (char_u *)buf);
422 }
423 }
424#elif defined(MACOS_CONVERT)
425 // Moved to os_mac_conv.c to avoid dependency problems.
426 mac_lang_init();
427#endif
428}
429
430/*
431 * Initialize the 'encoding' option to a default value.
432 */
433 static void
434set_init_default_encoding(void)
435{
436 char_u *p;
437 int opt_idx;
438
439# ifdef MSWIN
440 // MS-Windows has builtin support for conversion to and from Unicode, using
441 // "utf-8" for 'encoding' should work best for most users.
442 p = vim_strsave((char_u *)ENC_DFLT);
443# else
444 // enc_locale() will try to find the encoding of the current locale.
445 // This works best for properly configured systems, old and new.
446 p = enc_locale();
447# endif
448 if (p == NULL)
449 return;
450
451 // Try setting 'encoding' and check if the value is valid.
452 // If not, go back to the default encoding.
453 char_u *save_enc = p_enc;
454 p_enc = p;
455 if (STRCMP(p_enc, "gb18030") == 0)
456 {
457 // We don't support "gb18030", but "cp936" is a good substitute
458 // for practical purposes, thus use that. It's not an alias to
459 // still support conversion between gb18030 and utf-8.
460 p_enc = vim_strsave((char_u *)"cp936");
461 vim_free(p);
462 }
463 if (mb_init() == NULL)
464 {
465 opt_idx = findoption((char_u *)"encoding");
466 if (opt_idx >= 0)
467 {
468 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
469 options[opt_idx].flags |= P_DEF_ALLOCED;
470 }
471
472#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
473 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
474 {
475 // Adjust the default for 'isprint' and 'iskeyword' to match
476 // latin1. Also set the defaults for when 'nocompatible' is
477 // set.
478 set_string_option_direct((char_u *)"isp", -1,
479 ISP_LATIN1, OPT_FREE, SID_NONE);
480 set_string_option_direct((char_u *)"isk", -1,
481 ISK_LATIN1, OPT_FREE, SID_NONE);
482 opt_idx = findoption((char_u *)"isp");
483 if (opt_idx >= 0)
484 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
485 opt_idx = findoption((char_u *)"isk");
486 if (opt_idx >= 0)
487 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
488 (void)init_chartab();
489 }
490#endif
491
492#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
493 // Win32 console: When GetACP() returns a different value from
494 // GetConsoleCP() set 'termencoding'.
495 if (
496# ifdef VIMDLL
497 (!gui.in_use && !gui.starting) &&
498# endif
499 GetACP() != GetConsoleCP())
500 {
501 char buf[50];
502
503 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
504 // Use an alternative value.
505 if (GetConsoleCP() == 0)
506 sprintf(buf, "cp%ld", (long)GetACP());
507 else
508 sprintf(buf, "cp%ld", (long)GetConsoleCP());
509 p_tenc = vim_strsave((char_u *)buf);
510 if (p_tenc != NULL)
511 {
512 opt_idx = findoption((char_u *)"termencoding");
513 if (opt_idx >= 0)
514 {
515 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
516 options[opt_idx].flags |= P_DEF_ALLOCED;
517 }
518 convert_setup(&input_conv, p_tenc, p_enc);
519 convert_setup(&output_conv, p_enc, p_tenc);
520 }
521 else
522 p_tenc = empty_option;
523 }
524#endif
525#if defined(MSWIN)
526 // $HOME may have characters in active code page.
527 init_homedir();
528#endif
529 }
530 else
531 {
532 vim_free(p_enc);
533 p_enc = save_enc;
534 }
535
536}
537
538/*
539 * Initialize the options, first part.
540 *
541 * Called only once from main(), just after creating the first buffer.
542 * If "clean_arg" is TRUE Vim was started with --clean.
543 */
544 void
545set_init_1(int clean_arg)
546{
547#ifdef FEAT_LANGMAP
548 langmap_init();
549#endif
550
551 // Be Vi compatible by default
552 p_cp = TRUE;
553
554 // Use POSIX compatibility when $VIM_POSIX is set.
555 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
556 {
557 set_string_default("cpo", (char_u *)CPO_ALL);
558 set_string_default("shm", (char_u *)SHM_POSIX);
559 }
560
561 set_init_default_shell();
562 set_init_default_backupskip();
563 set_init_default_maxmemtot();
564 set_init_default_cdpath();
565 set_init_default_printencoding();
566#ifdef FEAT_POSTSCRIPT
567 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568#endif
569
570 /*
571 * Set all the options (except the terminal options) to their default
572 * value. Also set the global value for local options.
573 */
574 set_options_default(0);
575
matveytadbb1bf2022-02-01 17:26:12 +0000576#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000577 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000578#endif
579
Bram Moolenaar07268702018-03-01 21:57:32 +0100580#ifdef CLEAN_RUNTIMEPATH
581 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000582 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100583#endif
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#ifdef FEAT_GUI
586 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100587 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#endif
589
590 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100591 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100592 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 check_buf_options(curbuf);
594 check_win_options(curwin);
595 check_options();
596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100597 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 didset_options();
599
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000600#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100601 // Use the current chartab for the generic chartab. This is not in
602 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000603 init_spell_chartab();
604#endif
605
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000606 // Expand environment variables and things like "~" for the defaults.
607 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100612 // Detect use of mlterm.
613 // Mlterm is a terminal emulator akin to xterm that has some special
614 // abilities (bidi namely).
615 // NOTE: mlterm's author is being asked to 'set' a variable
616 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100618 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#endif
620
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200621 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000623 set_init_lang_env();
624 set_init_default_encoding();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 set_helplang_default(get_mess_lang());
629#endif
630}
631
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200632static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
633
634/*
635 * Set the "fileencodings" option to the default value for when 'encoding' is
636 * utf-8.
637 */
638 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000639set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200640{
641 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
642 OPT_FREE, 0);
643}
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645/*
646 * Set an option to its default value.
647 * This does not take care of side effects!
648 */
649 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100650set_option_default(
651 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100652 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
653 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100655 char_u *varp; // pointer to variable for current option
656 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000658 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
660
661 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
662 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100663 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
666 if (flags & P_STRING)
667 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200668 // 'fencs' default value depends on 'encoding'
669 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
670 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100671 // Use set_string_option_direct() for local options to handle
672 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200673 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200674 set_string_option_direct(NULL, opt_idx,
675 options[opt_idx].def_val[dvi], opt_flags, 0);
676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200678 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
679 free_string_option(*(char_u **)(varp));
680 *(char_u **)varp = options[opt_idx].def_val[dvi];
681 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 }
683 }
684 else if (flags & P_NUM)
685 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000686 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 win_comp_scroll(curwin);
688 else
689 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100690 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
691
692 if ((long *)varp == &curwin->w_p_so
693 || (long *)varp == &curwin->w_p_siso)
694 // 'scrolloff' and 'sidescrolloff' local values have a
695 // different default value than the global default.
696 *(long *)varp = -1;
697 else
698 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100699 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (both)
701 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100702 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100705 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100707 // the cast to long is required for Manx C, long_i is needed for
708 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000709 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000710#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100711 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000712 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
713 *(int *)varp = FALSE;
714#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100715 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (both)
717 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
718 *(int *)varp;
719 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100721 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000722 flagsp = insecure_flag(opt_idx, opt_flags);
723 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 }
725
726#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200727 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#endif
729}
730
731/*
732 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200733 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */
735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100736set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738{
739 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000741 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
Bram Moolenaardac13472019-09-16 21:06:21 +0200743 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200744 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200745 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100746 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200747# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200748 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200749 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200750# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100751 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 set_option_default(i, opt_flags, p_cp);
753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100754 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000755 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200757 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758}
759
760/*
761 * Set the Vi-default value of a string option.
762 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100763 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100765 static void
766set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 char_u *p;
769 int opt_idx;
770
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100771 if (escape && vim_strchr(val, ' ') != NULL)
772 p = vim_strsave_escaped(val, (char_u *)" ");
773 else
774 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000775 if (p == NULL) // we don't want a NULL
776 return;
777
778 opt_idx = findoption((char_u *)name);
779 if (opt_idx < 0)
780 return;
781
782 if (options[opt_idx].flags & P_DEF_ALLOCED)
783 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
784 options[opt_idx].def_val[VI_DEFAULT] = p;
785 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100788 void
789set_string_default(char *name, char_u *val)
790{
791 set_string_default_esc(name, val, FALSE);
792}
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200795 * For an option value that contains comma separated items, find "newval" in
796 * "origval". Return NULL if not found.
797 */
798 static char_u *
799find_dup_item(char_u *origval, char_u *newval, long_u flags)
800{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200801 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200802 size_t newlen;
803 char_u *s;
804
805 if (origval == NULL)
806 return NULL;
807
808 newlen = STRLEN(newval);
809 for (s = origval; *s != NUL; ++s)
810 {
811 if ((!(flags & P_COMMA)
812 || s == origval
813 || (s[-1] == ',' && !(bs & 1)))
814 && STRNCMP(s, newval, newlen) == 0
815 && (!(flags & P_COMMA)
816 || s[newlen] == ','
817 || s[newlen] == NUL))
818 return s;
819 // Count backslashes. Only a comma with an even number of backslashes
820 // or a single backslash preceded by a comma before it is recognized as
821 // a separator.
822 if ((s > origval + 1
823 && s[-1] == '\\'
824 && s[-2] != ',')
825 || (s == origval + 1
826 && s[-1] == '\\'))
827 ++bs;
828 else
829 bs = 0;
830 }
831 return NULL;
832}
833
834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * Set the Vi-default value of a number option.
836 * Used for 'lines' and 'columns'.
837 */
838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100839set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000841 int opt_idx;
842
843 opt_idx = findoption((char_u *)name);
844 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000845 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846}
847
Dominique Pelle748b3082022-01-08 12:41:16 +0000848#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200849/*
850 * Set all window-local and buffer-local options to the Vim default.
851 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200852 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200853 */
854 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200855set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200856{
857 win_T *save_curwin = curwin;
858 int i;
859
860 curwin = wp;
861 curbuf = curwin->w_buffer;
862 block_autocmds();
863
Bram Moolenaardac13472019-09-16 21:06:21 +0200864 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200865 {
866 struct vimoption *p = &(options[i]);
867 char_u *varp = get_varp_scope(p, OPT_LOCAL);
868
869 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200870 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200871 && !(options[i].flags & P_NODEFAULT)
872 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200873 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200874 }
875
876 unblock_autocmds();
877 curwin = save_curwin;
878 curbuf = curwin->w_buffer;
879}
Dominique Pelle748b3082022-01-08 12:41:16 +0000880#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200881
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000882#if defined(EXITFREE) || defined(PROTO)
883/*
884 * Free all options.
885 */
886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100887free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000888{
889 int i;
890
Bram Moolenaardac13472019-09-16 21:06:21 +0200891 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000892 {
893 if (options[i].indir == PV_NONE)
894 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100895 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100896 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000897 free_string_option(*(char_u **)options[i].var);
898 if (options[i].flags & P_DEF_ALLOCED)
899 free_string_option(options[i].def_val[VI_DEFAULT]);
900 }
901 else if (options[i].var != VAR_WIN
902 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100903 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200904 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000905 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000906 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000907 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000908}
909#endif
910
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912/*
913 * Initialize the options, part two: After getting Rows and Columns and
914 * setting 'term'.
915 */
916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100917set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000919 int idx;
920
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100922 * 'scroll' defaults to half the window height. The stored default is zero,
923 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000925 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000926 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000927 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 comp_col();
929
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000930 /*
931 * 'window' is only for backwards compatibility with Vi.
932 * Default is Rows - 1.
933 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000934 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000935 p_window = Rows - 1;
936 set_number_default("window", Rows - 1);
937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100938 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100939#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000940 /*
941 * If 'background' wasn't set by the user, try guessing the value,
942 * depending on the terminal name. Only need to check for terminals
943 * with a dark background, that can handle color.
944 */
945 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000946 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
947 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000949 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100950 // don't mark it as set, when starting the GUI it may be
951 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000952 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000955
956#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000958#endif
959#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100960 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000961#endif
962#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100963 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967/*
968 * Initialize the options, part three: After reading the .vimrc
969 */
970 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100971set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100973#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974/*
975 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
976 * This is done after other initializations, where 'shell' might have been
977 * set, but only if they have not been set before.
978 */
979 char_u *p;
980 int idx_srr;
981 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 int idx_sp;
984 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000988 if (idx_srr < 0)
989 do_srr = FALSE;
990 else
991 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100992# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000994 if (idx_sp < 0)
995 do_sp = FALSE;
996 else
997 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100998# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200999 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (p != NULL)
1001 {
1002 /*
1003 * Default for p_sp is "| tee", for p_srr is ">".
1004 * For known shells it is changed here to include stderr.
1005 */
1006 if ( fnamecmp(p, "csh") == 0
1007 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 || fnamecmp(p, "csh.exe") == 0
1010 || fnamecmp(p, "tcsh.exe") == 0
1011# endif
1012 )
1013 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001014# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (do_sp)
1016 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001017# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001019# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1023 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (do_srr)
1026 {
1027 p_srr = (char_u *)">&";
1028 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1029 }
1030 }
Mike Williams12795022021-06-28 20:53:58 +02001031# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001032 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1033 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001034 else if ( fnamecmp(p, "powershell") == 0
1035 || fnamecmp(p, "powershell.exe") == 0
1036 )
1037 {
1038# if defined(FEAT_QUICKFIX)
1039 if (do_sp)
1040 {
1041 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1042 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1043 }
1044# endif
1045 if (do_srr)
1046 {
1047 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1048 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1049 }
1050 }
1051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 else
Natanael Copa56318362021-05-06 18:46:35 +02001053 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if ( fnamecmp(p, "sh") == 0
1055 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001056 || fnamecmp(p, "mksh") == 0
1057 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001059 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001061 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001062 || fnamecmp(p, "ash") == 0
1063 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001064 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001065# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 || fnamecmp(p, "cmd") == 0
1067 || fnamecmp(p, "sh.exe") == 0
1068 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001069 || fnamecmp(p, "mksh.exe") == 0
1070 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001072 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 || fnamecmp(p, "bash.exe") == 0
1074 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001075 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001076 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001078 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001080# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (do_sp)
1082 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001083# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001085# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001086 if (fnamecmp(p, "pwsh") == 0)
1087 p_sp = (char_u *)">%s 2>&1";
1088 else
1089 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1092 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (do_srr)
1095 {
1096 p_srr = (char_u *)">%s 2>&1";
1097 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1098 }
1099 }
1100 vim_free(p);
1101 }
1102#endif
1103
Bram Moolenaar4f974752019-02-17 17:44:42 +01001104#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001106 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1107 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001109 * set, but only if they have not been set before.
1110 * Default values depend on shell (cmd.exe is default shell):
1111 *
1112 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001113 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001114 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001115 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001116 * "sh" like shells - "-c" "\""
1117 *
1118 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 */
Mike Williams12795022021-06-28 20:53:58 +02001120 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1121 {
1122 int idx_opt;
1123
1124 idx_opt = findoption((char_u *)"shcf");
1125 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1126 {
1127 p_shcf = (char_u*)"-Command";
1128 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1129 }
1130
1131 idx_opt = findoption((char_u *)"sxq");
1132 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1133 {
1134 p_sxq = (char_u*)"\"";
1135 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1136 }
1137 }
1138 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 int idx3;
1141
1142 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001143 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 p_shcf = (char_u *)"-c";
1146 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1147 }
1148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001149 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001151 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 p_sxq = (char_u *)"\"";
1154 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001157 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1158 {
1159 int idx3;
1160
1161 /*
1162 * cmd.exe on Windows will strip the first and last double quote given
1163 * on the command line, e.g. most of the time things like:
1164 * cmd /c "my path/to/echo" "my args to echo"
1165 * become:
1166 * my path/to/echo" "my args to echo
1167 * when executed.
1168 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001169 * To avoid this, set shellxquote to surround the command in
1170 * parenthesis. This appears to make most commands work, without
1171 * breaking commands that worked previously, such as
1172 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001173 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001174 idx3 = findoption((char_u *)"sxq");
1175 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1176 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001177 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001178 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1179 }
1180
Bram Moolenaara64ba222012-02-12 23:23:31 +01001181 idx3 = findoption((char_u *)"shcf");
1182 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1183 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001184 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001185 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1186 }
1187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#endif
1189
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001190 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001191 {
1192 int idx_ffs = findoption((char_u *)"ffs");
1193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001194 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001195 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1196 set_fileformat(default_fileformat(), OPT_LOCAL);
1197 }
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200}
1201
1202#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1203/*
1204 * When 'helplang' is still at its default value, set it to "lang".
1205 * Only the first two characters of "lang" are used.
1206 */
1207 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001208set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
1210 int idx;
1211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001212 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 return;
1214 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001215 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1216 return;
1217
1218 if (options[idx].flags & P_ALLOCED)
1219 free_string_option(p_hlg);
1220 p_hlg = vim_strsave(lang);
1221 if (p_hlg == NULL)
1222 p_hlg = empty_option;
1223 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001225 // zh_CN becomes "cn", zh_TW becomes "tw"
1226 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001227 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001228 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1229 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001230 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001231 // any C like setting, such as C.UTF-8, becomes "en"
1232 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1233 {
1234 p_hlg[0] = 'e';
1235 p_hlg[1] = 'n';
1236 }
1237 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001239 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240}
1241#endif
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243/*
1244 * 'title' and 'icon' only default to true if they have not been set or reset
1245 * in .vimrc and we can read the old value.
1246 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1247 * they can be reset. This reduces startup time when using X on a remote
1248 * machine.
1249 */
1250 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001251set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 int idx1;
1254 long val;
1255
1256 /*
1257 * If GUI is (going to be) used, we can always set the window title and
1258 * icon name. Saves a bit of time, because the X11 display server does
1259 * not need to be contacted.
1260 */
1261 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001262 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264#ifdef FEAT_GUI
1265 if (gui.starting || gui.in_use)
1266 val = TRUE;
1267 else
1268#endif
1269 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001270 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 p_title = val;
1272 }
1273 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001274 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001276 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001278
1279#ifdef FEAT_GUI
1280 if (gui.starting || gui.in_use)
1281 val = TRUE;
1282 else
1283#endif
1284 val = mch_can_restore_icon();
1285 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1286 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001289 void
1290ex_set(exarg_T *eap)
1291{
1292 int flags = 0;
1293
1294 if (eap->cmdidx == CMD_setlocal)
1295 flags = OPT_LOCAL;
1296 else if (eap->cmdidx == CMD_setglobal)
1297 flags = OPT_GLOBAL;
1298#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001299 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001300 ex_options(eap);
1301 else
1302#endif
1303 {
1304 if (eap->forceit)
1305 flags |= OPT_ONECOLUMN;
1306 (void)do_set(eap->arg, flags);
1307 }
1308}
1309
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001310/*
1311 * :set operator types
1312 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001313typedef enum {
1314 OP_NONE = 0,
1315 OP_ADDING, // "opt+=arg"
1316 OP_PREPENDING, // "opt^=arg"
1317 OP_REMOVING, // "opt-=arg"
1318} set_op_T;
1319
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001320typedef enum {
1321 PREFIX_NO = 0, // "no" prefix
1322 PREFIX_NONE, // no prefix
1323 PREFIX_INV, // "inv" prefix
1324} set_prefix_T;
1325
1326/*
1327 * Return the prefix type for the option name in *argp.
1328 */
1329 static set_prefix_T
1330get_option_prefix(char_u **argp)
1331{
1332 int prefix = PREFIX_NONE;
1333 char_u *arg = *argp;
1334
1335 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1336 {
1337 prefix = PREFIX_NO;
1338 arg += 2;
1339 }
1340 else if (STRNCMP(arg, "inv", 3) == 0)
1341 {
1342 prefix = PREFIX_INV;
1343 arg += 3;
1344 }
1345
1346 *argp = arg;
1347 return prefix;
1348}
1349
1350/*
1351 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1352 * and the option name length in "*lenp". For a <t_xx> option, return the key
1353 * number in "*keyp".
1354 *
1355 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1356 * otherwise returns OK.
1357 */
1358 static int
1359parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1360{
1361 int key = 0;
1362 int len;
1363 int opt_idx;
1364
1365 if (*arg == '<')
1366 {
1367 opt_idx = -1;
1368 // look out for <t_>;>
1369 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1370 len = 5;
1371 else
1372 {
1373 len = 1;
1374 while (arg[len] != NUL && arg[len] != '>')
1375 ++len;
1376 }
1377 if (arg[len] != '>')
1378 return FAIL;
1379
1380 arg[len] = NUL; // put NUL after name
1381 if (arg[1] == 't' && arg[2] == '_') // could be term code
1382 opt_idx = findoption(arg + 1);
1383 arg[len++] = '>'; // restore '>'
1384 if (opt_idx == -1)
1385 key = find_key_option(arg + 1, TRUE);
1386 }
1387 else
1388 {
1389 int nextchar; // next non-white char after option name
1390
1391 len = 0;
1392 /*
1393 * The two characters after "t_" may not be alphanumeric.
1394 */
1395 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1396 len = 4;
1397 else
1398 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1399 ++len;
1400 nextchar = arg[len];
1401 arg[len] = NUL; // put NUL after name
1402 opt_idx = findoption(arg);
1403 arg[len] = nextchar; // restore nextchar
1404 if (opt_idx == -1)
1405 key = find_key_option(arg, FALSE);
1406 }
1407
1408 *keyp = key;
1409 *lenp = len;
1410 *opt_idxp = opt_idx;
1411
1412 return OK;
1413}
1414
1415/*
1416 * Get the option operator (+=, ^=, -=).
1417 */
1418 static set_op_T
1419get_opt_op(char_u *arg)
1420{
1421 set_op_T op = OP_NONE;
1422
1423 if (*arg != NUL && *(arg + 1) == '=')
1424 {
1425 if (*arg == '+')
1426 op = OP_ADDING; // "+="
1427 else if (*arg == '^')
1428 op = OP_PREPENDING; // "^="
1429 else if (*arg == '-')
1430 op = OP_REMOVING; // "-="
1431 }
1432
1433 return op;
1434}
1435
1436/*
1437 * Validate whether the value of the option in "opt_idx" can be changed.
1438 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1439 * if it can be changed.
1440 */
1441 static int
1442validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1443{
1444 // Skip all options that are not window-local (used when showing
1445 // an already loaded buffer in a window).
1446 if ((opt_flags & OPT_WINONLY)
1447 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1448 return FAIL;
1449
1450 // Skip all options that are window-local (used for :vimgrep).
1451 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1452 && options[opt_idx].var == VAR_WIN)
1453 return FAIL;
1454
1455 // Disallow changing some options from modelines.
1456 if (opt_flags & OPT_MODELINE)
1457 {
1458 if (flags & (P_SECURE | P_NO_ML))
1459 {
1460 *errmsg = e_not_allowed_in_modeline;
1461 return FAIL;
1462 }
1463 if ((flags & P_MLE) && !p_mle)
1464 {
1465 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1466 return FAIL;
1467 }
1468#ifdef FEAT_DIFF
1469 // In diff mode some options are overruled. This avoids that
1470 // 'foldmethod' becomes "marker" instead of "diff" and that
1471 // "wrap" gets set.
1472 if (curwin->w_p_diff
1473 && opt_idx >= 0 // shut up coverity warning
1474 && (
1475# ifdef FEAT_FOLDING
1476 options[opt_idx].indir == PV_FDM ||
1477# endif
1478 options[opt_idx].indir == PV_WRAP))
1479 return FAIL;
1480#endif
1481 }
1482
1483#ifdef HAVE_SANDBOX
1484 // Disallow changing some options in the sandbox
1485 if (sandbox != 0 && (flags & P_SECURE))
1486 {
1487 *errmsg = e_not_allowed_in_sandbox;
1488 return FAIL;
1489 }
1490#endif
1491
1492 return OK;
1493}
1494
Bram Moolenaar47403942022-09-21 21:12:53 +01001495/*
1496 * Part of do_set() for string options.
1497 * Returns FAIL on failure, do not process further options.
1498 */
1499 static int
1500do_set_string(
1501 int opt_idx,
1502 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001503 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001504 int nextchar,
1505 set_op_T op_arg,
1506 int flags,
1507 int cp_val,
1508 char_u *varp_arg,
1509 char *errbuf,
1510 int *value_checked,
1511 char **errmsg)
1512{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001513 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001514 set_op_T op = op_arg;
1515 char_u *varp = varp_arg;
1516 char_u *save_arg = NULL;
1517 char_u *s = NULL;
1518 char_u *oldval = NULL; // previous value if *varp
1519 char_u *newval;
1520 char_u *origval = NULL;
1521 char_u *origval_l = NULL;
1522 char_u *origval_g = NULL;
1523#if defined(FEAT_EVAL)
1524 char_u *saved_origval = NULL;
1525 char_u *saved_origval_l = NULL;
1526 char_u *saved_origval_g = NULL;
1527 char_u *saved_newval = NULL;
1528#endif
1529 unsigned newlen;
1530 int comma;
1531 char_u whichwrap[80];
1532
1533 // When using ":set opt=val" for a global option
1534 // with a local value the local value will be
1535 // reset, use the global value here.
1536 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1537 && ((int)options[opt_idx].indir & PV_BOTH))
1538 varp = options[opt_idx].var;
1539
1540 // The old value is kept until we are sure that the new value is valid.
1541 oldval = *(char_u **)varp;
1542
1543 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1544 {
1545 origval_l = *(char_u **)get_varp_scope(
1546 &(options[opt_idx]), OPT_LOCAL);
1547 origval_g = *(char_u **)get_varp_scope(
1548 &(options[opt_idx]), OPT_GLOBAL);
1549
1550 // A global-local string option might have an empty option as value to
1551 // indicate that the global value should be used.
1552 if (((int)options[opt_idx].indir & PV_BOTH)
1553 && origval_l == empty_option)
1554 origval_l = origval_g;
1555 }
1556
1557 // When setting the local value of a global option, the old value may be
1558 // the global value.
1559 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1560 origval = *(char_u **)get_varp(&options[opt_idx]);
1561 else
1562 origval = oldval;
1563
1564 if (nextchar == '&') // set to default val
1565 {
1566 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1567 ? VI_DEFAULT : VIM_DEFAULT];
1568 if ((char_u **)varp == &p_bg)
1569 {
1570 // guess the value of 'background'
1571#ifdef FEAT_GUI
1572 if (gui.in_use)
1573 newval = gui_bg_default();
1574 else
1575#endif
1576 newval = term_bg_default();
1577 }
1578 else if ((char_u **)varp == &p_fencs && enc_utf8)
1579 newval = fencs_utf8_default;
1580
1581 // expand environment variables and ~ since the default value was
1582 // already expanded, only required when an environment variable was set
1583 // later
1584 if (newval == NULL)
1585 newval = empty_option;
1586 else
1587 {
1588 s = option_expand(opt_idx, newval);
1589 if (s == NULL)
1590 s = newval;
1591 newval = vim_strsave(s);
1592 }
1593 }
1594 else if (nextchar == '<') // set to global val
1595 {
1596 newval = vim_strsave(*(char_u **)get_varp_scope(
1597 &(options[opt_idx]), OPT_GLOBAL));
1598 }
1599 else
1600 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001601 ++arg; // jump to after the '=' or ':'
Bram Moolenaar47403942022-09-21 21:12:53 +01001602
1603 /*
1604 * Set 'keywordprg' to ":help" if an empty
1605 * value was passed to :set by the user.
Bram Moolenaar47403942022-09-21 21:12:53 +01001606 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001607 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
Bram Moolenaar47403942022-09-21 21:12:53 +01001608 {
Bram Moolenaar6f981142022-09-22 12:48:58 +01001609 save_arg = arg;
zeertzjqfcba86c2022-09-22 13:57:32 +01001610 arg = (char_u *)":help";
Bram Moolenaar47403942022-09-21 21:12:53 +01001611 }
1612 /*
1613 * Convert 'backspace' number to string, for
1614 * adding, prepending and removing string.
1615 */
1616 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1617 {
1618 int i = getdigits((char_u **)varp);
1619
1620 switch (i)
1621 {
1622 case 0:
1623 *(char_u **)varp = empty_option;
1624 break;
1625 case 1:
1626 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1627 break;
1628 case 2:
1629 *(char_u **)varp = vim_strsave(
1630 (char_u *)"indent,eol,start");
1631 break;
1632 case 3:
1633 *(char_u **)varp = vim_strsave(
1634 (char_u *)"indent,eol,nostop");
1635 break;
1636 }
1637 vim_free(oldval);
1638 if (origval == oldval)
1639 origval = *(char_u **)varp;
1640 if (origval_l == oldval)
1641 origval_l = *(char_u **)varp;
1642 if (origval_g == oldval)
1643 origval_g = *(char_u **)varp;
1644 oldval = *(char_u **)varp;
1645 }
1646 /*
1647 * Convert 'whichwrap' number to string, for backwards compatibility
1648 * with Vim 3.0.
1649 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001650 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001651 {
1652 *whichwrap = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001653 int i = getdigits(&arg);
Bram Moolenaar47403942022-09-21 21:12:53 +01001654 if (i & 1)
1655 STRCAT(whichwrap, "b,");
1656 if (i & 2)
1657 STRCAT(whichwrap, "s,");
1658 if (i & 4)
1659 STRCAT(whichwrap, "h,l,");
1660 if (i & 8)
1661 STRCAT(whichwrap, "<,>,");
1662 if (i & 16)
1663 STRCAT(whichwrap, "[,],");
1664 if (*whichwrap != NUL) // remove trailing ,
1665 whichwrap[STRLEN(whichwrap) - 1] = NUL;
Bram Moolenaar6f981142022-09-22 12:48:58 +01001666 save_arg = arg;
1667 arg = (char_u *)whichwrap;
Bram Moolenaar47403942022-09-21 21:12:53 +01001668 }
1669 /*
1670 * Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1671 * version 3.0
1672 */
Bram Moolenaar6f981142022-09-22 12:48:58 +01001673 else if (*arg == '>' && (varp == (char_u *)&p_dir
Bram Moolenaar47403942022-09-21 21:12:53 +01001674 || varp == (char_u *)&p_bdir))
Bram Moolenaar6f981142022-09-22 12:48:58 +01001675 ++arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001676
1677 /*
1678 * Copy the new string into allocated memory.
1679 * Can't use set_string_option_direct(), because we need to remove the
1680 * backslashes.
1681 */
1682 // get a bit too much
Bram Moolenaar6f981142022-09-22 12:48:58 +01001683 newlen = (unsigned)STRLEN(arg) + 1;
Bram Moolenaar47403942022-09-21 21:12:53 +01001684 if (op != OP_NONE)
1685 newlen += (unsigned)STRLEN(origval) + 1;
1686 newval = alloc(newlen);
1687 if (newval == NULL) // out of mem, don't change
1688 return FAIL;
1689 s = newval;
1690
1691 /*
1692 * Copy the string, skip over escaped chars.
1693 * For MS-DOS and WIN32 backslashes before normal file name characters
1694 * are not removed, and keep backslash at start, for "\\machine\path",
1695 * but do remove it for "\\\\machine\\path".
1696 * The reverse is found in ExpandOldSetting().
1697 */
zeertzjqfcba86c2022-09-22 13:57:32 +01001698 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar47403942022-09-21 21:12:53 +01001699 {
1700 int i;
1701
Bram Moolenaar6f981142022-09-22 12:48:58 +01001702 if (*arg == '\\' && arg[1] != NUL
Bram Moolenaar47403942022-09-21 21:12:53 +01001703#ifdef BACKSLASH_IN_FILENAME
1704 && !((flags & P_EXPAND)
Bram Moolenaar6f981142022-09-22 12:48:58 +01001705 && vim_isfilec(arg[1])
1706 && !VIM_ISWHITE(arg[1])
1707 && (arg[1] != '\\'
zeertzjqfcba86c2022-09-22 13:57:32 +01001708 || (s == newval && arg[2] != '\\')))
Bram Moolenaar47403942022-09-21 21:12:53 +01001709#endif
1710 )
Bram Moolenaar6f981142022-09-22 12:48:58 +01001711 ++arg; // remove backslash
1712 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar47403942022-09-21 21:12:53 +01001713 {
1714 // copy multibyte char
Bram Moolenaar6f981142022-09-22 12:48:58 +01001715 mch_memmove(s, arg, (size_t)i);
1716 arg += i;
Bram Moolenaar47403942022-09-21 21:12:53 +01001717 s += i;
1718 }
1719 else
Bram Moolenaar6f981142022-09-22 12:48:58 +01001720 *s++ = *arg++;
Bram Moolenaar47403942022-09-21 21:12:53 +01001721 }
1722 *s = NUL;
1723
1724 /*
1725 * Expand environment variables and ~.
1726 * Don't do it when adding without inserting a comma.
1727 */
1728 if (op == OP_NONE || (flags & P_COMMA))
1729 {
1730 s = option_expand(opt_idx, newval);
1731 if (s != NULL)
1732 {
1733 vim_free(newval);
1734 newlen = (unsigned)STRLEN(s) + 1;
1735 if (op != OP_NONE)
1736 newlen += (unsigned)STRLEN(origval) + 1;
1737 newval = alloc(newlen);
1738 if (newval == NULL)
1739 return FAIL;
1740 STRCPY(newval, s);
1741 }
1742 }
1743
1744 // locate newval[] in origval[] when removing it
1745 // and when adding to avoid duplicates
1746 int len = 0;
1747 if (op == OP_REMOVING || (flags & P_NODUP))
1748 {
1749 len = (int)STRLEN(newval);
1750 s = find_dup_item(origval, newval, flags);
1751
1752 // do not add if already there
1753 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1754 {
1755 op = OP_NONE;
1756 STRCPY(newval, origval);
1757 }
1758
1759 // if no duplicate, move pointer to end of original value
1760 if (s == NULL)
1761 s = origval + (int)STRLEN(origval);
1762 }
1763
1764 // concatenate the two strings; add a ',' if needed
1765 if (op == OP_ADDING || op == OP_PREPENDING)
1766 {
1767 comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1768 if (op == OP_ADDING)
1769 {
1770 len = (int)STRLEN(origval);
1771 // strip a trailing comma, would get 2
1772 if (comma && len > 1
1773 && (flags & P_ONECOMMA) == P_ONECOMMA
1774 && origval[len - 1] == ','
1775 && origval[len - 2] != '\\')
1776 len--;
1777 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1778 mch_memmove(newval, origval, (size_t)len);
1779 }
1780 else
1781 {
1782 len = (int)STRLEN(newval);
1783 STRMOVE(newval + len + comma, origval);
1784 }
1785 if (comma)
1786 newval[len] = ',';
1787 }
1788
1789 // Remove newval[] from origval[]. (Note: "len" has been set above and
1790 // is used here).
1791 if (op == OP_REMOVING)
1792 {
1793 STRCPY(newval, origval);
1794 if (*s)
1795 {
1796 // may need to remove a comma
1797 if (flags & P_COMMA)
1798 {
1799 if (s == origval)
1800 {
1801 // include comma after string
1802 if (s[len] == ',')
1803 ++len;
1804 }
1805 else
1806 {
1807 // include comma before string
1808 --s;
1809 ++len;
1810 }
1811 }
1812 STRMOVE(newval + (s - origval), s + len);
1813 }
1814 }
1815
1816 if (flags & P_FLAGLIST)
1817 {
1818 // Remove flags that appear twice.
1819 for (s = newval; *s;)
1820 {
1821 // if options have P_FLAGLIST and P_ONECOMMA such as
1822 // 'whichwrap'
1823 if (flags & P_ONECOMMA)
1824 {
1825 if (*s != ',' && *(s + 1) == ','
1826 && vim_strchr(s + 2, *s) != NULL)
1827 {
1828 // Remove the duplicated value and the next comma.
1829 STRMOVE(s, s + 2);
1830 continue;
1831 }
1832 }
1833 else
1834 {
1835 if ((!(flags & P_COMMA) || *s != ',')
1836 && vim_strchr(s + 1, *s) != NULL)
1837 {
1838 STRMOVE(s, s + 1);
1839 continue;
1840 }
1841 }
1842 ++s;
1843 }
1844 }
1845
zeertzjqfcba86c2022-09-22 13:57:32 +01001846 if (save_arg != NULL)
1847 arg = save_arg; // arg was temporarily changed, restore it
Bram Moolenaar47403942022-09-21 21:12:53 +01001848 }
1849
1850 /*
1851 * Set the new value.
1852 */
1853 *(char_u **)(varp) = newval;
1854
1855#if defined(FEAT_EVAL)
1856 if (!starting
1857# ifdef FEAT_CRYPT
1858 && options[opt_idx].indir != PV_KEY
1859# endif
1860 && origval != NULL && newval != NULL)
1861 {
1862 // origval may be freed by did_set_string_option(), make a copy.
1863 saved_origval = vim_strsave(origval);
1864 // newval (and varp) may become invalid if the buffer is closed by
1865 // autocommands.
1866 saved_newval = vim_strsave(newval);
1867 if (origval_l != NULL)
1868 saved_origval_l = vim_strsave(origval_l);
1869 if (origval_g != NULL)
1870 saved_origval_g = vim_strsave(origval_g);
1871 }
1872#endif
1873
1874 {
1875 long_u *p = insecure_flag(opt_idx, opt_flags);
1876 int secure_saved = secure;
1877
1878 // When an option is set in the sandbox, from a modeline or in secure
1879 // mode, then deal with side effects in secure mode. Also when the
1880 // value was set with the P_INSECURE flag and is not completely
1881 // replaced.
1882 if ((opt_flags & OPT_MODELINE)
1883#ifdef HAVE_SANDBOX
1884 || sandbox != 0
1885#endif
1886 || (op != OP_NONE && (*p & P_INSECURE)))
1887 secure = 1;
1888
1889 // Handle side effects, and set the global value for ":set" on local
1890 // options. Note: when setting 'syntax' or 'filetype' autocommands may
1891 // be triggered that can cause havoc.
1892 *errmsg = did_set_string_option(
1893 opt_idx, (char_u **)varp, oldval, errbuf,
1894 opt_flags, value_checked);
1895
1896 secure = secure_saved;
1897 }
1898
1899#if defined(FEAT_EVAL)
1900 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00001901 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01001902 saved_origval_l, saved_origval_g, saved_newval);
1903 vim_free(saved_origval);
1904 vim_free(saved_origval_l);
1905 vim_free(saved_origval_g);
1906 vim_free(saved_newval);
1907#endif
1908
Bram Moolenaar6f981142022-09-22 12:48:58 +01001909 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001910 return *errmsg == NULL ? OK : FAIL;
1911}
1912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00001914 * Set a boolean option.
1915 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001916 */
1917 static char *
1918do_set_option_bool(
1919 int opt_idx,
1920 int opt_flags,
1921 set_prefix_T prefix,
1922 long_u flags,
1923 char_u *varp,
1924 int nextchar,
1925 int afterchar,
1926 int cp_val)
1927
1928{
1929 varnumber_T value;
1930
1931 if (nextchar == '=' || nextchar == ':')
1932 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00001933 if (opt_idx < 0 || varp == NULL)
1934 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001935
1936 /*
1937 * ":set opt!": invert
1938 * ":set opt&": reset to default value
1939 * ":set opt<": reset to global value
1940 */
1941 if (nextchar == '!')
1942 value = *(int *)(varp) ^ 1;
1943 else if (nextchar == '&')
1944 value = (int)(long)(long_i)options[opt_idx].def_val[
1945 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
1946 else if (nextchar == '<')
1947 {
1948 // For 'autoread' -1 means to use global value.
1949 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
1950 value = -1;
1951 else
1952 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
1953 }
1954 else
1955 {
1956 /*
1957 * ":set invopt": invert
1958 * ":set opt" or ":set noopt": set or reset
1959 */
1960 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
1961 return e_trailing_characters;
1962 if (prefix == PREFIX_INV)
1963 value = *(int *)(varp) ^ 1;
1964 else
1965 value = prefix == PREFIX_NO ? 0 : 1;
1966 }
1967
1968 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
1969}
1970
1971/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00001972 * Set a numeric option.
1973 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001974 */
1975 static char *
1976do_set_option_numeric(
1977 int opt_idx,
1978 int opt_flags,
1979 char_u **argp,
1980 int nextchar,
1981 set_op_T op,
1982 long_u flags,
1983 int cp_val,
1984 char_u *varp,
1985 char *errbuf,
1986 size_t errbuflen)
1987{
1988 char_u *arg = *argp;
1989 varnumber_T value;
1990 int i;
1991 char *errmsg = NULL;
1992
Bram Moolenaar546933f2023-02-06 16:40:49 +00001993 if (opt_idx < 0 || varp == NULL)
1994 return NULL; // "cannot happen"
1995 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001996 /*
1997 * Different ways to set a number option:
1998 * & set to default value
1999 * < set to global value
2000 * <xx> accept special key codes for 'wildchar'
2001 * c accept any non-digit for 'wildchar'
2002 * [-]0-9 set number
2003 * other error
2004 */
2005 ++arg;
2006 if (nextchar == '&')
2007 value = (long)(long_i)options[opt_idx].def_val[
2008 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2009 else if (nextchar == '<')
2010 {
2011 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2012 // use the global value.
2013 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2014 value = NO_LOCAL_UNDOLEVEL;
2015 else
2016 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2017 }
2018 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2019 && (*arg == '<'
2020 || *arg == '^'
2021 || (*arg != NUL
2022 && (!arg[1] || VIM_ISWHITE(arg[1]))
2023 && !VIM_ISDIGIT(*arg))))
2024 {
2025 value = string_to_key(arg, FALSE);
2026 if (value == 0 && (long *)varp != &p_wcm)
2027 {
2028 errmsg = e_invalid_argument;
2029 goto skip;
2030 }
2031 }
2032 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2033 {
2034 // Allow negative (for 'undolevels'), octal and hex numbers.
2035 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
2036 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2037 {
2038 errmsg = e_number_required_after_equal;
2039 goto skip;
2040 }
2041 }
2042 else
2043 {
2044 errmsg = e_number_required_after_equal;
2045 goto skip;
2046 }
2047
2048 if (op == OP_ADDING)
2049 value = *(long *)varp + value;
2050 else if (op == OP_PREPENDING)
2051 value = *(long *)varp * value;
2052 else if (op == OP_REMOVING)
2053 value = *(long *)varp - value;
2054
2055 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2056 opt_flags);
2057
2058skip:
2059 *argp = arg;
2060 return errmsg;
2061}
2062
2063/*
2064 * Set a key code (t_xx) option
2065 */
2066 static char *
2067do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2068{
2069 char_u *arg = *argp;
2070 char_u *p;
2071
2072 if (nextchar == '&')
2073 {
2074 if (add_termcap_entry(key_name, TRUE) == FAIL)
2075 return e_not_found_in_termcap;
2076 }
2077 else
2078 {
2079 ++arg; // jump to after the '=' or ':'
2080 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2081 if (*p == '\\' && p[1] != NUL)
2082 ++p;
2083 nextchar = *p;
2084 *p = NUL;
2085 add_termcode(key_name, arg, FALSE);
2086 *p = nextchar;
2087 }
2088 if (full_screen)
2089 ttest(FALSE);
2090 redraw_all_later(UPD_CLEAR);
2091
2092 *argp = arg;
2093 return NULL;
2094}
2095
2096/*
2097 * Set an option to a new value.
2098 */
2099 static char *
2100do_set_option_value(
2101 int opt_idx,
2102 int opt_flags,
2103 char_u **argp,
2104 set_prefix_T prefix,
2105 set_op_T op,
2106 long_u flags,
2107 char_u *varp,
2108 char_u *key_name,
2109 int nextchar,
2110 int afterchar,
2111 int cp_val,
2112 int *stopopteval,
2113 char *errbuf,
2114 size_t errbuflen)
2115{
2116 int value_checked = FALSE;
2117 char *errmsg = NULL;
2118 char_u *arg = *argp;
2119
2120 if (flags & P_BOOL)
2121 {
2122 // boolean option
2123 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2124 nextchar, afterchar, cp_val);
2125 if (errmsg != NULL)
2126 goto skip;
2127 }
2128 else
2129 {
2130 // numeric or string option
2131 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2132 || prefix != PREFIX_NONE)
2133 {
2134 errmsg = e_invalid_argument;
2135 goto skip;
2136 }
2137
2138 if (flags & P_NUM)
2139 {
2140 // numeric option
2141 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2142 op, flags, cp_val, varp,
2143 errbuf, errbuflen);
2144 if (errmsg != NULL)
2145 goto skip;
2146 }
2147 else if (opt_idx >= 0)
2148 {
2149 // string option
2150 if (do_set_string(opt_idx, opt_flags, &arg, nextchar, op, flags,
2151 cp_val, varp, errbuf, &value_checked,
2152 &errmsg) == FAIL)
2153 {
2154 if (errmsg != NULL)
2155 goto skip;
2156 *stopopteval = TRUE;
2157 goto skip;
2158 }
2159 }
2160 else
2161 {
2162 // key code option
2163 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2164 if (errmsg != NULL)
2165 goto skip;
2166 }
2167 }
2168
2169 if (opt_idx >= 0)
2170 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2171
2172skip:
2173 *argp = arg;
2174 return errmsg;
2175}
2176
2177/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002178 * Set an option to a new value.
2179 * Return NULL if OK, return an untranslated error message when something is
2180 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2181 */
2182 static char *
2183do_set_option(
2184 int opt_flags,
2185 char_u **argp,
2186 char_u *arg_start,
2187 char_u **startarg,
2188 int *did_show,
2189 int *stopopteval,
2190 char *errbuf,
2191 size_t errbuflen)
2192{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002193 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002194 char_u *arg;
2195 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2196 set_op_T op;
2197 long_u flags; // flags for current option
2198 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002199 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002200 int nextchar; // next non-white char after option name
2201 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002202 char *errmsg = NULL;
2203 int key;
2204 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002205
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002206 prefix = get_option_prefix(argp);
2207 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002208
2209 // find end of name
2210 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002211 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2212 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002213
2214 // remember character after option name
2215 afterchar = arg[len];
2216
2217 if (in_vim9script())
2218 {
2219 char_u *p = skipwhite(arg + len);
2220
2221 // disallow white space before =val, +=val, -=val, ^=val
2222 if (p > arg + len && (p[0] == '='
2223 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2224 && p[1] == '=')))
2225 {
2226 errmsg = e_no_white_space_allowed_between_option_and;
2227 arg = p;
2228 *startarg = p;
2229 goto skip;
2230 }
2231 }
2232 else
2233 // skip white space, allow ":set ai ?", ":set hlsearch !"
2234 while (VIM_ISWHITE(arg[len]))
2235 ++len;
2236
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002237 op = get_opt_op(arg + len);
2238 if (op != OP_NONE)
2239 len++;
2240
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002241 nextchar = arg[len];
2242
2243 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2244 {
2245 if (in_vim9script() && arg > arg_start
2246 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2247 errmsg = e_no_white_space_allowed_between_option_and;
2248 else
2249 errmsg = e_unknown_option;
2250 goto skip;
2251 }
2252
2253 if (opt_idx >= 0)
2254 {
2255 if (options[opt_idx].var == NULL) // hidden option: skip
2256 {
2257 // Only give an error message when requesting the value of
2258 // a hidden option, ignore setting it.
2259 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2260 && (!(options[opt_idx].flags & P_BOOL)
2261 || nextchar == '?'))
2262 errmsg = e_option_not_supported;
2263 goto skip;
2264 }
2265
2266 flags = options[opt_idx].flags;
2267 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2268 }
2269 else
2270 {
2271 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002272 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002273 if (key < 0)
2274 {
2275 key_name[0] = KEY2TERMCAP0(key);
2276 key_name[1] = KEY2TERMCAP1(key);
2277 }
2278 else
2279 {
2280 key_name[0] = KS_KEY;
2281 key_name[1] = (key & 0xff);
2282 }
2283 }
2284
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002285 // Make sure the option value can be changed.
2286 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002287 goto skip;
2288
Bram Moolenaar40b48722023-02-05 17:04:50 +00002289 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002290 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2291 {
2292 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002293 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2294 {
2295 if (arg[3] == 'm') // "opt&vim": set to Vim default
2296 {
2297 cp_val = FALSE;
2298 arg += 3;
2299 }
2300 else // "opt&vi": set to Vi default
2301 {
2302 cp_val = TRUE;
2303 arg += 2;
2304 }
2305 }
2306 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2307 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2308 {
2309 errmsg = e_trailing_characters;
2310 goto skip;
2311 }
2312 }
2313
2314 /*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002315 * Allow '=' and ':' for historical reasons (MSDOS command.com).
2316 * Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002317 */
2318 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002319 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002320 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2321 && !(flags & P_BOOL)))
2322 {
2323 /*
2324 * print value
2325 */
2326 if (*did_show)
2327 msg_putchar('\n'); // cursor below last one
2328 else
2329 {
2330 gotocmdline(TRUE); // cursor at status line
2331 *did_show = TRUE; // remember that we did a line
2332 }
2333 if (opt_idx >= 0)
2334 {
2335 showoneopt(&options[opt_idx], opt_flags);
2336#ifdef FEAT_EVAL
2337 if (p_verbose > 0)
2338 {
2339 // Mention where the option was last set.
2340 if (varp == options[opt_idx].var)
2341 last_set_msg(options[opt_idx].script_ctx);
2342 else if ((int)options[opt_idx].indir & PV_WIN)
2343 last_set_msg(curwin->w_p_script_ctx[
2344 (int)options[opt_idx].indir & PV_MASK]);
2345 else if ((int)options[opt_idx].indir & PV_BUF)
2346 last_set_msg(curbuf->b_p_script_ctx[
2347 (int)options[opt_idx].indir & PV_MASK]);
2348 }
2349#endif
2350 }
2351 else
2352 {
2353 char_u *p;
2354
2355 p = find_termcode(key_name);
2356 if (p == NULL)
2357 {
2358 errmsg = e_key_code_not_set;
2359 goto skip;
2360 }
2361 else
2362 (void)show_one_termcode(key_name, p, TRUE);
2363 }
2364 if (nextchar != '?'
2365 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2366 errmsg = e_trailing_characters;
2367 }
2368 else
2369 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002370 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2371 flags, varp, key_name, nextchar,
2372 afterchar, cp_val, stopopteval, errbuf,
2373 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002374 }
2375
2376skip:
2377 *argp = arg;
2378 return errmsg;
2379}
2380
2381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 * Parse 'arg' for option settings.
2383 *
2384 * 'arg' may be IObuff, but only when no errors can be present and option
2385 * does not need to be expanded with option_expand().
2386 * "opt_flags":
2387 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002388 * OPT_GLOBAL for ":setglobal"
2389 * OPT_LOCAL for ":setlocal" and a modeline
2390 * OPT_MODELINE for a modeline
2391 * OPT_WINONLY to only set window-local options
2392 * OPT_NOWIN to skip setting window-local options
2393 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002395 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002398do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002399 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002400 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002402 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002404 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 if (*arg == NUL)
2407 {
2408 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002409 did_show = TRUE;
2410 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002413 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002415 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002416 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 /*
2419 * ":set all" show all options.
2420 * ":set all&" set all options to their default value.
2421 */
2422 arg += 3;
2423 if (*arg == '&')
2424 {
2425 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002426 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002428 didset_options();
2429 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002430 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002433 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002435 did_show = TRUE;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002438 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002441 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002442 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 arg += 7;
2444 }
2445 else
2446 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002447 int stopopteval = FALSE;
2448 char *errmsg = NULL;
2449 char errbuf[80];
2450 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002452 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2453 &did_show, &stopopteval, errbuf,
2454 sizeof(errbuf));
2455 if (stopopteval)
2456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 /*
2459 * Advance to next argument.
2460 * - skip until a blank found, taking care of backslashes
2461 * - skip blanks
2462 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2463 */
2464 for (i = 0; i < 2 ; ++i)
2465 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002466 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (*arg++ == '\\' && *arg != NUL)
2468 ++arg;
2469 arg = skipwhite(arg);
2470 if (*arg != '=')
2471 break;
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002474 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002476 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2477 i = (int)STRLEN(IObuff) + 2;
2478 if (i + (arg - startarg) < IOSIZE)
2479 {
2480 // append the argument with the error
2481 STRCAT(IObuff, ": ");
2482 mch_memmove(IObuff + i, startarg, (arg - startarg));
2483 IObuff[i + (arg - startarg)] = NUL;
2484 }
2485 // make sure all characters are printable
2486 trans_characters(IObuff, IOSIZE);
2487
2488 ++no_wait_return; // wait_return() done later
2489 emsg((char *)IObuff); // show error highlighted
2490 --no_wait_return;
2491
2492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496 arg = skipwhite(arg);
2497 }
2498
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002499theend:
2500 if (silent_mode && did_show)
2501 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002502 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002503 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002504 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002505 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002506 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002507 out_flush();
2508 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002509 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002510 }
2511
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 return OK;
2513}
2514
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002515/*
2516 * Call this when an option has been given a new value through a user command.
2517 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2518 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002519 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002520did_set_option(
2521 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002522 int opt_flags, // possibly with OPT_MODELINE
2523 int new_value, // value was replaced completely
2524 int value_checked) // value was checked to be safe, no need to set the
2525 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002526{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002527 long_u *p;
2528
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002529 options[opt_idx].flags |= P_WAS_SET;
2530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 // When an option is set in the sandbox, from a modeline or in secure mode
2532 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2533 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002535 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002536#ifdef HAVE_SANDBOX
2537 || sandbox != 0
2538#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002539 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540 *p = *p | P_INSECURE;
2541 else if (new_value)
2542 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002543}
2544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545/*
2546 * Convert a key name or string into a key value.
2547 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002548 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002550 int
2551string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002554 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (*arg == '^')
2556 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002557 if (multi_byte)
2558 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 return *arg;
2560}
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562/*
2563 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2564 * maketitle() to create and display it.
2565 * When switching the title or icon off, call mch_restore_title() to get
2566 * the old value back.
2567 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002568 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002569did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 if (starting != NO_SCREEN
2572#ifdef FEAT_GUI
2573 && !gui.starting
2574#endif
2575 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579/*
2580 * set_options_bin - called when 'bin' changes value.
2581 */
2582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002583set_options_bin(
2584 int oldval,
2585 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002586 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
2588 /*
2589 * The option values that are changed when 'bin' changes are
2590 * copied when 'bin is set and restored when 'bin' is reset.
2591 */
2592 if (newval)
2593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002594 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
2596 if (!(opt_flags & OPT_GLOBAL))
2597 {
2598 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2599 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2600 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2601 curbuf->b_p_et_nobin = curbuf->b_p_et;
2602 }
2603 if (!(opt_flags & OPT_LOCAL))
2604 {
2605 p_tw_nobin = p_tw;
2606 p_wm_nobin = p_wm;
2607 p_ml_nobin = p_ml;
2608 p_et_nobin = p_et;
2609 }
2610 }
2611
2612 if (!(opt_flags & OPT_GLOBAL))
2613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002614 curbuf->b_p_tw = 0; // no automatic line wrap
2615 curbuf->b_p_wm = 0; // no automatic line wrap
2616 curbuf->b_p_ml = 0; // no modelines
2617 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
2619 if (!(opt_flags & OPT_LOCAL))
2620 {
2621 p_tw = 0;
2622 p_wm = 0;
2623 p_ml = FALSE;
2624 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002628 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 if (!(opt_flags & OPT_GLOBAL))
2631 {
2632 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2633 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2634 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2635 curbuf->b_p_et = curbuf->b_p_et_nobin;
2636 }
2637 if (!(opt_flags & OPT_LOCAL))
2638 {
2639 p_tw = p_tw_nobin;
2640 p_wm = p_wm_nobin;
2641 p_ml = p_ml_nobin;
2642 p_et = p_et_nobin;
2643 }
2644 }
2645}
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647/*
2648 * Expand environment variables for some string options.
2649 * These string options cannot be indirect!
2650 * If "val" is NULL expand the current value of the option.
2651 * Return pointer to NameBuff, or NULL when not expanded.
2652 */
2653 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002654option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002656 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2658 return NULL;
2659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002660 // If val is longer than MAXPATHL no meaningful expansion can be done,
2661 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (val != NULL && STRLEN(val) > MAXPATHL)
2663 return NULL;
2664
2665 if (val == NULL)
2666 val = *(char_u **)options[opt_idx].var;
2667
2668 /*
2669 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2670 * Escape spaces when expanding 'tags', they are used to separate file
2671 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002672 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
2674 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002675 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002676#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002677 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2678#endif
2679 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002680 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 return NULL;
2682
2683 return NameBuff;
2684}
2685
2686/*
2687 * After setting various option values: recompute variables that depend on
2688 * option values.
2689 */
2690 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002691didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 (void)init_chartab();
2695
Bram Moolenaardac13472019-09-16 21:06:21 +02002696 didset_string_options();
2697
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002698#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002699 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002700 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002701 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002702 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002703#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002704 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002706#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002707 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002708 fill_breakat_flags();
2709#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002710 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002711}
2712
2713/*
2714 * More side effects of setting options.
2715 */
2716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002717didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002718{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002720 (void)highlight_changed();
2721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002723 check_opt_wim();
2724
Bram Moolenaareed9d462021-02-15 20:38:25 +01002725 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002726 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002727
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002729 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002730
2731#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002733 (void)check_clipboard_option();
2734#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002735#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002736 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002737 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002738 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002739 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741}
2742
2743/*
2744 * Check for string options that are NULL (normally only termcap options).
2745 */
2746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002747check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
2749 int opt_idx;
2750
2751 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2752 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2753 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2754}
2755
2756/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002757 * Return the option index found by a pointer into term_strings[].
2758 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002760 int
2761get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002763 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2766 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002767 return opt_idx;
2768 return -1; // cannot happen: didn't find it!
2769}
2770
2771/*
2772 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2773 * Return the option index or -1 if not found.
2774 */
2775 int
2776set_term_option_alloced(char_u **p)
2777{
2778 int opt_idx = get_term_opt_idx(p);
2779
2780 if (opt_idx >= 0)
2781 options[opt_idx].flags |= P_ALLOCED;
2782 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002785#if defined(FEAT_EVAL) || defined(PROTO)
2786/*
2787 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2788 * Return FALSE when it wasn't.
2789 * Return -1 for an unknown option.
2790 */
2791 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002792was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002793{
2794 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002795 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002796
2797 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002798 {
2799 flagp = insecure_flag(idx, opt_flags);
2800 return (*flagp & P_INSECURE) != 0;
2801 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002802 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002803 return -1;
2804}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002805
2806/*
2807 * Get a pointer to the flags used for the P_INSECURE flag of option
2808 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002809 * NOTE: Caller must make sure that "curwin" is set to the window from which
2810 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002811 */
2812 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002813insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002814{
2815 if (opt_flags & OPT_LOCAL)
2816 switch ((int)options[opt_idx].indir)
2817 {
2818#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002819 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002820#endif
2821#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002822# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002823 case PV_FDE: return &curwin->w_p_fde_flags;
2824 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002825# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002826# ifdef FEAT_BEVAL
2827 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2828# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002829 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002830 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002831# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002832 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002833# endif
2834#endif
2835 }
2836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002838 return &options[opt_idx].flags;
2839}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002840#endif
2841
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002842/*
2843 * Redraw the window title and/or tab page text later.
2844 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002845void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002846{
2847 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002848 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002849}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002850
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002852 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2853 * characters or characters in "allowed".
2854 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002855 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002856valid_name(char_u *val, char *allowed)
2857{
2858 char_u *s;
2859
2860 for (s = val; *s != NUL; ++s)
2861 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2862 return FALSE;
2863 return TRUE;
2864}
2865
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002866#if defined(FEAT_EVAL) || defined(PROTO)
2867/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002868 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002869 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002870 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002871 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002872set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002873{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002874 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2875 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002876 sctx_T new_script_ctx = script_ctx;
2877
Bram Moolenaar51258742020-05-03 17:19:33 +02002878 // Modeline already has the line number set.
2879 if (!(opt_flags & OPT_MODELINE))
2880 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // Remember where the option was set. For local options need to do that
2883 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002884 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002885 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002886 if (both || (opt_flags & OPT_LOCAL))
2887 {
2888 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002889 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002890 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002891 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002892 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002893 if (both)
2894 // also setting the "all buffers" value
2895 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2896 new_script_ctx;
2897 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002898 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002899}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002900
2901/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002902 * Get the script context of global option "name".
2903 *
2904 */
2905 sctx_T *
2906get_option_sctx(char *name)
2907{
2908 int idx = findoption((char_u *)name);
2909
2910 if (idx >= 0)
2911 return &options[idx].script_ctx;
2912 siemsg("no such option: %s", name);
2913 return NULL;
2914}
2915
2916/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002917 * Set the script_ctx for a termcap option.
2918 * "name" must be the two character code, e.g. "RV".
2919 * When "name" is NULL use "opt_idx".
2920 */
2921 void
2922set_term_option_sctx_idx(char *name, int opt_idx)
2923{
2924 char_u buf[5];
2925 int idx;
2926
2927 if (name == NULL)
2928 idx = opt_idx;
2929 else
2930 {
2931 buf[0] = 't';
2932 buf[1] = '_';
2933 buf[2] = name[0];
2934 buf[3] = name[1];
2935 buf[4] = 0;
2936 idx = findoption(buf);
2937 }
2938 if (idx >= 0)
2939 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2940}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002941#endif
2942
Bram Moolenaarf4140482020-02-15 23:06:45 +01002943#if defined(FEAT_EVAL)
2944/*
2945 * Apply the OptionSet autocommand.
2946 */
2947 static void
2948apply_optionset_autocmd(
2949 int opt_idx,
2950 long opt_flags,
2951 long oldval,
2952 long oldval_g,
2953 long newval,
2954 char *errmsg)
2955{
2956 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2957
2958 // Don't do this while starting up, failure or recursively.
2959 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2960 return;
2961
2962 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2963 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2964 oldval_g);
2965 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2966 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2967 (opt_flags & OPT_LOCAL) ? "local" : "global");
2968 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2969 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2970 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2971 if (opt_flags & OPT_LOCAL)
2972 {
2973 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2974 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2975 }
2976 if (opt_flags & OPT_GLOBAL)
2977 {
2978 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2979 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2980 }
2981 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2982 {
2983 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2984 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2985 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2986 }
2987 if (opt_flags & OPT_MODELINE)
2988 {
2989 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2990 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2991 }
2992 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2993 NULL, FALSE, NULL);
2994 reset_v_option_vars();
2995}
2996#endif
2997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998/*
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00002999 * Process the updated 'compatible' option value.
3000 */
3001 static void
3002did_set_compatible(void)
3003{
3004 compatible_set();
3005}
3006
3007#ifdef FEAT_LANGMAP
3008/*
3009 * Process the updated 'langremap' option value.
3010 */
3011 static void
3012did_set_langremap(void)
3013{
3014 // 'langremap' -> !'langnoremap'
3015 p_lnr = !p_lrm;
3016}
3017
3018/*
3019 * Process the updated 'langnoremap' option value.
3020 */
3021 static void
3022did_set_langnoremap(void)
3023{
3024 // 'langnoremap' -> !'langremap'
3025 p_lrm = !p_lnr;
3026}
3027#endif
3028
3029#ifdef FEAT_PERSISTENT_UNDO
3030/*
3031 * Process the updated 'undofile' option value.
3032 */
3033 static void
3034did_set_undofile(int opt_flags)
3035{
3036 // Only take action when the option was set. When reset we do not
3037 // delete the undo file, the option may be set again without making
3038 // any changes in between.
3039 if (curbuf->b_p_udf || p_udf)
3040 {
3041 char_u hash[UNDO_HASH_SIZE];
3042 buf_T *save_curbuf = curbuf;
3043
3044 FOR_ALL_BUFFERS(curbuf)
3045 {
3046 // When 'undofile' is set globally: for every buffer, otherwise
3047 // only for the current buffer: Try to read in the undofile,
3048 // if one exists, the buffer wasn't changed and the buffer was
3049 // loaded
3050 if ((curbuf == save_curbuf
3051 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
3052 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
3053 {
3054#ifdef FEAT_CRYPT
3055 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
3056 continue;
3057#endif
3058 u_compute_hash(hash);
3059 u_read_undo(NULL, hash, curbuf->b_fname);
3060 }
3061 }
3062 curbuf = save_curbuf;
3063 }
3064
3065}
3066#endif
3067
3068/*
3069 * Process the updated 'readonly' option value.
3070 */
3071 static void
3072did_set_readonly(int opt_flags)
3073{
3074 // when 'readonly' is reset globally, also reset readonlymode
3075 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
3076 readonlymode = FALSE;
3077
3078 // when 'readonly' is set may give W10 again
3079 if (curbuf->b_p_ro)
3080 curbuf->b_did_warn = FALSE;
3081
3082 redraw_titles();
3083}
3084
3085#ifdef FEAT_GUI
3086/*
3087 * Process the updated 'mousehide' option value.
3088 */
3089 static void
3090did_set_mousehide(void)
3091{
3092 if (!p_mh)
3093 gui_mch_mousehide(FALSE);
3094}
3095#endif
3096
3097/*
3098 * Process the updated 'modifiable' option value.
3099 */
3100 static char *
3101did_set_modifiable(int *doskip UNUSED)
3102{
3103 // when 'modifiable' is changed, redraw the window title
3104
3105# ifdef FEAT_TERMINAL
3106 // Cannot set 'modifiable' when in Terminal mode.
3107 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3108 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3109 {
3110 curbuf->b_p_ma = FALSE;
3111 *doskip = TRUE;
3112 return e_cannot_make_terminal_with_running_job_modifiable;
3113 }
3114# endif
3115 redraw_titles();
3116
3117 return NULL;
3118}
3119
3120/*
3121 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3122 * option value.
3123 */
3124 static void
3125did_set_eof_eol_fixeol_bomb(void)
3126{
3127 // redraw the window title and tab page text
3128 redraw_titles();
3129}
3130
3131/*
3132 * Process the updated 'binary' option value.
3133 */
3134 static void
3135did_set_binary(int opt_flags, long old_value)
3136{
3137 // when 'bin' is set also set some other options
3138 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
3139 redraw_titles();
3140}
3141
3142/*
3143 * Process the updated 'buflisted' option value.
3144 */
3145 static void
3146did_set_buflisted(long old_value)
3147{
3148 // when 'buflisted' changes, trigger autocommands
3149 if (old_value != curbuf->b_p_bl)
3150 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3151 NULL, NULL, TRUE, curbuf);
3152}
3153
3154/*
3155 * Process the updated 'swapfile' option value.
3156 */
3157 static void
3158did_set_swapfile(void)
3159{
3160 // when 'swf' is set, create swapfile, when reset remove swapfile
3161 if (curbuf->b_p_swf && p_uc)
3162 ml_open_file(curbuf); // create the swap file
3163 else
3164 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3165 // buf->b_p_swf
3166 mf_close_file(curbuf, TRUE); // remove the swap file
3167}
3168
3169/*
3170 * Process the updated 'terse' option value.
3171 */
3172 static void
3173did_set_terse(void)
3174{
3175 char_u *p;
3176
3177 // when 'terse' is set change 'shortmess'
3178 p = vim_strchr(p_shm, SHM_SEARCH);
3179
3180 // insert 's' in p_shm
3181 if (p_terse && p == NULL)
3182 {
3183 STRCPY(IObuff, p_shm);
3184 STRCAT(IObuff, "s");
3185 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
3186 }
3187 // remove 's' from p_shm
3188 else if (!p_terse && p != NULL)
3189 STRMOVE(p, p + 1);
3190}
3191
3192/*
3193 * Process the updated 'paste' option value.
3194 */
3195 static void
3196did_set_paste(void)
3197{
3198 // when 'paste' is set or reset also change other options
3199 paste_option_changed();
3200}
3201
3202/*
3203 * Process the updated 'insertmode' option value.
3204 */
3205 static void
3206did_set_insertmode(long old_value)
3207{
3208 // when 'insertmode' is set from an autocommand need to do work here
3209 if (p_im)
3210 {
3211 if ((State & MODE_INSERT) == 0)
3212 need_start_insertmode = TRUE;
3213 stop_insert_mode = FALSE;
3214 }
3215 // only reset if it was set previously
3216 else if (old_value)
3217 {
3218 need_start_insertmode = FALSE;
3219 stop_insert_mode = TRUE;
3220 if (restart_edit != 0 && mode_displayed)
3221 clear_cmdline = TRUE; // remove "(insert)"
3222 restart_edit = 0;
3223 }
3224}
3225
3226/*
3227 * Process the updated 'ignorecase' option value.
3228 */
3229 static void
3230did_set_ignorecase(void)
3231{
3232 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3233 if (p_hls)
3234 redraw_all_later(UPD_SOME_VALID);
3235}
3236
3237#ifdef FEAT_SEARCH_EXTRA
3238/*
3239 * Process the updated 'hlsearch' option value.
3240 */
3241 static void
3242did_set_hlsearch(void)
3243{
3244 // when 'hlsearch' is set or reset: reset no_hlsearch
3245 set_no_hlsearch(FALSE);
3246}
3247#endif
3248
3249/*
3250 * Process the updated 'scrollbind' option value.
3251 */
3252 static void
3253did_set_scrollbind(void)
3254{
3255 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3256 // at the end of normal_cmd()
3257 if (curwin->w_p_scb)
3258 {
3259 do_check_scrollbind(FALSE);
3260 curwin->w_scbind_pos = curwin->w_topline;
3261 }
3262}
3263
3264#ifdef FEAT_QUICKFIX
3265/*
3266 * Process the updated 'previewwindow' option value.
3267 */
3268 static char *
3269did_set_previewwindow(int *doskip)
3270{
3271 if (!curwin->w_p_pvw)
3272 return NULL;
3273
3274 // There can be only one window with 'previewwindow' set.
3275 win_T *win;
3276
3277 FOR_ALL_WINDOWS(win)
3278 if (win->w_p_pvw && win != curwin)
3279 {
3280 curwin->w_p_pvw = FALSE;
3281 *doskip = TRUE;
3282 return e_preview_window_already_exists;
3283 }
3284
3285 return NULL;
3286}
3287#endif
3288
3289/*
3290 * Process the updated 'smoothscroll' option value.
3291 */
3292 static void
3293did_set_smoothscroll(void)
3294{
3295 if (!curwin->w_p_sms)
3296 {
3297 curwin->w_skipcol = 0;
3298 changed_line_abv_curs();
3299 }
3300}
3301
3302/*
3303 * Process the updated 'textmode' option value.
3304 */
3305 static void
3306did_set_textmode(int opt_flags)
3307{
3308 // when 'textmode' is set or reset also change 'fileformat'
3309 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3310}
3311
3312/*
3313 * Process the updated 'textauto' option value.
3314 */
3315 static void
3316did_set_textauto(int opt_flags)
3317{
3318 // when 'textauto' is set or reset also change 'fileformats'
3319 set_string_option_direct((char_u *)"ffs", -1,
3320 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
3321 OPT_FREE | opt_flags, 0);
3322}
3323
3324/*
3325 * Process the updated 'lisp' option value.
3326 */
3327 static void
3328did_set_lisp(void)
3329{
3330 // When 'lisp' option changes include/exclude '-' in keyword characters.
3331 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3332}
3333
3334/*
3335 * Process the updated 'title' or the 'icon' option value.
3336 */
3337 static void
3338did_set_title_icon(void)
3339{
3340 // when 'title' changed, may need to change the title; same for 'icon'
3341 did_set_title();
3342}
3343
3344/*
3345 * Process the updated 'modified' option value.
3346 */
3347 static void
3348did_set_modified(long value)
3349{
3350 if (!value)
3351 save_file_ff(curbuf); // Buffer is unchanged
3352 redraw_titles();
3353 modified_was_set = value;
3354}
3355
3356#ifdef BACKSLASH_IN_FILENAME
3357/*
3358 * Process the updated 'shellslash' option value.
3359 */
3360 static void
3361did_set_shellslash(void)
3362{
3363 if (p_ssl)
3364 {
3365 psepc = '/';
3366 psepcN = '\\';
3367 pseps[0] = '/';
3368 }
3369 else
3370 {
3371 psepc = '\\';
3372 psepcN = '/';
3373 pseps[0] = '\\';
3374 }
3375
3376 // need to adjust the file name arguments and buffer names.
3377 buflist_slash_adjust();
3378 alist_slash_adjust();
3379# ifdef FEAT_EVAL
3380 scriptnames_slash_adjust();
3381# endif
3382}
3383#endif
3384
3385/*
3386 * Process the updated 'wrap' option value.
3387 */
3388 static void
3389did_set_wrap(void)
3390{
3391 // If 'wrap' is set, set w_leftcol to zero.
3392 if (curwin->w_p_wrap)
3393 curwin->w_leftcol = 0;
3394}
3395
3396/*
3397 * Process the updated 'equalalways' option value.
3398 */
3399 static void
3400did_set_equalalways(long old_value)
3401{
3402 if (p_ea && !old_value)
3403 win_equal(curwin, FALSE, 0);
3404}
3405
3406/*
3407 * Process the updated 'weirdinvert' option value.
3408 */
3409 static void
3410did_set_weirdinvert(long old_value)
3411{
3412 // When 'weirdinvert' changed, set/reset 't_xs'.
3413 // Then set 'weirdinvert' according to value of 't_xs'.
3414 if (p_wiv && !old_value)
3415 T_XS = (char_u *)"y";
3416 else if (!p_wiv && old_value)
3417 T_XS = empty_option;
3418 p_wiv = (*T_XS != NUL);
3419}
3420
3421#ifdef FEAT_BEVAL_GUI
3422/*
3423 * Process the updated 'ballooneval' option value.
3424 */
3425 static void
3426did_set_ballooneval(long old_value)
3427{
3428 if (!balloonEvalForTerm)
3429 {
3430 if (p_beval && !old_value)
3431 gui_mch_enable_beval_area(balloonEval);
3432 else if (!p_beval && old_value)
3433 gui_mch_disable_beval_area(balloonEval);
3434 }
3435
3436}
3437#endif
3438
3439#ifdef FEAT_BEVAL_TERM
3440/*
3441 * Process the updated 'balloonevalterm' option value.
3442 */
3443 static void
3444did_set_balloonevalterm(void)
3445{
3446 mch_bevalterm_changed();
3447}
3448#endif
3449
3450#ifdef FEAT_AUTOCHDIR
3451/*
3452 * Process the updated 'autochdir' option value.
3453 */
3454 static void
3455did_set_autochdir(void)
3456{
3457 // Change directories when the 'acd' option is set now.
3458 DO_AUTOCHDIR;
3459}
3460#endif
3461
3462#ifdef FEAT_DIFF
3463/*
3464 * Process the updated 'diff' option value.
3465 */
3466 static void
3467did_set_diff(void)
3468{
3469 // May add or remove the buffer from the list of diff buffers.
3470 diff_buf_adjust(curwin);
3471# ifdef FEAT_FOLDING
3472 if (foldmethodIsDiff(curwin))
3473 foldUpdateAll(curwin);
3474# endif
3475}
3476#endif
3477
3478#ifdef HAVE_INPUT_METHOD
3479/*
3480 * Process the updated 'imdisable' option value.
3481 */
3482 static void
3483did_set_imdisable(void)
3484{
3485 // Only de-activate it here, it will be enabled when changing mode.
3486 if (p_imdisable)
3487 im_set_active(FALSE);
3488 else if (State & MODE_INSERT)
3489 // When the option is set from an autocommand, it may need to take
3490 // effect right away.
3491 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3492}
3493#endif
3494
3495#ifdef FEAT_SPELL
3496/*
3497 * Process the updated 'spell' option value.
3498 */
3499 static char *
3500did_set_spell(void)
3501{
3502 if (curwin->w_p_spell)
3503 return did_set_spelllang(curwin);
3504
3505 return NULL;
3506}
3507#endif
3508
3509#ifdef FEAT_ARABIC
3510/*
3511 * Process the updated 'arabic' option value.
3512 */
3513 static char *
3514did_set_arabic(void)
3515{
3516 char *errmsg = NULL;
3517
3518 if (curwin->w_p_arab)
3519 {
3520 /*
3521 * 'arabic' is set, handle various sub-settings.
3522 */
3523 if (!p_tbidi)
3524 {
3525 // set rightleft mode
3526 if (!curwin->w_p_rl)
3527 {
3528 curwin->w_p_rl = TRUE;
3529 changed_window_setting();
3530 }
3531
3532 // Enable Arabic shaping (major part of what Arabic requires)
3533 if (!p_arshape)
3534 {
3535 p_arshape = TRUE;
3536 redraw_later_clear();
3537 }
3538 }
3539
3540 // Arabic requires a utf-8 encoding, inform the user if it's not
3541 // set.
3542 if (STRCMP(p_enc, "utf-8") != 0)
3543 {
3544 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3545
3546 msg_source(HL_ATTR(HLF_W));
3547 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3548#ifdef FEAT_EVAL
3549 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3550#endif
3551 }
3552
3553 // set 'delcombine'
3554 p_deco = TRUE;
3555
3556# ifdef FEAT_KEYMAP
3557 // Force-set the necessary keymap for arabic
3558 errmsg = set_option_value((char_u *)"keymap",
3559 0L, (char_u *)"arabic", OPT_LOCAL);
3560# endif
3561 }
3562 else
3563 {
3564 /*
3565 * 'arabic' is reset, handle various sub-settings.
3566 */
3567 if (!p_tbidi)
3568 {
3569 // reset rightleft mode
3570 if (curwin->w_p_rl)
3571 {
3572 curwin->w_p_rl = FALSE;
3573 changed_window_setting();
3574 }
3575
3576 // 'arabicshape' isn't reset, it is a global option and
3577 // another window may still need it "on".
3578 }
3579
3580 // 'delcombine' isn't reset, it is a global option and another
3581 // window may still want it "on".
3582
3583# ifdef FEAT_KEYMAP
3584 // Revert to the default keymap
3585 curbuf->b_p_iminsert = B_IMODE_NONE;
3586 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3587# endif
3588 }
3589
3590 return errmsg;
3591}
3592#endif
3593
3594#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3595/*
3596 * Process the updated 'number' or 'relativenumber' option value.
3597 */
3598 static void
3599did_set_number_relativenumber(char_u *varp)
3600{
3601 if (gui.in_use
3602 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3603 && curbuf->b_signlist != NULL)
3604 {
3605 // If the 'number' or 'relativenumber' options are modified and
3606 // 'signcolumn' is set to 'number', then clear the screen for a full
3607 // refresh. Otherwise the sign icons are not displayed properly in the
3608 // number column. If the 'number' option is set and only the
3609 // 'relativenumber' option is toggled, then don't refresh the screen
3610 // (optimization).
3611 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3612 redraw_all_later(UPD_CLEAR);
3613 }
3614}
3615#endif
3616
3617#ifdef FEAT_TERMGUICOLORS
3618 static char *
3619did_set_termguicolors(int *doskip UNUSED)
3620{
3621# ifdef FEAT_VTP
3622 // Do not turn on 'tgc' when 24-bit colors are not supported.
3623 if (
3624# ifdef VIMDLL
3625 !gui.in_use && !gui.starting &&
3626# endif
3627 !has_vtp_working())
3628 {
3629 p_tgc = 0;
3630 *doskip = TRUE;
3631 return e_24_bit_colors_are_not_supported_on_this_environment;
3632 }
3633 if (is_term_win32())
3634 swap_tcap();
3635# endif
3636# ifdef FEAT_GUI
3637 if (!gui.in_use && !gui.starting)
3638# endif
3639 highlight_gui_started();
3640# ifdef FEAT_VTP
3641 // reset t_Co
3642 if (is_term_win32())
3643 {
3644 control_console_color_rgb();
3645 set_termname(T_NAME);
3646 init_highlight(TRUE, FALSE);
3647 }
3648# endif
3649# ifdef FEAT_TERMINAL
3650 term_update_colors_all();
3651 term_update_palette_all();
3652 term_update_wincolor_all();
3653# endif
3654
3655 return NULL;
3656}
3657#endif
3658
3659/*
3660 * When some boolean options are changed, need to take some action.
3661 */
3662 static char *
3663did_set_bool_option(
3664 char_u *varp,
3665 int opt_flags,
3666 long value,
3667 long old_value,
3668 int *doskip)
3669{
3670 char *errmsg = NULL;
3671
3672 if ((int *)varp == &p_cp) // 'compatible'
3673 did_set_compatible();
3674#ifdef FEAT_LANGMAP
3675 else if ((int *)varp == &p_lrm) // 'langremap'
3676 did_set_langremap();
3677 else if ((int *)varp == &p_lnr) // 'langnoremap'
3678 did_set_langnoremap();
3679#endif
3680#ifdef FEAT_PERSISTENT_UNDO
3681 else if ((int *)varp == &curbuf->b_p_udf // buffer local 'undofile'
3682 || (int *)varp == &p_udf) // 'undofile'
3683 did_set_undofile(opt_flags);
3684#endif
3685 else if ((int *)varp == &curbuf->b_p_ro) // 'readonly'
3686 did_set_readonly(opt_flags);
3687#ifdef FEAT_GUI
3688 else if ((int *)varp == &p_mh) // 'mousehide'
3689 did_set_mousehide();
3690#endif
3691 else if ((int *)varp == &curbuf->b_p_ma)
3692 errmsg = did_set_modifiable(doskip); // 'modifiable'
3693 else if ((int *)varp == &curbuf->b_p_eof // 'endoffile'
3694 || (int *)varp == &curbuf->b_p_eol // 'endofline'
3695 || (int *)varp == &curbuf->b_p_fixeol // 'fixendofline'
3696 || (int *)varp == &curbuf->b_p_bomb) // 'bomb'
3697 did_set_eof_eol_fixeol_bomb();
3698 else if ((int *)varp == &curbuf->b_p_bin) // 'binary'
3699 did_set_binary(opt_flags, old_value);
3700 else if ((int *)varp == &curbuf->b_p_bl) // 'buflisted'
3701 did_set_buflisted(old_value);
3702 else if ((int *)varp == &curbuf->b_p_swf) // 'swapfile'
3703 did_set_swapfile();
3704 else if ((int *)varp == &p_terse) // 'terse'
3705 did_set_terse();
3706 else if ((int *)varp == &p_paste) // 'paste'
3707 did_set_paste();
3708 else if ((int *)varp == &p_im) // 'insertmode'
3709 did_set_insertmode(old_value);
3710 else if ((int *)varp == &p_ic) // 'ignorecase'
3711 did_set_ignorecase();
3712#ifdef FEAT_SEARCH_EXTRA
3713 else if ((int *)varp == &p_hls) // 'hlsearch'
3714 did_set_hlsearch();
3715#endif
3716 else if ((int *)varp == &curwin->w_p_scb) // 'scrollbind'
3717 did_set_scrollbind();
3718#ifdef FEAT_QUICKFIX
3719 else if ((int *)varp == &curwin->w_p_pvw) // 'previewwindow'
3720 errmsg = did_set_previewwindow(doskip);
3721#endif
3722 else if ((int *)varp == &curwin->w_p_sms) // 'smoothscroll'
3723 did_set_smoothscroll();
3724 else if ((int *)varp == &curbuf->b_p_tx) // 'textmode'
3725 did_set_textmode(opt_flags);
3726 else if ((int *)varp == &p_ta) // 'textauto'
3727 did_set_textauto(opt_flags);
3728 else if (varp == (char_u *)&(curbuf->b_p_lisp)) // 'lisp'
3729 did_set_lisp();
3730 else if ( (int *)varp == &p_title // 'title'
3731 || (int *)varp == &p_icon) // 'icon'
3732 did_set_title_icon();
3733 else if ((int *)varp == &curbuf->b_changed) // 'modified'
3734 did_set_modified(value);
3735#ifdef BACKSLASH_IN_FILENAME
3736 else if ((int *)varp == &p_ssl) // 'shellslash'
3737 did_set_shellslash();
3738#endif
3739 else if ((int *)varp == &curwin->w_p_wrap) // 'wrap'
3740 did_set_wrap();
3741 else if ((int *)varp == &p_ea) // 'equalalways'
3742 did_set_equalalways(old_value);
3743 else if ((int *)varp == &p_wiv) // weirdinvert'
3744 did_set_weirdinvert(old_value);
3745#ifdef FEAT_BEVAL_GUI
3746 else if ((int *)varp == &p_beval) // 'ballooneval'
3747 did_set_ballooneval(old_value);
3748#endif
3749#ifdef FEAT_BEVAL_TERM
3750 else if ((int *)varp == &p_bevalterm) // 'balloonevalterm'
3751 did_set_balloonevalterm();
3752#endif
3753#ifdef FEAT_AUTOCHDIR
3754 else if ((int *)varp == &p_acd) // 'autochdir'
3755 did_set_autochdir();
3756#endif
3757#ifdef FEAT_DIFF
3758 else if ((int *)varp == &curwin->w_p_diff) // 'diff'
3759 did_set_diff();
3760#endif
3761#ifdef HAVE_INPUT_METHOD
3762 else if ((int *)varp == &p_imdisable) // 'imdisable'
3763 did_set_imdisable();
3764#endif
3765#ifdef FEAT_SPELL
3766 else if ((int *)varp == &curwin->w_p_spell) // 'spell'
3767 errmsg = did_set_spell();
3768#endif
3769#ifdef FEAT_ARABIC
3770 else if ((int *)varp == &curwin->w_p_arab) // 'arabic'
3771 errmsg = did_set_arabic();
3772#endif
3773#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3774 else if ( (int *)varp == &curwin->w_p_nu // 'number'
3775 || (int *)varp == &curwin->w_p_rnu) // 'relativenumber'
3776 did_set_number_relativenumber(varp);
3777#endif
3778#ifdef FEAT_TERMGUICOLORS
3779 else if ((int *)varp == &p_tgc) // 'termguicolors'
3780 errmsg = did_set_termguicolors(doskip);
3781#endif
3782
3783 return errmsg;
3784}
3785
3786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 * Set the value of a boolean option, and take care of side effects.
3788 * Returns NULL for success, or an error message for an error.
3789 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003790 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003791set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003792 int opt_idx, // index in options[] table
3793 char_u *varp, // pointer to the option variable
3794 int value, // new value
3795 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
3797 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003798#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003799 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003800#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003801 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003803 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if ((secure
3805#ifdef HAVE_SANDBOX
3806 || sandbox != 0
3807#endif
3808 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003809 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003811#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003812 // Save the global value before changing anything. This is needed as for
3813 // a global-only option setting the "local value" in fact sets the global
3814 // value (since there is only one value).
3815 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3816 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
3817 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003818#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003820 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003822 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003823 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824#endif
3825
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003826#ifdef FEAT_GUI
3827 need_mouse_correct = TRUE;
3828#endif
3829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003830 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3832 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
3833
3834 /*
3835 * Handle side effects of changing a bool option.
3836 */
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003837 int doskip = FALSE;
3838 errmsg = did_set_bool_option(varp, opt_flags, value, old_value, &doskip);
3839 if (doskip)
3840 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003842 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 options[opt_idx].flags |= P_WAS_SET;
3845
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003846#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003847 apply_optionset_autocmd(opt_idx, opt_flags,
3848 (long)(old_value ? TRUE : FALSE),
3849 (long)(old_global_value ? TRUE : FALSE),
3850 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003851#endif
3852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003853 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003854 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003855 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003856 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003857
3858 if ((opt_flags & OPT_NO_REDRAW) == 0)
3859 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003861 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862}
3863
3864/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003865 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003867 static char *
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003868did_set_winheight_helpheight(long *pp, char *errmsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003870 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003872 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003873 p_wh = 1;
3874 }
3875 if (p_wmh > p_wh)
3876 {
3877 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3878 p_wh = p_wmh;
3879 }
3880 if (p_hh < 0)
3881 {
3882 errmsg = e_argument_must_be_positive;
3883 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003886 // Change window height NOW
3887 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003889 if (pp == &p_wh && curwin->w_height < p_wh)
3890 win_setheight((int)p_wh);
3891 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3892 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003895 return errmsg;
3896}
3897
3898/*
3899 * Process the new 'winminheight' option value.
3900 */
3901 static char *
3902did_set_winminheight(char *errmsg)
3903{
3904 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003906 errmsg = e_argument_must_be_positive;
3907 p_wmh = 0;
3908 }
3909 if (p_wmh > p_wh)
3910 {
3911 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3912 p_wmh = p_wh;
3913 }
3914 win_setminheight();
3915
3916 return errmsg;
3917}
3918
3919/*
3920 * Process the new 'winwidth' option value.
3921 */
3922 static char *
3923did_set_winwidth(char *errmsg)
3924{
3925 if (p_wiw < 1)
3926 {
3927 errmsg = e_argument_must_be_positive;
3928 p_wiw = 1;
3929 }
3930 if (p_wmw > p_wiw)
3931 {
3932 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3933 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003935
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003936 // Change window width NOW
3937 if (!ONE_WINDOW && curwin->w_width < p_wiw)
3938 win_setwidth((int)p_wiw);
3939
3940 return errmsg;
3941}
3942
3943/*
3944 * Process the new 'winminwidth' option value.
3945 */
3946 static char *
3947did_set_winminwidth(char *errmsg)
3948{
3949 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003950 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003951 errmsg = e_argument_must_be_positive;
3952 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003953 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003954 if (p_wmw > p_wiw)
3955 {
3956 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3957 p_wmw = p_wiw;
3958 }
3959 win_setminwidth();
3960
3961 return errmsg;
3962}
3963
3964/*
3965 * Process the new 'laststatus' option value.
3966 */
3967 static void
3968did_set_laststatus(void)
3969{
3970 last_status(FALSE); // (re)set last window status line
3971}
3972
3973/*
3974 * Process the new 'showtabline' option value.
3975 */
3976 static void
3977did_set_showtabline(void)
3978{
3979 shell_new_rows(); // recompute window positions and heights
3980}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003983/*
3984 * Process the new 'linespace' option value.
3985 */
3986 static void
3987did_set_linespace(void)
3988{
3989 // Recompute gui.char_height and resize the Vim window to keep the
3990 // same number of lines.
3991 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3992 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3993}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994#endif
3995
3996#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003997/*
3998 * Process the new 'foldlevel' option value.
3999 */
4000 static void
4001did_set_foldlevel(void)
4002{
4003 if (curwin->w_p_fdl < 0)
4004 curwin->w_p_fdl = 0;
4005 newFoldLevel();
4006}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004008/*
4009 * Process the new 'foldminlines' option value.
4010 */
4011 static void
4012did_set_foldminlines(void)
4013{
4014 foldUpdateAll(curwin);
4015}
4016
4017/*
4018 * Process the new 'foldnestmax' option value.
4019 */
4020 static void
4021did_set_foldnestmax(void)
4022{
4023 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 foldUpdateAll(curwin);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004025}
4026
4027/*
4028 * Process the new 'foldcolumn' option value.
4029 */
4030 static char *
4031did_set_foldcolumn(char *errmsg)
4032{
4033 if (curwin->w_p_fdc < 0)
4034 {
4035 errmsg = e_argument_must_be_positive;
4036 curwin->w_p_fdc = 0;
4037 }
4038 else if (curwin->w_p_fdc > 12)
4039 {
4040 errmsg = e_invalid_argument;
4041 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004044 return errmsg;
4045}
4046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004048/*
4049 * Process the new 'shiftwidth' or the 'tabstop' option value.
4050 */
4051 static void
4052did_set_shiftwidth_tabstop(long *pp)
4053{
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004054#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004055 if (foldmethodIsIndent(curwin))
4056 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004057#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004058 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4059 // parse 'cinoptions'.
4060 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4061 parse_cino(curbuf);
4062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004064/*
4065 * Process the new 'maxcombine' option value.
4066 */
4067 static void
4068did_set_maxcombine(void)
4069{
4070 if (p_mco > MAX_MCO)
4071 p_mco = MAX_MCO;
4072 else if (p_mco < 0)
4073 p_mco = 0;
4074 screenclear(); // will re-allocate the screen
4075}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004076
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004077/*
4078 * Process the new 'iminsert' option value.
4079 */
4080 static char *
4081did_set_iminsert(char *errmsg)
4082{
4083 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004085 errmsg = e_invalid_argument;
4086 curbuf->b_p_iminsert = B_IMODE_NONE;
4087 }
4088 p_iminsert = curbuf->b_p_iminsert;
4089 if (termcap_active) // don't do this in the alternate screen
4090 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004091#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004092 // Show/unshow value of 'keymap' in status lines.
4093 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004095
4096 return errmsg;
4097}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004099#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004100/*
4101 * Process the new 'imstyle' option value.
4102 */
4103 static char *
4104did_set_imstyle(char *errmsg)
4105{
4106 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4107 errmsg = e_invalid_argument;
4108
4109 return errmsg;
4110}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004111#endif
4112
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004113/*
4114 * Process the new 'window' option value.
4115 */
4116 static void
4117did_set_window(void)
4118{
4119 if (p_window < 1)
4120 p_window = 1;
4121 else if (p_window >= Rows)
4122 p_window = Rows - 1;
4123}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004124
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004125/*
4126 * Process the new 'imsearch' option value.
4127 */
4128 static char *
4129did_set_imsearch(char *errmsg)
4130{
4131 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004133 errmsg = e_invalid_argument;
4134 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004136 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004138 return errmsg;
4139}
4140
4141/*
4142 * Process the new 'titlelen' option value.
4143 */
4144 static char *
4145did_set_titlelen(long old_value, char *errmsg)
4146{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004147 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004148 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004150 errmsg = e_argument_must_be_positive;
4151 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004153 if (starting != NO_SCREEN && old_value != p_titlelen)
4154 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004156 return errmsg;
4157}
4158
4159/*
4160 * Process the new 'cmdheight' option value.
4161 */
4162 static char *
4163did_set_cmdheight(long old_value, char *errmsg)
4164{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004165 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004166 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004168 errmsg = e_argument_must_be_positive;
4169 p_ch = 1;
4170 }
4171 if (p_ch > Rows - min_rows() + 1)
4172 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004174 // Only compute the new window layout when startup has been
4175 // completed. Otherwise the frame sizes may be wrong.
4176 if ((p_ch != old_value
4177 || tabline_height() + topframe->fr_height != Rows - p_ch)
4178 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004180 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004182 )
4183 command_height();
4184
4185 return errmsg;
4186}
4187
4188/*
4189 * Process the new 'updatecount' option value.
4190 */
4191 static char *
4192did_set_updatecount(long old_value, char *errmsg)
4193{
4194 // when 'updatecount' changes from zero to non-zero, open swap files
4195 if (p_uc < 0)
4196 {
4197 errmsg = e_argument_must_be_positive;
4198 p_uc = 100;
4199 }
4200 if (p_uc && !old_value)
4201 ml_open_files();
4202
4203 return errmsg;
4204}
4205
4206#ifdef FEAT_CONCEAL
4207/*
4208 * Process the new 'conceallevel' option value.
4209 */
4210 static char *
4211did_set_conceallevel(char *errmsg)
4212{
4213 if (curwin->w_p_cole < 0)
4214 {
4215 errmsg = e_argument_must_be_positive;
4216 curwin->w_p_cole = 0;
4217 }
4218 else if (curwin->w_p_cole > 3)
4219 {
4220 errmsg = e_invalid_argument;
4221 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004224 return errmsg;
4225}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004228#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004229/*
4230 * Process the new 'pyxversion' option value.
4231 */
4232 static char *
4233did_set_pyxversion(char *errmsg)
4234{
4235 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4236 errmsg = e_invalid_argument;
4237
4238 return errmsg;
4239}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004240#endif
4241
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004242/*
4243 * Process the new global 'undolevels' option value.
4244 */
4245 static void
4246did_set_global_undolevels(long value, long old_value)
4247{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004248 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004249
4250 // use the old value, otherwise u_sync() may not work properly
4251 p_ul = old_value;
4252 u_sync(TRUE);
4253 p_ul = value;
4254}
4255
4256/*
4257 * Process the new buffer local 'undolevels' option value.
4258 */
4259 static void
4260did_set_buflocal_undolevels(long value, long old_value)
4261{
4262 // use the old value, otherwise u_sync() may not work properly
4263 curbuf->b_p_ul = old_value;
4264 u_sync(TRUE);
4265 curbuf->b_p_ul = value;
4266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004268#ifdef FEAT_LINEBREAK
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004269/*
4270 * Process the new 'numberwidth' option value.
4271 */
4272 static char *
4273did_set_numberwidth(char *errmsg)
4274{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004275 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004276 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004277 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004278 errmsg = e_argument_must_be_positive;
4279 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004280 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004281 if (curwin->w_p_nuw > 20)
4282 {
4283 errmsg = e_invalid_argument;
4284 curwin->w_p_nuw = 20;
4285 }
4286 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4287
4288 return errmsg;
4289}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004290#endif
4291
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004292/*
4293 * Process the new 'textwidth' option value.
4294 */
4295 static char *
4296did_set_textwidth(char *errmsg)
4297{
4298 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004299 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004300 errmsg = e_argument_must_be_positive;
4301 curbuf->b_p_tw = 0;
4302 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004303#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004304 {
4305 win_T *wp;
4306 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004307
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004308 FOR_ALL_TAB_WINDOWS(tp, wp)
4309 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004310 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004311#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004312
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004313 return errmsg;
4314}
4315
4316/*
4317 * When some number options are changed, need to take some action.
4318 */
4319 static char *
4320did_set_num_option(long *pp, long value, long old_value, char *errmsg)
4321{
4322 if (pp == &p_wh // 'winheight'
4323 || pp == &p_hh) // 'helpheight'
4324 errmsg = did_set_winheight_helpheight(pp, errmsg);
4325 else if (pp == &p_wmh) // 'winminheight'
4326 errmsg = did_set_winminheight(errmsg);
4327 else if (pp == &p_wiw) // 'winwidth'
4328 errmsg = did_set_winwidth(errmsg);
4329 else if (pp == &p_wmw) // 'winminwidth'
4330 errmsg = did_set_winminwidth(errmsg);
4331 else if (pp == &p_ls)
4332 did_set_laststatus(); // 'laststatus'
4333 else if (pp == &p_stal)
4334 did_set_showtabline(); // 'showtabline'
4335#ifdef FEAT_GUI
4336 else if (pp == &p_linespace) // 'linespace'
4337 did_set_linespace();
4338#endif
4339#ifdef FEAT_FOLDING
4340 else if (pp == &curwin->w_p_fdl) // 'foldlevel'
4341 did_set_foldlevel();
4342 else if (pp == &curwin->w_p_fml) // 'foldminlines'
4343 did_set_foldminlines();
4344 else if (pp == &curwin->w_p_fdn) // 'foldnestmax'
4345 did_set_foldnestmax();
4346 else if (pp == &curwin->w_p_fdc) // 'foldcolumn'
4347 errmsg = did_set_foldcolumn(errmsg);
4348#endif // FEAT_FOLDING
4349 else if ( pp == &curbuf->b_p_sw // 'shiftwidth'
4350 || pp == &curbuf->b_p_ts) // 'tabstop'
4351 did_set_shiftwidth_tabstop(pp);
4352 else if (pp == &p_mco) // 'maxcombine'
4353 did_set_maxcombine();
4354 else if (pp == &curbuf->b_p_iminsert) // 'iminsert'
4355 errmsg = did_set_iminsert(errmsg);
4356#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4357 else if (pp == &p_imst) // 'imstyle'
4358 errmsg = did_set_imstyle(errmsg);
4359#endif
4360 else if (pp == &p_window) // 'window'
4361 did_set_window();
4362 else if (pp == &curbuf->b_p_imsearch) // 'imsearch'
4363 errmsg = did_set_imsearch(errmsg);
4364 else if (pp == &p_titlelen) // 'titlelen'
4365 errmsg = did_set_titlelen(old_value, errmsg);
4366 else if (pp == &p_ch) // 'cmdheight'
4367 errmsg = did_set_cmdheight(old_value, errmsg);
4368 else if (pp == &p_uc) // 'updatecount'
4369 errmsg = did_set_updatecount(old_value, errmsg);
4370#ifdef FEAT_CONCEAL
4371 else if (pp == &curwin->w_p_cole) // 'conceallevel'
4372 errmsg = did_set_conceallevel(errmsg);
4373#endif
4374#ifdef MZSCHEME_GUI_THREADS
4375 else if (pp == &p_mzq) // 'mzquantum'
4376 mzvim_reset_timer();
4377#endif
4378#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
4379 else if (pp == &p_pyx) // 'pyxversion'
4380 errmsg = did_set_pyxversion(errmsg);
4381#endif
4382 else if (pp == &p_ul) // global 'undolevels'
4383 did_set_global_undolevels(value, old_value);
4384 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4385 did_set_buflocal_undolevels(value, old_value);
4386#ifdef FEAT_LINEBREAK
4387 else if (pp == &curwin->w_p_nuw) // 'numberwidth'
4388 errmsg = did_set_numberwidth(errmsg);
4389#endif
4390 else if (pp == &curbuf->b_p_tw) // 'textwidth'
4391 errmsg = did_set_textwidth(errmsg);
4392
4393 return errmsg;
4394}
4395
4396/*
4397 * Check the bounds of numeric options.
4398 */
4399 static char *
4400check_num_option_bounds(
4401 long *pp,
4402 long old_value,
4403 long old_Rows,
4404 long old_Columns,
4405 char *errbuf,
4406 size_t errbuflen,
4407 char *errmsg)
4408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 if (Rows < min_rows() && full_screen)
4410 {
4411 if (errbuf != NULL)
4412 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004413 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004414 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 errmsg = errbuf;
4416 }
4417 Rows = min_rows();
4418 }
4419 if (Columns < MIN_COLUMNS && full_screen)
4420 {
4421 if (errbuf != NULL)
4422 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004423 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004424 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 errmsg = errbuf;
4426 }
4427 Columns = MIN_COLUMNS;
4428 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004429 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 /*
4432 * If the screen (shell) height has been changed, assume it is the
4433 * physical screenheight.
4434 */
4435 if (old_Rows != Rows || old_Columns != Columns)
4436 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004437 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (updating_screen)
4439 *pp = old_value;
4440 else if (full_screen
4441#ifdef FEAT_GUI
4442 && !gui.starting
4443#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004444 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 set_shellsize((int)Columns, (int)Rows, TRUE);
4446 else
4447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004448 // Postpone the resizing; check the size and cmdline position for
4449 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 check_shellsize();
4451 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4452 cmdline_row = Rows - p_ch;
4453 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004454 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004455 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 if (curbuf->b_p_ts <= 0)
4459 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004460 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 curbuf->b_p_ts = 8;
4462 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004463 else if (curbuf->b_p_ts > TABSTOP_MAX)
4464 {
4465 errmsg = e_invalid_argument;
4466 curbuf->b_p_ts = 8;
4467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (p_tm < 0)
4469 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004470 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 p_tm = 0;
4472 }
4473 if ((curwin->w_p_scr <= 0
4474 || (curwin->w_p_scr > curwin->w_height
4475 && curwin->w_height > 0))
4476 && full_screen)
4477 {
4478 if (pp == &(curwin->w_p_scr))
4479 {
4480 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004481 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 win_comp_scroll(curwin);
4483 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004484 // If 'scroll' became invalid because of a side effect silently adjust
4485 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 else if (curwin->w_p_scr <= 0)
4487 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004488 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 curwin->w_p_scr = curwin->w_height;
4490 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004491 if (p_hi < 0)
4492 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004493 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004494 p_hi = 0;
4495 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004496 else if (p_hi > 10000)
4497 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004498 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004499 p_hi = 10000;
4500 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004501 if (p_re < 0 || p_re > 2)
4502 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004503 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004504 p_re = 0;
4505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (p_report < 0)
4507 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004508 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 p_report = 1;
4510 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004511 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004513 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 p_sj = Rows / 2;
4515 else
4516 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004517 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 p_sj = 1;
4519 }
4520 }
4521 if (p_so < 0 && full_screen)
4522 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004523 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 p_so = 0;
4525 }
4526 if (p_siso < 0 && full_screen)
4527 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004528 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 p_siso = 0;
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (p_cwh < 1)
4532 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004533 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 p_cwh = 1;
4535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (p_ut < 0)
4537 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004538 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 p_ut = 2000;
4540 }
4541 if (p_ss < 0)
4542 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004543 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 p_ss = 0;
4545 }
4546
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004547 return errmsg;
4548}
4549
4550/*
4551 * Set the value of a number option, and take care of side effects.
4552 * Returns NULL for success, or an error message for an error.
4553 */
4554 static char *
4555set_num_option(
4556 int opt_idx, // index in options[] table
4557 char_u *varp, // pointer to the option variable
4558 long value, // new value
4559 char *errbuf, // buffer for error messages
4560 size_t errbuflen, // length of "errbuf"
4561 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4562 // OPT_MODELINE, etc.
4563{
4564 char *errmsg = NULL;
4565 long old_value = *(long *)varp;
4566#if defined(FEAT_EVAL)
4567 long old_global_value = 0; // only used when setting a local and
4568 // global option
4569#endif
4570 long old_Rows = Rows; // remember old Rows
4571 long old_Columns = Columns; // remember old Columns
4572 long *pp = (long *)varp;
4573
4574 // Disallow changing some options from secure mode.
4575 if ((secure
4576#ifdef HAVE_SANDBOX
4577 || sandbox != 0
4578#endif
4579 ) && (options[opt_idx].flags & P_SECURE))
4580 return e_not_allowed_here;
4581
4582#if defined(FEAT_EVAL)
4583 // Save the global value before changing anything. This is needed as for
4584 // a global-only option setting the "local value" in fact sets the global
4585 // value (since there is only one value).
4586 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4587 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4588 OPT_GLOBAL);
4589#endif
4590
4591 *pp = value;
4592#ifdef FEAT_EVAL
4593 // Remember where the option was set.
4594 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4595#endif
4596#ifdef FEAT_GUI
4597 need_mouse_correct = TRUE;
4598#endif
4599
4600 if (curbuf->b_p_sw < 0)
4601 {
4602 errmsg = e_argument_must_be_positive;
4603#ifdef FEAT_VARTABS
4604 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4605 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4606 ? tabstop_first(curbuf->b_p_vts_array)
4607 : curbuf->b_p_ts;
4608#else
4609 curbuf->b_p_sw = curbuf->b_p_ts;
4610#endif
4611 }
4612
4613 /*
4614 * Number options that need some action when changed
4615 */
4616 errmsg = did_set_num_option(pp, value, old_value, errmsg);
4617
4618 /*
4619 * Check the bounds for numeric options here
4620 */
4621 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4622 errbuf, errbuflen, errmsg);
4623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004624 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4626 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4627
4628 options[opt_idx].flags |= P_WAS_SET;
4629
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004630#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004631 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4632 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004633#endif
4634
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004635 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004636 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004637 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004638 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004639 if ((opt_flags & OPT_NO_REDRAW) == 0)
4640 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 return errmsg;
4643}
4644
4645/*
4646 * Called after an option changed: check if something needs to be redrawn.
4647 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004649check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004651 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004652 int doclear = (flags & P_RCLR) == P_RCLR;
4653 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004655 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4659 changed_window_setting();
4660 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004661 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004662 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004663 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004664 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004665 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004667 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668}
4669
4670/*
4671 * Find index for option 'arg'.
4672 * Return -1 if not found.
4673 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004674 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004675findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 int opt_idx;
4678 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004679 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 int is_term_opt;
4681
4682 /*
4683 * For first call: Initialize the quick-access table.
4684 * It contains the index for the first option that starts with a certain
4685 * letter. There are 26 letters, plus the first "t_" option.
4686 */
4687 if (quick_tab[1] == 0)
4688 {
4689 p = options[0].fullname;
4690 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4691 {
4692 if (s[0] != p[0])
4693 {
4694 if (s[0] == 't' && s[1] == '_')
4695 quick_tab[26] = opt_idx;
4696 else
4697 quick_tab[CharOrdLow(s[0])] = opt_idx;
4698 }
4699 p = s;
4700 }
4701 }
4702
4703 /*
4704 * Check for name starting with an illegal character.
4705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return -1;
4708
4709 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4710 if (is_term_opt)
4711 opt_idx = quick_tab[26];
4712 else
4713 opt_idx = quick_tab[CharOrdLow(arg[0])];
4714 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004716 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 break;
4718 }
4719 if (s == NULL && !is_term_opt)
4720 {
4721 opt_idx = quick_tab[CharOrdLow(arg[0])];
4722 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4723 {
4724 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004725 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 break;
4727 s = NULL;
4728 }
4729 }
4730 if (s == NULL)
4731 opt_idx = -1;
4732 return opt_idx;
4733}
4734
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004735#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4736 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737/*
4738 * Get the value for an option.
4739 *
4740 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004741 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004742 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004743 * String option: gov_string, *stringval gets allocated string.
4744 * Hidden Number option: gov_hidden_number.
4745 * Hidden Toggle option: gov_hidden_bool.
4746 * Hidden String option: gov_hidden_string.
4747 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004748 *
4749 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004751 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004752get_option_value(
4753 char_u *name,
4754 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004755 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004756 int *flagsp,
4757 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 int opt_idx;
4760 char_u *varp;
4761
4762 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004763 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004764 {
4765 int key;
4766
4767 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004768 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004769 {
4770 char_u key_name[2];
4771 char_u *p;
4772
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004773 if (flagsp != NULL)
4774 *flagsp = 0; // terminal option has no flags
4775
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004776 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004777 if (key < 0)
4778 {
4779 key_name[0] = KEY2TERMCAP0(key);
4780 key_name[1] = KEY2TERMCAP1(key);
4781 }
4782 else
4783 {
4784 key_name[0] = KS_KEY;
4785 key_name[1] = (key & 0xff);
4786 }
4787 p = find_termcode(key_name);
4788 if (p != NULL)
4789 {
4790 if (stringval != NULL)
4791 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004792 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004793 }
4794 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004795 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004798 varp = get_varp_scope(&(options[opt_idx]), scope);
4799
4800 if (flagsp != NULL)
4801 // Return the P_xxxx option flags.
4802 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804 if (options[opt_idx].flags & P_STRING)
4805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004806 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004807 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (stringval != NULL)
4809 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004810 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004811 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4812 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004814 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004815 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004816 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 *stringval = vim_strsave(*(char_u **)(varp));
4821 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004822 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004825 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004826 return (options[opt_idx].flags & P_NUM)
4827 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 if (options[opt_idx].flags & P_NUM)
4829 *numval = *(long *)varp;
4830 else
4831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004832 // Special case: 'modified' is b_changed, but we also want to consider
4833 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if ((int *)varp == &curbuf->b_changed)
4835 *numval = curbufIsChanged();
4836 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004837 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004839 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840}
4841#endif
4842
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004843#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004844/*
4845 * Returns the option attributes and its value. Unlike the above function it
4846 * will return either global value or local value of the option depending on
4847 * what was requested, but it will never return global value if it was
4848 * requested to return local one and vice versa. Neither it will return
4849 * buffer-local value if it was requested to return window-local one.
4850 *
4851 * Pretends that option is absent if it is not present in the requested scope
4852 * (i.e. has no global, window-local or buffer-local value depending on
4853 * opt_type). Uses
4854 *
4855 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004856 * 0 hidden or unknown option, also option that does not have requested
4857 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004858 * see SOPT_* in vim.h for other flags
4859 *
4860 * Possible opt_type values: see SREQ_* in vim.h
4861 */
4862 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004863get_option_value_strict(
4864 char_u *name,
4865 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004866 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867 int opt_type,
4868 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004869{
4870 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004871 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004872 struct vimoption *p;
4873 int r = 0;
4874
4875 opt_idx = findoption(name);
4876 if (opt_idx < 0)
4877 return 0;
4878
4879 p = &(options[opt_idx]);
4880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004881 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004882 if (p->var == NULL)
4883 return 0;
4884
4885 if (p->flags & P_BOOL)
4886 r |= SOPT_BOOL;
4887 else if (p->flags & P_NUM)
4888 r |= SOPT_NUM;
4889 else if (p->flags & P_STRING)
4890 r |= SOPT_STRING;
4891
4892 if (p->indir == PV_NONE)
4893 {
4894 if (opt_type == SREQ_GLOBAL)
4895 r |= SOPT_GLOBAL;
4896 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004897 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004898 }
4899 else
4900 {
4901 if (p->indir & PV_BOTH)
4902 r |= SOPT_GLOBAL;
4903 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004905
4906 if (p->indir & PV_WIN)
4907 {
4908 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004909 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004910 else
4911 r |= SOPT_WIN;
4912 }
4913 else if (p->indir & PV_BUF)
4914 {
4915 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004916 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004917 else
4918 r |= SOPT_BUF;
4919 }
4920 }
4921
4922 if (stringval == NULL)
4923 return r;
4924
4925 if (opt_type == SREQ_GLOBAL)
4926 varp = p->var;
4927 else
4928 {
4929 if (opt_type == SREQ_BUF)
4930 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004931 // Special case: 'modified' is b_changed, but we also want to
4932 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004933 if (p->indir == PV_MOD)
4934 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004935 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004936 varp = NULL;
4937 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004938# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004939 else if (p->indir == PV_KEY)
4940 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004941 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004942 *stringval = NULL;
4943 varp = NULL;
4944 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004945# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004946 else
4947 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004948 buf_T *save_curbuf = curbuf;
4949
4950 // only getting a pointer, no need to use aucmd_prepbuf()
4951 curbuf = (buf_T *)from;
4952 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004953 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004954 curbuf = save_curbuf;
4955 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004956 }
4957 }
4958 else if (opt_type == SREQ_WIN)
4959 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004960 win_T *save_curwin = curwin;
4961
4962 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004963 curbuf = curwin->w_buffer;
4964 varp = get_varp(p);
4965 curwin = save_curwin;
4966 curbuf = curwin->w_buffer;
4967 }
4968 if (varp == p->var)
4969 return (r | SOPT_UNSET);
4970 }
4971
4972 if (varp != NULL)
4973 {
4974 if (p->flags & P_STRING)
4975 *stringval = vim_strsave(*(char_u **)(varp));
4976 else if (p->flags & P_NUM)
4977 *numval = *(long *) varp;
4978 else
4979 *numval = *(int *)varp;
4980 }
4981
4982 return r;
4983}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004984
4985/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004986 * Iterate over options. First argument is a pointer to a pointer to a
4987 * structure inside options[] array, second is option type like in the above
4988 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004989 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004990 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004991 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004992 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004993 * first argument to NULL.
4994 *
4995 * Returns full option name for current option on each call.
4996 */
4997 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004998option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004999{
5000 struct vimoption *ret = NULL;
5001 do
5002 {
5003 if (*option == NULL)
5004 *option = (void *) options;
5005 else if (((struct vimoption *) (*option))->fullname == NULL)
5006 {
5007 *option = NULL;
5008 return NULL;
5009 }
5010 else
5011 *option = (void *) (((struct vimoption *) (*option)) + 1);
5012
5013 ret = ((struct vimoption *) (*option));
5014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005015 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005016 if (ret->var == NULL)
5017 {
5018 ret = NULL;
5019 continue;
5020 }
5021
5022 switch (opt_type)
5023 {
5024 case SREQ_GLOBAL:
5025 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5026 ret = NULL;
5027 break;
5028 case SREQ_BUF:
5029 if (!(ret->indir & PV_BUF))
5030 ret = NULL;
5031 break;
5032 case SREQ_WIN:
5033 if (!(ret->indir & PV_WIN))
5034 ret = NULL;
5035 break;
5036 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005037 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005038 return NULL;
5039 }
5040 }
5041 while (ret == NULL);
5042
5043 return (char_u *)ret->fullname;
5044}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005045#endif
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005048 * Return the flags for the option at 'opt_idx'.
5049 */
5050 long_u
5051get_option_flags(int opt_idx)
5052{
5053 return options[opt_idx].flags;
5054}
5055
5056/*
5057 * Set a flag for the option at 'opt_idx'.
5058 */
5059 void
5060set_option_flag(int opt_idx, long_u flag)
5061{
5062 options[opt_idx].flags |= flag;
5063}
5064
5065/*
5066 * Clear a flag for the option at 'opt_idx'.
5067 */
5068 void
5069clear_option_flag(int opt_idx, long_u flag)
5070{
5071 options[opt_idx].flags &= ~flag;
5072}
5073
5074/*
5075 * Returns TRUE if the option at 'opt_idx' is a global option
5076 */
5077 int
5078is_global_option(int opt_idx)
5079{
5080 return options[opt_idx].indir == PV_NONE;
5081}
5082
5083/*
5084 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5085 * local value.
5086 */
5087 int
5088is_global_local_option(int opt_idx)
5089{
5090 return options[opt_idx].indir & PV_BOTH;
5091}
5092
5093/*
5094 * Returns TRUE if the option at 'opt_idx' is a window-local option
5095 */
5096 int
5097is_window_local_option(int opt_idx)
5098{
5099 return options[opt_idx].var == VAR_WIN;
5100}
5101
5102/*
5103 * Returns TRUE if the option at 'opt_idx' is a hidden option
5104 */
5105 int
5106is_hidden_option(int opt_idx)
5107{
5108 return options[opt_idx].var == NULL;
5109}
5110
5111#if defined(FEAT_CRYPT) || defined(PROTO)
5112/*
5113 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5114 */
5115 int
5116is_crypt_key_option(int opt_idx)
5117{
5118 return options[opt_idx].indir == PV_KEY;
5119}
5120#endif
5121
5122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 * Set the value of option "name".
5124 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005125 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005126 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005128 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005129set_option_value(
5130 char_u *name,
5131 long number,
5132 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005133 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
5135 int opt_idx;
5136 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005137 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005138 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005141 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005142 {
5143 int key;
5144
5145 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005146 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005147 {
5148 char_u key_name[2];
5149
5150 if (key < 0)
5151 {
5152 key_name[0] = KEY2TERMCAP0(key);
5153 key_name[1] = KEY2TERMCAP1(key);
5154 }
5155 else
5156 {
5157 key_name[0] = KS_KEY;
5158 key_name[1] = (key & 0xff);
5159 }
5160 add_termcode(key_name, string, FALSE);
5161 if (full_screen)
5162 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005163 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005164 return NULL;
5165 }
5166
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005167 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 else
5170 {
5171 flags = options[opt_idx].flags;
5172#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005173 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005175 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005176 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005177 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005180 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005181 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005182
5183 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5184 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005186 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005188 int idx;
5189
5190 // Either we are given a string or we are setting option
5191 // to zero.
5192 for (idx = 0; string[idx] == '0'; ++idx)
5193 ;
5194 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005195 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005196 // There's another character after zeros or the string
5197 // is empty. In both cases, we are trying to set a
5198 // num option using a string.
5199 semsg(_(e_number_required_after_str_equal_str),
5200 name, string);
5201 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005202
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005205 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005206 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005207 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005208 errbuf, sizeof(errbuf), opt_flags);
5209 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005210 else
5211 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005215 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216}
5217
5218/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005219 * Call set_option_value() and when an error is returned report it.
5220 */
5221 void
5222set_option_value_give_err(
5223 char_u *name,
5224 long number,
5225 char_u *string,
5226 int opt_flags) // OPT_LOCAL or 0 (both)
5227{
5228 char *errmsg = set_option_value(name, number, string, opt_flags);
5229
5230 if (errmsg != NULL)
5231 emsg(_(errmsg));
5232}
5233
5234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 * Get the terminal code for a terminal option.
5236 * Returns NULL when not found.
5237 */
5238 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005239get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
5241 int opt_idx;
5242 char_u *varp;
5243
5244 if (tname[0] != 't' || tname[1] != '_' ||
5245 tname[2] == NUL || tname[3] == NUL)
5246 return NULL;
5247 if ((opt_idx = findoption(tname)) >= 0)
5248 {
5249 varp = get_varp(&(options[opt_idx]));
5250 if (varp != NULL)
5251 varp = *(char_u **)(varp);
5252 return varp;
5253 }
5254 return find_termcode(tname + 2);
5255}
5256
5257 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005258get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 int i;
5261
5262 i = findoption((char_u *)"hl");
5263 if (i >= 0)
5264 return options[i].def_val[VI_DEFAULT];
5265 return (char_u *)NULL;
5266}
5267
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005268 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005269get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005270{
5271 int i;
5272
5273 i = findoption((char_u *)"enc");
5274 if (i >= 0)
5275 return options[i].def_val[VI_DEFAULT];
5276 return (char_u *)NULL;
5277}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005278
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005279#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005280 int
5281is_option_allocated(char *name)
5282{
5283 int idx = findoption((char_u *)name);
5284
5285 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5286}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005287#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005288
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005289#if defined(FEAT_EVAL) || defined(PROTO)
5290/*
5291 * Return TRUE if "name" is a string option.
5292 * Returns FALSE if option "name" does not exist.
5293 */
5294 int
5295is_string_option(char_u *name)
5296{
5297 int idx = findoption(name);
5298
5299 return idx >= 0 && (options[idx].flags & P_STRING);
5300}
5301#endif
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303/*
5304 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005305 * When "has_lt" is true there is a '<' before "*arg_arg".
5306 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 */
5308 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005309find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005311 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005313 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
5315 /*
5316 * Don't use get_special_key_code() for t_xx, we don't want it to call
5317 * add_termcap_entry().
5318 */
5319 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5320 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005321 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005323 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005325 key = find_special_key(&arg, &modifiers,
5326 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005327 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 key = 0;
5329 }
5330 return key;
5331}
5332
5333/*
5334 * if 'all' == 0: show changed options
5335 * if 'all' == 1: show all normal options
5336 * if 'all' == 2: show all terminal options
5337 */
5338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005339showoptions(
5340 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005341 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
5343 struct vimoption *p;
5344 int col;
5345 int isterm;
5346 char_u *varp;
5347 struct vimoption **items;
5348 int item_count;
5349 int run;
5350 int row, rows;
5351 int cols;
5352 int i;
5353 int len;
5354
5355#define INC 20
5356#define GAP 3
5357
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005358 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (items == NULL)
5360 return;
5361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005362 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005364 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005366 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005368 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005370 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005373 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 * 1. display the short items
5375 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005376 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 */
5378 for (run = 1; run <= 2 && !got_int; ++run)
5379 {
5380 /*
5381 * collect the items in items[]
5382 */
5383 item_count = 0;
5384 for (p = &options[0]; p->fullname != NULL; p++)
5385 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005386 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005387 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005388 continue;
5389
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 varp = NULL;
5391 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005392 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
5394 if (p->indir != PV_NONE && !isterm)
5395 varp = get_varp_scope(p, opt_flags);
5396 }
5397 else
5398 varp = get_varp(p);
5399 if (varp != NULL
5400 && ((all == 2 && isterm)
5401 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005402 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005404 if (opt_flags & OPT_ONECOLUMN)
5405 len = Columns;
5406 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005407 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
5409 {
5410 option_value2string(p, opt_flags);
5411 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5412 }
5413 if ((len <= INC - GAP && run == 1) ||
5414 (len > INC - GAP && run == 2))
5415 items[item_count++] = p;
5416 }
5417 }
5418
5419 /*
5420 * display the items
5421 */
5422 if (run == 1)
5423 {
5424 cols = (Columns + GAP - 3) / INC;
5425 if (cols == 0)
5426 cols = 1;
5427 rows = (item_count + cols - 1) / cols;
5428 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 rows = item_count;
5431 for (row = 0; row < rows && !got_int; ++row)
5432 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005433 msg_putchar('\n'); // go to next line
5434 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 break;
5436 col = 0;
5437 for (i = row; i < item_count; i += rows)
5438 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005439 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 showoneopt(items[i], opt_flags);
5441 col += INC;
5442 }
5443 out_flush();
5444 ui_breakcheck();
5445 }
5446 }
5447 vim_free(items);
5448}
5449
5450/*
5451 * Return TRUE if option "p" has its default value.
5452 */
5453 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005454optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 int dvi;
5457
5458 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005460 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005462 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005464 // the cast to long is required for Manx C, long_i is
5465 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005466 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005467 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5469}
5470
5471/*
5472 * showoneopt: show the value of one option
5473 * must not be called with a hidden option!
5474 */
5475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005476showoneopt(
5477 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005478 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005480 char_u *varp;
5481 int save_silent = silent_mode;
5482
5483 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005484 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486 varp = get_varp_scope(p, opt_flags);
5487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005488 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5490 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005491 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005493 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005495 msg_puts(" ");
5496 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (!(p->flags & P_BOOL))
5498 {
5499 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005500 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 option_value2string(p, opt_flags);
5502 msg_outtrans(NameBuff);
5503 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005504
5505 silent_mode = save_silent;
5506 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507}
5508
5509/*
5510 * Write modified options as ":set" commands to a file.
5511 *
5512 * There are three values for "opt_flags":
5513 * OPT_GLOBAL: Write global option values and fresh values of
5514 * buffer-local options (used for start of a session
5515 * file).
5516 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5517 * curwin (used for a vimrc file).
5518 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5519 * and local values for window-local options of
5520 * curwin. Local values are also written when at the
5521 * default value, because a modeline or autocommand
5522 * may have set them when doing ":edit file" and the
5523 * user has set them back at the default or fresh
5524 * value.
5525 * When "local_only" is TRUE, don't write fresh
5526 * values, only local values (for ":mkview").
5527 * (fresh value = value used for a new buffer or window for a local option).
5528 *
5529 * Return FAIL on error, OK otherwise.
5530 */
5531 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005532makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005535 char_u *varp; // currently used value
5536 char_u *varp_fresh; // local value
5537 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 char *cmd;
5539 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005540 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
5542 /*
5543 * The options that don't have a default (terminal name, columns, lines)
5544 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005545 * Do the loop over "options[]" twice: once for options with the
5546 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005548 for (pri = 1; pri >= 0; --pri)
5549 {
5550 for (p = &options[0]; !istermoption(p); p++)
5551 if (!(p->flags & P_NO_MKRC)
5552 && !istermoption(p)
5553 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005555 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5557 continue;
5558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005559 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5560 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5562 continue;
5563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005564 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005566 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 continue;
5568
Bram Moolenaard23b7142021-04-17 21:04:34 +02005569 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5570 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005571 continue;
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 round = 2;
5574 if (p->indir != PV_NONE)
5575 {
5576 if (p->var == VAR_WIN)
5577 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005578 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 if (!(opt_flags & OPT_LOCAL))
5580 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005581 // When fresh value of window-local option is not at the
5582 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5584 {
5585 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005586 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
5588 round = 1;
5589 varp_local = varp;
5590 varp = varp_fresh;
5591 }
5592 }
5593 }
5594 }
5595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005596 // Round 1: fresh value for window-local options.
5597 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 for ( ; round <= 2; varp = varp_local, ++round)
5599 {
5600 if (round == 1 || (opt_flags & OPT_GLOBAL))
5601 cmd = "set";
5602 else
5603 cmd = "setlocal";
5604
5605 if (p->flags & P_BOOL)
5606 {
5607 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5608 return FAIL;
5609 }
5610 else if (p->flags & P_NUM)
5611 {
5612 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5613 return FAIL;
5614 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005617 int do_endif = FALSE;
5618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005619 // Don't set 'syntax' and 'filetype' again if the value is
5620 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005621 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005622#if defined(FEAT_SYN_HL)
5623 p->indir == PV_SYN ||
5624#endif
5625 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
5627 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5628 *(char_u **)(varp)) < 0
5629 || put_eol(fd) < 0)
5630 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005631 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
5633 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005634 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005636 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
5638 if (put_line(fd, "endif") == FAIL)
5639 return FAIL;
5640 }
5641 }
5642 }
5643 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 return OK;
5646}
5647
5648#if defined(FEAT_FOLDING) || defined(PROTO)
5649/*
5650 * Generate set commands for the local fold options only. Used when
5651 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5652 */
5653 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005654makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005656 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005658 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 == FAIL
5660# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005661 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005663 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 == FAIL
5665 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5666 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5667 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5668 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5669 )
5670 return FAIL;
5671
5672 return OK;
5673}
5674#endif
5675
5676 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005677put_setstring(
5678 FILE *fd,
5679 char *cmd,
5680 char *name,
5681 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005682 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005685 char_u *buf = NULL;
5686 char_u *part = NULL;
5687 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688
5689 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5690 return FAIL;
5691 if (*valuep != NULL)
5692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005693 // Output 'pastetoggle' as key names. For other
5694 // options some characters have to be escaped with
5695 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (valuep == &p_pt)
5697 {
5698 s = *valuep;
5699 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005700 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 return FAIL;
5702 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005703 // expand the option value, replace $HOME by ~
5704 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005706 int size = (int)STRLEN(*valuep) + 1;
5707
5708 // replace home directory in the whole option value into "buf"
5709 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005710 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005711 goto fail;
5712 home_replace(NULL, *valuep, buf, size, FALSE);
5713
5714 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005715 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005716 // can be expanded when read back.
5717 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5718 && vim_strchr(*valuep, ',') != NULL)
5719 {
5720 part = alloc(size);
5721 if (part == NULL)
5722 goto fail;
5723
5724 // write line break to clear the option, e.g. ':set rtp='
5725 if (put_eol(fd) == FAIL)
5726 goto fail;
5727
5728 p = buf;
5729 while (*p != NUL)
5730 {
5731 // for each comma separated option part, append value to
5732 // the option, :set rtp+=value
5733 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5734 goto fail;
5735 (void)copy_option_part(&p, part, size, ",");
5736 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5737 goto fail;
5738 }
5739 vim_free(buf);
5740 vim_free(part);
5741 return OK;
5742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005744 {
5745 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005747 }
5748 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
5750 else if (put_escstr(fd, *valuep, 2) == FAIL)
5751 return FAIL;
5752 }
5753 if (put_eol(fd) < 0)
5754 return FAIL;
5755 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005756fail:
5757 vim_free(buf);
5758 vim_free(part);
5759 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760}
5761
5762 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005763put_setnum(
5764 FILE *fd,
5765 char *cmd,
5766 char *name,
5767 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
5769 long wc;
5770
5771 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5772 return FAIL;
5773 if (wc_use_keyname((char_u *)valuep, &wc))
5774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005775 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5777 return FAIL;
5778 }
5779 else if (fprintf(fd, "%ld", *valuep) < 0)
5780 return FAIL;
5781 if (put_eol(fd) < 0)
5782 return FAIL;
5783 return OK;
5784}
5785
5786 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005787put_setbool(
5788 FILE *fd,
5789 char *cmd,
5790 char *name,
5791 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005793 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005794 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5796 || put_eol(fd) < 0)
5797 return FAIL;
5798 return OK;
5799}
5800
5801/*
5802 * Clear all the terminal options.
5803 * If the option has been allocated, free the memory.
5804 * Terminal options are never hidden or indirect.
5805 */
5806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005807clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 /*
5810 * Reset a few things before clearing the old options. This may cause
5811 * outputting a few things that the terminal doesn't understand, but the
5812 * screen will be cleared later, so this is OK.
5813 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005814 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005815 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005817 // When starting the GUI close the display opened for the clipboard.
5818 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if (gui.starting)
5820 clear_xterm_clip();
5821#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005822 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005824 free_termoptions();
5825}
5826
5827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005828free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005829{
5830 struct vimoption *p;
5831
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005832 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (istermoption(p))
5834 {
5835 if (p->flags & P_ALLOCED)
5836 free_string_option(*(char_u **)(p->var));
5837 if (p->flags & P_DEF_ALLOCED)
5838 free_string_option(p->def_val[VI_DEFAULT]);
5839 *(char_u **)(p->var) = empty_option;
5840 p->def_val[VI_DEFAULT] = empty_option;
5841 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005842#ifdef FEAT_EVAL
5843 // remember where the option was cleared
5844 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 }
5847 clear_termcodes();
5848}
5849
5850/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005851 * Free the string for one term option, if it was allocated.
5852 * Set the string to empty_option and clear allocated flag.
5853 * "var" points to the option value.
5854 */
5855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005856free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005857{
5858 struct vimoption *p;
5859
5860 for (p = &options[0]; p->fullname != NULL; p++)
5861 if (p->var == var)
5862 {
5863 if (p->flags & P_ALLOCED)
5864 free_string_option(*(char_u **)(p->var));
5865 *(char_u **)(p->var) = empty_option;
5866 p->flags &= ~P_ALLOCED;
5867 break;
5868 }
5869}
5870
5871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 * Set the terminal option defaults to the current value.
5873 * Used after setting the terminal name.
5874 */
5875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005876set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 struct vimoption *p;
5879
5880 for (p = &options[0]; p->fullname != NULL; p++)
5881 {
5882 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5883 {
5884 if (p->flags & P_DEF_ALLOCED)
5885 {
5886 free_string_option(p->def_val[VI_DEFAULT]);
5887 p->flags &= ~P_DEF_ALLOCED;
5888 }
5889 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5890 if (p->flags & P_ALLOCED)
5891 {
5892 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005893 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 }
5895 }
5896 }
5897}
5898
5899/*
5900 * return TRUE if 'p' starts with 't_'
5901 */
5902 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005903istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5906}
5907
Bram Moolenaardac13472019-09-16 21:06:21 +02005908/*
5909 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5910 */
5911 int
5912istermoption_idx(int opt_idx)
5913{
5914 return istermoption(&options[opt_idx]);
5915}
5916
Bram Moolenaar113e1072019-01-20 15:30:40 +01005917#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005919 * Unset local option value, similar to ":set opt<".
5920 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005922unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005923{
5924 struct vimoption *p;
5925 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005926 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005927
5928 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005929 if (opt_idx < 0)
5930 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005931 p = &(options[opt_idx]);
5932
5933 switch ((int)p->indir)
5934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005935 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005936 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005937 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005938 break;
5939 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005940 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005941 break;
5942 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005943 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005944 break;
5945 case PV_AR:
5946 buf->b_p_ar = -1;
5947 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005948 case PV_BKC:
5949 clear_string_option(&buf->b_p_bkc);
5950 buf->b_bkc_flags = 0;
5951 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005952 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005953 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005954 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005955 case PV_TC:
5956 clear_string_option(&buf->b_p_tc);
5957 buf->b_tc_flags = 0;
5958 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005959 case PV_SISO:
5960 curwin->w_p_siso = -1;
5961 break;
5962 case PV_SO:
5963 curwin->w_p_so = -1;
5964 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005965# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005966 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005967 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005968 break;
5969 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005970 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005971 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005972# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005973 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005974 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005975 break;
5976 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005977 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005978 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005979# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005980 case PV_TSRFU:
5981 clear_string_option(&buf->b_p_tsrfu);
5982 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005983# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005984 case PV_FP:
5985 clear_string_option(&buf->b_p_fp);
5986 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005987# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005988 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005989 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005990 break;
5991 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005992 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005993 break;
5994 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005995 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005996 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005997# endif
5998# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005999 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006000 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006001 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006002# endif
6003# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006004 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006005 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006006 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006007# endif
6008# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006009 case PV_SBR:
6010 clear_string_option(&((win_T *)from)->w_p_sbr);
6011 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006012# endif
6013# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006014 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006015 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006016 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006017# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006018 case PV_UL:
6019 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6020 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006021 case PV_LW:
6022 clear_string_option(&buf->b_p_lw);
6023 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006024 case PV_MENC:
6025 clear_string_option(&buf->b_p_menc);
6026 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006027 case PV_LCS:
6028 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006029 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006030 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006031 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006032 case PV_FCS:
6033 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006034 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006035 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006036 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006037 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006038 clear_string_option(&((win_T *)from)->w_p_ve);
6039 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006040 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006041 }
6042}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006043#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006044
6045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006047 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 */
6049 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006050get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006052 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
6054 if (p->var == VAR_WIN)
6055 return (char_u *)GLOBAL_WO(get_varp(p));
6056 return p->var;
6057 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006058 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
6060 switch ((int)p->indir)
6061 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006062 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006064 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6065 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6066 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006068 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6069 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6070 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006071 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006072 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006073 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006074 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6075 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006077 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6078 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006080 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6081 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006082#ifdef FEAT_COMPL_FUNC
6083 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6084#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006085#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6086 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6087#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006088#if defined(FEAT_CRYPT)
6089 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6090#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006091#ifdef FEAT_LINEBREAK
6092 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6093#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006094#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006095 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006096#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006097 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006098 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006099 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006100 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006101 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006102 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006103 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 }
6108 return get_varp(p);
6109}
6110
6111/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006112 * Get pointer to option variable at 'opt_idx', depending on local or global
6113 * scope.
6114 */
6115 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006116get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006117{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006118 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006119}
6120
6121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 * Get pointer to option variable.
6123 */
6124 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006125get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006127 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (p->var == NULL)
6129 return NULL;
6130
6131 switch ((int)p->indir)
6132 {
6133 case PV_NONE: return p->var;
6134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006135 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006136 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006138 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006140 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006142 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006144 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006146 case PV_TC: return *curbuf->b_p_tc != NUL
6147 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006148 case PV_BKC: return *curbuf->b_p_bkc != NUL
6149 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006150 case PV_SISO: return curwin->w_p_siso >= 0
6151 ? (char_u *)&(curwin->w_p_siso) : p->var;
6152 case PV_SO: return curwin->w_p_so >= 0
6153 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006155 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006157 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6159#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006160 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006162 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006164#ifdef FEAT_COMPL_FUNC
6165 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6166 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6167#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006168 case PV_FP: return *curbuf->b_p_fp != NUL
6169 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006171 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006173 case PV_GP: return *curbuf->b_p_gp != NUL
6174 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6175 case PV_MP: return *curbuf->b_p_mp != NUL
6176 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006178#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6179 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6180 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6181#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006182#if defined(FEAT_CRYPT)
6183 case PV_CM: return *curbuf->b_p_cm != NUL
6184 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6185#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006186#ifdef FEAT_LINEBREAK
6187 case PV_SBR: return *curwin->w_p_sbr != NUL
6188 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6189#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006190#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006191 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006192 ? (char_u *)&(curwin->w_p_stl) : p->var;
6193#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006194 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6195 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006196 case PV_LW: return *curbuf->b_p_lw != NUL
6197 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006198 case PV_MENC: return *curbuf->b_p_menc != NUL
6199 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200#ifdef FEAT_ARABIC
6201 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6202#endif
6203 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006204 case PV_LCS: return *curwin->w_p_lcs != NUL
6205 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006206 case PV_FCS: return *curwin->w_p_fcs != NUL
6207 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006208 case PV_VE: return *curwin->w_p_ve != NUL
6209 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006210#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006211 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6212#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006213#ifdef FEAT_SYN_HL
6214 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6215 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006216 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006217 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219#ifdef FEAT_DIFF
6220 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6221#endif
6222#ifdef FEAT_FOLDING
6223 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6224 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6225 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6226 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6227 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6228 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6229 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6230# ifdef FEAT_EVAL
6231 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6232 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6233# endif
6234 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6235#endif
6236 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006237 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006238#ifdef FEAT_LINEBREAK
6239 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006242 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006243#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6245#endif
6246#ifdef FEAT_RIGHTLEFT
6247 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6248 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6249#endif
6250 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006251 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6253#ifdef FEAT_LINEBREAK
6254 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006255 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6256 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006258 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006260 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006261#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006262 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6263 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6264#endif
6265#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006266 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6267 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6268 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6272 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6275 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6277 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6279 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6280 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006281 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284#ifdef FEAT_FOLDING
6285 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006288#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006289 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006291#ifdef FEAT_COMPL_FUNC
6292 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006293 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006294#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006295#ifdef FEAT_EVAL
6296 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6297#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006298 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006300 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006306 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6308 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6309 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6310 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6311#ifdef FEAT_FIND_ID
6312# ifdef FEAT_EVAL
6313 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6314# endif
6315#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006316#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6318 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6319#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006320#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006321 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323#ifdef FEAT_CRYPT
6324 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006327 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6329 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6330 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6331 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6332 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006334 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6341#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006342 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006343 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6344#endif
6345#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006346 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6347 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6348 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006349 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350#endif
6351 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6352 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6353 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6354 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006355#ifdef FEAT_PERSISTENT_UNDO
6356 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6359#ifdef FEAT_KEYMAP
6360 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6361#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006362#ifdef FEAT_SIGNS
6363 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6364#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006365#ifdef FEAT_VARTABS
6366 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6367 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6368#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006369 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006371 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 return (char_u *)&(curbuf->b_p_wm);
6373}
6374
6375/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006376 * Return a pointer to the variable for option at 'opt_idx'
6377 */
6378 char_u *
6379get_option_var(int opt_idx)
6380{
6381 return options[opt_idx].var;
6382}
6383
Dominique Pelle748b3082022-01-08 12:41:16 +00006384#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006385/*
6386 * Return the full name of the option at 'opt_idx'
6387 */
6388 char_u *
6389get_option_fullname(int opt_idx)
6390{
6391 return (char_u *)options[opt_idx].fullname;
6392}
Dominique Pelle748b3082022-01-08 12:41:16 +00006393#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006394
6395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 * Get the value of 'equalprg', either the buffer-local one or the global one.
6397 */
6398 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006399get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400{
6401 if (*curbuf->b_p_ep == NUL)
6402 return p_ep;
6403 return curbuf->b_p_ep;
6404}
6405
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406/*
6407 * Copy options from one window to another.
6408 * Used when splitting a window.
6409 */
6410 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006411win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6414 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006415 after_copy_winopt(wp_to);
6416}
6417
6418/*
6419 * After copying window options: update variables depending on options.
6420 */
6421 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006422after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006423{
6424#ifdef FEAT_LINEBREAK
6425 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006426#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006427#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006428 fill_culopt_flags(NULL, wp);
6429 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006430#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006431 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6432 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006433}
6434
6435 static char_u *
6436copy_option_val(char_u *val)
6437{
6438 if (val == empty_option)
6439 return empty_option; // no need to allocate memory
6440 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443/*
6444 * Copy the options from one winopt_T to another.
6445 * Doesn't free the old option values in "to", use clear_winopt() for that.
6446 * The 'scroll' option is not copied, because it depends on the window height.
6447 * The 'previewwindow' option is reset, there can be only one preview window.
6448 */
6449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006450copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452#ifdef FEAT_ARABIC
6453 to->wo_arab = from->wo_arab;
6454#endif
6455 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006456 to->wo_lcs = copy_option_val(from->wo_lcs);
6457 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006459 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006460 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006461 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006462#ifdef FEAT_LINEBREAK
6463 to->wo_nuw = from->wo_nuw;
6464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465#ifdef FEAT_RIGHTLEFT
6466 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006467 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006469#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006470 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006471#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006472#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006473 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006476#ifdef FEAT_DIFF
6477 to->wo_wrap_save = from->wo_wrap_save;
6478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479#ifdef FEAT_LINEBREAK
6480 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006481 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006482 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006484 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006486 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006487 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006488 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006489 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006490#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006491 to->wo_spell = from->wo_spell;
6492#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006493#ifdef FEAT_SYN_HL
6494 to->wo_cuc = from->wo_cuc;
6495 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006496 to->wo_culopt = copy_option_val(from->wo_culopt);
6497 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499#ifdef FEAT_DIFF
6500 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006501 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006503#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006504 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006505 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006506#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006507#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006508 to->wo_twk = copy_option_val(from->wo_twk);
6509 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511#ifdef FEAT_FOLDING
6512 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006513 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006515 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006516 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 to->wo_fml = from->wo_fml;
6518 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006519 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006520 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006521 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006522 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 to->wo_fdn = from->wo_fdn;
6524# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006525 to->wo_fde = copy_option_val(from->wo_fde);
6526 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006528 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006530#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006531 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006532#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006533
6534#ifdef FEAT_EVAL
6535 // Copy the script context so that we know where the value was last set.
6536 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6537 sizeof(to->wo_script_ctx));
6538#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006539 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
6541
6542/*
6543 * Check string options in a window for a NULL value.
6544 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006546check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 check_winopt(&win->w_onebuf_opt);
6549 check_winopt(&win->w_allbuf_opt);
6550}
6551
6552/*
6553 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6554 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558#ifdef FEAT_FOLDING
6559 check_string_option(&wop->wo_fdi);
6560 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006561 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562# ifdef FEAT_EVAL
6563 check_string_option(&wop->wo_fde);
6564 check_string_option(&wop->wo_fdt);
6565# endif
6566 check_string_option(&wop->wo_fmr);
6567#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006568#ifdef FEAT_SIGNS
6569 check_string_option(&wop->wo_scl);
6570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571#ifdef FEAT_RIGHTLEFT
6572 check_string_option(&wop->wo_rlc);
6573#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006574#ifdef FEAT_LINEBREAK
6575 check_string_option(&wop->wo_sbr);
6576#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006577#ifdef FEAT_STL_OPT
6578 check_string_option(&wop->wo_stl);
6579#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006580#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006581 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006582 check_string_option(&wop->wo_cc);
6583#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006584#ifdef FEAT_CONCEAL
6585 check_string_option(&wop->wo_cocu);
6586#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006587#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006588 check_string_option(&wop->wo_twk);
6589 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006590#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006591#ifdef FEAT_LINEBREAK
6592 check_string_option(&wop->wo_briopt);
6593#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006594 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006595 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006596 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006597 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598}
6599
6600/*
6601 * Free the allocated memory inside a winopt_T.
6602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006604clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606#ifdef FEAT_FOLDING
6607 clear_string_option(&wop->wo_fdi);
6608 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006609 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610# ifdef FEAT_EVAL
6611 clear_string_option(&wop->wo_fde);
6612 clear_string_option(&wop->wo_fdt);
6613# endif
6614 clear_string_option(&wop->wo_fmr);
6615#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006616#ifdef FEAT_SIGNS
6617 clear_string_option(&wop->wo_scl);
6618#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006619#ifdef FEAT_LINEBREAK
6620 clear_string_option(&wop->wo_briopt);
6621#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006622 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623#ifdef FEAT_RIGHTLEFT
6624 clear_string_option(&wop->wo_rlc);
6625#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006626#ifdef FEAT_LINEBREAK
6627 clear_string_option(&wop->wo_sbr);
6628#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006629#ifdef FEAT_STL_OPT
6630 clear_string_option(&wop->wo_stl);
6631#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006632#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006633 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006634 clear_string_option(&wop->wo_cc);
6635#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006636#ifdef FEAT_CONCEAL
6637 clear_string_option(&wop->wo_cocu);
6638#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006639#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006640 clear_string_option(&wop->wo_twk);
6641 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006642#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006643 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006644 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006645 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006648#ifdef FEAT_EVAL
6649// Index into the options table for a buffer-local option enum.
6650static int buf_opt_idx[BV_COUNT];
6651# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6652
6653/*
6654 * Initialize buf_opt_idx[] if not done already.
6655 */
6656 static void
6657init_buf_opt_idx(void)
6658{
6659 static int did_init_buf_opt_idx = FALSE;
6660 int i;
6661
6662 if (did_init_buf_opt_idx)
6663 return;
6664 did_init_buf_opt_idx = TRUE;
6665 for (i = 0; !istermoption_idx(i); i++)
6666 if (options[i].indir & PV_BUF)
6667 buf_opt_idx[options[i].indir & PV_MASK] = i;
6668}
6669#else
6670# define COPY_OPT_SCTX(buf, bv)
6671#endif
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673/*
6674 * Copy global option values to local options for one buffer.
6675 * Used when creating a new buffer and sometimes when entering a buffer.
6676 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006677 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6679 * appropriate.
6680 * BCO_NOHELP Don't copy the values to a help buffer.
6681 */
6682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006683buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006686 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 int dont_do_help;
6688 int did_isk = FALSE;
6689
6690 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 * Skip this when the option defaults have not been set yet. Happens when
6692 * main() allocates the first buffer.
6693 */
6694 if (p_cpo != NULL)
6695 {
6696 /*
6697 * Always copy when entering and 'cpo' contains 'S'.
6698 * Don't copy when already initialized.
6699 * Don't copy when 'cpo' contains 's' and not entering.
6700 * 'S' BCO_ENTER initialized 's' should_copy
6701 * yes yes X X TRUE
6702 * yes no yes X FALSE
6703 * no X yes X FALSE
6704 * X no no yes FALSE
6705 * X no no no TRUE
6706 * no yes no X TRUE
6707 */
6708 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6709 && (buf->b_p_initialized
6710 || (!(flags & BCO_ENTER)
6711 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6712 should_copy = FALSE;
6713
6714 if (should_copy || (flags & BCO_ALWAYS))
6715 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006716#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006717 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006718 init_buf_opt_idx();
6719#endif
6720 // Don't copy the options specific to a help buffer when
6721 // BCO_NOHELP is given or the options were initialized already
6722 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6724 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006725 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {
6727 save_p_isk = buf->b_p_isk;
6728 buf->b_p_isk = NULL;
6729 }
6730 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006731 * Always free the allocated strings. If not already initialized,
6732 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 */
6734 if (!buf->b_p_initialized)
6735 {
6736 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006737 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006740 switch (*p_ffs)
6741 {
6742 case 'm':
6743 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6744 case 'd':
6745 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6746 case 'u':
6747 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6748 default:
6749 buf->b_p_ff = vim_strsave(p_ff);
6750 }
6751 if (buf->b_p_ff != NULL)
6752 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 buf->b_p_bh = empty_option;
6754 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
6756 else
6757 free_buf_options(buf, FALSE);
6758
6759 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006760 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 buf->b_p_ai_nopaste = p_ai_nopaste;
6762 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006763 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006765 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 buf->b_p_tw_nopaste = p_tw_nopaste;
6767 buf->b_p_tw_nobin = p_tw_nobin;
6768 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006769 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 buf->b_p_wm_nopaste = p_wm_nopaste;
6771 buf->b_p_wm_nobin = p_wm_nobin;
6772 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006773 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006774 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006775 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006776 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006777 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006779 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006781 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006783 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 buf->b_p_ml_nobin = p_ml_nobin;
6785 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006786 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006787 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006788 buf->b_p_swf = FALSE;
6789 else
6790 {
6791 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006792 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006795 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006796#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006797 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006798 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006800#ifdef FEAT_COMPL_FUNC
6801 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006802 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006803 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006804 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006805 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006806 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006807#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006808#ifdef FEAT_EVAL
6809 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006810 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006811 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006814 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006816#ifdef FEAT_VARTABS
6817 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006818 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006819 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006820 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006821 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006822 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006823 buf->b_p_vsts_nopaste = p_vsts_nopaste
6824 ? vim_strsave(p_vsts_nopaste) : NULL;
6825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006827 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006829 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830#ifdef FEAT_FOLDING
6831 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006832 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833#endif
6834 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006835 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006836 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006837 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006838 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6839 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006841 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006843 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006845 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006847 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006848
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006850 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006852 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006854 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006855 buf->b_p_cinsd = vim_strsave(p_cinsd);
6856 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006857 buf->b_p_lop = vim_strsave(p_lop);
6858 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006859
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006860 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006863 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006865 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006867 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006869 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006871 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006872 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006873 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006874#endif
6875#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006876 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006877 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006878 (void)compile_cap_prog(&buf->b_s);
6879 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006880 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006881 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006882 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006883 buf->b_s.b_p_spo = vim_strsave(p_spo);
6884 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006886#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006888 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006890 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006892 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006893#if defined(FEAT_EVAL)
6894 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006895 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897#ifdef FEAT_CRYPT
6898 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006899 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006902 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903#ifdef FEAT_KEYMAP
6904 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006905 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 buf->b_kmap_state |= KEYMAP_INIT;
6907#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006908#ifdef FEAT_TERMINAL
6909 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006910 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006911#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 // This isn't really an option, but copying the langmap and IME
6913 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006915 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006917 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 // options that are normally global but also have a local value
6920 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006922 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006923 buf->b_p_bkc = empty_option;
6924 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925#ifdef FEAT_QUICKFIX
6926 buf->b_p_gp = empty_option;
6927 buf->b_p_mp = empty_option;
6928 buf->b_p_efm = empty_option;
6929#endif
6930 buf->b_p_ep = empty_option;
6931 buf->b_p_kp = empty_option;
6932 buf->b_p_path = empty_option;
6933 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006934 buf->b_p_tc = empty_option;
6935 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#ifdef FEAT_FIND_ID
6937 buf->b_p_def = empty_option;
6938 buf->b_p_inc = empty_option;
6939# ifdef FEAT_EVAL
6940 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006941 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942# endif
6943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 buf->b_p_dict = empty_option;
6945 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006946#ifdef FEAT_COMPL_FUNC
6947 buf->b_p_tsrfu = empty_option;
6948#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006949 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006950 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006951#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6952 buf->b_p_bexpr = empty_option;
6953#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006954#if defined(FEAT_CRYPT)
6955 buf->b_p_cm = empty_option;
6956#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006957#ifdef FEAT_PERSISTENT_UNDO
6958 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006959 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006960#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006961 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006962 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964 /*
6965 * Don't copy the options set by ex_help(), use the saved values,
6966 * when going from a help buffer to a non-help buffer.
6967 * Don't touch these at all when BCO_NOHELP is used and going from
6968 * or to a help buffer.
6969 */
6970 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006973#ifdef FEAT_VARTABS
6974 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006975 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006976 else
6977 buf->b_p_vts_array = NULL;
6978#endif
6979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 else
6981 {
6982 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 did_isk = TRUE;
6985 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006986 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006987#ifdef FEAT_VARTABS
6988 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006989 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006990 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006991 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006992 else
6993 buf->b_p_vts_array = NULL;
6994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (buf->b_p_bt[0] == 'h')
6997 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006999 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 }
7001 }
7002
7003 /*
7004 * When the options should be copied (ignoring BCO_ALWAYS), set the
7005 * flag that indicates that the options have been initialized.
7006 */
7007 if (should_copy)
7008 buf->b_p_initialized = TRUE;
7009 }
7010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if (did_isk)
7013 (void)buf_init_chartab(buf, FALSE);
7014}
7015
7016/*
7017 * Reset the 'modifiable' option and its default value.
7018 */
7019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007020reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
7022 int opt_idx;
7023
7024 curbuf->b_p_ma = FALSE;
7025 p_ma = FALSE;
7026 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007027 if (opt_idx >= 0)
7028 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029}
7030
7031/*
7032 * Set the global value for 'iminsert' to the local value.
7033 */
7034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007035set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 p_iminsert = curbuf->b_p_iminsert;
7038}
7039
7040/*
7041 * Set the global value for 'imsearch' to the local value.
7042 */
7043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007044set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 p_imsearch = curbuf->b_p_imsearch;
7047}
7048
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049static int expand_option_idx = -1;
7050static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7051static int expand_option_flags = 0;
7052
7053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007054set_context_in_set_cmd(
7055 expand_T *xp,
7056 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 long_u flags = 0; // init for GCC
7061 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 char_u *p;
7063 char_u *s;
7064 int is_term_option = FALSE;
7065 int key;
7066
7067 expand_option_flags = opt_flags;
7068
7069 xp->xp_context = EXPAND_SETTINGS;
7070 if (*arg == NUL)
7071 {
7072 xp->xp_pattern = arg;
7073 return;
7074 }
7075 p = arg + STRLEN(arg) - 1;
7076 if (*p == ' ' && *(p - 1) != '\\')
7077 {
7078 xp->xp_pattern = p + 1;
7079 return;
7080 }
7081 while (p > arg)
7082 {
7083 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (*p == ' ' || *p == ',')
7086 {
7087 while (s > arg && *(s - 1) == '\\')
7088 --s;
7089 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 if (*p == ' ' && ((p - s) & 1) == 0)
7092 {
7093 ++p;
7094 break;
7095 }
7096 --p;
7097 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007098 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {
7100 xp->xp_context = EXPAND_BOOL_SETTINGS;
7101 p += 2;
7102 }
7103 if (STRNCMP(p, "inv", 3) == 0)
7104 {
7105 xp->xp_context = EXPAND_BOOL_SETTINGS;
7106 p += 3;
7107 }
7108 xp->xp_pattern = arg = p;
7109 if (*arg == '<')
7110 {
7111 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 return;
7114 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007115 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
7117 xp->xp_context = EXPAND_NOTHING;
7118 return;
7119 }
7120 nextchar = *++p;
7121 is_term_option = TRUE;
7122 expand_option_name[2] = KEY2TERMCAP0(key);
7123 expand_option_name[3] = KEY2TERMCAP1(key);
7124 }
7125 else
7126 {
7127 if (p[0] == 't' && p[1] == '_')
7128 {
7129 p += 2;
7130 if (*p != NUL)
7131 ++p;
7132 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 nextchar = *++p;
7135 is_term_option = TRUE;
7136 expand_option_name[2] = p[-2];
7137 expand_option_name[3] = p[-1];
7138 }
7139 else
7140 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007141 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7143 p++;
7144 if (*p == NUL)
7145 return;
7146 nextchar = *p;
7147 *p = NUL;
7148 opt_idx = findoption(arg);
7149 *p = nextchar;
7150 if (opt_idx == -1 || options[opt_idx].var == NULL)
7151 {
7152 xp->xp_context = EXPAND_NOTHING;
7153 return;
7154 }
7155 flags = options[opt_idx].flags;
7156 if (flags & P_BOOL)
7157 {
7158 xp->xp_context = EXPAND_NOTHING;
7159 return;
7160 }
7161 }
7162 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7165 {
7166 ++p;
7167 nextchar = '=';
7168 }
7169 if ((nextchar != '=' && nextchar != ':')
7170 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7171 {
7172 xp->xp_context = EXPAND_UNSUCCESSFUL;
7173 return;
7174 }
7175 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7176 {
7177 xp->xp_context = EXPAND_OLD_SETTING;
7178 if (is_term_option)
7179 expand_option_idx = -1;
7180 else
7181 expand_option_idx = opt_idx;
7182 xp->xp_pattern = p + 1;
7183 return;
7184 }
7185 xp->xp_context = EXPAND_NOTHING;
7186 if (is_term_option || (flags & P_NUM))
7187 return;
7188
7189 xp->xp_pattern = p + 1;
7190
7191 if (flags & P_EXPAND)
7192 {
7193 p = options[opt_idx].var;
7194 if (p == (char_u *)&p_bdir
7195 || p == (char_u *)&p_dir
7196 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007197 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200#ifdef FEAT_SESSION
7201 || p == (char_u *)&p_vdir
7202#endif
7203 )
7204 {
7205 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007206 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 xp->xp_backslash = XP_BS_THREE;
7208 else
7209 xp->xp_backslash = XP_BS_ONE;
7210 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007211 else if (p == (char_u *)&p_ft)
7212 {
7213 xp->xp_context = EXPAND_FILETYPE;
7214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 else
7216 {
7217 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (p == (char_u *)&p_tags)
7220 xp->xp_backslash = XP_BS_THREE;
7221 else
7222 xp->xp_backslash = XP_BS_ONE;
7223 }
7224 }
7225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007226 // For an option that is a list of file names, find the start of the
7227 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7229 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007230 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 if (*p == ' ' || *p == ',')
7232 {
7233 s = p;
7234 while (s > xp->xp_pattern && *(s - 1) == '\\')
7235 --s;
7236 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7237 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7238 {
7239 xp->xp_pattern = p + 1;
7240 break;
7241 }
7242 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007243
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007244#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007245 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007246 if (options[opt_idx].var == (char_u *)&p_sps
7247 && STRNCMP(p, "file:", 5) == 0)
7248 {
7249 xp->xp_pattern = p + 5;
7250 break;
7251 }
7252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007256/*
7257 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7258 *
7259 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7260 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7261 *
7262 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7263 * regular expression 'regmatch', then stores the match in matches[idx] and
7264 * returns TRUE.
7265 *
7266 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7267 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7268 *
7269 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7270 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7271 */
7272 static int
7273match_str(
7274 char_u *str,
7275 regmatch_T *regmatch,
7276 char_u **matches,
7277 int idx,
7278 int test_only,
7279 int fuzzy,
7280 char_u *fuzzystr,
7281 fuzmatch_str_T *fuzmatch)
7282{
7283 if (!fuzzy)
7284 {
7285 if (vim_regexec(regmatch, str, (colnr_T)0))
7286 {
7287 if (!test_only)
7288 matches[idx] = vim_strsave(str);
7289 return TRUE;
7290 }
7291 }
7292 else
7293 {
7294 int score;
7295
7296 score = fuzzy_match_str(str, fuzzystr);
7297 if (score != 0)
7298 {
7299 if (!test_only)
7300 {
7301 fuzmatch[idx].idx = idx;
7302 fuzmatch[idx].str = vim_strsave(str);
7303 fuzmatch[idx].score = score;
7304 }
7305
7306 return TRUE;
7307 }
7308 }
7309
7310 return FALSE;
7311}
7312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007314ExpandSettings(
7315 expand_T *xp,
7316 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007317 char_u *fuzzystr,
7318 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007319 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007320 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 int num_normal = 0; // Nr of matching non-term-code settings
7323 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int opt_idx;
7325 int match;
7326 int count = 0;
7327 char_u *str;
7328 int loop;
7329 int is_term_opt;
7330 char_u name_buf[MAX_KEY_NAME_LEN];
7331 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007332 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007333 int fuzzy;
7334 fuzmatch_str_T *fuzmatch = NULL;
7335
Christian Brabandtcb747892022-05-08 21:10:56 +01007336 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 // do this loop twice:
7339 // loop == 0: count the number of matching options
7340 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 for (loop = 0; loop <= 1; ++loop)
7342 {
7343 regmatch->rm_ic = ic;
7344 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7345 {
K.Takataeeec2542021-06-02 13:28:16 +02007346 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007347 {
7348 if (match_str((char_u *)names[match], regmatch, *matches,
7349 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 {
7351 if (loop == 0)
7352 num_normal++;
7353 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007354 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
7358 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7359 opt_idx++)
7360 {
7361 if (options[opt_idx].var == NULL)
7362 continue;
7363 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7364 && !(options[opt_idx].flags & P_BOOL))
7365 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007366 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (is_term_opt && num_normal > 0)
7368 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007369
7370 if (match_str(str, regmatch, *matches, count, (loop == 0),
7371 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 {
7373 if (loop == 0)
7374 {
7375 if (is_term_opt)
7376 num_term++;
7377 else
7378 num_normal++;
7379 }
7380 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007381 count++;
7382 }
7383 else if (!fuzzy && options[opt_idx].shortname != NULL
7384 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007385 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007386 {
7387 // Compare against the abbreviated option name (for regular
7388 // expression match). Fuzzy matching (previous if) already
7389 // matches against both the expanded and abbreviated names.
7390 if (loop == 0)
7391 {
7392 if (is_term_opt)
7393 num_term++;
7394 else
7395 num_normal++;
7396 }
7397 else
7398 (*matches)[count++] = vim_strsave(str);
7399 }
7400 else if (is_term_opt)
7401 {
7402 name_buf[0] = '<';
7403 name_buf[1] = 't';
7404 name_buf[2] = '_';
7405 name_buf[3] = str[2];
7406 name_buf[4] = str[3];
7407 name_buf[5] = '>';
7408 name_buf[6] = NUL;
7409
7410 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7411 fuzzy, fuzzystr, fuzmatch))
7412 {
7413 if (loop == 0)
7414 num_term++;
7415 else
7416 count++;
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 }
7419 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007420
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 /*
7422 * Check terminal key codes, these are not in the option table
7423 */
7424 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7425 {
7426 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7427 {
7428 if (!isprint(str[0]) || !isprint(str[1]))
7429 continue;
7430
7431 name_buf[0] = 't';
7432 name_buf[1] = '_';
7433 name_buf[2] = str[0];
7434 name_buf[3] = str[1];
7435 name_buf[4] = NUL;
7436
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007437 if (match_str(name_buf, regmatch, *matches, count,
7438 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7439 {
7440 if (loop == 0)
7441 num_term++;
7442 else
7443 count++;
7444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else
7446 {
7447 name_buf[0] = '<';
7448 name_buf[1] = 't';
7449 name_buf[2] = '_';
7450 name_buf[3] = str[0];
7451 name_buf[4] = str[1];
7452 name_buf[5] = '>';
7453 name_buf[6] = NUL;
7454
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007455 if (match_str(name_buf, regmatch, *matches, count,
7456 (loop == 0), fuzzy, fuzzystr,
7457 fuzmatch))
7458 {
7459 if (loop == 0)
7460 num_term++;
7461 else
7462 count++;
7463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465 }
7466
7467 /*
7468 * Check special key names.
7469 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007470 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7472 {
7473 name_buf[0] = '<';
7474 STRCPY(name_buf + 1, str);
7475 STRCAT(name_buf, ">");
7476
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007477 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7478 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
7480 if (loop == 0)
7481 num_term++;
7482 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007483 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485 }
7486 }
7487 if (loop == 0)
7488 {
7489 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007490 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007492 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 else
7494 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007495 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007497 *matches = ALLOC_MULT(char_u *, *numMatches);
7498 if (*matches == NULL)
7499 {
7500 *matches = (char_u **)"";
7501 return FAIL;
7502 }
7503 }
7504 else
7505 {
7506 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7507 if (fuzmatch == NULL)
7508 {
7509 *matches = (char_u **)"";
7510 return FAIL;
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513 }
7514 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007515
7516 if (fuzzy &&
7517 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7518 return FAIL;
7519
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 return OK;
7521}
7522
7523 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007524ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007526 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 char_u *buf;
7528
zeertzjqb0d45ec2023-01-25 15:04:22 +00007529 *numMatches = 0;
7530 *matches = ALLOC_ONE(char_u *);
7531 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 return FAIL;
7533
7534 /*
7535 * For a terminal key code expand_option_idx is < 0.
7536 */
7537 if (expand_option_idx < 0)
7538 {
7539 var = find_termcode(expand_option_name + 2);
7540 if (var == NULL)
7541 expand_option_idx = findoption(expand_option_name);
7542 }
7543
7544 if (expand_option_idx >= 0)
7545 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007546 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 option_value2string(&options[expand_option_idx], expand_option_flags);
7548 var = NameBuff;
7549 }
7550 else if (var == NULL)
7551 var = (char_u *)"";
7552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007553 // A backslash is required before some characters. This is the reverse of
7554 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 buf = vim_strsave_escaped(var, escape_chars);
7556
7557 if (buf == NULL)
7558 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007559 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 return FAIL;
7561 }
7562
7563#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007564 // For MS-Windows et al. we don't double backslashes at the start and
7565 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007566 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 if (var[0] == '\\' && var[1] == '\\'
7568 && expand_option_idx >= 0
7569 && (options[expand_option_idx].flags & P_EXPAND)
7570 && vim_isfilec(var[2])
7571 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007572 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573#endif
7574
zeertzjqb0d45ec2023-01-25 15:04:22 +00007575 *matches[0] = buf;
7576 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 return OK;
7578}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
7580/*
7581 * Get the value for the numeric or string option *opp in a nice format into
7582 * NameBuff[]. Must not be called with a hidden option!
7583 */
7584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007585option_value2string(
7586 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007587 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 char_u *varp;
7590
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007591 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
7593 if (opp->flags & P_NUM)
7594 {
7595 long wc = 0;
7596
7597 if (wc_use_keyname(varp, &wc))
7598 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7599 else if (wc != 0)
7600 STRCPY(NameBuff, transchar((int)wc));
7601 else
7602 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7603 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007604 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
7606 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007607 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 NameBuff[0] = NUL;
7609#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007610 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007611 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 STRCPY(NameBuff, "*****");
7613#endif
7614 else if (opp->flags & P_EXPAND)
7615 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007616 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 else if ((char_u **)opp->var == &p_pt)
7618 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7619 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007620 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622}
7623
7624/*
7625 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7626 * printed as a keyname.
7627 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7628 */
7629 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007630wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
7632 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7633 {
7634 *wcp = *(long *)varp;
7635 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7636 return TRUE;
7637 }
7638 return FALSE;
7639}
7640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 * Return TRUE if "x" is present in 'shortmess' option, or
7643 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7644 */
7645 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007646shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007648 return p_shm != NULL &&
7649 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 || (vim_strchr(p_shm, 'a') != NULL
7651 && vim_strchr((char_u *)SHM_A, x) != NULL));
7652}
7653
7654/*
7655 * paste_option_changed() - Called after p_paste was set or reset.
7656 */
7657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007658paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
7660 static int old_p_paste = FALSE;
7661 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007662 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664#ifdef FEAT_RIGHTLEFT
7665 static int save_ri = 0;
7666 static int save_hkmap = 0;
7667#endif
7668 buf_T *buf;
7669
7670 if (p_paste)
7671 {
7672 /*
7673 * Paste switched from off to on.
7674 * Save the current values, so they can be restored later.
7675 */
7676 if (!old_p_paste)
7677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007678 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007679 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
7681 buf->b_p_tw_nopaste = buf->b_p_tw;
7682 buf->b_p_wm_nopaste = buf->b_p_wm;
7683 buf->b_p_sts_nopaste = buf->b_p_sts;
7684 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007685 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007686#ifdef FEAT_VARTABS
7687 if (buf->b_p_vsts_nopaste)
7688 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007689 buf->b_p_vsts_nopaste =
7690 buf->b_p_vsts && buf->b_p_vsts != empty_option
7691 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
7694
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007695 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007697 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699#ifdef FEAT_RIGHTLEFT
7700 save_ri = p_ri;
7701 save_hkmap = p_hkmap;
7702#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007703 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007704 p_ai_nopaste = p_ai;
7705 p_et_nopaste = p_et;
7706 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 p_tw_nopaste = p_tw;
7708 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007709#ifdef FEAT_VARTABS
7710 if (p_vsts_nopaste)
7711 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007712 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7713 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716
7717 /*
7718 * Always set the option values, also when 'paste' is set when it is
7719 * already on.
7720 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007721 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007722 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007724 buf->b_p_tw = 0; // textwidth is 0
7725 buf->b_p_wm = 0; // wrapmargin is 0
7726 buf->b_p_sts = 0; // softtabstop is 0
7727 buf->b_p_ai = 0; // no auto-indent
7728 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007729#ifdef FEAT_VARTABS
7730 if (buf->b_p_vsts)
7731 free_string_option(buf->b_p_vsts);
7732 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007733 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
7736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007737 // set global options
7738 p_sm = 0; // no showmatch
7739 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007741 status_redraw_all(); // redraw to remove the ruler
7742 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007744 p_ri = 0; // no reverse insert
7745 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007747 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 p_tw = 0;
7749 p_wm = 0;
7750 p_sts = 0;
7751 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007752#ifdef FEAT_VARTABS
7753 if (p_vsts)
7754 free_string_option(p_vsts);
7755 p_vsts = empty_option;
7756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758
7759 /*
7760 * Paste switched from on to off: Restore saved values.
7761 */
7762 else if (old_p_paste)
7763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007764 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007765 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {
7767 buf->b_p_tw = buf->b_p_tw_nopaste;
7768 buf->b_p_wm = buf->b_p_wm_nopaste;
7769 buf->b_p_sts = buf->b_p_sts_nopaste;
7770 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007771 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007772#ifdef FEAT_VARTABS
7773 if (buf->b_p_vsts)
7774 free_string_option(buf->b_p_vsts);
7775 buf->b_p_vsts = buf->b_p_vsts_nopaste
7776 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007777 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007778 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007779 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007780 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007781 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007785 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007787 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007789 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791#ifdef FEAT_RIGHTLEFT
7792 p_ri = save_ri;
7793 p_hkmap = save_hkmap;
7794#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007795 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007796 p_ai = p_ai_nopaste;
7797 p_et = p_et_nopaste;
7798 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 p_tw = p_tw_nopaste;
7800 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007801#ifdef FEAT_VARTABS
7802 if (p_vsts)
7803 free_string_option(p_vsts);
7804 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807
7808 old_p_paste = p_paste;
7809}
7810
7811/*
7812 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7813 *
7814 * Reset 'compatible' and set the values for options that didn't get set yet
7815 * to the Vim defaults.
7816 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007817 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 */
7819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007820vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007822 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007823 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825
7826 if (!option_was_set((char_u *)"cp"))
7827 {
7828 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007829 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7831 set_option_default(opt_idx, OPT_FREE, FALSE);
7832 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007833 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007835
7836 if (fname != NULL)
7837 {
7838 p = vim_getenv(envname, &dofree);
7839 if (p == NULL)
7840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007841 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007842 p = FullName_save(fname, FALSE);
7843 if (p != NULL)
7844 {
7845 vim_setenv(envname, p);
7846 vim_free(p);
7847 }
7848 }
7849 else if (dofree)
7850 vim_free(p);
7851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
7854/*
7855 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7856 */
7857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007858change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007860 int opt_idx;
7861
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if (p_cp != on)
7863 {
7864 p_cp = on;
7865 compatible_set();
7866 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007867 opt_idx = findoption((char_u *)"cp");
7868 if (opt_idx >= 0)
7869 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870}
7871
7872/*
7873 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007874 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 */
7876 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007877option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
7879 int idx;
7880
7881 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007882 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 return FALSE;
7884 if (options[idx].flags & P_WAS_SET)
7885 return TRUE;
7886 return FALSE;
7887}
7888
7889/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007890 * Reset the flag indicating option "name" was set.
7891 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007892 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007893reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007894{
7895 int idx = findoption(name);
7896
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007897 if (idx < 0)
7898 return FAIL;
7899
7900 options[idx].flags &= ~P_WAS_SET;
7901 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007902}
7903
7904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 * compatible_set() - Called when 'compatible' has been set or unset.
7906 *
7907 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7908 * flag) to a Vi compatible value.
7909 * When 'compatible' is unset: Set all options that have a different default
7910 * for Vim (without the P_VI_DEF flag) to that default.
7911 */
7912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007913compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 int opt_idx;
7916
Bram Moolenaardac13472019-09-16 21:06:21 +02007917 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7919 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7920 set_option_default(opt_idx, OPT_FREE, p_cp);
7921 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007922 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923}
7924
Bram Moolenaardac13472019-09-16 21:06:21 +02007925#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927/*
7928 * fill_breakat_flags() -- called when 'breakat' changes value.
7929 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007931fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007933 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 int i;
7935
7936 for (i = 0; i < 256; i++)
7937 breakat_flags[i] = FALSE;
7938
7939 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007940 for (p = p_breakat; *p; p++)
7941 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943#endif
7944
7945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 * Check if backspacing over something is allowed.
7947 */
7948 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007949can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007950 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007952#ifdef FEAT_JOB_CHANNEL
7953 if (what == BS_START && bt_prompt(curbuf))
7954 return FALSE;
7955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 switch (*p_bs)
7957 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007958 case '3': return TRUE;
7959 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 case '1': return (what != BS_START);
7961 case '0': return FALSE;
7962 }
7963 return vim_strchr(p_bs, what) != NULL;
7964}
7965
7966/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007967 * Return the effective 'scrolloff' value for the current window, using the
7968 * global value when appropriate.
7969 */
7970 long
7971get_scrolloff_value(void)
7972{
7973 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7974}
7975
7976/*
7977 * Return the effective 'sidescrolloff' value for the current window, using the
7978 * global value when appropriate.
7979 */
7980 long
7981get_sidescrolloff_value(void)
7982{
7983 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7984}
7985
7986/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007987 * Get the local or global value of 'backupcopy'.
7988 */
7989 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007990get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007991{
7992 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7993}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007994
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007995#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007996/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007997 * Get the local or global value of 'formatlistpat'.
7998 */
7999 char_u *
8000get_flp_value(buf_T *buf)
8001{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008002 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8003 return p_flp;
8004 return buf->b_p_flp;
8005}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008006#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008007
8008/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008009 * Get the local or global value of the 'virtualedit' flags.
8010 */
8011 unsigned int
8012get_ve_flags(void)
8013{
Gary Johnson51ad8502021-08-03 18:33:08 +02008014 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8015 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008016}
8017
Bram Moolenaaree857022019-11-09 23:26:40 +01008018#if defined(FEAT_LINEBREAK) || defined(PROTO)
8019/*
8020 * Get the local or global value of 'showbreak'.
8021 */
8022 char_u *
8023get_showbreak_value(win_T *win)
8024{
8025 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8026 return p_sbr;
8027 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8028 return empty_option;
8029 return win->w_p_sbr;
8030}
8031#endif
8032
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008033#if defined(FEAT_EVAL) || defined(PROTO)
8034/*
8035 * Get window or buffer local options.
8036 */
8037 dict_T *
8038get_winbuf_options(int bufopt)
8039{
8040 dict_T *d;
8041 int opt_idx;
8042
8043 d = dict_alloc();
8044 if (d == NULL)
8045 return NULL;
8046
Bram Moolenaardac13472019-09-16 21:06:21 +02008047 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008048 {
8049 struct vimoption *opt = &options[opt_idx];
8050
8051 if ((bufopt && (opt->indir & PV_BUF))
8052 || (!bufopt && (opt->indir & PV_WIN)))
8053 {
8054 char_u *varp = get_varp(opt);
8055
8056 if (varp != NULL)
8057 {
8058 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008059 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008060 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008061 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008062 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008063 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008064 }
8065 }
8066 }
8067
8068 return d;
8069}
8070#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008071
Bram Moolenaardac13472019-09-16 21:06:21 +02008072#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008073/*
8074 * This is called when 'culopt' is changed
8075 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008076 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008077fill_culopt_flags(char_u *val, win_T *wp)
8078{
8079 char_u *p;
8080 char_u culopt_flags_new = 0;
8081
8082 if (val == NULL)
8083 p = wp->w_p_culopt;
8084 else
8085 p = val;
8086 while (*p != NUL)
8087 {
8088 if (STRNCMP(p, "line", 4) == 0)
8089 {
8090 p += 4;
8091 culopt_flags_new |= CULOPT_LINE;
8092 }
8093 else if (STRNCMP(p, "both", 4) == 0)
8094 {
8095 p += 4;
8096 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8097 }
8098 else if (STRNCMP(p, "number", 6) == 0)
8099 {
8100 p += 6;
8101 culopt_flags_new |= CULOPT_NBR;
8102 }
8103 else if (STRNCMP(p, "screenline", 10) == 0)
8104 {
8105 p += 10;
8106 culopt_flags_new |= CULOPT_SCRLINE;
8107 }
8108
8109 if (*p != ',' && *p != NUL)
8110 return FAIL;
8111 if (*p == ',')
8112 ++p;
8113 }
8114
8115 // Can't have both "line" and "screenline".
8116 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8117 return FAIL;
8118 wp->w_p_culopt_flags = culopt_flags_new;
8119
8120 return OK;
8121}
8122#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008123
8124/*
8125 * Get the value of 'magic' adjusted for Vim9 script.
8126 */
8127 int
8128magic_isset(void)
8129{
8130 switch (magic_overruled)
8131 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008132 case OPTION_MAGIC_ON: return TRUE;
8133 case OPTION_MAGIC_OFF: return FALSE;
8134 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008135 }
8136#ifdef FEAT_EVAL
8137 if (in_vim9script())
8138 return TRUE;
8139#endif
8140 return p_magic;
8141}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008142
8143/*
8144 * Set the callback function value for an option that accepts a function name,
8145 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8146 * Returns OK if the option is successfully set to a function, otherwise
8147 * returns FAIL.
8148 */
8149 int
8150option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8151{
8152#ifdef FEAT_EVAL
8153 typval_T *tv;
8154 callback_T cb;
8155
8156 if (optval == NULL || *optval == NUL)
8157 {
8158 free_callback(optcb);
8159 return OK;
8160 }
8161
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008162 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008163 || (STRNCMP(optval, "function(", 9) == 0)
8164 || (STRNCMP(optval, "funcref(", 8) == 0))
8165 // Lambda expression or a funcref
8166 tv = eval_expr(optval, NULL);
8167 else
8168 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008169 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008170 if (tv == NULL)
8171 return FAIL;
8172
8173 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008174 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008175 {
8176 free_tv(tv);
8177 return FAIL;
8178 }
8179
8180 free_callback(optcb);
8181 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008182 if (cb.cb_free_name)
8183 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008184 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008185
8186 // when using Vim9 style "import.funcname" it needs to be expanded to
8187 // "import#funcname".
8188 expand_autload_callback(optcb);
8189
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008190 return OK;
8191#else
8192 return FAIL;
8193#endif
8194}