blob: ee9c9e33bab5aa4e781dc091e5ac1e435d8fa297 [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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005140 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005141 {
5142 int key;
5143
5144 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005145 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005146 {
5147 char_u key_name[2];
5148
5149 if (key < 0)
5150 {
5151 key_name[0] = KEY2TERMCAP0(key);
5152 key_name[1] = KEY2TERMCAP1(key);
5153 }
5154 else
5155 {
5156 key_name[0] = KS_KEY;
5157 key_name[1] = (key & 0xff);
5158 }
5159 add_termcode(key_name, string, FALSE);
5160 if (full_screen)
5161 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005162 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005163 return NULL;
5164 }
5165
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005166 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 else
5169 {
5170 flags = options[opt_idx].flags;
5171#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005172 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005174 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005175 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005176 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005179 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005180 return set_string_option(opt_idx, string, opt_flags);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005181
5182 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5183 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005185 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005187 int idx;
5188
5189 // Either we are given a string or we are setting option
5190 // to zero.
5191 for (idx = 0; string[idx] == '0'; ++idx)
5192 ;
5193 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005194 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005195 // There's another character after zeros or the string
5196 // is empty. In both cases, we are trying to set a
5197 // num option using a string.
5198 semsg(_(e_number_required_after_str_equal_str),
5199 name, string);
5200 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005201
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005204 if (flags & P_NUM)
5205 return set_num_option(opt_idx, varp, number,
5206 NULL, 0, opt_flags);
5207 else
5208 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005212 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213}
5214
5215/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005216 * Call set_option_value() and when an error is returned report it.
5217 */
5218 void
5219set_option_value_give_err(
5220 char_u *name,
5221 long number,
5222 char_u *string,
5223 int opt_flags) // OPT_LOCAL or 0 (both)
5224{
5225 char *errmsg = set_option_value(name, number, string, opt_flags);
5226
5227 if (errmsg != NULL)
5228 emsg(_(errmsg));
5229}
5230
5231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 * Get the terminal code for a terminal option.
5233 * Returns NULL when not found.
5234 */
5235 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005236get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 int opt_idx;
5239 char_u *varp;
5240
5241 if (tname[0] != 't' || tname[1] != '_' ||
5242 tname[2] == NUL || tname[3] == NUL)
5243 return NULL;
5244 if ((opt_idx = findoption(tname)) >= 0)
5245 {
5246 varp = get_varp(&(options[opt_idx]));
5247 if (varp != NULL)
5248 varp = *(char_u **)(varp);
5249 return varp;
5250 }
5251 return find_termcode(tname + 2);
5252}
5253
5254 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005255get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
5257 int i;
5258
5259 i = findoption((char_u *)"hl");
5260 if (i >= 0)
5261 return options[i].def_val[VI_DEFAULT];
5262 return (char_u *)NULL;
5263}
5264
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005265 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005266get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005267{
5268 int i;
5269
5270 i = findoption((char_u *)"enc");
5271 if (i >= 0)
5272 return options[i].def_val[VI_DEFAULT];
5273 return (char_u *)NULL;
5274}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005275
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005276#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005277 int
5278is_option_allocated(char *name)
5279{
5280 int idx = findoption((char_u *)name);
5281
5282 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5283}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005284#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005285
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005286#if defined(FEAT_EVAL) || defined(PROTO)
5287/*
5288 * Return TRUE if "name" is a string option.
5289 * Returns FALSE if option "name" does not exist.
5290 */
5291 int
5292is_string_option(char_u *name)
5293{
5294 int idx = findoption(name);
5295
5296 return idx >= 0 && (options[idx].flags & P_STRING);
5297}
5298#endif
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300/*
5301 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005302 * When "has_lt" is true there is a '<' before "*arg_arg".
5303 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 */
5305 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005306find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005308 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005310 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312 /*
5313 * Don't use get_special_key_code() for t_xx, we don't want it to call
5314 * add_termcap_entry().
5315 */
5316 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5317 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005318 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005322 key = find_special_key(&arg, &modifiers,
5323 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005324 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 key = 0;
5326 }
5327 return key;
5328}
5329
5330/*
5331 * if 'all' == 0: show changed options
5332 * if 'all' == 1: show all normal options
5333 * if 'all' == 2: show all terminal options
5334 */
5335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005336showoptions(
5337 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005338 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 struct vimoption *p;
5341 int col;
5342 int isterm;
5343 char_u *varp;
5344 struct vimoption **items;
5345 int item_count;
5346 int run;
5347 int row, rows;
5348 int cols;
5349 int i;
5350 int len;
5351
5352#define INC 20
5353#define GAP 3
5354
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005355 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (items == NULL)
5357 return;
5358
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005359 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005361 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005363 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005365 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005367 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005370 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * 1. display the short items
5372 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005373 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 */
5375 for (run = 1; run <= 2 && !got_int; ++run)
5376 {
5377 /*
5378 * collect the items in items[]
5379 */
5380 item_count = 0;
5381 for (p = &options[0]; p->fullname != NULL; p++)
5382 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005383 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005384 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005385 continue;
5386
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 varp = NULL;
5388 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005389 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 if (p->indir != PV_NONE && !isterm)
5392 varp = get_varp_scope(p, opt_flags);
5393 }
5394 else
5395 varp = get_varp(p);
5396 if (varp != NULL
5397 && ((all == 2 && isterm)
5398 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005399 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005401 if (opt_flags & OPT_ONECOLUMN)
5402 len = Columns;
5403 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005404 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 else
5406 {
5407 option_value2string(p, opt_flags);
5408 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5409 }
5410 if ((len <= INC - GAP && run == 1) ||
5411 (len > INC - GAP && run == 2))
5412 items[item_count++] = p;
5413 }
5414 }
5415
5416 /*
5417 * display the items
5418 */
5419 if (run == 1)
5420 {
5421 cols = (Columns + GAP - 3) / INC;
5422 if (cols == 0)
5423 cols = 1;
5424 rows = (item_count + cols - 1) / cols;
5425 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005426 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 rows = item_count;
5428 for (row = 0; row < rows && !got_int; ++row)
5429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005430 msg_putchar('\n'); // go to next line
5431 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 break;
5433 col = 0;
5434 for (i = row; i < item_count; i += rows)
5435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 showoneopt(items[i], opt_flags);
5438 col += INC;
5439 }
5440 out_flush();
5441 ui_breakcheck();
5442 }
5443 }
5444 vim_free(items);
5445}
5446
5447/*
5448 * Return TRUE if option "p" has its default value.
5449 */
5450 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005451optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 int dvi;
5454
5455 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005456 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005457 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005459 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005461 // the cast to long is required for Manx C, long_i is
5462 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005463 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005464 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5466}
5467
5468/*
5469 * showoneopt: show the value of one option
5470 * must not be called with a hidden option!
5471 */
5472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005473showoneopt(
5474 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005475 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005477 char_u *varp;
5478 int save_silent = silent_mode;
5479
5480 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005481 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 varp = get_varp_scope(p, opt_flags);
5484
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005485 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5487 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005488 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005490 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005492 msg_puts(" ");
5493 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (!(p->flags & P_BOOL))
5495 {
5496 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 option_value2string(p, opt_flags);
5499 msg_outtrans(NameBuff);
5500 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005501
5502 silent_mode = save_silent;
5503 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504}
5505
5506/*
5507 * Write modified options as ":set" commands to a file.
5508 *
5509 * There are three values for "opt_flags":
5510 * OPT_GLOBAL: Write global option values and fresh values of
5511 * buffer-local options (used for start of a session
5512 * file).
5513 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5514 * curwin (used for a vimrc file).
5515 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5516 * and local values for window-local options of
5517 * curwin. Local values are also written when at the
5518 * default value, because a modeline or autocommand
5519 * may have set them when doing ":edit file" and the
5520 * user has set them back at the default or fresh
5521 * value.
5522 * When "local_only" is TRUE, don't write fresh
5523 * values, only local values (for ":mkview").
5524 * (fresh value = value used for a new buffer or window for a local option).
5525 *
5526 * Return FAIL on error, OK otherwise.
5527 */
5528 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005529makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005532 char_u *varp; // currently used value
5533 char_u *varp_fresh; // local value
5534 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 char *cmd;
5536 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005537 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
5539 /*
5540 * The options that don't have a default (terminal name, columns, lines)
5541 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005542 * Do the loop over "options[]" twice: once for options with the
5543 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005545 for (pri = 1; pri >= 0; --pri)
5546 {
5547 for (p = &options[0]; !istermoption(p); p++)
5548 if (!(p->flags & P_NO_MKRC)
5549 && !istermoption(p)
5550 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005552 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5554 continue;
5555
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005556 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5557 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5559 continue;
5560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005561 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005563 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 continue;
5565
Bram Moolenaard23b7142021-04-17 21:04:34 +02005566 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5567 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005568 continue;
5569
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 round = 2;
5571 if (p->indir != PV_NONE)
5572 {
5573 if (p->var == VAR_WIN)
5574 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 if (!(opt_flags & OPT_LOCAL))
5577 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005578 // When fresh value of window-local option is not at the
5579 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5581 {
5582 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005583 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 round = 1;
5586 varp_local = varp;
5587 varp = varp_fresh;
5588 }
5589 }
5590 }
5591 }
5592
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 // Round 1: fresh value for window-local options.
5594 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 for ( ; round <= 2; varp = varp_local, ++round)
5596 {
5597 if (round == 1 || (opt_flags & OPT_GLOBAL))
5598 cmd = "set";
5599 else
5600 cmd = "setlocal";
5601
5602 if (p->flags & P_BOOL)
5603 {
5604 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5605 return FAIL;
5606 }
5607 else if (p->flags & P_NUM)
5608 {
5609 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5610 return FAIL;
5611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005612 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005614 int do_endif = FALSE;
5615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005616 // Don't set 'syntax' and 'filetype' again if the value is
5617 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005618 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005619#if defined(FEAT_SYN_HL)
5620 p->indir == PV_SYN ||
5621#endif
5622 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
5624 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5625 *(char_u **)(varp)) < 0
5626 || put_eol(fd) < 0)
5627 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005628 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005631 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005633 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
5635 if (put_line(fd, "endif") == FAIL)
5636 return FAIL;
5637 }
5638 }
5639 }
5640 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 return OK;
5643}
5644
5645#if defined(FEAT_FOLDING) || defined(PROTO)
5646/*
5647 * Generate set commands for the local fold options only. Used when
5648 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5649 */
5650 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005651makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005653 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005655 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 == FAIL
5657# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005658 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005660 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 == FAIL
5662 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5663 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5664 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5665 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5666 )
5667 return FAIL;
5668
5669 return OK;
5670}
5671#endif
5672
5673 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005674put_setstring(
5675 FILE *fd,
5676 char *cmd,
5677 char *name,
5678 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005679 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005682 char_u *buf = NULL;
5683 char_u *part = NULL;
5684 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5687 return FAIL;
5688 if (*valuep != NULL)
5689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005690 // Output 'pastetoggle' as key names. For other
5691 // options some characters have to be escaped with
5692 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 if (valuep == &p_pt)
5694 {
5695 s = *valuep;
5696 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005697 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 return FAIL;
5699 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005700 // expand the option value, replace $HOME by ~
5701 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005703 int size = (int)STRLEN(*valuep) + 1;
5704
5705 // replace home directory in the whole option value into "buf"
5706 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005707 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005708 goto fail;
5709 home_replace(NULL, *valuep, buf, size, FALSE);
5710
5711 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005712 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005713 // can be expanded when read back.
5714 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5715 && vim_strchr(*valuep, ',') != NULL)
5716 {
5717 part = alloc(size);
5718 if (part == NULL)
5719 goto fail;
5720
5721 // write line break to clear the option, e.g. ':set rtp='
5722 if (put_eol(fd) == FAIL)
5723 goto fail;
5724
5725 p = buf;
5726 while (*p != NUL)
5727 {
5728 // for each comma separated option part, append value to
5729 // the option, :set rtp+=value
5730 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5731 goto fail;
5732 (void)copy_option_part(&p, part, size, ",");
5733 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5734 goto fail;
5735 }
5736 vim_free(buf);
5737 vim_free(part);
5738 return OK;
5739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005741 {
5742 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005744 }
5745 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747 else if (put_escstr(fd, *valuep, 2) == FAIL)
5748 return FAIL;
5749 }
5750 if (put_eol(fd) < 0)
5751 return FAIL;
5752 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005753fail:
5754 vim_free(buf);
5755 vim_free(part);
5756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
5758
5759 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005760put_setnum(
5761 FILE *fd,
5762 char *cmd,
5763 char *name,
5764 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 long wc;
5767
5768 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5769 return FAIL;
5770 if (wc_use_keyname((char_u *)valuep, &wc))
5771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005772 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5774 return FAIL;
5775 }
5776 else if (fprintf(fd, "%ld", *valuep) < 0)
5777 return FAIL;
5778 if (put_eol(fd) < 0)
5779 return FAIL;
5780 return OK;
5781}
5782
5783 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005784put_setbool(
5785 FILE *fd,
5786 char *cmd,
5787 char *name,
5788 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005790 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005791 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5793 || put_eol(fd) < 0)
5794 return FAIL;
5795 return OK;
5796}
5797
5798/*
5799 * Clear all the terminal options.
5800 * If the option has been allocated, free the memory.
5801 * Terminal options are never hidden or indirect.
5802 */
5803 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005804clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 /*
5807 * Reset a few things before clearing the old options. This may cause
5808 * outputting a few things that the terminal doesn't understand, but the
5809 * screen will be cleared later, so this is OK.
5810 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005811 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005812 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005814 // When starting the GUI close the display opened for the clipboard.
5815 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (gui.starting)
5817 clear_xterm_clip();
5818#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005819 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005821 free_termoptions();
5822}
5823
5824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005825free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005826{
5827 struct vimoption *p;
5828
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005829 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (istermoption(p))
5831 {
5832 if (p->flags & P_ALLOCED)
5833 free_string_option(*(char_u **)(p->var));
5834 if (p->flags & P_DEF_ALLOCED)
5835 free_string_option(p->def_val[VI_DEFAULT]);
5836 *(char_u **)(p->var) = empty_option;
5837 p->def_val[VI_DEFAULT] = empty_option;
5838 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005839#ifdef FEAT_EVAL
5840 // remember where the option was cleared
5841 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844 clear_termcodes();
5845}
5846
5847/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005848 * Free the string for one term option, if it was allocated.
5849 * Set the string to empty_option and clear allocated flag.
5850 * "var" points to the option value.
5851 */
5852 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005853free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005854{
5855 struct vimoption *p;
5856
5857 for (p = &options[0]; p->fullname != NULL; p++)
5858 if (p->var == var)
5859 {
5860 if (p->flags & P_ALLOCED)
5861 free_string_option(*(char_u **)(p->var));
5862 *(char_u **)(p->var) = empty_option;
5863 p->flags &= ~P_ALLOCED;
5864 break;
5865 }
5866}
5867
5868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 * Set the terminal option defaults to the current value.
5870 * Used after setting the terminal name.
5871 */
5872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005873set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 struct vimoption *p;
5876
5877 for (p = &options[0]; p->fullname != NULL; p++)
5878 {
5879 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5880 {
5881 if (p->flags & P_DEF_ALLOCED)
5882 {
5883 free_string_option(p->def_val[VI_DEFAULT]);
5884 p->flags &= ~P_DEF_ALLOCED;
5885 }
5886 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5887 if (p->flags & P_ALLOCED)
5888 {
5889 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005890 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
5892 }
5893 }
5894}
5895
5896/*
5897 * return TRUE if 'p' starts with 't_'
5898 */
5899 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005900istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5903}
5904
Bram Moolenaardac13472019-09-16 21:06:21 +02005905/*
5906 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5907 */
5908 int
5909istermoption_idx(int opt_idx)
5910{
5911 return istermoption(&options[opt_idx]);
5912}
5913
Bram Moolenaar113e1072019-01-20 15:30:40 +01005914#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005916 * Unset local option value, similar to ":set opt<".
5917 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005919unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005920{
5921 struct vimoption *p;
5922 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005923 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005924
5925 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005926 if (opt_idx < 0)
5927 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005928 p = &(options[opt_idx]);
5929
5930 switch ((int)p->indir)
5931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005932 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005933 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005934 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005935 break;
5936 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005937 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005938 break;
5939 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005940 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005941 break;
5942 case PV_AR:
5943 buf->b_p_ar = -1;
5944 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005945 case PV_BKC:
5946 clear_string_option(&buf->b_p_bkc);
5947 buf->b_bkc_flags = 0;
5948 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005949 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005950 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005951 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005952 case PV_TC:
5953 clear_string_option(&buf->b_p_tc);
5954 buf->b_tc_flags = 0;
5955 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005956 case PV_SISO:
5957 curwin->w_p_siso = -1;
5958 break;
5959 case PV_SO:
5960 curwin->w_p_so = -1;
5961 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005962# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005963 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005964 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005965 break;
5966 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005967 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005968 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005969# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005970 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005971 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005972 break;
5973 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005974 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005975 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005976# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005977 case PV_TSRFU:
5978 clear_string_option(&buf->b_p_tsrfu);
5979 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005980# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005981 case PV_FP:
5982 clear_string_option(&buf->b_p_fp);
5983 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005984# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005985 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005986 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005987 break;
5988 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005989 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005990 break;
5991 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005992 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005993 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005994# endif
5995# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005996 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005997 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005998 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005999# endif
6000# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006001 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006002 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006003 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006004# endif
6005# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006006 case PV_SBR:
6007 clear_string_option(&((win_T *)from)->w_p_sbr);
6008 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006009# endif
6010# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006011 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006012 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006013 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006014# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006015 case PV_UL:
6016 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6017 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006018 case PV_LW:
6019 clear_string_option(&buf->b_p_lw);
6020 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006021 case PV_MENC:
6022 clear_string_option(&buf->b_p_menc);
6023 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006024 case PV_LCS:
6025 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006026 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006027 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006028 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006029 case PV_FCS:
6030 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006031 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006032 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006033 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006034 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006035 clear_string_option(&((win_T *)from)->w_p_ve);
6036 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006037 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006038 }
6039}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006040#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006041
6042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006044 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 */
6046 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006047get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006049 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 {
6051 if (p->var == VAR_WIN)
6052 return (char_u *)GLOBAL_WO(get_varp(p));
6053 return p->var;
6054 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006055 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 {
6057 switch ((int)p->indir)
6058 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006059 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006061 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6062 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6063 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006065 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6066 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6067 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006068 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006069 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006070 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006071 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6072 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006074 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6075 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006077 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6078 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006079#ifdef FEAT_COMPL_FUNC
6080 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6081#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006082#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6083 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6084#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006085#if defined(FEAT_CRYPT)
6086 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6087#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006088#ifdef FEAT_LINEBREAK
6089 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6090#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006091#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006092 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006093#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006094 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006095 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006096 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006097 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006098 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006099 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006100 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006103 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
6105 return get_varp(p);
6106}
6107
6108/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006109 * Get pointer to option variable at 'opt_idx', depending on local or global
6110 * scope.
6111 */
6112 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006113get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006114{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006115 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006116}
6117
6118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 * Get pointer to option variable.
6120 */
6121 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006122get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006124 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (p->var == NULL)
6126 return NULL;
6127
6128 switch ((int)p->indir)
6129 {
6130 case PV_NONE: return p->var;
6131
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006132 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006133 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006135 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006137 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006139 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006141 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006143 case PV_TC: return *curbuf->b_p_tc != NUL
6144 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006145 case PV_BKC: return *curbuf->b_p_bkc != NUL
6146 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006147 case PV_SISO: return curwin->w_p_siso >= 0
6148 ? (char_u *)&(curwin->w_p_siso) : p->var;
6149 case PV_SO: return curwin->w_p_so >= 0
6150 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006152 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006154 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6156#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006157 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006159 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006161#ifdef FEAT_COMPL_FUNC
6162 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6163 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6164#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006165 case PV_FP: return *curbuf->b_p_fp != NUL
6166 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006168 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006170 case PV_GP: return *curbuf->b_p_gp != NUL
6171 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6172 case PV_MP: return *curbuf->b_p_mp != NUL
6173 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006175#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6176 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6177 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6178#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006179#if defined(FEAT_CRYPT)
6180 case PV_CM: return *curbuf->b_p_cm != NUL
6181 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6182#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006183#ifdef FEAT_LINEBREAK
6184 case PV_SBR: return *curwin->w_p_sbr != NUL
6185 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6186#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006187#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006188 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006189 ? (char_u *)&(curwin->w_p_stl) : p->var;
6190#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006191 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6192 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006193 case PV_LW: return *curbuf->b_p_lw != NUL
6194 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006195 case PV_MENC: return *curbuf->b_p_menc != NUL
6196 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197#ifdef FEAT_ARABIC
6198 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6199#endif
6200 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006201 case PV_LCS: return *curwin->w_p_lcs != NUL
6202 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006203 case PV_FCS: return *curwin->w_p_fcs != NUL
6204 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006205 case PV_VE: return *curwin->w_p_ve != NUL
6206 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006207#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006208 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6209#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006210#ifdef FEAT_SYN_HL
6211 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6212 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006213 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006214 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216#ifdef FEAT_DIFF
6217 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6218#endif
6219#ifdef FEAT_FOLDING
6220 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6221 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6222 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6223 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6224 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6225 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6226 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6227# ifdef FEAT_EVAL
6228 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6229 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6230# endif
6231 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6232#endif
6233 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006234 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006235#ifdef FEAT_LINEBREAK
6236 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006239 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006240#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6242#endif
6243#ifdef FEAT_RIGHTLEFT
6244 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6245 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6246#endif
6247 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006248 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6250#ifdef FEAT_LINEBREAK
6251 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006252 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6253 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006255 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006257 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006258#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006259 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6260 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6261#endif
6262#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006263 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6264 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6265 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267
6268 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6269 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6272 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6274 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6276 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6277 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006278 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281#ifdef FEAT_FOLDING
6282 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006285#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006286 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006288#ifdef FEAT_COMPL_FUNC
6289 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006290 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006291#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006292#ifdef FEAT_EVAL
6293 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6294#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006295 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006297 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006303 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6305 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6306 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6307 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6308#ifdef FEAT_FIND_ID
6309# ifdef FEAT_EVAL
6310 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6311# endif
6312#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006313#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6315 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6316#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006317#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006318 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#ifdef FEAT_CRYPT
6321 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006324 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6326 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6327 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6328 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6329 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006331 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6338#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006339 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006340 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6341#endif
6342#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006343 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6344 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6345 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006346 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347#endif
6348 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6349 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6350 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6351 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006352#ifdef FEAT_PERSISTENT_UNDO
6353 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6356#ifdef FEAT_KEYMAP
6357 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6358#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006359#ifdef FEAT_SIGNS
6360 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6361#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006362#ifdef FEAT_VARTABS
6363 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6364 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6365#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006366 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006368 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 return (char_u *)&(curbuf->b_p_wm);
6370}
6371
6372/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006373 * Return a pointer to the variable for option at 'opt_idx'
6374 */
6375 char_u *
6376get_option_var(int opt_idx)
6377{
6378 return options[opt_idx].var;
6379}
6380
Dominique Pelle748b3082022-01-08 12:41:16 +00006381#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006382/*
6383 * Return the full name of the option at 'opt_idx'
6384 */
6385 char_u *
6386get_option_fullname(int opt_idx)
6387{
6388 return (char_u *)options[opt_idx].fullname;
6389}
Dominique Pelle748b3082022-01-08 12:41:16 +00006390#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006391
6392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * Get the value of 'equalprg', either the buffer-local one or the global one.
6394 */
6395 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006396get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 if (*curbuf->b_p_ep == NUL)
6399 return p_ep;
6400 return curbuf->b_p_ep;
6401}
6402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403/*
6404 * Copy options from one window to another.
6405 * Used when splitting a window.
6406 */
6407 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006408win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6411 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006412 after_copy_winopt(wp_to);
6413}
6414
6415/*
6416 * After copying window options: update variables depending on options.
6417 */
6418 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006419after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006420{
6421#ifdef FEAT_LINEBREAK
6422 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006423#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006424#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006425 fill_culopt_flags(NULL, wp);
6426 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006427#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006428 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6429 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006430}
6431
6432 static char_u *
6433copy_option_val(char_u *val)
6434{
6435 if (val == empty_option)
6436 return empty_option; // no need to allocate memory
6437 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
6440/*
6441 * Copy the options from one winopt_T to another.
6442 * Doesn't free the old option values in "to", use clear_winopt() for that.
6443 * The 'scroll' option is not copied, because it depends on the window height.
6444 * The 'previewwindow' option is reset, there can be only one preview window.
6445 */
6446 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006447copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449#ifdef FEAT_ARABIC
6450 to->wo_arab = from->wo_arab;
6451#endif
6452 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006453 to->wo_lcs = copy_option_val(from->wo_lcs);
6454 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006456 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006457 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006458 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006459#ifdef FEAT_LINEBREAK
6460 to->wo_nuw = from->wo_nuw;
6461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462#ifdef FEAT_RIGHTLEFT
6463 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006464 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006466#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006467 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006468#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006469#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006470 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006473#ifdef FEAT_DIFF
6474 to->wo_wrap_save = from->wo_wrap_save;
6475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476#ifdef FEAT_LINEBREAK
6477 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006478 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006479 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006481 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006483 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006484 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006485 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006486 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006487#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006488 to->wo_spell = from->wo_spell;
6489#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006490#ifdef FEAT_SYN_HL
6491 to->wo_cuc = from->wo_cuc;
6492 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006493 to->wo_culopt = copy_option_val(from->wo_culopt);
6494 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496#ifdef FEAT_DIFF
6497 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006498 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006500#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006501 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006502 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006503#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006504#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006505 to->wo_twk = copy_option_val(from->wo_twk);
6506 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508#ifdef FEAT_FOLDING
6509 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006510 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006512 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006513 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 to->wo_fml = from->wo_fml;
6515 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006516 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006517 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006518 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006519 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 to->wo_fdn = from->wo_fdn;
6521# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006522 to->wo_fde = copy_option_val(from->wo_fde);
6523 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006525 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006527#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006528 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006529#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006530
6531#ifdef FEAT_EVAL
6532 // Copy the script context so that we know where the value was last set.
6533 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6534 sizeof(to->wo_script_ctx));
6535#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006536 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537}
6538
6539/*
6540 * Check string options in a window for a NULL value.
6541 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006543check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 check_winopt(&win->w_onebuf_opt);
6546 check_winopt(&win->w_allbuf_opt);
6547}
6548
6549/*
6550 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6551 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006552 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006553check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555#ifdef FEAT_FOLDING
6556 check_string_option(&wop->wo_fdi);
6557 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006558 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559# ifdef FEAT_EVAL
6560 check_string_option(&wop->wo_fde);
6561 check_string_option(&wop->wo_fdt);
6562# endif
6563 check_string_option(&wop->wo_fmr);
6564#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006565#ifdef FEAT_SIGNS
6566 check_string_option(&wop->wo_scl);
6567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568#ifdef FEAT_RIGHTLEFT
6569 check_string_option(&wop->wo_rlc);
6570#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006571#ifdef FEAT_LINEBREAK
6572 check_string_option(&wop->wo_sbr);
6573#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006574#ifdef FEAT_STL_OPT
6575 check_string_option(&wop->wo_stl);
6576#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006577#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006578 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006579 check_string_option(&wop->wo_cc);
6580#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006581#ifdef FEAT_CONCEAL
6582 check_string_option(&wop->wo_cocu);
6583#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006584#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006585 check_string_option(&wop->wo_twk);
6586 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006587#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006588#ifdef FEAT_LINEBREAK
6589 check_string_option(&wop->wo_briopt);
6590#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006591 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006592 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006593 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006594 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595}
6596
6597/*
6598 * Free the allocated memory inside a winopt_T.
6599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006601clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
6603#ifdef FEAT_FOLDING
6604 clear_string_option(&wop->wo_fdi);
6605 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006606 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607# ifdef FEAT_EVAL
6608 clear_string_option(&wop->wo_fde);
6609 clear_string_option(&wop->wo_fdt);
6610# endif
6611 clear_string_option(&wop->wo_fmr);
6612#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006613#ifdef FEAT_SIGNS
6614 clear_string_option(&wop->wo_scl);
6615#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006616#ifdef FEAT_LINEBREAK
6617 clear_string_option(&wop->wo_briopt);
6618#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006619 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620#ifdef FEAT_RIGHTLEFT
6621 clear_string_option(&wop->wo_rlc);
6622#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006623#ifdef FEAT_LINEBREAK
6624 clear_string_option(&wop->wo_sbr);
6625#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006626#ifdef FEAT_STL_OPT
6627 clear_string_option(&wop->wo_stl);
6628#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006629#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006630 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006631 clear_string_option(&wop->wo_cc);
6632#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006633#ifdef FEAT_CONCEAL
6634 clear_string_option(&wop->wo_cocu);
6635#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006636#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006637 clear_string_option(&wop->wo_twk);
6638 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006639#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006640 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006641 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006642 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643}
6644
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006645#ifdef FEAT_EVAL
6646// Index into the options table for a buffer-local option enum.
6647static int buf_opt_idx[BV_COUNT];
6648# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6649
6650/*
6651 * Initialize buf_opt_idx[] if not done already.
6652 */
6653 static void
6654init_buf_opt_idx(void)
6655{
6656 static int did_init_buf_opt_idx = FALSE;
6657 int i;
6658
6659 if (did_init_buf_opt_idx)
6660 return;
6661 did_init_buf_opt_idx = TRUE;
6662 for (i = 0; !istermoption_idx(i); i++)
6663 if (options[i].indir & PV_BUF)
6664 buf_opt_idx[options[i].indir & PV_MASK] = i;
6665}
6666#else
6667# define COPY_OPT_SCTX(buf, bv)
6668#endif
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670/*
6671 * Copy global option values to local options for one buffer.
6672 * Used when creating a new buffer and sometimes when entering a buffer.
6673 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006674 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6676 * appropriate.
6677 * BCO_NOHELP Don't copy the values to a help buffer.
6678 */
6679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006680buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
6682 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006683 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 int dont_do_help;
6685 int did_isk = FALSE;
6686
6687 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 * Skip this when the option defaults have not been set yet. Happens when
6689 * main() allocates the first buffer.
6690 */
6691 if (p_cpo != NULL)
6692 {
6693 /*
6694 * Always copy when entering and 'cpo' contains 'S'.
6695 * Don't copy when already initialized.
6696 * Don't copy when 'cpo' contains 's' and not entering.
6697 * 'S' BCO_ENTER initialized 's' should_copy
6698 * yes yes X X TRUE
6699 * yes no yes X FALSE
6700 * no X yes X FALSE
6701 * X no no yes FALSE
6702 * X no no no TRUE
6703 * no yes no X TRUE
6704 */
6705 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6706 && (buf->b_p_initialized
6707 || (!(flags & BCO_ENTER)
6708 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6709 should_copy = FALSE;
6710
6711 if (should_copy || (flags & BCO_ALWAYS))
6712 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006713#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006714 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006715 init_buf_opt_idx();
6716#endif
6717 // Don't copy the options specific to a help buffer when
6718 // BCO_NOHELP is given or the options were initialized already
6719 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6721 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006722 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
6724 save_p_isk = buf->b_p_isk;
6725 buf->b_p_isk = NULL;
6726 }
6727 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006728 * Always free the allocated strings. If not already initialized,
6729 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 */
6731 if (!buf->b_p_initialized)
6732 {
6733 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006734 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006737 switch (*p_ffs)
6738 {
6739 case 'm':
6740 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6741 case 'd':
6742 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6743 case 'u':
6744 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6745 default:
6746 buf->b_p_ff = vim_strsave(p_ff);
6747 }
6748 if (buf->b_p_ff != NULL)
6749 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 buf->b_p_bh = empty_option;
6751 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753 else
6754 free_buf_options(buf, FALSE);
6755
6756 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006757 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 buf->b_p_ai_nopaste = p_ai_nopaste;
6759 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006760 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006762 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 buf->b_p_tw_nopaste = p_tw_nopaste;
6764 buf->b_p_tw_nobin = p_tw_nobin;
6765 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006766 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 buf->b_p_wm_nopaste = p_wm_nopaste;
6768 buf->b_p_wm_nobin = p_wm_nobin;
6769 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006770 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006771 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006772 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006773 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006774 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006776 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006778 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006780 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 buf->b_p_ml_nobin = p_ml_nobin;
6782 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006783 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006784 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006785 buf->b_p_swf = FALSE;
6786 else
6787 {
6788 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006789 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006792 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006793#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006794 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006795 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006797#ifdef FEAT_COMPL_FUNC
6798 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006799 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006800 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006801 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006802 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006803 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006804#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006805#ifdef FEAT_EVAL
6806 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006807 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006808 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006811 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006813#ifdef FEAT_VARTABS
6814 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006815 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006816 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006817 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006818 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006819 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006820 buf->b_p_vsts_nopaste = p_vsts_nopaste
6821 ? vim_strsave(p_vsts_nopaste) : NULL;
6822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006824 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006826 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827#ifdef FEAT_FOLDING
6828 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006829 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830#endif
6831 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006832 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006833 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006834 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006835 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6836 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006838 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006840 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006842 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006844 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006847 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006849 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006851 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006852 buf->b_p_cinsd = vim_strsave(p_cinsd);
6853 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006854 buf->b_p_lop = vim_strsave(p_lop);
6855 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006856
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006857 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006860 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006862 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006864 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006866 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006868 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006869 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006870 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006871#endif
6872#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006873 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006874 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006875 (void)compile_cap_prog(&buf->b_s);
6876 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006877 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006878 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006879 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006880 buf->b_s.b_p_spo = vim_strsave(p_spo);
6881 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006883#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006885 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006887 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006889 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006890#if defined(FEAT_EVAL)
6891 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006892 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894#ifdef FEAT_CRYPT
6895 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006896 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006899 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#ifdef FEAT_KEYMAP
6901 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006902 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 buf->b_kmap_state |= KEYMAP_INIT;
6904#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006905#ifdef FEAT_TERMINAL
6906 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006907 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006908#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006909 // This isn't really an option, but copying the langmap and IME
6910 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006912 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006914 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 // options that are normally global but also have a local value
6917 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006919 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006920 buf->b_p_bkc = empty_option;
6921 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922#ifdef FEAT_QUICKFIX
6923 buf->b_p_gp = empty_option;
6924 buf->b_p_mp = empty_option;
6925 buf->b_p_efm = empty_option;
6926#endif
6927 buf->b_p_ep = empty_option;
6928 buf->b_p_kp = empty_option;
6929 buf->b_p_path = empty_option;
6930 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006931 buf->b_p_tc = empty_option;
6932 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933#ifdef FEAT_FIND_ID
6934 buf->b_p_def = empty_option;
6935 buf->b_p_inc = empty_option;
6936# ifdef FEAT_EVAL
6937 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006938 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939# endif
6940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 buf->b_p_dict = empty_option;
6942 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006943#ifdef FEAT_COMPL_FUNC
6944 buf->b_p_tsrfu = empty_option;
6945#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006946 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006947 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006948#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6949 buf->b_p_bexpr = empty_option;
6950#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006951#if defined(FEAT_CRYPT)
6952 buf->b_p_cm = empty_option;
6953#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006954#ifdef FEAT_PERSISTENT_UNDO
6955 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006956 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006957#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006958 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006959 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 /*
6962 * Don't copy the options set by ex_help(), use the saved values,
6963 * when going from a help buffer to a non-help buffer.
6964 * Don't touch these at all when BCO_NOHELP is used and going from
6965 * or to a help buffer.
6966 */
6967 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006970#ifdef FEAT_VARTABS
6971 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006972 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006973 else
6974 buf->b_p_vts_array = NULL;
6975#endif
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 else
6978 {
6979 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006980 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 did_isk = TRUE;
6982 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006983 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006984#ifdef FEAT_VARTABS
6985 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006986 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006987 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006988 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006989 else
6990 buf->b_p_vts_array = NULL;
6991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (buf->b_p_bt[0] == 'h')
6994 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006996 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
6998 }
6999
7000 /*
7001 * When the options should be copied (ignoring BCO_ALWAYS), set the
7002 * flag that indicates that the options have been initialized.
7003 */
7004 if (should_copy)
7005 buf->b_p_initialized = TRUE;
7006 }
7007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007008 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (did_isk)
7010 (void)buf_init_chartab(buf, FALSE);
7011}
7012
7013/*
7014 * Reset the 'modifiable' option and its default value.
7015 */
7016 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007017reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019 int opt_idx;
7020
7021 curbuf->b_p_ma = FALSE;
7022 p_ma = FALSE;
7023 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007024 if (opt_idx >= 0)
7025 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026}
7027
7028/*
7029 * Set the global value for 'iminsert' to the local value.
7030 */
7031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007032set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 p_iminsert = curbuf->b_p_iminsert;
7035}
7036
7037/*
7038 * Set the global value for 'imsearch' to the local value.
7039 */
7040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007041set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 p_imsearch = curbuf->b_p_imsearch;
7044}
7045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046static int expand_option_idx = -1;
7047static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7048static int expand_option_flags = 0;
7049
7050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007051set_context_in_set_cmd(
7052 expand_T *xp,
7053 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 long_u flags = 0; // init for GCC
7058 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 char_u *p;
7060 char_u *s;
7061 int is_term_option = FALSE;
7062 int key;
7063
7064 expand_option_flags = opt_flags;
7065
7066 xp->xp_context = EXPAND_SETTINGS;
7067 if (*arg == NUL)
7068 {
7069 xp->xp_pattern = arg;
7070 return;
7071 }
7072 p = arg + STRLEN(arg) - 1;
7073 if (*p == ' ' && *(p - 1) != '\\')
7074 {
7075 xp->xp_pattern = p + 1;
7076 return;
7077 }
7078 while (p > arg)
7079 {
7080 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 if (*p == ' ' || *p == ',')
7083 {
7084 while (s > arg && *(s - 1) == '\\')
7085 --s;
7086 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007087 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (*p == ' ' && ((p - s) & 1) == 0)
7089 {
7090 ++p;
7091 break;
7092 }
7093 --p;
7094 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007095 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
7097 xp->xp_context = EXPAND_BOOL_SETTINGS;
7098 p += 2;
7099 }
7100 if (STRNCMP(p, "inv", 3) == 0)
7101 {
7102 xp->xp_context = EXPAND_BOOL_SETTINGS;
7103 p += 3;
7104 }
7105 xp->xp_pattern = arg = p;
7106 if (*arg == '<')
7107 {
7108 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 return;
7111 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007112 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {
7114 xp->xp_context = EXPAND_NOTHING;
7115 return;
7116 }
7117 nextchar = *++p;
7118 is_term_option = TRUE;
7119 expand_option_name[2] = KEY2TERMCAP0(key);
7120 expand_option_name[3] = KEY2TERMCAP1(key);
7121 }
7122 else
7123 {
7124 if (p[0] == 't' && p[1] == '_')
7125 {
7126 p += 2;
7127 if (*p != NUL)
7128 ++p;
7129 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007130 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 nextchar = *++p;
7132 is_term_option = TRUE;
7133 expand_option_name[2] = p[-2];
7134 expand_option_name[3] = p[-1];
7135 }
7136 else
7137 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007138 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7140 p++;
7141 if (*p == NUL)
7142 return;
7143 nextchar = *p;
7144 *p = NUL;
7145 opt_idx = findoption(arg);
7146 *p = nextchar;
7147 if (opt_idx == -1 || options[opt_idx].var == NULL)
7148 {
7149 xp->xp_context = EXPAND_NOTHING;
7150 return;
7151 }
7152 flags = options[opt_idx].flags;
7153 if (flags & P_BOOL)
7154 {
7155 xp->xp_context = EXPAND_NOTHING;
7156 return;
7157 }
7158 }
7159 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007160 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7162 {
7163 ++p;
7164 nextchar = '=';
7165 }
7166 if ((nextchar != '=' && nextchar != ':')
7167 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7168 {
7169 xp->xp_context = EXPAND_UNSUCCESSFUL;
7170 return;
7171 }
7172 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7173 {
7174 xp->xp_context = EXPAND_OLD_SETTING;
7175 if (is_term_option)
7176 expand_option_idx = -1;
7177 else
7178 expand_option_idx = opt_idx;
7179 xp->xp_pattern = p + 1;
7180 return;
7181 }
7182 xp->xp_context = EXPAND_NOTHING;
7183 if (is_term_option || (flags & P_NUM))
7184 return;
7185
7186 xp->xp_pattern = p + 1;
7187
7188 if (flags & P_EXPAND)
7189 {
7190 p = options[opt_idx].var;
7191 if (p == (char_u *)&p_bdir
7192 || p == (char_u *)&p_dir
7193 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007194 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197#ifdef FEAT_SESSION
7198 || p == (char_u *)&p_vdir
7199#endif
7200 )
7201 {
7202 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007203 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 xp->xp_backslash = XP_BS_THREE;
7205 else
7206 xp->xp_backslash = XP_BS_ONE;
7207 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007208 else if (p == (char_u *)&p_ft)
7209 {
7210 xp->xp_context = EXPAND_FILETYPE;
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 else
7213 {
7214 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (p == (char_u *)&p_tags)
7217 xp->xp_backslash = XP_BS_THREE;
7218 else
7219 xp->xp_backslash = XP_BS_ONE;
7220 }
7221 }
7222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007223 // For an option that is a list of file names, find the start of the
7224 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007227 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if (*p == ' ' || *p == ',')
7229 {
7230 s = p;
7231 while (s > xp->xp_pattern && *(s - 1) == '\\')
7232 --s;
7233 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7234 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7235 {
7236 xp->xp_pattern = p + 1;
7237 break;
7238 }
7239 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007240
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007241#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007242 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007243 if (options[opt_idx].var == (char_u *)&p_sps
7244 && STRNCMP(p, "file:", 5) == 0)
7245 {
7246 xp->xp_pattern = p + 5;
7247 break;
7248 }
7249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251}
7252
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007253/*
7254 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7255 *
7256 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7257 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7258 *
7259 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7260 * regular expression 'regmatch', then stores the match in matches[idx] and
7261 * returns TRUE.
7262 *
7263 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7264 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7265 *
7266 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7267 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7268 */
7269 static int
7270match_str(
7271 char_u *str,
7272 regmatch_T *regmatch,
7273 char_u **matches,
7274 int idx,
7275 int test_only,
7276 int fuzzy,
7277 char_u *fuzzystr,
7278 fuzmatch_str_T *fuzmatch)
7279{
7280 if (!fuzzy)
7281 {
7282 if (vim_regexec(regmatch, str, (colnr_T)0))
7283 {
7284 if (!test_only)
7285 matches[idx] = vim_strsave(str);
7286 return TRUE;
7287 }
7288 }
7289 else
7290 {
7291 int score;
7292
7293 score = fuzzy_match_str(str, fuzzystr);
7294 if (score != 0)
7295 {
7296 if (!test_only)
7297 {
7298 fuzmatch[idx].idx = idx;
7299 fuzmatch[idx].str = vim_strsave(str);
7300 fuzmatch[idx].score = score;
7301 }
7302
7303 return TRUE;
7304 }
7305 }
7306
7307 return FALSE;
7308}
7309
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007311ExpandSettings(
7312 expand_T *xp,
7313 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007314 char_u *fuzzystr,
7315 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007316 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007317 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007319 int num_normal = 0; // Nr of matching non-term-code settings
7320 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 int opt_idx;
7322 int match;
7323 int count = 0;
7324 char_u *str;
7325 int loop;
7326 int is_term_opt;
7327 char_u name_buf[MAX_KEY_NAME_LEN];
7328 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007329 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007330 int fuzzy;
7331 fuzmatch_str_T *fuzmatch = NULL;
7332
Christian Brabandtcb747892022-05-08 21:10:56 +01007333 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007335 // do this loop twice:
7336 // loop == 0: count the number of matching options
7337 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 for (loop = 0; loop <= 1; ++loop)
7339 {
7340 regmatch->rm_ic = ic;
7341 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7342 {
K.Takataeeec2542021-06-02 13:28:16 +02007343 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007344 {
7345 if (match_str((char_u *)names[match], regmatch, *matches,
7346 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 {
7348 if (loop == 0)
7349 num_normal++;
7350 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007351 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
7355 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7356 opt_idx++)
7357 {
7358 if (options[opt_idx].var == NULL)
7359 continue;
7360 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7361 && !(options[opt_idx].flags & P_BOOL))
7362 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007363 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (is_term_opt && num_normal > 0)
7365 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007366
7367 if (match_str(str, regmatch, *matches, count, (loop == 0),
7368 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 if (loop == 0)
7371 {
7372 if (is_term_opt)
7373 num_term++;
7374 else
7375 num_normal++;
7376 }
7377 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007378 count++;
7379 }
7380 else if (!fuzzy && options[opt_idx].shortname != NULL
7381 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007382 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007383 {
7384 // Compare against the abbreviated option name (for regular
7385 // expression match). Fuzzy matching (previous if) already
7386 // matches against both the expanded and abbreviated names.
7387 if (loop == 0)
7388 {
7389 if (is_term_opt)
7390 num_term++;
7391 else
7392 num_normal++;
7393 }
7394 else
7395 (*matches)[count++] = vim_strsave(str);
7396 }
7397 else if (is_term_opt)
7398 {
7399 name_buf[0] = '<';
7400 name_buf[1] = 't';
7401 name_buf[2] = '_';
7402 name_buf[3] = str[2];
7403 name_buf[4] = str[3];
7404 name_buf[5] = '>';
7405 name_buf[6] = NUL;
7406
7407 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7408 fuzzy, fuzzystr, fuzmatch))
7409 {
7410 if (loop == 0)
7411 num_term++;
7412 else
7413 count++;
7414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007417
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 /*
7419 * Check terminal key codes, these are not in the option table
7420 */
7421 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7422 {
7423 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7424 {
7425 if (!isprint(str[0]) || !isprint(str[1]))
7426 continue;
7427
7428 name_buf[0] = 't';
7429 name_buf[1] = '_';
7430 name_buf[2] = str[0];
7431 name_buf[3] = str[1];
7432 name_buf[4] = NUL;
7433
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007434 if (match_str(name_buf, regmatch, *matches, count,
7435 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7436 {
7437 if (loop == 0)
7438 num_term++;
7439 else
7440 count++;
7441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 else
7443 {
7444 name_buf[0] = '<';
7445 name_buf[1] = 't';
7446 name_buf[2] = '_';
7447 name_buf[3] = str[0];
7448 name_buf[4] = str[1];
7449 name_buf[5] = '>';
7450 name_buf[6] = NUL;
7451
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007452 if (match_str(name_buf, regmatch, *matches, count,
7453 (loop == 0), fuzzy, fuzzystr,
7454 fuzmatch))
7455 {
7456 if (loop == 0)
7457 num_term++;
7458 else
7459 count++;
7460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462 }
7463
7464 /*
7465 * Check special key names.
7466 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007467 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7469 {
7470 name_buf[0] = '<';
7471 STRCPY(name_buf + 1, str);
7472 STRCAT(name_buf, ">");
7473
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007474 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7475 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 {
7477 if (loop == 0)
7478 num_term++;
7479 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007480 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482 }
7483 }
7484 if (loop == 0)
7485 {
7486 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007487 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007489 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 else
7491 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007492 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007494 *matches = ALLOC_MULT(char_u *, *numMatches);
7495 if (*matches == NULL)
7496 {
7497 *matches = (char_u **)"";
7498 return FAIL;
7499 }
7500 }
7501 else
7502 {
7503 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7504 if (fuzmatch == NULL)
7505 {
7506 *matches = (char_u **)"";
7507 return FAIL;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 }
7511 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007512
7513 if (fuzzy &&
7514 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7515 return FAIL;
7516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 return OK;
7518}
7519
7520 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007521ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007523 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 char_u *buf;
7525
zeertzjqb0d45ec2023-01-25 15:04:22 +00007526 *numMatches = 0;
7527 *matches = ALLOC_ONE(char_u *);
7528 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 return FAIL;
7530
7531 /*
7532 * For a terminal key code expand_option_idx is < 0.
7533 */
7534 if (expand_option_idx < 0)
7535 {
7536 var = find_termcode(expand_option_name + 2);
7537 if (var == NULL)
7538 expand_option_idx = findoption(expand_option_name);
7539 }
7540
7541 if (expand_option_idx >= 0)
7542 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007543 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 option_value2string(&options[expand_option_idx], expand_option_flags);
7545 var = NameBuff;
7546 }
7547 else if (var == NULL)
7548 var = (char_u *)"";
7549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007550 // A backslash is required before some characters. This is the reverse of
7551 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 buf = vim_strsave_escaped(var, escape_chars);
7553
7554 if (buf == NULL)
7555 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007556 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 return FAIL;
7558 }
7559
7560#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007561 // For MS-Windows et al. we don't double backslashes at the start and
7562 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007563 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (var[0] == '\\' && var[1] == '\\'
7565 && expand_option_idx >= 0
7566 && (options[expand_option_idx].flags & P_EXPAND)
7567 && vim_isfilec(var[2])
7568 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007569 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570#endif
7571
zeertzjqb0d45ec2023-01-25 15:04:22 +00007572 *matches[0] = buf;
7573 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 return OK;
7575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
7577/*
7578 * Get the value for the numeric or string option *opp in a nice format into
7579 * NameBuff[]. Must not be called with a hidden option!
7580 */
7581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007582option_value2string(
7583 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007584 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586 char_u *varp;
7587
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007588 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
7590 if (opp->flags & P_NUM)
7591 {
7592 long wc = 0;
7593
7594 if (wc_use_keyname(varp, &wc))
7595 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7596 else if (wc != 0)
7597 STRCPY(NameBuff, transchar((int)wc));
7598 else
7599 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7600 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007601 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 {
7603 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007604 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 NameBuff[0] = NUL;
7606#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007607 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007608 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 STRCPY(NameBuff, "*****");
7610#endif
7611 else if (opp->flags & P_EXPAND)
7612 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007613 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 else if ((char_u **)opp->var == &p_pt)
7615 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7616 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007617 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
7619}
7620
7621/*
7622 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7623 * printed as a keyname.
7624 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7625 */
7626 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007627wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
7629 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7630 {
7631 *wcp = *(long *)varp;
7632 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7633 return TRUE;
7634 }
7635 return FALSE;
7636}
7637
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 * Return TRUE if "x" is present in 'shortmess' option, or
7640 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7641 */
7642 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007643shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007645 return p_shm != NULL &&
7646 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 || (vim_strchr(p_shm, 'a') != NULL
7648 && vim_strchr((char_u *)SHM_A, x) != NULL));
7649}
7650
7651/*
7652 * paste_option_changed() - Called after p_paste was set or reset.
7653 */
7654 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007655paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657 static int old_p_paste = FALSE;
7658 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007659 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#ifdef FEAT_RIGHTLEFT
7662 static int save_ri = 0;
7663 static int save_hkmap = 0;
7664#endif
7665 buf_T *buf;
7666
7667 if (p_paste)
7668 {
7669 /*
7670 * Paste switched from off to on.
7671 * Save the current values, so they can be restored later.
7672 */
7673 if (!old_p_paste)
7674 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007675 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007676 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
7678 buf->b_p_tw_nopaste = buf->b_p_tw;
7679 buf->b_p_wm_nopaste = buf->b_p_wm;
7680 buf->b_p_sts_nopaste = buf->b_p_sts;
7681 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007682 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007683#ifdef FEAT_VARTABS
7684 if (buf->b_p_vsts_nopaste)
7685 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007686 buf->b_p_vsts_nopaste =
7687 buf->b_p_vsts && buf->b_p_vsts != empty_option
7688 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007692 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007694 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696#ifdef FEAT_RIGHTLEFT
7697 save_ri = p_ri;
7698 save_hkmap = p_hkmap;
7699#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007700 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007701 p_ai_nopaste = p_ai;
7702 p_et_nopaste = p_et;
7703 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 p_tw_nopaste = p_tw;
7705 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007706#ifdef FEAT_VARTABS
7707 if (p_vsts_nopaste)
7708 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007709 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7710 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713
7714 /*
7715 * Always set the option values, also when 'paste' is set when it is
7716 * already on.
7717 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007718 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007719 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007721 buf->b_p_tw = 0; // textwidth is 0
7722 buf->b_p_wm = 0; // wrapmargin is 0
7723 buf->b_p_sts = 0; // softtabstop is 0
7724 buf->b_p_ai = 0; // no auto-indent
7725 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007726#ifdef FEAT_VARTABS
7727 if (buf->b_p_vsts)
7728 free_string_option(buf->b_p_vsts);
7729 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007730 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 }
7733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007734 // set global options
7735 p_sm = 0; // no showmatch
7736 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007738 status_redraw_all(); // redraw to remove the ruler
7739 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007741 p_ri = 0; // no reverse insert
7742 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007744 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 p_tw = 0;
7746 p_wm = 0;
7747 p_sts = 0;
7748 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007749#ifdef FEAT_VARTABS
7750 if (p_vsts)
7751 free_string_option(p_vsts);
7752 p_vsts = empty_option;
7753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755
7756 /*
7757 * Paste switched from on to off: Restore saved values.
7758 */
7759 else if (old_p_paste)
7760 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007761 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007762 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {
7764 buf->b_p_tw = buf->b_p_tw_nopaste;
7765 buf->b_p_wm = buf->b_p_wm_nopaste;
7766 buf->b_p_sts = buf->b_p_sts_nopaste;
7767 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007768 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007769#ifdef FEAT_VARTABS
7770 if (buf->b_p_vsts)
7771 free_string_option(buf->b_p_vsts);
7772 buf->b_p_vsts = buf->b_p_vsts_nopaste
7773 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007774 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007775 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007776 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007777 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007778 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007782 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007784 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007786 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#ifdef FEAT_RIGHTLEFT
7789 p_ri = save_ri;
7790 p_hkmap = save_hkmap;
7791#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007792 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007793 p_ai = p_ai_nopaste;
7794 p_et = p_et_nopaste;
7795 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 p_tw = p_tw_nopaste;
7797 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007798#ifdef FEAT_VARTABS
7799 if (p_vsts)
7800 free_string_option(p_vsts);
7801 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804
7805 old_p_paste = p_paste;
7806}
7807
7808/*
7809 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7810 *
7811 * Reset 'compatible' and set the values for options that didn't get set yet
7812 * to the Vim defaults.
7813 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007814 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 */
7816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007817vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007819 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007820 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007821 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
7823 if (!option_was_set((char_u *)"cp"))
7824 {
7825 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007826 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7828 set_option_default(opt_idx, OPT_FREE, FALSE);
7829 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007830 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007832
7833 if (fname != NULL)
7834 {
7835 p = vim_getenv(envname, &dofree);
7836 if (p == NULL)
7837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007838 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007839 p = FullName_save(fname, FALSE);
7840 if (p != NULL)
7841 {
7842 vim_setenv(envname, p);
7843 vim_free(p);
7844 }
7845 }
7846 else if (dofree)
7847 vim_free(p);
7848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849}
7850
7851/*
7852 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7853 */
7854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007855change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007857 int opt_idx;
7858
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (p_cp != on)
7860 {
7861 p_cp = on;
7862 compatible_set();
7863 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007864 opt_idx = findoption((char_u *)"cp");
7865 if (opt_idx >= 0)
7866 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867}
7868
7869/*
7870 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007871 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 */
7873 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007874option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 int idx;
7877
7878 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007879 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 return FALSE;
7881 if (options[idx].flags & P_WAS_SET)
7882 return TRUE;
7883 return FALSE;
7884}
7885
7886/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007887 * Reset the flag indicating option "name" was set.
7888 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007890reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007891{
7892 int idx = findoption(name);
7893
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007894 if (idx < 0)
7895 return FAIL;
7896
7897 options[idx].flags &= ~P_WAS_SET;
7898 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007899}
7900
7901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 * compatible_set() - Called when 'compatible' has been set or unset.
7903 *
7904 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7905 * flag) to a Vi compatible value.
7906 * When 'compatible' is unset: Set all options that have a different default
7907 * for Vim (without the P_VI_DEF flag) to that default.
7908 */
7909 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007910compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 int opt_idx;
7913
Bram Moolenaardac13472019-09-16 21:06:21 +02007914 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7916 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7917 set_option_default(opt_idx, OPT_FREE, p_cp);
7918 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007919 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920}
7921
Bram Moolenaardac13472019-09-16 21:06:21 +02007922#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924/*
7925 * fill_breakat_flags() -- called when 'breakat' changes value.
7926 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007928fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007930 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 int i;
7932
7933 for (i = 0; i < 256; i++)
7934 breakat_flags[i] = FALSE;
7935
7936 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007937 for (p = p_breakat; *p; p++)
7938 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940#endif
7941
7942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 * Check if backspacing over something is allowed.
7944 */
7945 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007946can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007947 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007949#ifdef FEAT_JOB_CHANNEL
7950 if (what == BS_START && bt_prompt(curbuf))
7951 return FALSE;
7952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 switch (*p_bs)
7954 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007955 case '3': return TRUE;
7956 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 case '1': return (what != BS_START);
7958 case '0': return FALSE;
7959 }
7960 return vim_strchr(p_bs, what) != NULL;
7961}
7962
7963/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007964 * Return the effective 'scrolloff' value for the current window, using the
7965 * global value when appropriate.
7966 */
7967 long
7968get_scrolloff_value(void)
7969{
7970 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7971}
7972
7973/*
7974 * Return the effective 'sidescrolloff' value for the current window, using the
7975 * global value when appropriate.
7976 */
7977 long
7978get_sidescrolloff_value(void)
7979{
7980 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7981}
7982
7983/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007984 * Get the local or global value of 'backupcopy'.
7985 */
7986 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007987get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007988{
7989 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7990}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007991
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007992#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007993/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007994 * Get the local or global value of 'formatlistpat'.
7995 */
7996 char_u *
7997get_flp_value(buf_T *buf)
7998{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007999 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8000 return p_flp;
8001 return buf->b_p_flp;
8002}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008003#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008004
8005/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008006 * Get the local or global value of the 'virtualedit' flags.
8007 */
8008 unsigned int
8009get_ve_flags(void)
8010{
Gary Johnson51ad8502021-08-03 18:33:08 +02008011 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8012 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008013}
8014
Bram Moolenaaree857022019-11-09 23:26:40 +01008015#if defined(FEAT_LINEBREAK) || defined(PROTO)
8016/*
8017 * Get the local or global value of 'showbreak'.
8018 */
8019 char_u *
8020get_showbreak_value(win_T *win)
8021{
8022 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8023 return p_sbr;
8024 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8025 return empty_option;
8026 return win->w_p_sbr;
8027}
8028#endif
8029
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008030#if defined(FEAT_EVAL) || defined(PROTO)
8031/*
8032 * Get window or buffer local options.
8033 */
8034 dict_T *
8035get_winbuf_options(int bufopt)
8036{
8037 dict_T *d;
8038 int opt_idx;
8039
8040 d = dict_alloc();
8041 if (d == NULL)
8042 return NULL;
8043
Bram Moolenaardac13472019-09-16 21:06:21 +02008044 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008045 {
8046 struct vimoption *opt = &options[opt_idx];
8047
8048 if ((bufopt && (opt->indir & PV_BUF))
8049 || (!bufopt && (opt->indir & PV_WIN)))
8050 {
8051 char_u *varp = get_varp(opt);
8052
8053 if (varp != NULL)
8054 {
8055 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008056 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008057 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008058 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008059 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008060 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008061 }
8062 }
8063 }
8064
8065 return d;
8066}
8067#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008068
Bram Moolenaardac13472019-09-16 21:06:21 +02008069#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008070/*
8071 * This is called when 'culopt' is changed
8072 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008073 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008074fill_culopt_flags(char_u *val, win_T *wp)
8075{
8076 char_u *p;
8077 char_u culopt_flags_new = 0;
8078
8079 if (val == NULL)
8080 p = wp->w_p_culopt;
8081 else
8082 p = val;
8083 while (*p != NUL)
8084 {
8085 if (STRNCMP(p, "line", 4) == 0)
8086 {
8087 p += 4;
8088 culopt_flags_new |= CULOPT_LINE;
8089 }
8090 else if (STRNCMP(p, "both", 4) == 0)
8091 {
8092 p += 4;
8093 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8094 }
8095 else if (STRNCMP(p, "number", 6) == 0)
8096 {
8097 p += 6;
8098 culopt_flags_new |= CULOPT_NBR;
8099 }
8100 else if (STRNCMP(p, "screenline", 10) == 0)
8101 {
8102 p += 10;
8103 culopt_flags_new |= CULOPT_SCRLINE;
8104 }
8105
8106 if (*p != ',' && *p != NUL)
8107 return FAIL;
8108 if (*p == ',')
8109 ++p;
8110 }
8111
8112 // Can't have both "line" and "screenline".
8113 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8114 return FAIL;
8115 wp->w_p_culopt_flags = culopt_flags_new;
8116
8117 return OK;
8118}
8119#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008120
8121/*
8122 * Get the value of 'magic' adjusted for Vim9 script.
8123 */
8124 int
8125magic_isset(void)
8126{
8127 switch (magic_overruled)
8128 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008129 case OPTION_MAGIC_ON: return TRUE;
8130 case OPTION_MAGIC_OFF: return FALSE;
8131 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008132 }
8133#ifdef FEAT_EVAL
8134 if (in_vim9script())
8135 return TRUE;
8136#endif
8137 return p_magic;
8138}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008139
8140/*
8141 * Set the callback function value for an option that accepts a function name,
8142 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8143 * Returns OK if the option is successfully set to a function, otherwise
8144 * returns FAIL.
8145 */
8146 int
8147option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8148{
8149#ifdef FEAT_EVAL
8150 typval_T *tv;
8151 callback_T cb;
8152
8153 if (optval == NULL || *optval == NUL)
8154 {
8155 free_callback(optcb);
8156 return OK;
8157 }
8158
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008159 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008160 || (STRNCMP(optval, "function(", 9) == 0)
8161 || (STRNCMP(optval, "funcref(", 8) == 0))
8162 // Lambda expression or a funcref
8163 tv = eval_expr(optval, NULL);
8164 else
8165 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008166 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008167 if (tv == NULL)
8168 return FAIL;
8169
8170 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008171 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008172 {
8173 free_tv(tv);
8174 return FAIL;
8175 }
8176
8177 free_callback(optcb);
8178 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008179 if (cb.cb_free_name)
8180 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008181 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008182
8183 // when using Vim9 style "import.funcname" it needs to be expanded to
8184 // "import#funcname".
8185 expand_autload_callback(optcb);
8186
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008187 return OK;
8188#else
8189 return FAIL;
8190#endif
8191}