blob: d93d3859fb87239d05a7347f8b928771f1ce015f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void paste_option_changed(void);
70static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000073 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000075 static void
76set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000080 // Find default value for 'shell' option.
81 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000082 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010083#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000084 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010085 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +000087 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020088#if defined(MSWIN)
89 {
90 // For MS-Windows put the path in quotes instead of escaping spaces.
91 char_u *cmd;
92 size_t len;
93
94 if (vim_strchr(p, ' ') != NULL)
95 {
96 len = STRLEN(p) + 3; // two quotes and a trailing NUL
97 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +020098 if (cmd != NULL)
99 {
100 vim_snprintf((char *)cmd, len, "\"%s\"", p);
101 set_string_default("sh", cmd);
102 vim_free(cmd);
103 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200104 }
105 else
106 set_string_default("sh", p);
107 }
108#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100109 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200110#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000111}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000113/*
114 * Set the default for 'backupskip' to include environment variables for
115 * temp files.
116 */
117 static void
118set_init_default_backupskip(void)
119{
120 int opt_idx;
121 long_u n;
122 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100123#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000124 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100125#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000126 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100127#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000128 int len;
129 garray_T ga;
130 int mustfree;
131 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200132
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000133 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000135 ga_init2(&ga, 1, 100);
136 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
137 {
138 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100143# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000144 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100147#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 p = vim_getenv((char_u *)names[n], &mustfree);
149 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000151 // First time count the NUL, otherwise count the ','.
152 len = (int)STRLEN(p) + 3;
153 item = alloc(len);
154 STRCPY(item, p);
155 add_pathsep(item);
156 STRCAT(item, "*");
157 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
158 == NULL
159 && ga_grow(&ga, len) == OK)
160 {
161 if (ga.ga_len > 0)
162 STRCAT(ga.ga_data, ",");
163 STRCAT(ga.ga_data, item);
164 ga.ga_len += len;
165 }
166 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 if (mustfree)
169 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000171 if (ga.ga_data != NULL)
172 {
173 set_string_default("bsk", ga.ga_data);
174 vim_free(ga.ga_data);
175 }
176}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000178/*
179 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
180 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
181 */
182 static void
183set_init_default_maxmemtot(void)
184{
185 int opt_idx;
186 long_u n;
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000189 if (opt_idx < 0)
190 return;
191
192#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
193 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {
ichizok02560422022-04-05 14:18:44 +0100196#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000197 // Use amount of memory available at this moment.
198 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100199#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000200 // Use amount of memory available to Vim.
201 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100202#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000203 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
206 opt_idx = findoption((char_u *)"maxmem");
207 if (opt_idx >= 0)
208 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000210 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
211 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000212#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000216}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000218/*
219 * Initialize the 'cdpath' option to a default value.
220 */
221 static void
222set_init_default_cdpath(void)
223{
224 int opt_idx;
225 char_u *cdpath;
226 char_u *buf;
227 int i;
228 int j;
229 int mustfree = FALSE;
230
231 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
232 if (cdpath == NULL)
233 return;
234
235 buf = alloc((STRLEN(cdpath) << 1) + 2);
236 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000238 buf[0] = ','; // start with ",", current dir first
239 j = 1;
240 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000242 if (vim_ispathlistsep(cdpath[i]))
243 buf[j++] = ',';
244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000246 if (cdpath[i] == ' ' || cdpath[i] == ',')
247 buf[j++] = '\\';
248 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 }
250 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000251 buf[j] = NUL;
252 opt_idx = findoption((char_u *)"cdpath");
253 if (opt_idx >= 0)
254 {
255 options[opt_idx].def_val[VI_DEFAULT] = buf;
256 options[opt_idx].flags |= P_DEF_ALLOCED;
257 }
258 else
259 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000261 if (mustfree)
262 vim_free(cdpath);
263}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000265/*
266 * Initialize the 'printencoding' option to a default value.
267 */
268 static void
269set_init_default_printencoding(void)
270{
ichizok02560422022-04-05 14:18:44 +0100271#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000272 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100273 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100275# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000276 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100277# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000278 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100279# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000280 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100281# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000282 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000284 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000286}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287
288#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000289/*
290 * Initialize the 'printexpr' option to a default value.
291 */
292 static void
293set_init_default_printexpr(void)
294{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100295 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100297# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000298 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100299# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
301
ichizok02560422022-04-05 14:18:44 +0100302# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# endif
305 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000306}
307#endif
308
309#ifdef UNIX
310/*
311 * Force restricted-mode on for "nologin" or "false" $SHELL
312 */
313 static void
314set_init_restricted_mode(void)
315{
316 char_u *p;
317
318 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000319 if (p == NULL)
320 return;
321 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
322 restricted = TRUE;
323 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000324}
325#endif
326
327#ifdef CLEAN_RUNTIMEPATH
328/*
329 * When Vim is started with the "--clean" argument, set the default value
330 * for the 'runtimepath' and 'packpath' options.
331 */
332 static void
333set_init_clean_rtp(void)
334{
335 int opt_idx;
336
337 opt_idx = findoption((char_u *)"runtimepath");
338 if (opt_idx >= 0)
339 {
340 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
341 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
342 }
343 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000344 if (opt_idx < 0)
345 return;
346 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
347 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000348}
349#endif
350
351/*
352 * Expand environment variables and things like "~" for the defaults.
353 * If option_expand() returns non-NULL the variable is expanded. This can
354 * only happen for non-indirect options.
355 * Also set the default to the expanded value, so ":set" does not list
356 * them.
357 * Don't set the P_ALLOCED flag, because we don't want to free the
358 * default.
359 */
360 static void
361set_init_expand_env(void)
362{
363 int opt_idx;
364 char_u *p;
365
366 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
367 {
368 if ((options[opt_idx].flags & P_GETTEXT)
369 && options[opt_idx].var != NULL)
370 p = (char_u *)_(*(char **)options[opt_idx].var);
371 else
372 p = option_expand(opt_idx, NULL);
373 if (p != NULL && (p = vim_strsave(p)) != NULL)
374 {
375 *(char_u **)options[opt_idx].var = p;
376 // VIMEXP
377 // Defaults for all expanded options are currently the same for Vi
378 // and Vim. When this changes, add some code here! Also need to
379 // split P_DEF_ALLOCED in two.
380 if (options[opt_idx].flags & P_DEF_ALLOCED)
381 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
382 options[opt_idx].def_val[VI_DEFAULT] = p;
383 options[opt_idx].flags |= P_DEF_ALLOCED;
384 }
385 }
386}
387
388/*
389 * Initialize the 'LANG' environment variable to a default value.
390 */
391 static void
392set_init_lang_env(void)
393{
394#if defined(MSWIN) && defined(FEAT_GETTEXT)
395 // If $LANG isn't set, try to get a good value for it. This makes the
396 // right language be used automatically. Don't do this for English.
397 if (mch_getenv((char_u *)"LANG") == NULL)
398 {
399 char buf[20];
400 long_u n;
401
402 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
403 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
404 // only the first two.
405 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
406 (LPTSTR)buf, 20);
407 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
408 {
409 // There are a few exceptions (probably more)
410 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
411 STRCPY(buf, "zh_TW");
412 else if (STRNICMP(buf, "chs", 3) == 0
413 || STRNICMP(buf, "zhc", 3) == 0)
414 STRCPY(buf, "zh_CN");
415 else if (STRNICMP(buf, "jp", 2) == 0)
416 STRCPY(buf, "ja");
417 else
418 buf[2] = NUL; // truncate to two-letter code
419 vim_setenv((char_u *)"LANG", (char_u *)buf);
420 }
421 }
422#elif defined(MACOS_CONVERT)
423 // Moved to os_mac_conv.c to avoid dependency problems.
424 mac_lang_init();
425#endif
426}
427
428/*
429 * Initialize the 'encoding' option to a default value.
430 */
431 static void
432set_init_default_encoding(void)
433{
434 char_u *p;
435 int opt_idx;
436
437# ifdef MSWIN
438 // MS-Windows has builtin support for conversion to and from Unicode, using
439 // "utf-8" for 'encoding' should work best for most users.
440 p = vim_strsave((char_u *)ENC_DFLT);
441# else
442 // enc_locale() will try to find the encoding of the current locale.
443 // This works best for properly configured systems, old and new.
444 p = enc_locale();
445# endif
446 if (p == NULL)
447 return;
448
449 // Try setting 'encoding' and check if the value is valid.
450 // If not, go back to the default encoding.
451 char_u *save_enc = p_enc;
452 p_enc = p;
453 if (STRCMP(p_enc, "gb18030") == 0)
454 {
455 // We don't support "gb18030", but "cp936" is a good substitute
456 // for practical purposes, thus use that. It's not an alias to
457 // still support conversion between gb18030 and utf-8.
458 p_enc = vim_strsave((char_u *)"cp936");
459 vim_free(p);
460 }
461 if (mb_init() == NULL)
462 {
463 opt_idx = findoption((char_u *)"encoding");
464 if (opt_idx >= 0)
465 {
466 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
467 options[opt_idx].flags |= P_DEF_ALLOCED;
468 }
469
470#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
471 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
472 {
473 // Adjust the default for 'isprint' and 'iskeyword' to match
474 // latin1. Also set the defaults for when 'nocompatible' is
475 // set.
476 set_string_option_direct((char_u *)"isp", -1,
477 ISP_LATIN1, OPT_FREE, SID_NONE);
478 set_string_option_direct((char_u *)"isk", -1,
479 ISK_LATIN1, OPT_FREE, SID_NONE);
480 opt_idx = findoption((char_u *)"isp");
481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
483 opt_idx = findoption((char_u *)"isk");
484 if (opt_idx >= 0)
485 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
486 (void)init_chartab();
487 }
488#endif
489
490#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
491 // Win32 console: When GetACP() returns a different value from
492 // GetConsoleCP() set 'termencoding'.
493 if (
494# ifdef VIMDLL
495 (!gui.in_use && !gui.starting) &&
496# endif
497 GetACP() != GetConsoleCP())
498 {
499 char buf[50];
500
501 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
502 // Use an alternative value.
503 if (GetConsoleCP() == 0)
504 sprintf(buf, "cp%ld", (long)GetACP());
505 else
506 sprintf(buf, "cp%ld", (long)GetConsoleCP());
507 p_tenc = vim_strsave((char_u *)buf);
508 if (p_tenc != NULL)
509 {
510 opt_idx = findoption((char_u *)"termencoding");
511 if (opt_idx >= 0)
512 {
513 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
514 options[opt_idx].flags |= P_DEF_ALLOCED;
515 }
516 convert_setup(&input_conv, p_tenc, p_enc);
517 convert_setup(&output_conv, p_enc, p_tenc);
518 }
519 else
520 p_tenc = empty_option;
521 }
522#endif
523#if defined(MSWIN)
524 // $HOME may have characters in active code page.
525 init_homedir();
526#endif
527 }
528 else
529 {
530 vim_free(p_enc);
531 p_enc = save_enc;
532 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000533}
534
535/*
536 * Initialize the options, first part.
537 *
538 * Called only once from main(), just after creating the first buffer.
539 * If "clean_arg" is TRUE Vim was started with --clean.
540 */
541 void
542set_init_1(int clean_arg)
543{
544#ifdef FEAT_LANGMAP
545 langmap_init();
546#endif
547
548 // Be Vi compatible by default
549 p_cp = TRUE;
550
551 // Use POSIX compatibility when $VIM_POSIX is set.
552 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
553 {
554 set_string_default("cpo", (char_u *)CPO_ALL);
555 set_string_default("shm", (char_u *)SHM_POSIX);
556 }
557
558 set_init_default_shell();
559 set_init_default_backupskip();
560 set_init_default_maxmemtot();
561 set_init_default_cdpath();
562 set_init_default_printencoding();
563#ifdef FEAT_POSTSCRIPT
564 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#endif
566
567 /*
568 * Set all the options (except the terminal options) to their default
569 * value. Also set the global value for local options.
570 */
571 set_options_default(0);
572
matveytadbb1bf2022-02-01 17:26:12 +0000573#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000574 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000575#endif
576
Bram Moolenaar07268702018-03-01 21:57:32 +0100577#ifdef CLEAN_RUNTIMEPATH
578 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000579 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#ifdef FEAT_GUI
583 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100584 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
586
587 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100589 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 check_buf_options(curbuf);
591 check_win_options(curwin);
592 check_options();
593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 didset_options();
596
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000597#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // Use the current chartab for the generic chartab. This is not in
599 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000600 init_spell_chartab();
601#endif
602
K.Takatace3189d2023-02-15 19:13:43 +0000603 set_init_default_encoding();
604
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000605 // Expand environment variables and things like "~" for the defaults.
606 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100608 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // Detect use of mlterm.
612 // Mlterm is a terminal emulator akin to xterm that has some special
613 // abilities (bidi namely).
614 // NOTE: mlterm's author is being asked to 'set' a variable
615 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100617 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
619
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200620 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000622 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100625 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 set_helplang_default(get_mess_lang());
627#endif
628}
629
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200630static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
631
632/*
633 * Set the "fileencodings" option to the default value for when 'encoding' is
634 * utf-8.
635 */
636 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000637set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200638{
639 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
640 OPT_FREE, 0);
641}
642
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643/*
644 * Set an option to its default value.
645 * This does not take care of side effects!
646 */
647 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100648set_option_default(
649 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100650 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
651 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100653 char_u *varp; // pointer to variable for current option
654 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000656 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
658
659 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
660 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100661 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
663 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
664 if (flags & P_STRING)
665 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200666 // 'fencs' default value depends on 'encoding'
667 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
668 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 // Use set_string_option_direct() for local options to handle
670 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200671 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200672 set_string_option_direct(NULL, opt_idx,
673 options[opt_idx].def_val[dvi], opt_flags, 0);
674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200676 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
677 free_string_option(*(char_u **)(varp));
678 *(char_u **)varp = options[opt_idx].def_val[dvi];
679 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681 }
682 else if (flags & P_NUM)
683 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000684 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 win_comp_scroll(curwin);
686 else
687 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100688 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
689
690 if ((long *)varp == &curwin->w_p_so
691 || (long *)varp == &curwin->w_p_siso)
692 // 'scrolloff' and 'sidescrolloff' local values have a
693 // different default value than the global default.
694 *(long *)varp = -1;
695 else
696 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100697 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (both)
699 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100700 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
702 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100703 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100705 // the cast to long is required for Manx C, long_i is needed for
706 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000707 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000708#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100709 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000710 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
711 *(int *)varp = FALSE;
712#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100713 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (both)
715 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
716 *(int *)varp;
717 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100719 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000720 flagsp = insecure_flag(opt_idx, opt_flags);
721 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 }
723
724#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200725 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#endif
727}
728
729/*
730 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200731 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100734set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000739 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaardac13472019-09-16 21:06:21 +0200741 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200742 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200743 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100744 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200745# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200746 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200747 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200748# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100749 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 set_option_default(i, opt_flags, p_cp);
751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100752 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000753 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200755 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756}
757
758/*
759 * Set the Vi-default value of a string option.
760 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100761 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100763 static void
764set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 char_u *p;
767 int opt_idx;
768
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100769 if (escape && vim_strchr(val, ' ') != NULL)
770 p = vim_strsave_escaped(val, (char_u *)" ");
771 else
772 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000773 if (p == NULL) // we don't want a NULL
774 return;
775
776 opt_idx = findoption((char_u *)name);
777 if (opt_idx < 0)
778 return;
779
780 if (options[opt_idx].flags & P_DEF_ALLOCED)
781 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
782 options[opt_idx].def_val[VI_DEFAULT] = p;
783 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100786 void
787set_string_default(char *name, char_u *val)
788{
789 set_string_default_esc(name, val, FALSE);
790}
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200793 * For an option value that contains comma separated items, find "newval" in
794 * "origval". Return NULL if not found.
795 */
796 static char_u *
797find_dup_item(char_u *origval, char_u *newval, long_u flags)
798{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200799 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200800 size_t newlen;
801 char_u *s;
802
803 if (origval == NULL)
804 return NULL;
805
806 newlen = STRLEN(newval);
807 for (s = origval; *s != NUL; ++s)
808 {
809 if ((!(flags & P_COMMA)
810 || s == origval
811 || (s[-1] == ',' && !(bs & 1)))
812 && STRNCMP(s, newval, newlen) == 0
813 && (!(flags & P_COMMA)
814 || s[newlen] == ','
815 || s[newlen] == NUL))
816 return s;
817 // Count backslashes. Only a comma with an even number of backslashes
818 // or a single backslash preceded by a comma before it is recognized as
819 // a separator.
820 if ((s > origval + 1
821 && s[-1] == '\\'
822 && s[-2] != ',')
823 || (s == origval + 1
824 && s[-1] == '\\'))
825 ++bs;
826 else
827 bs = 0;
828 }
829 return NULL;
830}
831
832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Set the Vi-default value of a number option.
834 * Used for 'lines' and 'columns'.
835 */
836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100837set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000839 int opt_idx;
840
841 opt_idx = findoption((char_u *)name);
842 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000843 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844}
845
Dominique Pelle748b3082022-01-08 12:41:16 +0000846#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200847/*
848 * Set all window-local and buffer-local options to the Vim default.
849 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200850 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200851 */
852 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200853set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200854{
855 win_T *save_curwin = curwin;
856 int i;
857
858 curwin = wp;
859 curbuf = curwin->w_buffer;
860 block_autocmds();
861
Bram Moolenaardac13472019-09-16 21:06:21 +0200862 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200863 {
864 struct vimoption *p = &(options[i]);
865 char_u *varp = get_varp_scope(p, OPT_LOCAL);
866
867 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200868 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200869 && !(options[i].flags & P_NODEFAULT)
870 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200871 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200872 }
873
874 unblock_autocmds();
875 curwin = save_curwin;
876 curbuf = curwin->w_buffer;
877}
Dominique Pelle748b3082022-01-08 12:41:16 +0000878#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200879
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000880#if defined(EXITFREE) || defined(PROTO)
881/*
882 * Free all options.
883 */
884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100885free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000886{
887 int i;
888
Bram Moolenaardac13472019-09-16 21:06:21 +0200889 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000890 {
891 if (options[i].indir == PV_NONE)
892 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100894 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000895 free_string_option(*(char_u **)options[i].var);
896 if (options[i].flags & P_DEF_ALLOCED)
897 free_string_option(options[i].def_val[VI_DEFAULT]);
898 }
899 else if (options[i].var != VAR_WIN
900 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200902 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000903 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000904 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000905 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000906}
907#endif
908
909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910/*
911 * Initialize the options, part two: After getting Rows and Columns and
912 * setting 'term'.
913 */
914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100915set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000917 int idx;
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100920 * 'scroll' defaults to half the window height. The stored default is zero,
921 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000923 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000924 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000925 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 comp_col();
927
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000928 /*
929 * 'window' is only for backwards compatibility with Vi.
930 * Default is Rows - 1.
931 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000932 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000933 p_window = Rows - 1;
934 set_number_default("window", Rows - 1);
935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100936 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100937#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000938 /*
939 * If 'background' wasn't set by the user, try guessing the value,
940 * depending on the terminal name. Only need to check for terminals
941 * with a dark background, that can handle color.
942 */
943 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000944 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
945 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000947 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // don't mark it as set, when starting the GUI it may be
949 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000950 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000953
954#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000955 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000956#endif
957#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000958 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000959#endif
960#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000961 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Initialize the options, part three: After reading the .vimrc
967 */
968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100969set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100971#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972/*
973 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
974 * This is done after other initializations, where 'shell' might have been
975 * set, but only if they have not been set before.
976 */
977 char_u *p;
978 int idx_srr;
979 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100980# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 int idx_sp;
982 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000986 if (idx_srr < 0)
987 do_srr = FALSE;
988 else
989 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000992 if (idx_sp < 0)
993 do_sp = FALSE;
994 else
995 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100996# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200997 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (p != NULL)
999 {
1000 /*
1001 * Default for p_sp is "| tee", for p_srr is ">".
1002 * For known shells it is changed here to include stderr.
1003 */
1004 if ( fnamecmp(p, "csh") == 0
1005 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001006# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 || fnamecmp(p, "csh.exe") == 0
1008 || fnamecmp(p, "tcsh.exe") == 0
1009# endif
1010 )
1011 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001012# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (do_sp)
1014 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001015# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001017# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1021 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (do_srr)
1024 {
1025 p_srr = (char_u *)">&";
1026 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1027 }
1028 }
Mike Williams12795022021-06-28 20:53:58 +02001029# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001030 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1031 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001032 else if ( fnamecmp(p, "powershell") == 0
1033 || fnamecmp(p, "powershell.exe") == 0
1034 )
1035 {
1036# if defined(FEAT_QUICKFIX)
1037 if (do_sp)
1038 {
1039 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1040 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1041 }
1042# endif
1043 if (do_srr)
1044 {
1045 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1046 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1047 }
1048 }
1049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 else
Natanael Copa56318362021-05-06 18:46:35 +02001051 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if ( fnamecmp(p, "sh") == 0
1053 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001054 || fnamecmp(p, "mksh") == 0
1055 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001057 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001059 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001060 || fnamecmp(p, "ash") == 0
1061 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001062 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001063# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 || fnamecmp(p, "cmd") == 0
1065 || fnamecmp(p, "sh.exe") == 0
1066 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001067 || fnamecmp(p, "mksh.exe") == 0
1068 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001070 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 || fnamecmp(p, "bash.exe") == 0
1072 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001073 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001074 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001076 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001078# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (do_sp)
1080 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001081# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001083# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001084 if (fnamecmp(p, "pwsh") == 0)
1085 p_sp = (char_u *)">%s 2>&1";
1086 else
1087 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1090 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (do_srr)
1093 {
1094 p_srr = (char_u *)">%s 2>&1";
1095 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1096 }
1097 }
1098 vim_free(p);
1099 }
1100#endif
1101
Bram Moolenaar4f974752019-02-17 17:44:42 +01001102#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001104 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1105 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001107 * set, but only if they have not been set before.
1108 * Default values depend on shell (cmd.exe is default shell):
1109 *
1110 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001111 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001112 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001113 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001114 * "sh" like shells - "-c" "\""
1115 *
1116 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 */
Mike Williams12795022021-06-28 20:53:58 +02001118 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1119 {
1120 int idx_opt;
1121
1122 idx_opt = findoption((char_u *)"shcf");
1123 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1124 {
1125 p_shcf = (char_u*)"-Command";
1126 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1127 }
1128
1129 idx_opt = findoption((char_u *)"sxq");
1130 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1131 {
1132 p_sxq = (char_u*)"\"";
1133 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1134 }
1135 }
1136 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 {
1138 int idx3;
1139
1140 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001141 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
1143 p_shcf = (char_u *)"-c";
1144 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1145 }
1146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001147 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001149 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
1151 p_sxq = (char_u *)"\"";
1152 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001155 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1156 {
1157 int idx3;
1158
1159 /*
1160 * cmd.exe on Windows will strip the first and last double quote given
1161 * on the command line, e.g. most of the time things like:
1162 * cmd /c "my path/to/echo" "my args to echo"
1163 * become:
1164 * my path/to/echo" "my args to echo
1165 * when executed.
1166 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001167 * To avoid this, set shellxquote to surround the command in
1168 * parenthesis. This appears to make most commands work, without
1169 * breaking commands that worked previously, such as
1170 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001171 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001172 idx3 = findoption((char_u *)"sxq");
1173 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1174 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001175 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001176 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1177 }
1178
Bram Moolenaara64ba222012-02-12 23:23:31 +01001179 idx3 = findoption((char_u *)"shcf");
1180 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1181 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001182 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001183 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1184 }
1185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#endif
1187
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001188 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001189 {
1190 int idx_ffs = findoption((char_u *)"ffs");
1191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001192 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001193 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1194 set_fileformat(default_fileformat(), OPT_LOCAL);
1195 }
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198}
1199
1200#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1201/*
1202 * When 'helplang' is still at its default value, set it to "lang".
1203 * Only the first two characters of "lang" are used.
1204 */
1205 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001206set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207{
1208 int idx;
1209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 return;
1212 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001213 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1214 return;
1215
1216 if (options[idx].flags & P_ALLOCED)
1217 free_string_option(p_hlg);
1218 p_hlg = vim_strsave(lang);
1219 if (p_hlg == NULL)
1220 p_hlg = empty_option;
1221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001223 // zh_CN becomes "cn", zh_TW becomes "tw"
1224 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001225 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001226 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1227 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001228 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001229 // any C like setting, such as C.UTF-8, becomes "en"
1230 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1231 {
1232 p_hlg[0] = 'e';
1233 p_hlg[1] = 'n';
1234 }
1235 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001237 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238}
1239#endif
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241/*
1242 * 'title' and 'icon' only default to true if they have not been set or reset
1243 * in .vimrc and we can read the old value.
1244 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1245 * they can be reset. This reduces startup time when using X on a remote
1246 * machine.
1247 */
1248 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001249set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250{
1251 int idx1;
1252 long val;
1253
1254 /*
1255 * If GUI is (going to be) used, we can always set the window title and
1256 * icon name. Saves a bit of time, because the X11 display server does
1257 * not need to be contacted.
1258 */
1259 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001260 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262#ifdef FEAT_GUI
1263 if (gui.starting || gui.in_use)
1264 val = TRUE;
1265 else
1266#endif
1267 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001268 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 p_title = val;
1270 }
1271 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001272 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001274 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001276
1277#ifdef FEAT_GUI
1278 if (gui.starting || gui.in_use)
1279 val = TRUE;
1280 else
1281#endif
1282 val = mch_can_restore_icon();
1283 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1284 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001287 void
1288ex_set(exarg_T *eap)
1289{
1290 int flags = 0;
1291
1292 if (eap->cmdidx == CMD_setlocal)
1293 flags = OPT_LOCAL;
1294 else if (eap->cmdidx == CMD_setglobal)
1295 flags = OPT_GLOBAL;
1296#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001297 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001298 ex_options(eap);
1299 else
1300#endif
1301 {
1302 if (eap->forceit)
1303 flags |= OPT_ONECOLUMN;
1304 (void)do_set(eap->arg, flags);
1305 }
1306}
1307
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001308/*
1309 * :set operator types
1310 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001311typedef enum {
1312 OP_NONE = 0,
1313 OP_ADDING, // "opt+=arg"
1314 OP_PREPENDING, // "opt^=arg"
1315 OP_REMOVING, // "opt-=arg"
1316} set_op_T;
1317
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001318typedef enum {
1319 PREFIX_NO = 0, // "no" prefix
1320 PREFIX_NONE, // no prefix
1321 PREFIX_INV, // "inv" prefix
1322} set_prefix_T;
1323
1324/*
1325 * Return the prefix type for the option name in *argp.
1326 */
1327 static set_prefix_T
1328get_option_prefix(char_u **argp)
1329{
1330 int prefix = PREFIX_NONE;
1331 char_u *arg = *argp;
1332
1333 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1334 {
1335 prefix = PREFIX_NO;
1336 arg += 2;
1337 }
1338 else if (STRNCMP(arg, "inv", 3) == 0)
1339 {
1340 prefix = PREFIX_INV;
1341 arg += 3;
1342 }
1343
1344 *argp = arg;
1345 return prefix;
1346}
1347
1348/*
1349 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1350 * and the option name length in "*lenp". For a <t_xx> option, return the key
1351 * number in "*keyp".
1352 *
1353 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1354 * otherwise returns OK.
1355 */
1356 static int
1357parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1358{
1359 int key = 0;
1360 int len;
1361 int opt_idx;
1362
1363 if (*arg == '<')
1364 {
1365 opt_idx = -1;
1366 // look out for <t_>;>
1367 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1368 len = 5;
1369 else
1370 {
1371 len = 1;
1372 while (arg[len] != NUL && arg[len] != '>')
1373 ++len;
1374 }
1375 if (arg[len] != '>')
1376 return FAIL;
1377
1378 arg[len] = NUL; // put NUL after name
1379 if (arg[1] == 't' && arg[2] == '_') // could be term code
1380 opt_idx = findoption(arg + 1);
1381 arg[len++] = '>'; // restore '>'
1382 if (opt_idx == -1)
1383 key = find_key_option(arg + 1, TRUE);
1384 }
1385 else
1386 {
1387 int nextchar; // next non-white char after option name
1388
1389 len = 0;
1390 /*
1391 * The two characters after "t_" may not be alphanumeric.
1392 */
1393 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1394 len = 4;
1395 else
1396 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1397 ++len;
1398 nextchar = arg[len];
1399 arg[len] = NUL; // put NUL after name
1400 opt_idx = findoption(arg);
1401 arg[len] = nextchar; // restore nextchar
1402 if (opt_idx == -1)
1403 key = find_key_option(arg, FALSE);
1404 }
1405
1406 *keyp = key;
1407 *lenp = len;
1408 *opt_idxp = opt_idx;
1409
1410 return OK;
1411}
1412
1413/*
1414 * Get the option operator (+=, ^=, -=).
1415 */
1416 static set_op_T
1417get_opt_op(char_u *arg)
1418{
1419 set_op_T op = OP_NONE;
1420
1421 if (*arg != NUL && *(arg + 1) == '=')
1422 {
1423 if (*arg == '+')
1424 op = OP_ADDING; // "+="
1425 else if (*arg == '^')
1426 op = OP_PREPENDING; // "^="
1427 else if (*arg == '-')
1428 op = OP_REMOVING; // "-="
1429 }
1430
1431 return op;
1432}
1433
1434/*
1435 * Validate whether the value of the option in "opt_idx" can be changed.
1436 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1437 * if it can be changed.
1438 */
1439 static int
1440validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1441{
1442 // Skip all options that are not window-local (used when showing
1443 // an already loaded buffer in a window).
1444 if ((opt_flags & OPT_WINONLY)
1445 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1446 return FAIL;
1447
1448 // Skip all options that are window-local (used for :vimgrep).
1449 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1450 && options[opt_idx].var == VAR_WIN)
1451 return FAIL;
1452
1453 // Disallow changing some options from modelines.
1454 if (opt_flags & OPT_MODELINE)
1455 {
1456 if (flags & (P_SECURE | P_NO_ML))
1457 {
1458 *errmsg = e_not_allowed_in_modeline;
1459 return FAIL;
1460 }
1461 if ((flags & P_MLE) && !p_mle)
1462 {
1463 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1464 return FAIL;
1465 }
1466#ifdef FEAT_DIFF
1467 // In diff mode some options are overruled. This avoids that
1468 // 'foldmethod' becomes "marker" instead of "diff" and that
1469 // "wrap" gets set.
1470 if (curwin->w_p_diff
1471 && opt_idx >= 0 // shut up coverity warning
1472 && (
1473# ifdef FEAT_FOLDING
1474 options[opt_idx].indir == PV_FDM ||
1475# endif
1476 options[opt_idx].indir == PV_WRAP))
1477 return FAIL;
1478#endif
1479 }
1480
1481#ifdef HAVE_SANDBOX
1482 // Disallow changing some options in the sandbox
1483 if (sandbox != 0 && (flags & P_SECURE))
1484 {
1485 *errmsg = e_not_allowed_in_sandbox;
1486 return FAIL;
1487 }
1488#endif
1489
1490 return OK;
1491}
1492
Bram Moolenaar47403942022-09-21 21:12:53 +01001493/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001494 * Get the Vim/Vi default value for a string option.
1495 */
1496 static char_u *
1497stropt_get_default_val(
1498 int opt_idx,
1499 char_u *varp,
1500 int flags,
1501 int cp_val)
1502{
1503 char_u *newval;
1504
1505 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1506 ? VI_DEFAULT : VIM_DEFAULT];
1507 if ((char_u **)varp == &p_bg)
1508 {
1509 // guess the value of 'background'
1510#ifdef FEAT_GUI
1511 if (gui.in_use)
1512 newval = gui_bg_default();
1513 else
1514#endif
1515 newval = term_bg_default();
1516 }
1517 else if ((char_u **)varp == &p_fencs && enc_utf8)
1518 newval = fencs_utf8_default;
1519
1520 // expand environment variables and ~ since the default value was
1521 // already expanded, only required when an environment variable was set
1522 // later
1523 if (newval == NULL)
1524 newval = empty_option;
1525 else
1526 {
1527 char_u *s = option_expand(opt_idx, newval);
1528 if (s == NULL)
1529 s = newval;
1530 newval = vim_strsave(s);
1531 }
1532
1533 return newval;
1534}
1535
1536/*
1537 * Convert the 'backspace' option number value to a string: for adding,
1538 * prepending and removing string.
1539 */
1540 static void
1541opt_backspace_nr2str(
1542 char_u *varp,
1543 char_u **origval_p,
1544 char_u **origval_l_p,
1545 char_u **origval_g_p,
1546 char_u **oldval_p)
1547{
1548 int i = getdigits((char_u **)varp);
1549
1550 switch (i)
1551 {
1552 case 0:
1553 *(char_u **)varp = empty_option;
1554 break;
1555 case 1:
1556 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1557 break;
1558 case 2:
1559 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1560 break;
1561 case 3:
1562 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1563 break;
1564 }
1565 vim_free(*oldval_p);
1566 if (*origval_p == *oldval_p)
1567 *origval_p = *(char_u **)varp;
1568 if (*origval_l_p == *oldval_p)
1569 *origval_l_p = *(char_u **)varp;
1570 if (*origval_g_p == *oldval_p)
1571 *origval_g_p = *(char_u **)varp;
1572 *oldval_p = *(char_u **)varp;
1573}
1574
1575/*
1576 * Convert the 'whichwrap' option number value to a string, for backwards
1577 * compatibility with Vim 3.0.
1578 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1579 */
1580 static char_u *
1581opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1582{
1583 *whichwrap = NUL;
1584 int i = getdigits(argp);
1585 if (i & 1)
1586 STRCAT(whichwrap, "b,");
1587 if (i & 2)
1588 STRCAT(whichwrap, "s,");
1589 if (i & 4)
1590 STRCAT(whichwrap, "h,l,");
1591 if (i & 8)
1592 STRCAT(whichwrap, "<,>,");
1593 if (i & 16)
1594 STRCAT(whichwrap, "[,],");
1595 if (*whichwrap != NUL) // remove trailing ,
1596 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1597
1598 return whichwrap;
1599}
1600
1601/*
1602 * Copy the new string value into allocated memory for the option.
1603 * Can't use set_string_option_direct(), because we need to remove the
1604 * backslashes.
1605 */
1606 static char_u *
1607stropt_copy_value(
1608 char_u *origval,
1609 char_u **argp,
1610 set_op_T op,
1611 int flags UNUSED)
1612{
1613 char_u *arg = *argp;
1614 unsigned newlen;
1615 char_u *newval;
1616 char_u *s = NULL;
1617
1618 // get a bit too much
1619 newlen = (unsigned)STRLEN(arg) + 1;
1620 if (op != OP_NONE)
1621 newlen += (unsigned)STRLEN(origval) + 1;
1622 newval = alloc(newlen);
1623 if (newval == NULL) // out of mem, don't change
1624 return NULL;
1625 s = newval;
1626
1627 // Copy the string, skip over escaped chars.
1628 // For MS-DOS and WIN32 backslashes before normal file name characters
1629 // are not removed, and keep backslash at start, for "\\machine\path",
1630 // but do remove it for "\\\\machine\\path".
1631 // The reverse is found in ExpandOldSetting().
1632 while (*arg != NUL && !VIM_ISWHITE(*arg))
1633 {
1634 int i;
1635
1636 if (*arg == '\\' && arg[1] != NUL
1637#ifdef BACKSLASH_IN_FILENAME
1638 && !((flags & P_EXPAND)
1639 && vim_isfilec(arg[1])
1640 && !VIM_ISWHITE(arg[1])
1641 && (arg[1] != '\\'
1642 || (s == newval && arg[2] != '\\')))
1643#endif
1644 )
1645 ++arg; // remove backslash
1646 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1647 {
1648 // copy multibyte char
1649 mch_memmove(s, arg, (size_t)i);
1650 arg += i;
1651 s += i;
1652 }
1653 else
1654 *s++ = *arg++;
1655 }
1656 *s = NUL;
1657
1658 *argp = arg;
1659 return newval;
1660}
1661
1662/*
1663 * Expand environment variables and ~ in string option value 'newval'.
1664 */
1665 static char_u *
1666stropt_expand_envvar(
1667 int opt_idx,
1668 char_u *origval,
1669 char_u *newval,
1670 set_op_T op)
1671{
1672 char_u *s = option_expand(opt_idx, newval);
1673 if (s == NULL)
1674 return newval;
1675
1676 vim_free(newval);
1677 unsigned newlen = (unsigned)STRLEN(s) + 1;
1678 if (op != OP_NONE)
1679 newlen += (unsigned)STRLEN(origval) + 1;
1680
1681 newval = alloc(newlen);
1682 if (newval == NULL)
1683 return NULL;
1684
1685 STRCPY(newval, s);
1686
1687 return newval;
1688}
1689
1690/*
1691 * Concatenate the original and new values of a string option, adding a "," if
1692 * needed.
1693 */
1694 static void
1695stropt_concat_with_comma(
1696 char_u *origval,
1697 char_u *newval,
1698 set_op_T op,
1699 int flags)
1700{
1701 int len = 0;
1702
1703 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1704 if (op == OP_ADDING)
1705 {
1706 len = (int)STRLEN(origval);
1707 // strip a trailing comma, would get 2
1708 if (comma && len > 1
1709 && (flags & P_ONECOMMA) == P_ONECOMMA
1710 && origval[len - 1] == ','
1711 && origval[len - 2] != '\\')
1712 len--;
1713 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1714 mch_memmove(newval, origval, (size_t)len);
1715 }
1716 else
1717 {
1718 len = (int)STRLEN(newval);
1719 STRMOVE(newval + len + comma, origval);
1720 }
1721 if (comma)
1722 newval[len] = ',';
1723}
1724
1725/*
1726 * Remove a value from a string option. Copy string option value in "origval"
1727 * to "newval" and then remove the string "strval" of length "len".
1728 */
1729 static void
1730stropt_remove_val(
1731 char_u *origval,
1732 char_u *newval,
1733 int flags,
1734 char_u *strval,
1735 int len)
1736{
1737 // Remove newval[] from origval[]. (Note: "len" has been set above
1738 // and is used here).
1739 STRCPY(newval, origval);
1740 if (*strval)
1741 {
1742 // may need to remove a comma
1743 if (flags & P_COMMA)
1744 {
1745 if (strval == origval)
1746 {
1747 // include comma after string
1748 if (strval[len] == ',')
1749 ++len;
1750 }
1751 else
1752 {
1753 // include comma before string
1754 --strval;
1755 ++len;
1756 }
1757 }
1758 STRMOVE(newval + (strval - origval), strval + len);
1759 }
1760}
1761
1762/*
1763 * Remove flags that appear twice in the string option value 'newval'.
1764 */
1765 static void
1766stropt_remove_dupflags(char_u *newval, int flags)
1767{
1768 char_u *s = newval;
1769
1770 // Remove flags that appear twice.
1771 while (*s)
1772 {
1773 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1774 if (flags & P_ONECOMMA)
1775 {
1776 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1777 {
1778 // Remove the duplicated value and the next comma.
1779 STRMOVE(s, s + 2);
1780 continue;
1781 }
1782 }
1783 else
1784 {
1785 if ((!(flags & P_COMMA) || *s != ',')
1786 && vim_strchr(s + 1, *s) != NULL)
1787 {
1788 STRMOVE(s, s + 1);
1789 continue;
1790 }
1791 }
1792 ++s;
1793 }
1794}
1795
1796/*
1797 * Get the string value specified for a ":set" command. The following set
1798 * options are supported:
1799 * set {opt}&
1800 * set {opt}<
1801 * set {opt}={val}
1802 * set {opt}:{val}
1803 */
1804 static char_u *
1805stropt_get_newval(
1806 int nextchar,
1807 int opt_idx,
1808 char_u **argp,
1809 char_u *varp,
1810 char_u **origval_arg,
1811 char_u **origval_l_arg,
1812 char_u **origval_g_arg,
1813 char_u **oldval_arg,
1814 set_op_T *op_arg,
1815 int flags,
1816 int cp_val)
1817{
1818 char_u *arg = *argp;
1819 char_u *origval = *origval_arg;
1820 char_u *origval_l = *origval_l_arg;
1821 char_u *origval_g = *origval_g_arg;
1822 char_u *oldval = *oldval_arg;
1823 set_op_T op = *op_arg;
1824 char_u *save_arg = NULL;
1825 char_u *newval;
1826 char_u *s = NULL;
1827 char_u whichwrap[80];
1828
1829 if (nextchar == '&') // set to default val
1830 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1831 else if (nextchar == '<') // set to global val
1832 newval = vim_strsave(*(char_u **)get_varp_scope(
1833 &(options[opt_idx]), OPT_GLOBAL));
1834 else
1835 {
1836 ++arg; // jump to after the '=' or ':'
1837
1838 // Set 'keywordprg' to ":help" if an empty
1839 // value was passed to :set by the user.
1840 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1841 {
1842 save_arg = arg;
1843 arg = (char_u *)":help";
1844 }
1845 // Convert 'backspace' number to string
1846 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1847 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1848 &oldval);
1849 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1850 {
1851 // Convert 'whichwrap' number to string, for backwards
1852 // compatibility with Vim 3.0.
1853 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1854 save_arg = arg;
1855 arg = t;
1856 }
1857 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1858 // version 3.0
1859 else if (*arg == '>' && (varp == (char_u *)&p_dir
1860 || varp == (char_u *)&p_bdir))
1861 ++arg;
1862
1863 // Copy the new string into allocated memory.
1864 newval = stropt_copy_value(origval, &arg, op, flags);
1865 if (newval == NULL)
1866 goto done;
1867
1868 // Expand environment variables and ~.
1869 // Don't do it when adding without inserting a comma.
1870 if (op == OP_NONE || (flags & P_COMMA))
1871 {
1872 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1873 if (newval == NULL)
1874 goto done;
1875 }
1876
1877 // locate newval[] in origval[] when removing it and when adding to
1878 // avoid duplicates
1879 int len = 0;
1880 if (op == OP_REMOVING || (flags & P_NODUP))
1881 {
1882 len = (int)STRLEN(newval);
1883 s = find_dup_item(origval, newval, flags);
1884
1885 // do not add if already there
1886 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1887 {
1888 op = OP_NONE;
1889 STRCPY(newval, origval);
1890 }
1891
1892 // if no duplicate, move pointer to end of original value
1893 if (s == NULL)
1894 s = origval + (int)STRLEN(origval);
1895 }
1896
1897 // concatenate the two strings; add a ',' if needed
1898 if (op == OP_ADDING || op == OP_PREPENDING)
1899 stropt_concat_with_comma(origval, newval, op, flags);
1900 else if (op == OP_REMOVING)
1901 // Remove newval[] from origval[]. (Note: "len" has been set above
1902 // and is used here).
1903 stropt_remove_val(origval, newval, flags, s, len);
1904
1905 if (flags & P_FLAGLIST)
1906 // Remove flags that appear twice.
1907 stropt_remove_dupflags(newval, flags);
1908 }
1909
1910done:
1911 if (save_arg != NULL)
1912 arg = save_arg; // arg was temporarily changed, restore it
1913 *argp = arg;
1914 *origval_arg = origval;
1915 *origval_l_arg = origval_l;
1916 *origval_g_arg = origval_g;
1917 *oldval_arg = oldval;
1918 *op_arg = op;
1919
1920 return newval;
1921}
1922
1923/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001924 * Part of do_set() for string options.
1925 * Returns FAIL on failure, do not process further options.
1926 */
1927 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001928do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001929 int opt_idx,
1930 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001931 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001932 int nextchar,
1933 set_op_T op_arg,
1934 int flags,
1935 int cp_val,
1936 char_u *varp_arg,
1937 char *errbuf,
1938 int *value_checked,
1939 char **errmsg)
1940{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001941 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001942 set_op_T op = op_arg;
1943 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001944 char_u *oldval = NULL; // previous value if *varp
1945 char_u *newval;
1946 char_u *origval = NULL;
1947 char_u *origval_l = NULL;
1948 char_u *origval_g = NULL;
1949#if defined(FEAT_EVAL)
1950 char_u *saved_origval = NULL;
1951 char_u *saved_origval_l = NULL;
1952 char_u *saved_origval_g = NULL;
1953 char_u *saved_newval = NULL;
1954#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001955
1956 // When using ":set opt=val" for a global option
1957 // with a local value the local value will be
1958 // reset, use the global value here.
1959 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1960 && ((int)options[opt_idx].indir & PV_BOTH))
1961 varp = options[opt_idx].var;
1962
1963 // The old value is kept until we are sure that the new value is valid.
1964 oldval = *(char_u **)varp;
1965
1966 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1967 {
1968 origval_l = *(char_u **)get_varp_scope(
1969 &(options[opt_idx]), OPT_LOCAL);
1970 origval_g = *(char_u **)get_varp_scope(
1971 &(options[opt_idx]), OPT_GLOBAL);
1972
1973 // A global-local string option might have an empty option as value to
1974 // indicate that the global value should be used.
1975 if (((int)options[opt_idx].indir & PV_BOTH)
1976 && origval_l == empty_option)
1977 origval_l = origval_g;
1978 }
1979
1980 // When setting the local value of a global option, the old value may be
1981 // the global value.
1982 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1983 origval = *(char_u **)get_varp(&options[opt_idx]);
1984 else
1985 origval = oldval;
1986
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001987 // Get the new value for the option
1988 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1989 &origval_l, &origval_g, &oldval, &op, flags,
1990 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001991
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001992 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001993 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001994 if (newval == NULL)
1995 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001996
1997#if defined(FEAT_EVAL)
1998 if (!starting
1999# ifdef FEAT_CRYPT
2000 && options[opt_idx].indir != PV_KEY
2001# endif
2002 && origval != NULL && newval != NULL)
2003 {
2004 // origval may be freed by did_set_string_option(), make a copy.
2005 saved_origval = vim_strsave(origval);
2006 // newval (and varp) may become invalid if the buffer is closed by
2007 // autocommands.
2008 saved_newval = vim_strsave(newval);
2009 if (origval_l != NULL)
2010 saved_origval_l = vim_strsave(origval_l);
2011 if (origval_g != NULL)
2012 saved_origval_g = vim_strsave(origval_g);
2013 }
2014#endif
2015
2016 {
2017 long_u *p = insecure_flag(opt_idx, opt_flags);
2018 int secure_saved = secure;
2019
2020 // When an option is set in the sandbox, from a modeline or in secure
2021 // mode, then deal with side effects in secure mode. Also when the
2022 // value was set with the P_INSECURE flag and is not completely
2023 // replaced.
2024 if ((opt_flags & OPT_MODELINE)
2025#ifdef HAVE_SANDBOX
2026 || sandbox != 0
2027#endif
2028 || (op != OP_NONE && (*p & P_INSECURE)))
2029 secure = 1;
2030
2031 // Handle side effects, and set the global value for ":set" on local
2032 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2033 // be triggered that can cause havoc.
2034 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002035 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Bram Moolenaar47403942022-09-21 21:12:53 +01002036 opt_flags, value_checked);
2037
2038 secure = secure_saved;
2039 }
2040
2041#if defined(FEAT_EVAL)
2042 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002043 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002044 saved_origval_l, saved_origval_g, saved_newval);
2045 vim_free(saved_origval);
2046 vim_free(saved_origval_l);
2047 vim_free(saved_origval_g);
2048 vim_free(saved_newval);
2049#endif
2050
Bram Moolenaar6f981142022-09-22 12:48:58 +01002051 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002052 return *errmsg == NULL ? OK : FAIL;
2053}
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002056 * Set a boolean option.
2057 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002058 */
2059 static char *
2060do_set_option_bool(
2061 int opt_idx,
2062 int opt_flags,
2063 set_prefix_T prefix,
2064 long_u flags,
2065 char_u *varp,
2066 int nextchar,
2067 int afterchar,
2068 int cp_val)
2069
2070{
2071 varnumber_T value;
2072
2073 if (nextchar == '=' || nextchar == ':')
2074 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002075 if (opt_idx < 0 || varp == NULL)
2076 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002077
2078 /*
2079 * ":set opt!": invert
2080 * ":set opt&": reset to default value
2081 * ":set opt<": reset to global value
2082 */
2083 if (nextchar == '!')
2084 value = *(int *)(varp) ^ 1;
2085 else if (nextchar == '&')
2086 value = (int)(long)(long_i)options[opt_idx].def_val[
2087 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2088 else if (nextchar == '<')
2089 {
2090 // For 'autoread' -1 means to use global value.
2091 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2092 value = -1;
2093 else
2094 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2095 }
2096 else
2097 {
2098 /*
2099 * ":set invopt": invert
2100 * ":set opt" or ":set noopt": set or reset
2101 */
2102 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2103 return e_trailing_characters;
2104 if (prefix == PREFIX_INV)
2105 value = *(int *)(varp) ^ 1;
2106 else
2107 value = prefix == PREFIX_NO ? 0 : 1;
2108 }
2109
2110 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2111}
2112
2113/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002114 * Set a numeric option.
2115 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002116 */
2117 static char *
2118do_set_option_numeric(
2119 int opt_idx,
2120 int opt_flags,
2121 char_u **argp,
2122 int nextchar,
2123 set_op_T op,
2124 long_u flags,
2125 int cp_val,
2126 char_u *varp,
2127 char *errbuf,
2128 size_t errbuflen)
2129{
2130 char_u *arg = *argp;
2131 varnumber_T value;
2132 int i;
2133 char *errmsg = NULL;
2134
Bram Moolenaar546933f2023-02-06 16:40:49 +00002135 if (opt_idx < 0 || varp == NULL)
2136 return NULL; // "cannot happen"
2137 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002138 /*
2139 * Different ways to set a number option:
2140 * & set to default value
2141 * < set to global value
2142 * <xx> accept special key codes for 'wildchar'
2143 * c accept any non-digit for 'wildchar'
2144 * [-]0-9 set number
2145 * other error
2146 */
2147 ++arg;
2148 if (nextchar == '&')
2149 value = (long)(long_i)options[opt_idx].def_val[
2150 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2151 else if (nextchar == '<')
2152 {
2153 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2154 // use the global value.
2155 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2156 value = NO_LOCAL_UNDOLEVEL;
2157 else
2158 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2159 }
2160 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2161 && (*arg == '<'
2162 || *arg == '^'
2163 || (*arg != NUL
2164 && (!arg[1] || VIM_ISWHITE(arg[1]))
2165 && !VIM_ISDIGIT(*arg))))
2166 {
2167 value = string_to_key(arg, FALSE);
2168 if (value == 0 && (long *)varp != &p_wcm)
2169 {
2170 errmsg = e_invalid_argument;
2171 goto skip;
2172 }
2173 }
2174 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2175 {
2176 // Allow negative (for 'undolevels'), octal and hex numbers.
2177 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
2178 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2179 {
2180 errmsg = e_number_required_after_equal;
2181 goto skip;
2182 }
2183 }
2184 else
2185 {
2186 errmsg = e_number_required_after_equal;
2187 goto skip;
2188 }
2189
2190 if (op == OP_ADDING)
2191 value = *(long *)varp + value;
2192 else if (op == OP_PREPENDING)
2193 value = *(long *)varp * value;
2194 else if (op == OP_REMOVING)
2195 value = *(long *)varp - value;
2196
2197 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2198 opt_flags);
2199
2200skip:
2201 *argp = arg;
2202 return errmsg;
2203}
2204
2205/*
2206 * Set a key code (t_xx) option
2207 */
2208 static char *
2209do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2210{
2211 char_u *arg = *argp;
2212 char_u *p;
2213
2214 if (nextchar == '&')
2215 {
2216 if (add_termcap_entry(key_name, TRUE) == FAIL)
2217 return e_not_found_in_termcap;
2218 }
2219 else
2220 {
2221 ++arg; // jump to after the '=' or ':'
2222 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2223 if (*p == '\\' && p[1] != NUL)
2224 ++p;
2225 nextchar = *p;
2226 *p = NUL;
2227 add_termcode(key_name, arg, FALSE);
2228 *p = nextchar;
2229 }
2230 if (full_screen)
2231 ttest(FALSE);
2232 redraw_all_later(UPD_CLEAR);
2233
2234 *argp = arg;
2235 return NULL;
2236}
2237
2238/*
2239 * Set an option to a new value.
2240 */
2241 static char *
2242do_set_option_value(
2243 int opt_idx,
2244 int opt_flags,
2245 char_u **argp,
2246 set_prefix_T prefix,
2247 set_op_T op,
2248 long_u flags,
2249 char_u *varp,
2250 char_u *key_name,
2251 int nextchar,
2252 int afterchar,
2253 int cp_val,
2254 int *stopopteval,
2255 char *errbuf,
2256 size_t errbuflen)
2257{
2258 int value_checked = FALSE;
2259 char *errmsg = NULL;
2260 char_u *arg = *argp;
2261
2262 if (flags & P_BOOL)
2263 {
2264 // boolean option
2265 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2266 nextchar, afterchar, cp_val);
2267 if (errmsg != NULL)
2268 goto skip;
2269 }
2270 else
2271 {
2272 // numeric or string option
2273 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2274 || prefix != PREFIX_NONE)
2275 {
2276 errmsg = e_invalid_argument;
2277 goto skip;
2278 }
2279
2280 if (flags & P_NUM)
2281 {
2282 // numeric option
2283 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2284 op, flags, cp_val, varp,
2285 errbuf, errbuflen);
2286 if (errmsg != NULL)
2287 goto skip;
2288 }
2289 else if (opt_idx >= 0)
2290 {
2291 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002292 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2293 flags, cp_val, varp, errbuf,
2294 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002295 {
2296 if (errmsg != NULL)
2297 goto skip;
2298 *stopopteval = TRUE;
2299 goto skip;
2300 }
2301 }
2302 else
2303 {
2304 // key code option
2305 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2306 if (errmsg != NULL)
2307 goto skip;
2308 }
2309 }
2310
2311 if (opt_idx >= 0)
2312 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2313
2314skip:
2315 *argp = arg;
2316 return errmsg;
2317}
2318
2319/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002320 * Set an option to a new value.
2321 * Return NULL if OK, return an untranslated error message when something is
2322 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2323 */
2324 static char *
2325do_set_option(
2326 int opt_flags,
2327 char_u **argp,
2328 char_u *arg_start,
2329 char_u **startarg,
2330 int *did_show,
2331 int *stopopteval,
2332 char *errbuf,
2333 size_t errbuflen)
2334{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002335 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002336 char_u *arg;
2337 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2338 set_op_T op;
2339 long_u flags; // flags for current option
2340 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002341 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002342 int nextchar; // next non-white char after option name
2343 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002344 char *errmsg = NULL;
2345 int key;
2346 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002347
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002348 prefix = get_option_prefix(argp);
2349 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002350
2351 // find end of name
2352 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002353 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2354 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002355
2356 // remember character after option name
2357 afterchar = arg[len];
2358
2359 if (in_vim9script())
2360 {
2361 char_u *p = skipwhite(arg + len);
2362
2363 // disallow white space before =val, +=val, -=val, ^=val
2364 if (p > arg + len && (p[0] == '='
2365 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2366 && p[1] == '=')))
2367 {
2368 errmsg = e_no_white_space_allowed_between_option_and;
2369 arg = p;
2370 *startarg = p;
2371 goto skip;
2372 }
2373 }
2374 else
2375 // skip white space, allow ":set ai ?", ":set hlsearch !"
2376 while (VIM_ISWHITE(arg[len]))
2377 ++len;
2378
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002379 op = get_opt_op(arg + len);
2380 if (op != OP_NONE)
2381 len++;
2382
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002383 nextchar = arg[len];
2384
2385 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2386 {
2387 if (in_vim9script() && arg > arg_start
2388 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2389 errmsg = e_no_white_space_allowed_between_option_and;
2390 else
2391 errmsg = e_unknown_option;
2392 goto skip;
2393 }
2394
2395 if (opt_idx >= 0)
2396 {
2397 if (options[opt_idx].var == NULL) // hidden option: skip
2398 {
2399 // Only give an error message when requesting the value of
2400 // a hidden option, ignore setting it.
2401 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2402 && (!(options[opt_idx].flags & P_BOOL)
2403 || nextchar == '?'))
2404 errmsg = e_option_not_supported;
2405 goto skip;
2406 }
2407
2408 flags = options[opt_idx].flags;
2409 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2410 }
2411 else
2412 {
2413 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002414 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002415 if (key < 0)
2416 {
2417 key_name[0] = KEY2TERMCAP0(key);
2418 key_name[1] = KEY2TERMCAP1(key);
2419 }
2420 else
2421 {
2422 key_name[0] = KS_KEY;
2423 key_name[1] = (key & 0xff);
2424 }
2425 }
2426
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002427 // Make sure the option value can be changed.
2428 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002429 goto skip;
2430
Bram Moolenaar40b48722023-02-05 17:04:50 +00002431 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002432 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2433 {
2434 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002435 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2436 {
2437 if (arg[3] == 'm') // "opt&vim": set to Vim default
2438 {
2439 cp_val = FALSE;
2440 arg += 3;
2441 }
2442 else // "opt&vi": set to Vi default
2443 {
2444 cp_val = TRUE;
2445 arg += 2;
2446 }
2447 }
2448 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2449 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2450 {
2451 errmsg = e_trailing_characters;
2452 goto skip;
2453 }
2454 }
2455
2456 /*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002457 * Allow '=' and ':' for historical reasons (MSDOS command.com).
2458 * Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002459 */
2460 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002461 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002462 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2463 && !(flags & P_BOOL)))
2464 {
2465 /*
2466 * print value
2467 */
2468 if (*did_show)
2469 msg_putchar('\n'); // cursor below last one
2470 else
2471 {
2472 gotocmdline(TRUE); // cursor at status line
2473 *did_show = TRUE; // remember that we did a line
2474 }
2475 if (opt_idx >= 0)
2476 {
2477 showoneopt(&options[opt_idx], opt_flags);
2478#ifdef FEAT_EVAL
2479 if (p_verbose > 0)
2480 {
2481 // Mention where the option was last set.
2482 if (varp == options[opt_idx].var)
2483 last_set_msg(options[opt_idx].script_ctx);
2484 else if ((int)options[opt_idx].indir & PV_WIN)
2485 last_set_msg(curwin->w_p_script_ctx[
2486 (int)options[opt_idx].indir & PV_MASK]);
2487 else if ((int)options[opt_idx].indir & PV_BUF)
2488 last_set_msg(curbuf->b_p_script_ctx[
2489 (int)options[opt_idx].indir & PV_MASK]);
2490 }
2491#endif
2492 }
2493 else
2494 {
2495 char_u *p;
2496
2497 p = find_termcode(key_name);
2498 if (p == NULL)
2499 {
2500 errmsg = e_key_code_not_set;
2501 goto skip;
2502 }
2503 else
2504 (void)show_one_termcode(key_name, p, TRUE);
2505 }
2506 if (nextchar != '?'
2507 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2508 errmsg = e_trailing_characters;
2509 }
2510 else
2511 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002512 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2513 flags, varp, key_name, nextchar,
2514 afterchar, cp_val, stopopteval, errbuf,
2515 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002516 }
2517
2518skip:
2519 *argp = arg;
2520 return errmsg;
2521}
2522
2523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Parse 'arg' for option settings.
2525 *
2526 * 'arg' may be IObuff, but only when no errors can be present and option
2527 * does not need to be expanded with option_expand().
2528 * "opt_flags":
2529 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002530 * OPT_GLOBAL for ":setglobal"
2531 * OPT_LOCAL for ":setlocal" and a modeline
2532 * OPT_MODELINE for a modeline
2533 * OPT_WINONLY to only set window-local options
2534 * OPT_NOWIN to skip setting window-local options
2535 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002537 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 */
2539 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002540do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002541 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002542 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002544 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002546 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 if (*arg == NUL)
2549 {
2550 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 did_show = TRUE;
2552 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002555 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002557 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002558 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /*
2561 * ":set all" show all options.
2562 * ":set all&" set all options to their default value.
2563 */
2564 arg += 3;
2565 if (*arg == '&')
2566 {
2567 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002568 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002570 didset_options();
2571 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002572 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 did_show = TRUE;
2578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002580 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
2582 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002583 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002584 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 arg += 7;
2586 }
2587 else
2588 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002589 int stopopteval = FALSE;
2590 char *errmsg = NULL;
2591 char errbuf[80];
2592 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002594 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2595 &did_show, &stopopteval, errbuf,
2596 sizeof(errbuf));
2597 if (stopopteval)
2598 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 /*
2601 * Advance to next argument.
2602 * - skip until a blank found, taking care of backslashes
2603 * - skip blanks
2604 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2605 */
2606 for (i = 0; i < 2 ; ++i)
2607 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002608 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (*arg++ == '\\' && *arg != NUL)
2610 ++arg;
2611 arg = skipwhite(arg);
2612 if (*arg != '=')
2613 break;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002616 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002618 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2619 i = (int)STRLEN(IObuff) + 2;
2620 if (i + (arg - startarg) < IOSIZE)
2621 {
2622 // append the argument with the error
2623 STRCAT(IObuff, ": ");
2624 mch_memmove(IObuff + i, startarg, (arg - startarg));
2625 IObuff[i + (arg - startarg)] = NUL;
2626 }
2627 // make sure all characters are printable
2628 trans_characters(IObuff, IOSIZE);
2629
2630 ++no_wait_return; // wait_return() done later
2631 emsg((char *)IObuff); // show error highlighted
2632 --no_wait_return;
2633
2634 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637
2638 arg = skipwhite(arg);
2639 }
2640
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641theend:
2642 if (silent_mode && did_show)
2643 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002644 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002645 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002646 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002647 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002648 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002649 out_flush();
2650 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002651 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002652 }
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return OK;
2655}
2656
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002657/*
2658 * Call this when an option has been given a new value through a user command.
2659 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2660 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002662did_set_option(
2663 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002664 int opt_flags, // possibly with OPT_MODELINE
2665 int new_value, // value was replaced completely
2666 int value_checked) // value was checked to be safe, no need to set the
2667 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002668{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002669 long_u *p;
2670
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002671 options[opt_idx].flags |= P_WAS_SET;
2672
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002673 // When an option is set in the sandbox, from a modeline or in secure mode
2674 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2675 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002676 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002677 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002678#ifdef HAVE_SANDBOX
2679 || sandbox != 0
2680#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002681 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002682 *p = *p | P_INSECURE;
2683 else if (new_value)
2684 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002685}
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687/*
2688 * Convert a key name or string into a key value.
2689 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002690 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002692 int
2693string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002696 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (*arg == '^')
2698 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002699 if (multi_byte)
2700 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 return *arg;
2702}
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704/*
2705 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2706 * maketitle() to create and display it.
2707 * When switching the title or icon off, call mch_restore_title() to get
2708 * the old value back.
2709 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002710 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002711did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 if (starting != NO_SCREEN
2714#ifdef FEAT_GUI
2715 && !gui.starting
2716#endif
2717 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721/*
2722 * set_options_bin - called when 'bin' changes value.
2723 */
2724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002725set_options_bin(
2726 int oldval,
2727 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730 /*
2731 * The option values that are changed when 'bin' changes are
2732 * copied when 'bin is set and restored when 'bin' is reset.
2733 */
2734 if (newval)
2735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738 if (!(opt_flags & OPT_GLOBAL))
2739 {
2740 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2741 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2742 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2743 curbuf->b_p_et_nobin = curbuf->b_p_et;
2744 }
2745 if (!(opt_flags & OPT_LOCAL))
2746 {
2747 p_tw_nobin = p_tw;
2748 p_wm_nobin = p_wm;
2749 p_ml_nobin = p_ml;
2750 p_et_nobin = p_et;
2751 }
2752 }
2753
2754 if (!(opt_flags & OPT_GLOBAL))
2755 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 curbuf->b_p_tw = 0; // no automatic line wrap
2757 curbuf->b_p_wm = 0; // no automatic line wrap
2758 curbuf->b_p_ml = 0; // no modelines
2759 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 if (!(opt_flags & OPT_LOCAL))
2762 {
2763 p_tw = 0;
2764 p_wm = 0;
2765 p_ml = FALSE;
2766 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 if (!(opt_flags & OPT_GLOBAL))
2773 {
2774 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2775 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2776 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2777 curbuf->b_p_et = curbuf->b_p_et_nobin;
2778 }
2779 if (!(opt_flags & OPT_LOCAL))
2780 {
2781 p_tw = p_tw_nobin;
2782 p_wm = p_wm_nobin;
2783 p_ml = p_ml_nobin;
2784 p_et = p_et_nobin;
2785 }
2786 }
2787}
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789/*
2790 * Expand environment variables for some string options.
2791 * These string options cannot be indirect!
2792 * If "val" is NULL expand the current value of the option.
2793 * Return pointer to NameBuff, or NULL when not expanded.
2794 */
2795 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002796option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002798 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2800 return NULL;
2801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002802 // If val is longer than MAXPATHL no meaningful expansion can be done,
2803 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (val != NULL && STRLEN(val) > MAXPATHL)
2805 return NULL;
2806
2807 if (val == NULL)
2808 val = *(char_u **)options[opt_idx].var;
2809
2810 /*
2811 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2812 * Escape spaces when expanding 'tags', they are used to separate file
2813 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002814 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 */
2816 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002817 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002818#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002819 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2820#endif
2821 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002822 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 return NULL;
2824
2825 return NameBuff;
2826}
2827
2828/*
2829 * After setting various option values: recompute variables that depend on
2830 * option values.
2831 */
2832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002833didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 (void)init_chartab();
2837
Bram Moolenaardac13472019-09-16 21:06:21 +02002838 didset_string_options();
2839
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002840#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002841 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002842 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002843 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002844 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002845#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002846 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002847 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002848#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002850 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002851#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002852 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002853}
2854
2855/*
2856 * More side effects of setting options.
2857 */
2858 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002859didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002860{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002862 (void)highlight_changed();
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002865 check_opt_wim();
2866
Bram Moolenaareed9d462021-02-15 20:38:25 +01002867 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002868 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002871 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002872
2873#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002875 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002876#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002877#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002878 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002879 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002880 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002881 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883}
2884
2885/*
2886 * Check for string options that are NULL (normally only termcap options).
2887 */
2888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 int opt_idx;
2892
2893 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2894 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2895 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2896}
2897
2898/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002899 * Return the option index found by a pointer into term_strings[].
2900 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002902 int
2903get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002905 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2908 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002909 return opt_idx;
2910 return -1; // cannot happen: didn't find it!
2911}
2912
2913/*
2914 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2915 * Return the option index or -1 if not found.
2916 */
2917 int
2918set_term_option_alloced(char_u **p)
2919{
2920 int opt_idx = get_term_opt_idx(p);
2921
2922 if (opt_idx >= 0)
2923 options[opt_idx].flags |= P_ALLOCED;
2924 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925}
2926
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002927#if defined(FEAT_EVAL) || defined(PROTO)
2928/*
2929 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2930 * Return FALSE when it wasn't.
2931 * Return -1 for an unknown option.
2932 */
2933 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002934was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002935{
2936 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002937 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002938
2939 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002940 {
2941 flagp = insecure_flag(idx, opt_flags);
2942 return (*flagp & P_INSECURE) != 0;
2943 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002944 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002945 return -1;
2946}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002947
2948/*
2949 * Get a pointer to the flags used for the P_INSECURE flag of option
2950 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002951 * NOTE: Caller must make sure that "curwin" is set to the window from which
2952 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002953 */
2954 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002955insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002956{
2957 if (opt_flags & OPT_LOCAL)
2958 switch ((int)options[opt_idx].indir)
2959 {
2960#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002961 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002962#endif
2963#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002964# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002965 case PV_FDE: return &curwin->w_p_fde_flags;
2966 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002967# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002968# ifdef FEAT_BEVAL
2969 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2970# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002971 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002972 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002973# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002974 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002975# endif
2976#endif
2977 }
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002980 return &options[opt_idx].flags;
2981}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002982#endif
2983
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984/*
2985 * Redraw the window title and/or tab page text later.
2986 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002987void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002988{
2989 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002990 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002991}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002994 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2995 * characters or characters in "allowed".
2996 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002997 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002998valid_name(char_u *val, char *allowed)
2999{
3000 char_u *s;
3001
3002 for (s = val; *s != NUL; ++s)
3003 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3004 return FALSE;
3005 return TRUE;
3006}
3007
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003008#if defined(FEAT_EVAL) || defined(PROTO)
3009/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003010 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003011 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003012 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003013 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003014set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003015{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003016 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3017 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003018 sctx_T new_script_ctx = script_ctx;
3019
Bram Moolenaar51258742020-05-03 17:19:33 +02003020 // Modeline already has the line number set.
3021 if (!(opt_flags & OPT_MODELINE))
3022 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // Remember where the option was set. For local options need to do that
3025 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003027 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003028 if (both || (opt_flags & OPT_LOCAL))
3029 {
3030 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003031 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003032 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003033 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003034 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003035 if (both)
3036 // also setting the "all buffers" value
3037 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3038 new_script_ctx;
3039 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003040 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003041}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003042
3043/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003044 * Get the script context of global option "name".
3045 *
3046 */
3047 sctx_T *
3048get_option_sctx(char *name)
3049{
3050 int idx = findoption((char_u *)name);
3051
3052 if (idx >= 0)
3053 return &options[idx].script_ctx;
3054 siemsg("no such option: %s", name);
3055 return NULL;
3056}
3057
3058/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003059 * Set the script_ctx for a termcap option.
3060 * "name" must be the two character code, e.g. "RV".
3061 * When "name" is NULL use "opt_idx".
3062 */
3063 void
3064set_term_option_sctx_idx(char *name, int opt_idx)
3065{
3066 char_u buf[5];
3067 int idx;
3068
3069 if (name == NULL)
3070 idx = opt_idx;
3071 else
3072 {
3073 buf[0] = 't';
3074 buf[1] = '_';
3075 buf[2] = name[0];
3076 buf[3] = name[1];
3077 buf[4] = 0;
3078 idx = findoption(buf);
3079 }
3080 if (idx >= 0)
3081 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3082}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003083#endif
3084
Bram Moolenaarf4140482020-02-15 23:06:45 +01003085#if defined(FEAT_EVAL)
3086/*
3087 * Apply the OptionSet autocommand.
3088 */
3089 static void
3090apply_optionset_autocmd(
3091 int opt_idx,
3092 long opt_flags,
3093 long oldval,
3094 long oldval_g,
3095 long newval,
3096 char *errmsg)
3097{
3098 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3099
3100 // Don't do this while starting up, failure or recursively.
3101 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3102 return;
3103
3104 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3105 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3106 oldval_g);
3107 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3108 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3109 (opt_flags & OPT_LOCAL) ? "local" : "global");
3110 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3111 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3112 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3113 if (opt_flags & OPT_LOCAL)
3114 {
3115 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3116 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3117 }
3118 if (opt_flags & OPT_GLOBAL)
3119 {
3120 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3121 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3122 }
3123 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3124 {
3125 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3126 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3127 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3128 }
3129 if (opt_flags & OPT_MODELINE)
3130 {
3131 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3132 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3133 }
3134 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3135 NULL, FALSE, NULL);
3136 reset_v_option_vars();
3137}
3138#endif
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140/*
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003141 * Process the updated 'compatible' option value.
3142 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003143 char *
3144did_set_compatible(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003145{
3146 compatible_set();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003147 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003148}
3149
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003150#if defined(FEAT_LANGMAP) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003151/*
3152 * Process the updated 'langremap' option value.
3153 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003154 char *
3155did_set_langremap(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003156{
3157 // 'langremap' -> !'langnoremap'
3158 p_lnr = !p_lrm;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003159 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003160}
3161
3162/*
3163 * Process the updated 'langnoremap' option value.
3164 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003165 char *
3166did_set_langnoremap(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003167{
3168 // 'langnoremap' -> !'langremap'
3169 p_lrm = !p_lnr;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003170 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003171}
3172#endif
3173
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003174#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003175/*
3176 * Process the updated 'undofile' option value.
3177 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003178 char *
3179did_set_undofile(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003180{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003181 // Only take action when the option was set.
3182 if (!curbuf->b_p_udf && !p_udf)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003183 return NULL;
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003184
3185 // When reset we do not delete the undo file, the option may be set again
3186 // without making any changes in between.
3187 char_u hash[UNDO_HASH_SIZE];
3188 buf_T *save_curbuf = curbuf;
3189
3190 FOR_ALL_BUFFERS(curbuf)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003191 {
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003192 // When 'undofile' is set globally: for every buffer, otherwise
3193 // only for the current buffer: Try to read in the undofile,
3194 // if one exists, the buffer wasn't changed and the buffer was
3195 // loaded
3196 if ((curbuf == save_curbuf
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003197 || (args->os_flags & OPT_GLOBAL)
3198 || args->os_flags == 0)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003199 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003200 {
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003201#ifdef FEAT_CRYPT
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003202 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
3203 continue;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003204#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003205 u_compute_hash(hash);
3206 u_read_undo(NULL, hash, curbuf->b_fname);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003207 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003208 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003209 curbuf = save_curbuf;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003210
3211 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003212}
3213#endif
3214
3215/*
3216 * Process the updated 'readonly' option value.
3217 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003218 char *
3219did_set_readonly(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003220{
3221 // when 'readonly' is reset globally, also reset readonlymode
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003222 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003223 readonlymode = FALSE;
3224
3225 // when 'readonly' is set may give W10 again
3226 if (curbuf->b_p_ro)
3227 curbuf->b_did_warn = FALSE;
3228
3229 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003230
3231 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003232}
3233
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003234#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003235/*
3236 * Process the updated 'mousehide' option value.
3237 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003238 char *
3239did_set_mousehide(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003240{
3241 if (!p_mh)
3242 gui_mch_mousehide(FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003243 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003244}
3245#endif
3246
3247/*
3248 * Process the updated 'modifiable' option value.
3249 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003250 char *
3251did_set_modifiable(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003252{
3253 // when 'modifiable' is changed, redraw the window title
3254
3255# ifdef FEAT_TERMINAL
3256 // Cannot set 'modifiable' when in Terminal mode.
3257 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3258 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3259 {
3260 curbuf->b_p_ma = FALSE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003261 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003262 return e_cannot_make_terminal_with_running_job_modifiable;
3263 }
3264# endif
3265 redraw_titles();
3266
3267 return NULL;
3268}
3269
3270/*
3271 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3272 * option value.
3273 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003274 char *
3275did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003276{
3277 // redraw the window title and tab page text
3278 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003279 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003280}
3281
3282/*
3283 * Process the updated 'binary' option value.
3284 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003285 char *
3286did_set_binary(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003287{
3288 // when 'bin' is set also set some other options
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003289 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003290 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003291
3292 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003293}
3294
3295/*
3296 * Process the updated 'buflisted' option value.
3297 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003298 char *
3299did_set_buflisted(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003300{
3301 // when 'buflisted' changes, trigger autocommands
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003302 if (args->os_oldval.boolean != curbuf->b_p_bl)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003303 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3304 NULL, NULL, TRUE, curbuf);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003305 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003306}
3307
3308/*
3309 * Process the updated 'swapfile' option value.
3310 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003311 char *
3312did_set_swapfile(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003313{
3314 // when 'swf' is set, create swapfile, when reset remove swapfile
3315 if (curbuf->b_p_swf && p_uc)
3316 ml_open_file(curbuf); // create the swap file
3317 else
3318 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3319 // buf->b_p_swf
3320 mf_close_file(curbuf, TRUE); // remove the swap file
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003321 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003322}
3323
3324/*
3325 * Process the updated 'terse' option value.
3326 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003327 char *
3328did_set_terse(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003329{
3330 char_u *p;
3331
3332 // when 'terse' is set change 'shortmess'
3333 p = vim_strchr(p_shm, SHM_SEARCH);
3334
3335 // insert 's' in p_shm
3336 if (p_terse && p == NULL)
3337 {
3338 STRCPY(IObuff, p_shm);
3339 STRCAT(IObuff, "s");
3340 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
3341 }
3342 // remove 's' from p_shm
3343 else if (!p_terse && p != NULL)
3344 STRMOVE(p, p + 1);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003345 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003346}
3347
3348/*
3349 * Process the updated 'paste' option value.
3350 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003351 char *
3352did_set_paste(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003353{
3354 // when 'paste' is set or reset also change other options
3355 paste_option_changed();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003356 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003357}
3358
3359/*
3360 * Process the updated 'insertmode' option value.
3361 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003362 char *
3363did_set_insertmode(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003364{
3365 // when 'insertmode' is set from an autocommand need to do work here
3366 if (p_im)
3367 {
3368 if ((State & MODE_INSERT) == 0)
3369 need_start_insertmode = TRUE;
3370 stop_insert_mode = FALSE;
3371 }
3372 // only reset if it was set previously
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003373 else if (args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003374 {
3375 need_start_insertmode = FALSE;
3376 stop_insert_mode = TRUE;
3377 if (restart_edit != 0 && mode_displayed)
3378 clear_cmdline = TRUE; // remove "(insert)"
3379 restart_edit = 0;
3380 }
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003381
3382 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003383}
3384
3385/*
3386 * Process the updated 'ignorecase' option value.
3387 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003388 char *
3389did_set_ignorecase(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003390{
3391 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3392 if (p_hls)
3393 redraw_all_later(UPD_SOME_VALID);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003394 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003395}
3396
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003397#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003398/*
3399 * Process the updated 'hlsearch' option value.
3400 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003401 char *
3402did_set_hlsearch(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003403{
3404 // when 'hlsearch' is set or reset: reset no_hlsearch
3405 set_no_hlsearch(FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003406 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003407}
3408#endif
3409
3410/*
3411 * Process the updated 'scrollbind' option value.
3412 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003413 char *
3414did_set_scrollbind(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003415{
3416 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3417 // at the end of normal_cmd()
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003418 if (!curwin->w_p_scb)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003419 return NULL;
3420
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003421 do_check_scrollbind(FALSE);
3422 curwin->w_scbind_pos = curwin->w_topline;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003423 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003424}
3425
3426#ifdef FEAT_QUICKFIX
3427/*
3428 * Process the updated 'previewwindow' option value.
3429 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003430 char *
3431did_set_previewwindow(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003432{
3433 if (!curwin->w_p_pvw)
3434 return NULL;
3435
3436 // There can be only one window with 'previewwindow' set.
3437 win_T *win;
3438
3439 FOR_ALL_WINDOWS(win)
3440 if (win->w_p_pvw && win != curwin)
3441 {
3442 curwin->w_p_pvw = FALSE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003443 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003444 return e_preview_window_already_exists;
3445 }
3446
3447 return NULL;
3448}
3449#endif
3450
3451/*
3452 * Process the updated 'smoothscroll' option value.
3453 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003454 char *
3455did_set_smoothscroll(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003456{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003457 if (curwin->w_p_sms)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003458 return NULL;
3459
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003460 curwin->w_skipcol = 0;
3461 changed_line_abv_curs();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003462 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003463}
3464
3465/*
3466 * Process the updated 'textmode' option value.
3467 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003468 char *
3469did_set_textmode(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003470{
3471 // when 'textmode' is set or reset also change 'fileformat'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003472 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
3473
3474 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003475}
3476
3477/*
3478 * Process the updated 'textauto' option value.
3479 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003480 char *
3481did_set_textauto(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003482{
3483 // when 'textauto' is set or reset also change 'fileformats'
3484 set_string_option_direct((char_u *)"ffs", -1,
3485 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003486 OPT_FREE | args->os_flags, 0);
3487
3488 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003489}
3490
3491/*
3492 * Process the updated 'lisp' option value.
3493 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003494 char *
3495did_set_lisp(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003496{
3497 // When 'lisp' option changes include/exclude '-' in keyword characters.
3498 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003499 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003500}
3501
3502/*
3503 * Process the updated 'title' or the 'icon' option value.
3504 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003505 char *
3506did_set_title_icon(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003507{
3508 // when 'title' changed, may need to change the title; same for 'icon'
3509 did_set_title();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003510 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003511}
3512
3513/*
3514 * Process the updated 'modified' option value.
3515 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003516 char *
3517did_set_modified(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003518{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003519 if (!args->os_newval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003520 save_file_ff(curbuf); // Buffer is unchanged
3521 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003522 modified_was_set = args->os_newval.boolean;
3523 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003524}
3525
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003526#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003527/*
3528 * Process the updated 'shellslash' option value.
3529 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003530 char *
3531did_set_shellslash(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003532{
3533 if (p_ssl)
3534 {
3535 psepc = '/';
3536 psepcN = '\\';
3537 pseps[0] = '/';
3538 }
3539 else
3540 {
3541 psepc = '\\';
3542 psepcN = '/';
3543 pseps[0] = '\\';
3544 }
3545
3546 // need to adjust the file name arguments and buffer names.
3547 buflist_slash_adjust();
3548 alist_slash_adjust();
3549# ifdef FEAT_EVAL
3550 scriptnames_slash_adjust();
3551# endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003552 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003553}
3554#endif
3555
3556/*
3557 * Process the updated 'wrap' option value.
3558 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003559 char *
3560did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003561{
3562 // If 'wrap' is set, set w_leftcol to zero.
3563 if (curwin->w_p_wrap)
3564 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003565 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003566}
3567
3568/*
3569 * Process the updated 'equalalways' option value.
3570 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003571 char *
3572did_set_equalalways(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003573{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003574 if (p_ea && !args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003575 win_equal(curwin, FALSE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003576
3577 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003578}
3579
3580/*
3581 * Process the updated 'weirdinvert' option value.
3582 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003583 char *
3584did_set_weirdinvert(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003585{
3586 // When 'weirdinvert' changed, set/reset 't_xs'.
3587 // Then set 'weirdinvert' according to value of 't_xs'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003588 if (p_wiv && !args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003589 T_XS = (char_u *)"y";
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003590 else if (!p_wiv && args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003591 T_XS = empty_option;
3592 p_wiv = (*T_XS != NUL);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003593
3594 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003595}
3596
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003597#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003598/*
3599 * Process the updated 'ballooneval' option value.
3600 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003601 char *
3602did_set_ballooneval(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003603{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003604 if (balloonEvalForTerm)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003605 return NULL;
3606
3607 if (p_beval && !args->os_oldval.boolean)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003608 gui_mch_enable_beval_area(balloonEval);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003609 else if (!p_beval && args->os_oldval.boolean)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003610 gui_mch_disable_beval_area(balloonEval);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003611
3612 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003613}
3614#endif
3615
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003616#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003617/*
3618 * Process the updated 'balloonevalterm' option value.
3619 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003620 char *
3621did_set_balloonevalterm(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003622{
3623 mch_bevalterm_changed();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003624 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003625}
3626#endif
3627
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003628#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003629/*
3630 * Process the updated 'autochdir' option value.
3631 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003632 char *
3633did_set_autochdir(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003634{
3635 // Change directories when the 'acd' option is set now.
3636 DO_AUTOCHDIR;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003637 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003638}
3639#endif
3640
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003641#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003642/*
3643 * Process the updated 'diff' option value.
3644 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003645 char *
3646did_set_diff(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003647{
3648 // May add or remove the buffer from the list of diff buffers.
3649 diff_buf_adjust(curwin);
3650# ifdef FEAT_FOLDING
3651 if (foldmethodIsDiff(curwin))
3652 foldUpdateAll(curwin);
3653# endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003654 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003655}
3656#endif
3657
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003658#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003659/*
3660 * Process the updated 'imdisable' option value.
3661 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003662 char *
3663did_set_imdisable(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003664{
3665 // Only de-activate it here, it will be enabled when changing mode.
3666 if (p_imdisable)
3667 im_set_active(FALSE);
3668 else if (State & MODE_INSERT)
3669 // When the option is set from an autocommand, it may need to take
3670 // effect right away.
3671 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003672 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003673}
3674#endif
3675
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003676#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003677/*
3678 * Process the updated 'spell' option value.
3679 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003680 char *
3681did_set_spell(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003682{
3683 if (curwin->w_p_spell)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003684 return parse_spelllang(curwin);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003685
3686 return NULL;
3687}
3688#endif
3689
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003690#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003691/*
3692 * Process the updated 'arabic' option value.
3693 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003694 char *
3695did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003696{
3697 char *errmsg = NULL;
3698
3699 if (curwin->w_p_arab)
3700 {
3701 /*
3702 * 'arabic' is set, handle various sub-settings.
3703 */
3704 if (!p_tbidi)
3705 {
3706 // set rightleft mode
3707 if (!curwin->w_p_rl)
3708 {
3709 curwin->w_p_rl = TRUE;
3710 changed_window_setting();
3711 }
3712
3713 // Enable Arabic shaping (major part of what Arabic requires)
3714 if (!p_arshape)
3715 {
3716 p_arshape = TRUE;
3717 redraw_later_clear();
3718 }
3719 }
3720
3721 // Arabic requires a utf-8 encoding, inform the user if it's not
3722 // set.
3723 if (STRCMP(p_enc, "utf-8") != 0)
3724 {
3725 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3726
3727 msg_source(HL_ATTR(HLF_W));
3728 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3729#ifdef FEAT_EVAL
3730 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3731#endif
3732 }
3733
3734 // set 'delcombine'
3735 p_deco = TRUE;
3736
3737# ifdef FEAT_KEYMAP
3738 // Force-set the necessary keymap for arabic
3739 errmsg = set_option_value((char_u *)"keymap",
3740 0L, (char_u *)"arabic", OPT_LOCAL);
3741# endif
3742 }
3743 else
3744 {
3745 /*
3746 * 'arabic' is reset, handle various sub-settings.
3747 */
3748 if (!p_tbidi)
3749 {
3750 // reset rightleft mode
3751 if (curwin->w_p_rl)
3752 {
3753 curwin->w_p_rl = FALSE;
3754 changed_window_setting();
3755 }
3756
3757 // 'arabicshape' isn't reset, it is a global option and
3758 // another window may still need it "on".
3759 }
3760
3761 // 'delcombine' isn't reset, it is a global option and another
3762 // window may still want it "on".
3763
3764# ifdef FEAT_KEYMAP
3765 // Revert to the default keymap
3766 curbuf->b_p_iminsert = B_IMODE_NONE;
3767 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3768# endif
3769 }
3770
3771 return errmsg;
3772}
3773#endif
3774
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003775/*
3776 * Process the updated 'number' or 'relativenumber' option value.
3777 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003778 char *
3779did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003780{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003781#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003782 if (gui.in_use
3783 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3784 && curbuf->b_signlist != NULL)
3785 {
3786 // If the 'number' or 'relativenumber' options are modified and
3787 // 'signcolumn' is set to 'number', then clear the screen for a full
3788 // refresh. Otherwise the sign icons are not displayed properly in the
3789 // number column. If the 'number' option is set and only the
3790 // 'relativenumber' option is toggled, then don't refresh the screen
3791 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003792 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003793 redraw_all_later(UPD_CLEAR);
3794 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003795#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003796 return NULL;
3797}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003798
3799#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003800 char *
3801did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003802{
3803# ifdef FEAT_VTP
3804 // Do not turn on 'tgc' when 24-bit colors are not supported.
3805 if (
3806# ifdef VIMDLL
3807 !gui.in_use && !gui.starting &&
3808# endif
3809 !has_vtp_working())
3810 {
3811 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003812 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003813 return e_24_bit_colors_are_not_supported_on_this_environment;
3814 }
3815 if (is_term_win32())
3816 swap_tcap();
3817# endif
3818# ifdef FEAT_GUI
3819 if (!gui.in_use && !gui.starting)
3820# endif
3821 highlight_gui_started();
3822# ifdef FEAT_VTP
3823 // reset t_Co
3824 if (is_term_win32())
3825 {
3826 control_console_color_rgb();
3827 set_termname(T_NAME);
3828 init_highlight(TRUE, FALSE);
3829 }
3830# endif
3831# ifdef FEAT_TERMINAL
3832 term_update_colors_all();
3833 term_update_palette_all();
3834 term_update_wincolor_all();
3835# endif
3836
3837 return NULL;
3838}
3839#endif
3840
3841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 * Set the value of a boolean option, and take care of side effects.
3843 * Returns NULL for success, or an error message for an error.
3844 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003845 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003846set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003847 int opt_idx, // index in options[] table
3848 char_u *varp, // pointer to the option variable
3849 int value, // new value
3850 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003853#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003854 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003855#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003856 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003858 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if ((secure
3860#ifdef HAVE_SANDBOX
3861 || sandbox != 0
3862#endif
3863 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003864 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003866#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003867 // Save the global value before changing anything. This is needed as for
3868 // a global-only option setting the "local value" in fact sets the global
3869 // value (since there is only one value).
3870 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3871 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
3872 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003873#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003875 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003877 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003878 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879#endif
3880
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003881#ifdef FEAT_GUI
3882 need_mouse_correct = TRUE;
3883#endif
3884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003885 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3887 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
3888
3889 /*
3890 * Handle side effects of changing a bool option.
3891 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003892 if (options[opt_idx].opt_did_set_cb != NULL)
3893 {
3894 optset_T args;
3895
3896 args.os_varp = varp;
3897 args.os_flags = opt_flags;
3898 args.os_oldval.boolean = old_value;
3899 args.os_newval.boolean = value;
3900 args.os_doskip = FALSE;
3901 errmsg = options[opt_idx].opt_did_set_cb(&args);
3902 if (args.os_doskip)
3903 return errmsg;
3904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003906 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 options[opt_idx].flags |= P_WAS_SET;
3909
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003910#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003911 apply_optionset_autocmd(opt_idx, opt_flags,
3912 (long)(old_value ? TRUE : FALSE),
3913 (long)(old_global_value ? TRUE : FALSE),
3914 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003915#endif
3916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003917 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003918 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003919 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003920 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003921
3922 if ((opt_flags & OPT_NO_REDRAW) == 0)
3923 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003925 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926}
3927
3928/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003929 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003931 char *
3932did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003934 long *pp = (long *)args->os_varp;
3935 char *errmsg = NULL;
3936
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003937 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003939 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003940 p_wh = 1;
3941 }
3942 if (p_wmh > p_wh)
3943 {
3944 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3945 p_wh = p_wmh;
3946 }
3947 if (p_hh < 0)
3948 {
3949 errmsg = e_argument_must_be_positive;
3950 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003953 // Change window height NOW
3954 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003956 if (pp == &p_wh && curwin->w_height < p_wh)
3957 win_setheight((int)p_wh);
3958 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3959 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003962 return errmsg;
3963}
3964
3965/*
3966 * Process the new 'winminheight' option value.
3967 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003968 char *
3969did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003970{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003971 char *errmsg = NULL;
3972
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003973 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003975 errmsg = e_argument_must_be_positive;
3976 p_wmh = 0;
3977 }
3978 if (p_wmh > p_wh)
3979 {
3980 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3981 p_wmh = p_wh;
3982 }
3983 win_setminheight();
3984
3985 return errmsg;
3986}
3987
3988/*
3989 * Process the new 'winwidth' option value.
3990 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003991 char *
3992did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003993{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003994 char *errmsg = NULL;
3995
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003996 if (p_wiw < 1)
3997 {
3998 errmsg = e_argument_must_be_positive;
3999 p_wiw = 1;
4000 }
4001 if (p_wmw > p_wiw)
4002 {
4003 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4004 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004006
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004007 // Change window width NOW
4008 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4009 win_setwidth((int)p_wiw);
4010
4011 return errmsg;
4012}
4013
4014/*
4015 * Process the new 'winminwidth' option value.
4016 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004017 char *
4018did_set_winminwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004019{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004020 char *errmsg = NULL;
4021
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004022 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004023 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004024 errmsg = e_argument_must_be_positive;
4025 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004026 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004027 if (p_wmw > p_wiw)
4028 {
4029 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4030 p_wmw = p_wiw;
4031 }
4032 win_setminwidth();
4033
4034 return errmsg;
4035}
4036
4037/*
4038 * Process the new 'laststatus' option value.
4039 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004040 char *
4041did_set_laststatus(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004042{
4043 last_status(FALSE); // (re)set last window status line
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004044 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004045}
4046
4047/*
4048 * Process the new 'showtabline' option value.
4049 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004050 char *
4051did_set_showtabline(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004052{
4053 shell_new_rows(); // recompute window positions and heights
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004054 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004055}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004057#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004058/*
4059 * Process the new 'linespace' option value.
4060 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004061 char *
4062did_set_linespace(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004063{
4064 // Recompute gui.char_height and resize the Vim window to keep the
4065 // same number of lines.
4066 if (gui.in_use && gui_mch_adjust_charheight() == OK)
4067 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004068 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004069}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070#endif
4071
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004072#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004073/*
4074 * Process the new 'foldlevel' option value.
4075 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004076 char *
4077did_set_foldlevel(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004078{
4079 if (curwin->w_p_fdl < 0)
4080 curwin->w_p_fdl = 0;
4081 newFoldLevel();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004082 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004085/*
4086 * Process the new 'foldminlines' option value.
4087 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004088 char *
4089did_set_foldminlines(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004090{
4091 foldUpdateAll(curwin);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004092 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004093}
4094
4095/*
4096 * Process the new 'foldnestmax' option value.
4097 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004098 char *
4099did_set_foldnestmax(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004100{
4101 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 foldUpdateAll(curwin);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004103 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004104}
4105
4106/*
4107 * Process the new 'foldcolumn' option value.
4108 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004109 char *
4110did_set_foldcolumn(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004111{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004112 char *errmsg = NULL;
4113
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004114 if (curwin->w_p_fdc < 0)
4115 {
4116 errmsg = e_argument_must_be_positive;
4117 curwin->w_p_fdc = 0;
4118 }
4119 else if (curwin->w_p_fdc > 12)
4120 {
4121 errmsg = e_invalid_argument;
4122 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004125 return errmsg;
4126}
4127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004129/*
4130 * Process the new 'shiftwidth' or the 'tabstop' option value.
4131 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004132 char *
4133did_set_shiftwidth_tabstop(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004134{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004135 long *pp = (long *)args->os_varp;
4136 char *errmsg = NULL;
4137
4138 if (curbuf->b_p_sw < 0)
4139 {
4140 errmsg = e_argument_must_be_positive;
4141#ifdef FEAT_VARTABS
4142 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4143 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4144 ? tabstop_first(curbuf->b_p_vts_array)
4145 : curbuf->b_p_ts;
4146#else
4147 curbuf->b_p_sw = curbuf->b_p_ts;
4148#endif
4149 }
4150
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004151#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004152 if (foldmethodIsIndent(curwin))
4153 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004154#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004155 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4156 // parse 'cinoptions'.
4157 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4158 parse_cino(curbuf);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004159
4160 return errmsg;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004163/*
4164 * Process the new 'maxcombine' option value.
4165 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004166 char *
4167did_set_maxcombine(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004168{
4169 if (p_mco > MAX_MCO)
4170 p_mco = MAX_MCO;
4171 else if (p_mco < 0)
4172 p_mco = 0;
4173 screenclear(); // will re-allocate the screen
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004174 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004175}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004176
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004177/*
4178 * Process the new 'iminsert' option value.
4179 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004180 char *
4181did_set_iminsert(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004182{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004183 char *errmsg = NULL;
4184
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004185 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004187 errmsg = e_invalid_argument;
4188 curbuf->b_p_iminsert = B_IMODE_NONE;
4189 }
4190 p_iminsert = curbuf->b_p_iminsert;
4191 if (termcap_active) // don't do this in the alternate screen
4192 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004193#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004194 // Show/unshow value of 'keymap' in status lines.
4195 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004197
4198 return errmsg;
4199}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004201#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004202/*
4203 * Process the new 'imstyle' option value.
4204 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004205 char *
4206did_set_imstyle(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004207{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004208 char *errmsg = NULL;
4209
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004210 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4211 errmsg = e_invalid_argument;
4212
4213 return errmsg;
4214}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004215#endif
4216
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004217/*
4218 * Process the new 'window' option value.
4219 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004220 char *
4221did_set_window(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004222{
4223 if (p_window < 1)
4224 p_window = 1;
4225 else if (p_window >= Rows)
4226 p_window = Rows - 1;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004227 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004228}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004229
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004230/*
4231 * Process the new 'imsearch' option value.
4232 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004233 char *
4234did_set_imsearch(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004235{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004236 char *errmsg = NULL;
4237
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004238 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004240 errmsg = e_invalid_argument;
4241 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004243 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004245 return errmsg;
4246}
4247
4248/*
4249 * Process the new 'titlelen' option value.
4250 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004251 char *
4252did_set_titlelen(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004253{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004254 long old_value = args->os_oldval.number;
4255 char *errmsg = NULL;
4256
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004257 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004258 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004260 errmsg = e_argument_must_be_positive;
4261 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004263 if (starting != NO_SCREEN && old_value != p_titlelen)
4264 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004266 return errmsg;
4267}
4268
4269/*
4270 * Process the new 'cmdheight' option value.
4271 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004272 char *
4273did_set_cmdheight(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004274{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004275 long old_value = args->os_oldval.number;
4276 char *errmsg = NULL;
4277
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004278 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004279 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004281 errmsg = e_argument_must_be_positive;
4282 p_ch = 1;
4283 }
4284 if (p_ch > Rows - min_rows() + 1)
4285 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004287 // Only compute the new window layout when startup has been
4288 // completed. Otherwise the frame sizes may be wrong.
4289 if ((p_ch != old_value
4290 || tabline_height() + topframe->fr_height != Rows - p_ch)
4291 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004293 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004295 )
4296 command_height();
4297
4298 return errmsg;
4299}
4300
4301/*
4302 * Process the new 'updatecount' option value.
4303 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004304 char *
4305did_set_updatecount(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004306{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004307 long old_value = args->os_oldval.number;
4308 char *errmsg = NULL;
4309
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004310 // when 'updatecount' changes from zero to non-zero, open swap files
4311 if (p_uc < 0)
4312 {
4313 errmsg = e_argument_must_be_positive;
4314 p_uc = 100;
4315 }
4316 if (p_uc && !old_value)
4317 ml_open_files();
4318
4319 return errmsg;
4320}
4321
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004322#if defined(FEAT_CONCEAL) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004323/*
4324 * Process the new 'conceallevel' option value.
4325 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004326 char *
4327did_set_conceallevel(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004328{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004329 char *errmsg = NULL;
4330
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004331 if (curwin->w_p_cole < 0)
4332 {
4333 errmsg = e_argument_must_be_positive;
4334 curwin->w_p_cole = 0;
4335 }
4336 else if (curwin->w_p_cole > 3)
4337 {
4338 errmsg = e_invalid_argument;
4339 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004342 return errmsg;
4343}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004346#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004347/*
4348 * Process the new 'pyxversion' option value.
4349 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004350 char *
4351did_set_pyxversion(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004352{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004353 char *errmsg = NULL;
4354
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004355 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4356 errmsg = e_invalid_argument;
4357
4358 return errmsg;
4359}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004360#endif
4361
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004362/*
4363 * Process the new global 'undolevels' option value.
4364 */
4365 static void
4366did_set_global_undolevels(long value, long old_value)
4367{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004368 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004369
4370 // use the old value, otherwise u_sync() may not work properly
4371 p_ul = old_value;
4372 u_sync(TRUE);
4373 p_ul = value;
4374}
4375
4376/*
4377 * Process the new buffer local 'undolevels' option value.
4378 */
4379 static void
4380did_set_buflocal_undolevels(long value, long old_value)
4381{
4382 // use the old value, otherwise u_sync() may not work properly
4383 curbuf->b_p_ul = old_value;
4384 u_sync(TRUE);
4385 curbuf->b_p_ul = value;
4386}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004388#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004389/*
4390 * Process the new 'numberwidth' option value.
4391 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004392 char *
4393did_set_numberwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004394{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004395 char *errmsg = NULL;
4396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004397 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004398 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004399 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004400 errmsg = e_argument_must_be_positive;
4401 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004402 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004403 if (curwin->w_p_nuw > 20)
4404 {
4405 errmsg = e_invalid_argument;
4406 curwin->w_p_nuw = 20;
4407 }
4408 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4409
4410 return errmsg;
4411}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004412#endif
4413
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004414/*
4415 * Process the new 'textwidth' option value.
4416 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004417 char *
4418did_set_textwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004419{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004420 char *errmsg = NULL;
4421
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004422 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004423 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004424 errmsg = e_argument_must_be_positive;
4425 curbuf->b_p_tw = 0;
4426 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004427#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004428 {
4429 win_T *wp;
4430 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004431
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004432 FOR_ALL_TAB_WINDOWS(tp, wp)
4433 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004434 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004435#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004436
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004437 return errmsg;
4438}
4439
4440/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004441 * Process the new 'undolevels' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004442 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004443 char *
4444did_set_undolevels(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004445{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004446 long *pp = (long *)args->os_varp;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004447
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004448 if (pp == &p_ul) // global 'undolevels'
4449 did_set_global_undolevels(args->os_newval.number,
4450 args->os_oldval.number);
4451 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4452 did_set_buflocal_undolevels(args->os_newval.number,
4453 args->os_oldval.number);
4454
4455 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004456}
4457
4458/*
4459 * Check the bounds of numeric options.
4460 */
4461 static char *
4462check_num_option_bounds(
4463 long *pp,
4464 long old_value,
4465 long old_Rows,
4466 long old_Columns,
4467 char *errbuf,
4468 size_t errbuflen,
4469 char *errmsg)
4470{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 if (Rows < min_rows() && full_screen)
4472 {
4473 if (errbuf != NULL)
4474 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004475 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004476 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 errmsg = errbuf;
4478 }
4479 Rows = min_rows();
4480 }
4481 if (Columns < MIN_COLUMNS && full_screen)
4482 {
4483 if (errbuf != NULL)
4484 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004485 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004486 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 errmsg = errbuf;
4488 }
4489 Columns = MIN_COLUMNS;
4490 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004491 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /*
4494 * If the screen (shell) height has been changed, assume it is the
4495 * physical screenheight.
4496 */
4497 if (old_Rows != Rows || old_Columns != Columns)
4498 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004499 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (updating_screen)
4501 *pp = old_value;
4502 else if (full_screen
4503#ifdef FEAT_GUI
4504 && !gui.starting
4505#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004506 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 set_shellsize((int)Columns, (int)Rows, TRUE);
4508 else
4509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004510 // Postpone the resizing; check the size and cmdline position for
4511 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 check_shellsize();
4513 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4514 cmdline_row = Rows - p_ch;
4515 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004516 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004517 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (curbuf->b_p_ts <= 0)
4521 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004522 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 curbuf->b_p_ts = 8;
4524 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004525 else if (curbuf->b_p_ts > TABSTOP_MAX)
4526 {
4527 errmsg = e_invalid_argument;
4528 curbuf->b_p_ts = 8;
4529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (p_tm < 0)
4531 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004532 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 p_tm = 0;
4534 }
4535 if ((curwin->w_p_scr <= 0
4536 || (curwin->w_p_scr > curwin->w_height
4537 && curwin->w_height > 0))
4538 && full_screen)
4539 {
4540 if (pp == &(curwin->w_p_scr))
4541 {
4542 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004543 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 win_comp_scroll(curwin);
4545 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004546 // If 'scroll' became invalid because of a side effect silently adjust
4547 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 else if (curwin->w_p_scr <= 0)
4549 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004550 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 curwin->w_p_scr = curwin->w_height;
4552 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004553 if (p_hi < 0)
4554 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004555 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004556 p_hi = 0;
4557 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004558 else if (p_hi > 10000)
4559 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004560 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004561 p_hi = 10000;
4562 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 if (p_re < 0 || p_re > 2)
4564 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004565 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004566 p_re = 0;
4567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (p_report < 0)
4569 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004570 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 p_report = 1;
4572 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004573 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004575 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 p_sj = Rows / 2;
4577 else
4578 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004579 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 p_sj = 1;
4581 }
4582 }
4583 if (p_so < 0 && full_screen)
4584 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004585 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 p_so = 0;
4587 }
4588 if (p_siso < 0 && full_screen)
4589 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004590 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 p_siso = 0;
4592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (p_cwh < 1)
4594 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004595 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 p_cwh = 1;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (p_ut < 0)
4599 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004600 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 p_ut = 2000;
4602 }
4603 if (p_ss < 0)
4604 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004605 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 p_ss = 0;
4607 }
4608
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004609 return errmsg;
4610}
4611
4612/*
4613 * Set the value of a number option, and take care of side effects.
4614 * Returns NULL for success, or an error message for an error.
4615 */
4616 static char *
4617set_num_option(
4618 int opt_idx, // index in options[] table
4619 char_u *varp, // pointer to the option variable
4620 long value, // new value
4621 char *errbuf, // buffer for error messages
4622 size_t errbuflen, // length of "errbuf"
4623 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4624 // OPT_MODELINE, etc.
4625{
4626 char *errmsg = NULL;
4627 long old_value = *(long *)varp;
4628#if defined(FEAT_EVAL)
4629 long old_global_value = 0; // only used when setting a local and
4630 // global option
4631#endif
4632 long old_Rows = Rows; // remember old Rows
4633 long old_Columns = Columns; // remember old Columns
4634 long *pp = (long *)varp;
4635
4636 // Disallow changing some options from secure mode.
4637 if ((secure
4638#ifdef HAVE_SANDBOX
4639 || sandbox != 0
4640#endif
4641 ) && (options[opt_idx].flags & P_SECURE))
4642 return e_not_allowed_here;
4643
4644#if defined(FEAT_EVAL)
4645 // Save the global value before changing anything. This is needed as for
4646 // a global-only option setting the "local value" in fact sets the global
4647 // value (since there is only one value).
4648 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4649 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4650 OPT_GLOBAL);
4651#endif
4652
4653 *pp = value;
4654#ifdef FEAT_EVAL
4655 // Remember where the option was set.
4656 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4657#endif
4658#ifdef FEAT_GUI
4659 need_mouse_correct = TRUE;
4660#endif
4661
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004662 // Invoke the option specific callback function to validate and apply the
4663 // new value.
4664 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004665 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004666 optset_T args;
4667
4668 args.os_varp = varp;
4669 args.os_flags = opt_flags;
4670 args.os_oldval.number = old_value;
4671 args.os_newval.number = value;
4672 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004673 }
4674
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004675 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004676 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4677 errbuf, errbuflen, errmsg);
4678
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004679 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4681 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4682
4683 options[opt_idx].flags |= P_WAS_SET;
4684
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004685#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004686 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4687 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004688#endif
4689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004690 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004691 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004692 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004693 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004694 if ((opt_flags & OPT_NO_REDRAW) == 0)
4695 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697 return errmsg;
4698}
4699
4700/*
4701 * Called after an option changed: check if something needs to be redrawn.
4702 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004704check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004706 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004707 int doclear = (flags & P_RCLR) == P_RCLR;
4708 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004710 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4714 changed_window_setting();
4715 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004716 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004717 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004718 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004719 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004720 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004722 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723}
4724
4725/*
4726 * Find index for option 'arg'.
4727 * Return -1 if not found.
4728 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004729 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004730findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 int opt_idx;
4733 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004734 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 int is_term_opt;
4736
4737 /*
4738 * For first call: Initialize the quick-access table.
4739 * It contains the index for the first option that starts with a certain
4740 * letter. There are 26 letters, plus the first "t_" option.
4741 */
4742 if (quick_tab[1] == 0)
4743 {
4744 p = options[0].fullname;
4745 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4746 {
4747 if (s[0] != p[0])
4748 {
4749 if (s[0] == 't' && s[1] == '_')
4750 quick_tab[26] = opt_idx;
4751 else
4752 quick_tab[CharOrdLow(s[0])] = opt_idx;
4753 }
4754 p = s;
4755 }
4756 }
4757
4758 /*
4759 * Check for name starting with an illegal character.
4760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 return -1;
4763
4764 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4765 if (is_term_opt)
4766 opt_idx = quick_tab[26];
4767 else
4768 opt_idx = quick_tab[CharOrdLow(arg[0])];
4769 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4770 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004771 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 break;
4773 }
4774 if (s == NULL && !is_term_opt)
4775 {
4776 opt_idx = quick_tab[CharOrdLow(arg[0])];
4777 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4778 {
4779 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 break;
4782 s = NULL;
4783 }
4784 }
4785 if (s == NULL)
4786 opt_idx = -1;
4787 return opt_idx;
4788}
4789
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004790#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4791 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792/*
4793 * Get the value for an option.
4794 *
4795 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004796 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004797 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004798 * String option: gov_string, *stringval gets allocated string.
4799 * Hidden Number option: gov_hidden_number.
4800 * Hidden Toggle option: gov_hidden_bool.
4801 * Hidden String option: gov_hidden_string.
4802 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004803 *
4804 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004806 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004807get_option_value(
4808 char_u *name,
4809 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004810 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004811 int *flagsp,
4812 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 int opt_idx;
4815 char_u *varp;
4816
4817 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004818 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004819 {
4820 int key;
4821
4822 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004823 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004824 {
4825 char_u key_name[2];
4826 char_u *p;
4827
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004828 if (flagsp != NULL)
4829 *flagsp = 0; // terminal option has no flags
4830
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004831 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004832 if (key < 0)
4833 {
4834 key_name[0] = KEY2TERMCAP0(key);
4835 key_name[1] = KEY2TERMCAP1(key);
4836 }
4837 else
4838 {
4839 key_name[0] = KS_KEY;
4840 key_name[1] = (key & 0xff);
4841 }
4842 p = find_termcode(key_name);
4843 if (p != NULL)
4844 {
4845 if (stringval != NULL)
4846 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004847 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004848 }
4849 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004850 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004853 varp = get_varp_scope(&(options[opt_idx]), scope);
4854
4855 if (flagsp != NULL)
4856 // Return the P_xxxx option flags.
4857 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 if (options[opt_idx].flags & P_STRING)
4860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004862 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (stringval != NULL)
4864 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004865 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004866 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4867 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004870 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004871 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004874 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 *stringval = vim_strsave(*(char_u **)(varp));
4876 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004877 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004880 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004881 return (options[opt_idx].flags & P_NUM)
4882 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (options[opt_idx].flags & P_NUM)
4884 *numval = *(long *)varp;
4885 else
4886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004887 // Special case: 'modified' is b_changed, but we also want to consider
4888 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 if ((int *)varp == &curbuf->b_changed)
4890 *numval = curbufIsChanged();
4891 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004892 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004894 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895}
4896#endif
4897
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004898#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004899/*
4900 * Returns the option attributes and its value. Unlike the above function it
4901 * will return either global value or local value of the option depending on
4902 * what was requested, but it will never return global value if it was
4903 * requested to return local one and vice versa. Neither it will return
4904 * buffer-local value if it was requested to return window-local one.
4905 *
4906 * Pretends that option is absent if it is not present in the requested scope
4907 * (i.e. has no global, window-local or buffer-local value depending on
4908 * opt_type). Uses
4909 *
4910 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004911 * 0 hidden or unknown option, also option that does not have requested
4912 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004913 * see SOPT_* in vim.h for other flags
4914 *
4915 * Possible opt_type values: see SREQ_* in vim.h
4916 */
4917 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004918get_option_value_strict(
4919 char_u *name,
4920 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004921 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004922 int opt_type,
4923 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004924{
4925 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004926 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004927 struct vimoption *p;
4928 int r = 0;
4929
4930 opt_idx = findoption(name);
4931 if (opt_idx < 0)
4932 return 0;
4933
4934 p = &(options[opt_idx]);
4935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004936 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004937 if (p->var == NULL)
4938 return 0;
4939
4940 if (p->flags & P_BOOL)
4941 r |= SOPT_BOOL;
4942 else if (p->flags & P_NUM)
4943 r |= SOPT_NUM;
4944 else if (p->flags & P_STRING)
4945 r |= SOPT_STRING;
4946
4947 if (p->indir == PV_NONE)
4948 {
4949 if (opt_type == SREQ_GLOBAL)
4950 r |= SOPT_GLOBAL;
4951 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004953 }
4954 else
4955 {
4956 if (p->indir & PV_BOTH)
4957 r |= SOPT_GLOBAL;
4958 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004960
4961 if (p->indir & PV_WIN)
4962 {
4963 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004964 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004965 else
4966 r |= SOPT_WIN;
4967 }
4968 else if (p->indir & PV_BUF)
4969 {
4970 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004971 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004972 else
4973 r |= SOPT_BUF;
4974 }
4975 }
4976
4977 if (stringval == NULL)
4978 return r;
4979
4980 if (opt_type == SREQ_GLOBAL)
4981 varp = p->var;
4982 else
4983 {
4984 if (opt_type == SREQ_BUF)
4985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004986 // Special case: 'modified' is b_changed, but we also want to
4987 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004988 if (p->indir == PV_MOD)
4989 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004990 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004991 varp = NULL;
4992 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004993# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004994 else if (p->indir == PV_KEY)
4995 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004996 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004997 *stringval = NULL;
4998 varp = NULL;
4999 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005000# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005001 else
5002 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005003 buf_T *save_curbuf = curbuf;
5004
5005 // only getting a pointer, no need to use aucmd_prepbuf()
5006 curbuf = (buf_T *)from;
5007 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005008 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005009 curbuf = save_curbuf;
5010 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005011 }
5012 }
5013 else if (opt_type == SREQ_WIN)
5014 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005015 win_T *save_curwin = curwin;
5016
5017 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005018 curbuf = curwin->w_buffer;
5019 varp = get_varp(p);
5020 curwin = save_curwin;
5021 curbuf = curwin->w_buffer;
5022 }
5023 if (varp == p->var)
5024 return (r | SOPT_UNSET);
5025 }
5026
5027 if (varp != NULL)
5028 {
5029 if (p->flags & P_STRING)
5030 *stringval = vim_strsave(*(char_u **)(varp));
5031 else if (p->flags & P_NUM)
5032 *numval = *(long *) varp;
5033 else
5034 *numval = *(int *)varp;
5035 }
5036
5037 return r;
5038}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005039
5040/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005041 * Iterate over options. First argument is a pointer to a pointer to a
5042 * structure inside options[] array, second is option type like in the above
5043 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005044 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005045 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005046 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005047 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005048 * first argument to NULL.
5049 *
5050 * Returns full option name for current option on each call.
5051 */
5052 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005053option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005054{
5055 struct vimoption *ret = NULL;
5056 do
5057 {
5058 if (*option == NULL)
5059 *option = (void *) options;
5060 else if (((struct vimoption *) (*option))->fullname == NULL)
5061 {
5062 *option = NULL;
5063 return NULL;
5064 }
5065 else
5066 *option = (void *) (((struct vimoption *) (*option)) + 1);
5067
5068 ret = ((struct vimoption *) (*option));
5069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005070 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005071 if (ret->var == NULL)
5072 {
5073 ret = NULL;
5074 continue;
5075 }
5076
5077 switch (opt_type)
5078 {
5079 case SREQ_GLOBAL:
5080 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5081 ret = NULL;
5082 break;
5083 case SREQ_BUF:
5084 if (!(ret->indir & PV_BUF))
5085 ret = NULL;
5086 break;
5087 case SREQ_WIN:
5088 if (!(ret->indir & PV_WIN))
5089 ret = NULL;
5090 break;
5091 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005092 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005093 return NULL;
5094 }
5095 }
5096 while (ret == NULL);
5097
5098 return (char_u *)ret->fullname;
5099}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005100#endif
5101
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005103 * Return the flags for the option at 'opt_idx'.
5104 */
5105 long_u
5106get_option_flags(int opt_idx)
5107{
5108 return options[opt_idx].flags;
5109}
5110
5111/*
5112 * Set a flag for the option at 'opt_idx'.
5113 */
5114 void
5115set_option_flag(int opt_idx, long_u flag)
5116{
5117 options[opt_idx].flags |= flag;
5118}
5119
5120/*
5121 * Clear a flag for the option at 'opt_idx'.
5122 */
5123 void
5124clear_option_flag(int opt_idx, long_u flag)
5125{
5126 options[opt_idx].flags &= ~flag;
5127}
5128
5129/*
5130 * Returns TRUE if the option at 'opt_idx' is a global option
5131 */
5132 int
5133is_global_option(int opt_idx)
5134{
5135 return options[opt_idx].indir == PV_NONE;
5136}
5137
5138/*
5139 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5140 * local value.
5141 */
5142 int
5143is_global_local_option(int opt_idx)
5144{
5145 return options[opt_idx].indir & PV_BOTH;
5146}
5147
5148/*
5149 * Returns TRUE if the option at 'opt_idx' is a window-local option
5150 */
5151 int
5152is_window_local_option(int opt_idx)
5153{
5154 return options[opt_idx].var == VAR_WIN;
5155}
5156
5157/*
5158 * Returns TRUE if the option at 'opt_idx' is a hidden option
5159 */
5160 int
5161is_hidden_option(int opt_idx)
5162{
5163 return options[opt_idx].var == NULL;
5164}
5165
5166#if defined(FEAT_CRYPT) || defined(PROTO)
5167/*
5168 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5169 */
5170 int
5171is_crypt_key_option(int opt_idx)
5172{
5173 return options[opt_idx].indir == PV_KEY;
5174}
5175#endif
5176
5177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 * Set the value of option "name".
5179 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005180 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005181 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005183 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005184set_option_value(
5185 char_u *name,
5186 long number,
5187 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005188 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 int opt_idx;
5191 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005192 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005193 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005196 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005197 {
5198 int key;
5199
5200 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005201 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005202 {
5203 char_u key_name[2];
5204
5205 if (key < 0)
5206 {
5207 key_name[0] = KEY2TERMCAP0(key);
5208 key_name[1] = KEY2TERMCAP1(key);
5209 }
5210 else
5211 {
5212 key_name[0] = KS_KEY;
5213 key_name[1] = (key & 0xff);
5214 }
5215 add_termcode(key_name, string, FALSE);
5216 if (full_screen)
5217 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005218 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005219 return NULL;
5220 }
5221
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005222 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 else
5225 {
5226 flags = options[opt_idx].flags;
5227#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005228 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005230 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005231 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005232 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005235 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005236 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005237
5238 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5239 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005241 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005243 int idx;
5244
5245 // Either we are given a string or we are setting option
5246 // to zero.
5247 for (idx = 0; string[idx] == '0'; ++idx)
5248 ;
5249 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005250 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005251 // There's another character after zeros or the string
5252 // is empty. In both cases, we are trying to set a
5253 // num option using a string.
5254 semsg(_(e_number_required_after_str_equal_str),
5255 name, string);
5256 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005257
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005260 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005261 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005262 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005263 errbuf, sizeof(errbuf), opt_flags);
5264 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005265 else
5266 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005270 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271}
5272
5273/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005274 * Call set_option_value() and when an error is returned report it.
5275 */
5276 void
5277set_option_value_give_err(
5278 char_u *name,
5279 long number,
5280 char_u *string,
5281 int opt_flags) // OPT_LOCAL or 0 (both)
5282{
5283 char *errmsg = set_option_value(name, number, string, opt_flags);
5284
5285 if (errmsg != NULL)
5286 emsg(_(errmsg));
5287}
5288
5289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 * Get the terminal code for a terminal option.
5291 * Returns NULL when not found.
5292 */
5293 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005294get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 int opt_idx;
5297 char_u *varp;
5298
5299 if (tname[0] != 't' || tname[1] != '_' ||
5300 tname[2] == NUL || tname[3] == NUL)
5301 return NULL;
5302 if ((opt_idx = findoption(tname)) >= 0)
5303 {
5304 varp = get_varp(&(options[opt_idx]));
5305 if (varp != NULL)
5306 varp = *(char_u **)(varp);
5307 return varp;
5308 }
5309 return find_termcode(tname + 2);
5310}
5311
5312 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 int i;
5316
5317 i = findoption((char_u *)"hl");
5318 if (i >= 0)
5319 return options[i].def_val[VI_DEFAULT];
5320 return (char_u *)NULL;
5321}
5322
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005323 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005324get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005325{
5326 int i;
5327
5328 i = findoption((char_u *)"enc");
5329 if (i >= 0)
5330 return options[i].def_val[VI_DEFAULT];
5331 return (char_u *)NULL;
5332}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005333
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005334#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005335 int
5336is_option_allocated(char *name)
5337{
5338 int idx = findoption((char_u *)name);
5339
5340 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5341}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005342#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005343
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005344#if defined(FEAT_EVAL) || defined(PROTO)
5345/*
5346 * Return TRUE if "name" is a string option.
5347 * Returns FALSE if option "name" does not exist.
5348 */
5349 int
5350is_string_option(char_u *name)
5351{
5352 int idx = findoption(name);
5353
5354 return idx >= 0 && (options[idx].flags & P_STRING);
5355}
5356#endif
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358/*
5359 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005360 * When "has_lt" is true there is a '<' before "*arg_arg".
5361 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 */
5363 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005364find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005366 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005368 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 /*
5371 * Don't use get_special_key_code() for t_xx, we don't want it to call
5372 * add_termcap_entry().
5373 */
5374 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5375 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005376 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005378 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005380 key = find_special_key(&arg, &modifiers,
5381 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005382 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 key = 0;
5384 }
5385 return key;
5386}
5387
5388/*
5389 * if 'all' == 0: show changed options
5390 * if 'all' == 1: show all normal options
5391 * if 'all' == 2: show all terminal options
5392 */
5393 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005394showoptions(
5395 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005396 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 struct vimoption *p;
5399 int col;
5400 int isterm;
5401 char_u *varp;
5402 struct vimoption **items;
5403 int item_count;
5404 int run;
5405 int row, rows;
5406 int cols;
5407 int i;
5408 int len;
5409
5410#define INC 20
5411#define GAP 3
5412
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005413 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (items == NULL)
5415 return;
5416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005417 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005419 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005421 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005423 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005425 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005428 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * 1. display the short items
5430 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005431 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 */
5433 for (run = 1; run <= 2 && !got_int; ++run)
5434 {
5435 /*
5436 * collect the items in items[]
5437 */
5438 item_count = 0;
5439 for (p = &options[0]; p->fullname != NULL; p++)
5440 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005441 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005442 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005443 continue;
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 varp = NULL;
5446 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005447 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 if (p->indir != PV_NONE && !isterm)
5450 varp = get_varp_scope(p, opt_flags);
5451 }
5452 else
5453 varp = get_varp(p);
5454 if (varp != NULL
5455 && ((all == 2 && isterm)
5456 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005457 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005459 if (opt_flags & OPT_ONECOLUMN)
5460 len = Columns;
5461 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005462 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 else
5464 {
5465 option_value2string(p, opt_flags);
5466 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5467 }
5468 if ((len <= INC - GAP && run == 1) ||
5469 (len > INC - GAP && run == 2))
5470 items[item_count++] = p;
5471 }
5472 }
5473
5474 /*
5475 * display the items
5476 */
5477 if (run == 1)
5478 {
5479 cols = (Columns + GAP - 3) / INC;
5480 if (cols == 0)
5481 cols = 1;
5482 rows = (item_count + cols - 1) / cols;
5483 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005484 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 rows = item_count;
5486 for (row = 0; row < rows && !got_int; ++row)
5487 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005488 msg_putchar('\n'); // go to next line
5489 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 break;
5491 col = 0;
5492 for (i = row; i < item_count; i += rows)
5493 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005494 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 showoneopt(items[i], opt_flags);
5496 col += INC;
5497 }
5498 out_flush();
5499 ui_breakcheck();
5500 }
5501 }
5502 vim_free(items);
5503}
5504
5505/*
5506 * Return TRUE if option "p" has its default value.
5507 */
5508 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005509optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 int dvi;
5512
5513 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005514 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005515 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005517 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005519 // the cast to long is required for Manx C, long_i is
5520 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005521 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005522 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5524}
5525
5526/*
5527 * showoneopt: show the value of one option
5528 * must not be called with a hidden option!
5529 */
5530 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005531showoneopt(
5532 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005533 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005535 char_u *varp;
5536 int save_silent = silent_mode;
5537
5538 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005539 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
5541 varp = get_varp_scope(p, opt_flags);
5542
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005543 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5545 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005546 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005548 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005550 msg_puts(" ");
5551 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 if (!(p->flags & P_BOOL))
5553 {
5554 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005555 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 option_value2string(p, opt_flags);
5557 msg_outtrans(NameBuff);
5558 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005559
5560 silent_mode = save_silent;
5561 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563
5564/*
5565 * Write modified options as ":set" commands to a file.
5566 *
5567 * There are three values for "opt_flags":
5568 * OPT_GLOBAL: Write global option values and fresh values of
5569 * buffer-local options (used for start of a session
5570 * file).
5571 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5572 * curwin (used for a vimrc file).
5573 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5574 * and local values for window-local options of
5575 * curwin. Local values are also written when at the
5576 * default value, because a modeline or autocommand
5577 * may have set them when doing ":edit file" and the
5578 * user has set them back at the default or fresh
5579 * value.
5580 * When "local_only" is TRUE, don't write fresh
5581 * values, only local values (for ":mkview").
5582 * (fresh value = value used for a new buffer or window for a local option).
5583 *
5584 * Return FAIL on error, OK otherwise.
5585 */
5586 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005587makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
5589 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005590 char_u *varp; // currently used value
5591 char_u *varp_fresh; // local value
5592 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 char *cmd;
5594 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005595 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
5597 /*
5598 * The options that don't have a default (terminal name, columns, lines)
5599 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005600 * Do the loop over "options[]" twice: once for options with the
5601 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005603 for (pri = 1; pri >= 0; --pri)
5604 {
5605 for (p = &options[0]; !istermoption(p); p++)
5606 if (!(p->flags & P_NO_MKRC)
5607 && !istermoption(p)
5608 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005610 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5612 continue;
5613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5615 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5617 continue;
5618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005619 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005621 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 continue;
5623
Bram Moolenaard23b7142021-04-17 21:04:34 +02005624 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5625 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005626 continue;
5627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 round = 2;
5629 if (p->indir != PV_NONE)
5630 {
5631 if (p->var == VAR_WIN)
5632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 if (!(opt_flags & OPT_LOCAL))
5635 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005636 // When fresh value of window-local option is not at the
5637 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5639 {
5640 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005641 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 round = 1;
5644 varp_local = varp;
5645 varp = varp_fresh;
5646 }
5647 }
5648 }
5649 }
5650
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005651 // Round 1: fresh value for window-local options.
5652 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 for ( ; round <= 2; varp = varp_local, ++round)
5654 {
5655 if (round == 1 || (opt_flags & OPT_GLOBAL))
5656 cmd = "set";
5657 else
5658 cmd = "setlocal";
5659
5660 if (p->flags & P_BOOL)
5661 {
5662 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5663 return FAIL;
5664 }
5665 else if (p->flags & P_NUM)
5666 {
5667 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5668 return FAIL;
5669 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005670 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005672 int do_endif = FALSE;
5673
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005674 // Don't set 'syntax' and 'filetype' again if the value is
5675 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005676 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005677#if defined(FEAT_SYN_HL)
5678 p->indir == PV_SYN ||
5679#endif
5680 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
5682 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5683 *(char_u **)(varp)) < 0
5684 || put_eol(fd) < 0)
5685 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005686 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005689 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005691 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
5693 if (put_line(fd, "endif") == FAIL)
5694 return FAIL;
5695 }
5696 }
5697 }
5698 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return OK;
5701}
5702
5703#if defined(FEAT_FOLDING) || defined(PROTO)
5704/*
5705 * Generate set commands for the local fold options only. Used when
5706 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5707 */
5708 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005709makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005711 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005713 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 == FAIL
5715# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005716 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005718 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 == FAIL
5720 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5721 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5722 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5723 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5724 )
5725 return FAIL;
5726
5727 return OK;
5728}
5729#endif
5730
5731 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005732put_setstring(
5733 FILE *fd,
5734 char *cmd,
5735 char *name,
5736 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005737 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005740 char_u *buf = NULL;
5741 char_u *part = NULL;
5742 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5745 return FAIL;
5746 if (*valuep != NULL)
5747 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005748 // Output 'pastetoggle' as key names. For other
5749 // options some characters have to be escaped with
5750 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 if (valuep == &p_pt)
5752 {
5753 s = *valuep;
5754 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005755 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 return FAIL;
5757 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005758 // expand the option value, replace $HOME by ~
5759 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005761 int size = (int)STRLEN(*valuep) + 1;
5762
5763 // replace home directory in the whole option value into "buf"
5764 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005765 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005766 goto fail;
5767 home_replace(NULL, *valuep, buf, size, FALSE);
5768
5769 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005770 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005771 // can be expanded when read back.
5772 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5773 && vim_strchr(*valuep, ',') != NULL)
5774 {
5775 part = alloc(size);
5776 if (part == NULL)
5777 goto fail;
5778
5779 // write line break to clear the option, e.g. ':set rtp='
5780 if (put_eol(fd) == FAIL)
5781 goto fail;
5782
5783 p = buf;
5784 while (*p != NUL)
5785 {
5786 // for each comma separated option part, append value to
5787 // the option, :set rtp+=value
5788 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5789 goto fail;
5790 (void)copy_option_part(&p, part, size, ",");
5791 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5792 goto fail;
5793 }
5794 vim_free(buf);
5795 vim_free(part);
5796 return OK;
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005799 {
5800 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005802 }
5803 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
5805 else if (put_escstr(fd, *valuep, 2) == FAIL)
5806 return FAIL;
5807 }
5808 if (put_eol(fd) < 0)
5809 return FAIL;
5810 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005811fail:
5812 vim_free(buf);
5813 vim_free(part);
5814 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815}
5816
5817 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005818put_setnum(
5819 FILE *fd,
5820 char *cmd,
5821 char *name,
5822 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 long wc;
5825
5826 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5827 return FAIL;
5828 if (wc_use_keyname((char_u *)valuep, &wc))
5829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005830 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5832 return FAIL;
5833 }
5834 else if (fprintf(fd, "%ld", *valuep) < 0)
5835 return FAIL;
5836 if (put_eol(fd) < 0)
5837 return FAIL;
5838 return OK;
5839}
5840
5841 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005842put_setbool(
5843 FILE *fd,
5844 char *cmd,
5845 char *name,
5846 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005848 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005849 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5851 || put_eol(fd) < 0)
5852 return FAIL;
5853 return OK;
5854}
5855
5856/*
5857 * Clear all the terminal options.
5858 * If the option has been allocated, free the memory.
5859 * Terminal options are never hidden or indirect.
5860 */
5861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005862clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 /*
5865 * Reset a few things before clearing the old options. This may cause
5866 * outputting a few things that the terminal doesn't understand, but the
5867 * screen will be cleared later, so this is OK.
5868 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005869 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005870 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005872 // When starting the GUI close the display opened for the clipboard.
5873 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (gui.starting)
5875 clear_xterm_clip();
5876#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005877 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005879 free_termoptions();
5880}
5881
5882 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005883free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005884{
5885 struct vimoption *p;
5886
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005887 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 if (istermoption(p))
5889 {
5890 if (p->flags & P_ALLOCED)
5891 free_string_option(*(char_u **)(p->var));
5892 if (p->flags & P_DEF_ALLOCED)
5893 free_string_option(p->def_val[VI_DEFAULT]);
5894 *(char_u **)(p->var) = empty_option;
5895 p->def_val[VI_DEFAULT] = empty_option;
5896 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005897#ifdef FEAT_EVAL
5898 // remember where the option was cleared
5899 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902 clear_termcodes();
5903}
5904
5905/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005906 * Free the string for one term option, if it was allocated.
5907 * Set the string to empty_option and clear allocated flag.
5908 * "var" points to the option value.
5909 */
5910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005911free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005912{
5913 struct vimoption *p;
5914
5915 for (p = &options[0]; p->fullname != NULL; p++)
5916 if (p->var == var)
5917 {
5918 if (p->flags & P_ALLOCED)
5919 free_string_option(*(char_u **)(p->var));
5920 *(char_u **)(p->var) = empty_option;
5921 p->flags &= ~P_ALLOCED;
5922 break;
5923 }
5924}
5925
5926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 * Set the terminal option defaults to the current value.
5928 * Used after setting the terminal name.
5929 */
5930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005931set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932{
5933 struct vimoption *p;
5934
5935 for (p = &options[0]; p->fullname != NULL; p++)
5936 {
5937 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5938 {
5939 if (p->flags & P_DEF_ALLOCED)
5940 {
5941 free_string_option(p->def_val[VI_DEFAULT]);
5942 p->flags &= ~P_DEF_ALLOCED;
5943 }
5944 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5945 if (p->flags & P_ALLOCED)
5946 {
5947 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005948 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 }
5950 }
5951 }
5952}
5953
5954/*
5955 * return TRUE if 'p' starts with 't_'
5956 */
5957 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005958istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5961}
5962
Bram Moolenaardac13472019-09-16 21:06:21 +02005963/*
5964 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5965 */
5966 int
5967istermoption_idx(int opt_idx)
5968{
5969 return istermoption(&options[opt_idx]);
5970}
5971
Bram Moolenaar113e1072019-01-20 15:30:40 +01005972#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005974 * Unset local option value, similar to ":set opt<".
5975 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005976 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005977unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005978{
5979 struct vimoption *p;
5980 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005981 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005982
5983 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005984 if (opt_idx < 0)
5985 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005986 p = &(options[opt_idx]);
5987
5988 switch ((int)p->indir)
5989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005990 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005991 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005992 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005993 break;
5994 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005995 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005996 break;
5997 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005998 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005999 break;
6000 case PV_AR:
6001 buf->b_p_ar = -1;
6002 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006003 case PV_BKC:
6004 clear_string_option(&buf->b_p_bkc);
6005 buf->b_bkc_flags = 0;
6006 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006007 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006008 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006009 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006010 case PV_TC:
6011 clear_string_option(&buf->b_p_tc);
6012 buf->b_tc_flags = 0;
6013 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006014 case PV_SISO:
6015 curwin->w_p_siso = -1;
6016 break;
6017 case PV_SO:
6018 curwin->w_p_so = -1;
6019 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006020# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006021 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006022 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006023 break;
6024 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006025 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006026 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006027# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006028 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006029 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006030 break;
6031 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006032 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006033 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006034# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006035 case PV_TSRFU:
6036 clear_string_option(&buf->b_p_tsrfu);
6037 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006038# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006039 case PV_FP:
6040 clear_string_option(&buf->b_p_fp);
6041 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006042# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006043 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006044 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006045 break;
6046 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006047 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006048 break;
6049 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006050 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006051 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006052# endif
6053# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006054 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006055 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006056 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006057# endif
6058# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006059 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006060 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006061 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006062# endif
6063# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006064 case PV_SBR:
6065 clear_string_option(&((win_T *)from)->w_p_sbr);
6066 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006067# endif
6068# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006069 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006070 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006071 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006072# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006073 case PV_UL:
6074 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6075 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006076 case PV_LW:
6077 clear_string_option(&buf->b_p_lw);
6078 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006079 case PV_MENC:
6080 clear_string_option(&buf->b_p_menc);
6081 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006082 case PV_LCS:
6083 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006084 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006085 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006086 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006087 case PV_FCS:
6088 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006089 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006090 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006091 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006092 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006093 clear_string_option(&((win_T *)from)->w_p_ve);
6094 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006095 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006096 }
6097}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006098#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006099
6100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006102 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 */
6104 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006105get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006107 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 {
6109 if (p->var == VAR_WIN)
6110 return (char_u *)GLOBAL_WO(get_varp(p));
6111 return p->var;
6112 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006113 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 {
6115 switch ((int)p->indir)
6116 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006117 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006119 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6120 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6121 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006123 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6124 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6125 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006126 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006127 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006128 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006129 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6130 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006132 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6133 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006135 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6136 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006137#ifdef FEAT_COMPL_FUNC
6138 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6139#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006140#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6141 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6142#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006143#if defined(FEAT_CRYPT)
6144 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6145#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006146#ifdef FEAT_LINEBREAK
6147 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6148#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006149#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006150 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006151#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006152 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006153 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006154 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006155 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006156 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006157 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006158 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006161 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 }
6163 return get_varp(p);
6164}
6165
6166/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006167 * Get pointer to option variable at 'opt_idx', depending on local or global
6168 * scope.
6169 */
6170 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006171get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006172{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006173 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006174}
6175
6176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 * Get pointer to option variable.
6178 */
6179 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006180get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006182 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (p->var == NULL)
6184 return NULL;
6185
6186 switch ((int)p->indir)
6187 {
6188 case PV_NONE: return p->var;
6189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006190 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006191 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006193 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006195 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006197 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006199 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006201 case PV_TC: return *curbuf->b_p_tc != NUL
6202 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006203 case PV_BKC: return *curbuf->b_p_bkc != NUL
6204 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006205 case PV_SISO: return curwin->w_p_siso >= 0
6206 ? (char_u *)&(curwin->w_p_siso) : p->var;
6207 case PV_SO: return curwin->w_p_so >= 0
6208 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006210 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006212 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6214#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006215 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006217 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006219#ifdef FEAT_COMPL_FUNC
6220 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6221 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6222#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006223 case PV_FP: return *curbuf->b_p_fp != NUL
6224 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006226 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006228 case PV_GP: return *curbuf->b_p_gp != NUL
6229 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6230 case PV_MP: return *curbuf->b_p_mp != NUL
6231 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006233#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6234 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6235 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6236#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006237#if defined(FEAT_CRYPT)
6238 case PV_CM: return *curbuf->b_p_cm != NUL
6239 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6240#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006241#ifdef FEAT_LINEBREAK
6242 case PV_SBR: return *curwin->w_p_sbr != NUL
6243 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6244#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006245#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006246 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006247 ? (char_u *)&(curwin->w_p_stl) : p->var;
6248#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006249 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6250 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006251 case PV_LW: return *curbuf->b_p_lw != NUL
6252 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006253 case PV_MENC: return *curbuf->b_p_menc != NUL
6254 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255#ifdef FEAT_ARABIC
6256 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6257#endif
6258 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006259 case PV_LCS: return *curwin->w_p_lcs != NUL
6260 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006261 case PV_FCS: return *curwin->w_p_fcs != NUL
6262 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006263 case PV_VE: return *curwin->w_p_ve != NUL
6264 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006265#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006266 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6267#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006268#ifdef FEAT_SYN_HL
6269 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6270 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006271 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006272 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274#ifdef FEAT_DIFF
6275 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6276#endif
6277#ifdef FEAT_FOLDING
6278 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6279 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6280 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6281 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6282 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6283 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6284 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6285# ifdef FEAT_EVAL
6286 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6287 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6288# endif
6289 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6290#endif
6291 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006292 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006293#ifdef FEAT_LINEBREAK
6294 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006297 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006298#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6300#endif
6301#ifdef FEAT_RIGHTLEFT
6302 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6303 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6304#endif
6305 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006306 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6308#ifdef FEAT_LINEBREAK
6309 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006310 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6311 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006313 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006315 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006316#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006317 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6318 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6319#endif
6320#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006321 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6322 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6323 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6327 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6330 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6332 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6334 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6335 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006336 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339#ifdef FEAT_FOLDING
6340 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006343#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006344 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006346#ifdef FEAT_COMPL_FUNC
6347 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006348 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006349#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006350#ifdef FEAT_EVAL
6351 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6352#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006353 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006355 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006361 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6363 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6364 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6365 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6366#ifdef FEAT_FIND_ID
6367# ifdef FEAT_EVAL
6368 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6369# endif
6370#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006371#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6373 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6374#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006375#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006376 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378#ifdef FEAT_CRYPT
6379 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006382 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6384 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6385 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6386 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6387 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006389 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6396#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006397 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006398 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6399#endif
6400#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006401 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6402 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6403 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006404 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405#endif
6406 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6407 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6408 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6409 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006410#ifdef FEAT_PERSISTENT_UNDO
6411 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6414#ifdef FEAT_KEYMAP
6415 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6416#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006417#ifdef FEAT_SIGNS
6418 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6419#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006420#ifdef FEAT_VARTABS
6421 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6422 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6423#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006424 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006426 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 return (char_u *)&(curbuf->b_p_wm);
6428}
6429
6430/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006431 * Return a pointer to the variable for option at 'opt_idx'
6432 */
6433 char_u *
6434get_option_var(int opt_idx)
6435{
6436 return options[opt_idx].var;
6437}
6438
Dominique Pelle748b3082022-01-08 12:41:16 +00006439#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006440/*
6441 * Return the full name of the option at 'opt_idx'
6442 */
6443 char_u *
6444get_option_fullname(int opt_idx)
6445{
6446 return (char_u *)options[opt_idx].fullname;
6447}
Dominique Pelle748b3082022-01-08 12:41:16 +00006448#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006449
6450/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006451 * Return the did_set callback function for the option at 'opt_idx'
6452 */
6453 opt_did_set_cb_T
6454get_option_did_set_cb(int opt_idx)
6455{
6456 return options[opt_idx].opt_did_set_cb;
6457}
6458
6459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * Get the value of 'equalprg', either the buffer-local one or the global one.
6461 */
6462 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006463get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 if (*curbuf->b_p_ep == NUL)
6466 return p_ep;
6467 return curbuf->b_p_ep;
6468}
6469
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470/*
6471 * Copy options from one window to another.
6472 * Used when splitting a window.
6473 */
6474 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006475win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6478 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006479 after_copy_winopt(wp_to);
6480}
6481
6482/*
6483 * After copying window options: update variables depending on options.
6484 */
6485 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006486after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006487{
6488#ifdef FEAT_LINEBREAK
6489 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006490#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006491#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006492 fill_culopt_flags(NULL, wp);
6493 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006494#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006495 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6496 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006497}
6498
6499 static char_u *
6500copy_option_val(char_u *val)
6501{
6502 if (val == empty_option)
6503 return empty_option; // no need to allocate memory
6504 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507/*
6508 * Copy the options from one winopt_T to another.
6509 * Doesn't free the old option values in "to", use clear_winopt() for that.
6510 * The 'scroll' option is not copied, because it depends on the window height.
6511 * The 'previewwindow' option is reset, there can be only one preview window.
6512 */
6513 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006514copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516#ifdef FEAT_ARABIC
6517 to->wo_arab = from->wo_arab;
6518#endif
6519 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006520 to->wo_lcs = copy_option_val(from->wo_lcs);
6521 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006523 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006524 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006525 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006526#ifdef FEAT_LINEBREAK
6527 to->wo_nuw = from->wo_nuw;
6528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529#ifdef FEAT_RIGHTLEFT
6530 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006531 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006533#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006534 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006535#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006536#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006537 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006540#ifdef FEAT_DIFF
6541 to->wo_wrap_save = from->wo_wrap_save;
6542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543#ifdef FEAT_LINEBREAK
6544 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006545 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006546 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006548 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006550 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006551 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006552 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006553 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006554#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006555 to->wo_spell = from->wo_spell;
6556#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006557#ifdef FEAT_SYN_HL
6558 to->wo_cuc = from->wo_cuc;
6559 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006560 to->wo_culopt = copy_option_val(from->wo_culopt);
6561 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563#ifdef FEAT_DIFF
6564 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006565 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006567#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006568 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006569 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006570#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006571#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006572 to->wo_twk = copy_option_val(from->wo_twk);
6573 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575#ifdef FEAT_FOLDING
6576 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006577 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006579 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006580 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 to->wo_fml = from->wo_fml;
6582 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006583 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006584 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006585 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006586 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 to->wo_fdn = from->wo_fdn;
6588# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006589 to->wo_fde = copy_option_val(from->wo_fde);
6590 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006592 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006594#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006595 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006596#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006597
6598#ifdef FEAT_EVAL
6599 // Copy the script context so that we know where the value was last set.
6600 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6601 sizeof(to->wo_script_ctx));
6602#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006603 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604}
6605
6606/*
6607 * Check string options in a window for a NULL value.
6608 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006610check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 check_winopt(&win->w_onebuf_opt);
6613 check_winopt(&win->w_allbuf_opt);
6614}
6615
6616/*
6617 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6618 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006620check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622#ifdef FEAT_FOLDING
6623 check_string_option(&wop->wo_fdi);
6624 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006625 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626# ifdef FEAT_EVAL
6627 check_string_option(&wop->wo_fde);
6628 check_string_option(&wop->wo_fdt);
6629# endif
6630 check_string_option(&wop->wo_fmr);
6631#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006632#ifdef FEAT_SIGNS
6633 check_string_option(&wop->wo_scl);
6634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635#ifdef FEAT_RIGHTLEFT
6636 check_string_option(&wop->wo_rlc);
6637#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006638#ifdef FEAT_LINEBREAK
6639 check_string_option(&wop->wo_sbr);
6640#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006641#ifdef FEAT_STL_OPT
6642 check_string_option(&wop->wo_stl);
6643#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006644#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006645 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006646 check_string_option(&wop->wo_cc);
6647#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006648#ifdef FEAT_CONCEAL
6649 check_string_option(&wop->wo_cocu);
6650#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006651#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006652 check_string_option(&wop->wo_twk);
6653 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006654#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006655#ifdef FEAT_LINEBREAK
6656 check_string_option(&wop->wo_briopt);
6657#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006658 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006659 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006660 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006661 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662}
6663
6664/*
6665 * Free the allocated memory inside a winopt_T.
6666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006668clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670#ifdef FEAT_FOLDING
6671 clear_string_option(&wop->wo_fdi);
6672 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006673 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674# ifdef FEAT_EVAL
6675 clear_string_option(&wop->wo_fde);
6676 clear_string_option(&wop->wo_fdt);
6677# endif
6678 clear_string_option(&wop->wo_fmr);
6679#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006680#ifdef FEAT_SIGNS
6681 clear_string_option(&wop->wo_scl);
6682#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006683#ifdef FEAT_LINEBREAK
6684 clear_string_option(&wop->wo_briopt);
6685#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006686 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687#ifdef FEAT_RIGHTLEFT
6688 clear_string_option(&wop->wo_rlc);
6689#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006690#ifdef FEAT_LINEBREAK
6691 clear_string_option(&wop->wo_sbr);
6692#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006693#ifdef FEAT_STL_OPT
6694 clear_string_option(&wop->wo_stl);
6695#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006696#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006697 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006698 clear_string_option(&wop->wo_cc);
6699#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006700#ifdef FEAT_CONCEAL
6701 clear_string_option(&wop->wo_cocu);
6702#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006703#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006704 clear_string_option(&wop->wo_twk);
6705 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006706#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006707 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006708 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006709 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710}
6711
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006712#ifdef FEAT_EVAL
6713// Index into the options table for a buffer-local option enum.
6714static int buf_opt_idx[BV_COUNT];
6715# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6716
6717/*
6718 * Initialize buf_opt_idx[] if not done already.
6719 */
6720 static void
6721init_buf_opt_idx(void)
6722{
6723 static int did_init_buf_opt_idx = FALSE;
6724 int i;
6725
6726 if (did_init_buf_opt_idx)
6727 return;
6728 did_init_buf_opt_idx = TRUE;
6729 for (i = 0; !istermoption_idx(i); i++)
6730 if (options[i].indir & PV_BUF)
6731 buf_opt_idx[options[i].indir & PV_MASK] = i;
6732}
6733#else
6734# define COPY_OPT_SCTX(buf, bv)
6735#endif
6736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737/*
6738 * Copy global option values to local options for one buffer.
6739 * Used when creating a new buffer and sometimes when entering a buffer.
6740 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006741 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6743 * appropriate.
6744 * BCO_NOHELP Don't copy the values to a help buffer.
6745 */
6746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006747buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006750 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 int dont_do_help;
6752 int did_isk = FALSE;
6753
6754 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 * Skip this when the option defaults have not been set yet. Happens when
6756 * main() allocates the first buffer.
6757 */
6758 if (p_cpo != NULL)
6759 {
6760 /*
6761 * Always copy when entering and 'cpo' contains 'S'.
6762 * Don't copy when already initialized.
6763 * Don't copy when 'cpo' contains 's' and not entering.
6764 * 'S' BCO_ENTER initialized 's' should_copy
6765 * yes yes X X TRUE
6766 * yes no yes X FALSE
6767 * no X yes X FALSE
6768 * X no no yes FALSE
6769 * X no no no TRUE
6770 * no yes no X TRUE
6771 */
6772 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6773 && (buf->b_p_initialized
6774 || (!(flags & BCO_ENTER)
6775 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6776 should_copy = FALSE;
6777
6778 if (should_copy || (flags & BCO_ALWAYS))
6779 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006780#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006781 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006782 init_buf_opt_idx();
6783#endif
6784 // Don't copy the options specific to a help buffer when
6785 // BCO_NOHELP is given or the options were initialized already
6786 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6788 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006789 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 save_p_isk = buf->b_p_isk;
6792 buf->b_p_isk = NULL;
6793 }
6794 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006795 * Always free the allocated strings. If not already initialized,
6796 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 */
6798 if (!buf->b_p_initialized)
6799 {
6800 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006801 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006804 switch (*p_ffs)
6805 {
6806 case 'm':
6807 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6808 case 'd':
6809 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6810 case 'u':
6811 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6812 default:
6813 buf->b_p_ff = vim_strsave(p_ff);
6814 }
6815 if (buf->b_p_ff != NULL)
6816 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 buf->b_p_bh = empty_option;
6818 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820 else
6821 free_buf_options(buf, FALSE);
6822
6823 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006824 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 buf->b_p_ai_nopaste = p_ai_nopaste;
6826 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006827 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006829 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 buf->b_p_tw_nopaste = p_tw_nopaste;
6831 buf->b_p_tw_nobin = p_tw_nobin;
6832 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006833 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 buf->b_p_wm_nopaste = p_wm_nopaste;
6835 buf->b_p_wm_nobin = p_wm_nobin;
6836 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006837 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006838 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006839 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006840 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006841 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006843 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006845 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006847 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 buf->b_p_ml_nobin = p_ml_nobin;
6849 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006850 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006851 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006852 buf->b_p_swf = FALSE;
6853 else
6854 {
6855 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006856 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006859 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006860#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006861 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006862 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006864#ifdef FEAT_COMPL_FUNC
6865 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006866 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006867 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006868 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006869 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006870 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006871#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006872#ifdef FEAT_EVAL
6873 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006874 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006875 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006878 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006880#ifdef FEAT_VARTABS
6881 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006882 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006883 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006884 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006885 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006886 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006887 buf->b_p_vsts_nopaste = p_vsts_nopaste
6888 ? vim_strsave(p_vsts_nopaste) : NULL;
6889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006891 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006893 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894#ifdef FEAT_FOLDING
6895 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006896 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897#endif
6898 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006899 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006900 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006901 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006902 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6903 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006905 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006907 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006909 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006911 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006914 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006916 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006918 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006919 buf->b_p_cinsd = vim_strsave(p_cinsd);
6920 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006921 buf->b_p_lop = vim_strsave(p_lop);
6922 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006923
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006924 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006927 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006929 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006931 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006933 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006935 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006936 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006937 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006938#endif
6939#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006940 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006941 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006942 (void)compile_cap_prog(&buf->b_s);
6943 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006944 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006945 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006946 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006947 buf->b_s.b_p_spo = vim_strsave(p_spo);
6948 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006950#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006952 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006954 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006956 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006957#if defined(FEAT_EVAL)
6958 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006959 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961#ifdef FEAT_CRYPT
6962 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006963 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006966 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967#ifdef FEAT_KEYMAP
6968 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 buf->b_kmap_state |= KEYMAP_INIT;
6971#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006972#ifdef FEAT_TERMINAL
6973 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006974 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006975#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006976 // This isn't really an option, but copying the langmap and IME
6977 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006979 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006981 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006983 // options that are normally global but also have a local value
6984 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006986 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006987 buf->b_p_bkc = empty_option;
6988 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#ifdef FEAT_QUICKFIX
6990 buf->b_p_gp = empty_option;
6991 buf->b_p_mp = empty_option;
6992 buf->b_p_efm = empty_option;
6993#endif
6994 buf->b_p_ep = empty_option;
6995 buf->b_p_kp = empty_option;
6996 buf->b_p_path = empty_option;
6997 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006998 buf->b_p_tc = empty_option;
6999 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000#ifdef FEAT_FIND_ID
7001 buf->b_p_def = empty_option;
7002 buf->b_p_inc = empty_option;
7003# ifdef FEAT_EVAL
7004 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007005 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006# endif
7007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 buf->b_p_dict = empty_option;
7009 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007010#ifdef FEAT_COMPL_FUNC
7011 buf->b_p_tsrfu = empty_option;
7012#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007013 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007015#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7016 buf->b_p_bexpr = empty_option;
7017#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007018#if defined(FEAT_CRYPT)
7019 buf->b_p_cm = empty_option;
7020#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007021#ifdef FEAT_PERSISTENT_UNDO
7022 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007023 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007024#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007025 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007026 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
7028 /*
7029 * Don't copy the options set by ex_help(), use the saved values,
7030 * when going from a help buffer to a non-help buffer.
7031 * Don't touch these at all when BCO_NOHELP is used and going from
7032 * or to a help buffer.
7033 */
7034 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007037#ifdef FEAT_VARTABS
7038 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007039 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007040 else
7041 buf->b_p_vts_array = NULL;
7042#endif
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 else
7045 {
7046 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007047 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 did_isk = TRUE;
7049 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007050 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007051#ifdef FEAT_VARTABS
7052 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007054 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007055 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007056 else
7057 buf->b_p_vts_array = NULL;
7058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 if (buf->b_p_bt[0] == 'h')
7061 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007063 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065 }
7066
7067 /*
7068 * When the options should be copied (ignoring BCO_ALWAYS), set the
7069 * flag that indicates that the options have been initialized.
7070 */
7071 if (should_copy)
7072 buf->b_p_initialized = TRUE;
7073 }
7074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007075 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 if (did_isk)
7077 (void)buf_init_chartab(buf, FALSE);
7078}
7079
7080/*
7081 * Reset the 'modifiable' option and its default value.
7082 */
7083 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007084reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
7086 int opt_idx;
7087
7088 curbuf->b_p_ma = FALSE;
7089 p_ma = FALSE;
7090 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007091 if (opt_idx >= 0)
7092 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093}
7094
7095/*
7096 * Set the global value for 'iminsert' to the local value.
7097 */
7098 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007099set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 p_iminsert = curbuf->b_p_iminsert;
7102}
7103
7104/*
7105 * Set the global value for 'imsearch' to the local value.
7106 */
7107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007108set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 p_imsearch = curbuf->b_p_imsearch;
7111}
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113static int expand_option_idx = -1;
7114static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7115static int expand_option_flags = 0;
7116
7117 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007118set_context_in_set_cmd(
7119 expand_T *xp,
7120 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007121 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 long_u flags = 0; // init for GCC
7125 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 char_u *p;
7127 char_u *s;
7128 int is_term_option = FALSE;
7129 int key;
7130
7131 expand_option_flags = opt_flags;
7132
7133 xp->xp_context = EXPAND_SETTINGS;
7134 if (*arg == NUL)
7135 {
7136 xp->xp_pattern = arg;
7137 return;
7138 }
7139 p = arg + STRLEN(arg) - 1;
7140 if (*p == ' ' && *(p - 1) != '\\')
7141 {
7142 xp->xp_pattern = p + 1;
7143 return;
7144 }
7145 while (p > arg)
7146 {
7147 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (*p == ' ' || *p == ',')
7150 {
7151 while (s > arg && *(s - 1) == '\\')
7152 --s;
7153 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007154 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (*p == ' ' && ((p - s) & 1) == 0)
7156 {
7157 ++p;
7158 break;
7159 }
7160 --p;
7161 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007162 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
7164 xp->xp_context = EXPAND_BOOL_SETTINGS;
7165 p += 2;
7166 }
7167 if (STRNCMP(p, "inv", 3) == 0)
7168 {
7169 xp->xp_context = EXPAND_BOOL_SETTINGS;
7170 p += 3;
7171 }
7172 xp->xp_pattern = arg = p;
7173 if (*arg == '<')
7174 {
7175 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 return;
7178 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
7181 xp->xp_context = EXPAND_NOTHING;
7182 return;
7183 }
7184 nextchar = *++p;
7185 is_term_option = TRUE;
7186 expand_option_name[2] = KEY2TERMCAP0(key);
7187 expand_option_name[3] = KEY2TERMCAP1(key);
7188 }
7189 else
7190 {
7191 if (p[0] == 't' && p[1] == '_')
7192 {
7193 p += 2;
7194 if (*p != NUL)
7195 ++p;
7196 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007197 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 nextchar = *++p;
7199 is_term_option = TRUE;
7200 expand_option_name[2] = p[-2];
7201 expand_option_name[3] = p[-1];
7202 }
7203 else
7204 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007205 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7207 p++;
7208 if (*p == NUL)
7209 return;
7210 nextchar = *p;
7211 *p = NUL;
7212 opt_idx = findoption(arg);
7213 *p = nextchar;
7214 if (opt_idx == -1 || options[opt_idx].var == NULL)
7215 {
7216 xp->xp_context = EXPAND_NOTHING;
7217 return;
7218 }
7219 flags = options[opt_idx].flags;
7220 if (flags & P_BOOL)
7221 {
7222 xp->xp_context = EXPAND_NOTHING;
7223 return;
7224 }
7225 }
7226 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007227 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7229 {
7230 ++p;
7231 nextchar = '=';
7232 }
7233 if ((nextchar != '=' && nextchar != ':')
7234 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7235 {
7236 xp->xp_context = EXPAND_UNSUCCESSFUL;
7237 return;
7238 }
7239 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7240 {
7241 xp->xp_context = EXPAND_OLD_SETTING;
7242 if (is_term_option)
7243 expand_option_idx = -1;
7244 else
7245 expand_option_idx = opt_idx;
7246 xp->xp_pattern = p + 1;
7247 return;
7248 }
7249 xp->xp_context = EXPAND_NOTHING;
7250 if (is_term_option || (flags & P_NUM))
7251 return;
7252
7253 xp->xp_pattern = p + 1;
7254
7255 if (flags & P_EXPAND)
7256 {
7257 p = options[opt_idx].var;
7258 if (p == (char_u *)&p_bdir
7259 || p == (char_u *)&p_dir
7260 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007261 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264#ifdef FEAT_SESSION
7265 || p == (char_u *)&p_vdir
7266#endif
7267 )
7268 {
7269 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007270 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 xp->xp_backslash = XP_BS_THREE;
7272 else
7273 xp->xp_backslash = XP_BS_ONE;
7274 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007275 else if (p == (char_u *)&p_ft)
7276 {
7277 xp->xp_context = EXPAND_FILETYPE;
7278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 else
7280 {
7281 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007282 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (p == (char_u *)&p_tags)
7284 xp->xp_backslash = XP_BS_THREE;
7285 else
7286 xp->xp_backslash = XP_BS_ONE;
7287 }
7288 }
7289
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007290 // For an option that is a list of file names, find the start of the
7291 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007294 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (*p == ' ' || *p == ',')
7296 {
7297 s = p;
7298 while (s > xp->xp_pattern && *(s - 1) == '\\')
7299 --s;
7300 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7301 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7302 {
7303 xp->xp_pattern = p + 1;
7304 break;
7305 }
7306 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007307
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007308#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007310 if (options[opt_idx].var == (char_u *)&p_sps
7311 && STRNCMP(p, "file:", 5) == 0)
7312 {
7313 xp->xp_pattern = p + 5;
7314 break;
7315 }
7316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318}
7319
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007320/*
7321 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7322 *
7323 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7324 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7325 *
7326 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7327 * regular expression 'regmatch', then stores the match in matches[idx] and
7328 * returns TRUE.
7329 *
7330 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7331 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7332 *
7333 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7334 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7335 */
7336 static int
7337match_str(
7338 char_u *str,
7339 regmatch_T *regmatch,
7340 char_u **matches,
7341 int idx,
7342 int test_only,
7343 int fuzzy,
7344 char_u *fuzzystr,
7345 fuzmatch_str_T *fuzmatch)
7346{
7347 if (!fuzzy)
7348 {
7349 if (vim_regexec(regmatch, str, (colnr_T)0))
7350 {
7351 if (!test_only)
7352 matches[idx] = vim_strsave(str);
7353 return TRUE;
7354 }
7355 }
7356 else
7357 {
7358 int score;
7359
7360 score = fuzzy_match_str(str, fuzzystr);
7361 if (score != 0)
7362 {
7363 if (!test_only)
7364 {
7365 fuzmatch[idx].idx = idx;
7366 fuzmatch[idx].str = vim_strsave(str);
7367 fuzmatch[idx].score = score;
7368 }
7369
7370 return TRUE;
7371 }
7372 }
7373
7374 return FALSE;
7375}
7376
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007378ExpandSettings(
7379 expand_T *xp,
7380 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007381 char_u *fuzzystr,
7382 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007383 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007384 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007386 int num_normal = 0; // Nr of matching non-term-code settings
7387 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int opt_idx;
7389 int match;
7390 int count = 0;
7391 char_u *str;
7392 int loop;
7393 int is_term_opt;
7394 char_u name_buf[MAX_KEY_NAME_LEN];
7395 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007396 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007397 int fuzzy;
7398 fuzmatch_str_T *fuzmatch = NULL;
7399
Christian Brabandtcb747892022-05-08 21:10:56 +01007400 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007402 // do this loop twice:
7403 // loop == 0: count the number of matching options
7404 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 for (loop = 0; loop <= 1; ++loop)
7406 {
7407 regmatch->rm_ic = ic;
7408 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7409 {
K.Takataeeec2542021-06-02 13:28:16 +02007410 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007411 {
7412 if (match_str((char_u *)names[match], regmatch, *matches,
7413 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
7415 if (loop == 0)
7416 num_normal++;
7417 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007418 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 }
7422 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7423 opt_idx++)
7424 {
7425 if (options[opt_idx].var == NULL)
7426 continue;
7427 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7428 && !(options[opt_idx].flags & P_BOOL))
7429 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007430 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (is_term_opt && num_normal > 0)
7432 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007433
7434 if (match_str(str, regmatch, *matches, count, (loop == 0),
7435 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
7437 if (loop == 0)
7438 {
7439 if (is_term_opt)
7440 num_term++;
7441 else
7442 num_normal++;
7443 }
7444 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007445 count++;
7446 }
7447 else if (!fuzzy && options[opt_idx].shortname != NULL
7448 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007449 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007450 {
7451 // Compare against the abbreviated option name (for regular
7452 // expression match). Fuzzy matching (previous if) already
7453 // matches against both the expanded and abbreviated names.
7454 if (loop == 0)
7455 {
7456 if (is_term_opt)
7457 num_term++;
7458 else
7459 num_normal++;
7460 }
7461 else
7462 (*matches)[count++] = vim_strsave(str);
7463 }
7464 else if (is_term_opt)
7465 {
7466 name_buf[0] = '<';
7467 name_buf[1] = 't';
7468 name_buf[2] = '_';
7469 name_buf[3] = str[2];
7470 name_buf[4] = str[3];
7471 name_buf[5] = '>';
7472 name_buf[6] = NUL;
7473
7474 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7475 fuzzy, fuzzystr, fuzmatch))
7476 {
7477 if (loop == 0)
7478 num_term++;
7479 else
7480 count++;
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
7483 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007484
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 /*
7486 * Check terminal key codes, these are not in the option table
7487 */
7488 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7489 {
7490 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7491 {
7492 if (!isprint(str[0]) || !isprint(str[1]))
7493 continue;
7494
7495 name_buf[0] = 't';
7496 name_buf[1] = '_';
7497 name_buf[2] = str[0];
7498 name_buf[3] = str[1];
7499 name_buf[4] = NUL;
7500
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007501 if (match_str(name_buf, regmatch, *matches, count,
7502 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7503 {
7504 if (loop == 0)
7505 num_term++;
7506 else
7507 count++;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 else
7510 {
7511 name_buf[0] = '<';
7512 name_buf[1] = 't';
7513 name_buf[2] = '_';
7514 name_buf[3] = str[0];
7515 name_buf[4] = str[1];
7516 name_buf[5] = '>';
7517 name_buf[6] = NUL;
7518
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007519 if (match_str(name_buf, regmatch, *matches, count,
7520 (loop == 0), fuzzy, fuzzystr,
7521 fuzmatch))
7522 {
7523 if (loop == 0)
7524 num_term++;
7525 else
7526 count++;
7527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529 }
7530
7531 /*
7532 * Check special key names.
7533 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007534 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7536 {
7537 name_buf[0] = '<';
7538 STRCPY(name_buf + 1, str);
7539 STRCAT(name_buf, ">");
7540
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007541 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7542 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
7544 if (loop == 0)
7545 num_term++;
7546 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007547 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 }
7549 }
7550 }
7551 if (loop == 0)
7552 {
7553 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007554 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007556 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 else
7558 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007559 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007561 *matches = ALLOC_MULT(char_u *, *numMatches);
7562 if (*matches == NULL)
7563 {
7564 *matches = (char_u **)"";
7565 return FAIL;
7566 }
7567 }
7568 else
7569 {
7570 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7571 if (fuzmatch == NULL)
7572 {
7573 *matches = (char_u **)"";
7574 return FAIL;
7575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577 }
7578 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007579
7580 if (fuzzy &&
7581 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7582 return FAIL;
7583
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 return OK;
7585}
7586
7587 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007588ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007590 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 char_u *buf;
7592
zeertzjqb0d45ec2023-01-25 15:04:22 +00007593 *numMatches = 0;
7594 *matches = ALLOC_ONE(char_u *);
7595 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 return FAIL;
7597
7598 /*
7599 * For a terminal key code expand_option_idx is < 0.
7600 */
7601 if (expand_option_idx < 0)
7602 {
7603 var = find_termcode(expand_option_name + 2);
7604 if (var == NULL)
7605 expand_option_idx = findoption(expand_option_name);
7606 }
7607
7608 if (expand_option_idx >= 0)
7609 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007610 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 option_value2string(&options[expand_option_idx], expand_option_flags);
7612 var = NameBuff;
7613 }
7614 else if (var == NULL)
7615 var = (char_u *)"";
7616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007617 // A backslash is required before some characters. This is the reverse of
7618 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 buf = vim_strsave_escaped(var, escape_chars);
7620
7621 if (buf == NULL)
7622 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007623 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 return FAIL;
7625 }
7626
7627#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007628 // For MS-Windows et al. we don't double backslashes at the start and
7629 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007630 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 if (var[0] == '\\' && var[1] == '\\'
7632 && expand_option_idx >= 0
7633 && (options[expand_option_idx].flags & P_EXPAND)
7634 && vim_isfilec(var[2])
7635 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007636 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637#endif
7638
zeertzjqb0d45ec2023-01-25 15:04:22 +00007639 *matches[0] = buf;
7640 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 return OK;
7642}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
7644/*
7645 * Get the value for the numeric or string option *opp in a nice format into
7646 * NameBuff[]. Must not be called with a hidden option!
7647 */
7648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007649option_value2string(
7650 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007651 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 char_u *varp;
7654
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007655 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
7657 if (opp->flags & P_NUM)
7658 {
7659 long wc = 0;
7660
7661 if (wc_use_keyname(varp, &wc))
7662 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7663 else if (wc != 0)
7664 STRCPY(NameBuff, transchar((int)wc));
7665 else
7666 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7667 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007668 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
7670 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007671 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 NameBuff[0] = NUL;
7673#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007674 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007675 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 STRCPY(NameBuff, "*****");
7677#endif
7678 else if (opp->flags & P_EXPAND)
7679 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007680 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 else if ((char_u **)opp->var == &p_pt)
7682 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7683 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007684 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686}
7687
7688/*
7689 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7690 * printed as a keyname.
7691 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7692 */
7693 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007694wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7697 {
7698 *wcp = *(long *)varp;
7699 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7700 return TRUE;
7701 }
7702 return FALSE;
7703}
7704
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 * Return TRUE if "x" is present in 'shortmess' option, or
7707 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7708 */
7709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007710shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007712 return p_shm != NULL &&
7713 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 || (vim_strchr(p_shm, 'a') != NULL
7715 && vim_strchr((char_u *)SHM_A, x) != NULL));
7716}
7717
7718/*
7719 * paste_option_changed() - Called after p_paste was set or reset.
7720 */
7721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007722paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723{
7724 static int old_p_paste = FALSE;
7725 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007726 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728#ifdef FEAT_RIGHTLEFT
7729 static int save_ri = 0;
7730 static int save_hkmap = 0;
7731#endif
7732 buf_T *buf;
7733
7734 if (p_paste)
7735 {
7736 /*
7737 * Paste switched from off to on.
7738 * Save the current values, so they can be restored later.
7739 */
7740 if (!old_p_paste)
7741 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007742 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007743 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {
7745 buf->b_p_tw_nopaste = buf->b_p_tw;
7746 buf->b_p_wm_nopaste = buf->b_p_wm;
7747 buf->b_p_sts_nopaste = buf->b_p_sts;
7748 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007749 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007750#ifdef FEAT_VARTABS
7751 if (buf->b_p_vsts_nopaste)
7752 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007753 buf->b_p_vsts_nopaste =
7754 buf->b_p_vsts && buf->b_p_vsts != empty_option
7755 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007759 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007761 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763#ifdef FEAT_RIGHTLEFT
7764 save_ri = p_ri;
7765 save_hkmap = p_hkmap;
7766#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007767 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007768 p_ai_nopaste = p_ai;
7769 p_et_nopaste = p_et;
7770 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 p_tw_nopaste = p_tw;
7772 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007773#ifdef FEAT_VARTABS
7774 if (p_vsts_nopaste)
7775 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007776 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7777 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780
7781 /*
7782 * Always set the option values, also when 'paste' is set when it is
7783 * already on.
7784 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007785 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007786 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007788 buf->b_p_tw = 0; // textwidth is 0
7789 buf->b_p_wm = 0; // wrapmargin is 0
7790 buf->b_p_sts = 0; // softtabstop is 0
7791 buf->b_p_ai = 0; // no auto-indent
7792 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007793#ifdef FEAT_VARTABS
7794 if (buf->b_p_vsts)
7795 free_string_option(buf->b_p_vsts);
7796 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007797 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
7800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007801 // set global options
7802 p_sm = 0; // no showmatch
7803 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007805 status_redraw_all(); // redraw to remove the ruler
7806 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007808 p_ri = 0; // no reverse insert
7809 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007811 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 p_tw = 0;
7813 p_wm = 0;
7814 p_sts = 0;
7815 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007816#ifdef FEAT_VARTABS
7817 if (p_vsts)
7818 free_string_option(p_vsts);
7819 p_vsts = empty_option;
7820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 }
7822
7823 /*
7824 * Paste switched from on to off: Restore saved values.
7825 */
7826 else if (old_p_paste)
7827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007828 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007829 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831 buf->b_p_tw = buf->b_p_tw_nopaste;
7832 buf->b_p_wm = buf->b_p_wm_nopaste;
7833 buf->b_p_sts = buf->b_p_sts_nopaste;
7834 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007835 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007836#ifdef FEAT_VARTABS
7837 if (buf->b_p_vsts)
7838 free_string_option(buf->b_p_vsts);
7839 buf->b_p_vsts = buf->b_p_vsts_nopaste
7840 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007841 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007842 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007843 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007844 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007845 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007849 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007851 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007853 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855#ifdef FEAT_RIGHTLEFT
7856 p_ri = save_ri;
7857 p_hkmap = save_hkmap;
7858#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007859 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007860 p_ai = p_ai_nopaste;
7861 p_et = p_et_nopaste;
7862 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 p_tw = p_tw_nopaste;
7864 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007865#ifdef FEAT_VARTABS
7866 if (p_vsts)
7867 free_string_option(p_vsts);
7868 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871
7872 old_p_paste = p_paste;
7873}
7874
7875/*
7876 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7877 *
7878 * Reset 'compatible' and set the values for options that didn't get set yet
7879 * to the Vim defaults.
7880 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007881 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 */
7883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007884vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007886 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007887 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007888 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889
7890 if (!option_was_set((char_u *)"cp"))
7891 {
7892 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007893 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7895 set_option_default(opt_idx, OPT_FREE, FALSE);
7896 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007897 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007899
7900 if (fname != NULL)
7901 {
7902 p = vim_getenv(envname, &dofree);
7903 if (p == NULL)
7904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007905 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007906 p = FullName_save(fname, FALSE);
7907 if (p != NULL)
7908 {
7909 vim_setenv(envname, p);
7910 vim_free(p);
7911 }
7912 }
7913 else if (dofree)
7914 vim_free(p);
7915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916}
7917
7918/*
7919 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7920 */
7921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007922change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007924 int opt_idx;
7925
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 if (p_cp != on)
7927 {
7928 p_cp = on;
7929 compatible_set();
7930 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007931 opt_idx = findoption((char_u *)"cp");
7932 if (opt_idx >= 0)
7933 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934}
7935
7936/*
7937 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007938 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 */
7940 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007941option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 int idx;
7944
7945 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007946 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 return FALSE;
7948 if (options[idx].flags & P_WAS_SET)
7949 return TRUE;
7950 return FALSE;
7951}
7952
7953/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007954 * Reset the flag indicating option "name" was set.
7955 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007956 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007957reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007958{
7959 int idx = findoption(name);
7960
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007961 if (idx < 0)
7962 return FAIL;
7963
7964 options[idx].flags &= ~P_WAS_SET;
7965 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007966}
7967
7968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 * compatible_set() - Called when 'compatible' has been set or unset.
7970 *
7971 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7972 * flag) to a Vi compatible value.
7973 * When 'compatible' is unset: Set all options that have a different default
7974 * for Vim (without the P_VI_DEF flag) to that default.
7975 */
7976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007977compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 int opt_idx;
7980
Bram Moolenaardac13472019-09-16 21:06:21 +02007981 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7983 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7984 set_option_default(opt_idx, OPT_FREE, p_cp);
7985 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007986 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987}
7988
Bram Moolenaardac13472019-09-16 21:06:21 +02007989#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00007992 * Called when the 'breakat' option changes value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00007994 char *
7995did_set_breakat(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007997 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 int i;
7999
8000 for (i = 0; i < 256; i++)
8001 breakat_flags[i] = FALSE;
8002
8003 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008004 for (p = p_breakat; *p; p++)
8005 breakat_flags[*p] = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00008006
8007 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009#endif
8010
8011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 * Check if backspacing over something is allowed.
8013 */
8014 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008015can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008016 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008018#ifdef FEAT_JOB_CHANNEL
8019 if (what == BS_START && bt_prompt(curbuf))
8020 return FALSE;
8021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 switch (*p_bs)
8023 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008024 case '3': return TRUE;
8025 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 case '1': return (what != BS_START);
8027 case '0': return FALSE;
8028 }
8029 return vim_strchr(p_bs, what) != NULL;
8030}
8031
8032/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008033 * Return the effective 'scrolloff' value for the current window, using the
8034 * global value when appropriate.
8035 */
8036 long
8037get_scrolloff_value(void)
8038{
8039 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8040}
8041
8042/*
8043 * Return the effective 'sidescrolloff' value for the current window, using the
8044 * global value when appropriate.
8045 */
8046 long
8047get_sidescrolloff_value(void)
8048{
8049 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8050}
8051
8052/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008053 * Get the local or global value of 'backupcopy'.
8054 */
8055 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008056get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008057{
8058 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8059}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008060
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008061#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008062/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008063 * Get the local or global value of 'formatlistpat'.
8064 */
8065 char_u *
8066get_flp_value(buf_T *buf)
8067{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008068 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8069 return p_flp;
8070 return buf->b_p_flp;
8071}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008072#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008073
8074/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008075 * Get the local or global value of the 'virtualedit' flags.
8076 */
8077 unsigned int
8078get_ve_flags(void)
8079{
Gary Johnson51ad8502021-08-03 18:33:08 +02008080 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8081 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008082}
8083
Bram Moolenaaree857022019-11-09 23:26:40 +01008084#if defined(FEAT_LINEBREAK) || defined(PROTO)
8085/*
8086 * Get the local or global value of 'showbreak'.
8087 */
8088 char_u *
8089get_showbreak_value(win_T *win)
8090{
8091 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8092 return p_sbr;
8093 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8094 return empty_option;
8095 return win->w_p_sbr;
8096}
8097#endif
8098
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008099#if defined(FEAT_EVAL) || defined(PROTO)
8100/*
8101 * Get window or buffer local options.
8102 */
8103 dict_T *
8104get_winbuf_options(int bufopt)
8105{
8106 dict_T *d;
8107 int opt_idx;
8108
8109 d = dict_alloc();
8110 if (d == NULL)
8111 return NULL;
8112
Bram Moolenaardac13472019-09-16 21:06:21 +02008113 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008114 {
8115 struct vimoption *opt = &options[opt_idx];
8116
8117 if ((bufopt && (opt->indir & PV_BUF))
8118 || (!bufopt && (opt->indir & PV_WIN)))
8119 {
8120 char_u *varp = get_varp(opt);
8121
8122 if (varp != NULL)
8123 {
8124 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008125 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008126 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008127 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008128 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008129 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008130 }
8131 }
8132 }
8133
8134 return d;
8135}
8136#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008137
Bram Moolenaardac13472019-09-16 21:06:21 +02008138#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008139/*
8140 * This is called when 'culopt' is changed
8141 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008142 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008143fill_culopt_flags(char_u *val, win_T *wp)
8144{
8145 char_u *p;
8146 char_u culopt_flags_new = 0;
8147
8148 if (val == NULL)
8149 p = wp->w_p_culopt;
8150 else
8151 p = val;
8152 while (*p != NUL)
8153 {
8154 if (STRNCMP(p, "line", 4) == 0)
8155 {
8156 p += 4;
8157 culopt_flags_new |= CULOPT_LINE;
8158 }
8159 else if (STRNCMP(p, "both", 4) == 0)
8160 {
8161 p += 4;
8162 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8163 }
8164 else if (STRNCMP(p, "number", 6) == 0)
8165 {
8166 p += 6;
8167 culopt_flags_new |= CULOPT_NBR;
8168 }
8169 else if (STRNCMP(p, "screenline", 10) == 0)
8170 {
8171 p += 10;
8172 culopt_flags_new |= CULOPT_SCRLINE;
8173 }
8174
8175 if (*p != ',' && *p != NUL)
8176 return FAIL;
8177 if (*p == ',')
8178 ++p;
8179 }
8180
8181 // Can't have both "line" and "screenline".
8182 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8183 return FAIL;
8184 wp->w_p_culopt_flags = culopt_flags_new;
8185
8186 return OK;
8187}
8188#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008189
8190/*
8191 * Get the value of 'magic' adjusted for Vim9 script.
8192 */
8193 int
8194magic_isset(void)
8195{
8196 switch (magic_overruled)
8197 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008198 case OPTION_MAGIC_ON: return TRUE;
8199 case OPTION_MAGIC_OFF: return FALSE;
8200 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008201 }
8202#ifdef FEAT_EVAL
8203 if (in_vim9script())
8204 return TRUE;
8205#endif
8206 return p_magic;
8207}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008208
8209/*
8210 * Set the callback function value for an option that accepts a function name,
8211 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8212 * Returns OK if the option is successfully set to a function, otherwise
8213 * returns FAIL.
8214 */
8215 int
8216option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8217{
8218#ifdef FEAT_EVAL
8219 typval_T *tv;
8220 callback_T cb;
8221
8222 if (optval == NULL || *optval == NUL)
8223 {
8224 free_callback(optcb);
8225 return OK;
8226 }
8227
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008228 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008229 || (STRNCMP(optval, "function(", 9) == 0)
8230 || (STRNCMP(optval, "funcref(", 8) == 0))
8231 // Lambda expression or a funcref
8232 tv = eval_expr(optval, NULL);
8233 else
8234 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008235 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008236 if (tv == NULL)
8237 return FAIL;
8238
8239 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008240 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008241 {
8242 free_tv(tv);
8243 return FAIL;
8244 }
8245
8246 free_callback(optcb);
8247 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008248 if (cb.cb_free_name)
8249 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008250 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008251
8252 // when using Vim9 style "import.funcname" it needs to be expanded to
8253 // "import#funcname".
8254 expand_autload_callback(optcb);
8255
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008256 return OK;
8257#else
8258 return FAIL;
8259#endif
8260}