blob: 1b581ed355bdaaf917c30972a8f31b220db5302d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void paste_option_changed(void);
70static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000073 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000075 static void
76set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000080 // Find default value for 'shell' option.
81 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000082 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010083#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000084 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010085 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +000087 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020088#if defined(MSWIN)
89 {
90 // For MS-Windows put the path in quotes instead of escaping spaces.
91 char_u *cmd;
92 size_t len;
93
94 if (vim_strchr(p, ' ') != NULL)
95 {
96 len = STRLEN(p) + 3; // two quotes and a trailing NUL
97 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +020098 if (cmd != NULL)
99 {
100 vim_snprintf((char *)cmd, len, "\"%s\"", p);
101 set_string_default("sh", cmd);
102 vim_free(cmd);
103 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200104 }
105 else
106 set_string_default("sh", p);
107 }
108#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100109 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200110#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000111}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000113/*
114 * Set the default for 'backupskip' to include environment variables for
115 * temp files.
116 */
117 static void
118set_init_default_backupskip(void)
119{
120 int opt_idx;
121 long_u n;
122 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100123#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000124 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100125#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000126 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100127#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000128 int len;
129 garray_T ga;
130 int mustfree;
131 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200132
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000133 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000135 ga_init2(&ga, 1, 100);
136 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
137 {
138 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100143# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000144 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100147#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 p = vim_getenv((char_u *)names[n], &mustfree);
149 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000151 // First time count the NUL, otherwise count the ','.
152 len = (int)STRLEN(p) + 3;
153 item = alloc(len);
154 STRCPY(item, p);
155 add_pathsep(item);
156 STRCAT(item, "*");
157 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
158 == NULL
159 && ga_grow(&ga, len) == OK)
160 {
161 if (ga.ga_len > 0)
162 STRCAT(ga.ga_data, ",");
163 STRCAT(ga.ga_data, item);
164 ga.ga_len += len;
165 }
166 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 if (mustfree)
169 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000171 if (ga.ga_data != NULL)
172 {
173 set_string_default("bsk", ga.ga_data);
174 vim_free(ga.ga_data);
175 }
176}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000178/*
179 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
180 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
181 */
182 static void
183set_init_default_maxmemtot(void)
184{
185 int opt_idx;
186 long_u n;
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000189 if (opt_idx < 0)
190 return;
191
192#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
193 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {
ichizok02560422022-04-05 14:18:44 +0100196#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000197 // Use amount of memory available at this moment.
198 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100199#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000200 // Use amount of memory available to Vim.
201 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100202#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000203 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
206 opt_idx = findoption((char_u *)"maxmem");
207 if (opt_idx >= 0)
208 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000210 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
211 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000212#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000216}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000218/*
219 * Initialize the 'cdpath' option to a default value.
220 */
221 static void
222set_init_default_cdpath(void)
223{
224 int opt_idx;
225 char_u *cdpath;
226 char_u *buf;
227 int i;
228 int j;
229 int mustfree = FALSE;
230
231 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
232 if (cdpath == NULL)
233 return;
234
235 buf = alloc((STRLEN(cdpath) << 1) + 2);
236 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000238 buf[0] = ','; // start with ",", current dir first
239 j = 1;
240 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000242 if (vim_ispathlistsep(cdpath[i]))
243 buf[j++] = ',';
244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000246 if (cdpath[i] == ' ' || cdpath[i] == ',')
247 buf[j++] = '\\';
248 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 }
250 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000251 buf[j] = NUL;
252 opt_idx = findoption((char_u *)"cdpath");
253 if (opt_idx >= 0)
254 {
255 options[opt_idx].def_val[VI_DEFAULT] = buf;
256 options[opt_idx].flags |= P_DEF_ALLOCED;
257 }
258 else
259 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000261 if (mustfree)
262 vim_free(cdpath);
263}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000265/*
266 * Initialize the 'printencoding' option to a default value.
267 */
268 static void
269set_init_default_printencoding(void)
270{
ichizok02560422022-04-05 14:18:44 +0100271#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000272 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100273 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100275# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000276 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100277# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000278 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100279# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000280 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100281# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000282 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000284 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000286}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287
288#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000289/*
290 * Initialize the 'printexpr' option to a default value.
291 */
292 static void
293set_init_default_printexpr(void)
294{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100295 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100297# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000298 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100299# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
301
ichizok02560422022-04-05 14:18:44 +0100302# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# endif
305 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000306}
307#endif
308
309#ifdef UNIX
310/*
311 * Force restricted-mode on for "nologin" or "false" $SHELL
312 */
313 static void
314set_init_restricted_mode(void)
315{
316 char_u *p;
317
318 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000319 if (p == NULL)
320 return;
321 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
322 restricted = TRUE;
323 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000324}
325#endif
326
327#ifdef CLEAN_RUNTIMEPATH
328/*
329 * When Vim is started with the "--clean" argument, set the default value
330 * for the 'runtimepath' and 'packpath' options.
331 */
332 static void
333set_init_clean_rtp(void)
334{
335 int opt_idx;
336
337 opt_idx = findoption((char_u *)"runtimepath");
338 if (opt_idx >= 0)
339 {
340 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
341 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
342 }
343 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000344 if (opt_idx < 0)
345 return;
346 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
347 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000348}
349#endif
350
351/*
352 * Expand environment variables and things like "~" for the defaults.
353 * If option_expand() returns non-NULL the variable is expanded. This can
354 * only happen for non-indirect options.
355 * Also set the default to the expanded value, so ":set" does not list
356 * them.
357 * Don't set the P_ALLOCED flag, because we don't want to free the
358 * default.
359 */
360 static void
361set_init_expand_env(void)
362{
363 int opt_idx;
364 char_u *p;
365
366 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
367 {
368 if ((options[opt_idx].flags & P_GETTEXT)
369 && options[opt_idx].var != NULL)
370 p = (char_u *)_(*(char **)options[opt_idx].var);
371 else
372 p = option_expand(opt_idx, NULL);
373 if (p != NULL && (p = vim_strsave(p)) != NULL)
374 {
375 *(char_u **)options[opt_idx].var = p;
376 // VIMEXP
377 // Defaults for all expanded options are currently the same for Vi
378 // and Vim. When this changes, add some code here! Also need to
379 // split P_DEF_ALLOCED in two.
380 if (options[opt_idx].flags & P_DEF_ALLOCED)
381 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
382 options[opt_idx].def_val[VI_DEFAULT] = p;
383 options[opt_idx].flags |= P_DEF_ALLOCED;
384 }
385 }
386}
387
388/*
389 * Initialize the 'LANG' environment variable to a default value.
390 */
391 static void
392set_init_lang_env(void)
393{
394#if defined(MSWIN) && defined(FEAT_GETTEXT)
395 // If $LANG isn't set, try to get a good value for it. This makes the
396 // right language be used automatically. Don't do this for English.
397 if (mch_getenv((char_u *)"LANG") == NULL)
398 {
399 char buf[20];
400 long_u n;
401
402 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
403 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
404 // only the first two.
405 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
406 (LPTSTR)buf, 20);
407 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
408 {
409 // There are a few exceptions (probably more)
410 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
411 STRCPY(buf, "zh_TW");
412 else if (STRNICMP(buf, "chs", 3) == 0
413 || STRNICMP(buf, "zhc", 3) == 0)
414 STRCPY(buf, "zh_CN");
415 else if (STRNICMP(buf, "jp", 2) == 0)
416 STRCPY(buf, "ja");
417 else
418 buf[2] = NUL; // truncate to two-letter code
419 vim_setenv((char_u *)"LANG", (char_u *)buf);
420 }
421 }
422#elif defined(MACOS_CONVERT)
423 // Moved to os_mac_conv.c to avoid dependency problems.
424 mac_lang_init();
425#endif
426}
427
428/*
429 * Initialize the 'encoding' option to a default value.
430 */
431 static void
432set_init_default_encoding(void)
433{
434 char_u *p;
435 int opt_idx;
436
437# ifdef MSWIN
438 // MS-Windows has builtin support for conversion to and from Unicode, using
439 // "utf-8" for 'encoding' should work best for most users.
440 p = vim_strsave((char_u *)ENC_DFLT);
441# else
442 // enc_locale() will try to find the encoding of the current locale.
443 // This works best for properly configured systems, old and new.
444 p = enc_locale();
445# endif
446 if (p == NULL)
447 return;
448
449 // Try setting 'encoding' and check if the value is valid.
450 // If not, go back to the default encoding.
451 char_u *save_enc = p_enc;
452 p_enc = p;
453 if (STRCMP(p_enc, "gb18030") == 0)
454 {
455 // We don't support "gb18030", but "cp936" is a good substitute
456 // for practical purposes, thus use that. It's not an alias to
457 // still support conversion between gb18030 and utf-8.
458 p_enc = vim_strsave((char_u *)"cp936");
459 vim_free(p);
460 }
461 if (mb_init() == NULL)
462 {
463 opt_idx = findoption((char_u *)"encoding");
464 if (opt_idx >= 0)
465 {
466 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
467 options[opt_idx].flags |= P_DEF_ALLOCED;
468 }
469
470#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
471 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
472 {
473 // Adjust the default for 'isprint' and 'iskeyword' to match
474 // latin1. Also set the defaults for when 'nocompatible' is
475 // set.
476 set_string_option_direct((char_u *)"isp", -1,
477 ISP_LATIN1, OPT_FREE, SID_NONE);
478 set_string_option_direct((char_u *)"isk", -1,
479 ISK_LATIN1, OPT_FREE, SID_NONE);
480 opt_idx = findoption((char_u *)"isp");
481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
483 opt_idx = findoption((char_u *)"isk");
484 if (opt_idx >= 0)
485 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
486 (void)init_chartab();
487 }
488#endif
489
490#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
491 // Win32 console: When GetACP() returns a different value from
492 // GetConsoleCP() set 'termencoding'.
493 if (
494# ifdef VIMDLL
495 (!gui.in_use && !gui.starting) &&
496# endif
497 GetACP() != GetConsoleCP())
498 {
499 char buf[50];
500
501 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
502 // Use an alternative value.
503 if (GetConsoleCP() == 0)
504 sprintf(buf, "cp%ld", (long)GetACP());
505 else
506 sprintf(buf, "cp%ld", (long)GetConsoleCP());
507 p_tenc = vim_strsave((char_u *)buf);
508 if (p_tenc != NULL)
509 {
510 opt_idx = findoption((char_u *)"termencoding");
511 if (opt_idx >= 0)
512 {
513 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
514 options[opt_idx].flags |= P_DEF_ALLOCED;
515 }
516 convert_setup(&input_conv, p_tenc, p_enc);
517 convert_setup(&output_conv, p_enc, p_tenc);
518 }
519 else
520 p_tenc = empty_option;
521 }
522#endif
523#if defined(MSWIN)
524 // $HOME may have characters in active code page.
525 init_homedir();
526#endif
527 }
528 else
529 {
530 vim_free(p_enc);
531 p_enc = save_enc;
532 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000533}
534
535/*
536 * Initialize the options, first part.
537 *
538 * Called only once from main(), just after creating the first buffer.
539 * If "clean_arg" is TRUE Vim was started with --clean.
540 */
541 void
542set_init_1(int clean_arg)
543{
544#ifdef FEAT_LANGMAP
545 langmap_init();
546#endif
547
548 // Be Vi compatible by default
549 p_cp = TRUE;
550
551 // Use POSIX compatibility when $VIM_POSIX is set.
552 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
553 {
554 set_string_default("cpo", (char_u *)CPO_ALL);
555 set_string_default("shm", (char_u *)SHM_POSIX);
556 }
557
558 set_init_default_shell();
559 set_init_default_backupskip();
560 set_init_default_maxmemtot();
561 set_init_default_cdpath();
562 set_init_default_printencoding();
563#ifdef FEAT_POSTSCRIPT
564 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#endif
566
567 /*
568 * Set all the options (except the terminal options) to their default
569 * value. Also set the global value for local options.
570 */
571 set_options_default(0);
572
matveytadbb1bf2022-02-01 17:26:12 +0000573#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000574 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000575#endif
576
Bram Moolenaar07268702018-03-01 21:57:32 +0100577#ifdef CLEAN_RUNTIMEPATH
578 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000579 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#ifdef FEAT_GUI
583 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100584 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
586
587 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100589 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 check_buf_options(curbuf);
591 check_win_options(curwin);
592 check_options();
593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 didset_options();
596
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000597#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // Use the current chartab for the generic chartab. This is not in
599 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000600 init_spell_chartab();
601#endif
602
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000603 // Expand environment variables and things like "~" for the defaults.
604 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100606 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 // Detect use of mlterm.
610 // Mlterm is a terminal emulator akin to xterm that has some special
611 // abilities (bidi namely).
612 // NOTE: mlterm's author is being asked to 'set' a variable
613 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100615 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#endif
617
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200618 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000620 set_init_lang_env();
621 set_init_default_encoding();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100624 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 set_helplang_default(get_mess_lang());
626#endif
627}
628
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200629static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
630
631/*
632 * Set the "fileencodings" option to the default value for when 'encoding' is
633 * utf-8.
634 */
635 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000636set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200637{
638 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
639 OPT_FREE, 0);
640}
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642/*
643 * Set an option to its default value.
644 * This does not take care of side effects!
645 */
646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100647set_option_default(
648 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100649 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
650 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100652 char_u *varp; // pointer to variable for current option
653 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000655 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
657
658 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
659 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
663 if (flags & P_STRING)
664 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200665 // 'fencs' default value depends on 'encoding'
666 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
667 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 // Use set_string_option_direct() for local options to handle
669 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200670 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200671 set_string_option_direct(NULL, opt_idx,
672 options[opt_idx].def_val[dvi], opt_flags, 0);
673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200675 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
676 free_string_option(*(char_u **)(varp));
677 *(char_u **)varp = options[opt_idx].def_val[dvi];
678 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681 else if (flags & P_NUM)
682 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000683 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 win_comp_scroll(curwin);
685 else
686 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100687 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
688
689 if ((long *)varp == &curwin->w_p_so
690 || (long *)varp == &curwin->w_p_siso)
691 // 'scrolloff' and 'sidescrolloff' local values have a
692 // different default value than the global default.
693 *(long *)varp = -1;
694 else
695 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100696 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (both)
698 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100699 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
701 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100702 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // the cast to long is required for Manx C, long_i is needed for
705 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000706 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000707#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100708 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000709 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
710 *(int *)varp = FALSE;
711#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (both)
714 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
715 *(int *)varp;
716 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100718 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000719 flagsp = insecure_flag(opt_idx, opt_flags);
720 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
722
723#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200724 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#endif
726}
727
728/*
729 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200730 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100733set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100734 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000738 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaardac13472019-09-16 21:06:21 +0200740 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200741 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200742 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100743 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200744# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200745 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200746 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200747# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100748 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 set_option_default(i, opt_flags, p_cp);
750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100751 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000752 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200754 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755}
756
757/*
758 * Set the Vi-default value of a string option.
759 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100760 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100762 static void
763set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 char_u *p;
766 int opt_idx;
767
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100768 if (escape && vim_strchr(val, ' ') != NULL)
769 p = vim_strsave_escaped(val, (char_u *)" ");
770 else
771 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000772 if (p == NULL) // we don't want a NULL
773 return;
774
775 opt_idx = findoption((char_u *)name);
776 if (opt_idx < 0)
777 return;
778
779 if (options[opt_idx].flags & P_DEF_ALLOCED)
780 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
781 options[opt_idx].def_val[VI_DEFAULT] = p;
782 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783}
784
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100785 void
786set_string_default(char *name, char_u *val)
787{
788 set_string_default_esc(name, val, FALSE);
789}
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200792 * For an option value that contains comma separated items, find "newval" in
793 * "origval". Return NULL if not found.
794 */
795 static char_u *
796find_dup_item(char_u *origval, char_u *newval, long_u flags)
797{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200798 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200799 size_t newlen;
800 char_u *s;
801
802 if (origval == NULL)
803 return NULL;
804
805 newlen = STRLEN(newval);
806 for (s = origval; *s != NUL; ++s)
807 {
808 if ((!(flags & P_COMMA)
809 || s == origval
810 || (s[-1] == ',' && !(bs & 1)))
811 && STRNCMP(s, newval, newlen) == 0
812 && (!(flags & P_COMMA)
813 || s[newlen] == ','
814 || s[newlen] == NUL))
815 return s;
816 // Count backslashes. Only a comma with an even number of backslashes
817 // or a single backslash preceded by a comma before it is recognized as
818 // a separator.
819 if ((s > origval + 1
820 && s[-1] == '\\'
821 && s[-2] != ',')
822 || (s == origval + 1
823 && s[-1] == '\\'))
824 ++bs;
825 else
826 bs = 0;
827 }
828 return NULL;
829}
830
831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Set the Vi-default value of a number option.
833 * Used for 'lines' and 'columns'.
834 */
835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100836set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000838 int opt_idx;
839
840 opt_idx = findoption((char_u *)name);
841 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000842 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Dominique Pelle748b3082022-01-08 12:41:16 +0000845#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200846/*
847 * Set all window-local and buffer-local options to the Vim default.
848 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200849 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200850 */
851 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200852set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200853{
854 win_T *save_curwin = curwin;
855 int i;
856
857 curwin = wp;
858 curbuf = curwin->w_buffer;
859 block_autocmds();
860
Bram Moolenaardac13472019-09-16 21:06:21 +0200861 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200862 {
863 struct vimoption *p = &(options[i]);
864 char_u *varp = get_varp_scope(p, OPT_LOCAL);
865
866 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200867 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200868 && !(options[i].flags & P_NODEFAULT)
869 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200870 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200871 }
872
873 unblock_autocmds();
874 curwin = save_curwin;
875 curbuf = curwin->w_buffer;
876}
Dominique Pelle748b3082022-01-08 12:41:16 +0000877#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200878
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000879#if defined(EXITFREE) || defined(PROTO)
880/*
881 * Free all options.
882 */
883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100884free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000885{
886 int i;
887
Bram Moolenaardac13472019-09-16 21:06:21 +0200888 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000889 {
890 if (options[i].indir == PV_NONE)
891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100892 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100893 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000894 free_string_option(*(char_u **)options[i].var);
895 if (options[i].flags & P_DEF_ALLOCED)
896 free_string_option(options[i].def_val[VI_DEFAULT]);
897 }
898 else if (options[i].var != VAR_WIN
899 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100900 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200901 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000902 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000903 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000904 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000905}
906#endif
907
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909/*
910 * Initialize the options, part two: After getting Rows and Columns and
911 * setting 'term'.
912 */
913 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100914set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000916 int idx;
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100919 * 'scroll' defaults to half the window height. The stored default is zero,
920 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000922 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000923 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000924 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 comp_col();
926
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000927 /*
928 * 'window' is only for backwards compatibility with Vi.
929 * Default is Rows - 1.
930 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000931 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000932 p_window = Rows - 1;
933 set_number_default("window", Rows - 1);
934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100936#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000937 /*
938 * If 'background' wasn't set by the user, try guessing the value,
939 * depending on the terminal name. Only need to check for terminals
940 * with a dark background, that can handle color.
941 */
942 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000943 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
944 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000946 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 // don't mark it as set, when starting the GUI it may be
948 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000949 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000952
953#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100954 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000955#endif
956#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000958#endif
959#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100960 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
965 * Initialize the options, part three: After reading the .vimrc
966 */
967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100968set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100970#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
973 * This is done after other initializations, where 'shell' might have been
974 * set, but only if they have not been set before.
975 */
976 char_u *p;
977 int idx_srr;
978 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100979# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 int idx_sp;
981 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000985 if (idx_srr < 0)
986 do_srr = FALSE;
987 else
988 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000991 if (idx_sp < 0)
992 do_sp = FALSE;
993 else
994 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100995# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200996 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (p != NULL)
998 {
999 /*
1000 * Default for p_sp is "| tee", for p_srr is ">".
1001 * For known shells it is changed here to include stderr.
1002 */
1003 if ( fnamecmp(p, "csh") == 0
1004 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001005# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 || fnamecmp(p, "csh.exe") == 0
1007 || fnamecmp(p, "tcsh.exe") == 0
1008# endif
1009 )
1010 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001011# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if (do_sp)
1013 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001014# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001016# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1020 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (do_srr)
1023 {
1024 p_srr = (char_u *)">&";
1025 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1026 }
1027 }
Mike Williams12795022021-06-28 20:53:58 +02001028# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001029 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1030 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001031 else if ( fnamecmp(p, "powershell") == 0
1032 || fnamecmp(p, "powershell.exe") == 0
1033 )
1034 {
1035# if defined(FEAT_QUICKFIX)
1036 if (do_sp)
1037 {
1038 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1039 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1040 }
1041# endif
1042 if (do_srr)
1043 {
1044 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1045 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1046 }
1047 }
1048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 else
Natanael Copa56318362021-05-06 18:46:35 +02001050 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if ( fnamecmp(p, "sh") == 0
1052 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001053 || fnamecmp(p, "mksh") == 0
1054 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001056 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001058 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001059 || fnamecmp(p, "ash") == 0
1060 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001061 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001062# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 || fnamecmp(p, "cmd") == 0
1064 || fnamecmp(p, "sh.exe") == 0
1065 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001066 || fnamecmp(p, "mksh.exe") == 0
1067 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001069 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 || fnamecmp(p, "bash.exe") == 0
1071 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001072 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001073 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001075 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001077# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (do_sp)
1079 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001080# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001082# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001083 if (fnamecmp(p, "pwsh") == 0)
1084 p_sp = (char_u *)">%s 2>&1";
1085 else
1086 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1089 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (do_srr)
1092 {
1093 p_srr = (char_u *)">%s 2>&1";
1094 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1095 }
1096 }
1097 vim_free(p);
1098 }
1099#endif
1100
Bram Moolenaar4f974752019-02-17 17:44:42 +01001101#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001103 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1104 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001106 * set, but only if they have not been set before.
1107 * Default values depend on shell (cmd.exe is default shell):
1108 *
1109 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001110 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001111 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001112 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001113 * "sh" like shells - "-c" "\""
1114 *
1115 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
Mike Williams12795022021-06-28 20:53:58 +02001117 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1118 {
1119 int idx_opt;
1120
1121 idx_opt = findoption((char_u *)"shcf");
1122 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1123 {
1124 p_shcf = (char_u*)"-Command";
1125 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1126 }
1127
1128 idx_opt = findoption((char_u *)"sxq");
1129 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1130 {
1131 p_sxq = (char_u*)"\"";
1132 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1133 }
1134 }
1135 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {
1137 int idx3;
1138
1139 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001140 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 p_shcf = (char_u *)"-c";
1143 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1144 }
1145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001146 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001148 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
1150 p_sxq = (char_u *)"\"";
1151 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001154 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1155 {
1156 int idx3;
1157
1158 /*
1159 * cmd.exe on Windows will strip the first and last double quote given
1160 * on the command line, e.g. most of the time things like:
1161 * cmd /c "my path/to/echo" "my args to echo"
1162 * become:
1163 * my path/to/echo" "my args to echo
1164 * when executed.
1165 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001166 * To avoid this, set shellxquote to surround the command in
1167 * parenthesis. This appears to make most commands work, without
1168 * breaking commands that worked previously, such as
1169 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001170 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001171 idx3 = findoption((char_u *)"sxq");
1172 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1173 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001174 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001175 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1176 }
1177
Bram Moolenaara64ba222012-02-12 23:23:31 +01001178 idx3 = findoption((char_u *)"shcf");
1179 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1180 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001181 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001182 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1183 }
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001187 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001188 {
1189 int idx_ffs = findoption((char_u *)"ffs");
1190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001191 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001192 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1193 set_fileformat(default_fileformat(), OPT_LOCAL);
1194 }
1195
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197}
1198
1199#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1200/*
1201 * When 'helplang' is still at its default value, set it to "lang".
1202 * Only the first two characters of "lang" are used.
1203 */
1204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001205set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 int idx;
1208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001209 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 return;
1211 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001212 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1213 return;
1214
1215 if (options[idx].flags & P_ALLOCED)
1216 free_string_option(p_hlg);
1217 p_hlg = vim_strsave(lang);
1218 if (p_hlg == NULL)
1219 p_hlg = empty_option;
1220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001222 // zh_CN becomes "cn", zh_TW becomes "tw"
1223 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001224 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001225 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1226 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001227 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001228 // any C like setting, such as C.UTF-8, becomes "en"
1229 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1230 {
1231 p_hlg[0] = 'e';
1232 p_hlg[1] = 'n';
1233 }
1234 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001236 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237}
1238#endif
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240/*
1241 * 'title' and 'icon' only default to true if they have not been set or reset
1242 * in .vimrc and we can read the old value.
1243 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1244 * they can be reset. This reduces startup time when using X on a remote
1245 * machine.
1246 */
1247 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001248set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 int idx1;
1251 long val;
1252
1253 /*
1254 * If GUI is (going to be) used, we can always set the window title and
1255 * icon name. Saves a bit of time, because the X11 display server does
1256 * not need to be contacted.
1257 */
1258 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001259 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261#ifdef FEAT_GUI
1262 if (gui.starting || gui.in_use)
1263 val = TRUE;
1264 else
1265#endif
1266 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001267 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 p_title = val;
1269 }
1270 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001271 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001273 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001275
1276#ifdef FEAT_GUI
1277 if (gui.starting || gui.in_use)
1278 val = TRUE;
1279 else
1280#endif
1281 val = mch_can_restore_icon();
1282 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1283 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001286 void
1287ex_set(exarg_T *eap)
1288{
1289 int flags = 0;
1290
1291 if (eap->cmdidx == CMD_setlocal)
1292 flags = OPT_LOCAL;
1293 else if (eap->cmdidx == CMD_setglobal)
1294 flags = OPT_GLOBAL;
1295#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001296 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001297 ex_options(eap);
1298 else
1299#endif
1300 {
1301 if (eap->forceit)
1302 flags |= OPT_ONECOLUMN;
1303 (void)do_set(eap->arg, flags);
1304 }
1305}
1306
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001307/*
1308 * :set operator types
1309 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001310typedef enum {
1311 OP_NONE = 0,
1312 OP_ADDING, // "opt+=arg"
1313 OP_PREPENDING, // "opt^=arg"
1314 OP_REMOVING, // "opt-=arg"
1315} set_op_T;
1316
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001317typedef enum {
1318 PREFIX_NO = 0, // "no" prefix
1319 PREFIX_NONE, // no prefix
1320 PREFIX_INV, // "inv" prefix
1321} set_prefix_T;
1322
1323/*
1324 * Return the prefix type for the option name in *argp.
1325 */
1326 static set_prefix_T
1327get_option_prefix(char_u **argp)
1328{
1329 int prefix = PREFIX_NONE;
1330 char_u *arg = *argp;
1331
1332 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1333 {
1334 prefix = PREFIX_NO;
1335 arg += 2;
1336 }
1337 else if (STRNCMP(arg, "inv", 3) == 0)
1338 {
1339 prefix = PREFIX_INV;
1340 arg += 3;
1341 }
1342
1343 *argp = arg;
1344 return prefix;
1345}
1346
1347/*
1348 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1349 * and the option name length in "*lenp". For a <t_xx> option, return the key
1350 * number in "*keyp".
1351 *
1352 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1353 * otherwise returns OK.
1354 */
1355 static int
1356parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1357{
1358 int key = 0;
1359 int len;
1360 int opt_idx;
1361
1362 if (*arg == '<')
1363 {
1364 opt_idx = -1;
1365 // look out for <t_>;>
1366 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1367 len = 5;
1368 else
1369 {
1370 len = 1;
1371 while (arg[len] != NUL && arg[len] != '>')
1372 ++len;
1373 }
1374 if (arg[len] != '>')
1375 return FAIL;
1376
1377 arg[len] = NUL; // put NUL after name
1378 if (arg[1] == 't' && arg[2] == '_') // could be term code
1379 opt_idx = findoption(arg + 1);
1380 arg[len++] = '>'; // restore '>'
1381 if (opt_idx == -1)
1382 key = find_key_option(arg + 1, TRUE);
1383 }
1384 else
1385 {
1386 int nextchar; // next non-white char after option name
1387
1388 len = 0;
1389 /*
1390 * The two characters after "t_" may not be alphanumeric.
1391 */
1392 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1393 len = 4;
1394 else
1395 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1396 ++len;
1397 nextchar = arg[len];
1398 arg[len] = NUL; // put NUL after name
1399 opt_idx = findoption(arg);
1400 arg[len] = nextchar; // restore nextchar
1401 if (opt_idx == -1)
1402 key = find_key_option(arg, FALSE);
1403 }
1404
1405 *keyp = key;
1406 *lenp = len;
1407 *opt_idxp = opt_idx;
1408
1409 return OK;
1410}
1411
1412/*
1413 * Get the option operator (+=, ^=, -=).
1414 */
1415 static set_op_T
1416get_opt_op(char_u *arg)
1417{
1418 set_op_T op = OP_NONE;
1419
1420 if (*arg != NUL && *(arg + 1) == '=')
1421 {
1422 if (*arg == '+')
1423 op = OP_ADDING; // "+="
1424 else if (*arg == '^')
1425 op = OP_PREPENDING; // "^="
1426 else if (*arg == '-')
1427 op = OP_REMOVING; // "-="
1428 }
1429
1430 return op;
1431}
1432
1433/*
1434 * Validate whether the value of the option in "opt_idx" can be changed.
1435 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1436 * if it can be changed.
1437 */
1438 static int
1439validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1440{
1441 // Skip all options that are not window-local (used when showing
1442 // an already loaded buffer in a window).
1443 if ((opt_flags & OPT_WINONLY)
1444 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1445 return FAIL;
1446
1447 // Skip all options that are window-local (used for :vimgrep).
1448 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1449 && options[opt_idx].var == VAR_WIN)
1450 return FAIL;
1451
1452 // Disallow changing some options from modelines.
1453 if (opt_flags & OPT_MODELINE)
1454 {
1455 if (flags & (P_SECURE | P_NO_ML))
1456 {
1457 *errmsg = e_not_allowed_in_modeline;
1458 return FAIL;
1459 }
1460 if ((flags & P_MLE) && !p_mle)
1461 {
1462 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1463 return FAIL;
1464 }
1465#ifdef FEAT_DIFF
1466 // In diff mode some options are overruled. This avoids that
1467 // 'foldmethod' becomes "marker" instead of "diff" and that
1468 // "wrap" gets set.
1469 if (curwin->w_p_diff
1470 && opt_idx >= 0 // shut up coverity warning
1471 && (
1472# ifdef FEAT_FOLDING
1473 options[opt_idx].indir == PV_FDM ||
1474# endif
1475 options[opt_idx].indir == PV_WRAP))
1476 return FAIL;
1477#endif
1478 }
1479
1480#ifdef HAVE_SANDBOX
1481 // Disallow changing some options in the sandbox
1482 if (sandbox != 0 && (flags & P_SECURE))
1483 {
1484 *errmsg = e_not_allowed_in_sandbox;
1485 return FAIL;
1486 }
1487#endif
1488
1489 return OK;
1490}
1491
Bram Moolenaar47403942022-09-21 21:12:53 +01001492/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001493 * Get the Vim/Vi default value for a string option.
1494 */
1495 static char_u *
1496stropt_get_default_val(
1497 int opt_idx,
1498 char_u *varp,
1499 int flags,
1500 int cp_val)
1501{
1502 char_u *newval;
1503
1504 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1505 ? VI_DEFAULT : VIM_DEFAULT];
1506 if ((char_u **)varp == &p_bg)
1507 {
1508 // guess the value of 'background'
1509#ifdef FEAT_GUI
1510 if (gui.in_use)
1511 newval = gui_bg_default();
1512 else
1513#endif
1514 newval = term_bg_default();
1515 }
1516 else if ((char_u **)varp == &p_fencs && enc_utf8)
1517 newval = fencs_utf8_default;
1518
1519 // expand environment variables and ~ since the default value was
1520 // already expanded, only required when an environment variable was set
1521 // later
1522 if (newval == NULL)
1523 newval = empty_option;
1524 else
1525 {
1526 char_u *s = option_expand(opt_idx, newval);
1527 if (s == NULL)
1528 s = newval;
1529 newval = vim_strsave(s);
1530 }
1531
1532 return newval;
1533}
1534
1535/*
1536 * Convert the 'backspace' option number value to a string: for adding,
1537 * prepending and removing string.
1538 */
1539 static void
1540opt_backspace_nr2str(
1541 char_u *varp,
1542 char_u **origval_p,
1543 char_u **origval_l_p,
1544 char_u **origval_g_p,
1545 char_u **oldval_p)
1546{
1547 int i = getdigits((char_u **)varp);
1548
1549 switch (i)
1550 {
1551 case 0:
1552 *(char_u **)varp = empty_option;
1553 break;
1554 case 1:
1555 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1556 break;
1557 case 2:
1558 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1559 break;
1560 case 3:
1561 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1562 break;
1563 }
1564 vim_free(*oldval_p);
1565 if (*origval_p == *oldval_p)
1566 *origval_p = *(char_u **)varp;
1567 if (*origval_l_p == *oldval_p)
1568 *origval_l_p = *(char_u **)varp;
1569 if (*origval_g_p == *oldval_p)
1570 *origval_g_p = *(char_u **)varp;
1571 *oldval_p = *(char_u **)varp;
1572}
1573
1574/*
1575 * Convert the 'whichwrap' option number value to a string, for backwards
1576 * compatibility with Vim 3.0.
1577 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1578 */
1579 static char_u *
1580opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1581{
1582 *whichwrap = NUL;
1583 int i = getdigits(argp);
1584 if (i & 1)
1585 STRCAT(whichwrap, "b,");
1586 if (i & 2)
1587 STRCAT(whichwrap, "s,");
1588 if (i & 4)
1589 STRCAT(whichwrap, "h,l,");
1590 if (i & 8)
1591 STRCAT(whichwrap, "<,>,");
1592 if (i & 16)
1593 STRCAT(whichwrap, "[,],");
1594 if (*whichwrap != NUL) // remove trailing ,
1595 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1596
1597 return whichwrap;
1598}
1599
1600/*
1601 * Copy the new string value into allocated memory for the option.
1602 * Can't use set_string_option_direct(), because we need to remove the
1603 * backslashes.
1604 */
1605 static char_u *
1606stropt_copy_value(
1607 char_u *origval,
1608 char_u **argp,
1609 set_op_T op,
1610 int flags UNUSED)
1611{
1612 char_u *arg = *argp;
1613 unsigned newlen;
1614 char_u *newval;
1615 char_u *s = NULL;
1616
1617 // get a bit too much
1618 newlen = (unsigned)STRLEN(arg) + 1;
1619 if (op != OP_NONE)
1620 newlen += (unsigned)STRLEN(origval) + 1;
1621 newval = alloc(newlen);
1622 if (newval == NULL) // out of mem, don't change
1623 return NULL;
1624 s = newval;
1625
1626 // Copy the string, skip over escaped chars.
1627 // For MS-DOS and WIN32 backslashes before normal file name characters
1628 // are not removed, and keep backslash at start, for "\\machine\path",
1629 // but do remove it for "\\\\machine\\path".
1630 // The reverse is found in ExpandOldSetting().
1631 while (*arg != NUL && !VIM_ISWHITE(*arg))
1632 {
1633 int i;
1634
1635 if (*arg == '\\' && arg[1] != NUL
1636#ifdef BACKSLASH_IN_FILENAME
1637 && !((flags & P_EXPAND)
1638 && vim_isfilec(arg[1])
1639 && !VIM_ISWHITE(arg[1])
1640 && (arg[1] != '\\'
1641 || (s == newval && arg[2] != '\\')))
1642#endif
1643 )
1644 ++arg; // remove backslash
1645 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1646 {
1647 // copy multibyte char
1648 mch_memmove(s, arg, (size_t)i);
1649 arg += i;
1650 s += i;
1651 }
1652 else
1653 *s++ = *arg++;
1654 }
1655 *s = NUL;
1656
1657 *argp = arg;
1658 return newval;
1659}
1660
1661/*
1662 * Expand environment variables and ~ in string option value 'newval'.
1663 */
1664 static char_u *
1665stropt_expand_envvar(
1666 int opt_idx,
1667 char_u *origval,
1668 char_u *newval,
1669 set_op_T op)
1670{
1671 char_u *s = option_expand(opt_idx, newval);
1672 if (s == NULL)
1673 return newval;
1674
1675 vim_free(newval);
1676 unsigned newlen = (unsigned)STRLEN(s) + 1;
1677 if (op != OP_NONE)
1678 newlen += (unsigned)STRLEN(origval) + 1;
1679
1680 newval = alloc(newlen);
1681 if (newval == NULL)
1682 return NULL;
1683
1684 STRCPY(newval, s);
1685
1686 return newval;
1687}
1688
1689/*
1690 * Concatenate the original and new values of a string option, adding a "," if
1691 * needed.
1692 */
1693 static void
1694stropt_concat_with_comma(
1695 char_u *origval,
1696 char_u *newval,
1697 set_op_T op,
1698 int flags)
1699{
1700 int len = 0;
1701
1702 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1703 if (op == OP_ADDING)
1704 {
1705 len = (int)STRLEN(origval);
1706 // strip a trailing comma, would get 2
1707 if (comma && len > 1
1708 && (flags & P_ONECOMMA) == P_ONECOMMA
1709 && origval[len - 1] == ','
1710 && origval[len - 2] != '\\')
1711 len--;
1712 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1713 mch_memmove(newval, origval, (size_t)len);
1714 }
1715 else
1716 {
1717 len = (int)STRLEN(newval);
1718 STRMOVE(newval + len + comma, origval);
1719 }
1720 if (comma)
1721 newval[len] = ',';
1722}
1723
1724/*
1725 * Remove a value from a string option. Copy string option value in "origval"
1726 * to "newval" and then remove the string "strval" of length "len".
1727 */
1728 static void
1729stropt_remove_val(
1730 char_u *origval,
1731 char_u *newval,
1732 int flags,
1733 char_u *strval,
1734 int len)
1735{
1736 // Remove newval[] from origval[]. (Note: "len" has been set above
1737 // and is used here).
1738 STRCPY(newval, origval);
1739 if (*strval)
1740 {
1741 // may need to remove a comma
1742 if (flags & P_COMMA)
1743 {
1744 if (strval == origval)
1745 {
1746 // include comma after string
1747 if (strval[len] == ',')
1748 ++len;
1749 }
1750 else
1751 {
1752 // include comma before string
1753 --strval;
1754 ++len;
1755 }
1756 }
1757 STRMOVE(newval + (strval - origval), strval + len);
1758 }
1759}
1760
1761/*
1762 * Remove flags that appear twice in the string option value 'newval'.
1763 */
1764 static void
1765stropt_remove_dupflags(char_u *newval, int flags)
1766{
1767 char_u *s = newval;
1768
1769 // Remove flags that appear twice.
1770 while (*s)
1771 {
1772 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1773 if (flags & P_ONECOMMA)
1774 {
1775 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1776 {
1777 // Remove the duplicated value and the next comma.
1778 STRMOVE(s, s + 2);
1779 continue;
1780 }
1781 }
1782 else
1783 {
1784 if ((!(flags & P_COMMA) || *s != ',')
1785 && vim_strchr(s + 1, *s) != NULL)
1786 {
1787 STRMOVE(s, s + 1);
1788 continue;
1789 }
1790 }
1791 ++s;
1792 }
1793}
1794
1795/*
1796 * Get the string value specified for a ":set" command. The following set
1797 * options are supported:
1798 * set {opt}&
1799 * set {opt}<
1800 * set {opt}={val}
1801 * set {opt}:{val}
1802 */
1803 static char_u *
1804stropt_get_newval(
1805 int nextchar,
1806 int opt_idx,
1807 char_u **argp,
1808 char_u *varp,
1809 char_u **origval_arg,
1810 char_u **origval_l_arg,
1811 char_u **origval_g_arg,
1812 char_u **oldval_arg,
1813 set_op_T *op_arg,
1814 int flags,
1815 int cp_val)
1816{
1817 char_u *arg = *argp;
1818 char_u *origval = *origval_arg;
1819 char_u *origval_l = *origval_l_arg;
1820 char_u *origval_g = *origval_g_arg;
1821 char_u *oldval = *oldval_arg;
1822 set_op_T op = *op_arg;
1823 char_u *save_arg = NULL;
1824 char_u *newval;
1825 char_u *s = NULL;
1826 char_u whichwrap[80];
1827
1828 if (nextchar == '&') // set to default val
1829 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1830 else if (nextchar == '<') // set to global val
1831 newval = vim_strsave(*(char_u **)get_varp_scope(
1832 &(options[opt_idx]), OPT_GLOBAL));
1833 else
1834 {
1835 ++arg; // jump to after the '=' or ':'
1836
1837 // Set 'keywordprg' to ":help" if an empty
1838 // value was passed to :set by the user.
1839 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1840 {
1841 save_arg = arg;
1842 arg = (char_u *)":help";
1843 }
1844 // Convert 'backspace' number to string
1845 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1846 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1847 &oldval);
1848 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1849 {
1850 // Convert 'whichwrap' number to string, for backwards
1851 // compatibility with Vim 3.0.
1852 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1853 save_arg = arg;
1854 arg = t;
1855 }
1856 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1857 // version 3.0
1858 else if (*arg == '>' && (varp == (char_u *)&p_dir
1859 || varp == (char_u *)&p_bdir))
1860 ++arg;
1861
1862 // Copy the new string into allocated memory.
1863 newval = stropt_copy_value(origval, &arg, op, flags);
1864 if (newval == NULL)
1865 goto done;
1866
1867 // Expand environment variables and ~.
1868 // Don't do it when adding without inserting a comma.
1869 if (op == OP_NONE || (flags & P_COMMA))
1870 {
1871 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1872 if (newval == NULL)
1873 goto done;
1874 }
1875
1876 // locate newval[] in origval[] when removing it and when adding to
1877 // avoid duplicates
1878 int len = 0;
1879 if (op == OP_REMOVING || (flags & P_NODUP))
1880 {
1881 len = (int)STRLEN(newval);
1882 s = find_dup_item(origval, newval, flags);
1883
1884 // do not add if already there
1885 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1886 {
1887 op = OP_NONE;
1888 STRCPY(newval, origval);
1889 }
1890
1891 // if no duplicate, move pointer to end of original value
1892 if (s == NULL)
1893 s = origval + (int)STRLEN(origval);
1894 }
1895
1896 // concatenate the two strings; add a ',' if needed
1897 if (op == OP_ADDING || op == OP_PREPENDING)
1898 stropt_concat_with_comma(origval, newval, op, flags);
1899 else if (op == OP_REMOVING)
1900 // Remove newval[] from origval[]. (Note: "len" has been set above
1901 // and is used here).
1902 stropt_remove_val(origval, newval, flags, s, len);
1903
1904 if (flags & P_FLAGLIST)
1905 // Remove flags that appear twice.
1906 stropt_remove_dupflags(newval, flags);
1907 }
1908
1909done:
1910 if (save_arg != NULL)
1911 arg = save_arg; // arg was temporarily changed, restore it
1912 *argp = arg;
1913 *origval_arg = origval;
1914 *origval_l_arg = origval_l;
1915 *origval_g_arg = origval_g;
1916 *oldval_arg = oldval;
1917 *op_arg = op;
1918
1919 return newval;
1920}
1921
1922/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001923 * Part of do_set() for string options.
1924 * Returns FAIL on failure, do not process further options.
1925 */
1926 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001927do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001928 int opt_idx,
1929 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001930 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001931 int nextchar,
1932 set_op_T op_arg,
1933 int flags,
1934 int cp_val,
1935 char_u *varp_arg,
1936 char *errbuf,
1937 int *value_checked,
1938 char **errmsg)
1939{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001940 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001941 set_op_T op = op_arg;
1942 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001943 char_u *oldval = NULL; // previous value if *varp
1944 char_u *newval;
1945 char_u *origval = NULL;
1946 char_u *origval_l = NULL;
1947 char_u *origval_g = NULL;
1948#if defined(FEAT_EVAL)
1949 char_u *saved_origval = NULL;
1950 char_u *saved_origval_l = NULL;
1951 char_u *saved_origval_g = NULL;
1952 char_u *saved_newval = NULL;
1953#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001954
1955 // When using ":set opt=val" for a global option
1956 // with a local value the local value will be
1957 // reset, use the global value here.
1958 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1959 && ((int)options[opt_idx].indir & PV_BOTH))
1960 varp = options[opt_idx].var;
1961
1962 // The old value is kept until we are sure that the new value is valid.
1963 oldval = *(char_u **)varp;
1964
1965 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1966 {
1967 origval_l = *(char_u **)get_varp_scope(
1968 &(options[opt_idx]), OPT_LOCAL);
1969 origval_g = *(char_u **)get_varp_scope(
1970 &(options[opt_idx]), OPT_GLOBAL);
1971
1972 // A global-local string option might have an empty option as value to
1973 // indicate that the global value should be used.
1974 if (((int)options[opt_idx].indir & PV_BOTH)
1975 && origval_l == empty_option)
1976 origval_l = origval_g;
1977 }
1978
1979 // When setting the local value of a global option, the old value may be
1980 // the global value.
1981 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1982 origval = *(char_u **)get_varp(&options[opt_idx]);
1983 else
1984 origval = oldval;
1985
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001986 // Get the new value for the option
1987 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1988 &origval_l, &origval_g, &oldval, &op, flags,
1989 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001990
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001991 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001992 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001993 if (newval == NULL)
1994 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001995
1996#if defined(FEAT_EVAL)
1997 if (!starting
1998# ifdef FEAT_CRYPT
1999 && options[opt_idx].indir != PV_KEY
2000# endif
2001 && origval != NULL && newval != NULL)
2002 {
2003 // origval may be freed by did_set_string_option(), make a copy.
2004 saved_origval = vim_strsave(origval);
2005 // newval (and varp) may become invalid if the buffer is closed by
2006 // autocommands.
2007 saved_newval = vim_strsave(newval);
2008 if (origval_l != NULL)
2009 saved_origval_l = vim_strsave(origval_l);
2010 if (origval_g != NULL)
2011 saved_origval_g = vim_strsave(origval_g);
2012 }
2013#endif
2014
2015 {
2016 long_u *p = insecure_flag(opt_idx, opt_flags);
2017 int secure_saved = secure;
2018
2019 // When an option is set in the sandbox, from a modeline or in secure
2020 // mode, then deal with side effects in secure mode. Also when the
2021 // value was set with the P_INSECURE flag and is not completely
2022 // replaced.
2023 if ((opt_flags & OPT_MODELINE)
2024#ifdef HAVE_SANDBOX
2025 || sandbox != 0
2026#endif
2027 || (op != OP_NONE && (*p & P_INSECURE)))
2028 secure = 1;
2029
2030 // Handle side effects, and set the global value for ":set" on local
2031 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2032 // be triggered that can cause havoc.
2033 *errmsg = did_set_string_option(
2034 opt_idx, (char_u **)varp, oldval, errbuf,
2035 opt_flags, value_checked);
2036
2037 secure = secure_saved;
2038 }
2039
2040#if defined(FEAT_EVAL)
2041 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002042 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002043 saved_origval_l, saved_origval_g, saved_newval);
2044 vim_free(saved_origval);
2045 vim_free(saved_origval_l);
2046 vim_free(saved_origval_g);
2047 vim_free(saved_newval);
2048#endif
2049
Bram Moolenaar6f981142022-09-22 12:48:58 +01002050 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002051 return *errmsg == NULL ? OK : FAIL;
2052}
2053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002055 * Set a boolean option.
2056 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002057 */
2058 static char *
2059do_set_option_bool(
2060 int opt_idx,
2061 int opt_flags,
2062 set_prefix_T prefix,
2063 long_u flags,
2064 char_u *varp,
2065 int nextchar,
2066 int afterchar,
2067 int cp_val)
2068
2069{
2070 varnumber_T value;
2071
2072 if (nextchar == '=' || nextchar == ':')
2073 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002074 if (opt_idx < 0 || varp == NULL)
2075 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002076
2077 /*
2078 * ":set opt!": invert
2079 * ":set opt&": reset to default value
2080 * ":set opt<": reset to global value
2081 */
2082 if (nextchar == '!')
2083 value = *(int *)(varp) ^ 1;
2084 else if (nextchar == '&')
2085 value = (int)(long)(long_i)options[opt_idx].def_val[
2086 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2087 else if (nextchar == '<')
2088 {
2089 // For 'autoread' -1 means to use global value.
2090 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2091 value = -1;
2092 else
2093 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2094 }
2095 else
2096 {
2097 /*
2098 * ":set invopt": invert
2099 * ":set opt" or ":set noopt": set or reset
2100 */
2101 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2102 return e_trailing_characters;
2103 if (prefix == PREFIX_INV)
2104 value = *(int *)(varp) ^ 1;
2105 else
2106 value = prefix == PREFIX_NO ? 0 : 1;
2107 }
2108
2109 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2110}
2111
2112/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002113 * Set a numeric option.
2114 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002115 */
2116 static char *
2117do_set_option_numeric(
2118 int opt_idx,
2119 int opt_flags,
2120 char_u **argp,
2121 int nextchar,
2122 set_op_T op,
2123 long_u flags,
2124 int cp_val,
2125 char_u *varp,
2126 char *errbuf,
2127 size_t errbuflen)
2128{
2129 char_u *arg = *argp;
2130 varnumber_T value;
2131 int i;
2132 char *errmsg = NULL;
2133
Bram Moolenaar546933f2023-02-06 16:40:49 +00002134 if (opt_idx < 0 || varp == NULL)
2135 return NULL; // "cannot happen"
2136 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002137 /*
2138 * Different ways to set a number option:
2139 * & set to default value
2140 * < set to global value
2141 * <xx> accept special key codes for 'wildchar'
2142 * c accept any non-digit for 'wildchar'
2143 * [-]0-9 set number
2144 * other error
2145 */
2146 ++arg;
2147 if (nextchar == '&')
2148 value = (long)(long_i)options[opt_idx].def_val[
2149 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2150 else if (nextchar == '<')
2151 {
2152 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2153 // use the global value.
2154 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2155 value = NO_LOCAL_UNDOLEVEL;
2156 else
2157 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2158 }
2159 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2160 && (*arg == '<'
2161 || *arg == '^'
2162 || (*arg != NUL
2163 && (!arg[1] || VIM_ISWHITE(arg[1]))
2164 && !VIM_ISDIGIT(*arg))))
2165 {
2166 value = string_to_key(arg, FALSE);
2167 if (value == 0 && (long *)varp != &p_wcm)
2168 {
2169 errmsg = e_invalid_argument;
2170 goto skip;
2171 }
2172 }
2173 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2174 {
2175 // Allow negative (for 'undolevels'), octal and hex numbers.
2176 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
2177 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2178 {
2179 errmsg = e_number_required_after_equal;
2180 goto skip;
2181 }
2182 }
2183 else
2184 {
2185 errmsg = e_number_required_after_equal;
2186 goto skip;
2187 }
2188
2189 if (op == OP_ADDING)
2190 value = *(long *)varp + value;
2191 else if (op == OP_PREPENDING)
2192 value = *(long *)varp * value;
2193 else if (op == OP_REMOVING)
2194 value = *(long *)varp - value;
2195
2196 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2197 opt_flags);
2198
2199skip:
2200 *argp = arg;
2201 return errmsg;
2202}
2203
2204/*
2205 * Set a key code (t_xx) option
2206 */
2207 static char *
2208do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2209{
2210 char_u *arg = *argp;
2211 char_u *p;
2212
2213 if (nextchar == '&')
2214 {
2215 if (add_termcap_entry(key_name, TRUE) == FAIL)
2216 return e_not_found_in_termcap;
2217 }
2218 else
2219 {
2220 ++arg; // jump to after the '=' or ':'
2221 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2222 if (*p == '\\' && p[1] != NUL)
2223 ++p;
2224 nextchar = *p;
2225 *p = NUL;
2226 add_termcode(key_name, arg, FALSE);
2227 *p = nextchar;
2228 }
2229 if (full_screen)
2230 ttest(FALSE);
2231 redraw_all_later(UPD_CLEAR);
2232
2233 *argp = arg;
2234 return NULL;
2235}
2236
2237/*
2238 * Set an option to a new value.
2239 */
2240 static char *
2241do_set_option_value(
2242 int opt_idx,
2243 int opt_flags,
2244 char_u **argp,
2245 set_prefix_T prefix,
2246 set_op_T op,
2247 long_u flags,
2248 char_u *varp,
2249 char_u *key_name,
2250 int nextchar,
2251 int afterchar,
2252 int cp_val,
2253 int *stopopteval,
2254 char *errbuf,
2255 size_t errbuflen)
2256{
2257 int value_checked = FALSE;
2258 char *errmsg = NULL;
2259 char_u *arg = *argp;
2260
2261 if (flags & P_BOOL)
2262 {
2263 // boolean option
2264 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2265 nextchar, afterchar, cp_val);
2266 if (errmsg != NULL)
2267 goto skip;
2268 }
2269 else
2270 {
2271 // numeric or string option
2272 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2273 || prefix != PREFIX_NONE)
2274 {
2275 errmsg = e_invalid_argument;
2276 goto skip;
2277 }
2278
2279 if (flags & P_NUM)
2280 {
2281 // numeric option
2282 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2283 op, flags, cp_val, varp,
2284 errbuf, errbuflen);
2285 if (errmsg != NULL)
2286 goto skip;
2287 }
2288 else if (opt_idx >= 0)
2289 {
2290 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002291 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2292 flags, cp_val, varp, errbuf,
2293 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002294 {
2295 if (errmsg != NULL)
2296 goto skip;
2297 *stopopteval = TRUE;
2298 goto skip;
2299 }
2300 }
2301 else
2302 {
2303 // key code option
2304 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2305 if (errmsg != NULL)
2306 goto skip;
2307 }
2308 }
2309
2310 if (opt_idx >= 0)
2311 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2312
2313skip:
2314 *argp = arg;
2315 return errmsg;
2316}
2317
2318/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002319 * Set an option to a new value.
2320 * Return NULL if OK, return an untranslated error message when something is
2321 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2322 */
2323 static char *
2324do_set_option(
2325 int opt_flags,
2326 char_u **argp,
2327 char_u *arg_start,
2328 char_u **startarg,
2329 int *did_show,
2330 int *stopopteval,
2331 char *errbuf,
2332 size_t errbuflen)
2333{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002334 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002335 char_u *arg;
2336 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2337 set_op_T op;
2338 long_u flags; // flags for current option
2339 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002340 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002341 int nextchar; // next non-white char after option name
2342 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002343 char *errmsg = NULL;
2344 int key;
2345 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002346
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002347 prefix = get_option_prefix(argp);
2348 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002349
2350 // find end of name
2351 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002352 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2353 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002354
2355 // remember character after option name
2356 afterchar = arg[len];
2357
2358 if (in_vim9script())
2359 {
2360 char_u *p = skipwhite(arg + len);
2361
2362 // disallow white space before =val, +=val, -=val, ^=val
2363 if (p > arg + len && (p[0] == '='
2364 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2365 && p[1] == '=')))
2366 {
2367 errmsg = e_no_white_space_allowed_between_option_and;
2368 arg = p;
2369 *startarg = p;
2370 goto skip;
2371 }
2372 }
2373 else
2374 // skip white space, allow ":set ai ?", ":set hlsearch !"
2375 while (VIM_ISWHITE(arg[len]))
2376 ++len;
2377
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002378 op = get_opt_op(arg + len);
2379 if (op != OP_NONE)
2380 len++;
2381
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002382 nextchar = arg[len];
2383
2384 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2385 {
2386 if (in_vim9script() && arg > arg_start
2387 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2388 errmsg = e_no_white_space_allowed_between_option_and;
2389 else
2390 errmsg = e_unknown_option;
2391 goto skip;
2392 }
2393
2394 if (opt_idx >= 0)
2395 {
2396 if (options[opt_idx].var == NULL) // hidden option: skip
2397 {
2398 // Only give an error message when requesting the value of
2399 // a hidden option, ignore setting it.
2400 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2401 && (!(options[opt_idx].flags & P_BOOL)
2402 || nextchar == '?'))
2403 errmsg = e_option_not_supported;
2404 goto skip;
2405 }
2406
2407 flags = options[opt_idx].flags;
2408 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2409 }
2410 else
2411 {
2412 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002413 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002414 if (key < 0)
2415 {
2416 key_name[0] = KEY2TERMCAP0(key);
2417 key_name[1] = KEY2TERMCAP1(key);
2418 }
2419 else
2420 {
2421 key_name[0] = KS_KEY;
2422 key_name[1] = (key & 0xff);
2423 }
2424 }
2425
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002426 // Make sure the option value can be changed.
2427 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002428 goto skip;
2429
Bram Moolenaar40b48722023-02-05 17:04:50 +00002430 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002431 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2432 {
2433 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002434 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2435 {
2436 if (arg[3] == 'm') // "opt&vim": set to Vim default
2437 {
2438 cp_val = FALSE;
2439 arg += 3;
2440 }
2441 else // "opt&vi": set to Vi default
2442 {
2443 cp_val = TRUE;
2444 arg += 2;
2445 }
2446 }
2447 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2448 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2449 {
2450 errmsg = e_trailing_characters;
2451 goto skip;
2452 }
2453 }
2454
2455 /*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002456 * Allow '=' and ':' for historical reasons (MSDOS command.com).
2457 * Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002458 */
2459 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002460 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002461 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2462 && !(flags & P_BOOL)))
2463 {
2464 /*
2465 * print value
2466 */
2467 if (*did_show)
2468 msg_putchar('\n'); // cursor below last one
2469 else
2470 {
2471 gotocmdline(TRUE); // cursor at status line
2472 *did_show = TRUE; // remember that we did a line
2473 }
2474 if (opt_idx >= 0)
2475 {
2476 showoneopt(&options[opt_idx], opt_flags);
2477#ifdef FEAT_EVAL
2478 if (p_verbose > 0)
2479 {
2480 // Mention where the option was last set.
2481 if (varp == options[opt_idx].var)
2482 last_set_msg(options[opt_idx].script_ctx);
2483 else if ((int)options[opt_idx].indir & PV_WIN)
2484 last_set_msg(curwin->w_p_script_ctx[
2485 (int)options[opt_idx].indir & PV_MASK]);
2486 else if ((int)options[opt_idx].indir & PV_BUF)
2487 last_set_msg(curbuf->b_p_script_ctx[
2488 (int)options[opt_idx].indir & PV_MASK]);
2489 }
2490#endif
2491 }
2492 else
2493 {
2494 char_u *p;
2495
2496 p = find_termcode(key_name);
2497 if (p == NULL)
2498 {
2499 errmsg = e_key_code_not_set;
2500 goto skip;
2501 }
2502 else
2503 (void)show_one_termcode(key_name, p, TRUE);
2504 }
2505 if (nextchar != '?'
2506 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2507 errmsg = e_trailing_characters;
2508 }
2509 else
2510 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002511 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2512 flags, varp, key_name, nextchar,
2513 afterchar, cp_val, stopopteval, errbuf,
2514 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002515 }
2516
2517skip:
2518 *argp = arg;
2519 return errmsg;
2520}
2521
2522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 * Parse 'arg' for option settings.
2524 *
2525 * 'arg' may be IObuff, but only when no errors can be present and option
2526 * does not need to be expanded with option_expand().
2527 * "opt_flags":
2528 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002529 * OPT_GLOBAL for ":setglobal"
2530 * OPT_LOCAL for ":setlocal" and a modeline
2531 * OPT_MODELINE for a modeline
2532 * OPT_WINONLY to only set window-local options
2533 * OPT_NOWIN to skip setting window-local options
2534 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002536 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 */
2538 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002539do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002540 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002541 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002543 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002545 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547 if (*arg == NUL)
2548 {
2549 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002550 did_show = TRUE;
2551 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
2553
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002554 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002556 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002557 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 /*
2560 * ":set all" show all options.
2561 * ":set all&" set all options to their default value.
2562 */
2563 arg += 3;
2564 if (*arg == '&')
2565 {
2566 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002567 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002569 didset_options();
2570 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002571 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 did_show = TRUE;
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002579 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002582 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002583 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 arg += 7;
2585 }
2586 else
2587 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002588 int stopopteval = FALSE;
2589 char *errmsg = NULL;
2590 char errbuf[80];
2591 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002593 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2594 &did_show, &stopopteval, errbuf,
2595 sizeof(errbuf));
2596 if (stopopteval)
2597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 /*
2600 * Advance to next argument.
2601 * - skip until a blank found, taking care of backslashes
2602 * - skip blanks
2603 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2604 */
2605 for (i = 0; i < 2 ; ++i)
2606 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002607 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if (*arg++ == '\\' && *arg != NUL)
2609 ++arg;
2610 arg = skipwhite(arg);
2611 if (*arg != '=')
2612 break;
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002615 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002617 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2618 i = (int)STRLEN(IObuff) + 2;
2619 if (i + (arg - startarg) < IOSIZE)
2620 {
2621 // append the argument with the error
2622 STRCAT(IObuff, ": ");
2623 mch_memmove(IObuff + i, startarg, (arg - startarg));
2624 IObuff[i + (arg - startarg)] = NUL;
2625 }
2626 // make sure all characters are printable
2627 trans_characters(IObuff, IOSIZE);
2628
2629 ++no_wait_return; // wait_return() done later
2630 emsg((char *)IObuff); // show error highlighted
2631 --no_wait_return;
2632
2633 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636
2637 arg = skipwhite(arg);
2638 }
2639
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640theend:
2641 if (silent_mode && did_show)
2642 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002643 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002645 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002647 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002648 out_flush();
2649 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002650 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002651 }
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 return OK;
2654}
2655
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002656/*
2657 * Call this when an option has been given a new value through a user command.
2658 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2659 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002660 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002661did_set_option(
2662 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002663 int opt_flags, // possibly with OPT_MODELINE
2664 int new_value, // value was replaced completely
2665 int value_checked) // value was checked to be safe, no need to set the
2666 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002667{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002668 long_u *p;
2669
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002670 options[opt_idx].flags |= P_WAS_SET;
2671
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002672 // When an option is set in the sandbox, from a modeline or in secure mode
2673 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2674 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002676 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002677#ifdef HAVE_SANDBOX
2678 || sandbox != 0
2679#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002680 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002681 *p = *p | P_INSECURE;
2682 else if (new_value)
2683 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002684}
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686/*
2687 * Convert a key name or string into a key value.
2688 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002689 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002691 int
2692string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002695 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (*arg == '^')
2697 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002698 if (multi_byte)
2699 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return *arg;
2701}
2702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703/*
2704 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2705 * maketitle() to create and display it.
2706 * When switching the title or icon off, call mch_restore_title() to get
2707 * the old value back.
2708 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002709 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002710did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 if (starting != NO_SCREEN
2713#ifdef FEAT_GUI
2714 && !gui.starting
2715#endif
2716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720/*
2721 * set_options_bin - called when 'bin' changes value.
2722 */
2723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002724set_options_bin(
2725 int oldval,
2726 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 /*
2730 * The option values that are changed when 'bin' changes are
2731 * copied when 'bin is set and restored when 'bin' is reset.
2732 */
2733 if (newval)
2734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
2737 if (!(opt_flags & OPT_GLOBAL))
2738 {
2739 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2740 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2741 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2742 curbuf->b_p_et_nobin = curbuf->b_p_et;
2743 }
2744 if (!(opt_flags & OPT_LOCAL))
2745 {
2746 p_tw_nobin = p_tw;
2747 p_wm_nobin = p_wm;
2748 p_ml_nobin = p_ml;
2749 p_et_nobin = p_et;
2750 }
2751 }
2752
2753 if (!(opt_flags & OPT_GLOBAL))
2754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 curbuf->b_p_tw = 0; // no automatic line wrap
2756 curbuf->b_p_wm = 0; // no automatic line wrap
2757 curbuf->b_p_ml = 0; // no modelines
2758 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760 if (!(opt_flags & OPT_LOCAL))
2761 {
2762 p_tw = 0;
2763 p_wm = 0;
2764 p_ml = FALSE;
2765 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 {
2771 if (!(opt_flags & OPT_GLOBAL))
2772 {
2773 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2774 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2775 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2776 curbuf->b_p_et = curbuf->b_p_et_nobin;
2777 }
2778 if (!(opt_flags & OPT_LOCAL))
2779 {
2780 p_tw = p_tw_nobin;
2781 p_wm = p_wm_nobin;
2782 p_ml = p_ml_nobin;
2783 p_et = p_et_nobin;
2784 }
2785 }
2786}
2787
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788/*
2789 * Expand environment variables for some string options.
2790 * These string options cannot be indirect!
2791 * If "val" is NULL expand the current value of the option.
2792 * Return pointer to NameBuff, or NULL when not expanded.
2793 */
2794 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002795option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2799 return NULL;
2800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 // If val is longer than MAXPATHL no meaningful expansion can be done,
2802 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (val != NULL && STRLEN(val) > MAXPATHL)
2804 return NULL;
2805
2806 if (val == NULL)
2807 val = *(char_u **)options[opt_idx].var;
2808
2809 /*
2810 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2811 * Escape spaces when expanding 'tags', they are used to separate file
2812 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002813 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 */
2815 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002816 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002817#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002818 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2819#endif
2820 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002821 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 return NULL;
2823
2824 return NameBuff;
2825}
2826
2827/*
2828 * After setting various option values: recompute variables that depend on
2829 * option values.
2830 */
2831 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002832didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002834 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 (void)init_chartab();
2836
Bram Moolenaardac13472019-09-16 21:06:21 +02002837 didset_string_options();
2838
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002839#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002840 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002841 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002842 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002843 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002844#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002845 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002847#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002849 fill_breakat_flags();
2850#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002851 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002852}
2853
2854/*
2855 * More side effects of setting options.
2856 */
2857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002858didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002859{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002861 (void)highlight_changed();
2862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002864 check_opt_wim();
2865
Bram Moolenaareed9d462021-02-15 20:38:25 +01002866 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002867 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002870 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002871
2872#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002874 (void)check_clipboard_option();
2875#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002876#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002877 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002878 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002879 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002880 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882}
2883
2884/*
2885 * Check for string options that are NULL (normally only termcap options).
2886 */
2887 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002888check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
2890 int opt_idx;
2891
2892 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2893 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2894 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2895}
2896
2897/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002898 * Return the option index found by a pointer into term_strings[].
2899 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002901 int
2902get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002904 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
2906 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2907 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002908 return opt_idx;
2909 return -1; // cannot happen: didn't find it!
2910}
2911
2912/*
2913 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2914 * Return the option index or -1 if not found.
2915 */
2916 int
2917set_term_option_alloced(char_u **p)
2918{
2919 int opt_idx = get_term_opt_idx(p);
2920
2921 if (opt_idx >= 0)
2922 options[opt_idx].flags |= P_ALLOCED;
2923 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924}
2925
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002926#if defined(FEAT_EVAL) || defined(PROTO)
2927/*
2928 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2929 * Return FALSE when it wasn't.
2930 * Return -1 for an unknown option.
2931 */
2932 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002933was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002934{
2935 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002936 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002937
2938 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002939 {
2940 flagp = insecure_flag(idx, opt_flags);
2941 return (*flagp & P_INSECURE) != 0;
2942 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002943 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002944 return -1;
2945}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002946
2947/*
2948 * Get a pointer to the flags used for the P_INSECURE flag of option
2949 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002950 * NOTE: Caller must make sure that "curwin" is set to the window from which
2951 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002952 */
2953 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002954insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002955{
2956 if (opt_flags & OPT_LOCAL)
2957 switch ((int)options[opt_idx].indir)
2958 {
2959#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002960 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002961#endif
2962#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002963# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002964 case PV_FDE: return &curwin->w_p_fde_flags;
2965 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002966# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002967# ifdef FEAT_BEVAL
2968 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2969# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002970 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002971 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002972# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002973 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002974# endif
2975#endif
2976 }
2977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002979 return &options[opt_idx].flags;
2980}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002981#endif
2982
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002983/*
2984 * Redraw the window title and/or tab page text later.
2985 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002986void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002987{
2988 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002989 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002990}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002993 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2994 * characters or characters in "allowed".
2995 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002996 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002997valid_name(char_u *val, char *allowed)
2998{
2999 char_u *s;
3000
3001 for (s = val; *s != NUL; ++s)
3002 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3003 return FALSE;
3004 return TRUE;
3005}
3006
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003007#if defined(FEAT_EVAL) || defined(PROTO)
3008/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003009 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003010 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003011 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003012 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003013set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003014{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003015 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3016 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003017 sctx_T new_script_ctx = script_ctx;
3018
Bram Moolenaar51258742020-05-03 17:19:33 +02003019 // Modeline already has the line number set.
3020 if (!(opt_flags & OPT_MODELINE))
3021 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // Remember where the option was set. For local options need to do that
3024 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003025 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003026 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003027 if (both || (opt_flags & OPT_LOCAL))
3028 {
3029 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003030 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003031 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003032 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003033 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003034 if (both)
3035 // also setting the "all buffers" value
3036 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3037 new_script_ctx;
3038 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003039 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003040}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003041
3042/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003043 * Get the script context of global option "name".
3044 *
3045 */
3046 sctx_T *
3047get_option_sctx(char *name)
3048{
3049 int idx = findoption((char_u *)name);
3050
3051 if (idx >= 0)
3052 return &options[idx].script_ctx;
3053 siemsg("no such option: %s", name);
3054 return NULL;
3055}
3056
3057/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003058 * Set the script_ctx for a termcap option.
3059 * "name" must be the two character code, e.g. "RV".
3060 * When "name" is NULL use "opt_idx".
3061 */
3062 void
3063set_term_option_sctx_idx(char *name, int opt_idx)
3064{
3065 char_u buf[5];
3066 int idx;
3067
3068 if (name == NULL)
3069 idx = opt_idx;
3070 else
3071 {
3072 buf[0] = 't';
3073 buf[1] = '_';
3074 buf[2] = name[0];
3075 buf[3] = name[1];
3076 buf[4] = 0;
3077 idx = findoption(buf);
3078 }
3079 if (idx >= 0)
3080 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3081}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003082#endif
3083
Bram Moolenaarf4140482020-02-15 23:06:45 +01003084#if defined(FEAT_EVAL)
3085/*
3086 * Apply the OptionSet autocommand.
3087 */
3088 static void
3089apply_optionset_autocmd(
3090 int opt_idx,
3091 long opt_flags,
3092 long oldval,
3093 long oldval_g,
3094 long newval,
3095 char *errmsg)
3096{
3097 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3098
3099 // Don't do this while starting up, failure or recursively.
3100 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3101 return;
3102
3103 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3104 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3105 oldval_g);
3106 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3107 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3108 (opt_flags & OPT_LOCAL) ? "local" : "global");
3109 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3110 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3111 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3112 if (opt_flags & OPT_LOCAL)
3113 {
3114 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3115 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3116 }
3117 if (opt_flags & OPT_GLOBAL)
3118 {
3119 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3120 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3121 }
3122 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3123 {
3124 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3125 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3126 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3127 }
3128 if (opt_flags & OPT_MODELINE)
3129 {
3130 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3131 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3132 }
3133 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3134 NULL, FALSE, NULL);
3135 reset_v_option_vars();
3136}
3137#endif
3138
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139/*
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003140 * Process the updated 'compatible' option value.
3141 */
3142 static void
3143did_set_compatible(void)
3144{
3145 compatible_set();
3146}
3147
3148#ifdef FEAT_LANGMAP
3149/*
3150 * Process the updated 'langremap' option value.
3151 */
3152 static void
3153did_set_langremap(void)
3154{
3155 // 'langremap' -> !'langnoremap'
3156 p_lnr = !p_lrm;
3157}
3158
3159/*
3160 * Process the updated 'langnoremap' option value.
3161 */
3162 static void
3163did_set_langnoremap(void)
3164{
3165 // 'langnoremap' -> !'langremap'
3166 p_lrm = !p_lnr;
3167}
3168#endif
3169
3170#ifdef FEAT_PERSISTENT_UNDO
3171/*
3172 * Process the updated 'undofile' option value.
3173 */
3174 static void
3175did_set_undofile(int opt_flags)
3176{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003177 // Only take action when the option was set.
3178 if (!curbuf->b_p_udf && !p_udf)
3179 return;
3180
3181 // When reset we do not delete the undo file, the option may be set again
3182 // without making any changes in between.
3183 char_u hash[UNDO_HASH_SIZE];
3184 buf_T *save_curbuf = curbuf;
3185
3186 FOR_ALL_BUFFERS(curbuf)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003187 {
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003188 // When 'undofile' is set globally: for every buffer, otherwise
3189 // only for the current buffer: Try to read in the undofile,
3190 // if one exists, the buffer wasn't changed and the buffer was
3191 // loaded
3192 if ((curbuf == save_curbuf
3193 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
3194 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003195 {
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003196#ifdef FEAT_CRYPT
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003197 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
3198 continue;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003199#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003200 u_compute_hash(hash);
3201 u_read_undo(NULL, hash, curbuf->b_fname);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003202 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003203 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003204 curbuf = save_curbuf;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003205}
3206#endif
3207
3208/*
3209 * Process the updated 'readonly' option value.
3210 */
3211 static void
3212did_set_readonly(int opt_flags)
3213{
3214 // when 'readonly' is reset globally, also reset readonlymode
3215 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
3216 readonlymode = FALSE;
3217
3218 // when 'readonly' is set may give W10 again
3219 if (curbuf->b_p_ro)
3220 curbuf->b_did_warn = FALSE;
3221
3222 redraw_titles();
3223}
3224
3225#ifdef FEAT_GUI
3226/*
3227 * Process the updated 'mousehide' option value.
3228 */
3229 static void
3230did_set_mousehide(void)
3231{
3232 if (!p_mh)
3233 gui_mch_mousehide(FALSE);
3234}
3235#endif
3236
3237/*
3238 * Process the updated 'modifiable' option value.
3239 */
3240 static char *
3241did_set_modifiable(int *doskip UNUSED)
3242{
3243 // when 'modifiable' is changed, redraw the window title
3244
3245# ifdef FEAT_TERMINAL
3246 // Cannot set 'modifiable' when in Terminal mode.
3247 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3248 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3249 {
3250 curbuf->b_p_ma = FALSE;
3251 *doskip = TRUE;
3252 return e_cannot_make_terminal_with_running_job_modifiable;
3253 }
3254# endif
3255 redraw_titles();
3256
3257 return NULL;
3258}
3259
3260/*
3261 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3262 * option value.
3263 */
3264 static void
3265did_set_eof_eol_fixeol_bomb(void)
3266{
3267 // redraw the window title and tab page text
3268 redraw_titles();
3269}
3270
3271/*
3272 * Process the updated 'binary' option value.
3273 */
3274 static void
3275did_set_binary(int opt_flags, long old_value)
3276{
3277 // when 'bin' is set also set some other options
3278 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
3279 redraw_titles();
3280}
3281
3282/*
3283 * Process the updated 'buflisted' option value.
3284 */
3285 static void
3286did_set_buflisted(long old_value)
3287{
3288 // when 'buflisted' changes, trigger autocommands
3289 if (old_value != curbuf->b_p_bl)
3290 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3291 NULL, NULL, TRUE, curbuf);
3292}
3293
3294/*
3295 * Process the updated 'swapfile' option value.
3296 */
3297 static void
3298did_set_swapfile(void)
3299{
3300 // when 'swf' is set, create swapfile, when reset remove swapfile
3301 if (curbuf->b_p_swf && p_uc)
3302 ml_open_file(curbuf); // create the swap file
3303 else
3304 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3305 // buf->b_p_swf
3306 mf_close_file(curbuf, TRUE); // remove the swap file
3307}
3308
3309/*
3310 * Process the updated 'terse' option value.
3311 */
3312 static void
3313did_set_terse(void)
3314{
3315 char_u *p;
3316
3317 // when 'terse' is set change 'shortmess'
3318 p = vim_strchr(p_shm, SHM_SEARCH);
3319
3320 // insert 's' in p_shm
3321 if (p_terse && p == NULL)
3322 {
3323 STRCPY(IObuff, p_shm);
3324 STRCAT(IObuff, "s");
3325 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
3326 }
3327 // remove 's' from p_shm
3328 else if (!p_terse && p != NULL)
3329 STRMOVE(p, p + 1);
3330}
3331
3332/*
3333 * Process the updated 'paste' option value.
3334 */
3335 static void
3336did_set_paste(void)
3337{
3338 // when 'paste' is set or reset also change other options
3339 paste_option_changed();
3340}
3341
3342/*
3343 * Process the updated 'insertmode' option value.
3344 */
3345 static void
3346did_set_insertmode(long old_value)
3347{
3348 // when 'insertmode' is set from an autocommand need to do work here
3349 if (p_im)
3350 {
3351 if ((State & MODE_INSERT) == 0)
3352 need_start_insertmode = TRUE;
3353 stop_insert_mode = FALSE;
3354 }
3355 // only reset if it was set previously
3356 else if (old_value)
3357 {
3358 need_start_insertmode = FALSE;
3359 stop_insert_mode = TRUE;
3360 if (restart_edit != 0 && mode_displayed)
3361 clear_cmdline = TRUE; // remove "(insert)"
3362 restart_edit = 0;
3363 }
3364}
3365
3366/*
3367 * Process the updated 'ignorecase' option value.
3368 */
3369 static void
3370did_set_ignorecase(void)
3371{
3372 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3373 if (p_hls)
3374 redraw_all_later(UPD_SOME_VALID);
3375}
3376
3377#ifdef FEAT_SEARCH_EXTRA
3378/*
3379 * Process the updated 'hlsearch' option value.
3380 */
3381 static void
3382did_set_hlsearch(void)
3383{
3384 // when 'hlsearch' is set or reset: reset no_hlsearch
3385 set_no_hlsearch(FALSE);
3386}
3387#endif
3388
3389/*
3390 * Process the updated 'scrollbind' option value.
3391 */
3392 static void
3393did_set_scrollbind(void)
3394{
3395 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3396 // at the end of normal_cmd()
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003397 if (!curwin->w_p_scb)
3398 return;
3399 do_check_scrollbind(FALSE);
3400 curwin->w_scbind_pos = curwin->w_topline;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003401}
3402
3403#ifdef FEAT_QUICKFIX
3404/*
3405 * Process the updated 'previewwindow' option value.
3406 */
3407 static char *
3408did_set_previewwindow(int *doskip)
3409{
3410 if (!curwin->w_p_pvw)
3411 return NULL;
3412
3413 // There can be only one window with 'previewwindow' set.
3414 win_T *win;
3415
3416 FOR_ALL_WINDOWS(win)
3417 if (win->w_p_pvw && win != curwin)
3418 {
3419 curwin->w_p_pvw = FALSE;
3420 *doskip = TRUE;
3421 return e_preview_window_already_exists;
3422 }
3423
3424 return NULL;
3425}
3426#endif
3427
3428/*
3429 * Process the updated 'smoothscroll' option value.
3430 */
3431 static void
3432did_set_smoothscroll(void)
3433{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003434 if (curwin->w_p_sms)
3435 return;
3436 curwin->w_skipcol = 0;
3437 changed_line_abv_curs();
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003438}
3439
3440/*
3441 * Process the updated 'textmode' option value.
3442 */
3443 static void
3444did_set_textmode(int opt_flags)
3445{
3446 // when 'textmode' is set or reset also change 'fileformat'
3447 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3448}
3449
3450/*
3451 * Process the updated 'textauto' option value.
3452 */
3453 static void
3454did_set_textauto(int opt_flags)
3455{
3456 // when 'textauto' is set or reset also change 'fileformats'
3457 set_string_option_direct((char_u *)"ffs", -1,
3458 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
3459 OPT_FREE | opt_flags, 0);
3460}
3461
3462/*
3463 * Process the updated 'lisp' option value.
3464 */
3465 static void
3466did_set_lisp(void)
3467{
3468 // When 'lisp' option changes include/exclude '-' in keyword characters.
3469 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3470}
3471
3472/*
3473 * Process the updated 'title' or the 'icon' option value.
3474 */
3475 static void
3476did_set_title_icon(void)
3477{
3478 // when 'title' changed, may need to change the title; same for 'icon'
3479 did_set_title();
3480}
3481
3482/*
3483 * Process the updated 'modified' option value.
3484 */
3485 static void
3486did_set_modified(long value)
3487{
3488 if (!value)
3489 save_file_ff(curbuf); // Buffer is unchanged
3490 redraw_titles();
3491 modified_was_set = value;
3492}
3493
3494#ifdef BACKSLASH_IN_FILENAME
3495/*
3496 * Process the updated 'shellslash' option value.
3497 */
3498 static void
3499did_set_shellslash(void)
3500{
3501 if (p_ssl)
3502 {
3503 psepc = '/';
3504 psepcN = '\\';
3505 pseps[0] = '/';
3506 }
3507 else
3508 {
3509 psepc = '\\';
3510 psepcN = '/';
3511 pseps[0] = '\\';
3512 }
3513
3514 // need to adjust the file name arguments and buffer names.
3515 buflist_slash_adjust();
3516 alist_slash_adjust();
3517# ifdef FEAT_EVAL
3518 scriptnames_slash_adjust();
3519# endif
3520}
3521#endif
3522
3523/*
3524 * Process the updated 'wrap' option value.
3525 */
3526 static void
3527did_set_wrap(void)
3528{
3529 // If 'wrap' is set, set w_leftcol to zero.
3530 if (curwin->w_p_wrap)
3531 curwin->w_leftcol = 0;
3532}
3533
3534/*
3535 * Process the updated 'equalalways' option value.
3536 */
3537 static void
3538did_set_equalalways(long old_value)
3539{
3540 if (p_ea && !old_value)
3541 win_equal(curwin, FALSE, 0);
3542}
3543
3544/*
3545 * Process the updated 'weirdinvert' option value.
3546 */
3547 static void
3548did_set_weirdinvert(long old_value)
3549{
3550 // When 'weirdinvert' changed, set/reset 't_xs'.
3551 // Then set 'weirdinvert' according to value of 't_xs'.
3552 if (p_wiv && !old_value)
3553 T_XS = (char_u *)"y";
3554 else if (!p_wiv && old_value)
3555 T_XS = empty_option;
3556 p_wiv = (*T_XS != NUL);
3557}
3558
3559#ifdef FEAT_BEVAL_GUI
3560/*
3561 * Process the updated 'ballooneval' option value.
3562 */
3563 static void
3564did_set_ballooneval(long old_value)
3565{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003566 if (balloonEvalForTerm)
3567 return;
3568 if (p_beval && !old_value)
3569 gui_mch_enable_beval_area(balloonEval);
3570 else if (!p_beval && old_value)
3571 gui_mch_disable_beval_area(balloonEval);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003572}
3573#endif
3574
3575#ifdef FEAT_BEVAL_TERM
3576/*
3577 * Process the updated 'balloonevalterm' option value.
3578 */
3579 static void
3580did_set_balloonevalterm(void)
3581{
3582 mch_bevalterm_changed();
3583}
3584#endif
3585
3586#ifdef FEAT_AUTOCHDIR
3587/*
3588 * Process the updated 'autochdir' option value.
3589 */
3590 static void
3591did_set_autochdir(void)
3592{
3593 // Change directories when the 'acd' option is set now.
3594 DO_AUTOCHDIR;
3595}
3596#endif
3597
3598#ifdef FEAT_DIFF
3599/*
3600 * Process the updated 'diff' option value.
3601 */
3602 static void
3603did_set_diff(void)
3604{
3605 // May add or remove the buffer from the list of diff buffers.
3606 diff_buf_adjust(curwin);
3607# ifdef FEAT_FOLDING
3608 if (foldmethodIsDiff(curwin))
3609 foldUpdateAll(curwin);
3610# endif
3611}
3612#endif
3613
3614#ifdef HAVE_INPUT_METHOD
3615/*
3616 * Process the updated 'imdisable' option value.
3617 */
3618 static void
3619did_set_imdisable(void)
3620{
3621 // Only de-activate it here, it will be enabled when changing mode.
3622 if (p_imdisable)
3623 im_set_active(FALSE);
3624 else if (State & MODE_INSERT)
3625 // When the option is set from an autocommand, it may need to take
3626 // effect right away.
3627 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3628}
3629#endif
3630
3631#ifdef FEAT_SPELL
3632/*
3633 * Process the updated 'spell' option value.
3634 */
3635 static char *
3636did_set_spell(void)
3637{
3638 if (curwin->w_p_spell)
3639 return did_set_spelllang(curwin);
3640
3641 return NULL;
3642}
3643#endif
3644
3645#ifdef FEAT_ARABIC
3646/*
3647 * Process the updated 'arabic' option value.
3648 */
3649 static char *
3650did_set_arabic(void)
3651{
3652 char *errmsg = NULL;
3653
3654 if (curwin->w_p_arab)
3655 {
3656 /*
3657 * 'arabic' is set, handle various sub-settings.
3658 */
3659 if (!p_tbidi)
3660 {
3661 // set rightleft mode
3662 if (!curwin->w_p_rl)
3663 {
3664 curwin->w_p_rl = TRUE;
3665 changed_window_setting();
3666 }
3667
3668 // Enable Arabic shaping (major part of what Arabic requires)
3669 if (!p_arshape)
3670 {
3671 p_arshape = TRUE;
3672 redraw_later_clear();
3673 }
3674 }
3675
3676 // Arabic requires a utf-8 encoding, inform the user if it's not
3677 // set.
3678 if (STRCMP(p_enc, "utf-8") != 0)
3679 {
3680 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3681
3682 msg_source(HL_ATTR(HLF_W));
3683 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3684#ifdef FEAT_EVAL
3685 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3686#endif
3687 }
3688
3689 // set 'delcombine'
3690 p_deco = TRUE;
3691
3692# ifdef FEAT_KEYMAP
3693 // Force-set the necessary keymap for arabic
3694 errmsg = set_option_value((char_u *)"keymap",
3695 0L, (char_u *)"arabic", OPT_LOCAL);
3696# endif
3697 }
3698 else
3699 {
3700 /*
3701 * 'arabic' is reset, handle various sub-settings.
3702 */
3703 if (!p_tbidi)
3704 {
3705 // reset rightleft mode
3706 if (curwin->w_p_rl)
3707 {
3708 curwin->w_p_rl = FALSE;
3709 changed_window_setting();
3710 }
3711
3712 // 'arabicshape' isn't reset, it is a global option and
3713 // another window may still need it "on".
3714 }
3715
3716 // 'delcombine' isn't reset, it is a global option and another
3717 // window may still want it "on".
3718
3719# ifdef FEAT_KEYMAP
3720 // Revert to the default keymap
3721 curbuf->b_p_iminsert = B_IMODE_NONE;
3722 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3723# endif
3724 }
3725
3726 return errmsg;
3727}
3728#endif
3729
3730#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3731/*
3732 * Process the updated 'number' or 'relativenumber' option value.
3733 */
3734 static void
3735did_set_number_relativenumber(char_u *varp)
3736{
3737 if (gui.in_use
3738 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3739 && curbuf->b_signlist != NULL)
3740 {
3741 // If the 'number' or 'relativenumber' options are modified and
3742 // 'signcolumn' is set to 'number', then clear the screen for a full
3743 // refresh. Otherwise the sign icons are not displayed properly in the
3744 // number column. If the 'number' option is set and only the
3745 // 'relativenumber' option is toggled, then don't refresh the screen
3746 // (optimization).
3747 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3748 redraw_all_later(UPD_CLEAR);
3749 }
3750}
3751#endif
3752
3753#ifdef FEAT_TERMGUICOLORS
3754 static char *
3755did_set_termguicolors(int *doskip UNUSED)
3756{
3757# ifdef FEAT_VTP
3758 // Do not turn on 'tgc' when 24-bit colors are not supported.
3759 if (
3760# ifdef VIMDLL
3761 !gui.in_use && !gui.starting &&
3762# endif
3763 !has_vtp_working())
3764 {
3765 p_tgc = 0;
3766 *doskip = TRUE;
3767 return e_24_bit_colors_are_not_supported_on_this_environment;
3768 }
3769 if (is_term_win32())
3770 swap_tcap();
3771# endif
3772# ifdef FEAT_GUI
3773 if (!gui.in_use && !gui.starting)
3774# endif
3775 highlight_gui_started();
3776# ifdef FEAT_VTP
3777 // reset t_Co
3778 if (is_term_win32())
3779 {
3780 control_console_color_rgb();
3781 set_termname(T_NAME);
3782 init_highlight(TRUE, FALSE);
3783 }
3784# endif
3785# ifdef FEAT_TERMINAL
3786 term_update_colors_all();
3787 term_update_palette_all();
3788 term_update_wincolor_all();
3789# endif
3790
3791 return NULL;
3792}
3793#endif
3794
3795/*
3796 * When some boolean options are changed, need to take some action.
3797 */
3798 static char *
3799did_set_bool_option(
3800 char_u *varp,
3801 int opt_flags,
3802 long value,
3803 long old_value,
3804 int *doskip)
3805{
3806 char *errmsg = NULL;
3807
3808 if ((int *)varp == &p_cp) // 'compatible'
3809 did_set_compatible();
3810#ifdef FEAT_LANGMAP
3811 else if ((int *)varp == &p_lrm) // 'langremap'
3812 did_set_langremap();
3813 else if ((int *)varp == &p_lnr) // 'langnoremap'
3814 did_set_langnoremap();
3815#endif
3816#ifdef FEAT_PERSISTENT_UNDO
3817 else if ((int *)varp == &curbuf->b_p_udf // buffer local 'undofile'
3818 || (int *)varp == &p_udf) // 'undofile'
3819 did_set_undofile(opt_flags);
3820#endif
3821 else if ((int *)varp == &curbuf->b_p_ro) // 'readonly'
3822 did_set_readonly(opt_flags);
3823#ifdef FEAT_GUI
3824 else if ((int *)varp == &p_mh) // 'mousehide'
3825 did_set_mousehide();
3826#endif
3827 else if ((int *)varp == &curbuf->b_p_ma)
3828 errmsg = did_set_modifiable(doskip); // 'modifiable'
3829 else if ((int *)varp == &curbuf->b_p_eof // 'endoffile'
3830 || (int *)varp == &curbuf->b_p_eol // 'endofline'
3831 || (int *)varp == &curbuf->b_p_fixeol // 'fixendofline'
3832 || (int *)varp == &curbuf->b_p_bomb) // 'bomb'
3833 did_set_eof_eol_fixeol_bomb();
3834 else if ((int *)varp == &curbuf->b_p_bin) // 'binary'
3835 did_set_binary(opt_flags, old_value);
3836 else if ((int *)varp == &curbuf->b_p_bl) // 'buflisted'
3837 did_set_buflisted(old_value);
3838 else if ((int *)varp == &curbuf->b_p_swf) // 'swapfile'
3839 did_set_swapfile();
3840 else if ((int *)varp == &p_terse) // 'terse'
3841 did_set_terse();
3842 else if ((int *)varp == &p_paste) // 'paste'
3843 did_set_paste();
3844 else if ((int *)varp == &p_im) // 'insertmode'
3845 did_set_insertmode(old_value);
3846 else if ((int *)varp == &p_ic) // 'ignorecase'
3847 did_set_ignorecase();
3848#ifdef FEAT_SEARCH_EXTRA
3849 else if ((int *)varp == &p_hls) // 'hlsearch'
3850 did_set_hlsearch();
3851#endif
3852 else if ((int *)varp == &curwin->w_p_scb) // 'scrollbind'
3853 did_set_scrollbind();
3854#ifdef FEAT_QUICKFIX
3855 else if ((int *)varp == &curwin->w_p_pvw) // 'previewwindow'
3856 errmsg = did_set_previewwindow(doskip);
3857#endif
3858 else if ((int *)varp == &curwin->w_p_sms) // 'smoothscroll'
3859 did_set_smoothscroll();
3860 else if ((int *)varp == &curbuf->b_p_tx) // 'textmode'
3861 did_set_textmode(opt_flags);
3862 else if ((int *)varp == &p_ta) // 'textauto'
3863 did_set_textauto(opt_flags);
3864 else if (varp == (char_u *)&(curbuf->b_p_lisp)) // 'lisp'
3865 did_set_lisp();
3866 else if ( (int *)varp == &p_title // 'title'
3867 || (int *)varp == &p_icon) // 'icon'
3868 did_set_title_icon();
3869 else if ((int *)varp == &curbuf->b_changed) // 'modified'
3870 did_set_modified(value);
3871#ifdef BACKSLASH_IN_FILENAME
3872 else if ((int *)varp == &p_ssl) // 'shellslash'
3873 did_set_shellslash();
3874#endif
3875 else if ((int *)varp == &curwin->w_p_wrap) // 'wrap'
3876 did_set_wrap();
3877 else if ((int *)varp == &p_ea) // 'equalalways'
3878 did_set_equalalways(old_value);
3879 else if ((int *)varp == &p_wiv) // weirdinvert'
3880 did_set_weirdinvert(old_value);
3881#ifdef FEAT_BEVAL_GUI
3882 else if ((int *)varp == &p_beval) // 'ballooneval'
3883 did_set_ballooneval(old_value);
3884#endif
3885#ifdef FEAT_BEVAL_TERM
3886 else if ((int *)varp == &p_bevalterm) // 'balloonevalterm'
3887 did_set_balloonevalterm();
3888#endif
3889#ifdef FEAT_AUTOCHDIR
3890 else if ((int *)varp == &p_acd) // 'autochdir'
3891 did_set_autochdir();
3892#endif
3893#ifdef FEAT_DIFF
3894 else if ((int *)varp == &curwin->w_p_diff) // 'diff'
3895 did_set_diff();
3896#endif
3897#ifdef HAVE_INPUT_METHOD
3898 else if ((int *)varp == &p_imdisable) // 'imdisable'
3899 did_set_imdisable();
3900#endif
3901#ifdef FEAT_SPELL
3902 else if ((int *)varp == &curwin->w_p_spell) // 'spell'
3903 errmsg = did_set_spell();
3904#endif
3905#ifdef FEAT_ARABIC
3906 else if ((int *)varp == &curwin->w_p_arab) // 'arabic'
3907 errmsg = did_set_arabic();
3908#endif
3909#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3910 else if ( (int *)varp == &curwin->w_p_nu // 'number'
3911 || (int *)varp == &curwin->w_p_rnu) // 'relativenumber'
3912 did_set_number_relativenumber(varp);
3913#endif
3914#ifdef FEAT_TERMGUICOLORS
3915 else if ((int *)varp == &p_tgc) // 'termguicolors'
3916 errmsg = did_set_termguicolors(doskip);
3917#endif
3918
3919 return errmsg;
3920}
3921
3922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 * Set the value of a boolean option, and take care of side effects.
3924 * Returns NULL for success, or an error message for an error.
3925 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003926 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003927set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003928 int opt_idx, // index in options[] table
3929 char_u *varp, // pointer to the option variable
3930 int value, // new value
3931 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
3933 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003934#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003935 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003936#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003937 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003939 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if ((secure
3941#ifdef HAVE_SANDBOX
3942 || sandbox != 0
3943#endif
3944 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003945 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003947#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003948 // Save the global value before changing anything. This is needed as for
3949 // a global-only option setting the "local value" in fact sets the global
3950 // value (since there is only one value).
3951 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3952 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
3953 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003954#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003956 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003958 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003959 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#endif
3961
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003962#ifdef FEAT_GUI
3963 need_mouse_correct = TRUE;
3964#endif
3965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3968 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
3969
3970 /*
3971 * Handle side effects of changing a bool option.
3972 */
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003973 int doskip = FALSE;
3974 errmsg = did_set_bool_option(varp, opt_flags, value, old_value, &doskip);
3975 if (doskip)
3976 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003978 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 options[opt_idx].flags |= P_WAS_SET;
3981
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003982#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003983 apply_optionset_autocmd(opt_idx, opt_flags,
3984 (long)(old_value ? TRUE : FALSE),
3985 (long)(old_global_value ? TRUE : FALSE),
3986 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003987#endif
3988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003989 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003990 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003991 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003992 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003993
3994 if ((opt_flags & OPT_NO_REDRAW) == 0)
3995 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003997 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998}
3999
4000/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004001 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004003 static char *
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004004did_set_winheight_helpheight(long *pp, char *errmsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004006 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004008 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004009 p_wh = 1;
4010 }
4011 if (p_wmh > p_wh)
4012 {
4013 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4014 p_wh = p_wmh;
4015 }
4016 if (p_hh < 0)
4017 {
4018 errmsg = e_argument_must_be_positive;
4019 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004022 // Change window height NOW
4023 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004025 if (pp == &p_wh && curwin->w_height < p_wh)
4026 win_setheight((int)p_wh);
4027 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4028 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004031 return errmsg;
4032}
4033
4034/*
4035 * Process the new 'winminheight' option value.
4036 */
4037 static char *
4038did_set_winminheight(char *errmsg)
4039{
4040 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004042 errmsg = e_argument_must_be_positive;
4043 p_wmh = 0;
4044 }
4045 if (p_wmh > p_wh)
4046 {
4047 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4048 p_wmh = p_wh;
4049 }
4050 win_setminheight();
4051
4052 return errmsg;
4053}
4054
4055/*
4056 * Process the new 'winwidth' option value.
4057 */
4058 static char *
4059did_set_winwidth(char *errmsg)
4060{
4061 if (p_wiw < 1)
4062 {
4063 errmsg = e_argument_must_be_positive;
4064 p_wiw = 1;
4065 }
4066 if (p_wmw > p_wiw)
4067 {
4068 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4069 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004071
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004072 // Change window width NOW
4073 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4074 win_setwidth((int)p_wiw);
4075
4076 return errmsg;
4077}
4078
4079/*
4080 * Process the new 'winminwidth' option value.
4081 */
4082 static char *
4083did_set_winminwidth(char *errmsg)
4084{
4085 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004086 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004087 errmsg = e_argument_must_be_positive;
4088 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004089 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004090 if (p_wmw > p_wiw)
4091 {
4092 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4093 p_wmw = p_wiw;
4094 }
4095 win_setminwidth();
4096
4097 return errmsg;
4098}
4099
4100/*
4101 * Process the new 'laststatus' option value.
4102 */
4103 static void
4104did_set_laststatus(void)
4105{
4106 last_status(FALSE); // (re)set last window status line
4107}
4108
4109/*
4110 * Process the new 'showtabline' option value.
4111 */
4112 static void
4113did_set_showtabline(void)
4114{
4115 shell_new_rows(); // recompute window positions and heights
4116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004119/*
4120 * Process the new 'linespace' option value.
4121 */
4122 static void
4123did_set_linespace(void)
4124{
4125 // Recompute gui.char_height and resize the Vim window to keep the
4126 // same number of lines.
4127 if (gui.in_use && gui_mch_adjust_charheight() == OK)
4128 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
4129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#endif
4131
4132#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004133/*
4134 * Process the new 'foldlevel' option value.
4135 */
4136 static void
4137did_set_foldlevel(void)
4138{
4139 if (curwin->w_p_fdl < 0)
4140 curwin->w_p_fdl = 0;
4141 newFoldLevel();
4142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004144/*
4145 * Process the new 'foldminlines' option value.
4146 */
4147 static void
4148did_set_foldminlines(void)
4149{
4150 foldUpdateAll(curwin);
4151}
4152
4153/*
4154 * Process the new 'foldnestmax' option value.
4155 */
4156 static void
4157did_set_foldnestmax(void)
4158{
4159 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 foldUpdateAll(curwin);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004161}
4162
4163/*
4164 * Process the new 'foldcolumn' option value.
4165 */
4166 static char *
4167did_set_foldcolumn(char *errmsg)
4168{
4169 if (curwin->w_p_fdc < 0)
4170 {
4171 errmsg = e_argument_must_be_positive;
4172 curwin->w_p_fdc = 0;
4173 }
4174 else if (curwin->w_p_fdc > 12)
4175 {
4176 errmsg = e_invalid_argument;
4177 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004180 return errmsg;
4181}
4182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004184/*
4185 * Process the new 'shiftwidth' or the 'tabstop' option value.
4186 */
4187 static void
4188did_set_shiftwidth_tabstop(long *pp)
4189{
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004190#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004191 if (foldmethodIsIndent(curwin))
4192 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004193#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004194 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4195 // parse 'cinoptions'.
4196 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4197 parse_cino(curbuf);
4198}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004200/*
4201 * Process the new 'maxcombine' option value.
4202 */
4203 static void
4204did_set_maxcombine(void)
4205{
4206 if (p_mco > MAX_MCO)
4207 p_mco = MAX_MCO;
4208 else if (p_mco < 0)
4209 p_mco = 0;
4210 screenclear(); // will re-allocate the screen
4211}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004212
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004213/*
4214 * Process the new 'iminsert' option value.
4215 */
4216 static char *
4217did_set_iminsert(char *errmsg)
4218{
4219 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004221 errmsg = e_invalid_argument;
4222 curbuf->b_p_iminsert = B_IMODE_NONE;
4223 }
4224 p_iminsert = curbuf->b_p_iminsert;
4225 if (termcap_active) // don't do this in the alternate screen
4226 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004227#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004228 // Show/unshow value of 'keymap' in status lines.
4229 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004231
4232 return errmsg;
4233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004235#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004236/*
4237 * Process the new 'imstyle' option value.
4238 */
4239 static char *
4240did_set_imstyle(char *errmsg)
4241{
4242 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4243 errmsg = e_invalid_argument;
4244
4245 return errmsg;
4246}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004247#endif
4248
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004249/*
4250 * Process the new 'window' option value.
4251 */
4252 static void
4253did_set_window(void)
4254{
4255 if (p_window < 1)
4256 p_window = 1;
4257 else if (p_window >= Rows)
4258 p_window = Rows - 1;
4259}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004260
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004261/*
4262 * Process the new 'imsearch' option value.
4263 */
4264 static char *
4265did_set_imsearch(char *errmsg)
4266{
4267 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004269 errmsg = e_invalid_argument;
4270 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004272 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004274 return errmsg;
4275}
4276
4277/*
4278 * Process the new 'titlelen' option value.
4279 */
4280 static char *
4281did_set_titlelen(long old_value, char *errmsg)
4282{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004283 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004284 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004286 errmsg = e_argument_must_be_positive;
4287 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004289 if (starting != NO_SCREEN && old_value != p_titlelen)
4290 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004292 return errmsg;
4293}
4294
4295/*
4296 * Process the new 'cmdheight' option value.
4297 */
4298 static char *
4299did_set_cmdheight(long old_value, char *errmsg)
4300{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004301 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004302 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004304 errmsg = e_argument_must_be_positive;
4305 p_ch = 1;
4306 }
4307 if (p_ch > Rows - min_rows() + 1)
4308 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004310 // Only compute the new window layout when startup has been
4311 // completed. Otherwise the frame sizes may be wrong.
4312 if ((p_ch != old_value
4313 || tabline_height() + topframe->fr_height != Rows - p_ch)
4314 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004316 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004318 )
4319 command_height();
4320
4321 return errmsg;
4322}
4323
4324/*
4325 * Process the new 'updatecount' option value.
4326 */
4327 static char *
4328did_set_updatecount(long old_value, char *errmsg)
4329{
4330 // when 'updatecount' changes from zero to non-zero, open swap files
4331 if (p_uc < 0)
4332 {
4333 errmsg = e_argument_must_be_positive;
4334 p_uc = 100;
4335 }
4336 if (p_uc && !old_value)
4337 ml_open_files();
4338
4339 return errmsg;
4340}
4341
4342#ifdef FEAT_CONCEAL
4343/*
4344 * Process the new 'conceallevel' option value.
4345 */
4346 static char *
4347did_set_conceallevel(char *errmsg)
4348{
4349 if (curwin->w_p_cole < 0)
4350 {
4351 errmsg = e_argument_must_be_positive;
4352 curwin->w_p_cole = 0;
4353 }
4354 else if (curwin->w_p_cole > 3)
4355 {
4356 errmsg = e_invalid_argument;
4357 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004360 return errmsg;
4361}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004364#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004365/*
4366 * Process the new 'pyxversion' option value.
4367 */
4368 static char *
4369did_set_pyxversion(char *errmsg)
4370{
4371 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4372 errmsg = e_invalid_argument;
4373
4374 return errmsg;
4375}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004376#endif
4377
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004378/*
4379 * Process the new global 'undolevels' option value.
4380 */
4381 static void
4382did_set_global_undolevels(long value, long old_value)
4383{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004384 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004385
4386 // use the old value, otherwise u_sync() may not work properly
4387 p_ul = old_value;
4388 u_sync(TRUE);
4389 p_ul = value;
4390}
4391
4392/*
4393 * Process the new buffer local 'undolevels' option value.
4394 */
4395 static void
4396did_set_buflocal_undolevels(long value, long old_value)
4397{
4398 // use the old value, otherwise u_sync() may not work properly
4399 curbuf->b_p_ul = old_value;
4400 u_sync(TRUE);
4401 curbuf->b_p_ul = value;
4402}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004404#ifdef FEAT_LINEBREAK
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004405/*
4406 * Process the new 'numberwidth' option value.
4407 */
4408 static char *
4409did_set_numberwidth(char *errmsg)
4410{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004411 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004412 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004413 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004414 errmsg = e_argument_must_be_positive;
4415 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004416 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004417 if (curwin->w_p_nuw > 20)
4418 {
4419 errmsg = e_invalid_argument;
4420 curwin->w_p_nuw = 20;
4421 }
4422 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4423
4424 return errmsg;
4425}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004426#endif
4427
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004428/*
4429 * Process the new 'textwidth' option value.
4430 */
4431 static char *
4432did_set_textwidth(char *errmsg)
4433{
4434 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004435 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004436 errmsg = e_argument_must_be_positive;
4437 curbuf->b_p_tw = 0;
4438 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004439#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004440 {
4441 win_T *wp;
4442 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004443
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004444 FOR_ALL_TAB_WINDOWS(tp, wp)
4445 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004446 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004447#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004448
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004449 return errmsg;
4450}
4451
4452/*
4453 * When some number options are changed, need to take some action.
4454 */
4455 static char *
4456did_set_num_option(long *pp, long value, long old_value, char *errmsg)
4457{
4458 if (pp == &p_wh // 'winheight'
4459 || pp == &p_hh) // 'helpheight'
4460 errmsg = did_set_winheight_helpheight(pp, errmsg);
4461 else if (pp == &p_wmh) // 'winminheight'
4462 errmsg = did_set_winminheight(errmsg);
4463 else if (pp == &p_wiw) // 'winwidth'
4464 errmsg = did_set_winwidth(errmsg);
4465 else if (pp == &p_wmw) // 'winminwidth'
4466 errmsg = did_set_winminwidth(errmsg);
4467 else if (pp == &p_ls)
4468 did_set_laststatus(); // 'laststatus'
4469 else if (pp == &p_stal)
4470 did_set_showtabline(); // 'showtabline'
4471#ifdef FEAT_GUI
4472 else if (pp == &p_linespace) // 'linespace'
4473 did_set_linespace();
4474#endif
4475#ifdef FEAT_FOLDING
4476 else if (pp == &curwin->w_p_fdl) // 'foldlevel'
4477 did_set_foldlevel();
4478 else if (pp == &curwin->w_p_fml) // 'foldminlines'
4479 did_set_foldminlines();
4480 else if (pp == &curwin->w_p_fdn) // 'foldnestmax'
4481 did_set_foldnestmax();
4482 else if (pp == &curwin->w_p_fdc) // 'foldcolumn'
4483 errmsg = did_set_foldcolumn(errmsg);
4484#endif // FEAT_FOLDING
4485 else if ( pp == &curbuf->b_p_sw // 'shiftwidth'
4486 || pp == &curbuf->b_p_ts) // 'tabstop'
4487 did_set_shiftwidth_tabstop(pp);
4488 else if (pp == &p_mco) // 'maxcombine'
4489 did_set_maxcombine();
4490 else if (pp == &curbuf->b_p_iminsert) // 'iminsert'
4491 errmsg = did_set_iminsert(errmsg);
4492#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4493 else if (pp == &p_imst) // 'imstyle'
4494 errmsg = did_set_imstyle(errmsg);
4495#endif
4496 else if (pp == &p_window) // 'window'
4497 did_set_window();
4498 else if (pp == &curbuf->b_p_imsearch) // 'imsearch'
4499 errmsg = did_set_imsearch(errmsg);
4500 else if (pp == &p_titlelen) // 'titlelen'
4501 errmsg = did_set_titlelen(old_value, errmsg);
4502 else if (pp == &p_ch) // 'cmdheight'
4503 errmsg = did_set_cmdheight(old_value, errmsg);
4504 else if (pp == &p_uc) // 'updatecount'
4505 errmsg = did_set_updatecount(old_value, errmsg);
4506#ifdef FEAT_CONCEAL
4507 else if (pp == &curwin->w_p_cole) // 'conceallevel'
4508 errmsg = did_set_conceallevel(errmsg);
4509#endif
4510#ifdef MZSCHEME_GUI_THREADS
4511 else if (pp == &p_mzq) // 'mzquantum'
4512 mzvim_reset_timer();
4513#endif
4514#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
4515 else if (pp == &p_pyx) // 'pyxversion'
4516 errmsg = did_set_pyxversion(errmsg);
4517#endif
4518 else if (pp == &p_ul) // global 'undolevels'
4519 did_set_global_undolevels(value, old_value);
4520 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4521 did_set_buflocal_undolevels(value, old_value);
4522#ifdef FEAT_LINEBREAK
4523 else if (pp == &curwin->w_p_nuw) // 'numberwidth'
4524 errmsg = did_set_numberwidth(errmsg);
4525#endif
4526 else if (pp == &curbuf->b_p_tw) // 'textwidth'
4527 errmsg = did_set_textwidth(errmsg);
4528
4529 return errmsg;
4530}
4531
4532/*
4533 * Check the bounds of numeric options.
4534 */
4535 static char *
4536check_num_option_bounds(
4537 long *pp,
4538 long old_value,
4539 long old_Rows,
4540 long old_Columns,
4541 char *errbuf,
4542 size_t errbuflen,
4543 char *errmsg)
4544{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (Rows < min_rows() && full_screen)
4546 {
4547 if (errbuf != NULL)
4548 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004549 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004550 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 errmsg = errbuf;
4552 }
4553 Rows = min_rows();
4554 }
4555 if (Columns < MIN_COLUMNS && full_screen)
4556 {
4557 if (errbuf != NULL)
4558 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004559 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004560 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 errmsg = errbuf;
4562 }
4563 Columns = MIN_COLUMNS;
4564 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004565 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /*
4568 * If the screen (shell) height has been changed, assume it is the
4569 * physical screenheight.
4570 */
4571 if (old_Rows != Rows || old_Columns != Columns)
4572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004573 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (updating_screen)
4575 *pp = old_value;
4576 else if (full_screen
4577#ifdef FEAT_GUI
4578 && !gui.starting
4579#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004580 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 set_shellsize((int)Columns, (int)Rows, TRUE);
4582 else
4583 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004584 // Postpone the resizing; check the size and cmdline position for
4585 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 check_shellsize();
4587 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4588 cmdline_row = Rows - p_ch;
4589 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004590 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004591 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (curbuf->b_p_ts <= 0)
4595 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004596 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 curbuf->b_p_ts = 8;
4598 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004599 else if (curbuf->b_p_ts > TABSTOP_MAX)
4600 {
4601 errmsg = e_invalid_argument;
4602 curbuf->b_p_ts = 8;
4603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (p_tm < 0)
4605 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004606 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 p_tm = 0;
4608 }
4609 if ((curwin->w_p_scr <= 0
4610 || (curwin->w_p_scr > curwin->w_height
4611 && curwin->w_height > 0))
4612 && full_screen)
4613 {
4614 if (pp == &(curwin->w_p_scr))
4615 {
4616 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004617 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 win_comp_scroll(curwin);
4619 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004620 // If 'scroll' became invalid because of a side effect silently adjust
4621 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 else if (curwin->w_p_scr <= 0)
4623 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004624 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 curwin->w_p_scr = curwin->w_height;
4626 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004627 if (p_hi < 0)
4628 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004629 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004630 p_hi = 0;
4631 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004632 else if (p_hi > 10000)
4633 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004634 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004635 p_hi = 10000;
4636 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004637 if (p_re < 0 || p_re > 2)
4638 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004639 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004640 p_re = 0;
4641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (p_report < 0)
4643 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004644 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 p_report = 1;
4646 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004647 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004649 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 p_sj = Rows / 2;
4651 else
4652 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004653 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 p_sj = 1;
4655 }
4656 }
4657 if (p_so < 0 && full_screen)
4658 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004659 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 p_so = 0;
4661 }
4662 if (p_siso < 0 && full_screen)
4663 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004664 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 p_siso = 0;
4666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (p_cwh < 1)
4668 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004669 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 p_cwh = 1;
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (p_ut < 0)
4673 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004674 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 p_ut = 2000;
4676 }
4677 if (p_ss < 0)
4678 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004679 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 p_ss = 0;
4681 }
4682
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004683 return errmsg;
4684}
4685
4686/*
4687 * Set the value of a number option, and take care of side effects.
4688 * Returns NULL for success, or an error message for an error.
4689 */
4690 static char *
4691set_num_option(
4692 int opt_idx, // index in options[] table
4693 char_u *varp, // pointer to the option variable
4694 long value, // new value
4695 char *errbuf, // buffer for error messages
4696 size_t errbuflen, // length of "errbuf"
4697 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4698 // OPT_MODELINE, etc.
4699{
4700 char *errmsg = NULL;
4701 long old_value = *(long *)varp;
4702#if defined(FEAT_EVAL)
4703 long old_global_value = 0; // only used when setting a local and
4704 // global option
4705#endif
4706 long old_Rows = Rows; // remember old Rows
4707 long old_Columns = Columns; // remember old Columns
4708 long *pp = (long *)varp;
4709
4710 // Disallow changing some options from secure mode.
4711 if ((secure
4712#ifdef HAVE_SANDBOX
4713 || sandbox != 0
4714#endif
4715 ) && (options[opt_idx].flags & P_SECURE))
4716 return e_not_allowed_here;
4717
4718#if defined(FEAT_EVAL)
4719 // Save the global value before changing anything. This is needed as for
4720 // a global-only option setting the "local value" in fact sets the global
4721 // value (since there is only one value).
4722 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4723 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4724 OPT_GLOBAL);
4725#endif
4726
4727 *pp = value;
4728#ifdef FEAT_EVAL
4729 // Remember where the option was set.
4730 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4731#endif
4732#ifdef FEAT_GUI
4733 need_mouse_correct = TRUE;
4734#endif
4735
4736 if (curbuf->b_p_sw < 0)
4737 {
4738 errmsg = e_argument_must_be_positive;
4739#ifdef FEAT_VARTABS
4740 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4741 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4742 ? tabstop_first(curbuf->b_p_vts_array)
4743 : curbuf->b_p_ts;
4744#else
4745 curbuf->b_p_sw = curbuf->b_p_ts;
4746#endif
4747 }
4748
4749 /*
4750 * Number options that need some action when changed
4751 */
4752 errmsg = did_set_num_option(pp, value, old_value, errmsg);
4753
4754 /*
4755 * Check the bounds for numeric options here
4756 */
4757 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4758 errbuf, errbuflen, errmsg);
4759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004760 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4762 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4763
4764 options[opt_idx].flags |= P_WAS_SET;
4765
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004766#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004767 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4768 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004769#endif
4770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004771 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004772 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004773 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004774 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004775 if ((opt_flags & OPT_NO_REDRAW) == 0)
4776 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
4778 return errmsg;
4779}
4780
4781/*
4782 * Called after an option changed: check if something needs to be redrawn.
4783 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004785check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004788 int doclear = (flags & P_RCLR) == P_RCLR;
4789 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004791 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4795 changed_window_setting();
4796 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004797 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004798 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004799 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004800 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004801 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004803 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804}
4805
4806/*
4807 * Find index for option 'arg'.
4808 * Return -1 if not found.
4809 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004810 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004811findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 int opt_idx;
4814 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004815 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 int is_term_opt;
4817
4818 /*
4819 * For first call: Initialize the quick-access table.
4820 * It contains the index for the first option that starts with a certain
4821 * letter. There are 26 letters, plus the first "t_" option.
4822 */
4823 if (quick_tab[1] == 0)
4824 {
4825 p = options[0].fullname;
4826 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4827 {
4828 if (s[0] != p[0])
4829 {
4830 if (s[0] == 't' && s[1] == '_')
4831 quick_tab[26] = opt_idx;
4832 else
4833 quick_tab[CharOrdLow(s[0])] = opt_idx;
4834 }
4835 p = s;
4836 }
4837 }
4838
4839 /*
4840 * Check for name starting with an illegal character.
4841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 return -1;
4844
4845 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4846 if (is_term_opt)
4847 opt_idx = quick_tab[26];
4848 else
4849 opt_idx = quick_tab[CharOrdLow(arg[0])];
4850 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004852 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 break;
4854 }
4855 if (s == NULL && !is_term_opt)
4856 {
4857 opt_idx = quick_tab[CharOrdLow(arg[0])];
4858 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4859 {
4860 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 break;
4863 s = NULL;
4864 }
4865 }
4866 if (s == NULL)
4867 opt_idx = -1;
4868 return opt_idx;
4869}
4870
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004871#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4872 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873/*
4874 * Get the value for an option.
4875 *
4876 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004877 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004878 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004879 * String option: gov_string, *stringval gets allocated string.
4880 * Hidden Number option: gov_hidden_number.
4881 * Hidden Toggle option: gov_hidden_bool.
4882 * Hidden String option: gov_hidden_string.
4883 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004884 *
4885 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004887 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004888get_option_value(
4889 char_u *name,
4890 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004891 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004892 int *flagsp,
4893 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 int opt_idx;
4896 char_u *varp;
4897
4898 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004899 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004900 {
4901 int key;
4902
4903 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004904 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004905 {
4906 char_u key_name[2];
4907 char_u *p;
4908
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004909 if (flagsp != NULL)
4910 *flagsp = 0; // terminal option has no flags
4911
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004912 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004913 if (key < 0)
4914 {
4915 key_name[0] = KEY2TERMCAP0(key);
4916 key_name[1] = KEY2TERMCAP1(key);
4917 }
4918 else
4919 {
4920 key_name[0] = KS_KEY;
4921 key_name[1] = (key & 0xff);
4922 }
4923 p = find_termcode(key_name);
4924 if (p != NULL)
4925 {
4926 if (stringval != NULL)
4927 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004928 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004929 }
4930 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004931 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004934 varp = get_varp_scope(&(options[opt_idx]), scope);
4935
4936 if (flagsp != NULL)
4937 // Return the P_xxxx option flags.
4938 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 if (options[opt_idx].flags & P_STRING)
4941 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004942 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004943 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (stringval != NULL)
4945 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004946 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004947 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4948 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004950 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004951 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004952 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004955 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 *stringval = vim_strsave(*(char_u **)(varp));
4957 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004958 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004962 return (options[opt_idx].flags & P_NUM)
4963 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (options[opt_idx].flags & P_NUM)
4965 *numval = *(long *)varp;
4966 else
4967 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004968 // Special case: 'modified' is b_changed, but we also want to consider
4969 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if ((int *)varp == &curbuf->b_changed)
4971 *numval = curbufIsChanged();
4972 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004973 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004975 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976}
4977#endif
4978
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004979#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004980/*
4981 * Returns the option attributes and its value. Unlike the above function it
4982 * will return either global value or local value of the option depending on
4983 * what was requested, but it will never return global value if it was
4984 * requested to return local one and vice versa. Neither it will return
4985 * buffer-local value if it was requested to return window-local one.
4986 *
4987 * Pretends that option is absent if it is not present in the requested scope
4988 * (i.e. has no global, window-local or buffer-local value depending on
4989 * opt_type). Uses
4990 *
4991 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004992 * 0 hidden or unknown option, also option that does not have requested
4993 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004994 * see SOPT_* in vim.h for other flags
4995 *
4996 * Possible opt_type values: see SREQ_* in vim.h
4997 */
4998 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004999get_option_value_strict(
5000 char_u *name,
5001 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005002 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005003 int opt_type,
5004 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005005{
5006 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005007 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005008 struct vimoption *p;
5009 int r = 0;
5010
5011 opt_idx = findoption(name);
5012 if (opt_idx < 0)
5013 return 0;
5014
5015 p = &(options[opt_idx]);
5016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005017 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005018 if (p->var == NULL)
5019 return 0;
5020
5021 if (p->flags & P_BOOL)
5022 r |= SOPT_BOOL;
5023 else if (p->flags & P_NUM)
5024 r |= SOPT_NUM;
5025 else if (p->flags & P_STRING)
5026 r |= SOPT_STRING;
5027
5028 if (p->indir == PV_NONE)
5029 {
5030 if (opt_type == SREQ_GLOBAL)
5031 r |= SOPT_GLOBAL;
5032 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005033 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005034 }
5035 else
5036 {
5037 if (p->indir & PV_BOTH)
5038 r |= SOPT_GLOBAL;
5039 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005040 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005041
5042 if (p->indir & PV_WIN)
5043 {
5044 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005045 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005046 else
5047 r |= SOPT_WIN;
5048 }
5049 else if (p->indir & PV_BUF)
5050 {
5051 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005052 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005053 else
5054 r |= SOPT_BUF;
5055 }
5056 }
5057
5058 if (stringval == NULL)
5059 return r;
5060
5061 if (opt_type == SREQ_GLOBAL)
5062 varp = p->var;
5063 else
5064 {
5065 if (opt_type == SREQ_BUF)
5066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005067 // Special case: 'modified' is b_changed, but we also want to
5068 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005069 if (p->indir == PV_MOD)
5070 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005071 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005072 varp = NULL;
5073 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005074# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005075 else if (p->indir == PV_KEY)
5076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005077 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005078 *stringval = NULL;
5079 varp = NULL;
5080 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005081# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005082 else
5083 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005084 buf_T *save_curbuf = curbuf;
5085
5086 // only getting a pointer, no need to use aucmd_prepbuf()
5087 curbuf = (buf_T *)from;
5088 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005089 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005090 curbuf = save_curbuf;
5091 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005092 }
5093 }
5094 else if (opt_type == SREQ_WIN)
5095 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005096 win_T *save_curwin = curwin;
5097
5098 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005099 curbuf = curwin->w_buffer;
5100 varp = get_varp(p);
5101 curwin = save_curwin;
5102 curbuf = curwin->w_buffer;
5103 }
5104 if (varp == p->var)
5105 return (r | SOPT_UNSET);
5106 }
5107
5108 if (varp != NULL)
5109 {
5110 if (p->flags & P_STRING)
5111 *stringval = vim_strsave(*(char_u **)(varp));
5112 else if (p->flags & P_NUM)
5113 *numval = *(long *) varp;
5114 else
5115 *numval = *(int *)varp;
5116 }
5117
5118 return r;
5119}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005120
5121/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005122 * Iterate over options. First argument is a pointer to a pointer to a
5123 * structure inside options[] array, second is option type like in the above
5124 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005125 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005126 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005127 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005128 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005129 * first argument to NULL.
5130 *
5131 * Returns full option name for current option on each call.
5132 */
5133 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005134option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005135{
5136 struct vimoption *ret = NULL;
5137 do
5138 {
5139 if (*option == NULL)
5140 *option = (void *) options;
5141 else if (((struct vimoption *) (*option))->fullname == NULL)
5142 {
5143 *option = NULL;
5144 return NULL;
5145 }
5146 else
5147 *option = (void *) (((struct vimoption *) (*option)) + 1);
5148
5149 ret = ((struct vimoption *) (*option));
5150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005151 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005152 if (ret->var == NULL)
5153 {
5154 ret = NULL;
5155 continue;
5156 }
5157
5158 switch (opt_type)
5159 {
5160 case SREQ_GLOBAL:
5161 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5162 ret = NULL;
5163 break;
5164 case SREQ_BUF:
5165 if (!(ret->indir & PV_BUF))
5166 ret = NULL;
5167 break;
5168 case SREQ_WIN:
5169 if (!(ret->indir & PV_WIN))
5170 ret = NULL;
5171 break;
5172 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005173 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005174 return NULL;
5175 }
5176 }
5177 while (ret == NULL);
5178
5179 return (char_u *)ret->fullname;
5180}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005181#endif
5182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005184 * Return the flags for the option at 'opt_idx'.
5185 */
5186 long_u
5187get_option_flags(int opt_idx)
5188{
5189 return options[opt_idx].flags;
5190}
5191
5192/*
5193 * Set a flag for the option at 'opt_idx'.
5194 */
5195 void
5196set_option_flag(int opt_idx, long_u flag)
5197{
5198 options[opt_idx].flags |= flag;
5199}
5200
5201/*
5202 * Clear a flag for the option at 'opt_idx'.
5203 */
5204 void
5205clear_option_flag(int opt_idx, long_u flag)
5206{
5207 options[opt_idx].flags &= ~flag;
5208}
5209
5210/*
5211 * Returns TRUE if the option at 'opt_idx' is a global option
5212 */
5213 int
5214is_global_option(int opt_idx)
5215{
5216 return options[opt_idx].indir == PV_NONE;
5217}
5218
5219/*
5220 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5221 * local value.
5222 */
5223 int
5224is_global_local_option(int opt_idx)
5225{
5226 return options[opt_idx].indir & PV_BOTH;
5227}
5228
5229/*
5230 * Returns TRUE if the option at 'opt_idx' is a window-local option
5231 */
5232 int
5233is_window_local_option(int opt_idx)
5234{
5235 return options[opt_idx].var == VAR_WIN;
5236}
5237
5238/*
5239 * Returns TRUE if the option at 'opt_idx' is a hidden option
5240 */
5241 int
5242is_hidden_option(int opt_idx)
5243{
5244 return options[opt_idx].var == NULL;
5245}
5246
5247#if defined(FEAT_CRYPT) || defined(PROTO)
5248/*
5249 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5250 */
5251 int
5252is_crypt_key_option(int opt_idx)
5253{
5254 return options[opt_idx].indir == PV_KEY;
5255}
5256#endif
5257
5258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 * Set the value of option "name".
5260 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005261 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005262 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005264 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005265set_option_value(
5266 char_u *name,
5267 long number,
5268 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005269 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270{
5271 int opt_idx;
5272 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005273 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005274 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005277 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005278 {
5279 int key;
5280
5281 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005282 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005283 {
5284 char_u key_name[2];
5285
5286 if (key < 0)
5287 {
5288 key_name[0] = KEY2TERMCAP0(key);
5289 key_name[1] = KEY2TERMCAP1(key);
5290 }
5291 else
5292 {
5293 key_name[0] = KS_KEY;
5294 key_name[1] = (key & 0xff);
5295 }
5296 add_termcode(key_name, string, FALSE);
5297 if (full_screen)
5298 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005299 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005300 return NULL;
5301 }
5302
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005303 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 else
5306 {
5307 flags = options[opt_idx].flags;
5308#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005309 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005311 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005312 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005313 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005316 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005317 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005318
5319 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5320 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005322 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005324 int idx;
5325
5326 // Either we are given a string or we are setting option
5327 // to zero.
5328 for (idx = 0; string[idx] == '0'; ++idx)
5329 ;
5330 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005331 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005332 // There's another character after zeros or the string
5333 // is empty. In both cases, we are trying to set a
5334 // num option using a string.
5335 semsg(_(e_number_required_after_str_equal_str),
5336 name, string);
5337 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005338
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005341 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005342 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005343 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005344 errbuf, sizeof(errbuf), opt_flags);
5345 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005346 else
5347 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005351 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352}
5353
5354/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005355 * Call set_option_value() and when an error is returned report it.
5356 */
5357 void
5358set_option_value_give_err(
5359 char_u *name,
5360 long number,
5361 char_u *string,
5362 int opt_flags) // OPT_LOCAL or 0 (both)
5363{
5364 char *errmsg = set_option_value(name, number, string, opt_flags);
5365
5366 if (errmsg != NULL)
5367 emsg(_(errmsg));
5368}
5369
5370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * Get the terminal code for a terminal option.
5372 * Returns NULL when not found.
5373 */
5374 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005375get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
5377 int opt_idx;
5378 char_u *varp;
5379
5380 if (tname[0] != 't' || tname[1] != '_' ||
5381 tname[2] == NUL || tname[3] == NUL)
5382 return NULL;
5383 if ((opt_idx = findoption(tname)) >= 0)
5384 {
5385 varp = get_varp(&(options[opt_idx]));
5386 if (varp != NULL)
5387 varp = *(char_u **)(varp);
5388 return varp;
5389 }
5390 return find_termcode(tname + 2);
5391}
5392
5393 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005394get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 int i;
5397
5398 i = findoption((char_u *)"hl");
5399 if (i >= 0)
5400 return options[i].def_val[VI_DEFAULT];
5401 return (char_u *)NULL;
5402}
5403
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005404 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005405get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005406{
5407 int i;
5408
5409 i = findoption((char_u *)"enc");
5410 if (i >= 0)
5411 return options[i].def_val[VI_DEFAULT];
5412 return (char_u *)NULL;
5413}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005414
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005415#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005416 int
5417is_option_allocated(char *name)
5418{
5419 int idx = findoption((char_u *)name);
5420
5421 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5422}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005423#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005424
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005425#if defined(FEAT_EVAL) || defined(PROTO)
5426/*
5427 * Return TRUE if "name" is a string option.
5428 * Returns FALSE if option "name" does not exist.
5429 */
5430 int
5431is_string_option(char_u *name)
5432{
5433 int idx = findoption(name);
5434
5435 return idx >= 0 && (options[idx].flags & P_STRING);
5436}
5437#endif
5438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439/*
5440 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005441 * When "has_lt" is true there is a '<' before "*arg_arg".
5442 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 */
5444 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005445find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005447 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005449 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451 /*
5452 * Don't use get_special_key_code() for t_xx, we don't want it to call
5453 * add_termcap_entry().
5454 */
5455 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5456 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005457 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005461 key = find_special_key(&arg, &modifiers,
5462 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005463 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 key = 0;
5465 }
5466 return key;
5467}
5468
5469/*
5470 * if 'all' == 0: show changed options
5471 * if 'all' == 1: show all normal options
5472 * if 'all' == 2: show all terminal options
5473 */
5474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005475showoptions(
5476 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005477 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479 struct vimoption *p;
5480 int col;
5481 int isterm;
5482 char_u *varp;
5483 struct vimoption **items;
5484 int item_count;
5485 int run;
5486 int row, rows;
5487 int cols;
5488 int i;
5489 int len;
5490
5491#define INC 20
5492#define GAP 3
5493
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005494 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 if (items == NULL)
5496 return;
5497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005498 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005500 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005502 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005504 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005506 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005509 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 * 1. display the short items
5511 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005512 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 */
5514 for (run = 1; run <= 2 && !got_int; ++run)
5515 {
5516 /*
5517 * collect the items in items[]
5518 */
5519 item_count = 0;
5520 for (p = &options[0]; p->fullname != NULL; p++)
5521 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005522 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005523 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005524 continue;
5525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 varp = NULL;
5527 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005528 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 {
5530 if (p->indir != PV_NONE && !isterm)
5531 varp = get_varp_scope(p, opt_flags);
5532 }
5533 else
5534 varp = get_varp(p);
5535 if (varp != NULL
5536 && ((all == 2 && isterm)
5537 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005538 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005540 if (opt_flags & OPT_ONECOLUMN)
5541 len = Columns;
5542 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005543 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 else
5545 {
5546 option_value2string(p, opt_flags);
5547 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5548 }
5549 if ((len <= INC - GAP && run == 1) ||
5550 (len > INC - GAP && run == 2))
5551 items[item_count++] = p;
5552 }
5553 }
5554
5555 /*
5556 * display the items
5557 */
5558 if (run == 1)
5559 {
5560 cols = (Columns + GAP - 3) / INC;
5561 if (cols == 0)
5562 cols = 1;
5563 rows = (item_count + cols - 1) / cols;
5564 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005565 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 rows = item_count;
5567 for (row = 0; row < rows && !got_int; ++row)
5568 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005569 msg_putchar('\n'); // go to next line
5570 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 break;
5572 col = 0;
5573 for (i = row; i < item_count; i += rows)
5574 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 showoneopt(items[i], opt_flags);
5577 col += INC;
5578 }
5579 out_flush();
5580 ui_breakcheck();
5581 }
5582 }
5583 vim_free(items);
5584}
5585
5586/*
5587 * Return TRUE if option "p" has its default value.
5588 */
5589 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005590optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 int dvi;
5593
5594 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005595 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005596 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005598 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005600 // the cast to long is required for Manx C, long_i is
5601 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005602 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5605}
5606
5607/*
5608 * showoneopt: show the value of one option
5609 * must not be called with a hidden option!
5610 */
5611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005612showoneopt(
5613 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005616 char_u *varp;
5617 int save_silent = silent_mode;
5618
5619 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005620 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622 varp = get_varp_scope(p, opt_flags);
5623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005624 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5626 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005627 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005629 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005631 msg_puts(" ");
5632 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if (!(p->flags & P_BOOL))
5634 {
5635 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005636 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 option_value2string(p, opt_flags);
5638 msg_outtrans(NameBuff);
5639 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005640
5641 silent_mode = save_silent;
5642 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643}
5644
5645/*
5646 * Write modified options as ":set" commands to a file.
5647 *
5648 * There are three values for "opt_flags":
5649 * OPT_GLOBAL: Write global option values and fresh values of
5650 * buffer-local options (used for start of a session
5651 * file).
5652 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5653 * curwin (used for a vimrc file).
5654 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5655 * and local values for window-local options of
5656 * curwin. Local values are also written when at the
5657 * default value, because a modeline or autocommand
5658 * may have set them when doing ":edit file" and the
5659 * user has set them back at the default or fresh
5660 * value.
5661 * When "local_only" is TRUE, don't write fresh
5662 * values, only local values (for ":mkview").
5663 * (fresh value = value used for a new buffer or window for a local option).
5664 *
5665 * Return FAIL on error, OK otherwise.
5666 */
5667 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005668makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005671 char_u *varp; // currently used value
5672 char_u *varp_fresh; // local value
5673 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 char *cmd;
5675 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005676 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 /*
5679 * The options that don't have a default (terminal name, columns, lines)
5680 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005681 * Do the loop over "options[]" twice: once for options with the
5682 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005684 for (pri = 1; pri >= 0; --pri)
5685 {
5686 for (p = &options[0]; !istermoption(p); p++)
5687 if (!(p->flags & P_NO_MKRC)
5688 && !istermoption(p)
5689 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005691 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5693 continue;
5694
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005695 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5696 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5698 continue;
5699
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005700 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005702 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 continue;
5704
Bram Moolenaard23b7142021-04-17 21:04:34 +02005705 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5706 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005707 continue;
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 round = 2;
5710 if (p->indir != PV_NONE)
5711 {
5712 if (p->var == VAR_WIN)
5713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005714 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (!(opt_flags & OPT_LOCAL))
5716 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 // When fresh value of window-local option is not at the
5718 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5720 {
5721 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005722 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
5724 round = 1;
5725 varp_local = varp;
5726 varp = varp_fresh;
5727 }
5728 }
5729 }
5730 }
5731
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005732 // Round 1: fresh value for window-local options.
5733 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 for ( ; round <= 2; varp = varp_local, ++round)
5735 {
5736 if (round == 1 || (opt_flags & OPT_GLOBAL))
5737 cmd = "set";
5738 else
5739 cmd = "setlocal";
5740
5741 if (p->flags & P_BOOL)
5742 {
5743 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5744 return FAIL;
5745 }
5746 else if (p->flags & P_NUM)
5747 {
5748 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5749 return FAIL;
5750 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005751 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005753 int do_endif = FALSE;
5754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 // Don't set 'syntax' and 'filetype' again if the value is
5756 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005757 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005758#if defined(FEAT_SYN_HL)
5759 p->indir == PV_SYN ||
5760#endif
5761 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5764 *(char_u **)(varp)) < 0
5765 || put_eol(fd) < 0)
5766 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005767 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005770 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005772 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 if (put_line(fd, "endif") == FAIL)
5775 return FAIL;
5776 }
5777 }
5778 }
5779 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 return OK;
5782}
5783
5784#if defined(FEAT_FOLDING) || defined(PROTO)
5785/*
5786 * Generate set commands for the local fold options only. Used when
5787 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5788 */
5789 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005790makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005792 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005794 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 == FAIL
5796# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005797 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005799 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 == FAIL
5801 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5802 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5803 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5804 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5805 )
5806 return FAIL;
5807
5808 return OK;
5809}
5810#endif
5811
5812 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005813put_setstring(
5814 FILE *fd,
5815 char *cmd,
5816 char *name,
5817 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005818 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005821 char_u *buf = NULL;
5822 char_u *part = NULL;
5823 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5826 return FAIL;
5827 if (*valuep != NULL)
5828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005829 // Output 'pastetoggle' as key names. For other
5830 // options some characters have to be escaped with
5831 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 if (valuep == &p_pt)
5833 {
5834 s = *valuep;
5835 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005836 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 return FAIL;
5838 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005839 // expand the option value, replace $HOME by ~
5840 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005842 int size = (int)STRLEN(*valuep) + 1;
5843
5844 // replace home directory in the whole option value into "buf"
5845 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005846 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005847 goto fail;
5848 home_replace(NULL, *valuep, buf, size, FALSE);
5849
5850 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005851 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005852 // can be expanded when read back.
5853 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5854 && vim_strchr(*valuep, ',') != NULL)
5855 {
5856 part = alloc(size);
5857 if (part == NULL)
5858 goto fail;
5859
5860 // write line break to clear the option, e.g. ':set rtp='
5861 if (put_eol(fd) == FAIL)
5862 goto fail;
5863
5864 p = buf;
5865 while (*p != NUL)
5866 {
5867 // for each comma separated option part, append value to
5868 // the option, :set rtp+=value
5869 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5870 goto fail;
5871 (void)copy_option_part(&p, part, size, ",");
5872 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5873 goto fail;
5874 }
5875 vim_free(buf);
5876 vim_free(part);
5877 return OK;
5878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005880 {
5881 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005883 }
5884 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
5886 else if (put_escstr(fd, *valuep, 2) == FAIL)
5887 return FAIL;
5888 }
5889 if (put_eol(fd) < 0)
5890 return FAIL;
5891 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005892fail:
5893 vim_free(buf);
5894 vim_free(part);
5895 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896}
5897
5898 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005899put_setnum(
5900 FILE *fd,
5901 char *cmd,
5902 char *name,
5903 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 long wc;
5906
5907 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5908 return FAIL;
5909 if (wc_use_keyname((char_u *)valuep, &wc))
5910 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005911 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5913 return FAIL;
5914 }
5915 else if (fprintf(fd, "%ld", *valuep) < 0)
5916 return FAIL;
5917 if (put_eol(fd) < 0)
5918 return FAIL;
5919 return OK;
5920}
5921
5922 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005923put_setbool(
5924 FILE *fd,
5925 char *cmd,
5926 char *name,
5927 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005929 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005930 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5932 || put_eol(fd) < 0)
5933 return FAIL;
5934 return OK;
5935}
5936
5937/*
5938 * Clear all the terminal options.
5939 * If the option has been allocated, free the memory.
5940 * Terminal options are never hidden or indirect.
5941 */
5942 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005943clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 /*
5946 * Reset a few things before clearing the old options. This may cause
5947 * outputting a few things that the terminal doesn't understand, but the
5948 * screen will be cleared later, so this is OK.
5949 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005950 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005951 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005953 // When starting the GUI close the display opened for the clipboard.
5954 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (gui.starting)
5956 clear_xterm_clip();
5957#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005958 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005960 free_termoptions();
5961}
5962
5963 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005964free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005965{
5966 struct vimoption *p;
5967
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005968 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 if (istermoption(p))
5970 {
5971 if (p->flags & P_ALLOCED)
5972 free_string_option(*(char_u **)(p->var));
5973 if (p->flags & P_DEF_ALLOCED)
5974 free_string_option(p->def_val[VI_DEFAULT]);
5975 *(char_u **)(p->var) = empty_option;
5976 p->def_val[VI_DEFAULT] = empty_option;
5977 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005978#ifdef FEAT_EVAL
5979 // remember where the option was cleared
5980 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983 clear_termcodes();
5984}
5985
5986/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005987 * Free the string for one term option, if it was allocated.
5988 * Set the string to empty_option and clear allocated flag.
5989 * "var" points to the option value.
5990 */
5991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005992free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005993{
5994 struct vimoption *p;
5995
5996 for (p = &options[0]; p->fullname != NULL; p++)
5997 if (p->var == var)
5998 {
5999 if (p->flags & P_ALLOCED)
6000 free_string_option(*(char_u **)(p->var));
6001 *(char_u **)(p->var) = empty_option;
6002 p->flags &= ~P_ALLOCED;
6003 break;
6004 }
6005}
6006
6007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 * Set the terminal option defaults to the current value.
6009 * Used after setting the terminal name.
6010 */
6011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006012set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013{
6014 struct vimoption *p;
6015
6016 for (p = &options[0]; p->fullname != NULL; p++)
6017 {
6018 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6019 {
6020 if (p->flags & P_DEF_ALLOCED)
6021 {
6022 free_string_option(p->def_val[VI_DEFAULT]);
6023 p->flags &= ~P_DEF_ALLOCED;
6024 }
6025 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6026 if (p->flags & P_ALLOCED)
6027 {
6028 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006029 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
6031 }
6032 }
6033}
6034
6035/*
6036 * return TRUE if 'p' starts with 't_'
6037 */
6038 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006039istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6042}
6043
Bram Moolenaardac13472019-09-16 21:06:21 +02006044/*
6045 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6046 */
6047 int
6048istermoption_idx(int opt_idx)
6049{
6050 return istermoption(&options[opt_idx]);
6051}
6052
Bram Moolenaar113e1072019-01-20 15:30:40 +01006053#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006055 * Unset local option value, similar to ":set opt<".
6056 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006058unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006059{
6060 struct vimoption *p;
6061 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006062 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006063
6064 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006065 if (opt_idx < 0)
6066 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006067 p = &(options[opt_idx]);
6068
6069 switch ((int)p->indir)
6070 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006071 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006072 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006073 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006074 break;
6075 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006076 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006077 break;
6078 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006079 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006080 break;
6081 case PV_AR:
6082 buf->b_p_ar = -1;
6083 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006084 case PV_BKC:
6085 clear_string_option(&buf->b_p_bkc);
6086 buf->b_bkc_flags = 0;
6087 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006088 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006089 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006090 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006091 case PV_TC:
6092 clear_string_option(&buf->b_p_tc);
6093 buf->b_tc_flags = 0;
6094 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006095 case PV_SISO:
6096 curwin->w_p_siso = -1;
6097 break;
6098 case PV_SO:
6099 curwin->w_p_so = -1;
6100 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006101# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006102 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006103 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006104 break;
6105 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006106 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006107 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006108# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006109 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006110 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006111 break;
6112 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006113 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006114 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006115# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006116 case PV_TSRFU:
6117 clear_string_option(&buf->b_p_tsrfu);
6118 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006119# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006120 case PV_FP:
6121 clear_string_option(&buf->b_p_fp);
6122 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006123# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006124 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006125 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006126 break;
6127 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006128 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006129 break;
6130 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006131 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006132 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006133# endif
6134# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006135 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006136 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006137 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006138# endif
6139# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006140 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006141 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006142 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006143# endif
6144# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006145 case PV_SBR:
6146 clear_string_option(&((win_T *)from)->w_p_sbr);
6147 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006148# endif
6149# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006150 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006151 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006152 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006153# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006154 case PV_UL:
6155 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6156 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006157 case PV_LW:
6158 clear_string_option(&buf->b_p_lw);
6159 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006160 case PV_MENC:
6161 clear_string_option(&buf->b_p_menc);
6162 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006163 case PV_LCS:
6164 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006165 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006166 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006167 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006168 case PV_FCS:
6169 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006170 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006171 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006172 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006173 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006174 clear_string_option(&((win_T *)from)->w_p_ve);
6175 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006176 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006177 }
6178}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006179#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006180
6181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006183 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 */
6185 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006186get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006188 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 {
6190 if (p->var == VAR_WIN)
6191 return (char_u *)GLOBAL_WO(get_varp(p));
6192 return p->var;
6193 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006194 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
6196 switch ((int)p->indir)
6197 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006198 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006200 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6201 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6202 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006204 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6205 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6206 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006207 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006208 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006209 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006210 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6211 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006213 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6214 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006216 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6217 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006218#ifdef FEAT_COMPL_FUNC
6219 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6220#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006221#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6222 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6223#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006224#if defined(FEAT_CRYPT)
6225 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6226#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006227#ifdef FEAT_LINEBREAK
6228 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6229#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006230#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006231 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006232#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006233 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006234 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006235 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006236 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006237 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006238 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006239 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006242 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244 return get_varp(p);
6245}
6246
6247/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006248 * Get pointer to option variable at 'opt_idx', depending on local or global
6249 * scope.
6250 */
6251 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006252get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006253{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006254 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006255}
6256
6257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 * Get pointer to option variable.
6259 */
6260 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006261get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006263 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (p->var == NULL)
6265 return NULL;
6266
6267 switch ((int)p->indir)
6268 {
6269 case PV_NONE: return p->var;
6270
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006271 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006272 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006274 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006276 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006278 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006280 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006282 case PV_TC: return *curbuf->b_p_tc != NUL
6283 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006284 case PV_BKC: return *curbuf->b_p_bkc != NUL
6285 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006286 case PV_SISO: return curwin->w_p_siso >= 0
6287 ? (char_u *)&(curwin->w_p_siso) : p->var;
6288 case PV_SO: return curwin->w_p_so >= 0
6289 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006291 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006293 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6295#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006296 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006298 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006300#ifdef FEAT_COMPL_FUNC
6301 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6302 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6303#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006304 case PV_FP: return *curbuf->b_p_fp != NUL
6305 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006307 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006309 case PV_GP: return *curbuf->b_p_gp != NUL
6310 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6311 case PV_MP: return *curbuf->b_p_mp != NUL
6312 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006314#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6315 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6316 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6317#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006318#if defined(FEAT_CRYPT)
6319 case PV_CM: return *curbuf->b_p_cm != NUL
6320 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6321#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006322#ifdef FEAT_LINEBREAK
6323 case PV_SBR: return *curwin->w_p_sbr != NUL
6324 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6325#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006326#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006327 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006328 ? (char_u *)&(curwin->w_p_stl) : p->var;
6329#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006330 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6331 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006332 case PV_LW: return *curbuf->b_p_lw != NUL
6333 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006334 case PV_MENC: return *curbuf->b_p_menc != NUL
6335 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336#ifdef FEAT_ARABIC
6337 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6338#endif
6339 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006340 case PV_LCS: return *curwin->w_p_lcs != NUL
6341 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006342 case PV_FCS: return *curwin->w_p_fcs != NUL
6343 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006344 case PV_VE: return *curwin->w_p_ve != NUL
6345 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006346#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006347 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6348#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006349#ifdef FEAT_SYN_HL
6350 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6351 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006352 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006353 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355#ifdef FEAT_DIFF
6356 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6357#endif
6358#ifdef FEAT_FOLDING
6359 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6360 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6361 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6362 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6363 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6364 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6365 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6366# ifdef FEAT_EVAL
6367 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6368 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6369# endif
6370 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6371#endif
6372 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006373 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006374#ifdef FEAT_LINEBREAK
6375 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006378 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006379#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6381#endif
6382#ifdef FEAT_RIGHTLEFT
6383 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6384 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6385#endif
6386 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006387 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6389#ifdef FEAT_LINEBREAK
6390 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006391 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6392 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006394 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006396 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006397#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006398 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6399 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6400#endif
6401#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006402 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6403 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6404 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6408 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6411 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6413 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6415 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6416 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006417 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420#ifdef FEAT_FOLDING
6421 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006424#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006425 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006427#ifdef FEAT_COMPL_FUNC
6428 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006429 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006430#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006431#ifdef FEAT_EVAL
6432 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6433#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006434 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006436 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006442 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6444 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6445 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6446 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6447#ifdef FEAT_FIND_ID
6448# ifdef FEAT_EVAL
6449 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6450# endif
6451#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006452#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6454 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6455#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006456#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459#ifdef FEAT_CRYPT
6460 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006463 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6465 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6466 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6467 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6468 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006470 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6477#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006478 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006479 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6480#endif
6481#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006482 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6483 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6484 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006485 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486#endif
6487 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6488 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6489 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6490 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006491#ifdef FEAT_PERSISTENT_UNDO
6492 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6495#ifdef FEAT_KEYMAP
6496 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6497#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006498#ifdef FEAT_SIGNS
6499 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6500#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006501#ifdef FEAT_VARTABS
6502 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6503 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6504#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006505 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006507 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 return (char_u *)&(curbuf->b_p_wm);
6509}
6510
6511/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006512 * Return a pointer to the variable for option at 'opt_idx'
6513 */
6514 char_u *
6515get_option_var(int opt_idx)
6516{
6517 return options[opt_idx].var;
6518}
6519
Dominique Pelle748b3082022-01-08 12:41:16 +00006520#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006521/*
6522 * Return the full name of the option at 'opt_idx'
6523 */
6524 char_u *
6525get_option_fullname(int opt_idx)
6526{
6527 return (char_u *)options[opt_idx].fullname;
6528}
Dominique Pelle748b3082022-01-08 12:41:16 +00006529#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006530
6531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 * Get the value of 'equalprg', either the buffer-local one or the global one.
6533 */
6534 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006535get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 if (*curbuf->b_p_ep == NUL)
6538 return p_ep;
6539 return curbuf->b_p_ep;
6540}
6541
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542/*
6543 * Copy options from one window to another.
6544 * Used when splitting a window.
6545 */
6546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006547win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6550 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006551 after_copy_winopt(wp_to);
6552}
6553
6554/*
6555 * After copying window options: update variables depending on options.
6556 */
6557 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006558after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006559{
6560#ifdef FEAT_LINEBREAK
6561 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006562#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006563#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006564 fill_culopt_flags(NULL, wp);
6565 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006566#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006567 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6568 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006569}
6570
6571 static char_u *
6572copy_option_val(char_u *val)
6573{
6574 if (val == empty_option)
6575 return empty_option; // no need to allocate memory
6576 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
6579/*
6580 * Copy the options from one winopt_T to another.
6581 * Doesn't free the old option values in "to", use clear_winopt() for that.
6582 * The 'scroll' option is not copied, because it depends on the window height.
6583 * The 'previewwindow' option is reset, there can be only one preview window.
6584 */
6585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006586copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588#ifdef FEAT_ARABIC
6589 to->wo_arab = from->wo_arab;
6590#endif
6591 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006592 to->wo_lcs = copy_option_val(from->wo_lcs);
6593 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006595 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006596 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006597 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006598#ifdef FEAT_LINEBREAK
6599 to->wo_nuw = from->wo_nuw;
6600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601#ifdef FEAT_RIGHTLEFT
6602 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006603 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006605#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006606 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006607#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006608#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006609 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006612#ifdef FEAT_DIFF
6613 to->wo_wrap_save = from->wo_wrap_save;
6614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615#ifdef FEAT_LINEBREAK
6616 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006617 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006618 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006620 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006622 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006623 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006624 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006625 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006626#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006627 to->wo_spell = from->wo_spell;
6628#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006629#ifdef FEAT_SYN_HL
6630 to->wo_cuc = from->wo_cuc;
6631 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006632 to->wo_culopt = copy_option_val(from->wo_culopt);
6633 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635#ifdef FEAT_DIFF
6636 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006637 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006639#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006640 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006641 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006642#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006643#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006644 to->wo_twk = copy_option_val(from->wo_twk);
6645 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647#ifdef FEAT_FOLDING
6648 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006649 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006651 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006652 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 to->wo_fml = from->wo_fml;
6654 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006655 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006656 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006657 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006658 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 to->wo_fdn = from->wo_fdn;
6660# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006661 to->wo_fde = copy_option_val(from->wo_fde);
6662 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006664 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006666#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006667 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006668#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006669
6670#ifdef FEAT_EVAL
6671 // Copy the script context so that we know where the value was last set.
6672 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6673 sizeof(to->wo_script_ctx));
6674#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006675 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676}
6677
6678/*
6679 * Check string options in a window for a NULL value.
6680 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006682check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 check_winopt(&win->w_onebuf_opt);
6685 check_winopt(&win->w_allbuf_opt);
6686}
6687
6688/*
6689 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6690 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006692check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694#ifdef FEAT_FOLDING
6695 check_string_option(&wop->wo_fdi);
6696 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006697 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698# ifdef FEAT_EVAL
6699 check_string_option(&wop->wo_fde);
6700 check_string_option(&wop->wo_fdt);
6701# endif
6702 check_string_option(&wop->wo_fmr);
6703#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006704#ifdef FEAT_SIGNS
6705 check_string_option(&wop->wo_scl);
6706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707#ifdef FEAT_RIGHTLEFT
6708 check_string_option(&wop->wo_rlc);
6709#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006710#ifdef FEAT_LINEBREAK
6711 check_string_option(&wop->wo_sbr);
6712#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006713#ifdef FEAT_STL_OPT
6714 check_string_option(&wop->wo_stl);
6715#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006716#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006717 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006718 check_string_option(&wop->wo_cc);
6719#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006720#ifdef FEAT_CONCEAL
6721 check_string_option(&wop->wo_cocu);
6722#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006723#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006724 check_string_option(&wop->wo_twk);
6725 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006726#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006727#ifdef FEAT_LINEBREAK
6728 check_string_option(&wop->wo_briopt);
6729#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006730 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006731 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006732 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006733 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
6736/*
6737 * Free the allocated memory inside a winopt_T.
6738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006740clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742#ifdef FEAT_FOLDING
6743 clear_string_option(&wop->wo_fdi);
6744 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006745 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746# ifdef FEAT_EVAL
6747 clear_string_option(&wop->wo_fde);
6748 clear_string_option(&wop->wo_fdt);
6749# endif
6750 clear_string_option(&wop->wo_fmr);
6751#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006752#ifdef FEAT_SIGNS
6753 clear_string_option(&wop->wo_scl);
6754#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006755#ifdef FEAT_LINEBREAK
6756 clear_string_option(&wop->wo_briopt);
6757#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006758 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759#ifdef FEAT_RIGHTLEFT
6760 clear_string_option(&wop->wo_rlc);
6761#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006762#ifdef FEAT_LINEBREAK
6763 clear_string_option(&wop->wo_sbr);
6764#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006765#ifdef FEAT_STL_OPT
6766 clear_string_option(&wop->wo_stl);
6767#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006768#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006769 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006770 clear_string_option(&wop->wo_cc);
6771#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006772#ifdef FEAT_CONCEAL
6773 clear_string_option(&wop->wo_cocu);
6774#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006775#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006776 clear_string_option(&wop->wo_twk);
6777 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006778#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006779 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006780 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006781 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782}
6783
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006784#ifdef FEAT_EVAL
6785// Index into the options table for a buffer-local option enum.
6786static int buf_opt_idx[BV_COUNT];
6787# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6788
6789/*
6790 * Initialize buf_opt_idx[] if not done already.
6791 */
6792 static void
6793init_buf_opt_idx(void)
6794{
6795 static int did_init_buf_opt_idx = FALSE;
6796 int i;
6797
6798 if (did_init_buf_opt_idx)
6799 return;
6800 did_init_buf_opt_idx = TRUE;
6801 for (i = 0; !istermoption_idx(i); i++)
6802 if (options[i].indir & PV_BUF)
6803 buf_opt_idx[options[i].indir & PV_MASK] = i;
6804}
6805#else
6806# define COPY_OPT_SCTX(buf, bv)
6807#endif
6808
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809/*
6810 * Copy global option values to local options for one buffer.
6811 * Used when creating a new buffer and sometimes when entering a buffer.
6812 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006813 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6815 * appropriate.
6816 * BCO_NOHELP Don't copy the values to a help buffer.
6817 */
6818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006819buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006822 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 int dont_do_help;
6824 int did_isk = FALSE;
6825
6826 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 * Skip this when the option defaults have not been set yet. Happens when
6828 * main() allocates the first buffer.
6829 */
6830 if (p_cpo != NULL)
6831 {
6832 /*
6833 * Always copy when entering and 'cpo' contains 'S'.
6834 * Don't copy when already initialized.
6835 * Don't copy when 'cpo' contains 's' and not entering.
6836 * 'S' BCO_ENTER initialized 's' should_copy
6837 * yes yes X X TRUE
6838 * yes no yes X FALSE
6839 * no X yes X FALSE
6840 * X no no yes FALSE
6841 * X no no no TRUE
6842 * no yes no X TRUE
6843 */
6844 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6845 && (buf->b_p_initialized
6846 || (!(flags & BCO_ENTER)
6847 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6848 should_copy = FALSE;
6849
6850 if (should_copy || (flags & BCO_ALWAYS))
6851 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006852#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006853 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006854 init_buf_opt_idx();
6855#endif
6856 // Don't copy the options specific to a help buffer when
6857 // BCO_NOHELP is given or the options were initialized already
6858 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6860 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006861 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 save_p_isk = buf->b_p_isk;
6864 buf->b_p_isk = NULL;
6865 }
6866 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006867 * Always free the allocated strings. If not already initialized,
6868 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 */
6870 if (!buf->b_p_initialized)
6871 {
6872 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006873 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006876 switch (*p_ffs)
6877 {
6878 case 'm':
6879 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6880 case 'd':
6881 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6882 case 'u':
6883 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6884 default:
6885 buf->b_p_ff = vim_strsave(p_ff);
6886 }
6887 if (buf->b_p_ff != NULL)
6888 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 buf->b_p_bh = empty_option;
6890 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892 else
6893 free_buf_options(buf, FALSE);
6894
6895 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006896 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 buf->b_p_ai_nopaste = p_ai_nopaste;
6898 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006899 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006901 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 buf->b_p_tw_nopaste = p_tw_nopaste;
6903 buf->b_p_tw_nobin = p_tw_nobin;
6904 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006905 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 buf->b_p_wm_nopaste = p_wm_nopaste;
6907 buf->b_p_wm_nobin = p_wm_nobin;
6908 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006909 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006910 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006911 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006912 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006913 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006915 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006917 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006919 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 buf->b_p_ml_nobin = p_ml_nobin;
6921 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006922 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006923 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006924 buf->b_p_swf = FALSE;
6925 else
6926 {
6927 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006928 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006931 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006932#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006933 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006934 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006936#ifdef FEAT_COMPL_FUNC
6937 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006938 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006939 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006940 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006941 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006942 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006943#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006944#ifdef FEAT_EVAL
6945 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006946 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006947 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006950 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006952#ifdef FEAT_VARTABS
6953 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006954 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006955 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006956 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006957 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006958 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006959 buf->b_p_vsts_nopaste = p_vsts_nopaste
6960 ? vim_strsave(p_vsts_nopaste) : NULL;
6961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006963 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006965 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966#ifdef FEAT_FOLDING
6967 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006968 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#endif
6970 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006972 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006973 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006974 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6975 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006977 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006979 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006981 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006986 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006988 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006990 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006991 buf->b_p_cinsd = vim_strsave(p_cinsd);
6992 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006993 buf->b_p_lop = vim_strsave(p_lop);
6994 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006995
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006996 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006999 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007001 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007003 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007007 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007008 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007009 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007010#endif
7011#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007012 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007013 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007014 (void)compile_cap_prog(&buf->b_s);
7015 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007017 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007018 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007019 buf->b_s.b_p_spo = vim_strsave(p_spo);
7020 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007022#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007024 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007026 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007028 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007029#if defined(FEAT_EVAL)
7030 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033#ifdef FEAT_CRYPT
7034 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007035 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007038 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039#ifdef FEAT_KEYMAP
7040 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 buf->b_kmap_state |= KEYMAP_INIT;
7043#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007044#ifdef FEAT_TERMINAL
7045 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007046 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007047#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007048 // This isn't really an option, but copying the langmap and IME
7049 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007051 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 // options that are normally global but also have a local value
7056 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007058 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007059 buf->b_p_bkc = empty_option;
7060 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061#ifdef FEAT_QUICKFIX
7062 buf->b_p_gp = empty_option;
7063 buf->b_p_mp = empty_option;
7064 buf->b_p_efm = empty_option;
7065#endif
7066 buf->b_p_ep = empty_option;
7067 buf->b_p_kp = empty_option;
7068 buf->b_p_path = empty_option;
7069 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007070 buf->b_p_tc = empty_option;
7071 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#ifdef FEAT_FIND_ID
7073 buf->b_p_def = empty_option;
7074 buf->b_p_inc = empty_option;
7075# ifdef FEAT_EVAL
7076 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007077 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078# endif
7079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 buf->b_p_dict = empty_option;
7081 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007082#ifdef FEAT_COMPL_FUNC
7083 buf->b_p_tsrfu = empty_option;
7084#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007085 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007086 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007087#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7088 buf->b_p_bexpr = empty_option;
7089#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007090#if defined(FEAT_CRYPT)
7091 buf->b_p_cm = empty_option;
7092#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007093#ifdef FEAT_PERSISTENT_UNDO
7094 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007095 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007096#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007097 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007098 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
7100 /*
7101 * Don't copy the options set by ex_help(), use the saved values,
7102 * when going from a help buffer to a non-help buffer.
7103 * Don't touch these at all when BCO_NOHELP is used and going from
7104 * or to a help buffer.
7105 */
7106 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007109#ifdef FEAT_VARTABS
7110 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007111 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007112 else
7113 buf->b_p_vts_array = NULL;
7114#endif
7115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 else
7117 {
7118 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007119 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 did_isk = TRUE;
7121 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007122 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007123#ifdef FEAT_VARTABS
7124 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007125 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007126 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007127 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007128 else
7129 buf->b_p_vts_array = NULL;
7130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (buf->b_p_bt[0] == 'h')
7133 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007135 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 }
7137 }
7138
7139 /*
7140 * When the options should be copied (ignoring BCO_ALWAYS), set the
7141 * flag that indicates that the options have been initialized.
7142 */
7143 if (should_copy)
7144 buf->b_p_initialized = TRUE;
7145 }
7146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007147 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 if (did_isk)
7149 (void)buf_init_chartab(buf, FALSE);
7150}
7151
7152/*
7153 * Reset the 'modifiable' option and its default value.
7154 */
7155 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007156reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
7158 int opt_idx;
7159
7160 curbuf->b_p_ma = FALSE;
7161 p_ma = FALSE;
7162 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007163 if (opt_idx >= 0)
7164 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165}
7166
7167/*
7168 * Set the global value for 'iminsert' to the local value.
7169 */
7170 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007171set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 p_iminsert = curbuf->b_p_iminsert;
7174}
7175
7176/*
7177 * Set the global value for 'imsearch' to the local value.
7178 */
7179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007180set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 p_imsearch = curbuf->b_p_imsearch;
7183}
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185static int expand_option_idx = -1;
7186static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7187static int expand_option_flags = 0;
7188
7189 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007190set_context_in_set_cmd(
7191 expand_T *xp,
7192 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007193 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007196 long_u flags = 0; // init for GCC
7197 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 char_u *p;
7199 char_u *s;
7200 int is_term_option = FALSE;
7201 int key;
7202
7203 expand_option_flags = opt_flags;
7204
7205 xp->xp_context = EXPAND_SETTINGS;
7206 if (*arg == NUL)
7207 {
7208 xp->xp_pattern = arg;
7209 return;
7210 }
7211 p = arg + STRLEN(arg) - 1;
7212 if (*p == ' ' && *(p - 1) != '\\')
7213 {
7214 xp->xp_pattern = p + 1;
7215 return;
7216 }
7217 while (p > arg)
7218 {
7219 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007220 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (*p == ' ' || *p == ',')
7222 {
7223 while (s > arg && *(s - 1) == '\\')
7224 --s;
7225 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007226 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (*p == ' ' && ((p - s) & 1) == 0)
7228 {
7229 ++p;
7230 break;
7231 }
7232 --p;
7233 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007234 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
7236 xp->xp_context = EXPAND_BOOL_SETTINGS;
7237 p += 2;
7238 }
7239 if (STRNCMP(p, "inv", 3) == 0)
7240 {
7241 xp->xp_context = EXPAND_BOOL_SETTINGS;
7242 p += 3;
7243 }
7244 xp->xp_pattern = arg = p;
7245 if (*arg == '<')
7246 {
7247 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007248 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 return;
7250 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007251 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
7253 xp->xp_context = EXPAND_NOTHING;
7254 return;
7255 }
7256 nextchar = *++p;
7257 is_term_option = TRUE;
7258 expand_option_name[2] = KEY2TERMCAP0(key);
7259 expand_option_name[3] = KEY2TERMCAP1(key);
7260 }
7261 else
7262 {
7263 if (p[0] == 't' && p[1] == '_')
7264 {
7265 p += 2;
7266 if (*p != NUL)
7267 ++p;
7268 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007269 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 nextchar = *++p;
7271 is_term_option = TRUE;
7272 expand_option_name[2] = p[-2];
7273 expand_option_name[3] = p[-1];
7274 }
7275 else
7276 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007277 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7279 p++;
7280 if (*p == NUL)
7281 return;
7282 nextchar = *p;
7283 *p = NUL;
7284 opt_idx = findoption(arg);
7285 *p = nextchar;
7286 if (opt_idx == -1 || options[opt_idx].var == NULL)
7287 {
7288 xp->xp_context = EXPAND_NOTHING;
7289 return;
7290 }
7291 flags = options[opt_idx].flags;
7292 if (flags & P_BOOL)
7293 {
7294 xp->xp_context = EXPAND_NOTHING;
7295 return;
7296 }
7297 }
7298 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007299 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7301 {
7302 ++p;
7303 nextchar = '=';
7304 }
7305 if ((nextchar != '=' && nextchar != ':')
7306 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7307 {
7308 xp->xp_context = EXPAND_UNSUCCESSFUL;
7309 return;
7310 }
7311 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7312 {
7313 xp->xp_context = EXPAND_OLD_SETTING;
7314 if (is_term_option)
7315 expand_option_idx = -1;
7316 else
7317 expand_option_idx = opt_idx;
7318 xp->xp_pattern = p + 1;
7319 return;
7320 }
7321 xp->xp_context = EXPAND_NOTHING;
7322 if (is_term_option || (flags & P_NUM))
7323 return;
7324
7325 xp->xp_pattern = p + 1;
7326
7327 if (flags & P_EXPAND)
7328 {
7329 p = options[opt_idx].var;
7330 if (p == (char_u *)&p_bdir
7331 || p == (char_u *)&p_dir
7332 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007333 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#ifdef FEAT_SESSION
7337 || p == (char_u *)&p_vdir
7338#endif
7339 )
7340 {
7341 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007342 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 xp->xp_backslash = XP_BS_THREE;
7344 else
7345 xp->xp_backslash = XP_BS_ONE;
7346 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007347 else if (p == (char_u *)&p_ft)
7348 {
7349 xp->xp_context = EXPAND_FILETYPE;
7350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 else
7352 {
7353 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007354 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (p == (char_u *)&p_tags)
7356 xp->xp_backslash = XP_BS_THREE;
7357 else
7358 xp->xp_backslash = XP_BS_ONE;
7359 }
7360 }
7361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007362 // For an option that is a list of file names, find the start of the
7363 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7365 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (*p == ' ' || *p == ',')
7368 {
7369 s = p;
7370 while (s > xp->xp_pattern && *(s - 1) == '\\')
7371 --s;
7372 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7373 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7374 {
7375 xp->xp_pattern = p + 1;
7376 break;
7377 }
7378 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007379
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007380#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007381 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007382 if (options[opt_idx].var == (char_u *)&p_sps
7383 && STRNCMP(p, "file:", 5) == 0)
7384 {
7385 xp->xp_pattern = p + 5;
7386 break;
7387 }
7388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390}
7391
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007392/*
7393 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7394 *
7395 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7396 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7397 *
7398 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7399 * regular expression 'regmatch', then stores the match in matches[idx] and
7400 * returns TRUE.
7401 *
7402 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7403 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7404 *
7405 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7406 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7407 */
7408 static int
7409match_str(
7410 char_u *str,
7411 regmatch_T *regmatch,
7412 char_u **matches,
7413 int idx,
7414 int test_only,
7415 int fuzzy,
7416 char_u *fuzzystr,
7417 fuzmatch_str_T *fuzmatch)
7418{
7419 if (!fuzzy)
7420 {
7421 if (vim_regexec(regmatch, str, (colnr_T)0))
7422 {
7423 if (!test_only)
7424 matches[idx] = vim_strsave(str);
7425 return TRUE;
7426 }
7427 }
7428 else
7429 {
7430 int score;
7431
7432 score = fuzzy_match_str(str, fuzzystr);
7433 if (score != 0)
7434 {
7435 if (!test_only)
7436 {
7437 fuzmatch[idx].idx = idx;
7438 fuzmatch[idx].str = vim_strsave(str);
7439 fuzmatch[idx].score = score;
7440 }
7441
7442 return TRUE;
7443 }
7444 }
7445
7446 return FALSE;
7447}
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007450ExpandSettings(
7451 expand_T *xp,
7452 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007453 char_u *fuzzystr,
7454 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007455 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007456 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007458 int num_normal = 0; // Nr of matching non-term-code settings
7459 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 int opt_idx;
7461 int match;
7462 int count = 0;
7463 char_u *str;
7464 int loop;
7465 int is_term_opt;
7466 char_u name_buf[MAX_KEY_NAME_LEN];
7467 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007468 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007469 int fuzzy;
7470 fuzmatch_str_T *fuzmatch = NULL;
7471
Christian Brabandtcb747892022-05-08 21:10:56 +01007472 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007474 // do this loop twice:
7475 // loop == 0: count the number of matching options
7476 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 for (loop = 0; loop <= 1; ++loop)
7478 {
7479 regmatch->rm_ic = ic;
7480 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7481 {
K.Takataeeec2542021-06-02 13:28:16 +02007482 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007483 {
7484 if (match_str((char_u *)names[match], regmatch, *matches,
7485 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {
7487 if (loop == 0)
7488 num_normal++;
7489 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007490 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7495 opt_idx++)
7496 {
7497 if (options[opt_idx].var == NULL)
7498 continue;
7499 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7500 && !(options[opt_idx].flags & P_BOOL))
7501 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007502 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 if (is_term_opt && num_normal > 0)
7504 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007505
7506 if (match_str(str, regmatch, *matches, count, (loop == 0),
7507 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 {
7509 if (loop == 0)
7510 {
7511 if (is_term_opt)
7512 num_term++;
7513 else
7514 num_normal++;
7515 }
7516 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007517 count++;
7518 }
7519 else if (!fuzzy && options[opt_idx].shortname != NULL
7520 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007521 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007522 {
7523 // Compare against the abbreviated option name (for regular
7524 // expression match). Fuzzy matching (previous if) already
7525 // matches against both the expanded and abbreviated names.
7526 if (loop == 0)
7527 {
7528 if (is_term_opt)
7529 num_term++;
7530 else
7531 num_normal++;
7532 }
7533 else
7534 (*matches)[count++] = vim_strsave(str);
7535 }
7536 else if (is_term_opt)
7537 {
7538 name_buf[0] = '<';
7539 name_buf[1] = 't';
7540 name_buf[2] = '_';
7541 name_buf[3] = str[2];
7542 name_buf[4] = str[3];
7543 name_buf[5] = '>';
7544 name_buf[6] = NUL;
7545
7546 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7547 fuzzy, fuzzystr, fuzmatch))
7548 {
7549 if (loop == 0)
7550 num_term++;
7551 else
7552 count++;
7553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
7555 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007556
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 /*
7558 * Check terminal key codes, these are not in the option table
7559 */
7560 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7561 {
7562 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7563 {
7564 if (!isprint(str[0]) || !isprint(str[1]))
7565 continue;
7566
7567 name_buf[0] = 't';
7568 name_buf[1] = '_';
7569 name_buf[2] = str[0];
7570 name_buf[3] = str[1];
7571 name_buf[4] = NUL;
7572
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007573 if (match_str(name_buf, regmatch, *matches, count,
7574 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7575 {
7576 if (loop == 0)
7577 num_term++;
7578 else
7579 count++;
7580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 else
7582 {
7583 name_buf[0] = '<';
7584 name_buf[1] = 't';
7585 name_buf[2] = '_';
7586 name_buf[3] = str[0];
7587 name_buf[4] = str[1];
7588 name_buf[5] = '>';
7589 name_buf[6] = NUL;
7590
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007591 if (match_str(name_buf, regmatch, *matches, count,
7592 (loop == 0), fuzzy, fuzzystr,
7593 fuzmatch))
7594 {
7595 if (loop == 0)
7596 num_term++;
7597 else
7598 count++;
7599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601 }
7602
7603 /*
7604 * Check special key names.
7605 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007606 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7608 {
7609 name_buf[0] = '<';
7610 STRCPY(name_buf + 1, str);
7611 STRCAT(name_buf, ">");
7612
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007613 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7614 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
7616 if (loop == 0)
7617 num_term++;
7618 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007619 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 }
7621 }
7622 }
7623 if (loop == 0)
7624 {
7625 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007626 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007628 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 else
7630 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007631 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007633 *matches = ALLOC_MULT(char_u *, *numMatches);
7634 if (*matches == NULL)
7635 {
7636 *matches = (char_u **)"";
7637 return FAIL;
7638 }
7639 }
7640 else
7641 {
7642 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7643 if (fuzmatch == NULL)
7644 {
7645 *matches = (char_u **)"";
7646 return FAIL;
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649 }
7650 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007651
7652 if (fuzzy &&
7653 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7654 return FAIL;
7655
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 return OK;
7657}
7658
7659 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007660ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007662 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 char_u *buf;
7664
zeertzjqb0d45ec2023-01-25 15:04:22 +00007665 *numMatches = 0;
7666 *matches = ALLOC_ONE(char_u *);
7667 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 return FAIL;
7669
7670 /*
7671 * For a terminal key code expand_option_idx is < 0.
7672 */
7673 if (expand_option_idx < 0)
7674 {
7675 var = find_termcode(expand_option_name + 2);
7676 if (var == NULL)
7677 expand_option_idx = findoption(expand_option_name);
7678 }
7679
7680 if (expand_option_idx >= 0)
7681 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007682 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 option_value2string(&options[expand_option_idx], expand_option_flags);
7684 var = NameBuff;
7685 }
7686 else if (var == NULL)
7687 var = (char_u *)"";
7688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007689 // A backslash is required before some characters. This is the reverse of
7690 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 buf = vim_strsave_escaped(var, escape_chars);
7692
7693 if (buf == NULL)
7694 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007695 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 return FAIL;
7697 }
7698
7699#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007700 // For MS-Windows et al. we don't double backslashes at the start and
7701 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007702 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 if (var[0] == '\\' && var[1] == '\\'
7704 && expand_option_idx >= 0
7705 && (options[expand_option_idx].flags & P_EXPAND)
7706 && vim_isfilec(var[2])
7707 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007708 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709#endif
7710
zeertzjqb0d45ec2023-01-25 15:04:22 +00007711 *matches[0] = buf;
7712 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 return OK;
7714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715
7716/*
7717 * Get the value for the numeric or string option *opp in a nice format into
7718 * NameBuff[]. Must not be called with a hidden option!
7719 */
7720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007721option_value2string(
7722 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007723 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
7725 char_u *varp;
7726
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007727 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729 if (opp->flags & P_NUM)
7730 {
7731 long wc = 0;
7732
7733 if (wc_use_keyname(varp, &wc))
7734 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7735 else if (wc != 0)
7736 STRCPY(NameBuff, transchar((int)wc));
7737 else
7738 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7739 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007740 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
7742 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007743 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 NameBuff[0] = NUL;
7745#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007746 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007747 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 STRCPY(NameBuff, "*****");
7749#endif
7750 else if (opp->flags & P_EXPAND)
7751 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007752 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 else if ((char_u **)opp->var == &p_pt)
7754 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7755 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007756 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758}
7759
7760/*
7761 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7762 * printed as a keyname.
7763 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7764 */
7765 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007766wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7769 {
7770 *wcp = *(long *)varp;
7771 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7772 return TRUE;
7773 }
7774 return FALSE;
7775}
7776
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 * Return TRUE if "x" is present in 'shortmess' option, or
7779 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7780 */
7781 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007782shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007784 return p_shm != NULL &&
7785 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 || (vim_strchr(p_shm, 'a') != NULL
7787 && vim_strchr((char_u *)SHM_A, x) != NULL));
7788}
7789
7790/*
7791 * paste_option_changed() - Called after p_paste was set or reset.
7792 */
7793 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007794paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 static int old_p_paste = FALSE;
7797 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007798 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800#ifdef FEAT_RIGHTLEFT
7801 static int save_ri = 0;
7802 static int save_hkmap = 0;
7803#endif
7804 buf_T *buf;
7805
7806 if (p_paste)
7807 {
7808 /*
7809 * Paste switched from off to on.
7810 * Save the current values, so they can be restored later.
7811 */
7812 if (!old_p_paste)
7813 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007814 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007815 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 buf->b_p_tw_nopaste = buf->b_p_tw;
7818 buf->b_p_wm_nopaste = buf->b_p_wm;
7819 buf->b_p_sts_nopaste = buf->b_p_sts;
7820 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007821 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007822#ifdef FEAT_VARTABS
7823 if (buf->b_p_vsts_nopaste)
7824 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007825 buf->b_p_vsts_nopaste =
7826 buf->b_p_vsts && buf->b_p_vsts != empty_option
7827 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007831 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007833 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835#ifdef FEAT_RIGHTLEFT
7836 save_ri = p_ri;
7837 save_hkmap = p_hkmap;
7838#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007839 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007840 p_ai_nopaste = p_ai;
7841 p_et_nopaste = p_et;
7842 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 p_tw_nopaste = p_tw;
7844 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007845#ifdef FEAT_VARTABS
7846 if (p_vsts_nopaste)
7847 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007848 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7849 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852
7853 /*
7854 * Always set the option values, also when 'paste' is set when it is
7855 * already on.
7856 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007857 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007858 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007860 buf->b_p_tw = 0; // textwidth is 0
7861 buf->b_p_wm = 0; // wrapmargin is 0
7862 buf->b_p_sts = 0; // softtabstop is 0
7863 buf->b_p_ai = 0; // no auto-indent
7864 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007865#ifdef FEAT_VARTABS
7866 if (buf->b_p_vsts)
7867 free_string_option(buf->b_p_vsts);
7868 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007869 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007873 // set global options
7874 p_sm = 0; // no showmatch
7875 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007877 status_redraw_all(); // redraw to remove the ruler
7878 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007880 p_ri = 0; // no reverse insert
7881 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007883 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 p_tw = 0;
7885 p_wm = 0;
7886 p_sts = 0;
7887 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007888#ifdef FEAT_VARTABS
7889 if (p_vsts)
7890 free_string_option(p_vsts);
7891 p_vsts = empty_option;
7892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894
7895 /*
7896 * Paste switched from on to off: Restore saved values.
7897 */
7898 else if (old_p_paste)
7899 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007900 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007901 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {
7903 buf->b_p_tw = buf->b_p_tw_nopaste;
7904 buf->b_p_wm = buf->b_p_wm_nopaste;
7905 buf->b_p_sts = buf->b_p_sts_nopaste;
7906 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007907 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007908#ifdef FEAT_VARTABS
7909 if (buf->b_p_vsts)
7910 free_string_option(buf->b_p_vsts);
7911 buf->b_p_vsts = buf->b_p_vsts_nopaste
7912 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007913 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007914 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007915 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007916 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007917 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 }
7920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007921 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007923 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007925 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927#ifdef FEAT_RIGHTLEFT
7928 p_ri = save_ri;
7929 p_hkmap = save_hkmap;
7930#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007931 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007932 p_ai = p_ai_nopaste;
7933 p_et = p_et_nopaste;
7934 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 p_tw = p_tw_nopaste;
7936 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007937#ifdef FEAT_VARTABS
7938 if (p_vsts)
7939 free_string_option(p_vsts);
7940 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 }
7943
7944 old_p_paste = p_paste;
7945}
7946
7947/*
7948 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7949 *
7950 * Reset 'compatible' and set the values for options that didn't get set yet
7951 * to the Vim defaults.
7952 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007953 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 */
7955 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007956vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007958 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007959 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007960 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
7962 if (!option_was_set((char_u *)"cp"))
7963 {
7964 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007965 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7967 set_option_default(opt_idx, OPT_FREE, FALSE);
7968 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007969 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007971
7972 if (fname != NULL)
7973 {
7974 p = vim_getenv(envname, &dofree);
7975 if (p == NULL)
7976 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007977 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007978 p = FullName_save(fname, FALSE);
7979 if (p != NULL)
7980 {
7981 vim_setenv(envname, p);
7982 vim_free(p);
7983 }
7984 }
7985 else if (dofree)
7986 vim_free(p);
7987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988}
7989
7990/*
7991 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7992 */
7993 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007994change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007996 int opt_idx;
7997
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 if (p_cp != on)
7999 {
8000 p_cp = on;
8001 compatible_set();
8002 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008003 opt_idx = findoption((char_u *)"cp");
8004 if (opt_idx >= 0)
8005 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006}
8007
8008/*
8009 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008010 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 */
8012 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008013option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015 int idx;
8016
8017 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008018 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 return FALSE;
8020 if (options[idx].flags & P_WAS_SET)
8021 return TRUE;
8022 return FALSE;
8023}
8024
8025/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008026 * Reset the flag indicating option "name" was set.
8027 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008028 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008029reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008030{
8031 int idx = findoption(name);
8032
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008033 if (idx < 0)
8034 return FAIL;
8035
8036 options[idx].flags &= ~P_WAS_SET;
8037 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008038}
8039
8040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 * compatible_set() - Called when 'compatible' has been set or unset.
8042 *
8043 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8044 * flag) to a Vi compatible value.
8045 * When 'compatible' is unset: Set all options that have a different default
8046 * for Vim (without the P_VI_DEF flag) to that default.
8047 */
8048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008049compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051 int opt_idx;
8052
Bram Moolenaardac13472019-09-16 21:06:21 +02008053 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8055 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8056 set_option_default(opt_idx, OPT_FREE, p_cp);
8057 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008058 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059}
8060
Bram Moolenaardac13472019-09-16 21:06:21 +02008061#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063/*
8064 * fill_breakat_flags() -- called when 'breakat' changes value.
8065 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008067fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008069 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 int i;
8071
8072 for (i = 0; i < 256; i++)
8073 breakat_flags[i] = FALSE;
8074
8075 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008076 for (p = p_breakat; *p; p++)
8077 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079#endif
8080
8081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 * Check if backspacing over something is allowed.
8083 */
8084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008085can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008086 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008088#ifdef FEAT_JOB_CHANNEL
8089 if (what == BS_START && bt_prompt(curbuf))
8090 return FALSE;
8091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 switch (*p_bs)
8093 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008094 case '3': return TRUE;
8095 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 case '1': return (what != BS_START);
8097 case '0': return FALSE;
8098 }
8099 return vim_strchr(p_bs, what) != NULL;
8100}
8101
8102/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008103 * Return the effective 'scrolloff' value for the current window, using the
8104 * global value when appropriate.
8105 */
8106 long
8107get_scrolloff_value(void)
8108{
8109 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8110}
8111
8112/*
8113 * Return the effective 'sidescrolloff' value for the current window, using the
8114 * global value when appropriate.
8115 */
8116 long
8117get_sidescrolloff_value(void)
8118{
8119 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8120}
8121
8122/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008123 * Get the local or global value of 'backupcopy'.
8124 */
8125 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008126get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008127{
8128 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8129}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008130
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008131#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008132/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008133 * Get the local or global value of 'formatlistpat'.
8134 */
8135 char_u *
8136get_flp_value(buf_T *buf)
8137{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008138 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8139 return p_flp;
8140 return buf->b_p_flp;
8141}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008142#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008143
8144/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008145 * Get the local or global value of the 'virtualedit' flags.
8146 */
8147 unsigned int
8148get_ve_flags(void)
8149{
Gary Johnson51ad8502021-08-03 18:33:08 +02008150 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8151 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008152}
8153
Bram Moolenaaree857022019-11-09 23:26:40 +01008154#if defined(FEAT_LINEBREAK) || defined(PROTO)
8155/*
8156 * Get the local or global value of 'showbreak'.
8157 */
8158 char_u *
8159get_showbreak_value(win_T *win)
8160{
8161 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8162 return p_sbr;
8163 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8164 return empty_option;
8165 return win->w_p_sbr;
8166}
8167#endif
8168
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008169#if defined(FEAT_EVAL) || defined(PROTO)
8170/*
8171 * Get window or buffer local options.
8172 */
8173 dict_T *
8174get_winbuf_options(int bufopt)
8175{
8176 dict_T *d;
8177 int opt_idx;
8178
8179 d = dict_alloc();
8180 if (d == NULL)
8181 return NULL;
8182
Bram Moolenaardac13472019-09-16 21:06:21 +02008183 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008184 {
8185 struct vimoption *opt = &options[opt_idx];
8186
8187 if ((bufopt && (opt->indir & PV_BUF))
8188 || (!bufopt && (opt->indir & PV_WIN)))
8189 {
8190 char_u *varp = get_varp(opt);
8191
8192 if (varp != NULL)
8193 {
8194 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008195 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008196 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008197 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008198 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008199 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008200 }
8201 }
8202 }
8203
8204 return d;
8205}
8206#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008207
Bram Moolenaardac13472019-09-16 21:06:21 +02008208#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008209/*
8210 * This is called when 'culopt' is changed
8211 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008212 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008213fill_culopt_flags(char_u *val, win_T *wp)
8214{
8215 char_u *p;
8216 char_u culopt_flags_new = 0;
8217
8218 if (val == NULL)
8219 p = wp->w_p_culopt;
8220 else
8221 p = val;
8222 while (*p != NUL)
8223 {
8224 if (STRNCMP(p, "line", 4) == 0)
8225 {
8226 p += 4;
8227 culopt_flags_new |= CULOPT_LINE;
8228 }
8229 else if (STRNCMP(p, "both", 4) == 0)
8230 {
8231 p += 4;
8232 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8233 }
8234 else if (STRNCMP(p, "number", 6) == 0)
8235 {
8236 p += 6;
8237 culopt_flags_new |= CULOPT_NBR;
8238 }
8239 else if (STRNCMP(p, "screenline", 10) == 0)
8240 {
8241 p += 10;
8242 culopt_flags_new |= CULOPT_SCRLINE;
8243 }
8244
8245 if (*p != ',' && *p != NUL)
8246 return FAIL;
8247 if (*p == ',')
8248 ++p;
8249 }
8250
8251 // Can't have both "line" and "screenline".
8252 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8253 return FAIL;
8254 wp->w_p_culopt_flags = culopt_flags_new;
8255
8256 return OK;
8257}
8258#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008259
8260/*
8261 * Get the value of 'magic' adjusted for Vim9 script.
8262 */
8263 int
8264magic_isset(void)
8265{
8266 switch (magic_overruled)
8267 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008268 case OPTION_MAGIC_ON: return TRUE;
8269 case OPTION_MAGIC_OFF: return FALSE;
8270 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008271 }
8272#ifdef FEAT_EVAL
8273 if (in_vim9script())
8274 return TRUE;
8275#endif
8276 return p_magic;
8277}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008278
8279/*
8280 * Set the callback function value for an option that accepts a function name,
8281 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8282 * Returns OK if the option is successfully set to a function, otherwise
8283 * returns FAIL.
8284 */
8285 int
8286option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8287{
8288#ifdef FEAT_EVAL
8289 typval_T *tv;
8290 callback_T cb;
8291
8292 if (optval == NULL || *optval == NUL)
8293 {
8294 free_callback(optcb);
8295 return OK;
8296 }
8297
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008298 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008299 || (STRNCMP(optval, "function(", 9) == 0)
8300 || (STRNCMP(optval, "funcref(", 8) == 0))
8301 // Lambda expression or a funcref
8302 tv = eval_expr(optval, NULL);
8303 else
8304 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008305 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008306 if (tv == NULL)
8307 return FAIL;
8308
8309 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008310 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008311 {
8312 free_tv(tv);
8313 return FAIL;
8314 }
8315
8316 free_callback(optcb);
8317 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008318 if (cb.cb_free_name)
8319 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008320 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008321
8322 // when using Vim9 style "import.funcname" it needs to be expanded to
8323 // "import#funcname".
8324 expand_autload_callback(optcb);
8325
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008326 return OK;
8327#else
8328 return FAIL;
8329#endif
8330}