blob: 1203180fb6e7a11cf98183706163a9b534af69df [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
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000919 // 'scroll' defaults to half the window height. The stored default is zero,
920 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000921 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000922 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000923 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 comp_col();
925
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000926 // 'window' is only for backwards compatibility with Vi.
927 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000928 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000929 p_window = Rows - 1;
930 set_number_default("window", Rows - 1);
931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100932 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100933#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000934 // If 'background' wasn't set by the user, try guessing the value,
935 // depending on the terminal name. Only need to check for terminals
936 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000937 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000938 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
939 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000941 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100942 // don't mark it as set, when starting the GUI it may be
943 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000944 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000947
948#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000949 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000950#endif
951#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000952 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000953#endif
954#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000955 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Initialize the options, part three: After reading the .vimrc
961 */
962 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100963set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100965#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966/*
967 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
968 * This is done after other initializations, where 'shell' might have been
969 * set, but only if they have not been set before.
970 */
971 char_u *p;
972 int idx_srr;
973 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100974# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 int idx_sp;
976 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000980 if (idx_srr < 0)
981 do_srr = FALSE;
982 else
983 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000986 if (idx_sp < 0)
987 do_sp = FALSE;
988 else
989 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200991 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (p != NULL)
993 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000994 // Default for p_sp is "| tee", for p_srr is ">".
995 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if ( fnamecmp(p, "csh") == 0
997 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100998# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 || fnamecmp(p, "csh.exe") == 0
1000 || fnamecmp(p, "tcsh.exe") == 0
1001# endif
1002 )
1003 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001004# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (do_sp)
1006 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001007# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001009# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1013 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (do_srr)
1016 {
1017 p_srr = (char_u *)">&";
1018 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1019 }
1020 }
Mike Williams12795022021-06-28 20:53:58 +02001021# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001022 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1023 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001024 else if ( fnamecmp(p, "powershell") == 0
1025 || fnamecmp(p, "powershell.exe") == 0
1026 )
1027 {
1028# if defined(FEAT_QUICKFIX)
1029 if (do_sp)
1030 {
1031 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1032 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1033 }
1034# endif
1035 if (do_srr)
1036 {
1037 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1038 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1039 }
1040 }
1041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 else
Natanael Copa56318362021-05-06 18:46:35 +02001043 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if ( fnamecmp(p, "sh") == 0
1045 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001046 || fnamecmp(p, "mksh") == 0
1047 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001049 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001051 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001052 || fnamecmp(p, "ash") == 0
1053 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001054 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001055# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 || fnamecmp(p, "cmd") == 0
1057 || fnamecmp(p, "sh.exe") == 0
1058 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001059 || fnamecmp(p, "mksh.exe") == 0
1060 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001062 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 || fnamecmp(p, "bash.exe") == 0
1064 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001065 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001066 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001068 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001070# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (do_sp)
1072 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001073# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001075# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001076 if (fnamecmp(p, "pwsh") == 0)
1077 p_sp = (char_u *)">%s 2>&1";
1078 else
1079 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1082 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (do_srr)
1085 {
1086 p_srr = (char_u *)">%s 2>&1";
1087 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1088 }
1089 }
1090 vim_free(p);
1091 }
1092#endif
1093
Bram Moolenaar4f974752019-02-17 17:44:42 +01001094#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001096 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1097 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001099 * set, but only if they have not been set before.
1100 * Default values depend on shell (cmd.exe is default shell):
1101 *
1102 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001103 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001104 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001105 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001106 * "sh" like shells - "-c" "\""
1107 *
1108 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 */
Mike Williams12795022021-06-28 20:53:58 +02001110 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1111 {
1112 int idx_opt;
1113
1114 idx_opt = findoption((char_u *)"shcf");
1115 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1116 {
1117 p_shcf = (char_u*)"-Command";
1118 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1119 }
1120
1121 idx_opt = findoption((char_u *)"sxq");
1122 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1123 {
1124 p_sxq = (char_u*)"\"";
1125 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1126 }
1127 }
1128 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
1130 int idx3;
1131
1132 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001133 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
1135 p_shcf = (char_u *)"-c";
1136 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1137 }
1138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001139 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 idx3 = findoption((char_u *)"sxq");
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_sxq = (char_u *)"\"";
1144 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001147 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1148 {
1149 int idx3;
1150
1151 /*
1152 * cmd.exe on Windows will strip the first and last double quote given
1153 * on the command line, e.g. most of the time things like:
1154 * cmd /c "my path/to/echo" "my args to echo"
1155 * become:
1156 * my path/to/echo" "my args to echo
1157 * when executed.
1158 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001159 * To avoid this, set shellxquote to surround the command in
1160 * parenthesis. This appears to make most commands work, without
1161 * breaking commands that worked previously, such as
1162 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001163 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001164 idx3 = findoption((char_u *)"sxq");
1165 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1166 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001167 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001168 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1169 }
1170
Bram Moolenaara64ba222012-02-12 23:23:31 +01001171 idx3 = findoption((char_u *)"shcf");
1172 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1173 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001174 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001175 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1176 }
1177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001180 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001181 {
1182 int idx_ffs = findoption((char_u *)"ffs");
1183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001184 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001185 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1186 set_fileformat(default_fileformat(), OPT_LOCAL);
1187 }
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190}
1191
1192#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1193/*
1194 * When 'helplang' is still at its default value, set it to "lang".
1195 * Only the first two characters of "lang" are used.
1196 */
1197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001198set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
1200 int idx;
1201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001202 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 return;
1204 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001205 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1206 return;
1207
1208 if (options[idx].flags & P_ALLOCED)
1209 free_string_option(p_hlg);
1210 p_hlg = vim_strsave(lang);
1211 if (p_hlg == NULL)
1212 p_hlg = empty_option;
1213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001215 // zh_CN becomes "cn", zh_TW becomes "tw"
1216 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001217 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001218 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1219 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001220 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001221 // any C like setting, such as C.UTF-8, becomes "en"
1222 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1223 {
1224 p_hlg[0] = 'e';
1225 p_hlg[1] = 'n';
1226 }
1227 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001229 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230}
1231#endif
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233/*
1234 * 'title' and 'icon' only default to true if they have not been set or reset
1235 * in .vimrc and we can read the old value.
1236 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1237 * they can be reset. This reduces startup time when using X on a remote
1238 * machine.
1239 */
1240 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001241set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 int idx1;
1244 long val;
1245
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001246 // If GUI is (going to be) used, we can always set the window title and
1247 // icon name. Saves a bit of time, because the X11 display server does
1248 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001250 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252#ifdef FEAT_GUI
1253 if (gui.starting || gui.in_use)
1254 val = TRUE;
1255 else
1256#endif
1257 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001258 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 p_title = val;
1260 }
1261 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001262 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001264 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001266
1267#ifdef FEAT_GUI
1268 if (gui.starting || gui.in_use)
1269 val = TRUE;
1270 else
1271#endif
1272 val = mch_can_restore_icon();
1273 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1274 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001277 void
1278ex_set(exarg_T *eap)
1279{
1280 int flags = 0;
1281
1282 if (eap->cmdidx == CMD_setlocal)
1283 flags = OPT_LOCAL;
1284 else if (eap->cmdidx == CMD_setglobal)
1285 flags = OPT_GLOBAL;
1286#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001287 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001288 ex_options(eap);
1289 else
1290#endif
1291 {
1292 if (eap->forceit)
1293 flags |= OPT_ONECOLUMN;
1294 (void)do_set(eap->arg, flags);
1295 }
1296}
1297
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001298/*
1299 * :set operator types
1300 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001301typedef enum {
1302 OP_NONE = 0,
1303 OP_ADDING, // "opt+=arg"
1304 OP_PREPENDING, // "opt^=arg"
1305 OP_REMOVING, // "opt-=arg"
1306} set_op_T;
1307
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001308typedef enum {
1309 PREFIX_NO = 0, // "no" prefix
1310 PREFIX_NONE, // no prefix
1311 PREFIX_INV, // "inv" prefix
1312} set_prefix_T;
1313
1314/*
1315 * Return the prefix type for the option name in *argp.
1316 */
1317 static set_prefix_T
1318get_option_prefix(char_u **argp)
1319{
1320 int prefix = PREFIX_NONE;
1321 char_u *arg = *argp;
1322
1323 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1324 {
1325 prefix = PREFIX_NO;
1326 arg += 2;
1327 }
1328 else if (STRNCMP(arg, "inv", 3) == 0)
1329 {
1330 prefix = PREFIX_INV;
1331 arg += 3;
1332 }
1333
1334 *argp = arg;
1335 return prefix;
1336}
1337
1338/*
1339 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1340 * and the option name length in "*lenp". For a <t_xx> option, return the key
1341 * number in "*keyp".
1342 *
1343 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1344 * otherwise returns OK.
1345 */
1346 static int
1347parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1348{
1349 int key = 0;
1350 int len;
1351 int opt_idx;
1352
1353 if (*arg == '<')
1354 {
1355 opt_idx = -1;
1356 // look out for <t_>;>
1357 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1358 len = 5;
1359 else
1360 {
1361 len = 1;
1362 while (arg[len] != NUL && arg[len] != '>')
1363 ++len;
1364 }
1365 if (arg[len] != '>')
1366 return FAIL;
1367
1368 arg[len] = NUL; // put NUL after name
1369 if (arg[1] == 't' && arg[2] == '_') // could be term code
1370 opt_idx = findoption(arg + 1);
1371 arg[len++] = '>'; // restore '>'
1372 if (opt_idx == -1)
1373 key = find_key_option(arg + 1, TRUE);
1374 }
1375 else
1376 {
1377 int nextchar; // next non-white char after option name
1378
1379 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001380 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001381 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1382 len = 4;
1383 else
1384 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1385 ++len;
1386 nextchar = arg[len];
1387 arg[len] = NUL; // put NUL after name
1388 opt_idx = findoption(arg);
1389 arg[len] = nextchar; // restore nextchar
1390 if (opt_idx == -1)
1391 key = find_key_option(arg, FALSE);
1392 }
1393
1394 *keyp = key;
1395 *lenp = len;
1396 *opt_idxp = opt_idx;
1397
1398 return OK;
1399}
1400
1401/*
1402 * Get the option operator (+=, ^=, -=).
1403 */
1404 static set_op_T
1405get_opt_op(char_u *arg)
1406{
1407 set_op_T op = OP_NONE;
1408
1409 if (*arg != NUL && *(arg + 1) == '=')
1410 {
1411 if (*arg == '+')
1412 op = OP_ADDING; // "+="
1413 else if (*arg == '^')
1414 op = OP_PREPENDING; // "^="
1415 else if (*arg == '-')
1416 op = OP_REMOVING; // "-="
1417 }
1418
1419 return op;
1420}
1421
1422/*
1423 * Validate whether the value of the option in "opt_idx" can be changed.
1424 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1425 * if it can be changed.
1426 */
1427 static int
1428validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1429{
1430 // Skip all options that are not window-local (used when showing
1431 // an already loaded buffer in a window).
1432 if ((opt_flags & OPT_WINONLY)
1433 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1434 return FAIL;
1435
1436 // Skip all options that are window-local (used for :vimgrep).
1437 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1438 && options[opt_idx].var == VAR_WIN)
1439 return FAIL;
1440
1441 // Disallow changing some options from modelines.
1442 if (opt_flags & OPT_MODELINE)
1443 {
1444 if (flags & (P_SECURE | P_NO_ML))
1445 {
1446 *errmsg = e_not_allowed_in_modeline;
1447 return FAIL;
1448 }
1449 if ((flags & P_MLE) && !p_mle)
1450 {
1451 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1452 return FAIL;
1453 }
1454#ifdef FEAT_DIFF
1455 // In diff mode some options are overruled. This avoids that
1456 // 'foldmethod' becomes "marker" instead of "diff" and that
1457 // "wrap" gets set.
1458 if (curwin->w_p_diff
1459 && opt_idx >= 0 // shut up coverity warning
1460 && (
1461# ifdef FEAT_FOLDING
1462 options[opt_idx].indir == PV_FDM ||
1463# endif
1464 options[opt_idx].indir == PV_WRAP))
1465 return FAIL;
1466#endif
1467 }
1468
1469#ifdef HAVE_SANDBOX
1470 // Disallow changing some options in the sandbox
1471 if (sandbox != 0 && (flags & P_SECURE))
1472 {
1473 *errmsg = e_not_allowed_in_sandbox;
1474 return FAIL;
1475 }
1476#endif
1477
1478 return OK;
1479}
1480
Bram Moolenaar47403942022-09-21 21:12:53 +01001481/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001482 * Get the Vim/Vi default value for a string option.
1483 */
1484 static char_u *
1485stropt_get_default_val(
1486 int opt_idx,
1487 char_u *varp,
1488 int flags,
1489 int cp_val)
1490{
1491 char_u *newval;
1492
1493 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1494 ? VI_DEFAULT : VIM_DEFAULT];
1495 if ((char_u **)varp == &p_bg)
1496 {
1497 // guess the value of 'background'
1498#ifdef FEAT_GUI
1499 if (gui.in_use)
1500 newval = gui_bg_default();
1501 else
1502#endif
1503 newval = term_bg_default();
1504 }
1505 else if ((char_u **)varp == &p_fencs && enc_utf8)
1506 newval = fencs_utf8_default;
1507
1508 // expand environment variables and ~ since the default value was
1509 // already expanded, only required when an environment variable was set
1510 // later
1511 if (newval == NULL)
1512 newval = empty_option;
1513 else
1514 {
1515 char_u *s = option_expand(opt_idx, newval);
1516 if (s == NULL)
1517 s = newval;
1518 newval = vim_strsave(s);
1519 }
1520
1521 return newval;
1522}
1523
1524/*
1525 * Convert the 'backspace' option number value to a string: for adding,
1526 * prepending and removing string.
1527 */
1528 static void
1529opt_backspace_nr2str(
1530 char_u *varp,
1531 char_u **origval_p,
1532 char_u **origval_l_p,
1533 char_u **origval_g_p,
1534 char_u **oldval_p)
1535{
1536 int i = getdigits((char_u **)varp);
1537
1538 switch (i)
1539 {
1540 case 0:
1541 *(char_u **)varp = empty_option;
1542 break;
1543 case 1:
1544 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1545 break;
1546 case 2:
1547 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1548 break;
1549 case 3:
1550 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1551 break;
1552 }
1553 vim_free(*oldval_p);
1554 if (*origval_p == *oldval_p)
1555 *origval_p = *(char_u **)varp;
1556 if (*origval_l_p == *oldval_p)
1557 *origval_l_p = *(char_u **)varp;
1558 if (*origval_g_p == *oldval_p)
1559 *origval_g_p = *(char_u **)varp;
1560 *oldval_p = *(char_u **)varp;
1561}
1562
1563/*
1564 * Convert the 'whichwrap' option number value to a string, for backwards
1565 * compatibility with Vim 3.0.
1566 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1567 */
1568 static char_u *
1569opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1570{
1571 *whichwrap = NUL;
1572 int i = getdigits(argp);
1573 if (i & 1)
1574 STRCAT(whichwrap, "b,");
1575 if (i & 2)
1576 STRCAT(whichwrap, "s,");
1577 if (i & 4)
1578 STRCAT(whichwrap, "h,l,");
1579 if (i & 8)
1580 STRCAT(whichwrap, "<,>,");
1581 if (i & 16)
1582 STRCAT(whichwrap, "[,],");
1583 if (*whichwrap != NUL) // remove trailing ,
1584 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1585
1586 return whichwrap;
1587}
1588
1589/*
1590 * Copy the new string value into allocated memory for the option.
1591 * Can't use set_string_option_direct(), because we need to remove the
1592 * backslashes.
1593 */
1594 static char_u *
1595stropt_copy_value(
1596 char_u *origval,
1597 char_u **argp,
1598 set_op_T op,
1599 int flags UNUSED)
1600{
1601 char_u *arg = *argp;
1602 unsigned newlen;
1603 char_u *newval;
1604 char_u *s = NULL;
1605
1606 // get a bit too much
1607 newlen = (unsigned)STRLEN(arg) + 1;
1608 if (op != OP_NONE)
1609 newlen += (unsigned)STRLEN(origval) + 1;
1610 newval = alloc(newlen);
1611 if (newval == NULL) // out of mem, don't change
1612 return NULL;
1613 s = newval;
1614
1615 // Copy the string, skip over escaped chars.
1616 // For MS-DOS and WIN32 backslashes before normal file name characters
1617 // are not removed, and keep backslash at start, for "\\machine\path",
1618 // but do remove it for "\\\\machine\\path".
1619 // The reverse is found in ExpandOldSetting().
1620 while (*arg != NUL && !VIM_ISWHITE(*arg))
1621 {
1622 int i;
1623
1624 if (*arg == '\\' && arg[1] != NUL
1625#ifdef BACKSLASH_IN_FILENAME
1626 && !((flags & P_EXPAND)
1627 && vim_isfilec(arg[1])
1628 && !VIM_ISWHITE(arg[1])
1629 && (arg[1] != '\\'
1630 || (s == newval && arg[2] != '\\')))
1631#endif
1632 )
1633 ++arg; // remove backslash
1634 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1635 {
1636 // copy multibyte char
1637 mch_memmove(s, arg, (size_t)i);
1638 arg += i;
1639 s += i;
1640 }
1641 else
1642 *s++ = *arg++;
1643 }
1644 *s = NUL;
1645
1646 *argp = arg;
1647 return newval;
1648}
1649
1650/*
1651 * Expand environment variables and ~ in string option value 'newval'.
1652 */
1653 static char_u *
1654stropt_expand_envvar(
1655 int opt_idx,
1656 char_u *origval,
1657 char_u *newval,
1658 set_op_T op)
1659{
1660 char_u *s = option_expand(opt_idx, newval);
1661 if (s == NULL)
1662 return newval;
1663
1664 vim_free(newval);
1665 unsigned newlen = (unsigned)STRLEN(s) + 1;
1666 if (op != OP_NONE)
1667 newlen += (unsigned)STRLEN(origval) + 1;
1668
1669 newval = alloc(newlen);
1670 if (newval == NULL)
1671 return NULL;
1672
1673 STRCPY(newval, s);
1674
1675 return newval;
1676}
1677
1678/*
1679 * Concatenate the original and new values of a string option, adding a "," if
1680 * needed.
1681 */
1682 static void
1683stropt_concat_with_comma(
1684 char_u *origval,
1685 char_u *newval,
1686 set_op_T op,
1687 int flags)
1688{
1689 int len = 0;
1690
1691 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1692 if (op == OP_ADDING)
1693 {
1694 len = (int)STRLEN(origval);
1695 // strip a trailing comma, would get 2
1696 if (comma && len > 1
1697 && (flags & P_ONECOMMA) == P_ONECOMMA
1698 && origval[len - 1] == ','
1699 && origval[len - 2] != '\\')
1700 len--;
1701 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1702 mch_memmove(newval, origval, (size_t)len);
1703 }
1704 else
1705 {
1706 len = (int)STRLEN(newval);
1707 STRMOVE(newval + len + comma, origval);
1708 }
1709 if (comma)
1710 newval[len] = ',';
1711}
1712
1713/*
1714 * Remove a value from a string option. Copy string option value in "origval"
1715 * to "newval" and then remove the string "strval" of length "len".
1716 */
1717 static void
1718stropt_remove_val(
1719 char_u *origval,
1720 char_u *newval,
1721 int flags,
1722 char_u *strval,
1723 int len)
1724{
1725 // Remove newval[] from origval[]. (Note: "len" has been set above
1726 // and is used here).
1727 STRCPY(newval, origval);
1728 if (*strval)
1729 {
1730 // may need to remove a comma
1731 if (flags & P_COMMA)
1732 {
1733 if (strval == origval)
1734 {
1735 // include comma after string
1736 if (strval[len] == ',')
1737 ++len;
1738 }
1739 else
1740 {
1741 // include comma before string
1742 --strval;
1743 ++len;
1744 }
1745 }
1746 STRMOVE(newval + (strval - origval), strval + len);
1747 }
1748}
1749
1750/*
1751 * Remove flags that appear twice in the string option value 'newval'.
1752 */
1753 static void
1754stropt_remove_dupflags(char_u *newval, int flags)
1755{
1756 char_u *s = newval;
1757
1758 // Remove flags that appear twice.
1759 while (*s)
1760 {
1761 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1762 if (flags & P_ONECOMMA)
1763 {
1764 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1765 {
1766 // Remove the duplicated value and the next comma.
1767 STRMOVE(s, s + 2);
1768 continue;
1769 }
1770 }
1771 else
1772 {
1773 if ((!(flags & P_COMMA) || *s != ',')
1774 && vim_strchr(s + 1, *s) != NULL)
1775 {
1776 STRMOVE(s, s + 1);
1777 continue;
1778 }
1779 }
1780 ++s;
1781 }
1782}
1783
1784/*
1785 * Get the string value specified for a ":set" command. The following set
1786 * options are supported:
1787 * set {opt}&
1788 * set {opt}<
1789 * set {opt}={val}
1790 * set {opt}:{val}
1791 */
1792 static char_u *
1793stropt_get_newval(
1794 int nextchar,
1795 int opt_idx,
1796 char_u **argp,
1797 char_u *varp,
1798 char_u **origval_arg,
1799 char_u **origval_l_arg,
1800 char_u **origval_g_arg,
1801 char_u **oldval_arg,
1802 set_op_T *op_arg,
1803 int flags,
1804 int cp_val)
1805{
1806 char_u *arg = *argp;
1807 char_u *origval = *origval_arg;
1808 char_u *origval_l = *origval_l_arg;
1809 char_u *origval_g = *origval_g_arg;
1810 char_u *oldval = *oldval_arg;
1811 set_op_T op = *op_arg;
1812 char_u *save_arg = NULL;
1813 char_u *newval;
1814 char_u *s = NULL;
1815 char_u whichwrap[80];
1816
1817 if (nextchar == '&') // set to default val
1818 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1819 else if (nextchar == '<') // set to global val
1820 newval = vim_strsave(*(char_u **)get_varp_scope(
1821 &(options[opt_idx]), OPT_GLOBAL));
1822 else
1823 {
1824 ++arg; // jump to after the '=' or ':'
1825
1826 // Set 'keywordprg' to ":help" if an empty
1827 // value was passed to :set by the user.
1828 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1829 {
1830 save_arg = arg;
1831 arg = (char_u *)":help";
1832 }
1833 // Convert 'backspace' number to string
1834 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1835 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1836 &oldval);
1837 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1838 {
1839 // Convert 'whichwrap' number to string, for backwards
1840 // compatibility with Vim 3.0.
1841 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1842 save_arg = arg;
1843 arg = t;
1844 }
1845 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1846 // version 3.0
1847 else if (*arg == '>' && (varp == (char_u *)&p_dir
1848 || varp == (char_u *)&p_bdir))
1849 ++arg;
1850
1851 // Copy the new string into allocated memory.
1852 newval = stropt_copy_value(origval, &arg, op, flags);
1853 if (newval == NULL)
1854 goto done;
1855
1856 // Expand environment variables and ~.
1857 // Don't do it when adding without inserting a comma.
1858 if (op == OP_NONE || (flags & P_COMMA))
1859 {
1860 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1861 if (newval == NULL)
1862 goto done;
1863 }
1864
1865 // locate newval[] in origval[] when removing it and when adding to
1866 // avoid duplicates
1867 int len = 0;
1868 if (op == OP_REMOVING || (flags & P_NODUP))
1869 {
1870 len = (int)STRLEN(newval);
1871 s = find_dup_item(origval, newval, flags);
1872
1873 // do not add if already there
1874 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1875 {
1876 op = OP_NONE;
1877 STRCPY(newval, origval);
1878 }
1879
1880 // if no duplicate, move pointer to end of original value
1881 if (s == NULL)
1882 s = origval + (int)STRLEN(origval);
1883 }
1884
1885 // concatenate the two strings; add a ',' if needed
1886 if (op == OP_ADDING || op == OP_PREPENDING)
1887 stropt_concat_with_comma(origval, newval, op, flags);
1888 else if (op == OP_REMOVING)
1889 // Remove newval[] from origval[]. (Note: "len" has been set above
1890 // and is used here).
1891 stropt_remove_val(origval, newval, flags, s, len);
1892
1893 if (flags & P_FLAGLIST)
1894 // Remove flags that appear twice.
1895 stropt_remove_dupflags(newval, flags);
1896 }
1897
1898done:
1899 if (save_arg != NULL)
1900 arg = save_arg; // arg was temporarily changed, restore it
1901 *argp = arg;
1902 *origval_arg = origval;
1903 *origval_l_arg = origval_l;
1904 *origval_g_arg = origval_g;
1905 *oldval_arg = oldval;
1906 *op_arg = op;
1907
1908 return newval;
1909}
1910
1911/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001912 * Part of do_set() for string options.
1913 * Returns FAIL on failure, do not process further options.
1914 */
1915 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001916do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001917 int opt_idx,
1918 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001919 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001920 int nextchar,
1921 set_op_T op_arg,
1922 int flags,
1923 int cp_val,
1924 char_u *varp_arg,
1925 char *errbuf,
1926 int *value_checked,
1927 char **errmsg)
1928{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001929 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001930 set_op_T op = op_arg;
1931 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001932 char_u *oldval = NULL; // previous value if *varp
1933 char_u *newval;
1934 char_u *origval = NULL;
1935 char_u *origval_l = NULL;
1936 char_u *origval_g = NULL;
1937#if defined(FEAT_EVAL)
1938 char_u *saved_origval = NULL;
1939 char_u *saved_origval_l = NULL;
1940 char_u *saved_origval_g = NULL;
1941 char_u *saved_newval = NULL;
1942#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001943
1944 // When using ":set opt=val" for a global option
1945 // with a local value the local value will be
1946 // reset, use the global value here.
1947 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1948 && ((int)options[opt_idx].indir & PV_BOTH))
1949 varp = options[opt_idx].var;
1950
1951 // The old value is kept until we are sure that the new value is valid.
1952 oldval = *(char_u **)varp;
1953
1954 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1955 {
1956 origval_l = *(char_u **)get_varp_scope(
1957 &(options[opt_idx]), OPT_LOCAL);
1958 origval_g = *(char_u **)get_varp_scope(
1959 &(options[opt_idx]), OPT_GLOBAL);
1960
1961 // A global-local string option might have an empty option as value to
1962 // indicate that the global value should be used.
1963 if (((int)options[opt_idx].indir & PV_BOTH)
1964 && origval_l == empty_option)
1965 origval_l = origval_g;
1966 }
1967
1968 // When setting the local value of a global option, the old value may be
1969 // the global value.
1970 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1971 origval = *(char_u **)get_varp(&options[opt_idx]);
1972 else
1973 origval = oldval;
1974
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001975 // Get the new value for the option
1976 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1977 &origval_l, &origval_g, &oldval, &op, flags,
1978 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001979
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001980 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001981 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001982 if (newval == NULL)
1983 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001984
1985#if defined(FEAT_EVAL)
1986 if (!starting
1987# ifdef FEAT_CRYPT
1988 && options[opt_idx].indir != PV_KEY
1989# endif
1990 && origval != NULL && newval != NULL)
1991 {
1992 // origval may be freed by did_set_string_option(), make a copy.
1993 saved_origval = vim_strsave(origval);
1994 // newval (and varp) may become invalid if the buffer is closed by
1995 // autocommands.
1996 saved_newval = vim_strsave(newval);
1997 if (origval_l != NULL)
1998 saved_origval_l = vim_strsave(origval_l);
1999 if (origval_g != NULL)
2000 saved_origval_g = vim_strsave(origval_g);
2001 }
2002#endif
2003
2004 {
2005 long_u *p = insecure_flag(opt_idx, opt_flags);
2006 int secure_saved = secure;
2007
2008 // When an option is set in the sandbox, from a modeline or in secure
2009 // mode, then deal with side effects in secure mode. Also when the
2010 // value was set with the P_INSECURE flag and is not completely
2011 // replaced.
2012 if ((opt_flags & OPT_MODELINE)
2013#ifdef HAVE_SANDBOX
2014 || sandbox != 0
2015#endif
2016 || (op != OP_NONE && (*p & P_INSECURE)))
2017 secure = 1;
2018
2019 // Handle side effects, and set the global value for ":set" on local
2020 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2021 // be triggered that can cause havoc.
2022 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002023 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Bram Moolenaar47403942022-09-21 21:12:53 +01002024 opt_flags, value_checked);
2025
2026 secure = secure_saved;
2027 }
2028
2029#if defined(FEAT_EVAL)
2030 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002031 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002032 saved_origval_l, saved_origval_g, saved_newval);
2033 vim_free(saved_origval);
2034 vim_free(saved_origval_l);
2035 vim_free(saved_origval_g);
2036 vim_free(saved_newval);
2037#endif
2038
Bram Moolenaar6f981142022-09-22 12:48:58 +01002039 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002040 return *errmsg == NULL ? OK : FAIL;
2041}
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002044 * Set a boolean option.
2045 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002046 */
2047 static char *
2048do_set_option_bool(
2049 int opt_idx,
2050 int opt_flags,
2051 set_prefix_T prefix,
2052 long_u flags,
2053 char_u *varp,
2054 int nextchar,
2055 int afterchar,
2056 int cp_val)
2057
2058{
2059 varnumber_T value;
2060
2061 if (nextchar == '=' || nextchar == ':')
2062 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002063 if (opt_idx < 0 || varp == NULL)
2064 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002065
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002066 // ":set opt!": invert
2067 // ":set opt&": reset to default value
2068 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002069 if (nextchar == '!')
2070 value = *(int *)(varp) ^ 1;
2071 else if (nextchar == '&')
2072 value = (int)(long)(long_i)options[opt_idx].def_val[
2073 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2074 else if (nextchar == '<')
2075 {
2076 // For 'autoread' -1 means to use global value.
2077 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2078 value = -1;
2079 else
2080 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2081 }
2082 else
2083 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002084 // ":set invopt": invert
2085 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002086 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2087 return e_trailing_characters;
2088 if (prefix == PREFIX_INV)
2089 value = *(int *)(varp) ^ 1;
2090 else
2091 value = prefix == PREFIX_NO ? 0 : 1;
2092 }
2093
2094 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2095}
2096
2097/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002098 * Set a numeric option.
2099 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002100 */
2101 static char *
2102do_set_option_numeric(
2103 int opt_idx,
2104 int opt_flags,
2105 char_u **argp,
2106 int nextchar,
2107 set_op_T op,
2108 long_u flags,
2109 int cp_val,
2110 char_u *varp,
2111 char *errbuf,
2112 size_t errbuflen)
2113{
2114 char_u *arg = *argp;
2115 varnumber_T value;
2116 int i;
2117 char *errmsg = NULL;
2118
Bram Moolenaar546933f2023-02-06 16:40:49 +00002119 if (opt_idx < 0 || varp == NULL)
2120 return NULL; // "cannot happen"
2121 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002122 /*
2123 * Different ways to set a number option:
2124 * & set to default value
2125 * < set to global value
2126 * <xx> accept special key codes for 'wildchar'
2127 * c accept any non-digit for 'wildchar'
2128 * [-]0-9 set number
2129 * other error
2130 */
2131 ++arg;
2132 if (nextchar == '&')
2133 value = (long)(long_i)options[opt_idx].def_val[
2134 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2135 else if (nextchar == '<')
2136 {
2137 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2138 // use the global value.
2139 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2140 value = NO_LOCAL_UNDOLEVEL;
2141 else
2142 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2143 }
2144 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2145 && (*arg == '<'
2146 || *arg == '^'
2147 || (*arg != NUL
2148 && (!arg[1] || VIM_ISWHITE(arg[1]))
2149 && !VIM_ISDIGIT(*arg))))
2150 {
2151 value = string_to_key(arg, FALSE);
2152 if (value == 0 && (long *)varp != &p_wcm)
2153 {
2154 errmsg = e_invalid_argument;
2155 goto skip;
2156 }
2157 }
2158 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2159 {
2160 // Allow negative (for 'undolevels'), octal and hex numbers.
2161 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
2162 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2163 {
2164 errmsg = e_number_required_after_equal;
2165 goto skip;
2166 }
2167 }
2168 else
2169 {
2170 errmsg = e_number_required_after_equal;
2171 goto skip;
2172 }
2173
2174 if (op == OP_ADDING)
2175 value = *(long *)varp + value;
2176 else if (op == OP_PREPENDING)
2177 value = *(long *)varp * value;
2178 else if (op == OP_REMOVING)
2179 value = *(long *)varp - value;
2180
2181 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2182 opt_flags);
2183
2184skip:
2185 *argp = arg;
2186 return errmsg;
2187}
2188
2189/*
2190 * Set a key code (t_xx) option
2191 */
2192 static char *
2193do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2194{
2195 char_u *arg = *argp;
2196 char_u *p;
2197
2198 if (nextchar == '&')
2199 {
2200 if (add_termcap_entry(key_name, TRUE) == FAIL)
2201 return e_not_found_in_termcap;
2202 }
2203 else
2204 {
2205 ++arg; // jump to after the '=' or ':'
2206 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2207 if (*p == '\\' && p[1] != NUL)
2208 ++p;
2209 nextchar = *p;
2210 *p = NUL;
2211 add_termcode(key_name, arg, FALSE);
2212 *p = nextchar;
2213 }
2214 if (full_screen)
2215 ttest(FALSE);
2216 redraw_all_later(UPD_CLEAR);
2217
2218 *argp = arg;
2219 return NULL;
2220}
2221
2222/*
2223 * Set an option to a new value.
2224 */
2225 static char *
2226do_set_option_value(
2227 int opt_idx,
2228 int opt_flags,
2229 char_u **argp,
2230 set_prefix_T prefix,
2231 set_op_T op,
2232 long_u flags,
2233 char_u *varp,
2234 char_u *key_name,
2235 int nextchar,
2236 int afterchar,
2237 int cp_val,
2238 int *stopopteval,
2239 char *errbuf,
2240 size_t errbuflen)
2241{
2242 int value_checked = FALSE;
2243 char *errmsg = NULL;
2244 char_u *arg = *argp;
2245
2246 if (flags & P_BOOL)
2247 {
2248 // boolean option
2249 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2250 nextchar, afterchar, cp_val);
2251 if (errmsg != NULL)
2252 goto skip;
2253 }
2254 else
2255 {
2256 // numeric or string option
2257 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2258 || prefix != PREFIX_NONE)
2259 {
2260 errmsg = e_invalid_argument;
2261 goto skip;
2262 }
2263
2264 if (flags & P_NUM)
2265 {
2266 // numeric option
2267 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2268 op, flags, cp_val, varp,
2269 errbuf, errbuflen);
2270 if (errmsg != NULL)
2271 goto skip;
2272 }
2273 else if (opt_idx >= 0)
2274 {
2275 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002276 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2277 flags, cp_val, varp, errbuf,
2278 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002279 {
2280 if (errmsg != NULL)
2281 goto skip;
2282 *stopopteval = TRUE;
2283 goto skip;
2284 }
2285 }
2286 else
2287 {
2288 // key code option
2289 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2290 if (errmsg != NULL)
2291 goto skip;
2292 }
2293 }
2294
2295 if (opt_idx >= 0)
2296 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2297
2298skip:
2299 *argp = arg;
2300 return errmsg;
2301}
2302
2303/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002304 * Set an option to a new value.
2305 * Return NULL if OK, return an untranslated error message when something is
2306 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2307 */
2308 static char *
2309do_set_option(
2310 int opt_flags,
2311 char_u **argp,
2312 char_u *arg_start,
2313 char_u **startarg,
2314 int *did_show,
2315 int *stopopteval,
2316 char *errbuf,
2317 size_t errbuflen)
2318{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002319 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002320 char_u *arg;
2321 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2322 set_op_T op;
2323 long_u flags; // flags for current option
2324 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002325 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002326 int nextchar; // next non-white char after option name
2327 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002328 char *errmsg = NULL;
2329 int key;
2330 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002331
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002332 prefix = get_option_prefix(argp);
2333 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002334
2335 // find end of name
2336 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002337 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2338 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002339
2340 // remember character after option name
2341 afterchar = arg[len];
2342
2343 if (in_vim9script())
2344 {
2345 char_u *p = skipwhite(arg + len);
2346
2347 // disallow white space before =val, +=val, -=val, ^=val
2348 if (p > arg + len && (p[0] == '='
2349 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2350 && p[1] == '=')))
2351 {
2352 errmsg = e_no_white_space_allowed_between_option_and;
2353 arg = p;
2354 *startarg = p;
2355 goto skip;
2356 }
2357 }
2358 else
2359 // skip white space, allow ":set ai ?", ":set hlsearch !"
2360 while (VIM_ISWHITE(arg[len]))
2361 ++len;
2362
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002363 op = get_opt_op(arg + len);
2364 if (op != OP_NONE)
2365 len++;
2366
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002367 nextchar = arg[len];
2368
2369 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2370 {
2371 if (in_vim9script() && arg > arg_start
2372 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2373 errmsg = e_no_white_space_allowed_between_option_and;
2374 else
2375 errmsg = e_unknown_option;
2376 goto skip;
2377 }
2378
2379 if (opt_idx >= 0)
2380 {
2381 if (options[opt_idx].var == NULL) // hidden option: skip
2382 {
2383 // Only give an error message when requesting the value of
2384 // a hidden option, ignore setting it.
2385 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2386 && (!(options[opt_idx].flags & P_BOOL)
2387 || nextchar == '?'))
2388 errmsg = e_option_not_supported;
2389 goto skip;
2390 }
2391
2392 flags = options[opt_idx].flags;
2393 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2394 }
2395 else
2396 {
2397 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002398 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002399 if (key < 0)
2400 {
2401 key_name[0] = KEY2TERMCAP0(key);
2402 key_name[1] = KEY2TERMCAP1(key);
2403 }
2404 else
2405 {
2406 key_name[0] = KS_KEY;
2407 key_name[1] = (key & 0xff);
2408 }
2409 }
2410
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002411 // Make sure the option value can be changed.
2412 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002413 goto skip;
2414
Bram Moolenaar40b48722023-02-05 17:04:50 +00002415 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002416 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2417 {
2418 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002419 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2420 {
2421 if (arg[3] == 'm') // "opt&vim": set to Vim default
2422 {
2423 cp_val = FALSE;
2424 arg += 3;
2425 }
2426 else // "opt&vi": set to Vi default
2427 {
2428 cp_val = TRUE;
2429 arg += 2;
2430 }
2431 }
2432 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2433 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2434 {
2435 errmsg = e_trailing_characters;
2436 goto skip;
2437 }
2438 }
2439
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002440 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2441 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002442 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002443 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002444 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2445 && !(flags & P_BOOL)))
2446 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002447 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002448 if (*did_show)
2449 msg_putchar('\n'); // cursor below last one
2450 else
2451 {
2452 gotocmdline(TRUE); // cursor at status line
2453 *did_show = TRUE; // remember that we did a line
2454 }
2455 if (opt_idx >= 0)
2456 {
2457 showoneopt(&options[opt_idx], opt_flags);
2458#ifdef FEAT_EVAL
2459 if (p_verbose > 0)
2460 {
2461 // Mention where the option was last set.
2462 if (varp == options[opt_idx].var)
2463 last_set_msg(options[opt_idx].script_ctx);
2464 else if ((int)options[opt_idx].indir & PV_WIN)
2465 last_set_msg(curwin->w_p_script_ctx[
2466 (int)options[opt_idx].indir & PV_MASK]);
2467 else if ((int)options[opt_idx].indir & PV_BUF)
2468 last_set_msg(curbuf->b_p_script_ctx[
2469 (int)options[opt_idx].indir & PV_MASK]);
2470 }
2471#endif
2472 }
2473 else
2474 {
2475 char_u *p;
2476
2477 p = find_termcode(key_name);
2478 if (p == NULL)
2479 {
2480 errmsg = e_key_code_not_set;
2481 goto skip;
2482 }
2483 else
2484 (void)show_one_termcode(key_name, p, TRUE);
2485 }
2486 if (nextchar != '?'
2487 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2488 errmsg = e_trailing_characters;
2489 }
2490 else
2491 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002492 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2493 flags, varp, key_name, nextchar,
2494 afterchar, cp_val, stopopteval, errbuf,
2495 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002496 }
2497
2498skip:
2499 *argp = arg;
2500 return errmsg;
2501}
2502
2503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * Parse 'arg' for option settings.
2505 *
2506 * 'arg' may be IObuff, but only when no errors can be present and option
2507 * does not need to be expanded with option_expand().
2508 * "opt_flags":
2509 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002510 * OPT_GLOBAL for ":setglobal"
2511 * OPT_LOCAL for ":setlocal" and a modeline
2512 * OPT_MODELINE for a modeline
2513 * OPT_WINONLY to only set window-local options
2514 * OPT_NOWIN to skip setting window-local options
2515 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002517 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
2519 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002520do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002521 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002522 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002524 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002526 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 if (*arg == NUL)
2529 {
2530 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002531 did_show = TRUE;
2532 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
2534
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002535 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002537 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002538 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002540 // ":set all" show all options.
2541 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 arg += 3;
2543 if (*arg == '&')
2544 {
2545 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002546 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002548 didset_options();
2549 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002550 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555 did_show = TRUE;
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002558 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002561 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002562 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 arg += 7;
2564 }
2565 else
2566 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002567 int stopopteval = FALSE;
2568 char *errmsg = NULL;
2569 char errbuf[80];
2570 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002572 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2573 &did_show, &stopopteval, errbuf,
2574 sizeof(errbuf));
2575 if (stopopteval)
2576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002578 // Advance to next argument.
2579 // - skip until a blank found, taking care of backslashes
2580 // - skip blanks
2581 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 for (i = 0; i < 2 ; ++i)
2583 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002584 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (*arg++ == '\\' && *arg != NUL)
2586 ++arg;
2587 arg = skipwhite(arg);
2588 if (*arg != '=')
2589 break;
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002592 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002594 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2595 i = (int)STRLEN(IObuff) + 2;
2596 if (i + (arg - startarg) < IOSIZE)
2597 {
2598 // append the argument with the error
2599 STRCAT(IObuff, ": ");
2600 mch_memmove(IObuff + i, startarg, (arg - startarg));
2601 IObuff[i + (arg - startarg)] = NUL;
2602 }
2603 // make sure all characters are printable
2604 trans_characters(IObuff, IOSIZE);
2605
2606 ++no_wait_return; // wait_return() done later
2607 emsg((char *)IObuff); // show error highlighted
2608 --no_wait_return;
2609
2610 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613
2614 arg = skipwhite(arg);
2615 }
2616
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002617theend:
2618 if (silent_mode && did_show)
2619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002620 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002621 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002622 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002623 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002624 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002625 out_flush();
2626 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002627 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002628 }
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 return OK;
2631}
2632
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002633/*
2634 * Call this when an option has been given a new value through a user command.
2635 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2636 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002637 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002638did_set_option(
2639 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002640 int opt_flags, // possibly with OPT_MODELINE
2641 int new_value, // value was replaced completely
2642 int value_checked) // value was checked to be safe, no need to set the
2643 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002644{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002645 long_u *p;
2646
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002647 options[opt_idx].flags |= P_WAS_SET;
2648
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002649 // When an option is set in the sandbox, from a modeline or in secure mode
2650 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2651 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002652 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002653 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002654#ifdef HAVE_SANDBOX
2655 || sandbox != 0
2656#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002657 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002658 *p = *p | P_INSECURE;
2659 else if (new_value)
2660 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002661}
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663/*
2664 * Convert a key name or string into a key value.
2665 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002666 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002668 int
2669string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002672 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (*arg == '^')
2674 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002675 if (multi_byte)
2676 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 return *arg;
2678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
2681 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2682 * maketitle() to create and display it.
2683 * When switching the title or icon off, call mch_restore_title() to get
2684 * the old value back.
2685 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002686 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002687did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 if (starting != NO_SCREEN
2690#ifdef FEAT_GUI
2691 && !gui.starting
2692#endif
2693 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697/*
2698 * set_options_bin - called when 'bin' changes value.
2699 */
2700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002701set_options_bin(
2702 int oldval,
2703 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002706 // The option values that are changed when 'bin' changes are
2707 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (newval)
2709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002710 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
2712 if (!(opt_flags & OPT_GLOBAL))
2713 {
2714 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2715 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2716 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2717 curbuf->b_p_et_nobin = curbuf->b_p_et;
2718 }
2719 if (!(opt_flags & OPT_LOCAL))
2720 {
2721 p_tw_nobin = p_tw;
2722 p_wm_nobin = p_wm;
2723 p_ml_nobin = p_ml;
2724 p_et_nobin = p_et;
2725 }
2726 }
2727
2728 if (!(opt_flags & OPT_GLOBAL))
2729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 curbuf->b_p_tw = 0; // no automatic line wrap
2731 curbuf->b_p_wm = 0; // no automatic line wrap
2732 curbuf->b_p_ml = 0; // no modelines
2733 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735 if (!(opt_flags & OPT_LOCAL))
2736 {
2737 p_tw = 0;
2738 p_wm = 0;
2739 p_ml = FALSE;
2740 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
2746 if (!(opt_flags & OPT_GLOBAL))
2747 {
2748 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2749 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2750 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2751 curbuf->b_p_et = curbuf->b_p_et_nobin;
2752 }
2753 if (!(opt_flags & OPT_LOCAL))
2754 {
2755 p_tw = p_tw_nobin;
2756 p_wm = p_wm_nobin;
2757 p_ml = p_ml_nobin;
2758 p_et = p_et_nobin;
2759 }
2760 }
2761}
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763/*
2764 * Expand environment variables for some string options.
2765 * These string options cannot be indirect!
2766 * If "val" is NULL expand the current value of the option.
2767 * Return pointer to NameBuff, or NULL when not expanded.
2768 */
2769 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002770option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2774 return NULL;
2775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002776 // If val is longer than MAXPATHL no meaningful expansion can be done,
2777 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (val != NULL && STRLEN(val) > MAXPATHL)
2779 return NULL;
2780
2781 if (val == NULL)
2782 val = *(char_u **)options[opt_idx].var;
2783
2784 /*
2785 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2786 * Escape spaces when expanding 'tags', they are used to separate file
2787 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002788 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 */
2790 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002791 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002792#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002793 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2794#endif
2795 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002796 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 return NULL;
2798
2799 return NameBuff;
2800}
2801
2802/*
2803 * After setting various option values: recompute variables that depend on
2804 * option values.
2805 */
2806 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002807didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002809 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 (void)init_chartab();
2811
Bram Moolenaardac13472019-09-16 21:06:21 +02002812 didset_string_options();
2813
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002814#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002815 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002816 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002817 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002818 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002819#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002820 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002821 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002822#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002824 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002825#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002826 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002827}
2828
2829/*
2830 * More side effects of setting options.
2831 */
2832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002833didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002834{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002836 (void)highlight_changed();
2837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002839 check_opt_wim();
2840
Bram Moolenaareed9d462021-02-15 20:38:25 +01002841 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002842 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002844 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002845 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002846
2847#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002849 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002850#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002851#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002852 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002853 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002854 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002855 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857}
2858
2859/*
2860 * Check for string options that are NULL (normally only termcap options).
2861 */
2862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002863check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 int opt_idx;
2866
2867 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2868 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2869 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2870}
2871
2872/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002873 * Return the option index found by a pointer into term_strings[].
2874 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002876 int
2877get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002879 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
2881 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2882 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002883 return opt_idx;
2884 return -1; // cannot happen: didn't find it!
2885}
2886
2887/*
2888 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2889 * Return the option index or -1 if not found.
2890 */
2891 int
2892set_term_option_alloced(char_u **p)
2893{
2894 int opt_idx = get_term_opt_idx(p);
2895
2896 if (opt_idx >= 0)
2897 options[opt_idx].flags |= P_ALLOCED;
2898 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899}
2900
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002901#if defined(FEAT_EVAL) || defined(PROTO)
2902/*
2903 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2904 * Return FALSE when it wasn't.
2905 * Return -1 for an unknown option.
2906 */
2907 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002908was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002909{
2910 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002911 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002912
2913 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002914 {
2915 flagp = insecure_flag(idx, opt_flags);
2916 return (*flagp & P_INSECURE) != 0;
2917 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002918 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002919 return -1;
2920}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002921
2922/*
2923 * Get a pointer to the flags used for the P_INSECURE flag of option
2924 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002925 * NOTE: Caller must make sure that "curwin" is set to the window from which
2926 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002927 */
2928 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002929insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002930{
2931 if (opt_flags & OPT_LOCAL)
2932 switch ((int)options[opt_idx].indir)
2933 {
2934#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002935 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002936#endif
2937#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002938# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002939 case PV_FDE: return &curwin->w_p_fde_flags;
2940 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002941# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002942# ifdef FEAT_BEVAL
2943 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2944# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002945 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002946 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002947# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002948 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002949# endif
2950#endif
2951 }
2952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002954 return &options[opt_idx].flags;
2955}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002956#endif
2957
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002958/*
2959 * Redraw the window title and/or tab page text later.
2960 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002961void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002962{
2963 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002964 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002965}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002966
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002968 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2969 * characters or characters in "allowed".
2970 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002971 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002972valid_name(char_u *val, char *allowed)
2973{
2974 char_u *s;
2975
2976 for (s = val; *s != NUL; ++s)
2977 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2978 return FALSE;
2979 return TRUE;
2980}
2981
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002982#if defined(FEAT_EVAL) || defined(PROTO)
2983/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002984 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002985 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002986 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002987 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002988set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002989{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002990 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2991 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002992 sctx_T new_script_ctx = script_ctx;
2993
Bram Moolenaar51258742020-05-03 17:19:33 +02002994 // Modeline already has the line number set.
2995 if (!(opt_flags & OPT_MODELINE))
2996 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002997
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 // Remember where the option was set. For local options need to do that
2999 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003000 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003001 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003002 if (both || (opt_flags & OPT_LOCAL))
3003 {
3004 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003005 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003006 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003007 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003008 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003009 if (both)
3010 // also setting the "all buffers" value
3011 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3012 new_script_ctx;
3013 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003014 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003015}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003016
3017/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003018 * Get the script context of global option "name".
3019 *
3020 */
3021 sctx_T *
3022get_option_sctx(char *name)
3023{
3024 int idx = findoption((char_u *)name);
3025
3026 if (idx >= 0)
3027 return &options[idx].script_ctx;
3028 siemsg("no such option: %s", name);
3029 return NULL;
3030}
3031
3032/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003033 * Set the script_ctx for a termcap option.
3034 * "name" must be the two character code, e.g. "RV".
3035 * When "name" is NULL use "opt_idx".
3036 */
3037 void
3038set_term_option_sctx_idx(char *name, int opt_idx)
3039{
3040 char_u buf[5];
3041 int idx;
3042
3043 if (name == NULL)
3044 idx = opt_idx;
3045 else
3046 {
3047 buf[0] = 't';
3048 buf[1] = '_';
3049 buf[2] = name[0];
3050 buf[3] = name[1];
3051 buf[4] = 0;
3052 idx = findoption(buf);
3053 }
3054 if (idx >= 0)
3055 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3056}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003057#endif
3058
Bram Moolenaarf4140482020-02-15 23:06:45 +01003059#if defined(FEAT_EVAL)
3060/*
3061 * Apply the OptionSet autocommand.
3062 */
3063 static void
3064apply_optionset_autocmd(
3065 int opt_idx,
3066 long opt_flags,
3067 long oldval,
3068 long oldval_g,
3069 long newval,
3070 char *errmsg)
3071{
3072 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3073
3074 // Don't do this while starting up, failure or recursively.
3075 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3076 return;
3077
3078 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3079 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3080 oldval_g);
3081 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3082 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3083 (opt_flags & OPT_LOCAL) ? "local" : "global");
3084 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3085 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3086 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3087 if (opt_flags & OPT_LOCAL)
3088 {
3089 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3090 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3091 }
3092 if (opt_flags & OPT_GLOBAL)
3093 {
3094 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3095 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3096 }
3097 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3098 {
3099 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3100 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3101 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3102 }
3103 if (opt_flags & OPT_MODELINE)
3104 {
3105 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3106 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3107 }
3108 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3109 NULL, FALSE, NULL);
3110 reset_v_option_vars();
3111}
3112#endif
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114/*
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003115 * Process the updated 'compatible' option value.
3116 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003117 char *
3118did_set_compatible(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003119{
3120 compatible_set();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003121 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003122}
3123
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003124#if defined(FEAT_LANGMAP) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003125/*
3126 * Process the updated 'langremap' option value.
3127 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003128 char *
3129did_set_langremap(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003130{
3131 // 'langremap' -> !'langnoremap'
3132 p_lnr = !p_lrm;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003133 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003134}
3135
3136/*
3137 * Process the updated 'langnoremap' option value.
3138 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003139 char *
3140did_set_langnoremap(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003141{
3142 // 'langnoremap' -> !'langremap'
3143 p_lrm = !p_lnr;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003144 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003145}
3146#endif
3147
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003148#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003149/*
3150 * Process the updated 'undofile' option value.
3151 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003152 char *
3153did_set_undofile(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003154{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003155 // Only take action when the option was set.
3156 if (!curbuf->b_p_udf && !p_udf)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003157 return NULL;
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003158
3159 // When reset we do not delete the undo file, the option may be set again
3160 // without making any changes in between.
3161 char_u hash[UNDO_HASH_SIZE];
3162 buf_T *save_curbuf = curbuf;
3163
3164 FOR_ALL_BUFFERS(curbuf)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003165 {
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003166 // When 'undofile' is set globally: for every buffer, otherwise
3167 // only for the current buffer: Try to read in the undofile,
3168 // if one exists, the buffer wasn't changed and the buffer was
3169 // loaded
3170 if ((curbuf == save_curbuf
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003171 || (args->os_flags & OPT_GLOBAL)
3172 || args->os_flags == 0)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003173 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003174 {
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003175#ifdef FEAT_CRYPT
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003176 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
3177 continue;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003178#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003179 u_compute_hash(hash);
3180 u_read_undo(NULL, hash, curbuf->b_fname);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003181 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003182 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003183 curbuf = save_curbuf;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003184
3185 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003186}
3187#endif
3188
3189/*
3190 * Process the updated 'readonly' option value.
3191 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003192 char *
3193did_set_readonly(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003194{
3195 // when 'readonly' is reset globally, also reset readonlymode
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003196 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003197 readonlymode = FALSE;
3198
3199 // when 'readonly' is set may give W10 again
3200 if (curbuf->b_p_ro)
3201 curbuf->b_did_warn = FALSE;
3202
3203 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003204
3205 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003206}
3207
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003208#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003209/*
3210 * Process the updated 'mousehide' option value.
3211 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003212 char *
3213did_set_mousehide(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003214{
3215 if (!p_mh)
3216 gui_mch_mousehide(FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003217 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003218}
3219#endif
3220
3221/*
3222 * Process the updated 'modifiable' option value.
3223 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003224 char *
3225did_set_modifiable(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003226{
3227 // when 'modifiable' is changed, redraw the window title
3228
3229# ifdef FEAT_TERMINAL
3230 // Cannot set 'modifiable' when in Terminal mode.
3231 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3232 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3233 {
3234 curbuf->b_p_ma = FALSE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003235 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003236 return e_cannot_make_terminal_with_running_job_modifiable;
3237 }
3238# endif
3239 redraw_titles();
3240
3241 return NULL;
3242}
3243
3244/*
3245 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3246 * option value.
3247 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003248 char *
3249did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003250{
3251 // redraw the window title and tab page text
3252 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003253 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003254}
3255
3256/*
3257 * Process the updated 'binary' option value.
3258 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003259 char *
3260did_set_binary(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003261{
3262 // when 'bin' is set also set some other options
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003263 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003264 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003265
3266 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003267}
3268
3269/*
3270 * Process the updated 'buflisted' option value.
3271 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003272 char *
3273did_set_buflisted(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003274{
3275 // when 'buflisted' changes, trigger autocommands
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003276 if (args->os_oldval.boolean != curbuf->b_p_bl)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003277 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3278 NULL, NULL, TRUE, curbuf);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003279 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003280}
3281
3282/*
3283 * Process the updated 'swapfile' option value.
3284 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003285 char *
3286did_set_swapfile(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003287{
3288 // when 'swf' is set, create swapfile, when reset remove swapfile
3289 if (curbuf->b_p_swf && p_uc)
3290 ml_open_file(curbuf); // create the swap file
3291 else
3292 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3293 // buf->b_p_swf
3294 mf_close_file(curbuf, TRUE); // remove the swap file
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003295 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003296}
3297
3298/*
3299 * Process the updated 'terse' option value.
3300 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003301 char *
3302did_set_terse(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003303{
3304 char_u *p;
3305
3306 // when 'terse' is set change 'shortmess'
3307 p = vim_strchr(p_shm, SHM_SEARCH);
3308
3309 // insert 's' in p_shm
3310 if (p_terse && p == NULL)
3311 {
3312 STRCPY(IObuff, p_shm);
3313 STRCAT(IObuff, "s");
3314 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
3315 }
3316 // remove 's' from p_shm
3317 else if (!p_terse && p != NULL)
3318 STRMOVE(p, p + 1);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003319 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003320}
3321
3322/*
3323 * Process the updated 'paste' option value.
3324 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003325 char *
3326did_set_paste(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003327{
3328 // when 'paste' is set or reset also change other options
3329 paste_option_changed();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003330 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003331}
3332
3333/*
3334 * Process the updated 'insertmode' option value.
3335 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003336 char *
3337did_set_insertmode(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003338{
3339 // when 'insertmode' is set from an autocommand need to do work here
3340 if (p_im)
3341 {
3342 if ((State & MODE_INSERT) == 0)
3343 need_start_insertmode = TRUE;
3344 stop_insert_mode = FALSE;
3345 }
3346 // only reset if it was set previously
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003347 else if (args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003348 {
3349 need_start_insertmode = FALSE;
3350 stop_insert_mode = TRUE;
3351 if (restart_edit != 0 && mode_displayed)
3352 clear_cmdline = TRUE; // remove "(insert)"
3353 restart_edit = 0;
3354 }
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003355
3356 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003357}
3358
3359/*
3360 * Process the updated 'ignorecase' option value.
3361 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003362 char *
3363did_set_ignorecase(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003364{
3365 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3366 if (p_hls)
3367 redraw_all_later(UPD_SOME_VALID);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003368 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003369}
3370
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003371#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003372/*
3373 * Process the updated 'hlsearch' option value.
3374 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003375 char *
3376did_set_hlsearch(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003377{
3378 // when 'hlsearch' is set or reset: reset no_hlsearch
3379 set_no_hlsearch(FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003380 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003381}
3382#endif
3383
3384/*
3385 * Process the updated 'scrollbind' option value.
3386 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003387 char *
3388did_set_scrollbind(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003389{
3390 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3391 // at the end of normal_cmd()
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003392 if (!curwin->w_p_scb)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003393 return NULL;
3394
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003395 do_check_scrollbind(FALSE);
3396 curwin->w_scbind_pos = curwin->w_topline;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003397 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003398}
3399
3400#ifdef FEAT_QUICKFIX
3401/*
3402 * Process the updated 'previewwindow' option value.
3403 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003404 char *
3405did_set_previewwindow(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003406{
3407 if (!curwin->w_p_pvw)
3408 return NULL;
3409
3410 // There can be only one window with 'previewwindow' set.
3411 win_T *win;
3412
3413 FOR_ALL_WINDOWS(win)
3414 if (win->w_p_pvw && win != curwin)
3415 {
3416 curwin->w_p_pvw = FALSE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003417 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003418 return e_preview_window_already_exists;
3419 }
3420
3421 return NULL;
3422}
3423#endif
3424
3425/*
3426 * Process the updated 'smoothscroll' option value.
3427 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003428 char *
3429did_set_smoothscroll(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003430{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003431 if (curwin->w_p_sms)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003432 return NULL;
3433
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003434 curwin->w_skipcol = 0;
3435 changed_line_abv_curs();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003436 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003437}
3438
3439/*
3440 * Process the updated 'textmode' option value.
3441 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003442 char *
3443did_set_textmode(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003444{
3445 // when 'textmode' is set or reset also change 'fileformat'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003446 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
3447
3448 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003449}
3450
3451/*
3452 * Process the updated 'textauto' option value.
3453 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003454 char *
3455did_set_textauto(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003456{
3457 // when 'textauto' is set or reset also change 'fileformats'
3458 set_string_option_direct((char_u *)"ffs", -1,
3459 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003460 OPT_FREE | args->os_flags, 0);
3461
3462 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003463}
3464
3465/*
3466 * Process the updated 'lisp' option value.
3467 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003468 char *
3469did_set_lisp(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003470{
3471 // When 'lisp' option changes include/exclude '-' in keyword characters.
3472 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003473 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003474}
3475
3476/*
3477 * Process the updated 'title' or the 'icon' option value.
3478 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003479 char *
3480did_set_title_icon(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003481{
3482 // when 'title' changed, may need to change the title; same for 'icon'
3483 did_set_title();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003484 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003485}
3486
3487/*
3488 * Process the updated 'modified' option value.
3489 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003490 char *
3491did_set_modified(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003492{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003493 if (!args->os_newval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003494 save_file_ff(curbuf); // Buffer is unchanged
3495 redraw_titles();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003496 modified_was_set = args->os_newval.boolean;
3497 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003498}
3499
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003500#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003501/*
3502 * Process the updated 'shellslash' option value.
3503 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003504 char *
3505did_set_shellslash(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003506{
3507 if (p_ssl)
3508 {
3509 psepc = '/';
3510 psepcN = '\\';
3511 pseps[0] = '/';
3512 }
3513 else
3514 {
3515 psepc = '\\';
3516 psepcN = '/';
3517 pseps[0] = '\\';
3518 }
3519
3520 // need to adjust the file name arguments and buffer names.
3521 buflist_slash_adjust();
3522 alist_slash_adjust();
3523# ifdef FEAT_EVAL
3524 scriptnames_slash_adjust();
3525# endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003526 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003527}
3528#endif
3529
3530/*
3531 * Process the updated 'wrap' option value.
3532 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003533 char *
3534did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003535{
3536 // If 'wrap' is set, set w_leftcol to zero.
3537 if (curwin->w_p_wrap)
3538 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003539 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003540}
3541
3542/*
3543 * Process the updated 'equalalways' option value.
3544 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003545 char *
3546did_set_equalalways(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003547{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003548 if (p_ea && !args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003549 win_equal(curwin, FALSE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003550
3551 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003552}
3553
3554/*
3555 * Process the updated 'weirdinvert' option value.
3556 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003557 char *
3558did_set_weirdinvert(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003559{
3560 // When 'weirdinvert' changed, set/reset 't_xs'.
3561 // Then set 'weirdinvert' according to value of 't_xs'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003562 if (p_wiv && !args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003563 T_XS = (char_u *)"y";
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003564 else if (!p_wiv && args->os_oldval.boolean)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003565 T_XS = empty_option;
3566 p_wiv = (*T_XS != NUL);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003567
3568 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003569}
3570
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003571#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003572/*
3573 * Process the updated 'ballooneval' option value.
3574 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003575 char *
3576did_set_ballooneval(optset_T *args)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003577{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003578 if (balloonEvalForTerm)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003579 return NULL;
3580
3581 if (p_beval && !args->os_oldval.boolean)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003582 gui_mch_enable_beval_area(balloonEval);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003583 else if (!p_beval && args->os_oldval.boolean)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003584 gui_mch_disable_beval_area(balloonEval);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003585
3586 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003587}
3588#endif
3589
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003590#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003591/*
3592 * Process the updated 'balloonevalterm' option value.
3593 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003594 char *
3595did_set_balloonevalterm(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003596{
3597 mch_bevalterm_changed();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003598 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003599}
3600#endif
3601
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003602#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003603/*
3604 * Process the updated 'autochdir' option value.
3605 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003606 char *
3607did_set_autochdir(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003608{
3609 // Change directories when the 'acd' option is set now.
3610 DO_AUTOCHDIR;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003611 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003612}
3613#endif
3614
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003615#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003616/*
3617 * Process the updated 'diff' option value.
3618 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003619 char *
3620did_set_diff(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003621{
3622 // May add or remove the buffer from the list of diff buffers.
3623 diff_buf_adjust(curwin);
3624# ifdef FEAT_FOLDING
3625 if (foldmethodIsDiff(curwin))
3626 foldUpdateAll(curwin);
3627# endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003628 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003629}
3630#endif
3631
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003632#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003633/*
3634 * Process the updated 'imdisable' option value.
3635 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003636 char *
3637did_set_imdisable(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003638{
3639 // Only de-activate it here, it will be enabled when changing mode.
3640 if (p_imdisable)
3641 im_set_active(FALSE);
3642 else if (State & MODE_INSERT)
3643 // When the option is set from an autocommand, it may need to take
3644 // effect right away.
3645 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003646 return NULL;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003647}
3648#endif
3649
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003650#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003651/*
3652 * Process the updated 'spell' option value.
3653 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003654 char *
3655did_set_spell(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003656{
3657 if (curwin->w_p_spell)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003658 return parse_spelllang(curwin);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003659
3660 return NULL;
3661}
3662#endif
3663
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003664#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003665/*
3666 * Process the updated 'arabic' option value.
3667 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003668 char *
3669did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003670{
3671 char *errmsg = NULL;
3672
3673 if (curwin->w_p_arab)
3674 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003675 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003676 if (!p_tbidi)
3677 {
3678 // set rightleft mode
3679 if (!curwin->w_p_rl)
3680 {
3681 curwin->w_p_rl = TRUE;
3682 changed_window_setting();
3683 }
3684
3685 // Enable Arabic shaping (major part of what Arabic requires)
3686 if (!p_arshape)
3687 {
3688 p_arshape = TRUE;
3689 redraw_later_clear();
3690 }
3691 }
3692
3693 // Arabic requires a utf-8 encoding, inform the user if it's not
3694 // set.
3695 if (STRCMP(p_enc, "utf-8") != 0)
3696 {
3697 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3698
3699 msg_source(HL_ATTR(HLF_W));
3700 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3701#ifdef FEAT_EVAL
3702 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3703#endif
3704 }
3705
3706 // set 'delcombine'
3707 p_deco = TRUE;
3708
3709# ifdef FEAT_KEYMAP
3710 // Force-set the necessary keymap for arabic
3711 errmsg = set_option_value((char_u *)"keymap",
3712 0L, (char_u *)"arabic", OPT_LOCAL);
3713# endif
3714 }
3715 else
3716 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003717 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003718 if (!p_tbidi)
3719 {
3720 // reset rightleft mode
3721 if (curwin->w_p_rl)
3722 {
3723 curwin->w_p_rl = FALSE;
3724 changed_window_setting();
3725 }
3726
3727 // 'arabicshape' isn't reset, it is a global option and
3728 // another window may still need it "on".
3729 }
3730
3731 // 'delcombine' isn't reset, it is a global option and another
3732 // window may still want it "on".
3733
3734# ifdef FEAT_KEYMAP
3735 // Revert to the default keymap
3736 curbuf->b_p_iminsert = B_IMODE_NONE;
3737 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3738# endif
3739 }
3740
3741 return errmsg;
3742}
3743#endif
3744
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003745/*
3746 * Process the updated 'number' or 'relativenumber' option value.
3747 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003748 char *
3749did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003750{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003751#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003752 if (gui.in_use
3753 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3754 && curbuf->b_signlist != NULL)
3755 {
3756 // If the 'number' or 'relativenumber' options are modified and
3757 // 'signcolumn' is set to 'number', then clear the screen for a full
3758 // refresh. Otherwise the sign icons are not displayed properly in the
3759 // number column. If the 'number' option is set and only the
3760 // 'relativenumber' option is toggled, then don't refresh the screen
3761 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003762 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003763 redraw_all_later(UPD_CLEAR);
3764 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003765#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003766 return NULL;
3767}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003768
3769#ifdef FEAT_TERMGUICOLORS
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003770 char *
3771did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003772{
3773# ifdef FEAT_VTP
3774 // Do not turn on 'tgc' when 24-bit colors are not supported.
3775 if (
3776# ifdef VIMDLL
3777 !gui.in_use && !gui.starting &&
3778# endif
3779 !has_vtp_working())
3780 {
3781 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003782 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003783 return e_24_bit_colors_are_not_supported_on_this_environment;
3784 }
3785 if (is_term_win32())
3786 swap_tcap();
3787# endif
3788# ifdef FEAT_GUI
3789 if (!gui.in_use && !gui.starting)
3790# endif
3791 highlight_gui_started();
3792# ifdef FEAT_VTP
3793 // reset t_Co
3794 if (is_term_win32())
3795 {
3796 control_console_color_rgb();
3797 set_termname(T_NAME);
3798 init_highlight(TRUE, FALSE);
3799 }
3800# endif
3801# ifdef FEAT_TERMINAL
3802 term_update_colors_all();
3803 term_update_palette_all();
3804 term_update_wincolor_all();
3805# endif
3806
3807 return NULL;
3808}
3809#endif
3810
3811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 * Set the value of a boolean option, and take care of side effects.
3813 * Returns NULL for success, or an error message for an error.
3814 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003815 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003816set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003817 int opt_idx, // index in options[] table
3818 char_u *varp, // pointer to the option variable
3819 int value, // new value
3820 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003823#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003824 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003825#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003826 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003828 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if ((secure
3830#ifdef HAVE_SANDBOX
3831 || sandbox != 0
3832#endif
3833 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003834 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003836#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003837 // Save the global value before changing anything. This is needed as for
3838 // a global-only option setting the "local value" in fact sets the global
3839 // value (since there is only one value).
3840 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3841 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
3842 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003843#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003845 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003847 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003848 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#endif
3850
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003851#ifdef FEAT_GUI
3852 need_mouse_correct = TRUE;
3853#endif
3854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003855 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3857 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
3858
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003859 // Handle side effects of changing a bool option.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003860 if (options[opt_idx].opt_did_set_cb != NULL)
3861 {
3862 optset_T args;
3863
3864 args.os_varp = varp;
3865 args.os_flags = opt_flags;
3866 args.os_oldval.boolean = old_value;
3867 args.os_newval.boolean = value;
3868 args.os_doskip = FALSE;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003869 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003870 errmsg = options[opt_idx].opt_did_set_cb(&args);
3871 if (args.os_doskip)
3872 return errmsg;
3873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003875 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 options[opt_idx].flags |= P_WAS_SET;
3878
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003879#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003880 apply_optionset_autocmd(opt_idx, opt_flags,
3881 (long)(old_value ? TRUE : FALSE),
3882 (long)(old_global_value ? TRUE : FALSE),
3883 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003884#endif
3885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003886 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003887 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003888 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003889 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003890
3891 if ((opt_flags & OPT_NO_REDRAW) == 0)
3892 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003894 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895}
3896
3897/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003898 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003900 char *
3901did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003903 long *pp = (long *)args->os_varp;
3904 char *errmsg = NULL;
3905
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003906 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003908 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003909 p_wh = 1;
3910 }
3911 if (p_wmh > p_wh)
3912 {
3913 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3914 p_wh = p_wmh;
3915 }
3916 if (p_hh < 0)
3917 {
3918 errmsg = e_argument_must_be_positive;
3919 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003922 // Change window height NOW
3923 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003925 if (pp == &p_wh && curwin->w_height < p_wh)
3926 win_setheight((int)p_wh);
3927 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3928 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003931 return errmsg;
3932}
3933
3934/*
3935 * Process the new 'winminheight' option value.
3936 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003937 char *
3938did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003939{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003940 char *errmsg = NULL;
3941
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003942 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003944 errmsg = e_argument_must_be_positive;
3945 p_wmh = 0;
3946 }
3947 if (p_wmh > p_wh)
3948 {
3949 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
3950 p_wmh = p_wh;
3951 }
3952 win_setminheight();
3953
3954 return errmsg;
3955}
3956
3957/*
3958 * Process the new 'winwidth' option value.
3959 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003960 char *
3961did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003962{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003963 char *errmsg = NULL;
3964
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003965 if (p_wiw < 1)
3966 {
3967 errmsg = e_argument_must_be_positive;
3968 p_wiw = 1;
3969 }
3970 if (p_wmw > p_wiw)
3971 {
3972 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3973 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003975
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003976 // Change window width NOW
3977 if (!ONE_WINDOW && curwin->w_width < p_wiw)
3978 win_setwidth((int)p_wiw);
3979
3980 return errmsg;
3981}
3982
3983/*
3984 * Process the new 'winminwidth' option value.
3985 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003986 char *
3987did_set_winminwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003988{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003989 char *errmsg = NULL;
3990
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003991 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003992 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003993 errmsg = e_argument_must_be_positive;
3994 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003995 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003996 if (p_wmw > p_wiw)
3997 {
3998 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
3999 p_wmw = p_wiw;
4000 }
4001 win_setminwidth();
4002
4003 return errmsg;
4004}
4005
4006/*
4007 * Process the new 'laststatus' option value.
4008 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004009 char *
4010did_set_laststatus(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004011{
4012 last_status(FALSE); // (re)set last window status line
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004013 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004014}
4015
4016/*
4017 * Process the new 'showtabline' option value.
4018 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004019 char *
4020did_set_showtabline(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004021{
4022 shell_new_rows(); // recompute window positions and heights
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004023 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004026#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004027/*
4028 * Process the new 'linespace' option value.
4029 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004030 char *
4031did_set_linespace(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004032{
4033 // Recompute gui.char_height and resize the Vim window to keep the
4034 // same number of lines.
4035 if (gui.in_use && gui_mch_adjust_charheight() == OK)
4036 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004037 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004038}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039#endif
4040
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004041#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004042/*
4043 * Process the new 'foldlevel' option value.
4044 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004045 char *
4046did_set_foldlevel(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004047{
4048 if (curwin->w_p_fdl < 0)
4049 curwin->w_p_fdl = 0;
4050 newFoldLevel();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004051 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004054/*
4055 * Process the new 'foldminlines' option value.
4056 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004057 char *
4058did_set_foldminlines(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004059{
4060 foldUpdateAll(curwin);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004061 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004062}
4063
4064/*
4065 * Process the new 'foldnestmax' option value.
4066 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004067 char *
4068did_set_foldnestmax(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004069{
4070 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 foldUpdateAll(curwin);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004072 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004073}
4074
4075/*
4076 * Process the new 'foldcolumn' option value.
4077 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004078 char *
4079did_set_foldcolumn(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004080{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004081 char *errmsg = NULL;
4082
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004083 if (curwin->w_p_fdc < 0)
4084 {
4085 errmsg = e_argument_must_be_positive;
4086 curwin->w_p_fdc = 0;
4087 }
4088 else if (curwin->w_p_fdc > 12)
4089 {
4090 errmsg = e_invalid_argument;
4091 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004094 return errmsg;
4095}
4096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004098/*
4099 * Process the new 'shiftwidth' or the 'tabstop' option value.
4100 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004101 char *
4102did_set_shiftwidth_tabstop(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004103{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004104 long *pp = (long *)args->os_varp;
4105 char *errmsg = NULL;
4106
4107 if (curbuf->b_p_sw < 0)
4108 {
4109 errmsg = e_argument_must_be_positive;
4110#ifdef FEAT_VARTABS
4111 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4112 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4113 ? tabstop_first(curbuf->b_p_vts_array)
4114 : curbuf->b_p_ts;
4115#else
4116 curbuf->b_p_sw = curbuf->b_p_ts;
4117#endif
4118 }
4119
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004120#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004121 if (foldmethodIsIndent(curwin))
4122 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004123#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004124 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4125 // parse 'cinoptions'.
4126 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4127 parse_cino(curbuf);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004128
4129 return errmsg;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004132/*
4133 * Process the new 'maxcombine' option value.
4134 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004135 char *
4136did_set_maxcombine(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004137{
4138 if (p_mco > MAX_MCO)
4139 p_mco = MAX_MCO;
4140 else if (p_mco < 0)
4141 p_mco = 0;
4142 screenclear(); // will re-allocate the screen
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004143 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004144}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004145
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004146/*
4147 * Process the new 'iminsert' option value.
4148 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004149 char *
4150did_set_iminsert(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004151{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004152 char *errmsg = NULL;
4153
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004154 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004156 errmsg = e_invalid_argument;
4157 curbuf->b_p_iminsert = B_IMODE_NONE;
4158 }
4159 p_iminsert = curbuf->b_p_iminsert;
4160 if (termcap_active) // don't do this in the alternate screen
4161 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004162#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004163 // Show/unshow value of 'keymap' in status lines.
4164 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004166
4167 return errmsg;
4168}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004170#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004171/*
4172 * Process the new 'imstyle' option value.
4173 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004174 char *
4175did_set_imstyle(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004176{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004177 char *errmsg = NULL;
4178
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004179 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4180 errmsg = e_invalid_argument;
4181
4182 return errmsg;
4183}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004184#endif
4185
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004186/*
4187 * Process the new 'window' option value.
4188 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004189 char *
4190did_set_window(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004191{
4192 if (p_window < 1)
4193 p_window = 1;
4194 else if (p_window >= Rows)
4195 p_window = Rows - 1;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004196 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004197}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004198
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004199/*
4200 * Process the new 'imsearch' option value.
4201 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004202 char *
4203did_set_imsearch(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004204{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004205 char *errmsg = NULL;
4206
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004207 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004209 errmsg = e_invalid_argument;
4210 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004212 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004214 return errmsg;
4215}
4216
4217/*
4218 * Process the new 'titlelen' option value.
4219 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004220 char *
4221did_set_titlelen(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004222{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004223 long old_value = args->os_oldval.number;
4224 char *errmsg = NULL;
4225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004227 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004229 errmsg = e_argument_must_be_positive;
4230 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004232 if (starting != NO_SCREEN && old_value != p_titlelen)
4233 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004235 return errmsg;
4236}
4237
4238/*
4239 * Process the new 'cmdheight' option value.
4240 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004241 char *
4242did_set_cmdheight(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004243{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004244 long old_value = args->os_oldval.number;
4245 char *errmsg = NULL;
4246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004247 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004248 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004250 errmsg = e_argument_must_be_positive;
4251 p_ch = 1;
4252 }
4253 if (p_ch > Rows - min_rows() + 1)
4254 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004256 // Only compute the new window layout when startup has been
4257 // completed. Otherwise the frame sizes may be wrong.
4258 if ((p_ch != old_value
4259 || tabline_height() + topframe->fr_height != Rows - p_ch)
4260 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004262 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004264 )
4265 command_height();
4266
4267 return errmsg;
4268}
4269
4270/*
4271 * Process the new 'updatecount' option value.
4272 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004273 char *
4274did_set_updatecount(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
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004279 // when 'updatecount' changes from zero to non-zero, open swap files
4280 if (p_uc < 0)
4281 {
4282 errmsg = e_argument_must_be_positive;
4283 p_uc = 100;
4284 }
4285 if (p_uc && !old_value)
4286 ml_open_files();
4287
4288 return errmsg;
4289}
4290
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004291#if defined(FEAT_CONCEAL) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004292/*
4293 * Process the new 'conceallevel' option value.
4294 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004295 char *
4296did_set_conceallevel(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004297{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004298 char *errmsg = NULL;
4299
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004300 if (curwin->w_p_cole < 0)
4301 {
4302 errmsg = e_argument_must_be_positive;
4303 curwin->w_p_cole = 0;
4304 }
4305 else if (curwin->w_p_cole > 3)
4306 {
4307 errmsg = e_invalid_argument;
4308 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004311 return errmsg;
4312}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004315#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004316/*
4317 * Process the new 'pyxversion' option value.
4318 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004319 char *
4320did_set_pyxversion(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004321{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004322 char *errmsg = NULL;
4323
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004324 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4325 errmsg = e_invalid_argument;
4326
4327 return errmsg;
4328}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004329#endif
4330
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004331/*
4332 * Process the new global 'undolevels' option value.
4333 */
4334 static void
4335did_set_global_undolevels(long value, long old_value)
4336{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004337 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004338
4339 // use the old value, otherwise u_sync() may not work properly
4340 p_ul = old_value;
4341 u_sync(TRUE);
4342 p_ul = value;
4343}
4344
4345/*
4346 * Process the new buffer local 'undolevels' option value.
4347 */
4348 static void
4349did_set_buflocal_undolevels(long value, long old_value)
4350{
4351 // use the old value, otherwise u_sync() may not work properly
4352 curbuf->b_p_ul = old_value;
4353 u_sync(TRUE);
4354 curbuf->b_p_ul = value;
4355}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004357#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004358/*
4359 * Process the new 'numberwidth' option value.
4360 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004361 char *
4362did_set_numberwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004363{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004364 char *errmsg = NULL;
4365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004366 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004367 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004368 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004369 errmsg = e_argument_must_be_positive;
4370 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004371 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004372 if (curwin->w_p_nuw > 20)
4373 {
4374 errmsg = e_invalid_argument;
4375 curwin->w_p_nuw = 20;
4376 }
4377 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4378
4379 return errmsg;
4380}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004381#endif
4382
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004383/*
4384 * Process the new 'textwidth' option value.
4385 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004386 char *
4387did_set_textwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004388{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004389 char *errmsg = NULL;
4390
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004391 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004392 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004393 errmsg = e_argument_must_be_positive;
4394 curbuf->b_p_tw = 0;
4395 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004396#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004397 {
4398 win_T *wp;
4399 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004400
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004401 FOR_ALL_TAB_WINDOWS(tp, wp)
4402 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004403 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004404#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004405
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004406 return errmsg;
4407}
4408
4409/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004410 * Process the new 'undolevels' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004411 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004412 char *
4413did_set_undolevels(optset_T *args)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004414{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004415 long *pp = (long *)args->os_varp;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004416
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004417 if (pp == &p_ul) // global 'undolevels'
4418 did_set_global_undolevels(args->os_newval.number,
4419 args->os_oldval.number);
4420 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4421 did_set_buflocal_undolevels(args->os_newval.number,
4422 args->os_oldval.number);
4423
4424 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004425}
4426
4427/*
4428 * Check the bounds of numeric options.
4429 */
4430 static char *
4431check_num_option_bounds(
4432 long *pp,
4433 long old_value,
4434 long old_Rows,
4435 long old_Columns,
4436 char *errbuf,
4437 size_t errbuflen,
4438 char *errmsg)
4439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (Rows < min_rows() && full_screen)
4441 {
4442 if (errbuf != NULL)
4443 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004444 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004445 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 errmsg = errbuf;
4447 }
4448 Rows = min_rows();
4449 }
4450 if (Columns < MIN_COLUMNS && full_screen)
4451 {
4452 if (errbuf != NULL)
4453 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004454 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004455 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 errmsg = errbuf;
4457 }
4458 Columns = MIN_COLUMNS;
4459 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004460 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004462 // If the screen (shell) height has been changed, assume it is the
4463 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 if (old_Rows != Rows || old_Columns != Columns)
4465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004466 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (updating_screen)
4468 *pp = old_value;
4469 else if (full_screen
4470#ifdef FEAT_GUI
4471 && !gui.starting
4472#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004473 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 set_shellsize((int)Columns, (int)Rows, TRUE);
4475 else
4476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004477 // Postpone the resizing; check the size and cmdline position for
4478 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 check_shellsize();
4480 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4481 cmdline_row = Rows - p_ch;
4482 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004483 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004484 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (curbuf->b_p_ts <= 0)
4488 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004489 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 curbuf->b_p_ts = 8;
4491 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004492 else if (curbuf->b_p_ts > TABSTOP_MAX)
4493 {
4494 errmsg = e_invalid_argument;
4495 curbuf->b_p_ts = 8;
4496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (p_tm < 0)
4498 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004499 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 p_tm = 0;
4501 }
4502 if ((curwin->w_p_scr <= 0
4503 || (curwin->w_p_scr > curwin->w_height
4504 && curwin->w_height > 0))
4505 && full_screen)
4506 {
4507 if (pp == &(curwin->w_p_scr))
4508 {
4509 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004510 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 win_comp_scroll(curwin);
4512 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004513 // If 'scroll' became invalid because of a side effect silently adjust
4514 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 else if (curwin->w_p_scr <= 0)
4516 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004517 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 curwin->w_p_scr = curwin->w_height;
4519 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004520 if (p_hi < 0)
4521 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004522 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004523 p_hi = 0;
4524 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004525 else if (p_hi > 10000)
4526 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004527 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004528 p_hi = 10000;
4529 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004530 if (p_re < 0 || p_re > 2)
4531 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004532 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004533 p_re = 0;
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (p_report < 0)
4536 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004537 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 p_report = 1;
4539 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004540 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004542 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 p_sj = Rows / 2;
4544 else
4545 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004546 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 p_sj = 1;
4548 }
4549 }
4550 if (p_so < 0 && full_screen)
4551 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004552 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 p_so = 0;
4554 }
4555 if (p_siso < 0 && full_screen)
4556 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004557 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 p_siso = 0;
4559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (p_cwh < 1)
4561 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004562 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 p_cwh = 1;
4564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if (p_ut < 0)
4566 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004567 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 p_ut = 2000;
4569 }
4570 if (p_ss < 0)
4571 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004572 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 p_ss = 0;
4574 }
4575
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004576 return errmsg;
4577}
4578
4579/*
4580 * Set the value of a number option, and take care of side effects.
4581 * Returns NULL for success, or an error message for an error.
4582 */
4583 static char *
4584set_num_option(
4585 int opt_idx, // index in options[] table
4586 char_u *varp, // pointer to the option variable
4587 long value, // new value
4588 char *errbuf, // buffer for error messages
4589 size_t errbuflen, // length of "errbuf"
4590 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4591 // OPT_MODELINE, etc.
4592{
4593 char *errmsg = NULL;
4594 long old_value = *(long *)varp;
4595#if defined(FEAT_EVAL)
4596 long old_global_value = 0; // only used when setting a local and
4597 // global option
4598#endif
4599 long old_Rows = Rows; // remember old Rows
4600 long old_Columns = Columns; // remember old Columns
4601 long *pp = (long *)varp;
4602
4603 // Disallow changing some options from secure mode.
4604 if ((secure
4605#ifdef HAVE_SANDBOX
4606 || sandbox != 0
4607#endif
4608 ) && (options[opt_idx].flags & P_SECURE))
4609 return e_not_allowed_here;
4610
4611#if defined(FEAT_EVAL)
4612 // Save the global value before changing anything. This is needed as for
4613 // a global-only option setting the "local value" in fact sets the global
4614 // value (since there is only one value).
4615 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4616 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4617 OPT_GLOBAL);
4618#endif
4619
4620 *pp = value;
4621#ifdef FEAT_EVAL
4622 // Remember where the option was set.
4623 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4624#endif
4625#ifdef FEAT_GUI
4626 need_mouse_correct = TRUE;
4627#endif
4628
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004629 // Invoke the option specific callback function to validate and apply the
4630 // new value.
4631 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004632 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004633 optset_T args;
4634
4635 args.os_varp = varp;
4636 args.os_flags = opt_flags;
4637 args.os_oldval.number = old_value;
4638 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004639 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004640 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004641 }
4642
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004643 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004644 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4645 errbuf, errbuflen, errmsg);
4646
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004647 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4649 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4650
4651 options[opt_idx].flags |= P_WAS_SET;
4652
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004653#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004654 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4655 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004656#endif
4657
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004658 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004659 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004660 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004661 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004662 if ((opt_flags & OPT_NO_REDRAW) == 0)
4663 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665 return errmsg;
4666}
4667
4668/*
4669 * Called after an option changed: check if something needs to be redrawn.
4670 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004671 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004672check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004674 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004675 int doclear = (flags & P_RCLR) == P_RCLR;
4676 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
4681 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4682 changed_window_setting();
4683 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004684 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004685 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004686 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004687 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004688 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004690 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691}
4692
4693/*
4694 * Find index for option 'arg'.
4695 * Return -1 if not found.
4696 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004698findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 int opt_idx;
4701 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004702 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 int is_term_opt;
4704
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004705 // For first call: Initialize the quick-access table.
4706 // It contains the index for the first option that starts with a certain
4707 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 if (quick_tab[1] == 0)
4709 {
4710 p = options[0].fullname;
4711 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4712 {
4713 if (s[0] != p[0])
4714 {
4715 if (s[0] == 't' && s[1] == '_')
4716 quick_tab[26] = opt_idx;
4717 else
4718 quick_tab[CharOrdLow(s[0])] = opt_idx;
4719 }
4720 p = s;
4721 }
4722 }
4723
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004724 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return -1;
4727
4728 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4729 if (is_term_opt)
4730 opt_idx = quick_tab[26];
4731 else
4732 opt_idx = quick_tab[CharOrdLow(arg[0])];
4733 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 break;
4737 }
4738 if (s == NULL && !is_term_opt)
4739 {
4740 opt_idx = quick_tab[CharOrdLow(arg[0])];
4741 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4742 {
4743 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004744 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 break;
4746 s = NULL;
4747 }
4748 }
4749 if (s == NULL)
4750 opt_idx = -1;
4751 return opt_idx;
4752}
4753
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004754#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4755 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756/*
4757 * Get the value for an option.
4758 *
4759 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004760 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004761 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004762 * String option: gov_string, *stringval gets allocated string.
4763 * Hidden Number option: gov_hidden_number.
4764 * Hidden Toggle option: gov_hidden_bool.
4765 * Hidden String option: gov_hidden_string.
4766 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004767 *
4768 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004770 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004771get_option_value(
4772 char_u *name,
4773 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004775 int *flagsp,
4776 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
4778 int opt_idx;
4779 char_u *varp;
4780
4781 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004782 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004783 {
4784 int key;
4785
4786 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004787 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004788 {
4789 char_u key_name[2];
4790 char_u *p;
4791
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004792 if (flagsp != NULL)
4793 *flagsp = 0; // terminal option has no flags
4794
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004795 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004796 if (key < 0)
4797 {
4798 key_name[0] = KEY2TERMCAP0(key);
4799 key_name[1] = KEY2TERMCAP1(key);
4800 }
4801 else
4802 {
4803 key_name[0] = KS_KEY;
4804 key_name[1] = (key & 0xff);
4805 }
4806 p = find_termcode(key_name);
4807 if (p != NULL)
4808 {
4809 if (stringval != NULL)
4810 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004811 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004812 }
4813 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004814 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004817 varp = get_varp_scope(&(options[opt_idx]), scope);
4818
4819 if (flagsp != NULL)
4820 // Return the P_xxxx option flags.
4821 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 if (options[opt_idx].flags & P_STRING)
4824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004825 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004826 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (stringval != NULL)
4828 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004829 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004830 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4831 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004833 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004834 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004835 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004838 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 *stringval = vim_strsave(*(char_u **)(varp));
4840 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004841 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
4843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004844 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004845 return (options[opt_idx].flags & P_NUM)
4846 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (options[opt_idx].flags & P_NUM)
4848 *numval = *(long *)varp;
4849 else
4850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 // Special case: 'modified' is b_changed, but we also want to consider
4852 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if ((int *)varp == &curbuf->b_changed)
4854 *numval = curbufIsChanged();
4855 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004856 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004858 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859}
4860#endif
4861
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004862#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004863/*
4864 * Returns the option attributes and its value. Unlike the above function it
4865 * will return either global value or local value of the option depending on
4866 * what was requested, but it will never return global value if it was
4867 * requested to return local one and vice versa. Neither it will return
4868 * buffer-local value if it was requested to return window-local one.
4869 *
4870 * Pretends that option is absent if it is not present in the requested scope
4871 * (i.e. has no global, window-local or buffer-local value depending on
4872 * opt_type). Uses
4873 *
4874 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004875 * 0 hidden or unknown option, also option that does not have requested
4876 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004877 * see SOPT_* in vim.h for other flags
4878 *
4879 * Possible opt_type values: see SREQ_* in vim.h
4880 */
4881 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004882get_option_value_strict(
4883 char_u *name,
4884 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004885 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004886 int opt_type,
4887 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004888{
4889 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004890 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004891 struct vimoption *p;
4892 int r = 0;
4893
4894 opt_idx = findoption(name);
4895 if (opt_idx < 0)
4896 return 0;
4897
4898 p = &(options[opt_idx]);
4899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004900 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004901 if (p->var == NULL)
4902 return 0;
4903
4904 if (p->flags & P_BOOL)
4905 r |= SOPT_BOOL;
4906 else if (p->flags & P_NUM)
4907 r |= SOPT_NUM;
4908 else if (p->flags & P_STRING)
4909 r |= SOPT_STRING;
4910
4911 if (p->indir == PV_NONE)
4912 {
4913 if (opt_type == SREQ_GLOBAL)
4914 r |= SOPT_GLOBAL;
4915 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004916 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004917 }
4918 else
4919 {
4920 if (p->indir & PV_BOTH)
4921 r |= SOPT_GLOBAL;
4922 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004923 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004924
4925 if (p->indir & PV_WIN)
4926 {
4927 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004928 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004929 else
4930 r |= SOPT_WIN;
4931 }
4932 else if (p->indir & PV_BUF)
4933 {
4934 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004935 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004936 else
4937 r |= SOPT_BUF;
4938 }
4939 }
4940
4941 if (stringval == NULL)
4942 return r;
4943
4944 if (opt_type == SREQ_GLOBAL)
4945 varp = p->var;
4946 else
4947 {
4948 if (opt_type == SREQ_BUF)
4949 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004950 // Special case: 'modified' is b_changed, but we also want to
4951 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004952 if (p->indir == PV_MOD)
4953 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004954 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004955 varp = NULL;
4956 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004957# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004958 else if (p->indir == PV_KEY)
4959 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004960 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004961 *stringval = NULL;
4962 varp = NULL;
4963 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004964# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004965 else
4966 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004967 buf_T *save_curbuf = curbuf;
4968
4969 // only getting a pointer, no need to use aucmd_prepbuf()
4970 curbuf = (buf_T *)from;
4971 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004972 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004973 curbuf = save_curbuf;
4974 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004975 }
4976 }
4977 else if (opt_type == SREQ_WIN)
4978 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004979 win_T *save_curwin = curwin;
4980
4981 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004982 curbuf = curwin->w_buffer;
4983 varp = get_varp(p);
4984 curwin = save_curwin;
4985 curbuf = curwin->w_buffer;
4986 }
4987 if (varp == p->var)
4988 return (r | SOPT_UNSET);
4989 }
4990
4991 if (varp != NULL)
4992 {
4993 if (p->flags & P_STRING)
4994 *stringval = vim_strsave(*(char_u **)(varp));
4995 else if (p->flags & P_NUM)
4996 *numval = *(long *) varp;
4997 else
4998 *numval = *(int *)varp;
4999 }
5000
5001 return r;
5002}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005003
5004/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005005 * Iterate over options. First argument is a pointer to a pointer to a
5006 * structure inside options[] array, second is option type like in the above
5007 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005008 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005009 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005010 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005011 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005012 * first argument to NULL.
5013 *
5014 * Returns full option name for current option on each call.
5015 */
5016 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005017option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005018{
5019 struct vimoption *ret = NULL;
5020 do
5021 {
5022 if (*option == NULL)
5023 *option = (void *) options;
5024 else if (((struct vimoption *) (*option))->fullname == NULL)
5025 {
5026 *option = NULL;
5027 return NULL;
5028 }
5029 else
5030 *option = (void *) (((struct vimoption *) (*option)) + 1);
5031
5032 ret = ((struct vimoption *) (*option));
5033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005034 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005035 if (ret->var == NULL)
5036 {
5037 ret = NULL;
5038 continue;
5039 }
5040
5041 switch (opt_type)
5042 {
5043 case SREQ_GLOBAL:
5044 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5045 ret = NULL;
5046 break;
5047 case SREQ_BUF:
5048 if (!(ret->indir & PV_BUF))
5049 ret = NULL;
5050 break;
5051 case SREQ_WIN:
5052 if (!(ret->indir & PV_WIN))
5053 ret = NULL;
5054 break;
5055 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005056 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005057 return NULL;
5058 }
5059 }
5060 while (ret == NULL);
5061
5062 return (char_u *)ret->fullname;
5063}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005064#endif
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005067 * Return the flags for the option at 'opt_idx'.
5068 */
5069 long_u
5070get_option_flags(int opt_idx)
5071{
5072 return options[opt_idx].flags;
5073}
5074
5075/*
5076 * Set a flag for the option at 'opt_idx'.
5077 */
5078 void
5079set_option_flag(int opt_idx, long_u flag)
5080{
5081 options[opt_idx].flags |= flag;
5082}
5083
5084/*
5085 * Clear a flag for the option at 'opt_idx'.
5086 */
5087 void
5088clear_option_flag(int opt_idx, long_u flag)
5089{
5090 options[opt_idx].flags &= ~flag;
5091}
5092
5093/*
5094 * Returns TRUE if the option at 'opt_idx' is a global option
5095 */
5096 int
5097is_global_option(int opt_idx)
5098{
5099 return options[opt_idx].indir == PV_NONE;
5100}
5101
5102/*
5103 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5104 * local value.
5105 */
5106 int
5107is_global_local_option(int opt_idx)
5108{
5109 return options[opt_idx].indir & PV_BOTH;
5110}
5111
5112/*
5113 * Returns TRUE if the option at 'opt_idx' is a window-local option
5114 */
5115 int
5116is_window_local_option(int opt_idx)
5117{
5118 return options[opt_idx].var == VAR_WIN;
5119}
5120
5121/*
5122 * Returns TRUE if the option at 'opt_idx' is a hidden option
5123 */
5124 int
5125is_hidden_option(int opt_idx)
5126{
5127 return options[opt_idx].var == NULL;
5128}
5129
5130#if defined(FEAT_CRYPT) || defined(PROTO)
5131/*
5132 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5133 */
5134 int
5135is_crypt_key_option(int opt_idx)
5136{
5137 return options[opt_idx].indir == PV_KEY;
5138}
5139#endif
5140
5141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 * Set the value of option "name".
5143 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005144 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005145 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005147 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005148set_option_value(
5149 char_u *name,
5150 long number,
5151 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005152 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 int opt_idx;
5155 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005156 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005157 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005160 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005161 {
5162 int key;
5163
5164 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005165 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005166 {
5167 char_u key_name[2];
5168
5169 if (key < 0)
5170 {
5171 key_name[0] = KEY2TERMCAP0(key);
5172 key_name[1] = KEY2TERMCAP1(key);
5173 }
5174 else
5175 {
5176 key_name[0] = KS_KEY;
5177 key_name[1] = (key & 0xff);
5178 }
5179 add_termcode(key_name, string, FALSE);
5180 if (full_screen)
5181 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005182 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005183 return NULL;
5184 }
5185
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005186 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 else
5189 {
5190 flags = options[opt_idx].flags;
5191#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005192 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005194 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005195 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005196 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005199 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005200 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005201
5202 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5203 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005205 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005207 int idx;
5208
5209 // Either we are given a string or we are setting option
5210 // to zero.
5211 for (idx = 0; string[idx] == '0'; ++idx)
5212 ;
5213 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005214 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005215 // There's another character after zeros or the string
5216 // is empty. In both cases, we are trying to set a
5217 // num option using a string.
5218 semsg(_(e_number_required_after_str_equal_str),
5219 name, string);
5220 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005221
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005224 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005225 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005226 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005227 errbuf, sizeof(errbuf), opt_flags);
5228 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005229 else
5230 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005234 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235}
5236
5237/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005238 * Call set_option_value() and when an error is returned report it.
5239 */
5240 void
5241set_option_value_give_err(
5242 char_u *name,
5243 long number,
5244 char_u *string,
5245 int opt_flags) // OPT_LOCAL or 0 (both)
5246{
5247 char *errmsg = set_option_value(name, number, string, opt_flags);
5248
5249 if (errmsg != NULL)
5250 emsg(_(errmsg));
5251}
5252
5253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 * Get the terminal code for a terminal option.
5255 * Returns NULL when not found.
5256 */
5257 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005258get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 int opt_idx;
5261 char_u *varp;
5262
5263 if (tname[0] != 't' || tname[1] != '_' ||
5264 tname[2] == NUL || tname[3] == NUL)
5265 return NULL;
5266 if ((opt_idx = findoption(tname)) >= 0)
5267 {
5268 varp = get_varp(&(options[opt_idx]));
5269 if (varp != NULL)
5270 varp = *(char_u **)(varp);
5271 return varp;
5272 }
5273 return find_termcode(tname + 2);
5274}
5275
5276 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005277get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 int i;
5280
5281 i = findoption((char_u *)"hl");
5282 if (i >= 0)
5283 return options[i].def_val[VI_DEFAULT];
5284 return (char_u *)NULL;
5285}
5286
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005287 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005288get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005289{
5290 int i;
5291
5292 i = findoption((char_u *)"enc");
5293 if (i >= 0)
5294 return options[i].def_val[VI_DEFAULT];
5295 return (char_u *)NULL;
5296}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005297
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005298#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005299 int
5300is_option_allocated(char *name)
5301{
5302 int idx = findoption((char_u *)name);
5303
5304 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5305}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005306#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005307
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005308#if defined(FEAT_EVAL) || defined(PROTO)
5309/*
5310 * Return TRUE if "name" is a string option.
5311 * Returns FALSE if option "name" does not exist.
5312 */
5313 int
5314is_string_option(char_u *name)
5315{
5316 int idx = findoption(name);
5317
5318 return idx >= 0 && (options[idx].flags & P_STRING);
5319}
5320#endif
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322/*
5323 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005324 * When "has_lt" is true there is a '<' before "*arg_arg".
5325 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 */
5327 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005328find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005330 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005332 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005334 // Don't use get_special_key_code() for t_xx, we don't want it to call
5335 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5337 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005338 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005340 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005342 key = find_special_key(&arg, &modifiers,
5343 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005344 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 key = 0;
5346 }
5347 return key;
5348}
5349
5350/*
5351 * if 'all' == 0: show changed options
5352 * if 'all' == 1: show all normal options
5353 * if 'all' == 2: show all terminal options
5354 */
5355 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005356showoptions(
5357 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005358 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
5360 struct vimoption *p;
5361 int col;
5362 int isterm;
5363 char_u *varp;
5364 struct vimoption **items;
5365 int item_count;
5366 int run;
5367 int row, rows;
5368 int cols;
5369 int i;
5370 int len;
5371
5372#define INC 20
5373#define GAP 3
5374
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005375 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (items == NULL)
5377 return;
5378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005379 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005381 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005383 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005385 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005387 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005389 // Do the loop two times:
5390 // 1. display the short items
5391 // 2. display the long items (only strings and numbers)
5392 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 for (run = 1; run <= 2 && !got_int; ++run)
5394 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005395 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 item_count = 0;
5397 for (p = &options[0]; p->fullname != NULL; p++)
5398 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005399 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005400 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005401 continue;
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 varp = NULL;
5404 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005405 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 if (p->indir != PV_NONE && !isterm)
5408 varp = get_varp_scope(p, opt_flags);
5409 }
5410 else
5411 varp = get_varp(p);
5412 if (varp != NULL
5413 && ((all == 2 && isterm)
5414 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005415 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005417 if (opt_flags & OPT_ONECOLUMN)
5418 len = Columns;
5419 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005420 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 else
5422 {
5423 option_value2string(p, opt_flags);
5424 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5425 }
5426 if ((len <= INC - GAP && run == 1) ||
5427 (len > INC - GAP && run == 2))
5428 items[item_count++] = p;
5429 }
5430 }
5431
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005432 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (run == 1)
5434 {
5435 cols = (Columns + GAP - 3) / INC;
5436 if (cols == 0)
5437 cols = 1;
5438 rows = (item_count + cols - 1) / cols;
5439 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 rows = item_count;
5442 for (row = 0; row < rows && !got_int; ++row)
5443 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 msg_putchar('\n'); // go to next line
5445 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 break;
5447 col = 0;
5448 for (i = row; i < item_count; i += rows)
5449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 showoneopt(items[i], opt_flags);
5452 col += INC;
5453 }
5454 out_flush();
5455 ui_breakcheck();
5456 }
5457 }
5458 vim_free(items);
5459}
5460
5461/*
5462 * Return TRUE if option "p" has its default value.
5463 */
5464 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005465optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 int dvi;
5468
5469 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005470 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005471 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005473 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005475 // the cast to long is required for Manx C, long_i is
5476 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005477 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005478 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5480}
5481
5482/*
5483 * showoneopt: show the value of one option
5484 * must not be called with a hidden option!
5485 */
5486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005487showoneopt(
5488 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005489 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005491 char_u *varp;
5492 int save_silent = silent_mode;
5493
5494 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005495 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 varp = get_varp_scope(p, opt_flags);
5498
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005499 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5501 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005502 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005504 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005506 msg_puts(" ");
5507 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (!(p->flags & P_BOOL))
5509 {
5510 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005511 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 option_value2string(p, opt_flags);
5513 msg_outtrans(NameBuff);
5514 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005515
5516 silent_mode = save_silent;
5517 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518}
5519
5520/*
5521 * Write modified options as ":set" commands to a file.
5522 *
5523 * There are three values for "opt_flags":
5524 * OPT_GLOBAL: Write global option values and fresh values of
5525 * buffer-local options (used for start of a session
5526 * file).
5527 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5528 * curwin (used for a vimrc file).
5529 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5530 * and local values for window-local options of
5531 * curwin. Local values are also written when at the
5532 * default value, because a modeline or autocommand
5533 * may have set them when doing ":edit file" and the
5534 * user has set them back at the default or fresh
5535 * value.
5536 * When "local_only" is TRUE, don't write fresh
5537 * values, only local values (for ":mkview").
5538 * (fresh value = value used for a new buffer or window for a local option).
5539 *
5540 * Return FAIL on error, OK otherwise.
5541 */
5542 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005543makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005546 char_u *varp; // currently used value
5547 char_u *varp_fresh; // local value
5548 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 char *cmd;
5550 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005551 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 /*
5554 * The options that don't have a default (terminal name, columns, lines)
5555 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005556 * Do the loop over "options[]" twice: once for options with the
5557 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005559 for (pri = 1; pri >= 0; --pri)
5560 {
5561 for (p = &options[0]; !istermoption(p); p++)
5562 if (!(p->flags & P_NO_MKRC)
5563 && !istermoption(p)
5564 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5568 continue;
5569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005570 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5571 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5573 continue;
5574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005577 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 continue;
5579
Bram Moolenaard23b7142021-04-17 21:04:34 +02005580 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5581 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005582 continue;
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 round = 2;
5585 if (p->indir != PV_NONE)
5586 {
5587 if (p->var == VAR_WIN)
5588 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005589 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 if (!(opt_flags & OPT_LOCAL))
5591 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005592 // When fresh value of window-local option is not at the
5593 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5595 {
5596 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005597 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 round = 1;
5600 varp_local = varp;
5601 varp = varp_fresh;
5602 }
5603 }
5604 }
5605 }
5606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005607 // Round 1: fresh value for window-local options.
5608 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 for ( ; round <= 2; varp = varp_local, ++round)
5610 {
5611 if (round == 1 || (opt_flags & OPT_GLOBAL))
5612 cmd = "set";
5613 else
5614 cmd = "setlocal";
5615
5616 if (p->flags & P_BOOL)
5617 {
5618 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5619 return FAIL;
5620 }
5621 else if (p->flags & P_NUM)
5622 {
5623 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5624 return FAIL;
5625 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005626 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005628 int do_endif = FALSE;
5629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 // Don't set 'syntax' and 'filetype' again if the value is
5631 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005632 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005633#if defined(FEAT_SYN_HL)
5634 p->indir == PV_SYN ||
5635#endif
5636 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
5638 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5639 *(char_u **)(varp)) < 0
5640 || put_eol(fd) < 0)
5641 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005642 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
5644 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005645 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005647 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 if (put_line(fd, "endif") == FAIL)
5650 return FAIL;
5651 }
5652 }
5653 }
5654 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return OK;
5657}
5658
5659#if defined(FEAT_FOLDING) || defined(PROTO)
5660/*
5661 * Generate set commands for the local fold options only. Used when
5662 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5663 */
5664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005665makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005667 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005669 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 == FAIL
5671# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005672 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005674 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 == FAIL
5676 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5677 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5678 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5679 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5680 )
5681 return FAIL;
5682
5683 return OK;
5684}
5685#endif
5686
5687 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005688put_setstring(
5689 FILE *fd,
5690 char *cmd,
5691 char *name,
5692 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005693 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005696 char_u *buf = NULL;
5697 char_u *part = NULL;
5698 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5701 return FAIL;
5702 if (*valuep != NULL)
5703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005704 // Output 'pastetoggle' as key names. For other
5705 // options some characters have to be escaped with
5706 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 if (valuep == &p_pt)
5708 {
5709 s = *valuep;
5710 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005711 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 return FAIL;
5713 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005714 // expand the option value, replace $HOME by ~
5715 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005717 int size = (int)STRLEN(*valuep) + 1;
5718
5719 // replace home directory in the whole option value into "buf"
5720 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005721 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005722 goto fail;
5723 home_replace(NULL, *valuep, buf, size, FALSE);
5724
5725 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005726 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005727 // can be expanded when read back.
5728 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5729 && vim_strchr(*valuep, ',') != NULL)
5730 {
5731 part = alloc(size);
5732 if (part == NULL)
5733 goto fail;
5734
5735 // write line break to clear the option, e.g. ':set rtp='
5736 if (put_eol(fd) == FAIL)
5737 goto fail;
5738
5739 p = buf;
5740 while (*p != NUL)
5741 {
5742 // for each comma separated option part, append value to
5743 // the option, :set rtp+=value
5744 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5745 goto fail;
5746 (void)copy_option_part(&p, part, size, ",");
5747 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5748 goto fail;
5749 }
5750 vim_free(buf);
5751 vim_free(part);
5752 return OK;
5753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005755 {
5756 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005758 }
5759 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761 else if (put_escstr(fd, *valuep, 2) == FAIL)
5762 return FAIL;
5763 }
5764 if (put_eol(fd) < 0)
5765 return FAIL;
5766 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005767fail:
5768 vim_free(buf);
5769 vim_free(part);
5770 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771}
5772
5773 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774put_setnum(
5775 FILE *fd,
5776 char *cmd,
5777 char *name,
5778 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 long wc;
5781
5782 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5783 return FAIL;
5784 if (wc_use_keyname((char_u *)valuep, &wc))
5785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005786 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5788 return FAIL;
5789 }
5790 else if (fprintf(fd, "%ld", *valuep) < 0)
5791 return FAIL;
5792 if (put_eol(fd) < 0)
5793 return FAIL;
5794 return OK;
5795}
5796
5797 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005798put_setbool(
5799 FILE *fd,
5800 char *cmd,
5801 char *name,
5802 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005804 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005805 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5807 || put_eol(fd) < 0)
5808 return FAIL;
5809 return OK;
5810}
5811
5812/*
5813 * Clear all the terminal options.
5814 * If the option has been allocated, free the memory.
5815 * Terminal options are never hidden or indirect.
5816 */
5817 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005818clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005820 // Reset a few things before clearing the old options. This may cause
5821 // outputting a few things that the terminal doesn't understand, but the
5822 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005823 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005824 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005826 // When starting the GUI close the display opened for the clipboard.
5827 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 if (gui.starting)
5829 clear_xterm_clip();
5830#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005831 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005833 free_termoptions();
5834}
5835
5836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005837free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005838{
5839 struct vimoption *p;
5840
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005841 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 if (istermoption(p))
5843 {
5844 if (p->flags & P_ALLOCED)
5845 free_string_option(*(char_u **)(p->var));
5846 if (p->flags & P_DEF_ALLOCED)
5847 free_string_option(p->def_val[VI_DEFAULT]);
5848 *(char_u **)(p->var) = empty_option;
5849 p->def_val[VI_DEFAULT] = empty_option;
5850 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005851#ifdef FEAT_EVAL
5852 // remember where the option was cleared
5853 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
5856 clear_termcodes();
5857}
5858
5859/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005860 * Free the string for one term option, if it was allocated.
5861 * Set the string to empty_option and clear allocated flag.
5862 * "var" points to the option value.
5863 */
5864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005865free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005866{
5867 struct vimoption *p;
5868
5869 for (p = &options[0]; p->fullname != NULL; p++)
5870 if (p->var == var)
5871 {
5872 if (p->flags & P_ALLOCED)
5873 free_string_option(*(char_u **)(p->var));
5874 *(char_u **)(p->var) = empty_option;
5875 p->flags &= ~P_ALLOCED;
5876 break;
5877 }
5878}
5879
5880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 * Set the terminal option defaults to the current value.
5882 * Used after setting the terminal name.
5883 */
5884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005885set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 struct vimoption *p;
5888
5889 for (p = &options[0]; p->fullname != NULL; p++)
5890 {
5891 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5892 {
5893 if (p->flags & P_DEF_ALLOCED)
5894 {
5895 free_string_option(p->def_val[VI_DEFAULT]);
5896 p->flags &= ~P_DEF_ALLOCED;
5897 }
5898 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5899 if (p->flags & P_ALLOCED)
5900 {
5901 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005902 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 }
5904 }
5905 }
5906}
5907
5908/*
5909 * return TRUE if 'p' starts with 't_'
5910 */
5911 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005912istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5915}
5916
Bram Moolenaardac13472019-09-16 21:06:21 +02005917/*
5918 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5919 */
5920 int
5921istermoption_idx(int opt_idx)
5922{
5923 return istermoption(&options[opt_idx]);
5924}
5925
Bram Moolenaar113e1072019-01-20 15:30:40 +01005926#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005928 * Unset local option value, similar to ":set opt<".
5929 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005931unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005932{
5933 struct vimoption *p;
5934 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005935 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005936
5937 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005938 if (opt_idx < 0)
5939 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005940 p = &(options[opt_idx]);
5941
5942 switch ((int)p->indir)
5943 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005944 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005945 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005946 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005947 break;
5948 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005949 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005950 break;
5951 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005952 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005953 break;
5954 case PV_AR:
5955 buf->b_p_ar = -1;
5956 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005957 case PV_BKC:
5958 clear_string_option(&buf->b_p_bkc);
5959 buf->b_bkc_flags = 0;
5960 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005961 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005962 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005963 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005964 case PV_TC:
5965 clear_string_option(&buf->b_p_tc);
5966 buf->b_tc_flags = 0;
5967 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01005968 case PV_SISO:
5969 curwin->w_p_siso = -1;
5970 break;
5971 case PV_SO:
5972 curwin->w_p_so = -1;
5973 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005974# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005975 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005976 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005977 break;
5978 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005979 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005980 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005981# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005982 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005983 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005984 break;
5985 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005986 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005987 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005988# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005989 case PV_TSRFU:
5990 clear_string_option(&buf->b_p_tsrfu);
5991 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005992# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005993 case PV_FP:
5994 clear_string_option(&buf->b_p_fp);
5995 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005996# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005997 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005998 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005999 break;
6000 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006001 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006002 break;
6003 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006004 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006005 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006006# endif
6007# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006008 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006009 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006010 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006011# endif
6012# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006013 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006014 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006015 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006016# endif
6017# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006018 case PV_SBR:
6019 clear_string_option(&((win_T *)from)->w_p_sbr);
6020 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006021# endif
6022# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006023 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006024 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006025 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006026# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006027 case PV_UL:
6028 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6029 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006030 case PV_LW:
6031 clear_string_option(&buf->b_p_lw);
6032 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006033 case PV_MENC:
6034 clear_string_option(&buf->b_p_menc);
6035 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006036 case PV_LCS:
6037 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006038 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006039 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006040 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006041 case PV_FCS:
6042 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006043 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006044 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006045 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006046 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006047 clear_string_option(&((win_T *)from)->w_p_ve);
6048 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006049 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006050 }
6051}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006052#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006053
6054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006056 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 */
6058 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006059get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006061 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {
6063 if (p->var == VAR_WIN)
6064 return (char_u *)GLOBAL_WO(get_varp(p));
6065 return p->var;
6066 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006067 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
6069 switch ((int)p->indir)
6070 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006071 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006073 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6074 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6075 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006077 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6078 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6079 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006080 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006081 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006082 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006083 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6084 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006086 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6087 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006089 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6090 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006091#ifdef FEAT_COMPL_FUNC
6092 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6093#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006094#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6095 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6096#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006097#if defined(FEAT_CRYPT)
6098 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6099#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006100#ifdef FEAT_LINEBREAK
6101 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6102#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006103#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006104 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006105#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006106 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006107 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006108 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006109 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006110 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006111 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006112 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006113
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006115 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117 return get_varp(p);
6118}
6119
6120/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006121 * Get pointer to option variable at 'opt_idx', depending on local or global
6122 * scope.
6123 */
6124 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006125get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006126{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006127 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006128}
6129
6130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 * Get pointer to option variable.
6132 */
6133 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006134get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006136 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 if (p->var == NULL)
6138 return NULL;
6139
6140 switch ((int)p->indir)
6141 {
6142 case PV_NONE: return p->var;
6143
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006144 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006145 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006147 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006149 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006151 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006153 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006155 case PV_TC: return *curbuf->b_p_tc != NUL
6156 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006157 case PV_BKC: return *curbuf->b_p_bkc != NUL
6158 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006159 case PV_SISO: return curwin->w_p_siso >= 0
6160 ? (char_u *)&(curwin->w_p_siso) : p->var;
6161 case PV_SO: return curwin->w_p_so >= 0
6162 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006164 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006166 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6168#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006169 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006171 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006173#ifdef FEAT_COMPL_FUNC
6174 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6175 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6176#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006177 case PV_FP: return *curbuf->b_p_fp != NUL
6178 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006180 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006182 case PV_GP: return *curbuf->b_p_gp != NUL
6183 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6184 case PV_MP: return *curbuf->b_p_mp != NUL
6185 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006187#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6188 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6189 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6190#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006191#if defined(FEAT_CRYPT)
6192 case PV_CM: return *curbuf->b_p_cm != NUL
6193 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6194#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006195#ifdef FEAT_LINEBREAK
6196 case PV_SBR: return *curwin->w_p_sbr != NUL
6197 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6198#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006199#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006200 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006201 ? (char_u *)&(curwin->w_p_stl) : p->var;
6202#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006203 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6204 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006205 case PV_LW: return *curbuf->b_p_lw != NUL
6206 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006207 case PV_MENC: return *curbuf->b_p_menc != NUL
6208 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209#ifdef FEAT_ARABIC
6210 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6211#endif
6212 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006213 case PV_LCS: return *curwin->w_p_lcs != NUL
6214 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006215 case PV_FCS: return *curwin->w_p_fcs != NUL
6216 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006217 case PV_VE: return *curwin->w_p_ve != NUL
6218 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006219#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006220 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6221#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006222#ifdef FEAT_SYN_HL
6223 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6224 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006225 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006226 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228#ifdef FEAT_DIFF
6229 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6230#endif
6231#ifdef FEAT_FOLDING
6232 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6233 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6234 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6235 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6236 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6237 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6238 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6239# ifdef FEAT_EVAL
6240 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6241 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6242# endif
6243 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6244#endif
6245 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006246 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006247#ifdef FEAT_LINEBREAK
6248 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006251 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006252#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6254#endif
6255#ifdef FEAT_RIGHTLEFT
6256 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6257 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6258#endif
6259 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006260 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6262#ifdef FEAT_LINEBREAK
6263 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006264 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6265 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006267 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006269 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006270#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006271 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6272 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6273#endif
6274#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006275 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6276 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6277 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279
6280 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6281 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6284 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6286 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6288 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6289 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006290 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293#ifdef FEAT_FOLDING
6294 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006297#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006298 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006300#ifdef FEAT_COMPL_FUNC
6301 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006302 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006303#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006304#ifdef FEAT_EVAL
6305 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6306#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006307 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006309 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006315 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6317 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6318 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6319 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6320#ifdef FEAT_FIND_ID
6321# ifdef FEAT_EVAL
6322 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6323# endif
6324#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006325#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6327 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6328#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006329#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006330 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332#ifdef FEAT_CRYPT
6333 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006336 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6338 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6339 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6340 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6341 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006343 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6350#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006351 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006352 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6353#endif
6354#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006355 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6356 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6357 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006358 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359#endif
6360 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6361 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6362 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6363 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006364#ifdef FEAT_PERSISTENT_UNDO
6365 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6368#ifdef FEAT_KEYMAP
6369 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6370#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006371#ifdef FEAT_SIGNS
6372 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6373#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006374#ifdef FEAT_VARTABS
6375 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6376 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6377#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006378 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006380 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 return (char_u *)&(curbuf->b_p_wm);
6382}
6383
6384/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006385 * Return a pointer to the variable for option at 'opt_idx'
6386 */
6387 char_u *
6388get_option_var(int opt_idx)
6389{
6390 return options[opt_idx].var;
6391}
6392
Dominique Pelle748b3082022-01-08 12:41:16 +00006393#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006394/*
6395 * Return the full name of the option at 'opt_idx'
6396 */
6397 char_u *
6398get_option_fullname(int opt_idx)
6399{
6400 return (char_u *)options[opt_idx].fullname;
6401}
Dominique Pelle748b3082022-01-08 12:41:16 +00006402#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006403
6404/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006405 * Return the did_set callback function for the option at 'opt_idx'
6406 */
6407 opt_did_set_cb_T
6408get_option_did_set_cb(int opt_idx)
6409{
6410 return options[opt_idx].opt_did_set_cb;
6411}
6412
6413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 * Get the value of 'equalprg', either the buffer-local one or the global one.
6415 */
6416 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006417get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
6419 if (*curbuf->b_p_ep == NUL)
6420 return p_ep;
6421 return curbuf->b_p_ep;
6422}
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424/*
6425 * Copy options from one window to another.
6426 * Used when splitting a window.
6427 */
6428 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006429win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6432 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006433 after_copy_winopt(wp_to);
6434}
6435
6436/*
6437 * After copying window options: update variables depending on options.
6438 */
6439 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006440after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006441{
6442#ifdef FEAT_LINEBREAK
6443 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006444#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006445#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006446 fill_culopt_flags(NULL, wp);
6447 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006448#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006449 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6450 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006451}
6452
6453 static char_u *
6454copy_option_val(char_u *val)
6455{
6456 if (val == empty_option)
6457 return empty_option; // no need to allocate memory
6458 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
6461/*
6462 * Copy the options from one winopt_T to another.
6463 * Doesn't free the old option values in "to", use clear_winopt() for that.
6464 * The 'scroll' option is not copied, because it depends on the window height.
6465 * The 'previewwindow' option is reset, there can be only one preview window.
6466 */
6467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470#ifdef FEAT_ARABIC
6471 to->wo_arab = from->wo_arab;
6472#endif
6473 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006474 to->wo_lcs = copy_option_val(from->wo_lcs);
6475 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006477 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006478 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006479 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006480#ifdef FEAT_LINEBREAK
6481 to->wo_nuw = from->wo_nuw;
6482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483#ifdef FEAT_RIGHTLEFT
6484 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006485 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006487#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006488 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006489#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006490#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006491 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006494#ifdef FEAT_DIFF
6495 to->wo_wrap_save = from->wo_wrap_save;
6496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497#ifdef FEAT_LINEBREAK
6498 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006499 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006500 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006502 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006504 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006505 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006506 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006507 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006508#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006509 to->wo_spell = from->wo_spell;
6510#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006511#ifdef FEAT_SYN_HL
6512 to->wo_cuc = from->wo_cuc;
6513 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006514 to->wo_culopt = copy_option_val(from->wo_culopt);
6515 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517#ifdef FEAT_DIFF
6518 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006519 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006521#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006522 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006523 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006524#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006525#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006526 to->wo_twk = copy_option_val(from->wo_twk);
6527 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529#ifdef FEAT_FOLDING
6530 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006531 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006533 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006534 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 to->wo_fml = from->wo_fml;
6536 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006537 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006538 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006539 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006540 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 to->wo_fdn = from->wo_fdn;
6542# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006543 to->wo_fde = copy_option_val(from->wo_fde);
6544 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006546 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006548#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006549 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006550#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006551
6552#ifdef FEAT_EVAL
6553 // Copy the script context so that we know where the value was last set.
6554 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6555 sizeof(to->wo_script_ctx));
6556#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006557 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558}
6559
6560/*
6561 * Check string options in a window for a NULL value.
6562 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006564check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
6566 check_winopt(&win->w_onebuf_opt);
6567 check_winopt(&win->w_allbuf_opt);
6568}
6569
6570/*
6571 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6572 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006574check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576#ifdef FEAT_FOLDING
6577 check_string_option(&wop->wo_fdi);
6578 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006579 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580# ifdef FEAT_EVAL
6581 check_string_option(&wop->wo_fde);
6582 check_string_option(&wop->wo_fdt);
6583# endif
6584 check_string_option(&wop->wo_fmr);
6585#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006586#ifdef FEAT_SIGNS
6587 check_string_option(&wop->wo_scl);
6588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589#ifdef FEAT_RIGHTLEFT
6590 check_string_option(&wop->wo_rlc);
6591#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006592#ifdef FEAT_LINEBREAK
6593 check_string_option(&wop->wo_sbr);
6594#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006595#ifdef FEAT_STL_OPT
6596 check_string_option(&wop->wo_stl);
6597#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006598#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006599 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006600 check_string_option(&wop->wo_cc);
6601#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006602#ifdef FEAT_CONCEAL
6603 check_string_option(&wop->wo_cocu);
6604#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006605#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006606 check_string_option(&wop->wo_twk);
6607 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006608#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006609#ifdef FEAT_LINEBREAK
6610 check_string_option(&wop->wo_briopt);
6611#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006612 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006613 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006614 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006615 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616}
6617
6618/*
6619 * Free the allocated memory inside a winopt_T.
6620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006622clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624#ifdef FEAT_FOLDING
6625 clear_string_option(&wop->wo_fdi);
6626 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006627 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628# ifdef FEAT_EVAL
6629 clear_string_option(&wop->wo_fde);
6630 clear_string_option(&wop->wo_fdt);
6631# endif
6632 clear_string_option(&wop->wo_fmr);
6633#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006634#ifdef FEAT_SIGNS
6635 clear_string_option(&wop->wo_scl);
6636#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006637#ifdef FEAT_LINEBREAK
6638 clear_string_option(&wop->wo_briopt);
6639#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006640 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#ifdef FEAT_RIGHTLEFT
6642 clear_string_option(&wop->wo_rlc);
6643#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006644#ifdef FEAT_LINEBREAK
6645 clear_string_option(&wop->wo_sbr);
6646#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006647#ifdef FEAT_STL_OPT
6648 clear_string_option(&wop->wo_stl);
6649#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006650#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006651 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006652 clear_string_option(&wop->wo_cc);
6653#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006654#ifdef FEAT_CONCEAL
6655 clear_string_option(&wop->wo_cocu);
6656#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006657#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006658 clear_string_option(&wop->wo_twk);
6659 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006660#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006661 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006662 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006663 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
6665
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006666#ifdef FEAT_EVAL
6667// Index into the options table for a buffer-local option enum.
6668static int buf_opt_idx[BV_COUNT];
6669# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6670
6671/*
6672 * Initialize buf_opt_idx[] if not done already.
6673 */
6674 static void
6675init_buf_opt_idx(void)
6676{
6677 static int did_init_buf_opt_idx = FALSE;
6678 int i;
6679
6680 if (did_init_buf_opt_idx)
6681 return;
6682 did_init_buf_opt_idx = TRUE;
6683 for (i = 0; !istermoption_idx(i); i++)
6684 if (options[i].indir & PV_BUF)
6685 buf_opt_idx[options[i].indir & PV_MASK] = i;
6686}
6687#else
6688# define COPY_OPT_SCTX(buf, bv)
6689#endif
6690
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691/*
6692 * Copy global option values to local options for one buffer.
6693 * Used when creating a new buffer and sometimes when entering a buffer.
6694 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006695 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6697 * appropriate.
6698 * BCO_NOHELP Don't copy the values to a help buffer.
6699 */
6700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 int dont_do_help;
6706 int did_isk = FALSE;
6707
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006708 // Skip this when the option defaults have not been set yet. Happens when
6709 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (p_cpo != NULL)
6711 {
6712 /*
6713 * Always copy when entering and 'cpo' contains 'S'.
6714 * Don't copy when already initialized.
6715 * Don't copy when 'cpo' contains 's' and not entering.
6716 * 'S' BCO_ENTER initialized 's' should_copy
6717 * yes yes X X TRUE
6718 * yes no yes X FALSE
6719 * no X yes X FALSE
6720 * X no no yes FALSE
6721 * X no no no TRUE
6722 * no yes no X TRUE
6723 */
6724 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6725 && (buf->b_p_initialized
6726 || (!(flags & BCO_ENTER)
6727 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6728 should_copy = FALSE;
6729
6730 if (should_copy || (flags & BCO_ALWAYS))
6731 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006732#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006733 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006734 init_buf_opt_idx();
6735#endif
6736 // Don't copy the options specific to a help buffer when
6737 // BCO_NOHELP is given or the options were initialized already
6738 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6740 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006741 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
6743 save_p_isk = buf->b_p_isk;
6744 buf->b_p_isk = NULL;
6745 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006746 // Always free the allocated strings. If not already initialized,
6747 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (!buf->b_p_initialized)
6749 {
6750 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006751 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006754 switch (*p_ffs)
6755 {
6756 case 'm':
6757 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6758 case 'd':
6759 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6760 case 'u':
6761 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6762 default:
6763 buf->b_p_ff = vim_strsave(p_ff);
6764 }
6765 if (buf->b_p_ff != NULL)
6766 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 buf->b_p_bh = empty_option;
6768 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 }
6770 else
6771 free_buf_options(buf, FALSE);
6772
6773 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006774 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 buf->b_p_ai_nopaste = p_ai_nopaste;
6776 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006777 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006779 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 buf->b_p_tw_nopaste = p_tw_nopaste;
6781 buf->b_p_tw_nobin = p_tw_nobin;
6782 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006783 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 buf->b_p_wm_nopaste = p_wm_nopaste;
6785 buf->b_p_wm_nobin = p_wm_nobin;
6786 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006787 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006788 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006789 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006790 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006791 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006793 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006795 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006797 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 buf->b_p_ml_nobin = p_ml_nobin;
6799 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006800 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006801 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006802 buf->b_p_swf = FALSE;
6803 else
6804 {
6805 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006806 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006809 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006810#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006811 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006812 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006814#ifdef FEAT_COMPL_FUNC
6815 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006816 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006817 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006818 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006819 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006820 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006821#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006822#ifdef FEAT_EVAL
6823 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006824 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006825 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006828 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006830#ifdef FEAT_VARTABS
6831 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006832 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006833 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006834 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006835 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006836 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006837 buf->b_p_vsts_nopaste = p_vsts_nopaste
6838 ? vim_strsave(p_vsts_nopaste) : NULL;
6839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006841 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006843 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844#ifdef FEAT_FOLDING
6845 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006846 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847#endif
6848 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006849 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006850 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006851 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006852 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6853 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006855 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006857 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006859 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006861 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006864 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006866 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006868 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006869 buf->b_p_cinsd = vim_strsave(p_cinsd);
6870 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006871 buf->b_p_lop = vim_strsave(p_lop);
6872 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006873
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006874 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006877 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006879 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006881 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006883 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006885 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006886 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006887 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006888#endif
6889#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006890 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006891 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006892 (void)compile_cap_prog(&buf->b_s);
6893 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006894 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006895 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006896 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006897 buf->b_s.b_p_spo = vim_strsave(p_spo);
6898 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006900#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006902 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006904 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006906 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907#if defined(FEAT_EVAL)
6908 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006909 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911#ifdef FEAT_CRYPT
6912 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006913 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006916 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917#ifdef FEAT_KEYMAP
6918 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006919 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 buf->b_kmap_state |= KEYMAP_INIT;
6921#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006922#ifdef FEAT_TERMINAL
6923 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006924 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006925#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006926 // This isn't really an option, but copying the langmap and IME
6927 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006929 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006931 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006933 // options that are normally global but also have a local value
6934 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006936 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006937 buf->b_p_bkc = empty_option;
6938 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939#ifdef FEAT_QUICKFIX
6940 buf->b_p_gp = empty_option;
6941 buf->b_p_mp = empty_option;
6942 buf->b_p_efm = empty_option;
6943#endif
6944 buf->b_p_ep = empty_option;
6945 buf->b_p_kp = empty_option;
6946 buf->b_p_path = empty_option;
6947 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006948 buf->b_p_tc = empty_option;
6949 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950#ifdef FEAT_FIND_ID
6951 buf->b_p_def = empty_option;
6952 buf->b_p_inc = empty_option;
6953# ifdef FEAT_EVAL
6954 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006955 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956# endif
6957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 buf->b_p_dict = empty_option;
6959 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006960#ifdef FEAT_COMPL_FUNC
6961 buf->b_p_tsrfu = empty_option;
6962#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006963 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006965#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6966 buf->b_p_bexpr = empty_option;
6967#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006968#if defined(FEAT_CRYPT)
6969 buf->b_p_cm = empty_option;
6970#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006971#ifdef FEAT_PERSISTENT_UNDO
6972 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006973 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006974#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006975 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006976 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006978 // Don't copy the options set by ex_help(), use the saved values,
6979 // when going from a help buffer to a non-help buffer.
6980 // Don't touch these at all when BCO_NOHELP is used and going from
6981 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006985#ifdef FEAT_VARTABS
6986 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006987 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006988 else
6989 buf->b_p_vts_array = NULL;
6990#endif
6991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 else
6993 {
6994 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006995 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 did_isk = TRUE;
6997 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006998 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006999#ifdef FEAT_VARTABS
7000 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007001 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007002 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007003 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007004 else
7005 buf->b_p_vts_array = NULL;
7006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (buf->b_p_bt[0] == 'h')
7009 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007011 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 }
7013 }
7014
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007015 // When the options should be copied (ignoring BCO_ALWAYS), set the
7016 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (should_copy)
7018 buf->b_p_initialized = TRUE;
7019 }
7020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007021 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 if (did_isk)
7023 (void)buf_init_chartab(buf, FALSE);
7024}
7025
7026/*
7027 * Reset the 'modifiable' option and its default value.
7028 */
7029 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007030reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 int opt_idx;
7033
7034 curbuf->b_p_ma = FALSE;
7035 p_ma = FALSE;
7036 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007037 if (opt_idx >= 0)
7038 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039}
7040
7041/*
7042 * Set the global value for 'iminsert' to the local value.
7043 */
7044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007045set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 p_iminsert = curbuf->b_p_iminsert;
7048}
7049
7050/*
7051 * Set the global value for 'imsearch' to the local value.
7052 */
7053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007054set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 p_imsearch = curbuf->b_p_imsearch;
7057}
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059static int expand_option_idx = -1;
7060static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7061static int expand_option_flags = 0;
7062
7063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007064set_context_in_set_cmd(
7065 expand_T *xp,
7066 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 long_u flags = 0; // init for GCC
7071 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 char_u *p;
7073 char_u *s;
7074 int is_term_option = FALSE;
7075 int key;
7076
7077 expand_option_flags = opt_flags;
7078
7079 xp->xp_context = EXPAND_SETTINGS;
7080 if (*arg == NUL)
7081 {
7082 xp->xp_pattern = arg;
7083 return;
7084 }
7085 p = arg + STRLEN(arg) - 1;
7086 if (*p == ' ' && *(p - 1) != '\\')
7087 {
7088 xp->xp_pattern = p + 1;
7089 return;
7090 }
7091 while (p > arg)
7092 {
7093 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 if (*p == ' ' || *p == ',')
7096 {
7097 while (s > arg && *(s - 1) == '\\')
7098 --s;
7099 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 if (*p == ' ' && ((p - s) & 1) == 0)
7102 {
7103 ++p;
7104 break;
7105 }
7106 --p;
7107 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007108 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
7110 xp->xp_context = EXPAND_BOOL_SETTINGS;
7111 p += 2;
7112 }
7113 if (STRNCMP(p, "inv", 3) == 0)
7114 {
7115 xp->xp_context = EXPAND_BOOL_SETTINGS;
7116 p += 3;
7117 }
7118 xp->xp_pattern = arg = p;
7119 if (*arg == '<')
7120 {
7121 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007122 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 return;
7124 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {
7127 xp->xp_context = EXPAND_NOTHING;
7128 return;
7129 }
7130 nextchar = *++p;
7131 is_term_option = TRUE;
7132 expand_option_name[2] = KEY2TERMCAP0(key);
7133 expand_option_name[3] = KEY2TERMCAP1(key);
7134 }
7135 else
7136 {
7137 if (p[0] == 't' && p[1] == '_')
7138 {
7139 p += 2;
7140 if (*p != NUL)
7141 ++p;
7142 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007143 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 nextchar = *++p;
7145 is_term_option = TRUE;
7146 expand_option_name[2] = p[-2];
7147 expand_option_name[3] = p[-1];
7148 }
7149 else
7150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007151 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7153 p++;
7154 if (*p == NUL)
7155 return;
7156 nextchar = *p;
7157 *p = NUL;
7158 opt_idx = findoption(arg);
7159 *p = nextchar;
7160 if (opt_idx == -1 || options[opt_idx].var == NULL)
7161 {
7162 xp->xp_context = EXPAND_NOTHING;
7163 return;
7164 }
7165 flags = options[opt_idx].flags;
7166 if (flags & P_BOOL)
7167 {
7168 xp->xp_context = EXPAND_NOTHING;
7169 return;
7170 }
7171 }
7172 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007173 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7175 {
7176 ++p;
7177 nextchar = '=';
7178 }
7179 if ((nextchar != '=' && nextchar != ':')
7180 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7181 {
7182 xp->xp_context = EXPAND_UNSUCCESSFUL;
7183 return;
7184 }
7185 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7186 {
7187 xp->xp_context = EXPAND_OLD_SETTING;
7188 if (is_term_option)
7189 expand_option_idx = -1;
7190 else
7191 expand_option_idx = opt_idx;
7192 xp->xp_pattern = p + 1;
7193 return;
7194 }
7195 xp->xp_context = EXPAND_NOTHING;
7196 if (is_term_option || (flags & P_NUM))
7197 return;
7198
7199 xp->xp_pattern = p + 1;
7200
7201 if (flags & P_EXPAND)
7202 {
7203 p = options[opt_idx].var;
7204 if (p == (char_u *)&p_bdir
7205 || p == (char_u *)&p_dir
7206 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007207 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210#ifdef FEAT_SESSION
7211 || p == (char_u *)&p_vdir
7212#endif
7213 )
7214 {
7215 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007216 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 xp->xp_backslash = XP_BS_THREE;
7218 else
7219 xp->xp_backslash = XP_BS_ONE;
7220 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007221 else if (p == (char_u *)&p_ft)
7222 {
7223 xp->xp_context = EXPAND_FILETYPE;
7224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 else
7226 {
7227 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007228 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 if (p == (char_u *)&p_tags)
7230 xp->xp_backslash = XP_BS_THREE;
7231 else
7232 xp->xp_backslash = XP_BS_ONE;
7233 }
7234 }
7235
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007236 // For an option that is a list of file names, find the start of the
7237 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007240 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 if (*p == ' ' || *p == ',')
7242 {
7243 s = p;
7244 while (s > xp->xp_pattern && *(s - 1) == '\\')
7245 --s;
7246 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7247 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7248 {
7249 xp->xp_pattern = p + 1;
7250 break;
7251 }
7252 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007253
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007254#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007255 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007256 if (options[opt_idx].var == (char_u *)&p_sps
7257 && STRNCMP(p, "file:", 5) == 0)
7258 {
7259 xp->xp_pattern = p + 5;
7260 break;
7261 }
7262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264}
7265
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007266/*
7267 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7268 *
7269 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7270 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7271 *
7272 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7273 * regular expression 'regmatch', then stores the match in matches[idx] and
7274 * returns TRUE.
7275 *
7276 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7277 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7278 *
7279 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7280 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7281 */
7282 static int
7283match_str(
7284 char_u *str,
7285 regmatch_T *regmatch,
7286 char_u **matches,
7287 int idx,
7288 int test_only,
7289 int fuzzy,
7290 char_u *fuzzystr,
7291 fuzmatch_str_T *fuzmatch)
7292{
7293 if (!fuzzy)
7294 {
7295 if (vim_regexec(regmatch, str, (colnr_T)0))
7296 {
7297 if (!test_only)
7298 matches[idx] = vim_strsave(str);
7299 return TRUE;
7300 }
7301 }
7302 else
7303 {
7304 int score;
7305
7306 score = fuzzy_match_str(str, fuzzystr);
7307 if (score != 0)
7308 {
7309 if (!test_only)
7310 {
7311 fuzmatch[idx].idx = idx;
7312 fuzmatch[idx].str = vim_strsave(str);
7313 fuzmatch[idx].score = score;
7314 }
7315
7316 return TRUE;
7317 }
7318 }
7319
7320 return FALSE;
7321}
7322
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007324ExpandSettings(
7325 expand_T *xp,
7326 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007327 char_u *fuzzystr,
7328 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007329 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007330 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007332 int num_normal = 0; // Nr of matching non-term-code settings
7333 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 int opt_idx;
7335 int match;
7336 int count = 0;
7337 char_u *str;
7338 int loop;
7339 int is_term_opt;
7340 char_u name_buf[MAX_KEY_NAME_LEN];
7341 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007342 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007343 int fuzzy;
7344 fuzmatch_str_T *fuzmatch = NULL;
7345
Christian Brabandtcb747892022-05-08 21:10:56 +01007346 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007348 // do this loop twice:
7349 // loop == 0: count the number of matching options
7350 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 for (loop = 0; loop <= 1; ++loop)
7352 {
7353 regmatch->rm_ic = ic;
7354 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7355 {
K.Takataeeec2542021-06-02 13:28:16 +02007356 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007357 {
7358 if (match_str((char_u *)names[match], regmatch, *matches,
7359 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
7361 if (loop == 0)
7362 num_normal++;
7363 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007364 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
7368 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7369 opt_idx++)
7370 {
7371 if (options[opt_idx].var == NULL)
7372 continue;
7373 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7374 && !(options[opt_idx].flags & P_BOOL))
7375 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007376 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 if (is_term_opt && num_normal > 0)
7378 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007379
7380 if (match_str(str, regmatch, *matches, count, (loop == 0),
7381 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
7383 if (loop == 0)
7384 {
7385 if (is_term_opt)
7386 num_term++;
7387 else
7388 num_normal++;
7389 }
7390 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007391 count++;
7392 }
7393 else if (!fuzzy && options[opt_idx].shortname != NULL
7394 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007395 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007396 {
7397 // Compare against the abbreviated option name (for regular
7398 // expression match). Fuzzy matching (previous if) already
7399 // matches against both the expanded and abbreviated names.
7400 if (loop == 0)
7401 {
7402 if (is_term_opt)
7403 num_term++;
7404 else
7405 num_normal++;
7406 }
7407 else
7408 (*matches)[count++] = vim_strsave(str);
7409 }
7410 else if (is_term_opt)
7411 {
7412 name_buf[0] = '<';
7413 name_buf[1] = 't';
7414 name_buf[2] = '_';
7415 name_buf[3] = str[2];
7416 name_buf[4] = str[3];
7417 name_buf[5] = '>';
7418 name_buf[6] = NUL;
7419
7420 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7421 fuzzy, fuzzystr, fuzmatch))
7422 {
7423 if (loop == 0)
7424 num_term++;
7425 else
7426 count++;
7427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007430
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007431 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7433 {
7434 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7435 {
7436 if (!isprint(str[0]) || !isprint(str[1]))
7437 continue;
7438
7439 name_buf[0] = 't';
7440 name_buf[1] = '_';
7441 name_buf[2] = str[0];
7442 name_buf[3] = str[1];
7443 name_buf[4] = NUL;
7444
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007445 if (match_str(name_buf, regmatch, *matches, count,
7446 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7447 {
7448 if (loop == 0)
7449 num_term++;
7450 else
7451 count++;
7452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 else
7454 {
7455 name_buf[0] = '<';
7456 name_buf[1] = 't';
7457 name_buf[2] = '_';
7458 name_buf[3] = str[0];
7459 name_buf[4] = str[1];
7460 name_buf[5] = '>';
7461 name_buf[6] = NUL;
7462
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007463 if (match_str(name_buf, regmatch, *matches, count,
7464 (loop == 0), fuzzy, fuzzystr,
7465 fuzmatch))
7466 {
7467 if (loop == 0)
7468 num_term++;
7469 else
7470 count++;
7471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 }
7473 }
7474
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007475 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007476 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7478 {
7479 name_buf[0] = '<';
7480 STRCPY(name_buf + 1, str);
7481 STRCAT(name_buf, ">");
7482
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007483 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7484 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
7486 if (loop == 0)
7487 num_term++;
7488 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007489 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
7491 }
7492 }
7493 if (loop == 0)
7494 {
7495 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007496 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007498 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 else
7500 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007501 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007503 *matches = ALLOC_MULT(char_u *, *numMatches);
7504 if (*matches == NULL)
7505 {
7506 *matches = (char_u **)"";
7507 return FAIL;
7508 }
7509 }
7510 else
7511 {
7512 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7513 if (fuzmatch == NULL)
7514 {
7515 *matches = (char_u **)"";
7516 return FAIL;
7517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
7519 }
7520 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007521
7522 if (fuzzy &&
7523 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7524 return FAIL;
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 return OK;
7527}
7528
7529 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007530ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007532 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 char_u *buf;
7534
zeertzjqb0d45ec2023-01-25 15:04:22 +00007535 *numMatches = 0;
7536 *matches = ALLOC_ONE(char_u *);
7537 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 return FAIL;
7539
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007540 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 if (expand_option_idx < 0)
7542 {
7543 var = find_termcode(expand_option_name + 2);
7544 if (var == NULL)
7545 expand_option_idx = findoption(expand_option_name);
7546 }
7547
7548 if (expand_option_idx >= 0)
7549 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007550 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 option_value2string(&options[expand_option_idx], expand_option_flags);
7552 var = NameBuff;
7553 }
7554 else if (var == NULL)
7555 var = (char_u *)"";
7556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007557 // A backslash is required before some characters. This is the reverse of
7558 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 buf = vim_strsave_escaped(var, escape_chars);
7560
7561 if (buf == NULL)
7562 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007563 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 return FAIL;
7565 }
7566
7567#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007568 // For MS-Windows et al. we don't double backslashes at the start and
7569 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007570 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 if (var[0] == '\\' && var[1] == '\\'
7572 && expand_option_idx >= 0
7573 && (options[expand_option_idx].flags & P_EXPAND)
7574 && vim_isfilec(var[2])
7575 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007576 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577#endif
7578
zeertzjqb0d45ec2023-01-25 15:04:22 +00007579 *matches[0] = buf;
7580 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 return OK;
7582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
7584/*
7585 * Get the value for the numeric or string option *opp in a nice format into
7586 * NameBuff[]. Must not be called with a hidden option!
7587 */
7588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007589option_value2string(
7590 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007591 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 char_u *varp;
7594
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007595 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
7597 if (opp->flags & P_NUM)
7598 {
7599 long wc = 0;
7600
7601 if (wc_use_keyname(varp, &wc))
7602 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7603 else if (wc != 0)
7604 STRCPY(NameBuff, transchar((int)wc));
7605 else
7606 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7607 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007608 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
7610 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007611 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 NameBuff[0] = NUL;
7613#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007614 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007615 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 STRCPY(NameBuff, "*****");
7617#endif
7618 else if (opp->flags & P_EXPAND)
7619 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007620 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 else if ((char_u **)opp->var == &p_pt)
7622 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7623 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007624 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626}
7627
7628/*
7629 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7630 * printed as a keyname.
7631 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7632 */
7633 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007634wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
7636 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7637 {
7638 *wcp = *(long *)varp;
7639 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7640 return TRUE;
7641 }
7642 return FALSE;
7643}
7644
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 * Return TRUE if "x" is present in 'shortmess' option, or
7647 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7648 */
7649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007650shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007652 return p_shm != NULL &&
7653 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 || (vim_strchr(p_shm, 'a') != NULL
7655 && vim_strchr((char_u *)SHM_A, x) != NULL));
7656}
7657
7658/*
7659 * paste_option_changed() - Called after p_paste was set or reset.
7660 */
7661 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007662paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664 static int old_p_paste = FALSE;
7665 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007666 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668#ifdef FEAT_RIGHTLEFT
7669 static int save_ri = 0;
7670 static int save_hkmap = 0;
7671#endif
7672 buf_T *buf;
7673
7674 if (p_paste)
7675 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007676 // Paste switched from off to on.
7677 // Save the current values, so they can be restored later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 if (!old_p_paste)
7679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007680 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007681 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
7683 buf->b_p_tw_nopaste = buf->b_p_tw;
7684 buf->b_p_wm_nopaste = buf->b_p_wm;
7685 buf->b_p_sts_nopaste = buf->b_p_sts;
7686 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007687 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007688#ifdef FEAT_VARTABS
7689 if (buf->b_p_vsts_nopaste)
7690 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007691 buf->b_p_vsts_nopaste =
7692 buf->b_p_vsts && buf->b_p_vsts != empty_option
7693 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
7696
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007697 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007699 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701#ifdef FEAT_RIGHTLEFT
7702 save_ri = p_ri;
7703 save_hkmap = p_hkmap;
7704#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007705 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007706 p_ai_nopaste = p_ai;
7707 p_et_nopaste = p_et;
7708 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 p_tw_nopaste = p_tw;
7710 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007711#ifdef FEAT_VARTABS
7712 if (p_vsts_nopaste)
7713 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007714 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7715 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
7718
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007719 // Always set the option values, also when 'paste' is set when it is
7720 // already on. Set options for each buffer.
Bram Moolenaar29323592016-07-24 22:04:11 +02007721 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007723 buf->b_p_tw = 0; // textwidth is 0
7724 buf->b_p_wm = 0; // wrapmargin is 0
7725 buf->b_p_sts = 0; // softtabstop is 0
7726 buf->b_p_ai = 0; // no auto-indent
7727 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007728#ifdef FEAT_VARTABS
7729 if (buf->b_p_vsts)
7730 free_string_option(buf->b_p_vsts);
7731 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007732 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 }
7735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007736 // set global options
7737 p_sm = 0; // no showmatch
7738 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007740 status_redraw_all(); // redraw to remove the ruler
7741 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007743 p_ri = 0; // no reverse insert
7744 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007746 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 p_tw = 0;
7748 p_wm = 0;
7749 p_sts = 0;
7750 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007751#ifdef FEAT_VARTABS
7752 if (p_vsts)
7753 free_string_option(p_vsts);
7754 p_vsts = empty_option;
7755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007758 // Paste switched from on to off: Restore saved values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 else if (old_p_paste)
7760 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007761 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007762 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {
7764 buf->b_p_tw = buf->b_p_tw_nopaste;
7765 buf->b_p_wm = buf->b_p_wm_nopaste;
7766 buf->b_p_sts = buf->b_p_sts_nopaste;
7767 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007768 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007769#ifdef FEAT_VARTABS
7770 if (buf->b_p_vsts)
7771 free_string_option(buf->b_p_vsts);
7772 buf->b_p_vsts = buf->b_p_vsts_nopaste
7773 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007774 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007775 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007776 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007777 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007778 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007782 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007784 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007786 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#ifdef FEAT_RIGHTLEFT
7789 p_ri = save_ri;
7790 p_hkmap = save_hkmap;
7791#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007792 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007793 p_ai = p_ai_nopaste;
7794 p_et = p_et_nopaste;
7795 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 p_tw = p_tw_nopaste;
7797 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007798#ifdef FEAT_VARTABS
7799 if (p_vsts)
7800 free_string_option(p_vsts);
7801 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804
7805 old_p_paste = p_paste;
7806}
7807
7808/*
7809 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7810 *
7811 * Reset 'compatible' and set the values for options that didn't get set yet
7812 * to the Vim defaults.
7813 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007814 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 */
7816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007817vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007819 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007820 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007821 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
7823 if (!option_was_set((char_u *)"cp"))
7824 {
7825 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007826 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7828 set_option_default(opt_idx, OPT_FREE, FALSE);
7829 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007830 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007832
7833 if (fname != NULL)
7834 {
7835 p = vim_getenv(envname, &dofree);
7836 if (p == NULL)
7837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007838 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007839 p = FullName_save(fname, FALSE);
7840 if (p != NULL)
7841 {
7842 vim_setenv(envname, p);
7843 vim_free(p);
7844 }
7845 }
7846 else if (dofree)
7847 vim_free(p);
7848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849}
7850
7851/*
7852 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7853 */
7854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007855change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007857 int opt_idx;
7858
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (p_cp != on)
7860 {
7861 p_cp = on;
7862 compatible_set();
7863 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007864 opt_idx = findoption((char_u *)"cp");
7865 if (opt_idx >= 0)
7866 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867}
7868
7869/*
7870 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007871 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 */
7873 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007874option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 int idx;
7877
7878 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007879 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 return FALSE;
7881 if (options[idx].flags & P_WAS_SET)
7882 return TRUE;
7883 return FALSE;
7884}
7885
7886/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007887 * Reset the flag indicating option "name" was set.
7888 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007890reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007891{
7892 int idx = findoption(name);
7893
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007894 if (idx < 0)
7895 return FAIL;
7896
7897 options[idx].flags &= ~P_WAS_SET;
7898 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007899}
7900
7901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 * compatible_set() - Called when 'compatible' has been set or unset.
7903 *
7904 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7905 * flag) to a Vi compatible value.
7906 * When 'compatible' is unset: Set all options that have a different default
7907 * for Vim (without the P_VI_DEF flag) to that default.
7908 */
7909 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007910compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
7912 int opt_idx;
7913
Bram Moolenaardac13472019-09-16 21:06:21 +02007914 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7916 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7917 set_option_default(opt_idx, OPT_FREE, p_cp);
7918 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007919 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920}
7921
Bram Moolenaardac13472019-09-16 21:06:21 +02007922#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00007925 * Called when the 'breakat' option changes value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00007927 char *
7928did_set_breakat(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007930 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 int i;
7932
7933 for (i = 0; i < 256; i++)
7934 breakat_flags[i] = FALSE;
7935
7936 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007937 for (p = p_breakat; *p; p++)
7938 breakat_flags[*p] = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00007939
7940 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942#endif
7943
7944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 * Check if backspacing over something is allowed.
7946 */
7947 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007948can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007949 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007951#ifdef FEAT_JOB_CHANNEL
7952 if (what == BS_START && bt_prompt(curbuf))
7953 return FALSE;
7954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 switch (*p_bs)
7956 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007957 case '3': return TRUE;
7958 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 case '1': return (what != BS_START);
7960 case '0': return FALSE;
7961 }
7962 return vim_strchr(p_bs, what) != NULL;
7963}
7964
7965/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007966 * Return the effective 'scrolloff' value for the current window, using the
7967 * global value when appropriate.
7968 */
7969 long
7970get_scrolloff_value(void)
7971{
7972 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7973}
7974
7975/*
7976 * Return the effective 'sidescrolloff' value for the current window, using the
7977 * global value when appropriate.
7978 */
7979 long
7980get_sidescrolloff_value(void)
7981{
7982 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7983}
7984
7985/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007986 * Get the local or global value of 'backupcopy'.
7987 */
7988 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007989get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007990{
7991 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7992}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007993
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007994#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007995/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007996 * Get the local or global value of 'formatlistpat'.
7997 */
7998 char_u *
7999get_flp_value(buf_T *buf)
8000{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008001 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8002 return p_flp;
8003 return buf->b_p_flp;
8004}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008005#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008006
8007/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008008 * Get the local or global value of the 'virtualedit' flags.
8009 */
8010 unsigned int
8011get_ve_flags(void)
8012{
Gary Johnson51ad8502021-08-03 18:33:08 +02008013 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8014 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008015}
8016
Bram Moolenaaree857022019-11-09 23:26:40 +01008017#if defined(FEAT_LINEBREAK) || defined(PROTO)
8018/*
8019 * Get the local or global value of 'showbreak'.
8020 */
8021 char_u *
8022get_showbreak_value(win_T *win)
8023{
8024 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8025 return p_sbr;
8026 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8027 return empty_option;
8028 return win->w_p_sbr;
8029}
8030#endif
8031
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008032#if defined(FEAT_EVAL) || defined(PROTO)
8033/*
8034 * Get window or buffer local options.
8035 */
8036 dict_T *
8037get_winbuf_options(int bufopt)
8038{
8039 dict_T *d;
8040 int opt_idx;
8041
8042 d = dict_alloc();
8043 if (d == NULL)
8044 return NULL;
8045
Bram Moolenaardac13472019-09-16 21:06:21 +02008046 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008047 {
8048 struct vimoption *opt = &options[opt_idx];
8049
8050 if ((bufopt && (opt->indir & PV_BUF))
8051 || (!bufopt && (opt->indir & PV_WIN)))
8052 {
8053 char_u *varp = get_varp(opt);
8054
8055 if (varp != NULL)
8056 {
8057 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008058 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008059 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008060 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008061 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008062 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008063 }
8064 }
8065 }
8066
8067 return d;
8068}
8069#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008070
Bram Moolenaardac13472019-09-16 21:06:21 +02008071#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008072/*
8073 * This is called when 'culopt' is changed
8074 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008075 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008076fill_culopt_flags(char_u *val, win_T *wp)
8077{
8078 char_u *p;
8079 char_u culopt_flags_new = 0;
8080
8081 if (val == NULL)
8082 p = wp->w_p_culopt;
8083 else
8084 p = val;
8085 while (*p != NUL)
8086 {
8087 if (STRNCMP(p, "line", 4) == 0)
8088 {
8089 p += 4;
8090 culopt_flags_new |= CULOPT_LINE;
8091 }
8092 else if (STRNCMP(p, "both", 4) == 0)
8093 {
8094 p += 4;
8095 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8096 }
8097 else if (STRNCMP(p, "number", 6) == 0)
8098 {
8099 p += 6;
8100 culopt_flags_new |= CULOPT_NBR;
8101 }
8102 else if (STRNCMP(p, "screenline", 10) == 0)
8103 {
8104 p += 10;
8105 culopt_flags_new |= CULOPT_SCRLINE;
8106 }
8107
8108 if (*p != ',' && *p != NUL)
8109 return FAIL;
8110 if (*p == ',')
8111 ++p;
8112 }
8113
8114 // Can't have both "line" and "screenline".
8115 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8116 return FAIL;
8117 wp->w_p_culopt_flags = culopt_flags_new;
8118
8119 return OK;
8120}
8121#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008122
8123/*
8124 * Get the value of 'magic' adjusted for Vim9 script.
8125 */
8126 int
8127magic_isset(void)
8128{
8129 switch (magic_overruled)
8130 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008131 case OPTION_MAGIC_ON: return TRUE;
8132 case OPTION_MAGIC_OFF: return FALSE;
8133 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008134 }
8135#ifdef FEAT_EVAL
8136 if (in_vim9script())
8137 return TRUE;
8138#endif
8139 return p_magic;
8140}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008141
8142/*
8143 * Set the callback function value for an option that accepts a function name,
8144 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8145 * Returns OK if the option is successfully set to a function, otherwise
8146 * returns FAIL.
8147 */
8148 int
8149option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8150{
8151#ifdef FEAT_EVAL
8152 typval_T *tv;
8153 callback_T cb;
8154
8155 if (optval == NULL || *optval == NUL)
8156 {
8157 free_callback(optcb);
8158 return OK;
8159 }
8160
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008161 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008162 || (STRNCMP(optval, "function(", 9) == 0)
8163 || (STRNCMP(optval, "funcref(", 8) == 0))
8164 // Lambda expression or a funcref
8165 tv = eval_expr(optval, NULL);
8166 else
8167 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008168 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008169 if (tv == NULL)
8170 return FAIL;
8171
8172 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008173 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008174 {
8175 free_tv(tv);
8176 return FAIL;
8177 }
8178
8179 free_callback(optcb);
8180 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008181 if (cb.cb_free_name)
8182 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008183 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008184
8185 // when using Vim9 style "import.funcname" it needs to be expanded to
8186 // "import#funcname".
8187 expand_autload_callback(optcb);
8188
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008189 return OK;
8190#else
8191 return FAIL;
8192#endif
8193}