blob: 3fa3b1c140eff681266445a375a5ecd0c6729273 [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;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003901 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003902 errmsg = options[opt_idx].opt_did_set_cb(&args);
3903 if (args.os_doskip)
3904 return errmsg;
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003907 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 options[opt_idx].flags |= P_WAS_SET;
3910
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003911#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003912 apply_optionset_autocmd(opt_idx, opt_flags,
3913 (long)(old_value ? TRUE : FALSE),
3914 (long)(old_global_value ? TRUE : FALSE),
3915 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003916#endif
3917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003918 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003919 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003920 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003921 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003922
3923 if ((opt_flags & OPT_NO_REDRAW) == 0)
3924 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003926 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927}
3928
3929/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003930 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003932 char *
3933did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003935 long *pp = (long *)args->os_varp;
3936 char *errmsg = NULL;
3937
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003938 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003940 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003941 p_wh = 1;
3942 }
3943 if (p_wmh > p_wh)
3944 {
3945 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3946 p_wh = p_wmh;
3947 }
3948 if (p_hh < 0)
3949 {
3950 errmsg = e_argument_must_be_positive;
3951 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003954 // Change window height NOW
3955 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003957 if (pp == &p_wh && curwin->w_height < p_wh)
3958 win_setheight((int)p_wh);
3959 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3960 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003963 return errmsg;
3964}
3965
3966/*
3967 * Process the new 'winminheight' option value.
3968 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003969 char *
3970did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003971{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003972 char *errmsg = NULL;
3973
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003974 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003976 errmsg = e_argument_must_be_positive;
3977 p_wmh = 0;
3978 }
3979 if (p_wmh > p_wh)
3980 {
3981 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3982 p_wmh = p_wh;
3983 }
3984 win_setminheight();
3985
3986 return errmsg;
3987}
3988
3989/*
3990 * Process the new 'winwidth' option value.
3991 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003992 char *
3993did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003994{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003995 char *errmsg = NULL;
3996
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003997 if (p_wiw < 1)
3998 {
3999 errmsg = e_argument_must_be_positive;
4000 p_wiw = 1;
4001 }
4002 if (p_wmw > p_wiw)
4003 {
4004 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4005 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004007
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004008 // Change window width NOW
4009 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4010 win_setwidth((int)p_wiw);
4011
4012 return errmsg;
4013}
4014
4015/*
4016 * Process the new 'winminwidth' option value.
4017 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004018 char *
4019did_set_winminwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004020{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004021 char *errmsg = NULL;
4022
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004023 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004024 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004025 errmsg = e_argument_must_be_positive;
4026 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004027 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004028 if (p_wmw > p_wiw)
4029 {
4030 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4031 p_wmw = p_wiw;
4032 }
4033 win_setminwidth();
4034
4035 return errmsg;
4036}
4037
4038/*
4039 * Process the new 'laststatus' option value.
4040 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004041 char *
4042did_set_laststatus(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004043{
4044 last_status(FALSE); // (re)set last window status line
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004045 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004046}
4047
4048/*
4049 * Process the new 'showtabline' option value.
4050 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004051 char *
4052did_set_showtabline(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004053{
4054 shell_new_rows(); // recompute window positions and heights
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004055 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004058#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004059/*
4060 * Process the new 'linespace' option value.
4061 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004062 char *
4063did_set_linespace(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004064{
4065 // Recompute gui.char_height and resize the Vim window to keep the
4066 // same number of lines.
4067 if (gui.in_use && gui_mch_adjust_charheight() == OK)
4068 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004069 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071#endif
4072
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004073#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004074/*
4075 * Process the new 'foldlevel' option value.
4076 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004077 char *
4078did_set_foldlevel(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004079{
4080 if (curwin->w_p_fdl < 0)
4081 curwin->w_p_fdl = 0;
4082 newFoldLevel();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004083 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004086/*
4087 * Process the new 'foldminlines' option value.
4088 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004089 char *
4090did_set_foldminlines(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004091{
4092 foldUpdateAll(curwin);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004093 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004094}
4095
4096/*
4097 * Process the new 'foldnestmax' option value.
4098 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004099 char *
4100did_set_foldnestmax(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004101{
4102 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 foldUpdateAll(curwin);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004104 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004105}
4106
4107/*
4108 * Process the new 'foldcolumn' option value.
4109 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004110 char *
4111did_set_foldcolumn(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004112{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004113 char *errmsg = NULL;
4114
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004115 if (curwin->w_p_fdc < 0)
4116 {
4117 errmsg = e_argument_must_be_positive;
4118 curwin->w_p_fdc = 0;
4119 }
4120 else if (curwin->w_p_fdc > 12)
4121 {
4122 errmsg = e_invalid_argument;
4123 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004126 return errmsg;
4127}
4128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004130/*
4131 * Process the new 'shiftwidth' or the 'tabstop' option value.
4132 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004133 char *
4134did_set_shiftwidth_tabstop(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004135{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004136 long *pp = (long *)args->os_varp;
4137 char *errmsg = NULL;
4138
4139 if (curbuf->b_p_sw < 0)
4140 {
4141 errmsg = e_argument_must_be_positive;
4142#ifdef FEAT_VARTABS
4143 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4144 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4145 ? tabstop_first(curbuf->b_p_vts_array)
4146 : curbuf->b_p_ts;
4147#else
4148 curbuf->b_p_sw = curbuf->b_p_ts;
4149#endif
4150 }
4151
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004152#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004153 if (foldmethodIsIndent(curwin))
4154 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004155#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004156 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4157 // parse 'cinoptions'.
4158 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4159 parse_cino(curbuf);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004160
4161 return errmsg;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004164/*
4165 * Process the new 'maxcombine' option value.
4166 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004167 char *
4168did_set_maxcombine(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004169{
4170 if (p_mco > MAX_MCO)
4171 p_mco = MAX_MCO;
4172 else if (p_mco < 0)
4173 p_mco = 0;
4174 screenclear(); // will re-allocate the screen
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004175 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004176}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004177
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004178/*
4179 * Process the new 'iminsert' option value.
4180 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004181 char *
4182did_set_iminsert(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004183{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004184 char *errmsg = NULL;
4185
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004186 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004188 errmsg = e_invalid_argument;
4189 curbuf->b_p_iminsert = B_IMODE_NONE;
4190 }
4191 p_iminsert = curbuf->b_p_iminsert;
4192 if (termcap_active) // don't do this in the alternate screen
4193 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004194#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004195 // Show/unshow value of 'keymap' in status lines.
4196 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004198
4199 return errmsg;
4200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004202#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004203/*
4204 * Process the new 'imstyle' option value.
4205 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004206 char *
4207did_set_imstyle(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004208{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004209 char *errmsg = NULL;
4210
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004211 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4212 errmsg = e_invalid_argument;
4213
4214 return errmsg;
4215}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004216#endif
4217
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004218/*
4219 * Process the new 'window' option value.
4220 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004221 char *
4222did_set_window(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004223{
4224 if (p_window < 1)
4225 p_window = 1;
4226 else if (p_window >= Rows)
4227 p_window = Rows - 1;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004228 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004229}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004230
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004231/*
4232 * Process the new 'imsearch' option value.
4233 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004234 char *
4235did_set_imsearch(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004236{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004237 char *errmsg = NULL;
4238
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004239 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004241 errmsg = e_invalid_argument;
4242 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004244 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004246 return errmsg;
4247}
4248
4249/*
4250 * Process the new 'titlelen' option value.
4251 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004252 char *
4253did_set_titlelen(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004254{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004255 long old_value = args->os_oldval.number;
4256 char *errmsg = NULL;
4257
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004258 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004259 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004261 errmsg = e_argument_must_be_positive;
4262 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004264 if (starting != NO_SCREEN && old_value != p_titlelen)
4265 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004267 return errmsg;
4268}
4269
4270/*
4271 * Process the new 'cmdheight' option value.
4272 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004273 char *
4274did_set_cmdheight(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004275{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004276 long old_value = args->os_oldval.number;
4277 char *errmsg = NULL;
4278
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004279 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004280 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004282 errmsg = e_argument_must_be_positive;
4283 p_ch = 1;
4284 }
4285 if (p_ch > Rows - min_rows() + 1)
4286 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004288 // Only compute the new window layout when startup has been
4289 // completed. Otherwise the frame sizes may be wrong.
4290 if ((p_ch != old_value
4291 || tabline_height() + topframe->fr_height != Rows - p_ch)
4292 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004294 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004296 )
4297 command_height();
4298
4299 return errmsg;
4300}
4301
4302/*
4303 * Process the new 'updatecount' option value.
4304 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004305 char *
4306did_set_updatecount(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004307{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004308 long old_value = args->os_oldval.number;
4309 char *errmsg = NULL;
4310
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004311 // when 'updatecount' changes from zero to non-zero, open swap files
4312 if (p_uc < 0)
4313 {
4314 errmsg = e_argument_must_be_positive;
4315 p_uc = 100;
4316 }
4317 if (p_uc && !old_value)
4318 ml_open_files();
4319
4320 return errmsg;
4321}
4322
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004323#if defined(FEAT_CONCEAL) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004324/*
4325 * Process the new 'conceallevel' option value.
4326 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004327 char *
4328did_set_conceallevel(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004329{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004330 char *errmsg = NULL;
4331
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004332 if (curwin->w_p_cole < 0)
4333 {
4334 errmsg = e_argument_must_be_positive;
4335 curwin->w_p_cole = 0;
4336 }
4337 else if (curwin->w_p_cole > 3)
4338 {
4339 errmsg = e_invalid_argument;
4340 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004343 return errmsg;
4344}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004347#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004348/*
4349 * Process the new 'pyxversion' option value.
4350 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004351 char *
4352did_set_pyxversion(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004353{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004354 char *errmsg = NULL;
4355
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004356 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4357 errmsg = e_invalid_argument;
4358
4359 return errmsg;
4360}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004361#endif
4362
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004363/*
4364 * Process the new global 'undolevels' option value.
4365 */
4366 static void
4367did_set_global_undolevels(long value, long old_value)
4368{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004369 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004370
4371 // use the old value, otherwise u_sync() may not work properly
4372 p_ul = old_value;
4373 u_sync(TRUE);
4374 p_ul = value;
4375}
4376
4377/*
4378 * Process the new buffer local 'undolevels' option value.
4379 */
4380 static void
4381did_set_buflocal_undolevels(long value, long old_value)
4382{
4383 // use the old value, otherwise u_sync() may not work properly
4384 curbuf->b_p_ul = old_value;
4385 u_sync(TRUE);
4386 curbuf->b_p_ul = value;
4387}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004389#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004390/*
4391 * Process the new 'numberwidth' option value.
4392 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004393 char *
4394did_set_numberwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004395{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004396 char *errmsg = NULL;
4397
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004398 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004399 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004400 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004401 errmsg = e_argument_must_be_positive;
4402 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004403 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004404 if (curwin->w_p_nuw > 20)
4405 {
4406 errmsg = e_invalid_argument;
4407 curwin->w_p_nuw = 20;
4408 }
4409 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4410
4411 return errmsg;
4412}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004413#endif
4414
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004415/*
4416 * Process the new 'textwidth' option value.
4417 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004418 char *
4419did_set_textwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004420{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004421 char *errmsg = NULL;
4422
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004423 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004424 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004425 errmsg = e_argument_must_be_positive;
4426 curbuf->b_p_tw = 0;
4427 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004428#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004429 {
4430 win_T *wp;
4431 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004432
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004433 FOR_ALL_TAB_WINDOWS(tp, wp)
4434 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004435 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004436#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004437
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004438 return errmsg;
4439}
4440
4441/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004442 * Process the new 'undolevels' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004443 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004444 char *
4445did_set_undolevels(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004446{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004447 long *pp = (long *)args->os_varp;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004448
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004449 if (pp == &p_ul) // global 'undolevels'
4450 did_set_global_undolevels(args->os_newval.number,
4451 args->os_oldval.number);
4452 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4453 did_set_buflocal_undolevels(args->os_newval.number,
4454 args->os_oldval.number);
4455
4456 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004457}
4458
4459/*
4460 * Check the bounds of numeric options.
4461 */
4462 static char *
4463check_num_option_bounds(
4464 long *pp,
4465 long old_value,
4466 long old_Rows,
4467 long old_Columns,
4468 char *errbuf,
4469 size_t errbuflen,
4470 char *errmsg)
4471{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (Rows < min_rows() && full_screen)
4473 {
4474 if (errbuf != NULL)
4475 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004476 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004477 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 errmsg = errbuf;
4479 }
4480 Rows = min_rows();
4481 }
4482 if (Columns < MIN_COLUMNS && full_screen)
4483 {
4484 if (errbuf != NULL)
4485 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004486 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004487 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 errmsg = errbuf;
4489 }
4490 Columns = MIN_COLUMNS;
4491 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004492 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /*
4495 * If the screen (shell) height has been changed, assume it is the
4496 * physical screenheight.
4497 */
4498 if (old_Rows != Rows || old_Columns != Columns)
4499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004500 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (updating_screen)
4502 *pp = old_value;
4503 else if (full_screen
4504#ifdef FEAT_GUI
4505 && !gui.starting
4506#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004507 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 set_shellsize((int)Columns, (int)Rows, TRUE);
4509 else
4510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004511 // Postpone the resizing; check the size and cmdline position for
4512 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 check_shellsize();
4514 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4515 cmdline_row = Rows - p_ch;
4516 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004517 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004518 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (curbuf->b_p_ts <= 0)
4522 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004523 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 curbuf->b_p_ts = 8;
4525 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004526 else if (curbuf->b_p_ts > TABSTOP_MAX)
4527 {
4528 errmsg = e_invalid_argument;
4529 curbuf->b_p_ts = 8;
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (p_tm < 0)
4532 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004533 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 p_tm = 0;
4535 }
4536 if ((curwin->w_p_scr <= 0
4537 || (curwin->w_p_scr > curwin->w_height
4538 && curwin->w_height > 0))
4539 && full_screen)
4540 {
4541 if (pp == &(curwin->w_p_scr))
4542 {
4543 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004544 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 win_comp_scroll(curwin);
4546 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004547 // If 'scroll' became invalid because of a side effect silently adjust
4548 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 else if (curwin->w_p_scr <= 0)
4550 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004551 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 curwin->w_p_scr = curwin->w_height;
4553 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004554 if (p_hi < 0)
4555 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004556 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004557 p_hi = 0;
4558 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004559 else if (p_hi > 10000)
4560 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004561 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004562 p_hi = 10000;
4563 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004564 if (p_re < 0 || p_re > 2)
4565 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004566 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004567 p_re = 0;
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (p_report < 0)
4570 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004571 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 p_report = 1;
4573 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004574 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004576 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 p_sj = Rows / 2;
4578 else
4579 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004580 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 p_sj = 1;
4582 }
4583 }
4584 if (p_so < 0 && full_screen)
4585 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004586 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 p_so = 0;
4588 }
4589 if (p_siso < 0 && full_screen)
4590 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004591 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 p_siso = 0;
4593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (p_cwh < 1)
4595 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004596 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 p_cwh = 1;
4598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if (p_ut < 0)
4600 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004601 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 p_ut = 2000;
4603 }
4604 if (p_ss < 0)
4605 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004606 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 p_ss = 0;
4608 }
4609
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004610 return errmsg;
4611}
4612
4613/*
4614 * Set the value of a number option, and take care of side effects.
4615 * Returns NULL for success, or an error message for an error.
4616 */
4617 static char *
4618set_num_option(
4619 int opt_idx, // index in options[] table
4620 char_u *varp, // pointer to the option variable
4621 long value, // new value
4622 char *errbuf, // buffer for error messages
4623 size_t errbuflen, // length of "errbuf"
4624 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4625 // OPT_MODELINE, etc.
4626{
4627 char *errmsg = NULL;
4628 long old_value = *(long *)varp;
4629#if defined(FEAT_EVAL)
4630 long old_global_value = 0; // only used when setting a local and
4631 // global option
4632#endif
4633 long old_Rows = Rows; // remember old Rows
4634 long old_Columns = Columns; // remember old Columns
4635 long *pp = (long *)varp;
4636
4637 // Disallow changing some options from secure mode.
4638 if ((secure
4639#ifdef HAVE_SANDBOX
4640 || sandbox != 0
4641#endif
4642 ) && (options[opt_idx].flags & P_SECURE))
4643 return e_not_allowed_here;
4644
4645#if defined(FEAT_EVAL)
4646 // Save the global value before changing anything. This is needed as for
4647 // a global-only option setting the "local value" in fact sets the global
4648 // value (since there is only one value).
4649 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4650 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4651 OPT_GLOBAL);
4652#endif
4653
4654 *pp = value;
4655#ifdef FEAT_EVAL
4656 // Remember where the option was set.
4657 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4658#endif
4659#ifdef FEAT_GUI
4660 need_mouse_correct = TRUE;
4661#endif
4662
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004663 // Invoke the option specific callback function to validate and apply the
4664 // new value.
4665 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004666 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004667 optset_T args;
4668
4669 args.os_varp = varp;
4670 args.os_flags = opt_flags;
4671 args.os_oldval.number = old_value;
4672 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004673 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004674 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004675 }
4676
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004677 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004678 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4679 errbuf, errbuflen, errmsg);
4680
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004681 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4683 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4684
4685 options[opt_idx].flags |= P_WAS_SET;
4686
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004687#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004688 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4689 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004690#endif
4691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004692 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004693 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004694 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004695 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004696 if ((opt_flags & OPT_NO_REDRAW) == 0)
4697 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
4699 return errmsg;
4700}
4701
4702/*
4703 * Called after an option changed: check if something needs to be redrawn.
4704 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004706check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004708 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004709 int doclear = (flags & P_RCLR) == P_RCLR;
4710 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004712 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
4715 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4716 changed_window_setting();
4717 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004718 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004719 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004720 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004721 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004722 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004724 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725}
4726
4727/*
4728 * Find index for option 'arg'.
4729 * Return -1 if not found.
4730 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004731 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004732findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 int opt_idx;
4735 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004736 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 int is_term_opt;
4738
4739 /*
4740 * For first call: Initialize the quick-access table.
4741 * It contains the index for the first option that starts with a certain
4742 * letter. There are 26 letters, plus the first "t_" option.
4743 */
4744 if (quick_tab[1] == 0)
4745 {
4746 p = options[0].fullname;
4747 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4748 {
4749 if (s[0] != p[0])
4750 {
4751 if (s[0] == 't' && s[1] == '_')
4752 quick_tab[26] = opt_idx;
4753 else
4754 quick_tab[CharOrdLow(s[0])] = opt_idx;
4755 }
4756 p = s;
4757 }
4758 }
4759
4760 /*
4761 * Check for name starting with an illegal character.
4762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 return -1;
4765
4766 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4767 if (is_term_opt)
4768 opt_idx = quick_tab[26];
4769 else
4770 opt_idx = quick_tab[CharOrdLow(arg[0])];
4771 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004773 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 break;
4775 }
4776 if (s == NULL && !is_term_opt)
4777 {
4778 opt_idx = quick_tab[CharOrdLow(arg[0])];
4779 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4780 {
4781 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 break;
4784 s = NULL;
4785 }
4786 }
4787 if (s == NULL)
4788 opt_idx = -1;
4789 return opt_idx;
4790}
4791
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004792#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4793 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794/*
4795 * Get the value for an option.
4796 *
4797 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004798 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004799 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004800 * String option: gov_string, *stringval gets allocated string.
4801 * Hidden Number option: gov_hidden_number.
4802 * Hidden Toggle option: gov_hidden_bool.
4803 * Hidden String option: gov_hidden_string.
4804 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004805 *
4806 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004808 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004809get_option_value(
4810 char_u *name,
4811 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004812 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004813 int *flagsp,
4814 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
4816 int opt_idx;
4817 char_u *varp;
4818
4819 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004820 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004821 {
4822 int key;
4823
4824 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004825 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004826 {
4827 char_u key_name[2];
4828 char_u *p;
4829
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004830 if (flagsp != NULL)
4831 *flagsp = 0; // terminal option has no flags
4832
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004833 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004834 if (key < 0)
4835 {
4836 key_name[0] = KEY2TERMCAP0(key);
4837 key_name[1] = KEY2TERMCAP1(key);
4838 }
4839 else
4840 {
4841 key_name[0] = KS_KEY;
4842 key_name[1] = (key & 0xff);
4843 }
4844 p = find_termcode(key_name);
4845 if (p != NULL)
4846 {
4847 if (stringval != NULL)
4848 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004849 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004850 }
4851 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004852 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004855 varp = get_varp_scope(&(options[opt_idx]), scope);
4856
4857 if (flagsp != NULL)
4858 // Return the P_xxxx option flags.
4859 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 if (options[opt_idx].flags & P_STRING)
4862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004863 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004864 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (stringval != NULL)
4866 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004867 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004868 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4869 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004871 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004872 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004873 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 *stringval = vim_strsave(*(char_u **)(varp));
4878 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004879 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004882 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004883 return (options[opt_idx].flags & P_NUM)
4884 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 if (options[opt_idx].flags & P_NUM)
4886 *numval = *(long *)varp;
4887 else
4888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004889 // Special case: 'modified' is b_changed, but we also want to consider
4890 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if ((int *)varp == &curbuf->b_changed)
4892 *numval = curbufIsChanged();
4893 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004894 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004896 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897}
4898#endif
4899
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004900#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004901/*
4902 * Returns the option attributes and its value. Unlike the above function it
4903 * will return either global value or local value of the option depending on
4904 * what was requested, but it will never return global value if it was
4905 * requested to return local one and vice versa. Neither it will return
4906 * buffer-local value if it was requested to return window-local one.
4907 *
4908 * Pretends that option is absent if it is not present in the requested scope
4909 * (i.e. has no global, window-local or buffer-local value depending on
4910 * opt_type). Uses
4911 *
4912 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004913 * 0 hidden or unknown option, also option that does not have requested
4914 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004915 * see SOPT_* in vim.h for other flags
4916 *
4917 * Possible opt_type values: see SREQ_* in vim.h
4918 */
4919 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004920get_option_value_strict(
4921 char_u *name,
4922 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004923 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004924 int opt_type,
4925 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004926{
4927 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004928 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004929 struct vimoption *p;
4930 int r = 0;
4931
4932 opt_idx = findoption(name);
4933 if (opt_idx < 0)
4934 return 0;
4935
4936 p = &(options[opt_idx]);
4937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004938 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004939 if (p->var == NULL)
4940 return 0;
4941
4942 if (p->flags & P_BOOL)
4943 r |= SOPT_BOOL;
4944 else if (p->flags & P_NUM)
4945 r |= SOPT_NUM;
4946 else if (p->flags & P_STRING)
4947 r |= SOPT_STRING;
4948
4949 if (p->indir == PV_NONE)
4950 {
4951 if (opt_type == SREQ_GLOBAL)
4952 r |= SOPT_GLOBAL;
4953 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004955 }
4956 else
4957 {
4958 if (p->indir & PV_BOTH)
4959 r |= SOPT_GLOBAL;
4960 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004962
4963 if (p->indir & PV_WIN)
4964 {
4965 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004966 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004967 else
4968 r |= SOPT_WIN;
4969 }
4970 else if (p->indir & PV_BUF)
4971 {
4972 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004973 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004974 else
4975 r |= SOPT_BUF;
4976 }
4977 }
4978
4979 if (stringval == NULL)
4980 return r;
4981
4982 if (opt_type == SREQ_GLOBAL)
4983 varp = p->var;
4984 else
4985 {
4986 if (opt_type == SREQ_BUF)
4987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004988 // Special case: 'modified' is b_changed, but we also want to
4989 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004990 if (p->indir == PV_MOD)
4991 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004992 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004993 varp = NULL;
4994 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004995# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004996 else if (p->indir == PV_KEY)
4997 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004998 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004999 *stringval = NULL;
5000 varp = NULL;
5001 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005002# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005003 else
5004 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005005 buf_T *save_curbuf = curbuf;
5006
5007 // only getting a pointer, no need to use aucmd_prepbuf()
5008 curbuf = (buf_T *)from;
5009 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005010 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005011 curbuf = save_curbuf;
5012 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005013 }
5014 }
5015 else if (opt_type == SREQ_WIN)
5016 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005017 win_T *save_curwin = curwin;
5018
5019 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005020 curbuf = curwin->w_buffer;
5021 varp = get_varp(p);
5022 curwin = save_curwin;
5023 curbuf = curwin->w_buffer;
5024 }
5025 if (varp == p->var)
5026 return (r | SOPT_UNSET);
5027 }
5028
5029 if (varp != NULL)
5030 {
5031 if (p->flags & P_STRING)
5032 *stringval = vim_strsave(*(char_u **)(varp));
5033 else if (p->flags & P_NUM)
5034 *numval = *(long *) varp;
5035 else
5036 *numval = *(int *)varp;
5037 }
5038
5039 return r;
5040}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005041
5042/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005043 * Iterate over options. First argument is a pointer to a pointer to a
5044 * structure inside options[] array, second is option type like in the above
5045 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005046 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005047 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005048 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005049 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005050 * first argument to NULL.
5051 *
5052 * Returns full option name for current option on each call.
5053 */
5054 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005055option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005056{
5057 struct vimoption *ret = NULL;
5058 do
5059 {
5060 if (*option == NULL)
5061 *option = (void *) options;
5062 else if (((struct vimoption *) (*option))->fullname == NULL)
5063 {
5064 *option = NULL;
5065 return NULL;
5066 }
5067 else
5068 *option = (void *) (((struct vimoption *) (*option)) + 1);
5069
5070 ret = ((struct vimoption *) (*option));
5071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005072 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005073 if (ret->var == NULL)
5074 {
5075 ret = NULL;
5076 continue;
5077 }
5078
5079 switch (opt_type)
5080 {
5081 case SREQ_GLOBAL:
5082 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5083 ret = NULL;
5084 break;
5085 case SREQ_BUF:
5086 if (!(ret->indir & PV_BUF))
5087 ret = NULL;
5088 break;
5089 case SREQ_WIN:
5090 if (!(ret->indir & PV_WIN))
5091 ret = NULL;
5092 break;
5093 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005094 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005095 return NULL;
5096 }
5097 }
5098 while (ret == NULL);
5099
5100 return (char_u *)ret->fullname;
5101}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102#endif
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005105 * Return the flags for the option at 'opt_idx'.
5106 */
5107 long_u
5108get_option_flags(int opt_idx)
5109{
5110 return options[opt_idx].flags;
5111}
5112
5113/*
5114 * Set a flag for the option at 'opt_idx'.
5115 */
5116 void
5117set_option_flag(int opt_idx, long_u flag)
5118{
5119 options[opt_idx].flags |= flag;
5120}
5121
5122/*
5123 * Clear a flag for the option at 'opt_idx'.
5124 */
5125 void
5126clear_option_flag(int opt_idx, long_u flag)
5127{
5128 options[opt_idx].flags &= ~flag;
5129}
5130
5131/*
5132 * Returns TRUE if the option at 'opt_idx' is a global option
5133 */
5134 int
5135is_global_option(int opt_idx)
5136{
5137 return options[opt_idx].indir == PV_NONE;
5138}
5139
5140/*
5141 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5142 * local value.
5143 */
5144 int
5145is_global_local_option(int opt_idx)
5146{
5147 return options[opt_idx].indir & PV_BOTH;
5148}
5149
5150/*
5151 * Returns TRUE if the option at 'opt_idx' is a window-local option
5152 */
5153 int
5154is_window_local_option(int opt_idx)
5155{
5156 return options[opt_idx].var == VAR_WIN;
5157}
5158
5159/*
5160 * Returns TRUE if the option at 'opt_idx' is a hidden option
5161 */
5162 int
5163is_hidden_option(int opt_idx)
5164{
5165 return options[opt_idx].var == NULL;
5166}
5167
5168#if defined(FEAT_CRYPT) || defined(PROTO)
5169/*
5170 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5171 */
5172 int
5173is_crypt_key_option(int opt_idx)
5174{
5175 return options[opt_idx].indir == PV_KEY;
5176}
5177#endif
5178
5179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * Set the value of option "name".
5181 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005182 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005183 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005185 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005186set_option_value(
5187 char_u *name,
5188 long number,
5189 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005190 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
5192 int opt_idx;
5193 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005194 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005195 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
5197 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005198 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005199 {
5200 int key;
5201
5202 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005203 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005204 {
5205 char_u key_name[2];
5206
5207 if (key < 0)
5208 {
5209 key_name[0] = KEY2TERMCAP0(key);
5210 key_name[1] = KEY2TERMCAP1(key);
5211 }
5212 else
5213 {
5214 key_name[0] = KS_KEY;
5215 key_name[1] = (key & 0xff);
5216 }
5217 add_termcode(key_name, string, FALSE);
5218 if (full_screen)
5219 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005220 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005221 return NULL;
5222 }
5223
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005224 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 else
5227 {
5228 flags = options[opt_idx].flags;
5229#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005230 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005232 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005233 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005234 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005237 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005238 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005239
5240 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5241 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005243 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005245 int idx;
5246
5247 // Either we are given a string or we are setting option
5248 // to zero.
5249 for (idx = 0; string[idx] == '0'; ++idx)
5250 ;
5251 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005252 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005253 // There's another character after zeros or the string
5254 // is empty. In both cases, we are trying to set a
5255 // num option using a string.
5256 semsg(_(e_number_required_after_str_equal_str),
5257 name, string);
5258 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005259
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005262 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005263 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005264 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005265 errbuf, sizeof(errbuf), opt_flags);
5266 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005267 else
5268 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005272 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273}
5274
5275/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005276 * Call set_option_value() and when an error is returned report it.
5277 */
5278 void
5279set_option_value_give_err(
5280 char_u *name,
5281 long number,
5282 char_u *string,
5283 int opt_flags) // OPT_LOCAL or 0 (both)
5284{
5285 char *errmsg = set_option_value(name, number, string, opt_flags);
5286
5287 if (errmsg != NULL)
5288 emsg(_(errmsg));
5289}
5290
5291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 * Get the terminal code for a terminal option.
5293 * Returns NULL when not found.
5294 */
5295 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005296get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
5298 int opt_idx;
5299 char_u *varp;
5300
5301 if (tname[0] != 't' || tname[1] != '_' ||
5302 tname[2] == NUL || tname[3] == NUL)
5303 return NULL;
5304 if ((opt_idx = findoption(tname)) >= 0)
5305 {
5306 varp = get_varp(&(options[opt_idx]));
5307 if (varp != NULL)
5308 varp = *(char_u **)(varp);
5309 return varp;
5310 }
5311 return find_termcode(tname + 2);
5312}
5313
5314 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005315get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 int i;
5318
5319 i = findoption((char_u *)"hl");
5320 if (i >= 0)
5321 return options[i].def_val[VI_DEFAULT];
5322 return (char_u *)NULL;
5323}
5324
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005325 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005326get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005327{
5328 int i;
5329
5330 i = findoption((char_u *)"enc");
5331 if (i >= 0)
5332 return options[i].def_val[VI_DEFAULT];
5333 return (char_u *)NULL;
5334}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005335
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005336#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005337 int
5338is_option_allocated(char *name)
5339{
5340 int idx = findoption((char_u *)name);
5341
5342 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5343}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005344#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005345
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005346#if defined(FEAT_EVAL) || defined(PROTO)
5347/*
5348 * Return TRUE if "name" is a string option.
5349 * Returns FALSE if option "name" does not exist.
5350 */
5351 int
5352is_string_option(char_u *name)
5353{
5354 int idx = findoption(name);
5355
5356 return idx >= 0 && (options[idx].flags & P_STRING);
5357}
5358#endif
5359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360/*
5361 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005362 * When "has_lt" is true there is a '<' before "*arg_arg".
5363 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 */
5365 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005366find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005368 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005370 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 /*
5373 * Don't use get_special_key_code() for t_xx, we don't want it to call
5374 * add_termcap_entry().
5375 */
5376 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5377 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005378 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005380 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005382 key = find_special_key(&arg, &modifiers,
5383 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005384 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 key = 0;
5386 }
5387 return key;
5388}
5389
5390/*
5391 * if 'all' == 0: show changed options
5392 * if 'all' == 1: show all normal options
5393 * if 'all' == 2: show all terminal options
5394 */
5395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005396showoptions(
5397 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005398 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
5400 struct vimoption *p;
5401 int col;
5402 int isterm;
5403 char_u *varp;
5404 struct vimoption **items;
5405 int item_count;
5406 int run;
5407 int row, rows;
5408 int cols;
5409 int i;
5410 int len;
5411
5412#define INC 20
5413#define GAP 3
5414
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005415 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 if (items == NULL)
5417 return;
5418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005421 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005423 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005425 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005427 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005430 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 * 1. display the short items
5432 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005433 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 */
5435 for (run = 1; run <= 2 && !got_int; ++run)
5436 {
5437 /*
5438 * collect the items in items[]
5439 */
5440 item_count = 0;
5441 for (p = &options[0]; p->fullname != NULL; p++)
5442 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005443 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005444 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005445 continue;
5446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 varp = NULL;
5448 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005449 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
5451 if (p->indir != PV_NONE && !isterm)
5452 varp = get_varp_scope(p, opt_flags);
5453 }
5454 else
5455 varp = get_varp(p);
5456 if (varp != NULL
5457 && ((all == 2 && isterm)
5458 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005459 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005461 if (opt_flags & OPT_ONECOLUMN)
5462 len = Columns;
5463 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005464 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 else
5466 {
5467 option_value2string(p, opt_flags);
5468 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5469 }
5470 if ((len <= INC - GAP && run == 1) ||
5471 (len > INC - GAP && run == 2))
5472 items[item_count++] = p;
5473 }
5474 }
5475
5476 /*
5477 * display the items
5478 */
5479 if (run == 1)
5480 {
5481 cols = (Columns + GAP - 3) / INC;
5482 if (cols == 0)
5483 cols = 1;
5484 rows = (item_count + cols - 1) / cols;
5485 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005486 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 rows = item_count;
5488 for (row = 0; row < rows && !got_int; ++row)
5489 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005490 msg_putchar('\n'); // go to next line
5491 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 break;
5493 col = 0;
5494 for (i = row; i < item_count; i += rows)
5495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005496 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 showoneopt(items[i], opt_flags);
5498 col += INC;
5499 }
5500 out_flush();
5501 ui_breakcheck();
5502 }
5503 }
5504 vim_free(items);
5505}
5506
5507/*
5508 * Return TRUE if option "p" has its default value.
5509 */
5510 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005511optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 int dvi;
5514
5515 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005516 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005517 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005519 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005521 // the cast to long is required for Manx C, long_i is
5522 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005523 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5526}
5527
5528/*
5529 * showoneopt: show the value of one option
5530 * must not be called with a hidden option!
5531 */
5532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005533showoneopt(
5534 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005535 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005537 char_u *varp;
5538 int save_silent = silent_mode;
5539
5540 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005541 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
5543 varp = get_varp_scope(p, opt_flags);
5544
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005545 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5547 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005548 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005550 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005552 msg_puts(" ");
5553 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (!(p->flags & P_BOOL))
5555 {
5556 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005557 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 option_value2string(p, opt_flags);
5559 msg_outtrans(NameBuff);
5560 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005561
5562 silent_mode = save_silent;
5563 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564}
5565
5566/*
5567 * Write modified options as ":set" commands to a file.
5568 *
5569 * There are three values for "opt_flags":
5570 * OPT_GLOBAL: Write global option values and fresh values of
5571 * buffer-local options (used for start of a session
5572 * file).
5573 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5574 * curwin (used for a vimrc file).
5575 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5576 * and local values for window-local options of
5577 * curwin. Local values are also written when at the
5578 * default value, because a modeline or autocommand
5579 * may have set them when doing ":edit file" and the
5580 * user has set them back at the default or fresh
5581 * value.
5582 * When "local_only" is TRUE, don't write fresh
5583 * values, only local values (for ":mkview").
5584 * (fresh value = value used for a new buffer or window for a local option).
5585 *
5586 * Return FAIL on error, OK otherwise.
5587 */
5588 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005589makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005592 char_u *varp; // currently used value
5593 char_u *varp_fresh; // local value
5594 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 char *cmd;
5596 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005597 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
5599 /*
5600 * The options that don't have a default (terminal name, columns, lines)
5601 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005602 * Do the loop over "options[]" twice: once for options with the
5603 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005605 for (pri = 1; pri >= 0; --pri)
5606 {
5607 for (p = &options[0]; !istermoption(p); p++)
5608 if (!(p->flags & P_NO_MKRC)
5609 && !istermoption(p)
5610 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005612 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5614 continue;
5615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005616 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5617 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5619 continue;
5620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005621 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005623 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 continue;
5625
Bram Moolenaard23b7142021-04-17 21:04:34 +02005626 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5627 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005628 continue;
5629
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 round = 2;
5631 if (p->indir != PV_NONE)
5632 {
5633 if (p->var == VAR_WIN)
5634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005635 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (!(opt_flags & OPT_LOCAL))
5637 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005638 // When fresh value of window-local option is not at the
5639 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5641 {
5642 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005643 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
5645 round = 1;
5646 varp_local = varp;
5647 varp = varp_fresh;
5648 }
5649 }
5650 }
5651 }
5652
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005653 // Round 1: fresh value for window-local options.
5654 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 for ( ; round <= 2; varp = varp_local, ++round)
5656 {
5657 if (round == 1 || (opt_flags & OPT_GLOBAL))
5658 cmd = "set";
5659 else
5660 cmd = "setlocal";
5661
5662 if (p->flags & P_BOOL)
5663 {
5664 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5665 return FAIL;
5666 }
5667 else if (p->flags & P_NUM)
5668 {
5669 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5670 return FAIL;
5671 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005672 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005674 int do_endif = FALSE;
5675
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005676 // Don't set 'syntax' and 'filetype' again if the value is
5677 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005678 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005679#if defined(FEAT_SYN_HL)
5680 p->indir == PV_SYN ||
5681#endif
5682 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
5684 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5685 *(char_u **)(varp)) < 0
5686 || put_eol(fd) < 0)
5687 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005688 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005691 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005693 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
5695 if (put_line(fd, "endif") == FAIL)
5696 return FAIL;
5697 }
5698 }
5699 }
5700 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 return OK;
5703}
5704
5705#if defined(FEAT_FOLDING) || defined(PROTO)
5706/*
5707 * Generate set commands for the local fold options only. Used when
5708 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5709 */
5710 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005711makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005713 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005715 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 == FAIL
5717# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005718 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005720 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 == FAIL
5722 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5723 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5724 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5725 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5726 )
5727 return FAIL;
5728
5729 return OK;
5730}
5731#endif
5732
5733 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005734put_setstring(
5735 FILE *fd,
5736 char *cmd,
5737 char *name,
5738 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005739 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005742 char_u *buf = NULL;
5743 char_u *part = NULL;
5744 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5747 return FAIL;
5748 if (*valuep != NULL)
5749 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005750 // Output 'pastetoggle' as key names. For other
5751 // options some characters have to be escaped with
5752 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (valuep == &p_pt)
5754 {
5755 s = *valuep;
5756 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005757 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 return FAIL;
5759 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005760 // expand the option value, replace $HOME by ~
5761 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005763 int size = (int)STRLEN(*valuep) + 1;
5764
5765 // replace home directory in the whole option value into "buf"
5766 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005767 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005768 goto fail;
5769 home_replace(NULL, *valuep, buf, size, FALSE);
5770
5771 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005772 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005773 // can be expanded when read back.
5774 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5775 && vim_strchr(*valuep, ',') != NULL)
5776 {
5777 part = alloc(size);
5778 if (part == NULL)
5779 goto fail;
5780
5781 // write line break to clear the option, e.g. ':set rtp='
5782 if (put_eol(fd) == FAIL)
5783 goto fail;
5784
5785 p = buf;
5786 while (*p != NUL)
5787 {
5788 // for each comma separated option part, append value to
5789 // the option, :set rtp+=value
5790 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5791 goto fail;
5792 (void)copy_option_part(&p, part, size, ",");
5793 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5794 goto fail;
5795 }
5796 vim_free(buf);
5797 vim_free(part);
5798 return OK;
5799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005801 {
5802 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005804 }
5805 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807 else if (put_escstr(fd, *valuep, 2) == FAIL)
5808 return FAIL;
5809 }
5810 if (put_eol(fd) < 0)
5811 return FAIL;
5812 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005813fail:
5814 vim_free(buf);
5815 vim_free(part);
5816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817}
5818
5819 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005820put_setnum(
5821 FILE *fd,
5822 char *cmd,
5823 char *name,
5824 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 long wc;
5827
5828 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5829 return FAIL;
5830 if (wc_use_keyname((char_u *)valuep, &wc))
5831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005832 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5834 return FAIL;
5835 }
5836 else if (fprintf(fd, "%ld", *valuep) < 0)
5837 return FAIL;
5838 if (put_eol(fd) < 0)
5839 return FAIL;
5840 return OK;
5841}
5842
5843 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005844put_setbool(
5845 FILE *fd,
5846 char *cmd,
5847 char *name,
5848 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005850 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005851 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5853 || put_eol(fd) < 0)
5854 return FAIL;
5855 return OK;
5856}
5857
5858/*
5859 * Clear all the terminal options.
5860 * If the option has been allocated, free the memory.
5861 * Terminal options are never hidden or indirect.
5862 */
5863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005864clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 /*
5867 * Reset a few things before clearing the old options. This may cause
5868 * outputting a few things that the terminal doesn't understand, but the
5869 * screen will be cleared later, so this is OK.
5870 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005871 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005872 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005874 // When starting the GUI close the display opened for the clipboard.
5875 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 if (gui.starting)
5877 clear_xterm_clip();
5878#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005879 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005881 free_termoptions();
5882}
5883
5884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005885free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005886{
5887 struct vimoption *p;
5888
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005889 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 if (istermoption(p))
5891 {
5892 if (p->flags & P_ALLOCED)
5893 free_string_option(*(char_u **)(p->var));
5894 if (p->flags & P_DEF_ALLOCED)
5895 free_string_option(p->def_val[VI_DEFAULT]);
5896 *(char_u **)(p->var) = empty_option;
5897 p->def_val[VI_DEFAULT] = empty_option;
5898 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005899#ifdef FEAT_EVAL
5900 // remember where the option was cleared
5901 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 }
5904 clear_termcodes();
5905}
5906
5907/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005908 * Free the string for one term option, if it was allocated.
5909 * Set the string to empty_option and clear allocated flag.
5910 * "var" points to the option value.
5911 */
5912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005913free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005914{
5915 struct vimoption *p;
5916
5917 for (p = &options[0]; p->fullname != NULL; p++)
5918 if (p->var == var)
5919 {
5920 if (p->flags & P_ALLOCED)
5921 free_string_option(*(char_u **)(p->var));
5922 *(char_u **)(p->var) = empty_option;
5923 p->flags &= ~P_ALLOCED;
5924 break;
5925 }
5926}
5927
5928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 * Set the terminal option defaults to the current value.
5930 * Used after setting the terminal name.
5931 */
5932 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005933set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 struct vimoption *p;
5936
5937 for (p = &options[0]; p->fullname != NULL; p++)
5938 {
5939 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5940 {
5941 if (p->flags & P_DEF_ALLOCED)
5942 {
5943 free_string_option(p->def_val[VI_DEFAULT]);
5944 p->flags &= ~P_DEF_ALLOCED;
5945 }
5946 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5947 if (p->flags & P_ALLOCED)
5948 {
5949 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005950 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 }
5952 }
5953 }
5954}
5955
5956/*
5957 * return TRUE if 'p' starts with 't_'
5958 */
5959 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005960istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5963}
5964
Bram Moolenaardac13472019-09-16 21:06:21 +02005965/*
5966 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5967 */
5968 int
5969istermoption_idx(int opt_idx)
5970{
5971 return istermoption(&options[opt_idx]);
5972}
5973
Bram Moolenaar113e1072019-01-20 15:30:40 +01005974#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005976 * Unset local option value, similar to ":set opt<".
5977 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005978 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005979unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005980{
5981 struct vimoption *p;
5982 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005983 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005984
5985 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005986 if (opt_idx < 0)
5987 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005988 p = &(options[opt_idx]);
5989
5990 switch ((int)p->indir)
5991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005992 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005993 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005994 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005995 break;
5996 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005997 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005998 break;
5999 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006000 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006001 break;
6002 case PV_AR:
6003 buf->b_p_ar = -1;
6004 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006005 case PV_BKC:
6006 clear_string_option(&buf->b_p_bkc);
6007 buf->b_bkc_flags = 0;
6008 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006009 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006010 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006011 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006012 case PV_TC:
6013 clear_string_option(&buf->b_p_tc);
6014 buf->b_tc_flags = 0;
6015 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006016 case PV_SISO:
6017 curwin->w_p_siso = -1;
6018 break;
6019 case PV_SO:
6020 curwin->w_p_so = -1;
6021 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006022# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006023 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006024 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006025 break;
6026 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006027 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006028 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006029# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006030 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006031 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006032 break;
6033 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006034 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006035 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006036# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006037 case PV_TSRFU:
6038 clear_string_option(&buf->b_p_tsrfu);
6039 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006040# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006041 case PV_FP:
6042 clear_string_option(&buf->b_p_fp);
6043 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006044# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006045 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006046 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006047 break;
6048 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006049 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006050 break;
6051 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006052 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006053 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006054# endif
6055# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006056 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006057 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006058 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006059# endif
6060# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006061 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006062 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006063 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006064# endif
6065# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006066 case PV_SBR:
6067 clear_string_option(&((win_T *)from)->w_p_sbr);
6068 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006069# endif
6070# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006071 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006072 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006073 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006074# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006075 case PV_UL:
6076 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6077 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006078 case PV_LW:
6079 clear_string_option(&buf->b_p_lw);
6080 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006081 case PV_MENC:
6082 clear_string_option(&buf->b_p_menc);
6083 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006084 case PV_LCS:
6085 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006086 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006087 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006088 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006089 case PV_FCS:
6090 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006091 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006092 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006093 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006094 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006095 clear_string_option(&((win_T *)from)->w_p_ve);
6096 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006097 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006098 }
6099}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006100#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006101
6102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006104 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 */
6106 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006107get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006109 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 {
6111 if (p->var == VAR_WIN)
6112 return (char_u *)GLOBAL_WO(get_varp(p));
6113 return p->var;
6114 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006115 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 {
6117 switch ((int)p->indir)
6118 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006119 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006121 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6122 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6123 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006125 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6126 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6127 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006128 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006129 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006130 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006131 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6132 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006134 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6135 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006137 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6138 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006139#ifdef FEAT_COMPL_FUNC
6140 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6141#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006142#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6143 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6144#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006145#if defined(FEAT_CRYPT)
6146 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6147#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006148#ifdef FEAT_LINEBREAK
6149 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6150#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006151#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006152 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006153#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006154 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006155 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006156 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006157 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006158 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006159 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006160 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006163 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 }
6165 return get_varp(p);
6166}
6167
6168/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006169 * Get pointer to option variable at 'opt_idx', depending on local or global
6170 * scope.
6171 */
6172 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006173get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006174{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006175 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006176}
6177
6178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 * Get pointer to option variable.
6180 */
6181 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006182get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006184 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (p->var == NULL)
6186 return NULL;
6187
6188 switch ((int)p->indir)
6189 {
6190 case PV_NONE: return p->var;
6191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006192 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006193 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006195 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006197 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006199 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006201 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006203 case PV_TC: return *curbuf->b_p_tc != NUL
6204 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006205 case PV_BKC: return *curbuf->b_p_bkc != NUL
6206 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006207 case PV_SISO: return curwin->w_p_siso >= 0
6208 ? (char_u *)&(curwin->w_p_siso) : p->var;
6209 case PV_SO: return curwin->w_p_so >= 0
6210 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006212 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006214 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6216#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006217 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006219 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006221#ifdef FEAT_COMPL_FUNC
6222 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6223 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6224#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006225 case PV_FP: return *curbuf->b_p_fp != NUL
6226 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006228 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006230 case PV_GP: return *curbuf->b_p_gp != NUL
6231 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6232 case PV_MP: return *curbuf->b_p_mp != NUL
6233 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006235#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6236 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6237 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6238#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006239#if defined(FEAT_CRYPT)
6240 case PV_CM: return *curbuf->b_p_cm != NUL
6241 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6242#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006243#ifdef FEAT_LINEBREAK
6244 case PV_SBR: return *curwin->w_p_sbr != NUL
6245 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6246#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006247#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006248 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006249 ? (char_u *)&(curwin->w_p_stl) : p->var;
6250#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006251 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6252 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006253 case PV_LW: return *curbuf->b_p_lw != NUL
6254 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006255 case PV_MENC: return *curbuf->b_p_menc != NUL
6256 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257#ifdef FEAT_ARABIC
6258 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6259#endif
6260 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006261 case PV_LCS: return *curwin->w_p_lcs != NUL
6262 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006263 case PV_FCS: return *curwin->w_p_fcs != NUL
6264 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006265 case PV_VE: return *curwin->w_p_ve != NUL
6266 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006267#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006268 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6269#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006270#ifdef FEAT_SYN_HL
6271 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6272 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006273 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006274 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276#ifdef FEAT_DIFF
6277 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6278#endif
6279#ifdef FEAT_FOLDING
6280 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6281 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6282 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6283 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6284 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6285 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6286 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6287# ifdef FEAT_EVAL
6288 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6289 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6290# endif
6291 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6292#endif
6293 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006294 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006295#ifdef FEAT_LINEBREAK
6296 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006299 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006300#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6302#endif
6303#ifdef FEAT_RIGHTLEFT
6304 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6305 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6306#endif
6307 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006308 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6310#ifdef FEAT_LINEBREAK
6311 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006312 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6313 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006315 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006317 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006318#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006319 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6320 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6321#endif
6322#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006323 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6324 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6325 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
6328 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6329 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6332 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6334 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6336 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6337 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006338 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341#ifdef FEAT_FOLDING
6342 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006345#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006346 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006348#ifdef FEAT_COMPL_FUNC
6349 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006350 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006351#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006352#ifdef FEAT_EVAL
6353 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6354#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006355 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006357 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006363 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6365 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6366 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6367 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6368#ifdef FEAT_FIND_ID
6369# ifdef FEAT_EVAL
6370 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6371# endif
6372#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006373#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6375 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6376#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006377#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380#ifdef FEAT_CRYPT
6381 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006384 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6386 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6387 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6388 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6389 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006391 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6398#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006399 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006400 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6401#endif
6402#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006403 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6404 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6405 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006406 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407#endif
6408 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6409 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6410 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6411 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006412#ifdef FEAT_PERSISTENT_UNDO
6413 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6416#ifdef FEAT_KEYMAP
6417 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6418#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006419#ifdef FEAT_SIGNS
6420 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6421#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006422#ifdef FEAT_VARTABS
6423 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6424 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6425#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006426 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006428 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 return (char_u *)&(curbuf->b_p_wm);
6430}
6431
6432/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006433 * Return a pointer to the variable for option at 'opt_idx'
6434 */
6435 char_u *
6436get_option_var(int opt_idx)
6437{
6438 return options[opt_idx].var;
6439}
6440
Dominique Pelle748b3082022-01-08 12:41:16 +00006441#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006442/*
6443 * Return the full name of the option at 'opt_idx'
6444 */
6445 char_u *
6446get_option_fullname(int opt_idx)
6447{
6448 return (char_u *)options[opt_idx].fullname;
6449}
Dominique Pelle748b3082022-01-08 12:41:16 +00006450#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006451
6452/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006453 * Return the did_set callback function for the option at 'opt_idx'
6454 */
6455 opt_did_set_cb_T
6456get_option_did_set_cb(int opt_idx)
6457{
6458 return options[opt_idx].opt_did_set_cb;
6459}
6460
6461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 * Get the value of 'equalprg', either the buffer-local one or the global one.
6463 */
6464 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006465get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
6467 if (*curbuf->b_p_ep == NUL)
6468 return p_ep;
6469 return curbuf->b_p_ep;
6470}
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472/*
6473 * Copy options from one window to another.
6474 * Used when splitting a window.
6475 */
6476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006477win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6480 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006481 after_copy_winopt(wp_to);
6482}
6483
6484/*
6485 * After copying window options: update variables depending on options.
6486 */
6487 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006488after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006489{
6490#ifdef FEAT_LINEBREAK
6491 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006492#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006493#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006494 fill_culopt_flags(NULL, wp);
6495 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006496#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006497 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6498 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006499}
6500
6501 static char_u *
6502copy_option_val(char_u *val)
6503{
6504 if (val == empty_option)
6505 return empty_option; // no need to allocate memory
6506 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509/*
6510 * Copy the options from one winopt_T to another.
6511 * Doesn't free the old option values in "to", use clear_winopt() for that.
6512 * The 'scroll' option is not copied, because it depends on the window height.
6513 * The 'previewwindow' option is reset, there can be only one preview window.
6514 */
6515 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006516copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518#ifdef FEAT_ARABIC
6519 to->wo_arab = from->wo_arab;
6520#endif
6521 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006522 to->wo_lcs = copy_option_val(from->wo_lcs);
6523 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006525 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006526 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006527 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006528#ifdef FEAT_LINEBREAK
6529 to->wo_nuw = from->wo_nuw;
6530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531#ifdef FEAT_RIGHTLEFT
6532 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006533 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006535#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006536 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006537#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006538#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006539 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006542#ifdef FEAT_DIFF
6543 to->wo_wrap_save = from->wo_wrap_save;
6544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545#ifdef FEAT_LINEBREAK
6546 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006547 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006548 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006550 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006552 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006553 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006554 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006555 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006556#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006557 to->wo_spell = from->wo_spell;
6558#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006559#ifdef FEAT_SYN_HL
6560 to->wo_cuc = from->wo_cuc;
6561 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006562 to->wo_culopt = copy_option_val(from->wo_culopt);
6563 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565#ifdef FEAT_DIFF
6566 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006567 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006569#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006570 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006571 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006572#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006573#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006574 to->wo_twk = copy_option_val(from->wo_twk);
6575 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577#ifdef FEAT_FOLDING
6578 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006579 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006581 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006582 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 to->wo_fml = from->wo_fml;
6584 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006585 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006586 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006587 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006588 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 to->wo_fdn = from->wo_fdn;
6590# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006591 to->wo_fde = copy_option_val(from->wo_fde);
6592 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006594 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006596#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006597 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006598#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006599
6600#ifdef FEAT_EVAL
6601 // Copy the script context so that we know where the value was last set.
6602 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6603 sizeof(to->wo_script_ctx));
6604#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006605 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606}
6607
6608/*
6609 * Check string options in a window for a NULL value.
6610 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006612check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 check_winopt(&win->w_onebuf_opt);
6615 check_winopt(&win->w_allbuf_opt);
6616}
6617
6618/*
6619 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6620 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006621 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006622check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624#ifdef FEAT_FOLDING
6625 check_string_option(&wop->wo_fdi);
6626 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006627 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628# ifdef FEAT_EVAL
6629 check_string_option(&wop->wo_fde);
6630 check_string_option(&wop->wo_fdt);
6631# endif
6632 check_string_option(&wop->wo_fmr);
6633#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006634#ifdef FEAT_SIGNS
6635 check_string_option(&wop->wo_scl);
6636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#ifdef FEAT_RIGHTLEFT
6638 check_string_option(&wop->wo_rlc);
6639#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006640#ifdef FEAT_LINEBREAK
6641 check_string_option(&wop->wo_sbr);
6642#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006643#ifdef FEAT_STL_OPT
6644 check_string_option(&wop->wo_stl);
6645#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006646#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006647 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006648 check_string_option(&wop->wo_cc);
6649#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006650#ifdef FEAT_CONCEAL
6651 check_string_option(&wop->wo_cocu);
6652#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006653#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006654 check_string_option(&wop->wo_twk);
6655 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006656#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006657#ifdef FEAT_LINEBREAK
6658 check_string_option(&wop->wo_briopt);
6659#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006660 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006661 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006662 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006663 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
6665
6666/*
6667 * Free the allocated memory inside a winopt_T.
6668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006670clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672#ifdef FEAT_FOLDING
6673 clear_string_option(&wop->wo_fdi);
6674 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006675 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676# ifdef FEAT_EVAL
6677 clear_string_option(&wop->wo_fde);
6678 clear_string_option(&wop->wo_fdt);
6679# endif
6680 clear_string_option(&wop->wo_fmr);
6681#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006682#ifdef FEAT_SIGNS
6683 clear_string_option(&wop->wo_scl);
6684#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006685#ifdef FEAT_LINEBREAK
6686 clear_string_option(&wop->wo_briopt);
6687#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006688 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689#ifdef FEAT_RIGHTLEFT
6690 clear_string_option(&wop->wo_rlc);
6691#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006692#ifdef FEAT_LINEBREAK
6693 clear_string_option(&wop->wo_sbr);
6694#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006695#ifdef FEAT_STL_OPT
6696 clear_string_option(&wop->wo_stl);
6697#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006698#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006699 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006700 clear_string_option(&wop->wo_cc);
6701#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006702#ifdef FEAT_CONCEAL
6703 clear_string_option(&wop->wo_cocu);
6704#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006705#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006706 clear_string_option(&wop->wo_twk);
6707 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006708#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006709 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006710 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006711 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712}
6713
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006714#ifdef FEAT_EVAL
6715// Index into the options table for a buffer-local option enum.
6716static int buf_opt_idx[BV_COUNT];
6717# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6718
6719/*
6720 * Initialize buf_opt_idx[] if not done already.
6721 */
6722 static void
6723init_buf_opt_idx(void)
6724{
6725 static int did_init_buf_opt_idx = FALSE;
6726 int i;
6727
6728 if (did_init_buf_opt_idx)
6729 return;
6730 did_init_buf_opt_idx = TRUE;
6731 for (i = 0; !istermoption_idx(i); i++)
6732 if (options[i].indir & PV_BUF)
6733 buf_opt_idx[options[i].indir & PV_MASK] = i;
6734}
6735#else
6736# define COPY_OPT_SCTX(buf, bv)
6737#endif
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739/*
6740 * Copy global option values to local options for one buffer.
6741 * Used when creating a new buffer and sometimes when entering a buffer.
6742 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006743 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6745 * appropriate.
6746 * BCO_NOHELP Don't copy the values to a help buffer.
6747 */
6748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006749buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006752 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 int dont_do_help;
6754 int did_isk = FALSE;
6755
6756 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 * Skip this when the option defaults have not been set yet. Happens when
6758 * main() allocates the first buffer.
6759 */
6760 if (p_cpo != NULL)
6761 {
6762 /*
6763 * Always copy when entering and 'cpo' contains 'S'.
6764 * Don't copy when already initialized.
6765 * Don't copy when 'cpo' contains 's' and not entering.
6766 * 'S' BCO_ENTER initialized 's' should_copy
6767 * yes yes X X TRUE
6768 * yes no yes X FALSE
6769 * no X yes X FALSE
6770 * X no no yes FALSE
6771 * X no no no TRUE
6772 * no yes no X TRUE
6773 */
6774 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6775 && (buf->b_p_initialized
6776 || (!(flags & BCO_ENTER)
6777 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6778 should_copy = FALSE;
6779
6780 if (should_copy || (flags & BCO_ALWAYS))
6781 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006782#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006783 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006784 init_buf_opt_idx();
6785#endif
6786 // Don't copy the options specific to a help buffer when
6787 // BCO_NOHELP is given or the options were initialized already
6788 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6790 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006791 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {
6793 save_p_isk = buf->b_p_isk;
6794 buf->b_p_isk = NULL;
6795 }
6796 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006797 * Always free the allocated strings. If not already initialized,
6798 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 */
6800 if (!buf->b_p_initialized)
6801 {
6802 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006803 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006806 switch (*p_ffs)
6807 {
6808 case 'm':
6809 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6810 case 'd':
6811 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6812 case 'u':
6813 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6814 default:
6815 buf->b_p_ff = vim_strsave(p_ff);
6816 }
6817 if (buf->b_p_ff != NULL)
6818 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 buf->b_p_bh = empty_option;
6820 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
6822 else
6823 free_buf_options(buf, FALSE);
6824
6825 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006826 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 buf->b_p_ai_nopaste = p_ai_nopaste;
6828 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006829 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006831 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 buf->b_p_tw_nopaste = p_tw_nopaste;
6833 buf->b_p_tw_nobin = p_tw_nobin;
6834 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006835 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 buf->b_p_wm_nopaste = p_wm_nopaste;
6837 buf->b_p_wm_nobin = p_wm_nobin;
6838 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006839 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006840 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006841 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006842 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006843 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006845 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006847 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006849 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 buf->b_p_ml_nobin = p_ml_nobin;
6851 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006852 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006853 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006854 buf->b_p_swf = FALSE;
6855 else
6856 {
6857 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006858 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006861 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006862#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006863 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006864 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006866#ifdef FEAT_COMPL_FUNC
6867 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006868 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006869 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006870 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006871 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006872 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006873#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006874#ifdef FEAT_EVAL
6875 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006876 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006877 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006880 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006882#ifdef FEAT_VARTABS
6883 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006884 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006885 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006886 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006887 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006888 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006889 buf->b_p_vsts_nopaste = p_vsts_nopaste
6890 ? vim_strsave(p_vsts_nopaste) : NULL;
6891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006893 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006895 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896#ifdef FEAT_FOLDING
6897 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006898 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899#endif
6900 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006901 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006902 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006903 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006904 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6905 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006907 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006909 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006911 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006913 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006914
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006916 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006918 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006920 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006921 buf->b_p_cinsd = vim_strsave(p_cinsd);
6922 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006923 buf->b_p_lop = vim_strsave(p_lop);
6924 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006925
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006926 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006929 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006931 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006933 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006935 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006937 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006938 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006939 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006940#endif
6941#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006942 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006943 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006944 (void)compile_cap_prog(&buf->b_s);
6945 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006946 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006947 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006948 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006949 buf->b_s.b_p_spo = vim_strsave(p_spo);
6950 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006952#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006954 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006956 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006958 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006959#if defined(FEAT_EVAL)
6960 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006961 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963#ifdef FEAT_CRYPT
6964 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006965 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006968 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#ifdef FEAT_KEYMAP
6970 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 buf->b_kmap_state |= KEYMAP_INIT;
6973#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006974#ifdef FEAT_TERMINAL
6975 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006976 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006977#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // This isn't really an option, but copying the langmap and IME
6979 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006981 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 // options that are normally global but also have a local value
6986 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006988 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006989 buf->b_p_bkc = empty_option;
6990 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991#ifdef FEAT_QUICKFIX
6992 buf->b_p_gp = empty_option;
6993 buf->b_p_mp = empty_option;
6994 buf->b_p_efm = empty_option;
6995#endif
6996 buf->b_p_ep = empty_option;
6997 buf->b_p_kp = empty_option;
6998 buf->b_p_path = empty_option;
6999 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007000 buf->b_p_tc = empty_option;
7001 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002#ifdef FEAT_FIND_ID
7003 buf->b_p_def = empty_option;
7004 buf->b_p_inc = empty_option;
7005# ifdef FEAT_EVAL
7006 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007007 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008# endif
7009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 buf->b_p_dict = empty_option;
7011 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007012#ifdef FEAT_COMPL_FUNC
7013 buf->b_p_tsrfu = empty_option;
7014#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007015 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007017#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7018 buf->b_p_bexpr = empty_option;
7019#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007020#if defined(FEAT_CRYPT)
7021 buf->b_p_cm = empty_option;
7022#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007023#ifdef FEAT_PERSISTENT_UNDO
7024 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007025 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007026#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007027 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007028 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030 /*
7031 * Don't copy the options set by ex_help(), use the saved values,
7032 * when going from a help buffer to a non-help buffer.
7033 * Don't touch these at all when BCO_NOHELP is used and going from
7034 * or to a help buffer.
7035 */
7036 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007039#ifdef FEAT_VARTABS
7040 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007041 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007042 else
7043 buf->b_p_vts_array = NULL;
7044#endif
7045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 else
7047 {
7048 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 did_isk = TRUE;
7051 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007052 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007053#ifdef FEAT_VARTABS
7054 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007055 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007056 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007057 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007058 else
7059 buf->b_p_vts_array = NULL;
7060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 if (buf->b_p_bt[0] == 'h')
7063 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007065 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067 }
7068
7069 /*
7070 * When the options should be copied (ignoring BCO_ALWAYS), set the
7071 * flag that indicates that the options have been initialized.
7072 */
7073 if (should_copy)
7074 buf->b_p_initialized = TRUE;
7075 }
7076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 if (did_isk)
7079 (void)buf_init_chartab(buf, FALSE);
7080}
7081
7082/*
7083 * Reset the 'modifiable' option and its default value.
7084 */
7085 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007086reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 int opt_idx;
7089
7090 curbuf->b_p_ma = FALSE;
7091 p_ma = FALSE;
7092 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007093 if (opt_idx >= 0)
7094 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095}
7096
7097/*
7098 * Set the global value for 'iminsert' to the local value.
7099 */
7100 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007101set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 p_iminsert = curbuf->b_p_iminsert;
7104}
7105
7106/*
7107 * Set the global value for 'imsearch' to the local value.
7108 */
7109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007110set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
7112 p_imsearch = curbuf->b_p_imsearch;
7113}
7114
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115static int expand_option_idx = -1;
7116static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7117static int expand_option_flags = 0;
7118
7119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007120set_context_in_set_cmd(
7121 expand_T *xp,
7122 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007123 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007126 long_u flags = 0; // init for GCC
7127 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 char_u *p;
7129 char_u *s;
7130 int is_term_option = FALSE;
7131 int key;
7132
7133 expand_option_flags = opt_flags;
7134
7135 xp->xp_context = EXPAND_SETTINGS;
7136 if (*arg == NUL)
7137 {
7138 xp->xp_pattern = arg;
7139 return;
7140 }
7141 p = arg + STRLEN(arg) - 1;
7142 if (*p == ' ' && *(p - 1) != '\\')
7143 {
7144 xp->xp_pattern = p + 1;
7145 return;
7146 }
7147 while (p > arg)
7148 {
7149 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007150 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (*p == ' ' || *p == ',')
7152 {
7153 while (s > arg && *(s - 1) == '\\')
7154 --s;
7155 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007156 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (*p == ' ' && ((p - s) & 1) == 0)
7158 {
7159 ++p;
7160 break;
7161 }
7162 --p;
7163 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007164 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {
7166 xp->xp_context = EXPAND_BOOL_SETTINGS;
7167 p += 2;
7168 }
7169 if (STRNCMP(p, "inv", 3) == 0)
7170 {
7171 xp->xp_context = EXPAND_BOOL_SETTINGS;
7172 p += 3;
7173 }
7174 xp->xp_pattern = arg = p;
7175 if (*arg == '<')
7176 {
7177 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007178 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 return;
7180 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 {
7183 xp->xp_context = EXPAND_NOTHING;
7184 return;
7185 }
7186 nextchar = *++p;
7187 is_term_option = TRUE;
7188 expand_option_name[2] = KEY2TERMCAP0(key);
7189 expand_option_name[3] = KEY2TERMCAP1(key);
7190 }
7191 else
7192 {
7193 if (p[0] == 't' && p[1] == '_')
7194 {
7195 p += 2;
7196 if (*p != NUL)
7197 ++p;
7198 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007199 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 nextchar = *++p;
7201 is_term_option = TRUE;
7202 expand_option_name[2] = p[-2];
7203 expand_option_name[3] = p[-1];
7204 }
7205 else
7206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007207 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7209 p++;
7210 if (*p == NUL)
7211 return;
7212 nextchar = *p;
7213 *p = NUL;
7214 opt_idx = findoption(arg);
7215 *p = nextchar;
7216 if (opt_idx == -1 || options[opt_idx].var == NULL)
7217 {
7218 xp->xp_context = EXPAND_NOTHING;
7219 return;
7220 }
7221 flags = options[opt_idx].flags;
7222 if (flags & P_BOOL)
7223 {
7224 xp->xp_context = EXPAND_NOTHING;
7225 return;
7226 }
7227 }
7228 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007229 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7231 {
7232 ++p;
7233 nextchar = '=';
7234 }
7235 if ((nextchar != '=' && nextchar != ':')
7236 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7237 {
7238 xp->xp_context = EXPAND_UNSUCCESSFUL;
7239 return;
7240 }
7241 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7242 {
7243 xp->xp_context = EXPAND_OLD_SETTING;
7244 if (is_term_option)
7245 expand_option_idx = -1;
7246 else
7247 expand_option_idx = opt_idx;
7248 xp->xp_pattern = p + 1;
7249 return;
7250 }
7251 xp->xp_context = EXPAND_NOTHING;
7252 if (is_term_option || (flags & P_NUM))
7253 return;
7254
7255 xp->xp_pattern = p + 1;
7256
7257 if (flags & P_EXPAND)
7258 {
7259 p = options[opt_idx].var;
7260 if (p == (char_u *)&p_bdir
7261 || p == (char_u *)&p_dir
7262 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007263 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266#ifdef FEAT_SESSION
7267 || p == (char_u *)&p_vdir
7268#endif
7269 )
7270 {
7271 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007272 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 xp->xp_backslash = XP_BS_THREE;
7274 else
7275 xp->xp_backslash = XP_BS_ONE;
7276 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007277 else if (p == (char_u *)&p_ft)
7278 {
7279 xp->xp_context = EXPAND_FILETYPE;
7280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 else
7282 {
7283 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007284 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (p == (char_u *)&p_tags)
7286 xp->xp_backslash = XP_BS_THREE;
7287 else
7288 xp->xp_backslash = XP_BS_ONE;
7289 }
7290 }
7291
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007292 // For an option that is a list of file names, find the start of the
7293 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7295 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007296 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (*p == ' ' || *p == ',')
7298 {
7299 s = p;
7300 while (s > xp->xp_pattern && *(s - 1) == '\\')
7301 --s;
7302 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7303 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7304 {
7305 xp->xp_pattern = p + 1;
7306 break;
7307 }
7308 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007309
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007310#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007311 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007312 if (options[opt_idx].var == (char_u *)&p_sps
7313 && STRNCMP(p, "file:", 5) == 0)
7314 {
7315 xp->xp_pattern = p + 5;
7316 break;
7317 }
7318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320}
7321
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007322/*
7323 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7324 *
7325 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7326 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7327 *
7328 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7329 * regular expression 'regmatch', then stores the match in matches[idx] and
7330 * returns TRUE.
7331 *
7332 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7333 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7334 *
7335 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7336 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7337 */
7338 static int
7339match_str(
7340 char_u *str,
7341 regmatch_T *regmatch,
7342 char_u **matches,
7343 int idx,
7344 int test_only,
7345 int fuzzy,
7346 char_u *fuzzystr,
7347 fuzmatch_str_T *fuzmatch)
7348{
7349 if (!fuzzy)
7350 {
7351 if (vim_regexec(regmatch, str, (colnr_T)0))
7352 {
7353 if (!test_only)
7354 matches[idx] = vim_strsave(str);
7355 return TRUE;
7356 }
7357 }
7358 else
7359 {
7360 int score;
7361
7362 score = fuzzy_match_str(str, fuzzystr);
7363 if (score != 0)
7364 {
7365 if (!test_only)
7366 {
7367 fuzmatch[idx].idx = idx;
7368 fuzmatch[idx].str = vim_strsave(str);
7369 fuzmatch[idx].score = score;
7370 }
7371
7372 return TRUE;
7373 }
7374 }
7375
7376 return FALSE;
7377}
7378
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007380ExpandSettings(
7381 expand_T *xp,
7382 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007383 char_u *fuzzystr,
7384 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007385 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007386 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007388 int num_normal = 0; // Nr of matching non-term-code settings
7389 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 int opt_idx;
7391 int match;
7392 int count = 0;
7393 char_u *str;
7394 int loop;
7395 int is_term_opt;
7396 char_u name_buf[MAX_KEY_NAME_LEN];
7397 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007398 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007399 int fuzzy;
7400 fuzmatch_str_T *fuzmatch = NULL;
7401
Christian Brabandtcb747892022-05-08 21:10:56 +01007402 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007404 // do this loop twice:
7405 // loop == 0: count the number of matching options
7406 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 for (loop = 0; loop <= 1; ++loop)
7408 {
7409 regmatch->rm_ic = ic;
7410 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7411 {
K.Takataeeec2542021-06-02 13:28:16 +02007412 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007413 {
7414 if (match_str((char_u *)names[match], regmatch, *matches,
7415 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 {
7417 if (loop == 0)
7418 num_normal++;
7419 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007420 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
7424 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7425 opt_idx++)
7426 {
7427 if (options[opt_idx].var == NULL)
7428 continue;
7429 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7430 && !(options[opt_idx].flags & P_BOOL))
7431 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007432 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (is_term_opt && num_normal > 0)
7434 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007435
7436 if (match_str(str, regmatch, *matches, count, (loop == 0),
7437 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 {
7439 if (loop == 0)
7440 {
7441 if (is_term_opt)
7442 num_term++;
7443 else
7444 num_normal++;
7445 }
7446 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007447 count++;
7448 }
7449 else if (!fuzzy && options[opt_idx].shortname != NULL
7450 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007451 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007452 {
7453 // Compare against the abbreviated option name (for regular
7454 // expression match). Fuzzy matching (previous if) already
7455 // matches against both the expanded and abbreviated names.
7456 if (loop == 0)
7457 {
7458 if (is_term_opt)
7459 num_term++;
7460 else
7461 num_normal++;
7462 }
7463 else
7464 (*matches)[count++] = vim_strsave(str);
7465 }
7466 else if (is_term_opt)
7467 {
7468 name_buf[0] = '<';
7469 name_buf[1] = 't';
7470 name_buf[2] = '_';
7471 name_buf[3] = str[2];
7472 name_buf[4] = str[3];
7473 name_buf[5] = '>';
7474 name_buf[6] = NUL;
7475
7476 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7477 fuzzy, fuzzystr, fuzmatch))
7478 {
7479 if (loop == 0)
7480 num_term++;
7481 else
7482 count++;
7483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 /*
7488 * Check terminal key codes, these are not in the option table
7489 */
7490 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7491 {
7492 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7493 {
7494 if (!isprint(str[0]) || !isprint(str[1]))
7495 continue;
7496
7497 name_buf[0] = 't';
7498 name_buf[1] = '_';
7499 name_buf[2] = str[0];
7500 name_buf[3] = str[1];
7501 name_buf[4] = NUL;
7502
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007503 if (match_str(name_buf, regmatch, *matches, count,
7504 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7505 {
7506 if (loop == 0)
7507 num_term++;
7508 else
7509 count++;
7510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 else
7512 {
7513 name_buf[0] = '<';
7514 name_buf[1] = 't';
7515 name_buf[2] = '_';
7516 name_buf[3] = str[0];
7517 name_buf[4] = str[1];
7518 name_buf[5] = '>';
7519 name_buf[6] = NUL;
7520
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007521 if (match_str(name_buf, regmatch, *matches, count,
7522 (loop == 0), fuzzy, fuzzystr,
7523 fuzmatch))
7524 {
7525 if (loop == 0)
7526 num_term++;
7527 else
7528 count++;
7529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 }
7531 }
7532
7533 /*
7534 * Check special key names.
7535 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007536 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7538 {
7539 name_buf[0] = '<';
7540 STRCPY(name_buf + 1, str);
7541 STRCAT(name_buf, ">");
7542
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007543 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7544 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
7546 if (loop == 0)
7547 num_term++;
7548 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007549 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551 }
7552 }
7553 if (loop == 0)
7554 {
7555 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007556 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007558 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 else
7560 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007561 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007563 *matches = ALLOC_MULT(char_u *, *numMatches);
7564 if (*matches == NULL)
7565 {
7566 *matches = (char_u **)"";
7567 return FAIL;
7568 }
7569 }
7570 else
7571 {
7572 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7573 if (fuzmatch == NULL)
7574 {
7575 *matches = (char_u **)"";
7576 return FAIL;
7577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 }
7579 }
7580 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007581
7582 if (fuzzy &&
7583 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7584 return FAIL;
7585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 return OK;
7587}
7588
7589 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007590ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007592 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 char_u *buf;
7594
zeertzjqb0d45ec2023-01-25 15:04:22 +00007595 *numMatches = 0;
7596 *matches = ALLOC_ONE(char_u *);
7597 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 return FAIL;
7599
7600 /*
7601 * For a terminal key code expand_option_idx is < 0.
7602 */
7603 if (expand_option_idx < 0)
7604 {
7605 var = find_termcode(expand_option_name + 2);
7606 if (var == NULL)
7607 expand_option_idx = findoption(expand_option_name);
7608 }
7609
7610 if (expand_option_idx >= 0)
7611 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007612 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 option_value2string(&options[expand_option_idx], expand_option_flags);
7614 var = NameBuff;
7615 }
7616 else if (var == NULL)
7617 var = (char_u *)"";
7618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007619 // A backslash is required before some characters. This is the reverse of
7620 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 buf = vim_strsave_escaped(var, escape_chars);
7622
7623 if (buf == NULL)
7624 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007625 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 return FAIL;
7627 }
7628
7629#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007630 // For MS-Windows et al. we don't double backslashes at the start and
7631 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007632 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (var[0] == '\\' && var[1] == '\\'
7634 && expand_option_idx >= 0
7635 && (options[expand_option_idx].flags & P_EXPAND)
7636 && vim_isfilec(var[2])
7637 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007638 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639#endif
7640
zeertzjqb0d45ec2023-01-25 15:04:22 +00007641 *matches[0] = buf;
7642 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 return OK;
7644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
7646/*
7647 * Get the value for the numeric or string option *opp in a nice format into
7648 * NameBuff[]. Must not be called with a hidden option!
7649 */
7650 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007651option_value2string(
7652 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007653 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 char_u *varp;
7656
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007657 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
7659 if (opp->flags & P_NUM)
7660 {
7661 long wc = 0;
7662
7663 if (wc_use_keyname(varp, &wc))
7664 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7665 else if (wc != 0)
7666 STRCPY(NameBuff, transchar((int)wc));
7667 else
7668 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7669 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007670 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {
7672 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007673 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 NameBuff[0] = NUL;
7675#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007676 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007677 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 STRCPY(NameBuff, "*****");
7679#endif
7680 else if (opp->flags & P_EXPAND)
7681 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007682 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 else if ((char_u **)opp->var == &p_pt)
7684 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7685 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007686 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688}
7689
7690/*
7691 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7692 * printed as a keyname.
7693 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7694 */
7695 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007696wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
7698 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7699 {
7700 *wcp = *(long *)varp;
7701 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7702 return TRUE;
7703 }
7704 return FALSE;
7705}
7706
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 * Return TRUE if "x" is present in 'shortmess' option, or
7709 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7710 */
7711 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007712shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007714 return p_shm != NULL &&
7715 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 || (vim_strchr(p_shm, 'a') != NULL
7717 && vim_strchr((char_u *)SHM_A, x) != NULL));
7718}
7719
7720/*
7721 * paste_option_changed() - Called after p_paste was set or reset.
7722 */
7723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007724paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 static int old_p_paste = FALSE;
7727 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007728 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730#ifdef FEAT_RIGHTLEFT
7731 static int save_ri = 0;
7732 static int save_hkmap = 0;
7733#endif
7734 buf_T *buf;
7735
7736 if (p_paste)
7737 {
7738 /*
7739 * Paste switched from off to on.
7740 * Save the current values, so they can be restored later.
7741 */
7742 if (!old_p_paste)
7743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007744 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007745 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {
7747 buf->b_p_tw_nopaste = buf->b_p_tw;
7748 buf->b_p_wm_nopaste = buf->b_p_wm;
7749 buf->b_p_sts_nopaste = buf->b_p_sts;
7750 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007751 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007752#ifdef FEAT_VARTABS
7753 if (buf->b_p_vsts_nopaste)
7754 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007755 buf->b_p_vsts_nopaste =
7756 buf->b_p_vsts && buf->b_p_vsts != empty_option
7757 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
7760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007761 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007763 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765#ifdef FEAT_RIGHTLEFT
7766 save_ri = p_ri;
7767 save_hkmap = p_hkmap;
7768#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007769 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007770 p_ai_nopaste = p_ai;
7771 p_et_nopaste = p_et;
7772 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 p_tw_nopaste = p_tw;
7774 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007775#ifdef FEAT_VARTABS
7776 if (p_vsts_nopaste)
7777 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007778 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7779 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
7782
7783 /*
7784 * Always set the option values, also when 'paste' is set when it is
7785 * already on.
7786 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007787 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007788 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007790 buf->b_p_tw = 0; // textwidth is 0
7791 buf->b_p_wm = 0; // wrapmargin is 0
7792 buf->b_p_sts = 0; // softtabstop is 0
7793 buf->b_p_ai = 0; // no auto-indent
7794 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007795#ifdef FEAT_VARTABS
7796 if (buf->b_p_vsts)
7797 free_string_option(buf->b_p_vsts);
7798 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007799 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 }
7802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007803 // set global options
7804 p_sm = 0; // no showmatch
7805 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007807 status_redraw_all(); // redraw to remove the ruler
7808 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007810 p_ri = 0; // no reverse insert
7811 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007813 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 p_tw = 0;
7815 p_wm = 0;
7816 p_sts = 0;
7817 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007818#ifdef FEAT_VARTABS
7819 if (p_vsts)
7820 free_string_option(p_vsts);
7821 p_vsts = empty_option;
7822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 }
7824
7825 /*
7826 * Paste switched from on to off: Restore saved values.
7827 */
7828 else if (old_p_paste)
7829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007830 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007831 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {
7833 buf->b_p_tw = buf->b_p_tw_nopaste;
7834 buf->b_p_wm = buf->b_p_wm_nopaste;
7835 buf->b_p_sts = buf->b_p_sts_nopaste;
7836 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007837 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007838#ifdef FEAT_VARTABS
7839 if (buf->b_p_vsts)
7840 free_string_option(buf->b_p_vsts);
7841 buf->b_p_vsts = buf->b_p_vsts_nopaste
7842 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007843 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007844 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007845 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007846 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007847 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007851 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007853 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007855 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857#ifdef FEAT_RIGHTLEFT
7858 p_ri = save_ri;
7859 p_hkmap = save_hkmap;
7860#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007861 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007862 p_ai = p_ai_nopaste;
7863 p_et = p_et_nopaste;
7864 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 p_tw = p_tw_nopaste;
7866 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007867#ifdef FEAT_VARTABS
7868 if (p_vsts)
7869 free_string_option(p_vsts);
7870 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873
7874 old_p_paste = p_paste;
7875}
7876
7877/*
7878 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7879 *
7880 * Reset 'compatible' and set the values for options that didn't get set yet
7881 * to the Vim defaults.
7882 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007883 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 */
7885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007886vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007888 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007889 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007890 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
7892 if (!option_was_set((char_u *)"cp"))
7893 {
7894 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007895 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7897 set_option_default(opt_idx, OPT_FREE, FALSE);
7898 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007899 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007901
7902 if (fname != NULL)
7903 {
7904 p = vim_getenv(envname, &dofree);
7905 if (p == NULL)
7906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007907 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007908 p = FullName_save(fname, FALSE);
7909 if (p != NULL)
7910 {
7911 vim_setenv(envname, p);
7912 vim_free(p);
7913 }
7914 }
7915 else if (dofree)
7916 vim_free(p);
7917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918}
7919
7920/*
7921 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7922 */
7923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007924change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007926 int opt_idx;
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 if (p_cp != on)
7929 {
7930 p_cp = on;
7931 compatible_set();
7932 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007933 opt_idx = findoption((char_u *)"cp");
7934 if (opt_idx >= 0)
7935 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936}
7937
7938/*
7939 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007940 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 */
7942 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007943option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 int idx;
7946
7947 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007948 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 return FALSE;
7950 if (options[idx].flags & P_WAS_SET)
7951 return TRUE;
7952 return FALSE;
7953}
7954
7955/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007956 * Reset the flag indicating option "name" was set.
7957 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007958 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007959reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007960{
7961 int idx = findoption(name);
7962
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007963 if (idx < 0)
7964 return FAIL;
7965
7966 options[idx].flags &= ~P_WAS_SET;
7967 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007968}
7969
7970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 * compatible_set() - Called when 'compatible' has been set or unset.
7972 *
7973 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7974 * flag) to a Vi compatible value.
7975 * When 'compatible' is unset: Set all options that have a different default
7976 * for Vim (without the P_VI_DEF flag) to that default.
7977 */
7978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007979compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 int opt_idx;
7982
Bram Moolenaardac13472019-09-16 21:06:21 +02007983 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7985 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7986 set_option_default(opt_idx, OPT_FREE, p_cp);
7987 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007988 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989}
7990
Bram Moolenaardac13472019-09-16 21:06:21 +02007991#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00007994 * Called when the 'breakat' option changes value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00007996 char *
7997did_set_breakat(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007999 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 int i;
8001
8002 for (i = 0; i < 256; i++)
8003 breakat_flags[i] = FALSE;
8004
8005 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008006 for (p = p_breakat; *p; p++)
8007 breakat_flags[*p] = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00008008
8009 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#endif
8012
8013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 * Check if backspacing over something is allowed.
8015 */
8016 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008017can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008018 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008020#ifdef FEAT_JOB_CHANNEL
8021 if (what == BS_START && bt_prompt(curbuf))
8022 return FALSE;
8023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 switch (*p_bs)
8025 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008026 case '3': return TRUE;
8027 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 case '1': return (what != BS_START);
8029 case '0': return FALSE;
8030 }
8031 return vim_strchr(p_bs, what) != NULL;
8032}
8033
8034/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008035 * Return the effective 'scrolloff' value for the current window, using the
8036 * global value when appropriate.
8037 */
8038 long
8039get_scrolloff_value(void)
8040{
8041 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8042}
8043
8044/*
8045 * Return the effective 'sidescrolloff' value for the current window, using the
8046 * global value when appropriate.
8047 */
8048 long
8049get_sidescrolloff_value(void)
8050{
8051 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8052}
8053
8054/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008055 * Get the local or global value of 'backupcopy'.
8056 */
8057 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008058get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008059{
8060 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8061}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008062
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008063#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008064/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008065 * Get the local or global value of 'formatlistpat'.
8066 */
8067 char_u *
8068get_flp_value(buf_T *buf)
8069{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008070 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8071 return p_flp;
8072 return buf->b_p_flp;
8073}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008074#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008075
8076/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008077 * Get the local or global value of the 'virtualedit' flags.
8078 */
8079 unsigned int
8080get_ve_flags(void)
8081{
Gary Johnson51ad8502021-08-03 18:33:08 +02008082 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8083 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008084}
8085
Bram Moolenaaree857022019-11-09 23:26:40 +01008086#if defined(FEAT_LINEBREAK) || defined(PROTO)
8087/*
8088 * Get the local or global value of 'showbreak'.
8089 */
8090 char_u *
8091get_showbreak_value(win_T *win)
8092{
8093 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8094 return p_sbr;
8095 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8096 return empty_option;
8097 return win->w_p_sbr;
8098}
8099#endif
8100
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008101#if defined(FEAT_EVAL) || defined(PROTO)
8102/*
8103 * Get window or buffer local options.
8104 */
8105 dict_T *
8106get_winbuf_options(int bufopt)
8107{
8108 dict_T *d;
8109 int opt_idx;
8110
8111 d = dict_alloc();
8112 if (d == NULL)
8113 return NULL;
8114
Bram Moolenaardac13472019-09-16 21:06:21 +02008115 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008116 {
8117 struct vimoption *opt = &options[opt_idx];
8118
8119 if ((bufopt && (opt->indir & PV_BUF))
8120 || (!bufopt && (opt->indir & PV_WIN)))
8121 {
8122 char_u *varp = get_varp(opt);
8123
8124 if (varp != NULL)
8125 {
8126 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008127 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008128 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008129 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008130 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008131 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008132 }
8133 }
8134 }
8135
8136 return d;
8137}
8138#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008139
Bram Moolenaardac13472019-09-16 21:06:21 +02008140#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008141/*
8142 * This is called when 'culopt' is changed
8143 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008144 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008145fill_culopt_flags(char_u *val, win_T *wp)
8146{
8147 char_u *p;
8148 char_u culopt_flags_new = 0;
8149
8150 if (val == NULL)
8151 p = wp->w_p_culopt;
8152 else
8153 p = val;
8154 while (*p != NUL)
8155 {
8156 if (STRNCMP(p, "line", 4) == 0)
8157 {
8158 p += 4;
8159 culopt_flags_new |= CULOPT_LINE;
8160 }
8161 else if (STRNCMP(p, "both", 4) == 0)
8162 {
8163 p += 4;
8164 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8165 }
8166 else if (STRNCMP(p, "number", 6) == 0)
8167 {
8168 p += 6;
8169 culopt_flags_new |= CULOPT_NBR;
8170 }
8171 else if (STRNCMP(p, "screenline", 10) == 0)
8172 {
8173 p += 10;
8174 culopt_flags_new |= CULOPT_SCRLINE;
8175 }
8176
8177 if (*p != ',' && *p != NUL)
8178 return FAIL;
8179 if (*p == ',')
8180 ++p;
8181 }
8182
8183 // Can't have both "line" and "screenline".
8184 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8185 return FAIL;
8186 wp->w_p_culopt_flags = culopt_flags_new;
8187
8188 return OK;
8189}
8190#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008191
8192/*
8193 * Get the value of 'magic' adjusted for Vim9 script.
8194 */
8195 int
8196magic_isset(void)
8197{
8198 switch (magic_overruled)
8199 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008200 case OPTION_MAGIC_ON: return TRUE;
8201 case OPTION_MAGIC_OFF: return FALSE;
8202 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008203 }
8204#ifdef FEAT_EVAL
8205 if (in_vim9script())
8206 return TRUE;
8207#endif
8208 return p_magic;
8209}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008210
8211/*
8212 * Set the callback function value for an option that accepts a function name,
8213 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8214 * Returns OK if the option is successfully set to a function, otherwise
8215 * returns FAIL.
8216 */
8217 int
8218option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8219{
8220#ifdef FEAT_EVAL
8221 typval_T *tv;
8222 callback_T cb;
8223
8224 if (optval == NULL || *optval == NUL)
8225 {
8226 free_callback(optcb);
8227 return OK;
8228 }
8229
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008230 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008231 || (STRNCMP(optval, "function(", 9) == 0)
8232 || (STRNCMP(optval, "funcref(", 8) == 0))
8233 // Lambda expression or a funcref
8234 tv = eval_expr(optval, NULL);
8235 else
8236 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008237 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008238 if (tv == NULL)
8239 return FAIL;
8240
8241 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008242 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008243 {
8244 free_tv(tv);
8245 return FAIL;
8246 }
8247
8248 free_callback(optcb);
8249 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008250 if (cb.cb_free_name)
8251 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008252 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008253
8254 // when using Vim9 style "import.funcname" it needs to be expanded to
8255 // "import#funcname".
8256 expand_autload_callback(optcb);
8257
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008258 return OK;
8259#else
8260 return FAIL;
8261#endif
8262}