blob: e5e78d823f646be03ea7bd06d3936b20cab4f581 [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 compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000072 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000074 static void
75set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000079 // Find default value for 'shell' option.
80 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000081 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010082#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000083 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010084 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +000086 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020087#if defined(MSWIN)
88 {
89 // For MS-Windows put the path in quotes instead of escaping spaces.
90 char_u *cmd;
91 size_t len;
92
93 if (vim_strchr(p, ' ') != NULL)
94 {
95 len = STRLEN(p) + 3; // two quotes and a trailing NUL
96 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +020097 if (cmd != NULL)
98 {
99 vim_snprintf((char *)cmd, len, "\"%s\"", p);
100 set_string_default("sh", cmd);
101 vim_free(cmd);
102 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200103 }
104 else
105 set_string_default("sh", p);
106 }
107#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100108 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200109#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000110}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000112/*
113 * Set the default for 'backupskip' to include environment variables for
114 * temp files.
115 */
116 static void
117set_init_default_backupskip(void)
118{
119 int opt_idx;
120 long_u n;
121 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100122#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000123 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100124#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000125 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100126#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000127 int len;
128 garray_T ga;
Bram Moolenaar14338022023-03-15 22:05:44 +0000129 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200130
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000131 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000133 ga_init2(&ga, 1, 100);
134 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
135 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000136 int mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100137#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000138 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000144 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100145#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 p = vim_getenv((char_u *)names[n], &mustfree);
147 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000149 // First time count the NUL, otherwise count the ','.
150 len = (int)STRLEN(p) + 3;
151 item = alloc(len);
Bram Moolenaar14338022023-03-15 22:05:44 +0000152 if (item != NULL)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000153 {
Bram Moolenaar14338022023-03-15 22:05:44 +0000154 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);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000169 if (mustfree)
170 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000172 if (ga.ga_data != NULL)
173 {
174 set_string_default("bsk", ga.ga_data);
175 vim_free(ga.ga_data);
176 }
177}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000179/*
180 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
181 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
182 */
183 static void
184set_init_default_maxmemtot(void)
185{
186 int opt_idx;
187 long_u n;
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000190 if (opt_idx < 0)
191 return;
192
193#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
194 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
ichizok02560422022-04-05 14:18:44 +0100197#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000198 // Use amount of memory available at this moment.
199 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100200#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000201 // Use amount of memory available to Vim.
202 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100203#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000204 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000206 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
207 opt_idx = findoption((char_u *)"maxmem");
208 if (opt_idx >= 0)
209 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000210#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000211 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
212 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000214 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000217}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000219/*
220 * Initialize the 'cdpath' option to a default value.
221 */
222 static void
223set_init_default_cdpath(void)
224{
225 int opt_idx;
226 char_u *cdpath;
227 char_u *buf;
228 int i;
229 int j;
230 int mustfree = FALSE;
231
232 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
233 if (cdpath == NULL)
234 return;
235
236 buf = alloc((STRLEN(cdpath) << 1) + 2);
237 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000239 buf[0] = ','; // start with ",", current dir first
240 j = 1;
241 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000243 if (vim_ispathlistsep(cdpath[i]))
244 buf[j++] = ',';
245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000247 if (cdpath[i] == ' ' || cdpath[i] == ',')
248 buf[j++] = '\\';
249 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 }
251 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000252 buf[j] = NUL;
253 opt_idx = findoption((char_u *)"cdpath");
254 if (opt_idx >= 0)
255 {
256 options[opt_idx].def_val[VI_DEFAULT] = buf;
257 options[opt_idx].flags |= P_DEF_ALLOCED;
258 }
259 else
260 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000262 if (mustfree)
263 vim_free(cdpath);
264}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000266/*
267 * Initialize the 'printencoding' option to a default value.
268 */
269 static void
270set_init_default_printencoding(void)
271{
ichizok02560422022-04-05 14:18:44 +0100272#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000273 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100274 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100276# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100278# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000279 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100280# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100282# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000283 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000285 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000287}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288
289#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000290/*
291 * Initialize the 'printexpr' option to a default value.
292 */
293 static void
294set_init_default_printexpr(void)
295{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100296 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100298# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000299 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100300# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
302
ichizok02560422022-04-05 14:18:44 +0100303# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# endif
306 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000307}
308#endif
309
310#ifdef UNIX
311/*
312 * Force restricted-mode on for "nologin" or "false" $SHELL
313 */
314 static void
315set_init_restricted_mode(void)
316{
317 char_u *p;
318
319 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000320 if (p == NULL)
321 return;
322 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
323 restricted = TRUE;
324 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000325}
326#endif
327
328#ifdef CLEAN_RUNTIMEPATH
329/*
330 * When Vim is started with the "--clean" argument, set the default value
331 * for the 'runtimepath' and 'packpath' options.
332 */
333 static void
334set_init_clean_rtp(void)
335{
336 int opt_idx;
337
338 opt_idx = findoption((char_u *)"runtimepath");
339 if (opt_idx >= 0)
340 {
341 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
342 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
343 }
344 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000345 if (opt_idx < 0)
346 return;
347 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
348 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000349}
350#endif
351
352/*
353 * Expand environment variables and things like "~" for the defaults.
354 * If option_expand() returns non-NULL the variable is expanded. This can
355 * only happen for non-indirect options.
356 * Also set the default to the expanded value, so ":set" does not list
357 * them.
358 * Don't set the P_ALLOCED flag, because we don't want to free the
359 * default.
360 */
361 static void
362set_init_expand_env(void)
363{
364 int opt_idx;
365 char_u *p;
366
367 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
368 {
369 if ((options[opt_idx].flags & P_GETTEXT)
370 && options[opt_idx].var != NULL)
371 p = (char_u *)_(*(char **)options[opt_idx].var);
372 else
373 p = option_expand(opt_idx, NULL);
374 if (p != NULL && (p = vim_strsave(p)) != NULL)
375 {
376 *(char_u **)options[opt_idx].var = p;
377 // VIMEXP
378 // Defaults for all expanded options are currently the same for Vi
379 // and Vim. When this changes, add some code here! Also need to
380 // split P_DEF_ALLOCED in two.
381 if (options[opt_idx].flags & P_DEF_ALLOCED)
382 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
383 options[opt_idx].def_val[VI_DEFAULT] = p;
384 options[opt_idx].flags |= P_DEF_ALLOCED;
385 }
386 }
387}
388
389/*
390 * Initialize the 'LANG' environment variable to a default value.
391 */
392 static void
393set_init_lang_env(void)
394{
395#if defined(MSWIN) && defined(FEAT_GETTEXT)
396 // If $LANG isn't set, try to get a good value for it. This makes the
397 // right language be used automatically. Don't do this for English.
398 if (mch_getenv((char_u *)"LANG") == NULL)
399 {
400 char buf[20];
401 long_u n;
402
403 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
404 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
405 // only the first two.
406 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
407 (LPTSTR)buf, 20);
408 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
409 {
410 // There are a few exceptions (probably more)
411 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
412 STRCPY(buf, "zh_TW");
413 else if (STRNICMP(buf, "chs", 3) == 0
414 || STRNICMP(buf, "zhc", 3) == 0)
415 STRCPY(buf, "zh_CN");
416 else if (STRNICMP(buf, "jp", 2) == 0)
417 STRCPY(buf, "ja");
418 else
419 buf[2] = NUL; // truncate to two-letter code
420 vim_setenv((char_u *)"LANG", (char_u *)buf);
421 }
422 }
423#elif defined(MACOS_CONVERT)
424 // Moved to os_mac_conv.c to avoid dependency problems.
425 mac_lang_init();
426#endif
427}
428
429/*
430 * Initialize the 'encoding' option to a default value.
431 */
432 static void
433set_init_default_encoding(void)
434{
435 char_u *p;
436 int opt_idx;
437
438# ifdef MSWIN
439 // MS-Windows has builtin support for conversion to and from Unicode, using
440 // "utf-8" for 'encoding' should work best for most users.
441 p = vim_strsave((char_u *)ENC_DFLT);
442# else
443 // enc_locale() will try to find the encoding of the current locale.
444 // This works best for properly configured systems, old and new.
445 p = enc_locale();
446# endif
447 if (p == NULL)
448 return;
449
450 // Try setting 'encoding' and check if the value is valid.
451 // If not, go back to the default encoding.
452 char_u *save_enc = p_enc;
453 p_enc = p;
454 if (STRCMP(p_enc, "gb18030") == 0)
455 {
456 // We don't support "gb18030", but "cp936" is a good substitute
457 // for practical purposes, thus use that. It's not an alias to
458 // still support conversion between gb18030 and utf-8.
459 p_enc = vim_strsave((char_u *)"cp936");
460 vim_free(p);
461 }
462 if (mb_init() == NULL)
463 {
464 opt_idx = findoption((char_u *)"encoding");
465 if (opt_idx >= 0)
466 {
467 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
468 options[opt_idx].flags |= P_DEF_ALLOCED;
469 }
470
471#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
472 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
473 {
474 // Adjust the default for 'isprint' and 'iskeyword' to match
475 // latin1. Also set the defaults for when 'nocompatible' is
476 // set.
477 set_string_option_direct((char_u *)"isp", -1,
478 ISP_LATIN1, OPT_FREE, SID_NONE);
479 set_string_option_direct((char_u *)"isk", -1,
480 ISK_LATIN1, OPT_FREE, SID_NONE);
481 opt_idx = findoption((char_u *)"isp");
482 if (opt_idx >= 0)
483 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
484 opt_idx = findoption((char_u *)"isk");
485 if (opt_idx >= 0)
486 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
487 (void)init_chartab();
488 }
489#endif
490
491#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
492 // Win32 console: When GetACP() returns a different value from
493 // GetConsoleCP() set 'termencoding'.
494 if (
495# ifdef VIMDLL
496 (!gui.in_use && !gui.starting) &&
497# endif
498 GetACP() != GetConsoleCP())
499 {
500 char buf[50];
501
502 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
503 // Use an alternative value.
504 if (GetConsoleCP() == 0)
505 sprintf(buf, "cp%ld", (long)GetACP());
506 else
507 sprintf(buf, "cp%ld", (long)GetConsoleCP());
508 p_tenc = vim_strsave((char_u *)buf);
509 if (p_tenc != NULL)
510 {
511 opt_idx = findoption((char_u *)"termencoding");
512 if (opt_idx >= 0)
513 {
514 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
515 options[opt_idx].flags |= P_DEF_ALLOCED;
516 }
517 convert_setup(&input_conv, p_tenc, p_enc);
518 convert_setup(&output_conv, p_enc, p_tenc);
519 }
520 else
521 p_tenc = empty_option;
522 }
523#endif
524#if defined(MSWIN)
525 // $HOME may have characters in active code page.
526 init_homedir();
527#endif
528 }
529 else
530 {
531 vim_free(p_enc);
532 p_enc = save_enc;
533 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000534}
535
536/*
537 * Initialize the options, first part.
538 *
539 * Called only once from main(), just after creating the first buffer.
540 * If "clean_arg" is TRUE Vim was started with --clean.
541 */
542 void
543set_init_1(int clean_arg)
544{
545#ifdef FEAT_LANGMAP
546 langmap_init();
547#endif
548
549 // Be Vi compatible by default
550 p_cp = TRUE;
551
552 // Use POSIX compatibility when $VIM_POSIX is set.
553 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
554 {
555 set_string_default("cpo", (char_u *)CPO_ALL);
556 set_string_default("shm", (char_u *)SHM_POSIX);
557 }
558
559 set_init_default_shell();
560 set_init_default_backupskip();
561 set_init_default_maxmemtot();
562 set_init_default_cdpath();
563 set_init_default_printencoding();
564#ifdef FEAT_POSTSCRIPT
565 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#endif
567
568 /*
569 * Set all the options (except the terminal options) to their default
570 * value. Also set the global value for local options.
571 */
572 set_options_default(0);
573
matveytadbb1bf2022-02-01 17:26:12 +0000574#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000575 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000576#endif
577
Bram Moolenaar07268702018-03-01 21:57:32 +0100578#ifdef CLEAN_RUNTIMEPATH
579 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000580 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583#ifdef FEAT_GUI
584 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100585 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586#endif
587
588 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100589 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100590 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 check_buf_options(curbuf);
592 check_win_options(curwin);
593 check_options();
594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 didset_options();
597
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000598#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100599 // Use the current chartab for the generic chartab. This is not in
600 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000601 init_spell_chartab();
602#endif
603
K.Takatace3189d2023-02-15 19:13:43 +0000604 set_init_default_encoding();
605
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000606 // Expand environment variables and things like "~" for the defaults.
607 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100612 // Detect use of mlterm.
613 // Mlterm is a terminal emulator akin to xterm that has some special
614 // abilities (bidi namely).
615 // NOTE: mlterm's author is being asked to 'set' a variable
616 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100618 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#endif
620
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200621 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000623 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100626 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 set_helplang_default(get_mess_lang());
628#endif
629}
630
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200631static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
632
633/*
634 * Set the "fileencodings" option to the default value for when 'encoding' is
635 * utf-8.
636 */
637 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000638set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200639{
640 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
641 OPT_FREE, 0);
642}
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644/*
645 * Set an option to its default value.
646 * This does not take care of side effects!
647 */
648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100649set_option_default(
650 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100651 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
652 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100654 char_u *varp; // pointer to variable for current option
655 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000657 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
659
660 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
661 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100662 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {
664 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
665 if (flags & P_STRING)
666 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200667 // 'fencs' default value depends on 'encoding'
668 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
669 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 // Use set_string_option_direct() for local options to handle
671 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200672 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200673 set_string_option_direct(NULL, opt_idx,
674 options[opt_idx].def_val[dvi], opt_flags, 0);
675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200677 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
678 free_string_option(*(char_u **)(varp));
679 *(char_u **)varp = options[opt_idx].def_val[dvi];
680 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682 }
683 else if (flags & P_NUM)
684 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000685 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 win_comp_scroll(curwin);
687 else
688 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100689 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
690
691 if ((long *)varp == &curwin->w_p_so
692 || (long *)varp == &curwin->w_p_siso)
693 // 'scrolloff' and 'sidescrolloff' local values have a
694 // different default value than the global default.
695 *(long *)varp = -1;
696 else
697 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100698 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (both)
700 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100701 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100706 // the cast to long is required for Manx C, long_i is needed for
707 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000708 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000709#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100710 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000711 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
712 *(int *)varp = FALSE;
713#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100714 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (both)
716 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
717 *(int *)varp;
718 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000721 flagsp = insecure_flag(opt_idx, opt_flags);
722 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
724
725#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200726 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728}
729
730/*
731 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200732 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100735set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100736 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000740 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
Bram Moolenaardac13472019-09-16 21:06:21 +0200742 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200743 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200744 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100745 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200746# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200747 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200748 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200749# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100750 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 set_option_default(i, opt_flags, p_cp);
752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100753 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000754 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200756 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757}
758
759/*
760 * Set the Vi-default value of a string option.
761 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100762 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100764 static void
765set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
767 char_u *p;
768 int opt_idx;
769
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100770 if (escape && vim_strchr(val, ' ') != NULL)
771 p = vim_strsave_escaped(val, (char_u *)" ");
772 else
773 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000774 if (p == NULL) // we don't want a NULL
775 return;
776
777 opt_idx = findoption((char_u *)name);
778 if (opt_idx < 0)
779 return;
780
781 if (options[opt_idx].flags & P_DEF_ALLOCED)
782 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
783 options[opt_idx].def_val[VI_DEFAULT] = p;
784 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785}
786
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100787 void
788set_string_default(char *name, char_u *val)
789{
790 set_string_default_esc(name, val, FALSE);
791}
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200794 * For an option value that contains comma separated items, find "newval" in
795 * "origval". Return NULL if not found.
796 */
797 static char_u *
798find_dup_item(char_u *origval, char_u *newval, long_u flags)
799{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200800 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200801 size_t newlen;
802 char_u *s;
803
804 if (origval == NULL)
805 return NULL;
806
807 newlen = STRLEN(newval);
808 for (s = origval; *s != NUL; ++s)
809 {
810 if ((!(flags & P_COMMA)
811 || s == origval
812 || (s[-1] == ',' && !(bs & 1)))
813 && STRNCMP(s, newval, newlen) == 0
814 && (!(flags & P_COMMA)
815 || s[newlen] == ','
816 || s[newlen] == NUL))
817 return s;
818 // Count backslashes. Only a comma with an even number of backslashes
819 // or a single backslash preceded by a comma before it is recognized as
820 // a separator.
821 if ((s > origval + 1
822 && s[-1] == '\\'
823 && s[-2] != ',')
824 || (s == origval + 1
825 && s[-1] == '\\'))
826 ++bs;
827 else
828 bs = 0;
829 }
830 return NULL;
831}
832
833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * Set the Vi-default value of a number option.
835 * Used for 'lines' and 'columns'.
836 */
837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100838set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000840 int opt_idx;
841
842 opt_idx = findoption((char_u *)name);
843 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846
Dominique Pelle748b3082022-01-08 12:41:16 +0000847#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200848/*
849 * Set all window-local and buffer-local options to the Vim default.
850 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200851 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200852 */
853 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200854set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200855{
856 win_T *save_curwin = curwin;
857 int i;
858
859 curwin = wp;
860 curbuf = curwin->w_buffer;
861 block_autocmds();
862
Bram Moolenaardac13472019-09-16 21:06:21 +0200863 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200864 {
865 struct vimoption *p = &(options[i]);
866 char_u *varp = get_varp_scope(p, OPT_LOCAL);
867
868 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200869 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200870 && !(options[i].flags & P_NODEFAULT)
871 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200872 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200873 }
874
875 unblock_autocmds();
876 curwin = save_curwin;
877 curbuf = curwin->w_buffer;
878}
Dominique Pelle748b3082022-01-08 12:41:16 +0000879#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200880
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000881#if defined(EXITFREE) || defined(PROTO)
882/*
883 * Free all options.
884 */
885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100886free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000887{
888 int i;
889
Bram Moolenaardac13472019-09-16 21:06:21 +0200890 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000891 {
892 if (options[i].indir == PV_NONE)
893 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100894 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100895 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000896 free_string_option(*(char_u **)options[i].var);
897 if (options[i].flags & P_DEF_ALLOCED)
898 free_string_option(options[i].def_val[VI_DEFAULT]);
899 }
900 else if (options[i].var != VAR_WIN
901 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200903 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000904 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000905 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000906 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000907}
908#endif
909
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911/*
912 * Initialize the options, part two: After getting Rows and Columns and
913 * setting 'term'.
914 */
915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100916set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000918 int idx;
919
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000920 // 'scroll' defaults to half the window height. The stored default is zero,
921 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000922 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000923 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000924 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 comp_col();
926
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000927 // 'window' is only for backwards compatibility with Vi.
928 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000929 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000930 p_window = Rows - 1;
931 set_number_default("window", Rows - 1);
932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100933 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100934#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000935 // If 'background' wasn't set by the user, try guessing the value,
936 // depending on the terminal name. Only need to check for terminals
937 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000938 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000939 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
940 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000942 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100943 // don't mark it as set, when starting the GUI it may be
944 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000945 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000948
949#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000950 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000951#endif
952#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000953 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000954#endif
955#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000956 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959
960/*
961 * Initialize the options, part three: After reading the .vimrc
962 */
963 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100964set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100966#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967/*
968 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
969 * This is done after other initializations, where 'shell' might have been
970 * set, but only if they have not been set before.
971 */
972 char_u *p;
973 int idx_srr;
974 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100975# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 int idx_sp;
977 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100978# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000981 if (idx_srr < 0)
982 do_srr = FALSE;
983 else
984 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100985# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000987 if (idx_sp < 0)
988 do_sp = FALSE;
989 else
990 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200992 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (p != NULL)
994 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000995 // Default for p_sp is "| tee", for p_srr is ">".
996 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if ( fnamecmp(p, "csh") == 0
998 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100999# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 || fnamecmp(p, "csh.exe") == 0
1001 || fnamecmp(p, "tcsh.exe") == 0
1002# endif
1003 )
1004 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001005# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (do_sp)
1007 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001010# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1014 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (do_srr)
1017 {
1018 p_srr = (char_u *)">&";
1019 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1020 }
1021 }
Mike Williams12795022021-06-28 20:53:58 +02001022# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001023 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1024 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001025 else if ( fnamecmp(p, "powershell") == 0
1026 || fnamecmp(p, "powershell.exe") == 0
1027 )
1028 {
1029# if defined(FEAT_QUICKFIX)
1030 if (do_sp)
1031 {
1032 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1033 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1034 }
1035# endif
1036 if (do_srr)
1037 {
1038 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1039 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1040 }
1041 }
1042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 else
Natanael Copa56318362021-05-06 18:46:35 +02001044 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if ( fnamecmp(p, "sh") == 0
1046 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001047 || fnamecmp(p, "mksh") == 0
1048 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001050 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001052 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001053 || fnamecmp(p, "ash") == 0
1054 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001055 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001056# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 || fnamecmp(p, "cmd") == 0
1058 || fnamecmp(p, "sh.exe") == 0
1059 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001060 || fnamecmp(p, "mksh.exe") == 0
1061 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001063 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 || fnamecmp(p, "bash.exe") == 0
1065 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001066 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001067 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001069 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001071# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (do_sp)
1073 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001074# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001076# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001077 if (fnamecmp(p, "pwsh") == 0)
1078 p_sp = (char_u *)">%s 2>&1";
1079 else
1080 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1083 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (do_srr)
1086 {
1087 p_srr = (char_u *)">%s 2>&1";
1088 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1089 }
1090 }
1091 vim_free(p);
1092 }
1093#endif
1094
Bram Moolenaar4f974752019-02-17 17:44:42 +01001095#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001097 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1098 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001100 * set, but only if they have not been set before.
1101 * Default values depend on shell (cmd.exe is default shell):
1102 *
1103 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001104 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001105 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001106 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001107 * "sh" like shells - "-c" "\""
1108 *
1109 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 */
Mike Williams12795022021-06-28 20:53:58 +02001111 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1112 {
1113 int idx_opt;
1114
1115 idx_opt = findoption((char_u *)"shcf");
1116 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1117 {
1118 p_shcf = (char_u*)"-Command";
1119 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1120 }
1121
1122 idx_opt = findoption((char_u *)"sxq");
1123 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1124 {
1125 p_sxq = (char_u*)"\"";
1126 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1127 }
1128 }
1129 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
1131 int idx3;
1132
1133 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001134 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
1136 p_shcf = (char_u *)"-c";
1137 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1138 }
1139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001140 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001142 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
1144 p_sxq = (char_u *)"\"";
1145 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001148 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1149 {
1150 int idx3;
1151
1152 /*
1153 * cmd.exe on Windows will strip the first and last double quote given
1154 * on the command line, e.g. most of the time things like:
1155 * cmd /c "my path/to/echo" "my args to echo"
1156 * become:
1157 * my path/to/echo" "my args to echo
1158 * when executed.
1159 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001160 * To avoid this, set shellxquote to surround the command in
1161 * parenthesis. This appears to make most commands work, without
1162 * breaking commands that worked previously, such as
1163 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001164 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001165 idx3 = findoption((char_u *)"sxq");
1166 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1167 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001168 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001169 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1170 }
1171
Bram Moolenaara64ba222012-02-12 23:23:31 +01001172 idx3 = findoption((char_u *)"shcf");
1173 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1174 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001175 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001176 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1177 }
1178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001181 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001182 {
1183 int idx_ffs = findoption((char_u *)"ffs");
1184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001185 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001186 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1187 set_fileformat(default_fileformat(), OPT_LOCAL);
1188 }
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191}
1192
1193#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1194/*
1195 * When 'helplang' is still at its default value, set it to "lang".
1196 * Only the first two characters of "lang" are used.
1197 */
1198 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001199set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
1201 int idx;
1202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001203 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 return;
1205 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001206 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1207 return;
1208
1209 if (options[idx].flags & P_ALLOCED)
1210 free_string_option(p_hlg);
1211 p_hlg = vim_strsave(lang);
1212 if (p_hlg == NULL)
1213 p_hlg = empty_option;
1214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001216 // zh_CN becomes "cn", zh_TW becomes "tw"
1217 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001218 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001219 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1220 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001221 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001222 // any C like setting, such as C.UTF-8, becomes "en"
1223 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1224 {
1225 p_hlg[0] = 'e';
1226 p_hlg[1] = 'n';
1227 }
1228 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001230 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231}
1232#endif
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234/*
1235 * 'title' and 'icon' only default to true if they have not been set or reset
1236 * in .vimrc and we can read the old value.
1237 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1238 * they can be reset. This reduces startup time when using X on a remote
1239 * machine.
1240 */
1241 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001242set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
1244 int idx1;
1245 long val;
1246
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001247 // If GUI is (going to be) used, we can always set the window title and
1248 // icon name. Saves a bit of time, because the X11 display server does
1249 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001251 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
1253#ifdef FEAT_GUI
1254 if (gui.starting || gui.in_use)
1255 val = TRUE;
1256 else
1257#endif
1258 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001259 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 p_title = val;
1261 }
1262 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001263 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001265 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001267
1268#ifdef FEAT_GUI
1269 if (gui.starting || gui.in_use)
1270 val = TRUE;
1271 else
1272#endif
1273 val = mch_can_restore_icon();
1274 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1275 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001278 void
1279ex_set(exarg_T *eap)
1280{
1281 int flags = 0;
1282
1283 if (eap->cmdidx == CMD_setlocal)
1284 flags = OPT_LOCAL;
1285 else if (eap->cmdidx == CMD_setglobal)
1286 flags = OPT_GLOBAL;
1287#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001288 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001289 ex_options(eap);
1290 else
1291#endif
1292 {
1293 if (eap->forceit)
1294 flags |= OPT_ONECOLUMN;
1295 (void)do_set(eap->arg, flags);
1296 }
1297}
1298
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001299/*
1300 * :set operator types
1301 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001302typedef enum {
1303 OP_NONE = 0,
1304 OP_ADDING, // "opt+=arg"
1305 OP_PREPENDING, // "opt^=arg"
1306 OP_REMOVING, // "opt-=arg"
1307} set_op_T;
1308
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001309typedef enum {
1310 PREFIX_NO = 0, // "no" prefix
1311 PREFIX_NONE, // no prefix
1312 PREFIX_INV, // "inv" prefix
1313} set_prefix_T;
1314
1315/*
1316 * Return the prefix type for the option name in *argp.
1317 */
1318 static set_prefix_T
1319get_option_prefix(char_u **argp)
1320{
1321 int prefix = PREFIX_NONE;
1322 char_u *arg = *argp;
1323
1324 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1325 {
1326 prefix = PREFIX_NO;
1327 arg += 2;
1328 }
1329 else if (STRNCMP(arg, "inv", 3) == 0)
1330 {
1331 prefix = PREFIX_INV;
1332 arg += 3;
1333 }
1334
1335 *argp = arg;
1336 return prefix;
1337}
1338
1339/*
1340 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1341 * and the option name length in "*lenp". For a <t_xx> option, return the key
1342 * number in "*keyp".
1343 *
1344 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1345 * otherwise returns OK.
1346 */
1347 static int
1348parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1349{
1350 int key = 0;
1351 int len;
1352 int opt_idx;
1353
1354 if (*arg == '<')
1355 {
1356 opt_idx = -1;
1357 // look out for <t_>;>
1358 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1359 len = 5;
1360 else
1361 {
1362 len = 1;
1363 while (arg[len] != NUL && arg[len] != '>')
1364 ++len;
1365 }
1366 if (arg[len] != '>')
1367 return FAIL;
1368
1369 arg[len] = NUL; // put NUL after name
1370 if (arg[1] == 't' && arg[2] == '_') // could be term code
1371 opt_idx = findoption(arg + 1);
1372 arg[len++] = '>'; // restore '>'
1373 if (opt_idx == -1)
1374 key = find_key_option(arg + 1, TRUE);
1375 }
1376 else
1377 {
1378 int nextchar; // next non-white char after option name
1379
1380 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001381 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001382 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1383 len = 4;
1384 else
1385 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1386 ++len;
1387 nextchar = arg[len];
1388 arg[len] = NUL; // put NUL after name
1389 opt_idx = findoption(arg);
1390 arg[len] = nextchar; // restore nextchar
1391 if (opt_idx == -1)
1392 key = find_key_option(arg, FALSE);
1393 }
1394
1395 *keyp = key;
1396 *lenp = len;
1397 *opt_idxp = opt_idx;
1398
1399 return OK;
1400}
1401
1402/*
1403 * Get the option operator (+=, ^=, -=).
1404 */
1405 static set_op_T
1406get_opt_op(char_u *arg)
1407{
1408 set_op_T op = OP_NONE;
1409
1410 if (*arg != NUL && *(arg + 1) == '=')
1411 {
1412 if (*arg == '+')
1413 op = OP_ADDING; // "+="
1414 else if (*arg == '^')
1415 op = OP_PREPENDING; // "^="
1416 else if (*arg == '-')
1417 op = OP_REMOVING; // "-="
1418 }
1419
1420 return op;
1421}
1422
1423/*
1424 * Validate whether the value of the option in "opt_idx" can be changed.
1425 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1426 * if it can be changed.
1427 */
1428 static int
1429validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1430{
1431 // Skip all options that are not window-local (used when showing
1432 // an already loaded buffer in a window).
1433 if ((opt_flags & OPT_WINONLY)
1434 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1435 return FAIL;
1436
1437 // Skip all options that are window-local (used for :vimgrep).
1438 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1439 && options[opt_idx].var == VAR_WIN)
1440 return FAIL;
1441
1442 // Disallow changing some options from modelines.
1443 if (opt_flags & OPT_MODELINE)
1444 {
1445 if (flags & (P_SECURE | P_NO_ML))
1446 {
1447 *errmsg = e_not_allowed_in_modeline;
1448 return FAIL;
1449 }
1450 if ((flags & P_MLE) && !p_mle)
1451 {
1452 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1453 return FAIL;
1454 }
1455#ifdef FEAT_DIFF
1456 // In diff mode some options are overruled. This avoids that
1457 // 'foldmethod' becomes "marker" instead of "diff" and that
1458 // "wrap" gets set.
1459 if (curwin->w_p_diff
1460 && opt_idx >= 0 // shut up coverity warning
1461 && (
1462# ifdef FEAT_FOLDING
1463 options[opt_idx].indir == PV_FDM ||
1464# endif
1465 options[opt_idx].indir == PV_WRAP))
1466 return FAIL;
1467#endif
1468 }
1469
1470#ifdef HAVE_SANDBOX
1471 // Disallow changing some options in the sandbox
1472 if (sandbox != 0 && (flags & P_SECURE))
1473 {
1474 *errmsg = e_not_allowed_in_sandbox;
1475 return FAIL;
1476 }
1477#endif
1478
1479 return OK;
1480}
1481
Bram Moolenaar47403942022-09-21 21:12:53 +01001482/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001483 * Get the Vim/Vi default value for a string option.
1484 */
1485 static char_u *
1486stropt_get_default_val(
1487 int opt_idx,
1488 char_u *varp,
1489 int flags,
1490 int cp_val)
1491{
1492 char_u *newval;
1493
1494 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1495 ? VI_DEFAULT : VIM_DEFAULT];
1496 if ((char_u **)varp == &p_bg)
1497 {
1498 // guess the value of 'background'
1499#ifdef FEAT_GUI
1500 if (gui.in_use)
1501 newval = gui_bg_default();
1502 else
1503#endif
1504 newval = term_bg_default();
1505 }
1506 else if ((char_u **)varp == &p_fencs && enc_utf8)
1507 newval = fencs_utf8_default;
1508
1509 // expand environment variables and ~ since the default value was
1510 // already expanded, only required when an environment variable was set
1511 // later
1512 if (newval == NULL)
1513 newval = empty_option;
1514 else
1515 {
1516 char_u *s = option_expand(opt_idx, newval);
1517 if (s == NULL)
1518 s = newval;
1519 newval = vim_strsave(s);
1520 }
1521
1522 return newval;
1523}
1524
1525/*
1526 * Convert the 'backspace' option number value to a string: for adding,
1527 * prepending and removing string.
1528 */
1529 static void
1530opt_backspace_nr2str(
1531 char_u *varp,
1532 char_u **origval_p,
1533 char_u **origval_l_p,
1534 char_u **origval_g_p,
1535 char_u **oldval_p)
1536{
1537 int i = getdigits((char_u **)varp);
1538
1539 switch (i)
1540 {
1541 case 0:
1542 *(char_u **)varp = empty_option;
1543 break;
1544 case 1:
1545 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1546 break;
1547 case 2:
1548 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1549 break;
1550 case 3:
1551 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1552 break;
1553 }
1554 vim_free(*oldval_p);
1555 if (*origval_p == *oldval_p)
1556 *origval_p = *(char_u **)varp;
1557 if (*origval_l_p == *oldval_p)
1558 *origval_l_p = *(char_u **)varp;
1559 if (*origval_g_p == *oldval_p)
1560 *origval_g_p = *(char_u **)varp;
1561 *oldval_p = *(char_u **)varp;
1562}
1563
1564/*
1565 * Convert the 'whichwrap' option number value to a string, for backwards
1566 * compatibility with Vim 3.0.
1567 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1568 */
1569 static char_u *
1570opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1571{
1572 *whichwrap = NUL;
1573 int i = getdigits(argp);
1574 if (i & 1)
1575 STRCAT(whichwrap, "b,");
1576 if (i & 2)
1577 STRCAT(whichwrap, "s,");
1578 if (i & 4)
1579 STRCAT(whichwrap, "h,l,");
1580 if (i & 8)
1581 STRCAT(whichwrap, "<,>,");
1582 if (i & 16)
1583 STRCAT(whichwrap, "[,],");
1584 if (*whichwrap != NUL) // remove trailing ,
1585 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1586
1587 return whichwrap;
1588}
1589
1590/*
1591 * Copy the new string value into allocated memory for the option.
1592 * Can't use set_string_option_direct(), because we need to remove the
1593 * backslashes.
1594 */
1595 static char_u *
1596stropt_copy_value(
1597 char_u *origval,
1598 char_u **argp,
1599 set_op_T op,
1600 int flags UNUSED)
1601{
1602 char_u *arg = *argp;
1603 unsigned newlen;
1604 char_u *newval;
1605 char_u *s = NULL;
1606
1607 // get a bit too much
1608 newlen = (unsigned)STRLEN(arg) + 1;
1609 if (op != OP_NONE)
1610 newlen += (unsigned)STRLEN(origval) + 1;
1611 newval = alloc(newlen);
1612 if (newval == NULL) // out of mem, don't change
1613 return NULL;
1614 s = newval;
1615
1616 // Copy the string, skip over escaped chars.
1617 // For MS-DOS and WIN32 backslashes before normal file name characters
1618 // are not removed, and keep backslash at start, for "\\machine\path",
1619 // but do remove it for "\\\\machine\\path".
1620 // The reverse is found in ExpandOldSetting().
1621 while (*arg != NUL && !VIM_ISWHITE(*arg))
1622 {
1623 int i;
1624
1625 if (*arg == '\\' && arg[1] != NUL
1626#ifdef BACKSLASH_IN_FILENAME
1627 && !((flags & P_EXPAND)
1628 && vim_isfilec(arg[1])
1629 && !VIM_ISWHITE(arg[1])
1630 && (arg[1] != '\\'
1631 || (s == newval && arg[2] != '\\')))
1632#endif
1633 )
1634 ++arg; // remove backslash
1635 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1636 {
1637 // copy multibyte char
1638 mch_memmove(s, arg, (size_t)i);
1639 arg += i;
1640 s += i;
1641 }
1642 else
1643 *s++ = *arg++;
1644 }
1645 *s = NUL;
1646
1647 *argp = arg;
1648 return newval;
1649}
1650
1651/*
1652 * Expand environment variables and ~ in string option value 'newval'.
1653 */
1654 static char_u *
1655stropt_expand_envvar(
1656 int opt_idx,
1657 char_u *origval,
1658 char_u *newval,
1659 set_op_T op)
1660{
1661 char_u *s = option_expand(opt_idx, newval);
1662 if (s == NULL)
1663 return newval;
1664
1665 vim_free(newval);
1666 unsigned newlen = (unsigned)STRLEN(s) + 1;
1667 if (op != OP_NONE)
1668 newlen += (unsigned)STRLEN(origval) + 1;
1669
1670 newval = alloc(newlen);
1671 if (newval == NULL)
1672 return NULL;
1673
1674 STRCPY(newval, s);
1675
1676 return newval;
1677}
1678
1679/*
1680 * Concatenate the original and new values of a string option, adding a "," if
1681 * needed.
1682 */
1683 static void
1684stropt_concat_with_comma(
1685 char_u *origval,
1686 char_u *newval,
1687 set_op_T op,
1688 int flags)
1689{
1690 int len = 0;
1691
1692 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1693 if (op == OP_ADDING)
1694 {
1695 len = (int)STRLEN(origval);
1696 // strip a trailing comma, would get 2
1697 if (comma && len > 1
1698 && (flags & P_ONECOMMA) == P_ONECOMMA
1699 && origval[len - 1] == ','
1700 && origval[len - 2] != '\\')
1701 len--;
1702 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1703 mch_memmove(newval, origval, (size_t)len);
1704 }
1705 else
1706 {
1707 len = (int)STRLEN(newval);
1708 STRMOVE(newval + len + comma, origval);
1709 }
1710 if (comma)
1711 newval[len] = ',';
1712}
1713
1714/*
1715 * Remove a value from a string option. Copy string option value in "origval"
1716 * to "newval" and then remove the string "strval" of length "len".
1717 */
1718 static void
1719stropt_remove_val(
1720 char_u *origval,
1721 char_u *newval,
1722 int flags,
1723 char_u *strval,
1724 int len)
1725{
1726 // Remove newval[] from origval[]. (Note: "len" has been set above
1727 // and is used here).
1728 STRCPY(newval, origval);
1729 if (*strval)
1730 {
1731 // may need to remove a comma
1732 if (flags & P_COMMA)
1733 {
1734 if (strval == origval)
1735 {
1736 // include comma after string
1737 if (strval[len] == ',')
1738 ++len;
1739 }
1740 else
1741 {
1742 // include comma before string
1743 --strval;
1744 ++len;
1745 }
1746 }
1747 STRMOVE(newval + (strval - origval), strval + len);
1748 }
1749}
1750
1751/*
1752 * Remove flags that appear twice in the string option value 'newval'.
1753 */
1754 static void
1755stropt_remove_dupflags(char_u *newval, int flags)
1756{
1757 char_u *s = newval;
1758
1759 // Remove flags that appear twice.
1760 while (*s)
1761 {
1762 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1763 if (flags & P_ONECOMMA)
1764 {
1765 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1766 {
1767 // Remove the duplicated value and the next comma.
1768 STRMOVE(s, s + 2);
1769 continue;
1770 }
1771 }
1772 else
1773 {
1774 if ((!(flags & P_COMMA) || *s != ',')
1775 && vim_strchr(s + 1, *s) != NULL)
1776 {
1777 STRMOVE(s, s + 1);
1778 continue;
1779 }
1780 }
1781 ++s;
1782 }
1783}
1784
1785/*
1786 * Get the string value specified for a ":set" command. The following set
1787 * options are supported:
1788 * set {opt}&
1789 * set {opt}<
1790 * set {opt}={val}
1791 * set {opt}:{val}
1792 */
1793 static char_u *
1794stropt_get_newval(
1795 int nextchar,
1796 int opt_idx,
1797 char_u **argp,
1798 char_u *varp,
1799 char_u **origval_arg,
1800 char_u **origval_l_arg,
1801 char_u **origval_g_arg,
1802 char_u **oldval_arg,
1803 set_op_T *op_arg,
1804 int flags,
1805 int cp_val)
1806{
1807 char_u *arg = *argp;
1808 char_u *origval = *origval_arg;
1809 char_u *origval_l = *origval_l_arg;
1810 char_u *origval_g = *origval_g_arg;
1811 char_u *oldval = *oldval_arg;
1812 set_op_T op = *op_arg;
1813 char_u *save_arg = NULL;
1814 char_u *newval;
1815 char_u *s = NULL;
1816 char_u whichwrap[80];
1817
1818 if (nextchar == '&') // set to default val
1819 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1820 else if (nextchar == '<') // set to global val
1821 newval = vim_strsave(*(char_u **)get_varp_scope(
1822 &(options[opt_idx]), OPT_GLOBAL));
1823 else
1824 {
1825 ++arg; // jump to after the '=' or ':'
1826
1827 // Set 'keywordprg' to ":help" if an empty
1828 // value was passed to :set by the user.
1829 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1830 {
1831 save_arg = arg;
1832 arg = (char_u *)":help";
1833 }
1834 // Convert 'backspace' number to string
1835 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1836 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1837 &oldval);
1838 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1839 {
1840 // Convert 'whichwrap' number to string, for backwards
1841 // compatibility with Vim 3.0.
1842 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1843 save_arg = arg;
1844 arg = t;
1845 }
1846 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1847 // version 3.0
1848 else if (*arg == '>' && (varp == (char_u *)&p_dir
1849 || varp == (char_u *)&p_bdir))
1850 ++arg;
1851
1852 // Copy the new string into allocated memory.
1853 newval = stropt_copy_value(origval, &arg, op, flags);
1854 if (newval == NULL)
1855 goto done;
1856
1857 // Expand environment variables and ~.
1858 // Don't do it when adding without inserting a comma.
1859 if (op == OP_NONE || (flags & P_COMMA))
1860 {
1861 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1862 if (newval == NULL)
1863 goto done;
1864 }
1865
1866 // locate newval[] in origval[] when removing it and when adding to
1867 // avoid duplicates
1868 int len = 0;
1869 if (op == OP_REMOVING || (flags & P_NODUP))
1870 {
1871 len = (int)STRLEN(newval);
1872 s = find_dup_item(origval, newval, flags);
1873
1874 // do not add if already there
1875 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1876 {
1877 op = OP_NONE;
1878 STRCPY(newval, origval);
1879 }
1880
1881 // if no duplicate, move pointer to end of original value
1882 if (s == NULL)
1883 s = origval + (int)STRLEN(origval);
1884 }
1885
1886 // concatenate the two strings; add a ',' if needed
1887 if (op == OP_ADDING || op == OP_PREPENDING)
1888 stropt_concat_with_comma(origval, newval, op, flags);
1889 else if (op == OP_REMOVING)
1890 // Remove newval[] from origval[]. (Note: "len" has been set above
1891 // and is used here).
1892 stropt_remove_val(origval, newval, flags, s, len);
1893
1894 if (flags & P_FLAGLIST)
1895 // Remove flags that appear twice.
1896 stropt_remove_dupflags(newval, flags);
1897 }
1898
1899done:
1900 if (save_arg != NULL)
1901 arg = save_arg; // arg was temporarily changed, restore it
1902 *argp = arg;
1903 *origval_arg = origval;
1904 *origval_l_arg = origval_l;
1905 *origval_g_arg = origval_g;
1906 *oldval_arg = oldval;
1907 *op_arg = op;
1908
1909 return newval;
1910}
1911
1912/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001913 * Part of do_set() for string options.
1914 * Returns FAIL on failure, do not process further options.
1915 */
1916 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001917do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001918 int opt_idx,
1919 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001920 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001921 int nextchar,
1922 set_op_T op_arg,
1923 int flags,
1924 int cp_val,
1925 char_u *varp_arg,
1926 char *errbuf,
1927 int *value_checked,
1928 char **errmsg)
1929{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001930 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001931 set_op_T op = op_arg;
1932 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001933 char_u *oldval = NULL; // previous value if *varp
1934 char_u *newval;
1935 char_u *origval = NULL;
1936 char_u *origval_l = NULL;
1937 char_u *origval_g = NULL;
1938#if defined(FEAT_EVAL)
1939 char_u *saved_origval = NULL;
1940 char_u *saved_origval_l = NULL;
1941 char_u *saved_origval_g = NULL;
1942 char_u *saved_newval = NULL;
1943#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001944
1945 // When using ":set opt=val" for a global option
1946 // with a local value the local value will be
1947 // reset, use the global value here.
1948 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1949 && ((int)options[opt_idx].indir & PV_BOTH))
1950 varp = options[opt_idx].var;
1951
1952 // The old value is kept until we are sure that the new value is valid.
1953 oldval = *(char_u **)varp;
1954
1955 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1956 {
1957 origval_l = *(char_u **)get_varp_scope(
1958 &(options[opt_idx]), OPT_LOCAL);
1959 origval_g = *(char_u **)get_varp_scope(
1960 &(options[opt_idx]), OPT_GLOBAL);
1961
1962 // A global-local string option might have an empty option as value to
1963 // indicate that the global value should be used.
1964 if (((int)options[opt_idx].indir & PV_BOTH)
1965 && origval_l == empty_option)
1966 origval_l = origval_g;
1967 }
1968
1969 // When setting the local value of a global option, the old value may be
1970 // the global value.
1971 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1972 origval = *(char_u **)get_varp(&options[opt_idx]);
1973 else
1974 origval = oldval;
1975
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001976 // Get the new value for the option
1977 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1978 &origval_l, &origval_g, &oldval, &op, flags,
1979 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001980
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001981 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001982 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001983 if (newval == NULL)
1984 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001985
1986#if defined(FEAT_EVAL)
1987 if (!starting
1988# ifdef FEAT_CRYPT
1989 && options[opt_idx].indir != PV_KEY
1990# endif
1991 && origval != NULL && newval != NULL)
1992 {
1993 // origval may be freed by did_set_string_option(), make a copy.
1994 saved_origval = vim_strsave(origval);
1995 // newval (and varp) may become invalid if the buffer is closed by
1996 // autocommands.
1997 saved_newval = vim_strsave(newval);
1998 if (origval_l != NULL)
1999 saved_origval_l = vim_strsave(origval_l);
2000 if (origval_g != NULL)
2001 saved_origval_g = vim_strsave(origval_g);
2002 }
2003#endif
2004
2005 {
2006 long_u *p = insecure_flag(opt_idx, opt_flags);
2007 int secure_saved = secure;
2008
2009 // When an option is set in the sandbox, from a modeline or in secure
2010 // mode, then deal with side effects in secure mode. Also when the
2011 // value was set with the P_INSECURE flag and is not completely
2012 // replaced.
2013 if ((opt_flags & OPT_MODELINE)
2014#ifdef HAVE_SANDBOX
2015 || sandbox != 0
2016#endif
2017 || (op != OP_NONE && (*p & P_INSECURE)))
2018 secure = 1;
2019
2020 // Handle side effects, and set the global value for ":set" on local
2021 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2022 // be triggered that can cause havoc.
2023 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002024 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Bram Moolenaar47403942022-09-21 21:12:53 +01002025 opt_flags, value_checked);
2026
2027 secure = secure_saved;
2028 }
2029
2030#if defined(FEAT_EVAL)
2031 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002032 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002033 saved_origval_l, saved_origval_g, saved_newval);
2034 vim_free(saved_origval);
2035 vim_free(saved_origval_l);
2036 vim_free(saved_origval_g);
2037 vim_free(saved_newval);
2038#endif
2039
Bram Moolenaar6f981142022-09-22 12:48:58 +01002040 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002041 return *errmsg == NULL ? OK : FAIL;
2042}
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002045 * Set a boolean option.
2046 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002047 */
2048 static char *
2049do_set_option_bool(
2050 int opt_idx,
2051 int opt_flags,
2052 set_prefix_T prefix,
2053 long_u flags,
2054 char_u *varp,
2055 int nextchar,
2056 int afterchar,
2057 int cp_val)
2058
2059{
2060 varnumber_T value;
2061
2062 if (nextchar == '=' || nextchar == ':')
2063 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002064 if (opt_idx < 0 || varp == NULL)
2065 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002066
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002067 // ":set opt!": invert
2068 // ":set opt&": reset to default value
2069 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002070 if (nextchar == '!')
2071 value = *(int *)(varp) ^ 1;
2072 else if (nextchar == '&')
2073 value = (int)(long)(long_i)options[opt_idx].def_val[
2074 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2075 else if (nextchar == '<')
2076 {
2077 // For 'autoread' -1 means to use global value.
2078 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2079 value = -1;
2080 else
2081 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2082 }
2083 else
2084 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002085 // ":set invopt": invert
2086 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002087 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2088 return e_trailing_characters;
2089 if (prefix == PREFIX_INV)
2090 value = *(int *)(varp) ^ 1;
2091 else
2092 value = prefix == PREFIX_NO ? 0 : 1;
2093 }
2094
2095 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2096}
2097
2098/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002099 * Set a numeric option.
2100 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002101 */
2102 static char *
2103do_set_option_numeric(
2104 int opt_idx,
2105 int opt_flags,
2106 char_u **argp,
2107 int nextchar,
2108 set_op_T op,
2109 long_u flags,
2110 int cp_val,
2111 char_u *varp,
2112 char *errbuf,
2113 size_t errbuflen)
2114{
2115 char_u *arg = *argp;
2116 varnumber_T value;
2117 int i;
2118 char *errmsg = NULL;
2119
Bram Moolenaar546933f2023-02-06 16:40:49 +00002120 if (opt_idx < 0 || varp == NULL)
2121 return NULL; // "cannot happen"
2122 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002123 /*
2124 * Different ways to set a number option:
2125 * & set to default value
2126 * < set to global value
2127 * <xx> accept special key codes for 'wildchar'
2128 * c accept any non-digit for 'wildchar'
2129 * [-]0-9 set number
2130 * other error
2131 */
2132 ++arg;
2133 if (nextchar == '&')
2134 value = (long)(long_i)options[opt_idx].def_val[
2135 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2136 else if (nextchar == '<')
2137 {
2138 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2139 // use the global value.
2140 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2141 value = NO_LOCAL_UNDOLEVEL;
2142 else
2143 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2144 }
2145 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2146 && (*arg == '<'
2147 || *arg == '^'
2148 || (*arg != NUL
2149 && (!arg[1] || VIM_ISWHITE(arg[1]))
2150 && !VIM_ISDIGIT(*arg))))
2151 {
2152 value = string_to_key(arg, FALSE);
2153 if (value == 0 && (long *)varp != &p_wcm)
2154 {
2155 errmsg = e_invalid_argument;
2156 goto skip;
2157 }
2158 }
2159 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2160 {
2161 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002162 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002163 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2164 {
2165 errmsg = e_number_required_after_equal;
2166 goto skip;
2167 }
2168 }
2169 else
2170 {
2171 errmsg = e_number_required_after_equal;
2172 goto skip;
2173 }
2174
2175 if (op == OP_ADDING)
2176 value = *(long *)varp + value;
2177 else if (op == OP_PREPENDING)
2178 value = *(long *)varp * value;
2179 else if (op == OP_REMOVING)
2180 value = *(long *)varp - value;
2181
2182 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2183 opt_flags);
2184
2185skip:
2186 *argp = arg;
2187 return errmsg;
2188}
2189
2190/*
2191 * Set a key code (t_xx) option
2192 */
2193 static char *
2194do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2195{
2196 char_u *arg = *argp;
2197 char_u *p;
2198
2199 if (nextchar == '&')
2200 {
2201 if (add_termcap_entry(key_name, TRUE) == FAIL)
2202 return e_not_found_in_termcap;
2203 }
2204 else
2205 {
2206 ++arg; // jump to after the '=' or ':'
2207 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2208 if (*p == '\\' && p[1] != NUL)
2209 ++p;
2210 nextchar = *p;
2211 *p = NUL;
2212 add_termcode(key_name, arg, FALSE);
2213 *p = nextchar;
2214 }
2215 if (full_screen)
2216 ttest(FALSE);
2217 redraw_all_later(UPD_CLEAR);
2218
2219 *argp = arg;
2220 return NULL;
2221}
2222
2223/*
2224 * Set an option to a new value.
2225 */
2226 static char *
2227do_set_option_value(
2228 int opt_idx,
2229 int opt_flags,
2230 char_u **argp,
2231 set_prefix_T prefix,
2232 set_op_T op,
2233 long_u flags,
2234 char_u *varp,
2235 char_u *key_name,
2236 int nextchar,
2237 int afterchar,
2238 int cp_val,
2239 int *stopopteval,
2240 char *errbuf,
2241 size_t errbuflen)
2242{
2243 int value_checked = FALSE;
2244 char *errmsg = NULL;
2245 char_u *arg = *argp;
2246
2247 if (flags & P_BOOL)
2248 {
2249 // boolean option
2250 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2251 nextchar, afterchar, cp_val);
2252 if (errmsg != NULL)
2253 goto skip;
2254 }
2255 else
2256 {
2257 // numeric or string option
2258 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2259 || prefix != PREFIX_NONE)
2260 {
2261 errmsg = e_invalid_argument;
2262 goto skip;
2263 }
2264
2265 if (flags & P_NUM)
2266 {
2267 // numeric option
2268 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2269 op, flags, cp_val, varp,
2270 errbuf, errbuflen);
2271 if (errmsg != NULL)
2272 goto skip;
2273 }
2274 else if (opt_idx >= 0)
2275 {
2276 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002277 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2278 flags, cp_val, varp, errbuf,
2279 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002280 {
2281 if (errmsg != NULL)
2282 goto skip;
2283 *stopopteval = TRUE;
2284 goto skip;
2285 }
2286 }
2287 else
2288 {
2289 // key code option
2290 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2291 if (errmsg != NULL)
2292 goto skip;
2293 }
2294 }
2295
2296 if (opt_idx >= 0)
2297 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2298
2299skip:
2300 *argp = arg;
2301 return errmsg;
2302}
2303
2304/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002305 * Set an option to a new value.
2306 * Return NULL if OK, return an untranslated error message when something is
2307 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2308 */
2309 static char *
2310do_set_option(
2311 int opt_flags,
2312 char_u **argp,
2313 char_u *arg_start,
2314 char_u **startarg,
2315 int *did_show,
2316 int *stopopteval,
2317 char *errbuf,
2318 size_t errbuflen)
2319{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002320 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002321 char_u *arg;
2322 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2323 set_op_T op;
2324 long_u flags; // flags for current option
2325 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002326 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002327 int nextchar; // next non-white char after option name
2328 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002329 char *errmsg = NULL;
2330 int key;
2331 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002332
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002333 prefix = get_option_prefix(argp);
2334 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002335
2336 // find end of name
2337 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002338 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2339 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002340
2341 // remember character after option name
2342 afterchar = arg[len];
2343
2344 if (in_vim9script())
2345 {
2346 char_u *p = skipwhite(arg + len);
2347
2348 // disallow white space before =val, +=val, -=val, ^=val
2349 if (p > arg + len && (p[0] == '='
2350 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2351 && p[1] == '=')))
2352 {
2353 errmsg = e_no_white_space_allowed_between_option_and;
2354 arg = p;
2355 *startarg = p;
2356 goto skip;
2357 }
2358 }
2359 else
2360 // skip white space, allow ":set ai ?", ":set hlsearch !"
2361 while (VIM_ISWHITE(arg[len]))
2362 ++len;
2363
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002364 op = get_opt_op(arg + len);
2365 if (op != OP_NONE)
2366 len++;
2367
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002368 nextchar = arg[len];
2369
2370 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2371 {
2372 if (in_vim9script() && arg > arg_start
2373 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2374 errmsg = e_no_white_space_allowed_between_option_and;
2375 else
2376 errmsg = e_unknown_option;
2377 goto skip;
2378 }
2379
2380 if (opt_idx >= 0)
2381 {
2382 if (options[opt_idx].var == NULL) // hidden option: skip
2383 {
2384 // Only give an error message when requesting the value of
2385 // a hidden option, ignore setting it.
2386 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2387 && (!(options[opt_idx].flags & P_BOOL)
2388 || nextchar == '?'))
2389 errmsg = e_option_not_supported;
2390 goto skip;
2391 }
2392
2393 flags = options[opt_idx].flags;
2394 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2395 }
2396 else
2397 {
2398 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002399 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002400 if (key < 0)
2401 {
2402 key_name[0] = KEY2TERMCAP0(key);
2403 key_name[1] = KEY2TERMCAP1(key);
2404 }
2405 else
2406 {
2407 key_name[0] = KS_KEY;
2408 key_name[1] = (key & 0xff);
2409 }
2410 }
2411
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002412 // Make sure the option value can be changed.
2413 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002414 goto skip;
2415
Bram Moolenaar40b48722023-02-05 17:04:50 +00002416 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002417 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2418 {
2419 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002420 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2421 {
2422 if (arg[3] == 'm') // "opt&vim": set to Vim default
2423 {
2424 cp_val = FALSE;
2425 arg += 3;
2426 }
2427 else // "opt&vi": set to Vi default
2428 {
2429 cp_val = TRUE;
2430 arg += 2;
2431 }
2432 }
2433 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2434 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2435 {
2436 errmsg = e_trailing_characters;
2437 goto skip;
2438 }
2439 }
2440
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002441 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2442 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002443 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002444 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002445 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2446 && !(flags & P_BOOL)))
2447 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002448 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002449 if (*did_show)
2450 msg_putchar('\n'); // cursor below last one
2451 else
2452 {
2453 gotocmdline(TRUE); // cursor at status line
2454 *did_show = TRUE; // remember that we did a line
2455 }
2456 if (opt_idx >= 0)
2457 {
2458 showoneopt(&options[opt_idx], opt_flags);
2459#ifdef FEAT_EVAL
2460 if (p_verbose > 0)
2461 {
2462 // Mention where the option was last set.
2463 if (varp == options[opt_idx].var)
2464 last_set_msg(options[opt_idx].script_ctx);
2465 else if ((int)options[opt_idx].indir & PV_WIN)
2466 last_set_msg(curwin->w_p_script_ctx[
2467 (int)options[opt_idx].indir & PV_MASK]);
2468 else if ((int)options[opt_idx].indir & PV_BUF)
2469 last_set_msg(curbuf->b_p_script_ctx[
2470 (int)options[opt_idx].indir & PV_MASK]);
2471 }
2472#endif
2473 }
2474 else
2475 {
2476 char_u *p;
2477
2478 p = find_termcode(key_name);
2479 if (p == NULL)
2480 {
2481 errmsg = e_key_code_not_set;
2482 goto skip;
2483 }
2484 else
2485 (void)show_one_termcode(key_name, p, TRUE);
2486 }
2487 if (nextchar != '?'
2488 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2489 errmsg = e_trailing_characters;
2490 }
2491 else
2492 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002493 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2494 flags, varp, key_name, nextchar,
2495 afterchar, cp_val, stopopteval, errbuf,
2496 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002497 }
2498
2499skip:
2500 *argp = arg;
2501 return errmsg;
2502}
2503
2504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 * Parse 'arg' for option settings.
2506 *
2507 * 'arg' may be IObuff, but only when no errors can be present and option
2508 * does not need to be expanded with option_expand().
2509 * "opt_flags":
2510 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002511 * OPT_GLOBAL for ":setglobal"
2512 * OPT_LOCAL for ":setlocal" and a modeline
2513 * OPT_MODELINE for a modeline
2514 * OPT_WINONLY to only set window-local options
2515 * OPT_NOWIN to skip setting window-local options
2516 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002518 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 */
2520 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002521do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002522 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002523 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002525 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002527 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529 if (*arg == NUL)
2530 {
2531 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002532 did_show = TRUE;
2533 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002536 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002538 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002539 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002541 // ":set all" show all options.
2542 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 arg += 3;
2544 if (*arg == '&')
2545 {
2546 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002547 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002549 didset_options();
2550 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002551 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
2553 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002556 did_show = TRUE;
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002559 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002562 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 arg += 7;
2565 }
2566 else
2567 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002568 int stopopteval = FALSE;
2569 char *errmsg = NULL;
2570 char errbuf[80];
2571 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002573 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2574 &did_show, &stopopteval, errbuf,
2575 sizeof(errbuf));
2576 if (stopopteval)
2577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002579 // Advance to next argument.
2580 // - skip until a blank found, taking care of backslashes
2581 // - skip blanks
2582 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 for (i = 0; i < 2 ; ++i)
2584 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002585 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (*arg++ == '\\' && *arg != NUL)
2587 ++arg;
2588 arg = skipwhite(arg);
2589 if (*arg != '=')
2590 break;
2591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002593 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002595 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2596 i = (int)STRLEN(IObuff) + 2;
2597 if (i + (arg - startarg) < IOSIZE)
2598 {
2599 // append the argument with the error
2600 STRCAT(IObuff, ": ");
2601 mch_memmove(IObuff + i, startarg, (arg - startarg));
2602 IObuff[i + (arg - startarg)] = NUL;
2603 }
2604 // make sure all characters are printable
2605 trans_characters(IObuff, IOSIZE);
2606
2607 ++no_wait_return; // wait_return() done later
2608 emsg((char *)IObuff); // show error highlighted
2609 --no_wait_return;
2610
2611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
2615 arg = skipwhite(arg);
2616 }
2617
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002618theend:
2619 if (silent_mode && did_show)
2620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002621 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002622 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002623 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002624 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 out_flush();
2627 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002628 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002629 }
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 return OK;
2632}
2633
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002634/*
2635 * Call this when an option has been given a new value through a user command.
2636 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2637 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002639did_set_option(
2640 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002641 int opt_flags, // possibly with OPT_MODELINE
2642 int new_value, // value was replaced completely
2643 int value_checked) // value was checked to be safe, no need to set the
2644 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002645{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002646 long_u *p;
2647
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002648 options[opt_idx].flags |= P_WAS_SET;
2649
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002650 // When an option is set in the sandbox, from a modeline or in secure mode
2651 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2652 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002653 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002654 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002655#ifdef HAVE_SANDBOX
2656 || sandbox != 0
2657#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002658 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002659 *p = *p | P_INSECURE;
2660 else if (new_value)
2661 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002662}
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664/*
2665 * Convert a key name or string into a key value.
2666 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002667 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002669 int
2670string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671{
2672 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002673 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (*arg == '^')
2675 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002676 if (multi_byte)
2677 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return *arg;
2679}
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681/*
2682 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2683 * maketitle() to create and display it.
2684 * When switching the title or icon off, call mch_restore_title() to get
2685 * the old value back.
2686 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002687 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002688did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 if (starting != NO_SCREEN
2691#ifdef FEAT_GUI
2692 && !gui.starting
2693#endif
2694 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698/*
2699 * set_options_bin - called when 'bin' changes value.
2700 */
2701 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002702set_options_bin(
2703 int oldval,
2704 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002707 // The option values that are changed when 'bin' changes are
2708 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (newval)
2710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
2713 if (!(opt_flags & OPT_GLOBAL))
2714 {
2715 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2716 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2717 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2718 curbuf->b_p_et_nobin = curbuf->b_p_et;
2719 }
2720 if (!(opt_flags & OPT_LOCAL))
2721 {
2722 p_tw_nobin = p_tw;
2723 p_wm_nobin = p_wm;
2724 p_ml_nobin = p_ml;
2725 p_et_nobin = p_et;
2726 }
2727 }
2728
2729 if (!(opt_flags & OPT_GLOBAL))
2730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 curbuf->b_p_tw = 0; // no automatic line wrap
2732 curbuf->b_p_wm = 0; // no automatic line wrap
2733 curbuf->b_p_ml = 0; // no modelines
2734 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 if (!(opt_flags & OPT_LOCAL))
2737 {
2738 p_tw = 0;
2739 p_wm = 0;
2740 p_ml = FALSE;
2741 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002742 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 if (!(opt_flags & OPT_GLOBAL))
2748 {
2749 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2750 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2751 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2752 curbuf->b_p_et = curbuf->b_p_et_nobin;
2753 }
2754 if (!(opt_flags & OPT_LOCAL))
2755 {
2756 p_tw = p_tw_nobin;
2757 p_wm = p_wm_nobin;
2758 p_ml = p_ml_nobin;
2759 p_et = p_et_nobin;
2760 }
2761 }
2762}
2763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764/*
2765 * Expand environment variables for some string options.
2766 * These string options cannot be indirect!
2767 * If "val" is NULL expand the current value of the option.
2768 * Return pointer to NameBuff, or NULL when not expanded.
2769 */
2770 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002771option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2775 return NULL;
2776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 // If val is longer than MAXPATHL no meaningful expansion can be done,
2778 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (val != NULL && STRLEN(val) > MAXPATHL)
2780 return NULL;
2781
2782 if (val == NULL)
2783 val = *(char_u **)options[opt_idx].var;
2784
2785 /*
2786 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2787 * Escape spaces when expanding 'tags', they are used to separate file
2788 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002789 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 */
2791 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002792 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002793#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002794 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2795#endif
2796 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 return NULL;
2799
2800 return NameBuff;
2801}
2802
2803/*
2804 * After setting various option values: recompute variables that depend on
2805 * option values.
2806 */
2807 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002808didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002810 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 (void)init_chartab();
2812
Bram Moolenaardac13472019-09-16 21:06:21 +02002813 didset_string_options();
2814
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002815#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002816 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002817 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002818 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002819 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002820#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002821 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002822 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002823#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002825 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002826#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002827 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002828}
2829
2830/*
2831 * More side effects of setting options.
2832 */
2833 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002834didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002835{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002836 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002837 (void)highlight_changed();
2838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002840 check_opt_wim();
2841
Bram Moolenaareed9d462021-02-15 20:38:25 +01002842 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002843 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002846 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002847
2848#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002850 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002851#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002852#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002853 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002854 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002855 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002856 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858}
2859
2860/*
2861 * Check for string options that are NULL (normally only termcap options).
2862 */
2863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002864check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865{
2866 int opt_idx;
2867
2868 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2869 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2870 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2871}
2872
2873/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002874 * Return the option index found by a pointer into term_strings[].
2875 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002877 int
2878get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002880 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
2882 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2883 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002884 return opt_idx;
2885 return -1; // cannot happen: didn't find it!
2886}
2887
2888/*
2889 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2890 * Return the option index or -1 if not found.
2891 */
2892 int
2893set_term_option_alloced(char_u **p)
2894{
2895 int opt_idx = get_term_opt_idx(p);
2896
2897 if (opt_idx >= 0)
2898 options[opt_idx].flags |= P_ALLOCED;
2899 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900}
2901
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002902#if defined(FEAT_EVAL) || defined(PROTO)
2903/*
2904 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2905 * Return FALSE when it wasn't.
2906 * Return -1 for an unknown option.
2907 */
2908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002909was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002910{
2911 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002912 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002913
2914 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002915 {
2916 flagp = insecure_flag(idx, opt_flags);
2917 return (*flagp & P_INSECURE) != 0;
2918 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002919 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002920 return -1;
2921}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002922
2923/*
2924 * Get a pointer to the flags used for the P_INSECURE flag of option
2925 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002926 * NOTE: Caller must make sure that "curwin" is set to the window from which
2927 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002928 */
2929 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002930insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002931{
2932 if (opt_flags & OPT_LOCAL)
2933 switch ((int)options[opt_idx].indir)
2934 {
2935#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002936 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002937#endif
2938#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002939# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002940 case PV_FDE: return &curwin->w_p_fde_flags;
2941 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002942# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002943# ifdef FEAT_BEVAL
2944 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2945# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002946 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002947 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002948# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002949 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002950# endif
2951#endif
2952 }
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002955 return &options[opt_idx].flags;
2956}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002957#endif
2958
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002959/*
2960 * Redraw the window title and/or tab page text later.
2961 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002962void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002963{
2964 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002965 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002966}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002969 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2970 * characters or characters in "allowed".
2971 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002972 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002973valid_name(char_u *val, char *allowed)
2974{
2975 char_u *s;
2976
2977 for (s = val; *s != NUL; ++s)
2978 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2979 return FALSE;
2980 return TRUE;
2981}
2982
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002983#if defined(FEAT_EVAL) || defined(PROTO)
2984/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002985 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002986 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002987 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002988 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002989set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002990{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002991 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2992 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002993 sctx_T new_script_ctx = script_ctx;
2994
Bram Moolenaar51258742020-05-03 17:19:33 +02002995 // Modeline already has the line number set.
2996 if (!(opt_flags & OPT_MODELINE))
2997 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // Remember where the option was set. For local options need to do that
3000 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003001 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003002 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003003 if (both || (opt_flags & OPT_LOCAL))
3004 {
3005 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003006 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003007 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003008 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003009 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003010 if (both)
3011 // also setting the "all buffers" value
3012 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3013 new_script_ctx;
3014 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003015 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003016}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003017
3018/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003019 * Get the script context of global option "name".
3020 *
3021 */
3022 sctx_T *
3023get_option_sctx(char *name)
3024{
3025 int idx = findoption((char_u *)name);
3026
3027 if (idx >= 0)
3028 return &options[idx].script_ctx;
3029 siemsg("no such option: %s", name);
3030 return NULL;
3031}
3032
3033/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003034 * Set the script_ctx for a termcap option.
3035 * "name" must be the two character code, e.g. "RV".
3036 * When "name" is NULL use "opt_idx".
3037 */
3038 void
3039set_term_option_sctx_idx(char *name, int opt_idx)
3040{
3041 char_u buf[5];
3042 int idx;
3043
3044 if (name == NULL)
3045 idx = opt_idx;
3046 else
3047 {
3048 buf[0] = 't';
3049 buf[1] = '_';
3050 buf[2] = name[0];
3051 buf[3] = name[1];
3052 buf[4] = 0;
3053 idx = findoption(buf);
3054 }
3055 if (idx >= 0)
3056 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3057}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003058#endif
3059
Bram Moolenaarf4140482020-02-15 23:06:45 +01003060#if defined(FEAT_EVAL)
3061/*
3062 * Apply the OptionSet autocommand.
3063 */
3064 static void
3065apply_optionset_autocmd(
3066 int opt_idx,
3067 long opt_flags,
3068 long oldval,
3069 long oldval_g,
3070 long newval,
3071 char *errmsg)
3072{
3073 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3074
3075 // Don't do this while starting up, failure or recursively.
3076 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3077 return;
3078
3079 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3080 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3081 oldval_g);
3082 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3083 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3084 (opt_flags & OPT_LOCAL) ? "local" : "global");
3085 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3086 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3087 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3088 if (opt_flags & OPT_LOCAL)
3089 {
3090 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3091 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3092 }
3093 if (opt_flags & OPT_GLOBAL)
3094 {
3095 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3096 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3097 }
3098 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3099 {
3100 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3101 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3102 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3103 }
3104 if (opt_flags & OPT_MODELINE)
3105 {
3106 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3107 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3108 }
3109 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3110 NULL, FALSE, NULL);
3111 reset_v_option_vars();
3112}
3113#endif
3114
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003115#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003116/*
3117 * Process the updated 'arabic' option value.
3118 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003119 char *
3120did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003121{
3122 char *errmsg = NULL;
3123
3124 if (curwin->w_p_arab)
3125 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003126 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003127 if (!p_tbidi)
3128 {
3129 // set rightleft mode
3130 if (!curwin->w_p_rl)
3131 {
3132 curwin->w_p_rl = TRUE;
3133 changed_window_setting();
3134 }
3135
3136 // Enable Arabic shaping (major part of what Arabic requires)
3137 if (!p_arshape)
3138 {
3139 p_arshape = TRUE;
3140 redraw_later_clear();
3141 }
3142 }
3143
3144 // Arabic requires a utf-8 encoding, inform the user if it's not
3145 // set.
3146 if (STRCMP(p_enc, "utf-8") != 0)
3147 {
3148 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3149
3150 msg_source(HL_ATTR(HLF_W));
3151 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3152#ifdef FEAT_EVAL
3153 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3154#endif
3155 }
3156
3157 // set 'delcombine'
3158 p_deco = TRUE;
3159
3160# ifdef FEAT_KEYMAP
3161 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003162 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3163 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003164# endif
3165 }
3166 else
3167 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003168 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003169 if (!p_tbidi)
3170 {
3171 // reset rightleft mode
3172 if (curwin->w_p_rl)
3173 {
3174 curwin->w_p_rl = FALSE;
3175 changed_window_setting();
3176 }
3177
3178 // 'arabicshape' isn't reset, it is a global option and
3179 // another window may still need it "on".
3180 }
3181
3182 // 'delcombine' isn't reset, it is a global option and another
3183 // window may still want it "on".
3184
3185# ifdef FEAT_KEYMAP
3186 // Revert to the default keymap
3187 curbuf->b_p_iminsert = B_IMODE_NONE;
3188 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3189# endif
3190 }
3191
3192 return errmsg;
3193}
3194#endif
3195
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003196#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3197/*
3198 * Process the updated 'autochdir' option value.
3199 */
3200 char *
3201did_set_autochdir(optset_T *args UNUSED)
3202{
3203 // Change directories when the 'acd' option is set now.
3204 DO_AUTOCHDIR;
3205 return NULL;
3206}
3207#endif
3208
3209#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3210/*
3211 * Process the updated 'ballooneval' option value.
3212 */
3213 char *
3214did_set_ballooneval(optset_T *args)
3215{
3216 if (balloonEvalForTerm)
3217 return NULL;
3218
3219 if (p_beval && !args->os_oldval.boolean)
3220 gui_mch_enable_beval_area(balloonEval);
3221 else if (!p_beval && args->os_oldval.boolean)
3222 gui_mch_disable_beval_area(balloonEval);
3223
3224 return NULL;
3225}
3226#endif
3227
3228#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3229/*
3230 * Process the updated 'balloonevalterm' option value.
3231 */
3232 char *
3233did_set_balloonevalterm(optset_T *args UNUSED)
3234{
3235 mch_bevalterm_changed();
3236 return NULL;
3237}
3238#endif
3239
3240/*
3241 * Process the updated 'binary' option value.
3242 */
3243 char *
3244did_set_binary(optset_T *args)
3245{
3246 // when 'bin' is set also set some other options
3247 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3248 redraw_titles();
3249
3250 return NULL;
3251}
3252
3253#if defined(FEAT_LINEBREAK) || defined(PROTO)
3254/*
3255 * Called when the 'breakat' option changes value.
3256 */
3257 char *
3258did_set_breakat(optset_T *args UNUSED)
3259{
3260 char_u *p;
3261 int i;
3262
3263 for (i = 0; i < 256; i++)
3264 breakat_flags[i] = FALSE;
3265
3266 if (p_breakat != NULL)
3267 for (p = p_breakat; *p; p++)
3268 breakat_flags[*p] = TRUE;
3269
3270 return NULL;
3271}
3272#endif
3273
3274/*
3275 * Process the updated 'buflisted' option value.
3276 */
3277 char *
3278did_set_buflisted(optset_T *args)
3279{
3280 // when 'buflisted' changes, trigger autocommands
3281 if (args->os_oldval.boolean != curbuf->b_p_bl)
3282 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3283 NULL, NULL, TRUE, curbuf);
3284 return NULL;
3285}
3286
3287/*
3288 * Process the new 'cmdheight' option value.
3289 */
3290 char *
3291did_set_cmdheight(optset_T *args)
3292{
3293 long old_value = args->os_oldval.number;
3294 char *errmsg = NULL;
3295
3296 // if p_ch changed value, change the command line height
3297 if (p_ch < 1)
3298 {
3299 errmsg = e_argument_must_be_positive;
3300 p_ch = 1;
3301 }
3302 if (p_ch > Rows - min_rows() + 1)
3303 p_ch = Rows - min_rows() + 1;
3304
3305 // Only compute the new window layout when startup has been
3306 // completed. Otherwise the frame sizes may be wrong.
3307 if ((p_ch != old_value
3308 || tabline_height() + topframe->fr_height != Rows - p_ch)
3309 && full_screen
3310#ifdef FEAT_GUI
3311 && !gui.starting
3312#endif
3313 )
3314 command_height();
3315
3316 return errmsg;
3317}
3318
3319/*
3320 * Process the updated 'compatible' option value.
3321 */
3322 char *
3323did_set_compatible(optset_T *args UNUSED)
3324{
3325 compatible_set();
3326 return NULL;
3327}
3328
3329#if defined(FEAT_CONCEAL) || defined(PROTO)
3330/*
3331 * Process the new 'conceallevel' option value.
3332 */
3333 char *
3334did_set_conceallevel(optset_T *args UNUSED)
3335{
3336 char *errmsg = NULL;
3337
3338 if (curwin->w_p_cole < 0)
3339 {
3340 errmsg = e_argument_must_be_positive;
3341 curwin->w_p_cole = 0;
3342 }
3343 else if (curwin->w_p_cole > 3)
3344 {
3345 errmsg = e_invalid_argument;
3346 curwin->w_p_cole = 3;
3347 }
3348
3349 return errmsg;
3350}
3351#endif
3352
3353#if defined(FEAT_DIFF) || defined(PROTO)
3354/*
3355 * Process the updated 'diff' option value.
3356 */
3357 char *
3358did_set_diff(optset_T *args UNUSED)
3359{
3360 // May add or remove the buffer from the list of diff buffers.
3361 diff_buf_adjust(curwin);
3362# ifdef FEAT_FOLDING
3363 if (foldmethodIsDiff(curwin))
3364 foldUpdateAll(curwin);
3365# endif
3366 return NULL;
3367}
3368#endif
3369
3370/*
3371 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3372 * option value.
3373 */
3374 char *
3375did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3376{
3377 // redraw the window title and tab page text
3378 redraw_titles();
3379 return NULL;
3380}
3381
3382/*
3383 * Process the updated 'equalalways' option value.
3384 */
3385 char *
3386did_set_equalalways(optset_T *args)
3387{
3388 if (p_ea && !args->os_oldval.boolean)
3389 win_equal(curwin, FALSE, 0);
3390
3391 return NULL;
3392}
3393
3394#if defined(FEAT_FOLDING) || defined(PROTO)
3395/*
3396 * Process the new 'foldcolumn' option value.
3397 */
3398 char *
3399did_set_foldcolumn(optset_T *args UNUSED)
3400{
3401 char *errmsg = NULL;
3402
3403 if (curwin->w_p_fdc < 0)
3404 {
3405 errmsg = e_argument_must_be_positive;
3406 curwin->w_p_fdc = 0;
3407 }
3408 else if (curwin->w_p_fdc > 12)
3409 {
3410 errmsg = e_invalid_argument;
3411 curwin->w_p_fdc = 12;
3412 }
3413
3414 return errmsg;
3415}
3416
3417/*
3418 * Process the new 'foldlevel' option value.
3419 */
3420 char *
3421did_set_foldlevel(optset_T *args UNUSED)
3422{
3423 if (curwin->w_p_fdl < 0)
3424 curwin->w_p_fdl = 0;
3425 newFoldLevel();
3426 return NULL;
3427}
3428
3429/*
3430 * Process the new 'foldminlines' option value.
3431 */
3432 char *
3433did_set_foldminlines(optset_T *args UNUSED)
3434{
3435 foldUpdateAll(curwin);
3436 return NULL;
3437}
3438
3439/*
3440 * Process the new 'foldnestmax' option value.
3441 */
3442 char *
3443did_set_foldnestmax(optset_T *args UNUSED)
3444{
3445 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3446 foldUpdateAll(curwin);
3447 return NULL;
3448}
3449#endif
3450
3451#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3452/*
3453 * Process the updated 'hlsearch' option value.
3454 */
3455 char *
3456did_set_hlsearch(optset_T *args UNUSED)
3457{
3458 // when 'hlsearch' is set or reset: reset no_hlsearch
3459 set_no_hlsearch(FALSE);
3460 return NULL;
3461}
3462#endif
3463
3464/*
3465 * Process the updated 'ignorecase' option value.
3466 */
3467 char *
3468did_set_ignorecase(optset_T *args UNUSED)
3469{
3470 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3471 if (p_hls)
3472 redraw_all_later(UPD_SOME_VALID);
3473 return NULL;
3474}
3475
3476#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3477/*
3478 * Process the updated 'imdisable' option value.
3479 */
3480 char *
3481did_set_imdisable(optset_T *args UNUSED)
3482{
3483 // Only de-activate it here, it will be enabled when changing mode.
3484 if (p_imdisable)
3485 im_set_active(FALSE);
3486 else if (State & MODE_INSERT)
3487 // When the option is set from an autocommand, it may need to take
3488 // effect right away.
3489 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3490 return NULL;
3491}
3492#endif
3493
3494/*
3495 * Process the new 'iminsert' option value.
3496 */
3497 char *
3498did_set_iminsert(optset_T *args UNUSED)
3499{
3500 char *errmsg = NULL;
3501
3502 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3503 {
3504 errmsg = e_invalid_argument;
3505 curbuf->b_p_iminsert = B_IMODE_NONE;
3506 }
3507 p_iminsert = curbuf->b_p_iminsert;
3508 if (termcap_active) // don't do this in the alternate screen
3509 showmode();
3510#if defined(FEAT_KEYMAP)
3511 // Show/unshow value of 'keymap' in status lines.
3512 status_redraw_curbuf();
3513#endif
3514
3515 return errmsg;
3516}
3517
3518/*
3519 * Process the new 'imsearch' option value.
3520 */
3521 char *
3522did_set_imsearch(optset_T *args UNUSED)
3523{
3524 char *errmsg = NULL;
3525
3526 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3527 {
3528 errmsg = e_invalid_argument;
3529 curbuf->b_p_imsearch = B_IMODE_NONE;
3530 }
3531 p_imsearch = curbuf->b_p_imsearch;
3532
3533 return errmsg;
3534}
3535
3536#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3537/*
3538 * Process the new 'imstyle' option value.
3539 */
3540 char *
3541did_set_imstyle(optset_T *args UNUSED)
3542{
3543 char *errmsg = NULL;
3544
3545 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3546 errmsg = e_invalid_argument;
3547
3548 return errmsg;
3549}
3550#endif
3551
3552/*
3553 * Process the updated 'insertmode' option value.
3554 */
3555 char *
3556did_set_insertmode(optset_T *args)
3557{
3558 // when 'insertmode' is set from an autocommand need to do work here
3559 if (p_im)
3560 {
3561 if ((State & MODE_INSERT) == 0)
3562 need_start_insertmode = TRUE;
3563 stop_insert_mode = FALSE;
3564 }
3565 // only reset if it was set previously
3566 else if (args->os_oldval.boolean)
3567 {
3568 need_start_insertmode = FALSE;
3569 stop_insert_mode = TRUE;
3570 if (restart_edit != 0 && mode_displayed)
3571 clear_cmdline = TRUE; // remove "(insert)"
3572 restart_edit = 0;
3573 }
3574
3575 return NULL;
3576}
3577
3578#if defined(FEAT_LANGMAP) || defined(PROTO)
3579/*
3580 * Process the updated 'langnoremap' option value.
3581 */
3582 char *
3583did_set_langnoremap(optset_T *args UNUSED)
3584{
3585 // 'langnoremap' -> !'langremap'
3586 p_lrm = !p_lnr;
3587 return NULL;
3588}
3589
3590/*
3591 * Process the updated 'langremap' option value.
3592 */
3593 char *
3594did_set_langremap(optset_T *args UNUSED)
3595{
3596 // 'langremap' -> !'langnoremap'
3597 p_lnr = !p_lrm;
3598 return NULL;
3599}
3600#endif
3601
3602/*
3603 * Process the new 'laststatus' option value.
3604 */
3605 char *
3606did_set_laststatus(optset_T *args UNUSED)
3607{
3608 last_status(FALSE); // (re)set last window status line
3609 return NULL;
3610}
3611
3612#if defined(FEAT_GUI) || defined(PROTO)
3613/*
3614 * Process the new 'linespace' option value.
3615 */
3616 char *
3617did_set_linespace(optset_T *args UNUSED)
3618{
3619 // Recompute gui.char_height and resize the Vim window to keep the
3620 // same number of lines.
3621 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3622 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3623 return NULL;
3624}
3625#endif
3626
3627/*
3628 * Process the updated 'lisp' option value.
3629 */
3630 char *
3631did_set_lisp(optset_T *args UNUSED)
3632{
3633 // When 'lisp' option changes include/exclude '-' in keyword characters.
3634 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3635 return NULL;
3636}
3637
3638/*
3639 * Process the new 'maxcombine' option value.
3640 */
3641 char *
3642did_set_maxcombine(optset_T *args UNUSED)
3643{
3644 if (p_mco > MAX_MCO)
3645 p_mco = MAX_MCO;
3646 else if (p_mco < 0)
3647 p_mco = 0;
3648 screenclear(); // will re-allocate the screen
3649 return NULL;
3650}
3651
3652/*
3653 * Process the updated 'modifiable' option value.
3654 */
3655 char *
3656did_set_modifiable(optset_T *args UNUSED)
3657{
3658 // when 'modifiable' is changed, redraw the window title
3659
3660# ifdef FEAT_TERMINAL
3661 // Cannot set 'modifiable' when in Terminal mode.
3662 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3663 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3664 {
3665 curbuf->b_p_ma = FALSE;
3666 args->os_doskip = TRUE;
3667 return e_cannot_make_terminal_with_running_job_modifiable;
3668 }
3669# endif
3670 redraw_titles();
3671
3672 return NULL;
3673}
3674
3675/*
3676 * Process the updated 'modified' option value.
3677 */
3678 char *
3679did_set_modified(optset_T *args)
3680{
3681 if (!args->os_newval.boolean)
3682 save_file_ff(curbuf); // Buffer is unchanged
3683 redraw_titles();
3684 modified_was_set = args->os_newval.boolean;
3685 return NULL;
3686}
3687
3688#if defined(FEAT_GUI) || defined(PROTO)
3689/*
3690 * Process the updated 'mousehide' option value.
3691 */
3692 char *
3693did_set_mousehide(optset_T *args UNUSED)
3694{
3695 if (!p_mh)
3696 gui_mch_mousehide(FALSE);
3697 return NULL;
3698}
3699#endif
3700
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003701/*
3702 * Process the updated 'number' or 'relativenumber' option value.
3703 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003704 char *
3705did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003706{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003707#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003708 if (gui.in_use
3709 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3710 && curbuf->b_signlist != NULL)
3711 {
3712 // If the 'number' or 'relativenumber' options are modified and
3713 // 'signcolumn' is set to 'number', then clear the screen for a full
3714 // refresh. Otherwise the sign icons are not displayed properly in the
3715 // number column. If the 'number' option is set and only the
3716 // 'relativenumber' option is toggled, then don't refresh the screen
3717 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003718 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003719 redraw_all_later(UPD_CLEAR);
3720 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003721#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003722 return NULL;
3723}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003724
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003725#if defined(FEAT_LINEBREAK) || defined(PROTO)
3726/*
3727 * Process the new 'numberwidth' option value.
3728 */
3729 char *
3730did_set_numberwidth(optset_T *args UNUSED)
3731{
3732 char *errmsg = NULL;
3733
3734 // 'numberwidth' must be positive
3735 if (curwin->w_p_nuw < 1)
3736 {
3737 errmsg = e_argument_must_be_positive;
3738 curwin->w_p_nuw = 1;
3739 }
3740 if (curwin->w_p_nuw > 20)
3741 {
3742 errmsg = e_invalid_argument;
3743 curwin->w_p_nuw = 20;
3744 }
3745 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3746
3747 return errmsg;
3748}
3749#endif
3750
3751/*
3752 * Process the updated 'paste' option value. Called after p_paste was set or
3753 * reset. When 'paste' is set or reset also change other options.
3754 */
3755 char *
3756did_set_paste(optset_T *args UNUSED)
3757{
3758 static int old_p_paste = FALSE;
3759 static int save_sm = 0;
3760 static int save_sta = 0;
3761 static int save_ru = 0;
3762#ifdef FEAT_RIGHTLEFT
3763 static int save_ri = 0;
3764 static int save_hkmap = 0;
3765#endif
3766 buf_T *buf;
3767
3768 if (p_paste)
3769 {
3770 // Paste switched from off to on.
3771 // Save the current values, so they can be restored later.
3772 if (!old_p_paste)
3773 {
3774 // save options for each buffer
3775 FOR_ALL_BUFFERS(buf)
3776 {
3777 buf->b_p_tw_nopaste = buf->b_p_tw;
3778 buf->b_p_wm_nopaste = buf->b_p_wm;
3779 buf->b_p_sts_nopaste = buf->b_p_sts;
3780 buf->b_p_ai_nopaste = buf->b_p_ai;
3781 buf->b_p_et_nopaste = buf->b_p_et;
3782#ifdef FEAT_VARTABS
3783 if (buf->b_p_vsts_nopaste)
3784 vim_free(buf->b_p_vsts_nopaste);
3785 buf->b_p_vsts_nopaste =
3786 buf->b_p_vsts && buf->b_p_vsts != empty_option
3787 ? vim_strsave(buf->b_p_vsts) : NULL;
3788#endif
3789 }
3790
3791 // save global options
3792 save_sm = p_sm;
3793 save_sta = p_sta;
3794 save_ru = p_ru;
3795#ifdef FEAT_RIGHTLEFT
3796 save_ri = p_ri;
3797 save_hkmap = p_hkmap;
3798#endif
3799 // save global values for local buffer options
3800 p_ai_nopaste = p_ai;
3801 p_et_nopaste = p_et;
3802 p_sts_nopaste = p_sts;
3803 p_tw_nopaste = p_tw;
3804 p_wm_nopaste = p_wm;
3805#ifdef FEAT_VARTABS
3806 if (p_vsts_nopaste)
3807 vim_free(p_vsts_nopaste);
3808 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3809 ? vim_strsave(p_vsts) : NULL;
3810#endif
3811 }
3812
3813 // Always set the option values, also when 'paste' is set when it is
3814 // already on. Set options for each buffer.
3815 FOR_ALL_BUFFERS(buf)
3816 {
3817 buf->b_p_tw = 0; // textwidth is 0
3818 buf->b_p_wm = 0; // wrapmargin is 0
3819 buf->b_p_sts = 0; // softtabstop is 0
3820 buf->b_p_ai = 0; // no auto-indent
3821 buf->b_p_et = 0; // no expandtab
3822#ifdef FEAT_VARTABS
3823 if (buf->b_p_vsts)
3824 free_string_option(buf->b_p_vsts);
3825 buf->b_p_vsts = empty_option;
3826 VIM_CLEAR(buf->b_p_vsts_array);
3827#endif
3828 }
3829
3830 // set global options
3831 p_sm = 0; // no showmatch
3832 p_sta = 0; // no smarttab
3833 if (p_ru)
3834 status_redraw_all(); // redraw to remove the ruler
3835 p_ru = 0; // no ruler
3836#ifdef FEAT_RIGHTLEFT
3837 p_ri = 0; // no reverse insert
3838 p_hkmap = 0; // no Hebrew keyboard
3839#endif
3840 // set global values for local buffer options
3841 p_tw = 0;
3842 p_wm = 0;
3843 p_sts = 0;
3844 p_ai = 0;
3845#ifdef FEAT_VARTABS
3846 if (p_vsts)
3847 free_string_option(p_vsts);
3848 p_vsts = empty_option;
3849#endif
3850 }
3851
3852 // Paste switched from on to off: Restore saved values.
3853 else if (old_p_paste)
3854 {
3855 // restore options for each buffer
3856 FOR_ALL_BUFFERS(buf)
3857 {
3858 buf->b_p_tw = buf->b_p_tw_nopaste;
3859 buf->b_p_wm = buf->b_p_wm_nopaste;
3860 buf->b_p_sts = buf->b_p_sts_nopaste;
3861 buf->b_p_ai = buf->b_p_ai_nopaste;
3862 buf->b_p_et = buf->b_p_et_nopaste;
3863#ifdef FEAT_VARTABS
3864 if (buf->b_p_vsts)
3865 free_string_option(buf->b_p_vsts);
3866 buf->b_p_vsts = buf->b_p_vsts_nopaste
3867 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3868 vim_free(buf->b_p_vsts_array);
3869 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3870 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3871 else
3872 buf->b_p_vsts_array = NULL;
3873#endif
3874 }
3875
3876 // restore global options
3877 p_sm = save_sm;
3878 p_sta = save_sta;
3879 if (p_ru != save_ru)
3880 status_redraw_all(); // redraw to draw the ruler
3881 p_ru = save_ru;
3882#ifdef FEAT_RIGHTLEFT
3883 p_ri = save_ri;
3884 p_hkmap = save_hkmap;
3885#endif
3886 // set global values for local buffer options
3887 p_ai = p_ai_nopaste;
3888 p_et = p_et_nopaste;
3889 p_sts = p_sts_nopaste;
3890 p_tw = p_tw_nopaste;
3891 p_wm = p_wm_nopaste;
3892#ifdef FEAT_VARTABS
3893 if (p_vsts)
3894 free_string_option(p_vsts);
3895 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3896#endif
3897 }
3898
3899 old_p_paste = p_paste;
3900
3901 return NULL;
3902}
3903
3904
3905#ifdef FEAT_QUICKFIX
3906/*
3907 * Process the updated 'previewwindow' option value.
3908 */
3909 char *
3910did_set_previewwindow(optset_T *args)
3911{
3912 if (!curwin->w_p_pvw)
3913 return NULL;
3914
3915 // There can be only one window with 'previewwindow' set.
3916 win_T *win;
3917
3918 FOR_ALL_WINDOWS(win)
3919 if (win->w_p_pvw && win != curwin)
3920 {
3921 curwin->w_p_pvw = FALSE;
3922 args->os_doskip = TRUE;
3923 return e_preview_window_already_exists;
3924 }
3925
3926 return NULL;
3927}
3928#endif
3929
3930#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3931/*
3932 * Process the new 'pyxversion' option value.
3933 */
3934 char *
3935did_set_pyxversion(optset_T *args UNUSED)
3936{
3937 char *errmsg = NULL;
3938
3939 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3940 errmsg = e_invalid_argument;
3941
3942 return errmsg;
3943}
3944#endif
3945
3946/*
3947 * Process the updated 'readonly' option value.
3948 */
3949 char *
3950did_set_readonly(optset_T *args)
3951{
3952 // when 'readonly' is reset globally, also reset readonlymode
3953 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3954 readonlymode = FALSE;
3955
3956 // when 'readonly' is set may give W10 again
3957 if (curbuf->b_p_ro)
3958 curbuf->b_did_warn = FALSE;
3959
3960 redraw_titles();
3961
3962 return NULL;
3963}
3964
3965/*
3966 * Process the updated 'scrollbind' option value.
3967 */
3968 char *
3969did_set_scrollbind(optset_T *args UNUSED)
3970{
3971 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3972 // at the end of normal_cmd()
3973 if (!curwin->w_p_scb)
3974 return NULL;
3975
3976 do_check_scrollbind(FALSE);
3977 curwin->w_scbind_pos = curwin->w_topline;
3978 return NULL;
3979}
3980
3981#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3982/*
3983 * Process the updated 'shellslash' option value.
3984 */
3985 char *
3986did_set_shellslash(optset_T *args UNUSED)
3987{
3988 if (p_ssl)
3989 {
3990 psepc = '/';
3991 psepcN = '\\';
3992 pseps[0] = '/';
3993 }
3994 else
3995 {
3996 psepc = '\\';
3997 psepcN = '/';
3998 pseps[0] = '\\';
3999 }
4000
4001 // need to adjust the file name arguments and buffer names.
4002 buflist_slash_adjust();
4003 alist_slash_adjust();
4004# ifdef FEAT_EVAL
4005 scriptnames_slash_adjust();
4006# endif
4007 return NULL;
4008}
4009#endif
4010
4011/*
4012 * Process the new 'shiftwidth' or the 'tabstop' option value.
4013 */
4014 char *
4015did_set_shiftwidth_tabstop(optset_T *args)
4016{
4017 long *pp = (long *)args->os_varp;
4018 char *errmsg = NULL;
4019
4020 if (curbuf->b_p_sw < 0)
4021 {
4022 errmsg = e_argument_must_be_positive;
4023#ifdef FEAT_VARTABS
4024 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4025 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4026 ? tabstop_first(curbuf->b_p_vts_array)
4027 : curbuf->b_p_ts;
4028#else
4029 curbuf->b_p_sw = curbuf->b_p_ts;
4030#endif
4031 }
4032
4033#ifdef FEAT_FOLDING
4034 if (foldmethodIsIndent(curwin))
4035 foldUpdateAll(curwin);
4036#endif
4037 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4038 // parse 'cinoptions'.
4039 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4040 parse_cino(curbuf);
4041
4042 return errmsg;
4043}
4044
4045/*
4046 * Process the new 'showtabline' option value.
4047 */
4048 char *
4049did_set_showtabline(optset_T *args UNUSED)
4050{
4051 shell_new_rows(); // recompute window positions and heights
4052 return NULL;
4053}
4054
4055/*
4056 * Process the updated 'smoothscroll' option value.
4057 */
4058 char *
4059did_set_smoothscroll(optset_T *args UNUSED)
4060{
4061 if (curwin->w_p_sms)
4062 return NULL;
4063
4064 curwin->w_skipcol = 0;
4065 changed_line_abv_curs();
4066 return NULL;
4067}
4068
4069#if defined(FEAT_SPELL) || defined(PROTO)
4070/*
4071 * Process the updated 'spell' option value.
4072 */
4073 char *
4074did_set_spell(optset_T *args UNUSED)
4075{
4076 if (curwin->w_p_spell)
4077 return parse_spelllang(curwin);
4078
4079 return NULL;
4080}
4081#endif
4082
4083/*
4084 * Process the updated 'swapfile' option value.
4085 */
4086 char *
4087did_set_swapfile(optset_T *args UNUSED)
4088{
4089 // when 'swf' is set, create swapfile, when reset remove swapfile
4090 if (curbuf->b_p_swf && p_uc)
4091 ml_open_file(curbuf); // create the swap file
4092 else
4093 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4094 // buf->b_p_swf
4095 mf_close_file(curbuf, TRUE); // remove the swap file
4096 return NULL;
4097}
4098
4099#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004100 char *
4101did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004102{
4103# ifdef FEAT_VTP
4104 // Do not turn on 'tgc' when 24-bit colors are not supported.
4105 if (
4106# ifdef VIMDLL
4107 !gui.in_use && !gui.starting &&
4108# endif
4109 !has_vtp_working())
4110 {
4111 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004112 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004113 return e_24_bit_colors_are_not_supported_on_this_environment;
4114 }
4115 if (is_term_win32())
4116 swap_tcap();
4117# endif
4118# ifdef FEAT_GUI
4119 if (!gui.in_use && !gui.starting)
4120# endif
4121 highlight_gui_started();
4122# ifdef FEAT_VTP
4123 // reset t_Co
4124 if (is_term_win32())
4125 {
4126 control_console_color_rgb();
4127 set_termname(T_NAME);
4128 init_highlight(TRUE, FALSE);
4129 }
4130# endif
4131# ifdef FEAT_TERMINAL
4132 term_update_colors_all();
4133 term_update_palette_all();
4134 term_update_wincolor_all();
4135# endif
4136
4137 return NULL;
4138}
4139#endif
4140
4141/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004142 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004144 char *
4145did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004147 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004149 // when 'terse' is set change 'shortmess'
4150 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004152 // insert 's' in p_shm
4153 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004154 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004155 STRCPY(IObuff, p_shm);
4156 STRCAT(IObuff, "s");
4157 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004158 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004159 // remove 's' from p_shm
4160 else if (!p_terse && p != NULL)
4161 STRMOVE(p, p + 1);
4162 return NULL;
4163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004165/*
4166 * Process the updated 'textauto' option value.
4167 */
4168 char *
4169did_set_textauto(optset_T *args)
4170{
4171 // when 'textauto' is set or reset also change 'fileformats'
4172 set_string_option_direct((char_u *)"ffs", -1,
4173 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4174 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004175
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004176 return NULL;
4177}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004179/*
4180 * Process the updated 'textmode' option value.
4181 */
4182 char *
4183did_set_textmode(optset_T *args)
4184{
4185 // when 'textmode' is set or reset also change 'fileformat'
4186 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4187
4188 return NULL;
4189}
4190
4191/*
4192 * Process the new 'textwidth' option value.
4193 */
4194 char *
4195did_set_textwidth(optset_T *args UNUSED)
4196{
4197 char *errmsg = NULL;
4198
4199 if (curbuf->b_p_tw < 0)
4200 {
4201 errmsg = e_argument_must_be_positive;
4202 curbuf->b_p_tw = 0;
4203 }
4204#ifdef FEAT_SYN_HL
4205 {
4206 win_T *wp;
4207 tabpage_T *tp;
4208
4209 FOR_ALL_TAB_WINDOWS(tp, wp)
4210 check_colorcolumn(wp);
4211 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004212#endif
4213
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004214 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215}
4216
4217/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004218 * Process the updated 'title' or the 'icon' option value.
4219 */
4220 char *
4221did_set_title_icon(optset_T *args UNUSED)
4222{
4223 // when 'title' changed, may need to change the title; same for 'icon'
4224 did_set_title();
4225 return NULL;
4226}
4227
4228/*
4229 * Process the new 'titlelen' option value.
4230 */
4231 char *
4232did_set_titlelen(optset_T *args)
4233{
4234 long old_value = args->os_oldval.number;
4235 char *errmsg = NULL;
4236
4237 // if 'titlelen' has changed, redraw the title
4238 if (p_titlelen < 0)
4239 {
4240 errmsg = e_argument_must_be_positive;
4241 p_titlelen = 85;
4242 }
4243 if (starting != NO_SCREEN && old_value != p_titlelen)
4244 need_maketitle = TRUE;
4245
4246 return errmsg;
4247}
4248
4249#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4250/*
4251 * Process the updated 'undofile' option value.
4252 */
4253 char *
4254did_set_undofile(optset_T *args)
4255{
4256 // Only take action when the option was set.
4257 if (!curbuf->b_p_udf && !p_udf)
4258 return NULL;
4259
4260 // When reset we do not delete the undo file, the option may be set again
4261 // without making any changes in between.
4262 char_u hash[UNDO_HASH_SIZE];
4263 buf_T *save_curbuf = curbuf;
4264
4265 FOR_ALL_BUFFERS(curbuf)
4266 {
4267 // When 'undofile' is set globally: for every buffer, otherwise
4268 // only for the current buffer: Try to read in the undofile,
4269 // if one exists, the buffer wasn't changed and the buffer was
4270 // loaded
4271 if ((curbuf == save_curbuf
4272 || (args->os_flags & OPT_GLOBAL)
4273 || args->os_flags == 0)
4274 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4275 {
4276#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004277 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004278 continue;
4279#endif
4280 u_compute_hash(hash);
4281 u_read_undo(NULL, hash, curbuf->b_fname);
4282 }
4283 }
4284 curbuf = save_curbuf;
4285
4286 return NULL;
4287}
4288#endif
4289
4290/*
4291 * Process the new global 'undolevels' option value.
4292 */
4293 static void
4294update_global_undolevels(long value, long old_value)
4295{
4296 // sync undo before 'undolevels' changes
4297
4298 // use the old value, otherwise u_sync() may not work properly
4299 p_ul = old_value;
4300 u_sync(TRUE);
4301 p_ul = value;
4302}
4303
4304/*
4305 * Process the new buffer local 'undolevels' option value.
4306 */
4307 static void
4308update_buflocal_undolevels(long value, long old_value)
4309{
4310 // use the old value, otherwise u_sync() may not work properly
4311 curbuf->b_p_ul = old_value;
4312 u_sync(TRUE);
4313 curbuf->b_p_ul = value;
4314}
4315
4316/*
4317 * Process the new 'undolevels' option value.
4318 */
4319 char *
4320did_set_undolevels(optset_T *args)
4321{
4322 long *pp = (long *)args->os_varp;
4323
4324 if (pp == &p_ul) // global 'undolevels'
4325 update_global_undolevels(args->os_newval.number,
4326 args->os_oldval.number);
4327 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4328 update_buflocal_undolevels(args->os_newval.number,
4329 args->os_oldval.number);
4330
4331 return NULL;
4332}
4333
4334/*
4335 * Process the new 'updatecount' option value.
4336 */
4337 char *
4338did_set_updatecount(optset_T *args)
4339{
4340 long old_value = args->os_oldval.number;
4341 char *errmsg = NULL;
4342
4343 // when 'updatecount' changes from zero to non-zero, open swap files
4344 if (p_uc < 0)
4345 {
4346 errmsg = e_argument_must_be_positive;
4347 p_uc = 100;
4348 }
4349 if (p_uc && !old_value)
4350 ml_open_files();
4351
4352 return errmsg;
4353}
4354
4355/*
4356 * Process the updated 'weirdinvert' option value.
4357 */
4358 char *
4359did_set_weirdinvert(optset_T *args)
4360{
4361 // When 'weirdinvert' changed, set/reset 't_xs'.
4362 // Then set 'weirdinvert' according to value of 't_xs'.
4363 if (p_wiv && !args->os_oldval.boolean)
4364 T_XS = (char_u *)"y";
4365 else if (!p_wiv && args->os_oldval.boolean)
4366 T_XS = empty_option;
4367 p_wiv = (*T_XS != NUL);
4368
4369 return NULL;
4370}
4371
4372/*
4373 * Process the new 'window' option value.
4374 */
4375 char *
4376did_set_window(optset_T *args UNUSED)
4377{
4378 if (p_window < 1)
4379 p_window = 1;
4380 else if (p_window >= Rows)
4381 p_window = Rows - 1;
4382 return NULL;
4383}
4384
4385/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004386 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004388 char *
4389did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004391 long *pp = (long *)args->os_varp;
4392 char *errmsg = NULL;
4393
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004394 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004396 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004397 p_wh = 1;
4398 }
4399 if (p_wmh > p_wh)
4400 {
4401 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4402 p_wh = p_wmh;
4403 }
4404 if (p_hh < 0)
4405 {
4406 errmsg = e_argument_must_be_positive;
4407 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004410 // Change window height NOW
4411 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004413 if (pp == &p_wh && curwin->w_height < p_wh)
4414 win_setheight((int)p_wh);
4415 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4416 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004419 return errmsg;
4420}
4421
4422/*
4423 * Process the new 'winminheight' option value.
4424 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004425 char *
4426did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004427{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004428 char *errmsg = NULL;
4429
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004430 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004432 errmsg = e_argument_must_be_positive;
4433 p_wmh = 0;
4434 }
4435 if (p_wmh > p_wh)
4436 {
4437 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4438 p_wmh = p_wh;
4439 }
4440 win_setminheight();
4441
4442 return errmsg;
4443}
4444
4445/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004446 * Process the new 'winminwidth' option value.
4447 */
4448 char *
4449did_set_winminwidth(optset_T *args UNUSED)
4450{
4451 char *errmsg = NULL;
4452
4453 if (p_wmw < 0)
4454 {
4455 errmsg = e_argument_must_be_positive;
4456 p_wmw = 0;
4457 }
4458 if (p_wmw > p_wiw)
4459 {
4460 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4461 p_wmw = p_wiw;
4462 }
4463 win_setminwidth();
4464
4465 return errmsg;
4466}
4467
4468/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004469 * Process the new 'winwidth' option value.
4470 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004471 char *
4472did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004473{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004474 char *errmsg = NULL;
4475
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004476 if (p_wiw < 1)
4477 {
4478 errmsg = e_argument_must_be_positive;
4479 p_wiw = 1;
4480 }
4481 if (p_wmw > p_wiw)
4482 {
4483 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4484 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004486
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004487 // Change window width NOW
4488 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4489 win_setwidth((int)p_wiw);
4490
4491 return errmsg;
4492}
4493
4494/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004495 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004496 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004497 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004498did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004499{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004500 // If 'wrap' is set, set w_leftcol to zero.
4501 if (curwin->w_p_wrap)
4502 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004503 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004504}
4505
4506/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004507 * Set the value of a boolean option, and take care of side effects.
4508 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004509 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004510 static char *
4511set_bool_option(
4512 int opt_idx, // index in options[] table
4513 char_u *varp, // pointer to the option variable
4514 int value, // new value
4515 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004516{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004517 int old_value = *(int *)varp;
4518#if defined(FEAT_EVAL)
4519 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004521 char *errmsg = NULL;
4522
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004523 // Disallow changing some options from secure mode
4524 if ((secure
4525#ifdef HAVE_SANDBOX
4526 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004527#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004528 ) && (options[opt_idx].flags & P_SECURE))
4529 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004530
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004531#if defined(FEAT_EVAL)
4532 // Save the global value before changing anything. This is needed as for
4533 // a global-only option setting the "local value" in fact sets the global
4534 // value (since there is only one value).
4535 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4536 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4537 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004539
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004540 *(int *)varp = value; // set the new value
4541#ifdef FEAT_EVAL
4542 // Remember where the option was set.
4543 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004544#endif
4545
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004547 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004550 // May set global value for local option.
4551 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4552 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004553
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004554 // Handle side effects of changing a bool option.
4555 if (options[opt_idx].opt_did_set_cb != NULL)
4556 {
4557 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004558
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004559 CLEAR_FIELD(args);
4560 args.os_varp = varp;
4561 args.os_flags = opt_flags;
4562 args.os_oldval.boolean = old_value;
4563 args.os_newval.boolean = value;
4564 args.os_errbuf = NULL;
4565 errmsg = options[opt_idx].opt_did_set_cb(&args);
4566 if (args.os_doskip)
4567 return errmsg;
4568 }
4569
4570 // after handling side effects, call autocommand
4571
4572 options[opt_idx].flags |= P_WAS_SET;
4573
4574#if defined(FEAT_EVAL)
4575 apply_optionset_autocmd(opt_idx, opt_flags,
4576 (long)(old_value ? TRUE : FALSE),
4577 (long)(old_global_value ? TRUE : FALSE),
4578 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004579#endif
4580
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004581 comp_col(); // in case 'ruler' or 'showcmd' changed
4582 if (curwin->w_curswant != MAXCOL
4583 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4584 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004585
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004586 if ((opt_flags & OPT_NO_REDRAW) == 0)
4587 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004588
4589 return errmsg;
4590}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004591
4592/*
4593 * Check the bounds of numeric options.
4594 */
4595 static char *
4596check_num_option_bounds(
4597 long *pp,
4598 long old_value,
4599 long old_Rows,
4600 long old_Columns,
4601 char *errbuf,
4602 size_t errbuflen,
4603 char *errmsg)
4604{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (Rows < min_rows() && full_screen)
4606 {
4607 if (errbuf != NULL)
4608 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004609 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004610 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 errmsg = errbuf;
4612 }
4613 Rows = min_rows();
4614 }
4615 if (Columns < MIN_COLUMNS && full_screen)
4616 {
4617 if (errbuf != NULL)
4618 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004619 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004620 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 errmsg = errbuf;
4622 }
4623 Columns = MIN_COLUMNS;
4624 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004625 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004627 // If the screen (shell) height has been changed, assume it is the
4628 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (old_Rows != Rows || old_Columns != Columns)
4630 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004631 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (updating_screen)
4633 *pp = old_value;
4634 else if (full_screen
4635#ifdef FEAT_GUI
4636 && !gui.starting
4637#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004638 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 set_shellsize((int)Columns, (int)Rows, TRUE);
4640 else
4641 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004642 // Postpone the resizing; check the size and cmdline position for
4643 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 check_shellsize();
4645 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4646 cmdline_row = Rows - p_ch;
4647 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004648 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004649 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if (curbuf->b_p_ts <= 0)
4653 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004654 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 curbuf->b_p_ts = 8;
4656 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004657 else if (curbuf->b_p_ts > TABSTOP_MAX)
4658 {
4659 errmsg = e_invalid_argument;
4660 curbuf->b_p_ts = 8;
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (p_tm < 0)
4663 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004664 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 p_tm = 0;
4666 }
4667 if ((curwin->w_p_scr <= 0
4668 || (curwin->w_p_scr > curwin->w_height
4669 && curwin->w_height > 0))
4670 && full_screen)
4671 {
4672 if (pp == &(curwin->w_p_scr))
4673 {
4674 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004675 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 win_comp_scroll(curwin);
4677 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // If 'scroll' became invalid because of a side effect silently adjust
4679 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 else if (curwin->w_p_scr <= 0)
4681 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004682 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 curwin->w_p_scr = curwin->w_height;
4684 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004685 if (p_hi < 0)
4686 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004687 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004688 p_hi = 0;
4689 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004690 else if (p_hi > 10000)
4691 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004692 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004693 p_hi = 10000;
4694 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004695 if (p_re < 0 || p_re > 2)
4696 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004697 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004698 p_re = 0;
4699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 if (p_report < 0)
4701 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004702 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 p_report = 1;
4704 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004705 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004707 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 p_sj = Rows / 2;
4709 else
4710 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004711 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 p_sj = 1;
4713 }
4714 }
4715 if (p_so < 0 && full_screen)
4716 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004717 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 p_so = 0;
4719 }
4720 if (p_siso < 0 && full_screen)
4721 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004722 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 p_siso = 0;
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (p_cwh < 1)
4726 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004727 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 p_cwh = 1;
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (p_ut < 0)
4731 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004732 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 p_ut = 2000;
4734 }
4735 if (p_ss < 0)
4736 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004737 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 p_ss = 0;
4739 }
4740
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004741 return errmsg;
4742}
4743
4744/*
4745 * Set the value of a number option, and take care of side effects.
4746 * Returns NULL for success, or an error message for an error.
4747 */
4748 static char *
4749set_num_option(
4750 int opt_idx, // index in options[] table
4751 char_u *varp, // pointer to the option variable
4752 long value, // new value
4753 char *errbuf, // buffer for error messages
4754 size_t errbuflen, // length of "errbuf"
4755 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4756 // OPT_MODELINE, etc.
4757{
4758 char *errmsg = NULL;
4759 long old_value = *(long *)varp;
4760#if defined(FEAT_EVAL)
4761 long old_global_value = 0; // only used when setting a local and
4762 // global option
4763#endif
4764 long old_Rows = Rows; // remember old Rows
4765 long old_Columns = Columns; // remember old Columns
4766 long *pp = (long *)varp;
4767
4768 // Disallow changing some options from secure mode.
4769 if ((secure
4770#ifdef HAVE_SANDBOX
4771 || sandbox != 0
4772#endif
4773 ) && (options[opt_idx].flags & P_SECURE))
4774 return e_not_allowed_here;
4775
4776#if defined(FEAT_EVAL)
4777 // Save the global value before changing anything. This is needed as for
4778 // a global-only option setting the "local value" in fact sets the global
4779 // value (since there is only one value).
4780 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4781 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4782 OPT_GLOBAL);
4783#endif
4784
4785 *pp = value;
4786#ifdef FEAT_EVAL
4787 // Remember where the option was set.
4788 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4789#endif
4790#ifdef FEAT_GUI
4791 need_mouse_correct = TRUE;
4792#endif
4793
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004794 // Invoke the option specific callback function to validate and apply the
4795 // new value.
4796 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004797 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004798 optset_T args;
4799
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004800 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004801 args.os_varp = varp;
4802 args.os_flags = opt_flags;
4803 args.os_oldval.number = old_value;
4804 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004805 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004806 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004807 }
4808
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004809 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004810 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4811 errbuf, errbuflen, errmsg);
4812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4815 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4816
4817 options[opt_idx].flags |= P_WAS_SET;
4818
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004819#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004820 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4821 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004822#endif
4823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004824 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004825 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004826 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004827 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004828 if ((opt_flags & OPT_NO_REDRAW) == 0)
4829 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
4831 return errmsg;
4832}
4833
4834/*
4835 * Called after an option changed: check if something needs to be redrawn.
4836 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004838check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004840 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004841 int doclear = (flags & P_RCLR) == P_RCLR;
4842 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004844 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4848 changed_window_setting();
4849 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004850 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004851 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004852 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004853 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004854 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004856 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857}
4858
4859/*
4860 * Find index for option 'arg'.
4861 * Return -1 if not found.
4862 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004863 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004864findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865{
4866 int opt_idx;
4867 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 int is_term_opt;
4870
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004871 // For first call: Initialize the quick-access table.
4872 // It contains the index for the first option that starts with a certain
4873 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (quick_tab[1] == 0)
4875 {
4876 p = options[0].fullname;
4877 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4878 {
4879 if (s[0] != p[0])
4880 {
4881 if (s[0] == 't' && s[1] == '_')
4882 quick_tab[26] = opt_idx;
4883 else
4884 quick_tab[CharOrdLow(s[0])] = opt_idx;
4885 }
4886 p = s;
4887 }
4888 }
4889
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004890 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return -1;
4893
4894 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4895 if (is_term_opt)
4896 opt_idx = quick_tab[26];
4897 else
4898 opt_idx = quick_tab[CharOrdLow(arg[0])];
4899 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004901 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 break;
4903 }
4904 if (s == NULL && !is_term_opt)
4905 {
4906 opt_idx = quick_tab[CharOrdLow(arg[0])];
4907 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4908 {
4909 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004910 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 break;
4912 s = NULL;
4913 }
4914 }
4915 if (s == NULL)
4916 opt_idx = -1;
4917 return opt_idx;
4918}
4919
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004920#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4921 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922/*
4923 * Get the value for an option.
4924 *
4925 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004926 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004927 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004928 * String option: gov_string, *stringval gets allocated string.
4929 * Hidden Number option: gov_hidden_number.
4930 * Hidden Toggle option: gov_hidden_bool.
4931 * Hidden String option: gov_hidden_string.
4932 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004933 *
4934 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004936 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004937get_option_value(
4938 char_u *name,
4939 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004940 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004941 int *flagsp,
4942 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
4944 int opt_idx;
4945 char_u *varp;
4946
4947 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004948 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004949 {
4950 int key;
4951
4952 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004953 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004954 {
4955 char_u key_name[2];
4956 char_u *p;
4957
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004958 if (flagsp != NULL)
4959 *flagsp = 0; // terminal option has no flags
4960
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004961 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004962 if (key < 0)
4963 {
4964 key_name[0] = KEY2TERMCAP0(key);
4965 key_name[1] = KEY2TERMCAP1(key);
4966 }
4967 else
4968 {
4969 key_name[0] = KS_KEY;
4970 key_name[1] = (key & 0xff);
4971 }
4972 p = find_termcode(key_name);
4973 if (p != NULL)
4974 {
4975 if (stringval != NULL)
4976 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004977 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004978 }
4979 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004980 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004983 varp = get_varp_scope(&(options[opt_idx]), scope);
4984
4985 if (flagsp != NULL)
4986 // Return the P_xxxx option flags.
4987 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 if (options[opt_idx].flags & P_STRING)
4990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004991 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004992 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 if (stringval != NULL)
4994 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004995 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004996 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4997 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005000 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005001 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 *stringval = vim_strsave(*(char_u **)(varp));
5006 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005007 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005010 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005011 return (options[opt_idx].flags & P_NUM)
5012 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (options[opt_idx].flags & P_NUM)
5014 *numval = *(long *)varp;
5015 else
5016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005017 // Special case: 'modified' is b_changed, but we also want to consider
5018 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if ((int *)varp == &curbuf->b_changed)
5020 *numval = curbufIsChanged();
5021 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005022 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005024 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025}
5026#endif
5027
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005028#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005029/*
5030 * Returns the option attributes and its value. Unlike the above function it
5031 * will return either global value or local value of the option depending on
5032 * what was requested, but it will never return global value if it was
5033 * requested to return local one and vice versa. Neither it will return
5034 * buffer-local value if it was requested to return window-local one.
5035 *
5036 * Pretends that option is absent if it is not present in the requested scope
5037 * (i.e. has no global, window-local or buffer-local value depending on
5038 * opt_type). Uses
5039 *
5040 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005041 * 0 hidden or unknown option, also option that does not have requested
5042 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005043 * see SOPT_* in vim.h for other flags
5044 *
5045 * Possible opt_type values: see SREQ_* in vim.h
5046 */
5047 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005048get_option_value_strict(
5049 char_u *name,
5050 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005051 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005052 int opt_type,
5053 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005054{
5055 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005056 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005057 struct vimoption *p;
5058 int r = 0;
5059
5060 opt_idx = findoption(name);
5061 if (opt_idx < 0)
5062 return 0;
5063
5064 p = &(options[opt_idx]);
5065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005066 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005067 if (p->var == NULL)
5068 return 0;
5069
5070 if (p->flags & P_BOOL)
5071 r |= SOPT_BOOL;
5072 else if (p->flags & P_NUM)
5073 r |= SOPT_NUM;
5074 else if (p->flags & P_STRING)
5075 r |= SOPT_STRING;
5076
5077 if (p->indir == PV_NONE)
5078 {
5079 if (opt_type == SREQ_GLOBAL)
5080 r |= SOPT_GLOBAL;
5081 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005082 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083 }
5084 else
5085 {
5086 if (p->indir & PV_BOTH)
5087 r |= SOPT_GLOBAL;
5088 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005089 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005090
5091 if (p->indir & PV_WIN)
5092 {
5093 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005094 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005095 else
5096 r |= SOPT_WIN;
5097 }
5098 else if (p->indir & PV_BUF)
5099 {
5100 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005101 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102 else
5103 r |= SOPT_BUF;
5104 }
5105 }
5106
5107 if (stringval == NULL)
5108 return r;
5109
5110 if (opt_type == SREQ_GLOBAL)
5111 varp = p->var;
5112 else
5113 {
5114 if (opt_type == SREQ_BUF)
5115 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005116 // Special case: 'modified' is b_changed, but we also want to
5117 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005118 if (p->indir == PV_MOD)
5119 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005120 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005121 varp = NULL;
5122 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005123# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005124 else if (p->indir == PV_KEY)
5125 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005126 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005127 *stringval = NULL;
5128 varp = NULL;
5129 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005130# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 else
5132 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005133 buf_T *save_curbuf = curbuf;
5134
5135 // only getting a pointer, no need to use aucmd_prepbuf()
5136 curbuf = (buf_T *)from;
5137 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005138 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005139 curbuf = save_curbuf;
5140 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005141 }
5142 }
5143 else if (opt_type == SREQ_WIN)
5144 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005145 win_T *save_curwin = curwin;
5146
5147 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005148 curbuf = curwin->w_buffer;
5149 varp = get_varp(p);
5150 curwin = save_curwin;
5151 curbuf = curwin->w_buffer;
5152 }
5153 if (varp == p->var)
5154 return (r | SOPT_UNSET);
5155 }
5156
5157 if (varp != NULL)
5158 {
5159 if (p->flags & P_STRING)
5160 *stringval = vim_strsave(*(char_u **)(varp));
5161 else if (p->flags & P_NUM)
5162 *numval = *(long *) varp;
5163 else
5164 *numval = *(int *)varp;
5165 }
5166
5167 return r;
5168}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005169
5170/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005171 * Iterate over options. First argument is a pointer to a pointer to a
5172 * structure inside options[] array, second is option type like in the above
5173 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005174 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005175 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005176 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005177 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005178 * first argument to NULL.
5179 *
5180 * Returns full option name for current option on each call.
5181 */
5182 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005183option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005184{
5185 struct vimoption *ret = NULL;
5186 do
5187 {
5188 if (*option == NULL)
5189 *option = (void *) options;
5190 else if (((struct vimoption *) (*option))->fullname == NULL)
5191 {
5192 *option = NULL;
5193 return NULL;
5194 }
5195 else
5196 *option = (void *) (((struct vimoption *) (*option)) + 1);
5197
5198 ret = ((struct vimoption *) (*option));
5199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005200 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005201 if (ret->var == NULL)
5202 {
5203 ret = NULL;
5204 continue;
5205 }
5206
5207 switch (opt_type)
5208 {
5209 case SREQ_GLOBAL:
5210 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5211 ret = NULL;
5212 break;
5213 case SREQ_BUF:
5214 if (!(ret->indir & PV_BUF))
5215 ret = NULL;
5216 break;
5217 case SREQ_WIN:
5218 if (!(ret->indir & PV_WIN))
5219 ret = NULL;
5220 break;
5221 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005222 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005223 return NULL;
5224 }
5225 }
5226 while (ret == NULL);
5227
5228 return (char_u *)ret->fullname;
5229}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005230#endif
5231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005233 * Return the flags for the option at 'opt_idx'.
5234 */
5235 long_u
5236get_option_flags(int opt_idx)
5237{
5238 return options[opt_idx].flags;
5239}
5240
5241/*
5242 * Set a flag for the option at 'opt_idx'.
5243 */
5244 void
5245set_option_flag(int opt_idx, long_u flag)
5246{
5247 options[opt_idx].flags |= flag;
5248}
5249
5250/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005251 * Returns TRUE if the option at 'opt_idx' is a global option
5252 */
5253 int
5254is_global_option(int opt_idx)
5255{
5256 return options[opt_idx].indir == PV_NONE;
5257}
5258
5259/*
5260 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5261 * local value.
5262 */
5263 int
5264is_global_local_option(int opt_idx)
5265{
5266 return options[opt_idx].indir & PV_BOTH;
5267}
5268
5269/*
5270 * Returns TRUE if the option at 'opt_idx' is a window-local option
5271 */
5272 int
5273is_window_local_option(int opt_idx)
5274{
5275 return options[opt_idx].var == VAR_WIN;
5276}
5277
5278/*
5279 * Returns TRUE if the option at 'opt_idx' is a hidden option
5280 */
5281 int
5282is_hidden_option(int opt_idx)
5283{
5284 return options[opt_idx].var == NULL;
5285}
5286
5287#if defined(FEAT_CRYPT) || defined(PROTO)
5288/*
5289 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5290 */
5291 int
5292is_crypt_key_option(int opt_idx)
5293{
5294 return options[opt_idx].indir == PV_KEY;
5295}
5296#endif
5297
5298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * Set the value of option "name".
5300 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005301 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005302 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005304 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005305set_option_value(
5306 char_u *name,
5307 long number,
5308 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005309 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
5311 int opt_idx;
5312 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005313 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005314 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005317 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005318 {
5319 int key;
5320
5321 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005322 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005323 {
5324 char_u key_name[2];
5325
5326 if (key < 0)
5327 {
5328 key_name[0] = KEY2TERMCAP0(key);
5329 key_name[1] = KEY2TERMCAP1(key);
5330 }
5331 else
5332 {
5333 key_name[0] = KS_KEY;
5334 key_name[1] = (key & 0xff);
5335 }
5336 add_termcode(key_name, string, FALSE);
5337 if (full_screen)
5338 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005339 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005340 return NULL;
5341 }
5342
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005343 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 else
5346 {
5347 flags = options[opt_idx].flags;
5348#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005349 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005351 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005352 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005353 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005356 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005357 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005358
5359 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5360 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005362 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005364 int idx;
5365
5366 // Either we are given a string or we are setting option
5367 // to zero.
5368 for (idx = 0; string[idx] == '0'; ++idx)
5369 ;
5370 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005371 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005372 // There's another character after zeros or the string
5373 // is empty. In both cases, we are trying to set a
5374 // num option using a string.
5375 semsg(_(e_number_required_after_str_equal_str),
5376 name, string);
5377 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005378
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005381 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005382 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005383 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005384 errbuf, sizeof(errbuf), opt_flags);
5385 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005386 else
5387 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005389
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005391 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392}
5393
5394/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005395 * Call set_option_value() and when an error is returned report it.
5396 */
5397 void
5398set_option_value_give_err(
5399 char_u *name,
5400 long number,
5401 char_u *string,
5402 int opt_flags) // OPT_LOCAL or 0 (both)
5403{
5404 char *errmsg = set_option_value(name, number, string, opt_flags);
5405
5406 if (errmsg != NULL)
5407 emsg(_(errmsg));
5408}
5409
5410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 * Get the terminal code for a terminal option.
5412 * Returns NULL when not found.
5413 */
5414 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005415get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 int opt_idx;
5418 char_u *varp;
5419
5420 if (tname[0] != 't' || tname[1] != '_' ||
5421 tname[2] == NUL || tname[3] == NUL)
5422 return NULL;
5423 if ((opt_idx = findoption(tname)) >= 0)
5424 {
5425 varp = get_varp(&(options[opt_idx]));
5426 if (varp != NULL)
5427 varp = *(char_u **)(varp);
5428 return varp;
5429 }
5430 return find_termcode(tname + 2);
5431}
5432
5433 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005434get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 int i;
5437
5438 i = findoption((char_u *)"hl");
5439 if (i >= 0)
5440 return options[i].def_val[VI_DEFAULT];
5441 return (char_u *)NULL;
5442}
5443
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005444 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005445get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005446{
5447 int i;
5448
5449 i = findoption((char_u *)"enc");
5450 if (i >= 0)
5451 return options[i].def_val[VI_DEFAULT];
5452 return (char_u *)NULL;
5453}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005454
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005455#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005456 int
5457is_option_allocated(char *name)
5458{
5459 int idx = findoption((char_u *)name);
5460
5461 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5462}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005463#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005464
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005465#if defined(FEAT_EVAL) || defined(PROTO)
5466/*
5467 * Return TRUE if "name" is a string option.
5468 * Returns FALSE if option "name" does not exist.
5469 */
5470 int
5471is_string_option(char_u *name)
5472{
5473 int idx = findoption(name);
5474
5475 return idx >= 0 && (options[idx].flags & P_STRING);
5476}
5477#endif
5478
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479/*
5480 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005481 * When "has_lt" is true there is a '<' before "*arg_arg".
5482 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
5484 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005485find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005487 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005489 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005491 // Don't use get_special_key_code() for t_xx, we don't want it to call
5492 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5494 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005495 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005499 key = find_special_key(&arg, &modifiers,
5500 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005501 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 key = 0;
5503 }
5504 return key;
5505}
5506
5507/*
5508 * if 'all' == 0: show changed options
5509 * if 'all' == 1: show all normal options
5510 * if 'all' == 2: show all terminal options
5511 */
5512 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005513showoptions(
5514 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005515 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 struct vimoption *p;
5518 int col;
5519 int isterm;
5520 char_u *varp;
5521 struct vimoption **items;
5522 int item_count;
5523 int run;
5524 int row, rows;
5525 int cols;
5526 int i;
5527 int len;
5528
5529#define INC 20
5530#define GAP 3
5531
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005532 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (items == NULL)
5534 return;
5535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005536 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005538 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005540 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005542 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005544 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005546 // Do the loop two times:
5547 // 1. display the short items
5548 // 2. display the long items (only strings and numbers)
5549 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 for (run = 1; run <= 2 && !got_int; ++run)
5551 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005552 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 item_count = 0;
5554 for (p = &options[0]; p->fullname != NULL; p++)
5555 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005556 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005557 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005558 continue;
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 varp = NULL;
5561 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005562 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
5564 if (p->indir != PV_NONE && !isterm)
5565 varp = get_varp_scope(p, opt_flags);
5566 }
5567 else
5568 varp = get_varp(p);
5569 if (varp != NULL
5570 && ((all == 2 && isterm)
5571 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005572 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005574 if (opt_flags & OPT_ONECOLUMN)
5575 len = Columns;
5576 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005577 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 else
5579 {
5580 option_value2string(p, opt_flags);
5581 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5582 }
5583 if ((len <= INC - GAP && run == 1) ||
5584 (len > INC - GAP && run == 2))
5585 items[item_count++] = p;
5586 }
5587 }
5588
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005589 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 if (run == 1)
5591 {
5592 cols = (Columns + GAP - 3) / INC;
5593 if (cols == 0)
5594 cols = 1;
5595 rows = (item_count + cols - 1) / cols;
5596 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 rows = item_count;
5599 for (row = 0; row < rows && !got_int; ++row)
5600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005601 msg_putchar('\n'); // go to next line
5602 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 break;
5604 col = 0;
5605 for (i = row; i < item_count; i += rows)
5606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005607 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 showoneopt(items[i], opt_flags);
5609 col += INC;
5610 }
5611 out_flush();
5612 ui_breakcheck();
5613 }
5614 }
5615 vim_free(items);
5616}
5617
5618/*
5619 * Return TRUE if option "p" has its default value.
5620 */
5621 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005622optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 int dvi;
5625
5626 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005628 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005630 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005632 // the cast to long is required for Manx C, long_i is
5633 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005634 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005635 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5637}
5638
5639/*
5640 * showoneopt: show the value of one option
5641 * must not be called with a hidden option!
5642 */
5643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005644showoneopt(
5645 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005646 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005648 char_u *varp;
5649 int save_silent = silent_mode;
5650
5651 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005652 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 varp = get_varp_scope(p, opt_flags);
5655
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005656 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5658 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005659 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005661 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005663 msg_puts(" ");
5664 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 if (!(p->flags & P_BOOL))
5666 {
5667 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005668 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 option_value2string(p, opt_flags);
5670 msg_outtrans(NameBuff);
5671 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005672
5673 silent_mode = save_silent;
5674 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675}
5676
5677/*
5678 * Write modified options as ":set" commands to a file.
5679 *
5680 * There are three values for "opt_flags":
5681 * OPT_GLOBAL: Write global option values and fresh values of
5682 * buffer-local options (used for start of a session
5683 * file).
5684 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5685 * curwin (used for a vimrc file).
5686 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5687 * and local values for window-local options of
5688 * curwin. Local values are also written when at the
5689 * default value, because a modeline or autocommand
5690 * may have set them when doing ":edit file" and the
5691 * user has set them back at the default or fresh
5692 * value.
5693 * When "local_only" is TRUE, don't write fresh
5694 * values, only local values (for ":mkview").
5695 * (fresh value = value used for a new buffer or window for a local option).
5696 *
5697 * Return FAIL on error, OK otherwise.
5698 */
5699 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005700makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005703 char_u *varp; // currently used value
5704 char_u *varp_fresh; // local value
5705 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 char *cmd;
5707 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005708 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
5710 /*
5711 * The options that don't have a default (terminal name, columns, lines)
5712 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005713 * Do the loop over "options[]" twice: once for options with the
5714 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005716 for (pri = 1; pri >= 0; --pri)
5717 {
5718 for (p = &options[0]; !istermoption(p); p++)
5719 if (!(p->flags & P_NO_MKRC)
5720 && !istermoption(p)
5721 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005723 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5725 continue;
5726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005727 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5728 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5730 continue;
5731
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005732 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005734 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 continue;
5736
Bram Moolenaard23b7142021-04-17 21:04:34 +02005737 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5738 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005739 continue;
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 round = 2;
5742 if (p->indir != PV_NONE)
5743 {
5744 if (p->var == VAR_WIN)
5745 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005746 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (!(opt_flags & OPT_LOCAL))
5748 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005749 // When fresh value of window-local option is not at the
5750 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5752 {
5753 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005754 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
5756 round = 1;
5757 varp_local = varp;
5758 varp = varp_fresh;
5759 }
5760 }
5761 }
5762 }
5763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005764 // Round 1: fresh value for window-local options.
5765 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 for ( ; round <= 2; varp = varp_local, ++round)
5767 {
5768 if (round == 1 || (opt_flags & OPT_GLOBAL))
5769 cmd = "set";
5770 else
5771 cmd = "setlocal";
5772
5773 if (p->flags & P_BOOL)
5774 {
5775 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5776 return FAIL;
5777 }
5778 else if (p->flags & P_NUM)
5779 {
5780 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5781 return FAIL;
5782 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005783 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005785 int do_endif = FALSE;
5786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005787 // Don't set 'syntax' and 'filetype' again if the value is
5788 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005789 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005790#if defined(FEAT_SYN_HL)
5791 p->indir == PV_SYN ||
5792#endif
5793 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5796 *(char_u **)(varp)) < 0
5797 || put_eol(fd) < 0)
5798 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005799 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
5801 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005802 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005804 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
5806 if (put_line(fd, "endif") == FAIL)
5807 return FAIL;
5808 }
5809 }
5810 }
5811 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 return OK;
5814}
5815
5816#if defined(FEAT_FOLDING) || defined(PROTO)
5817/*
5818 * Generate set commands for the local fold options only. Used when
5819 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5820 */
5821 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005822makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005824 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005826 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 == FAIL
5828# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005829 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005831 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 == FAIL
5833 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5834 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5835 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5836 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5837 )
5838 return FAIL;
5839
5840 return OK;
5841}
5842#endif
5843
5844 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005845put_setstring(
5846 FILE *fd,
5847 char *cmd,
5848 char *name,
5849 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005850 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005853 char_u *buf = NULL;
5854 char_u *part = NULL;
5855 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5858 return FAIL;
5859 if (*valuep != NULL)
5860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005861 // Output 'pastetoggle' as key names. For other
5862 // options some characters have to be escaped with
5863 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (valuep == &p_pt)
5865 {
5866 s = *valuep;
5867 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005868 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 return FAIL;
5870 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005871 // expand the option value, replace $HOME by ~
5872 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005874 int size = (int)STRLEN(*valuep) + 1;
5875
5876 // replace home directory in the whole option value into "buf"
5877 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005878 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005879 goto fail;
5880 home_replace(NULL, *valuep, buf, size, FALSE);
5881
5882 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005883 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005884 // can be expanded when read back.
5885 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5886 && vim_strchr(*valuep, ',') != NULL)
5887 {
5888 part = alloc(size);
5889 if (part == NULL)
5890 goto fail;
5891
5892 // write line break to clear the option, e.g. ':set rtp='
5893 if (put_eol(fd) == FAIL)
5894 goto fail;
5895
5896 p = buf;
5897 while (*p != NUL)
5898 {
5899 // for each comma separated option part, append value to
5900 // the option, :set rtp+=value
5901 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5902 goto fail;
5903 (void)copy_option_part(&p, part, size, ",");
5904 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5905 goto fail;
5906 }
5907 vim_free(buf);
5908 vim_free(part);
5909 return OK;
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005912 {
5913 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005915 }
5916 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 }
5918 else if (put_escstr(fd, *valuep, 2) == FAIL)
5919 return FAIL;
5920 }
5921 if (put_eol(fd) < 0)
5922 return FAIL;
5923 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005924fail:
5925 vim_free(buf);
5926 vim_free(part);
5927 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928}
5929
5930 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005931put_setnum(
5932 FILE *fd,
5933 char *cmd,
5934 char *name,
5935 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 long wc;
5938
5939 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5940 return FAIL;
5941 if (wc_use_keyname((char_u *)valuep, &wc))
5942 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005943 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5945 return FAIL;
5946 }
5947 else if (fprintf(fd, "%ld", *valuep) < 0)
5948 return FAIL;
5949 if (put_eol(fd) < 0)
5950 return FAIL;
5951 return OK;
5952}
5953
5954 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005955put_setbool(
5956 FILE *fd,
5957 char *cmd,
5958 char *name,
5959 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005961 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005962 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5964 || put_eol(fd) < 0)
5965 return FAIL;
5966 return OK;
5967}
5968
5969/*
5970 * Clear all the terminal options.
5971 * If the option has been allocated, free the memory.
5972 * Terminal options are never hidden or indirect.
5973 */
5974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005975clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005977 // Reset a few things before clearing the old options. This may cause
5978 // outputting a few things that the terminal doesn't understand, but the
5979 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005980 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005981 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005983 // When starting the GUI close the display opened for the clipboard.
5984 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 if (gui.starting)
5986 clear_xterm_clip();
5987#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005988 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005990 free_termoptions();
5991}
5992
5993 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005994free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005995{
5996 struct vimoption *p;
5997
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005998 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (istermoption(p))
6000 {
6001 if (p->flags & P_ALLOCED)
6002 free_string_option(*(char_u **)(p->var));
6003 if (p->flags & P_DEF_ALLOCED)
6004 free_string_option(p->def_val[VI_DEFAULT]);
6005 *(char_u **)(p->var) = empty_option;
6006 p->def_val[VI_DEFAULT] = empty_option;
6007 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006008#ifdef FEAT_EVAL
6009 // remember where the option was cleared
6010 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 }
6013 clear_termcodes();
6014}
6015
6016/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006017 * Free the string for one term option, if it was allocated.
6018 * Set the string to empty_option and clear allocated flag.
6019 * "var" points to the option value.
6020 */
6021 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006022free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006023{
6024 struct vimoption *p;
6025
6026 for (p = &options[0]; p->fullname != NULL; p++)
6027 if (p->var == var)
6028 {
6029 if (p->flags & P_ALLOCED)
6030 free_string_option(*(char_u **)(p->var));
6031 *(char_u **)(p->var) = empty_option;
6032 p->flags &= ~P_ALLOCED;
6033 break;
6034 }
6035}
6036
6037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 * Set the terminal option defaults to the current value.
6039 * Used after setting the terminal name.
6040 */
6041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006042set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 struct vimoption *p;
6045
6046 for (p = &options[0]; p->fullname != NULL; p++)
6047 {
6048 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6049 {
6050 if (p->flags & P_DEF_ALLOCED)
6051 {
6052 free_string_option(p->def_val[VI_DEFAULT]);
6053 p->flags &= ~P_DEF_ALLOCED;
6054 }
6055 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6056 if (p->flags & P_ALLOCED)
6057 {
6058 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006059 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061 }
6062 }
6063}
6064
6065/*
6066 * return TRUE if 'p' starts with 't_'
6067 */
6068 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006069istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6072}
6073
Bram Moolenaardac13472019-09-16 21:06:21 +02006074/*
6075 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6076 */
6077 int
6078istermoption_idx(int opt_idx)
6079{
6080 return istermoption(&options[opt_idx]);
6081}
6082
Bram Moolenaar113e1072019-01-20 15:30:40 +01006083#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006085 * Unset local option value, similar to ":set opt<".
6086 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006087 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006088unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006089{
6090 struct vimoption *p;
6091 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006092 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006093
6094 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006095 if (opt_idx < 0)
6096 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006097 p = &(options[opt_idx]);
6098
6099 switch ((int)p->indir)
6100 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006101 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006102 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006103 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006104 break;
6105 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006106 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006107 break;
6108 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006109 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006110 break;
6111 case PV_AR:
6112 buf->b_p_ar = -1;
6113 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006114 case PV_BKC:
6115 clear_string_option(&buf->b_p_bkc);
6116 buf->b_bkc_flags = 0;
6117 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006118 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006119 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006120 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006121 case PV_TC:
6122 clear_string_option(&buf->b_p_tc);
6123 buf->b_tc_flags = 0;
6124 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006125 case PV_SISO:
6126 curwin->w_p_siso = -1;
6127 break;
6128 case PV_SO:
6129 curwin->w_p_so = -1;
6130 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006131# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006132 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006133 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006134 break;
6135 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006136 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006137 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006138# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006139 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006140 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006141 break;
6142 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006143 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006144 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006145# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006146 case PV_TSRFU:
6147 clear_string_option(&buf->b_p_tsrfu);
6148 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006149# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006150 case PV_FP:
6151 clear_string_option(&buf->b_p_fp);
6152 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006153# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006154 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006155 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006156 break;
6157 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006158 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006159 break;
6160 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006161 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006162 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006163# endif
6164# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006165 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006166 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006167 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006168# endif
6169# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006170 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006171 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006172 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006173# endif
6174# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006175 case PV_SBR:
6176 clear_string_option(&((win_T *)from)->w_p_sbr);
6177 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006178# endif
6179# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006180 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006181 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006182 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006183# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006184 case PV_UL:
6185 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6186 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006187 case PV_LW:
6188 clear_string_option(&buf->b_p_lw);
6189 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006190 case PV_MENC:
6191 clear_string_option(&buf->b_p_menc);
6192 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006193 case PV_LCS:
6194 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006195 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006196 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006197 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006198 case PV_FCS:
6199 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006200 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006201 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006202 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006203 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006204 clear_string_option(&((win_T *)from)->w_p_ve);
6205 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006206 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006207 }
6208}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006209#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006210
6211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006213 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
6215 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006216get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006218 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
6220 if (p->var == VAR_WIN)
6221 return (char_u *)GLOBAL_WO(get_varp(p));
6222 return p->var;
6223 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006224 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 {
6226 switch ((int)p->indir)
6227 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006228 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006230 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6231 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6232 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006234 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6235 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6236 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006237 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006238 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006239 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006240 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6241 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006243 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6244 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006246 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6247 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006248#ifdef FEAT_COMPL_FUNC
6249 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6250#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006251#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6252 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6253#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006254#if defined(FEAT_CRYPT)
6255 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6256#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006257#ifdef FEAT_LINEBREAK
6258 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6259#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006260#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006261 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006262#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006263 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006264 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006265 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006266 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006267 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006268 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006269 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006272 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
6274 return get_varp(p);
6275}
6276
6277/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006278 * Get pointer to option variable at 'opt_idx', depending on local or global
6279 * scope.
6280 */
6281 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006282get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006283{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006284 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006285}
6286
6287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 * Get pointer to option variable.
6289 */
6290 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006291get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006293 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 if (p->var == NULL)
6295 return NULL;
6296
6297 switch ((int)p->indir)
6298 {
6299 case PV_NONE: return p->var;
6300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006301 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006302 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006304 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006306 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006308 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006310 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006312 case PV_TC: return *curbuf->b_p_tc != NUL
6313 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006314 case PV_BKC: return *curbuf->b_p_bkc != NUL
6315 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006316 case PV_SISO: return curwin->w_p_siso >= 0
6317 ? (char_u *)&(curwin->w_p_siso) : p->var;
6318 case PV_SO: return curwin->w_p_so >= 0
6319 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006321 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006323 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6325#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006326 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006328 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006330#ifdef FEAT_COMPL_FUNC
6331 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6332 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6333#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006334 case PV_FP: return *curbuf->b_p_fp != NUL
6335 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006337 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006339 case PV_GP: return *curbuf->b_p_gp != NUL
6340 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6341 case PV_MP: return *curbuf->b_p_mp != NUL
6342 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006344#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6345 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6346 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6347#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006348#if defined(FEAT_CRYPT)
6349 case PV_CM: return *curbuf->b_p_cm != NUL
6350 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6351#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006352#ifdef FEAT_LINEBREAK
6353 case PV_SBR: return *curwin->w_p_sbr != NUL
6354 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6355#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006356#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006357 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006358 ? (char_u *)&(curwin->w_p_stl) : p->var;
6359#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006360 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6361 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006362 case PV_LW: return *curbuf->b_p_lw != NUL
6363 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006364 case PV_MENC: return *curbuf->b_p_menc != NUL
6365 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366#ifdef FEAT_ARABIC
6367 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6368#endif
6369 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006370 case PV_LCS: return *curwin->w_p_lcs != NUL
6371 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006372 case PV_FCS: return *curwin->w_p_fcs != NUL
6373 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006374 case PV_VE: return *curwin->w_p_ve != NUL
6375 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006376#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006377 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6378#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006379#ifdef FEAT_SYN_HL
6380 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6381 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006382 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006383 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385#ifdef FEAT_DIFF
6386 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6387#endif
6388#ifdef FEAT_FOLDING
6389 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6390 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6391 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6392 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6393 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6394 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6395 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6396# ifdef FEAT_EVAL
6397 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6398 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6399# endif
6400 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6401#endif
6402 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006403 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006404#ifdef FEAT_LINEBREAK
6405 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006408 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006409#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6411#endif
6412#ifdef FEAT_RIGHTLEFT
6413 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6414 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6415#endif
6416 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006417 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6419#ifdef FEAT_LINEBREAK
6420 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006421 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6422 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006424 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006426 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006427#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006428 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6429 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6430#endif
6431#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006432 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6433 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6434 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
6437 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6438 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6441 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6443 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6445 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6446 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006447 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450#ifdef FEAT_FOLDING
6451 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006454#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006455 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006457#ifdef FEAT_COMPL_FUNC
6458 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006459 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006460#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006461#ifdef FEAT_EVAL
6462 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6463#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006464 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006466 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006472 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6474 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6475 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6476 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6477#ifdef FEAT_FIND_ID
6478# ifdef FEAT_EVAL
6479 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6480# endif
6481#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006482#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6484 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6485#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006486#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489#ifdef FEAT_CRYPT
6490 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006493 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6495 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6496 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6497 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6498 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006500 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6507#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006508 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006509 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6510#endif
6511#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006512 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6513 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6514 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006515 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516#endif
6517 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6518 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6519 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6520 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006521#ifdef FEAT_PERSISTENT_UNDO
6522 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6525#ifdef FEAT_KEYMAP
6526 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6527#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006528#ifdef FEAT_SIGNS
6529 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6530#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006531#ifdef FEAT_VARTABS
6532 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6533 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6534#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006535 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006537 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return (char_u *)&(curbuf->b_p_wm);
6539}
6540
6541/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006542 * Return a pointer to the variable for option at 'opt_idx'
6543 */
6544 char_u *
6545get_option_var(int opt_idx)
6546{
6547 return options[opt_idx].var;
6548}
6549
Dominique Pelle748b3082022-01-08 12:41:16 +00006550#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006551/*
6552 * Return the full name of the option at 'opt_idx'
6553 */
6554 char_u *
6555get_option_fullname(int opt_idx)
6556{
6557 return (char_u *)options[opt_idx].fullname;
6558}
Dominique Pelle748b3082022-01-08 12:41:16 +00006559#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006560
6561/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006562 * Return the did_set callback function for the option at 'opt_idx'
6563 */
6564 opt_did_set_cb_T
6565get_option_did_set_cb(int opt_idx)
6566{
6567 return options[opt_idx].opt_did_set_cb;
6568}
6569
6570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 * Get the value of 'equalprg', either the buffer-local one or the global one.
6572 */
6573 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006574get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 if (*curbuf->b_p_ep == NUL)
6577 return p_ep;
6578 return curbuf->b_p_ep;
6579}
6580
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581/*
6582 * Copy options from one window to another.
6583 * Used when splitting a window.
6584 */
6585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006586win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6589 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006590 after_copy_winopt(wp_to);
6591}
6592
6593/*
6594 * After copying window options: update variables depending on options.
6595 */
6596 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006597after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006598{
6599#ifdef FEAT_LINEBREAK
6600 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006601#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006602#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006603 fill_culopt_flags(NULL, wp);
6604 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006605#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006606 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6607 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006608}
6609
6610 static char_u *
6611copy_option_val(char_u *val)
6612{
6613 if (val == empty_option)
6614 return empty_option; // no need to allocate memory
6615 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618/*
6619 * Copy the options from one winopt_T to another.
6620 * Doesn't free the old option values in "to", use clear_winopt() for that.
6621 * The 'scroll' option is not copied, because it depends on the window height.
6622 * The 'previewwindow' option is reset, there can be only one preview window.
6623 */
6624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006625copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627#ifdef FEAT_ARABIC
6628 to->wo_arab = from->wo_arab;
6629#endif
6630 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006631 to->wo_lcs = copy_option_val(from->wo_lcs);
6632 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006634 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006635 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006636 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006637#ifdef FEAT_LINEBREAK
6638 to->wo_nuw = from->wo_nuw;
6639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640#ifdef FEAT_RIGHTLEFT
6641 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006642 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006644#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006645 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006646#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006647#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006648 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006651#ifdef FEAT_DIFF
6652 to->wo_wrap_save = from->wo_wrap_save;
6653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654#ifdef FEAT_LINEBREAK
6655 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006656 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006657 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006659 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006661 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006662 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006663 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006664 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006665#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006666 to->wo_spell = from->wo_spell;
6667#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006668#ifdef FEAT_SYN_HL
6669 to->wo_cuc = from->wo_cuc;
6670 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006671 to->wo_culopt = copy_option_val(from->wo_culopt);
6672 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674#ifdef FEAT_DIFF
6675 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006676 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006678#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006679 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006680 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006681#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006682#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006683 to->wo_twk = copy_option_val(from->wo_twk);
6684 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686#ifdef FEAT_FOLDING
6687 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006688 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006690 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006691 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 to->wo_fml = from->wo_fml;
6693 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006694 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006695 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006696 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006697 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 to->wo_fdn = from->wo_fdn;
6699# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006700 to->wo_fde = copy_option_val(from->wo_fde);
6701 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006703 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006705#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006706 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006707#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006708
6709#ifdef FEAT_EVAL
6710 // Copy the script context so that we know where the value was last set.
6711 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6712 sizeof(to->wo_script_ctx));
6713#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006714 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715}
6716
6717/*
6718 * Check string options in a window for a NULL value.
6719 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006721check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 check_winopt(&win->w_onebuf_opt);
6724 check_winopt(&win->w_allbuf_opt);
6725}
6726
6727/*
6728 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6729 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006731check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733#ifdef FEAT_FOLDING
6734 check_string_option(&wop->wo_fdi);
6735 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006736 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737# ifdef FEAT_EVAL
6738 check_string_option(&wop->wo_fde);
6739 check_string_option(&wop->wo_fdt);
6740# endif
6741 check_string_option(&wop->wo_fmr);
6742#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006743#ifdef FEAT_SIGNS
6744 check_string_option(&wop->wo_scl);
6745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746#ifdef FEAT_RIGHTLEFT
6747 check_string_option(&wop->wo_rlc);
6748#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006749#ifdef FEAT_LINEBREAK
6750 check_string_option(&wop->wo_sbr);
6751#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006752#ifdef FEAT_STL_OPT
6753 check_string_option(&wop->wo_stl);
6754#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006755#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006756 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006757 check_string_option(&wop->wo_cc);
6758#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006759#ifdef FEAT_CONCEAL
6760 check_string_option(&wop->wo_cocu);
6761#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006762#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006763 check_string_option(&wop->wo_twk);
6764 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006765#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006766#ifdef FEAT_LINEBREAK
6767 check_string_option(&wop->wo_briopt);
6768#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006769 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006770 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006771 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006772 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773}
6774
6775/*
6776 * Free the allocated memory inside a winopt_T.
6777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006779clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781#ifdef FEAT_FOLDING
6782 clear_string_option(&wop->wo_fdi);
6783 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006784 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785# ifdef FEAT_EVAL
6786 clear_string_option(&wop->wo_fde);
6787 clear_string_option(&wop->wo_fdt);
6788# endif
6789 clear_string_option(&wop->wo_fmr);
6790#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006791#ifdef FEAT_SIGNS
6792 clear_string_option(&wop->wo_scl);
6793#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006794#ifdef FEAT_LINEBREAK
6795 clear_string_option(&wop->wo_briopt);
6796#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006797 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798#ifdef FEAT_RIGHTLEFT
6799 clear_string_option(&wop->wo_rlc);
6800#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006801#ifdef FEAT_LINEBREAK
6802 clear_string_option(&wop->wo_sbr);
6803#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006804#ifdef FEAT_STL_OPT
6805 clear_string_option(&wop->wo_stl);
6806#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006807#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006808 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006809 clear_string_option(&wop->wo_cc);
6810#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006811#ifdef FEAT_CONCEAL
6812 clear_string_option(&wop->wo_cocu);
6813#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006814#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006815 clear_string_option(&wop->wo_twk);
6816 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006817#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006818 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006819 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006820 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821}
6822
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006823#ifdef FEAT_EVAL
6824// Index into the options table for a buffer-local option enum.
6825static int buf_opt_idx[BV_COUNT];
6826# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6827
6828/*
6829 * Initialize buf_opt_idx[] if not done already.
6830 */
6831 static void
6832init_buf_opt_idx(void)
6833{
6834 static int did_init_buf_opt_idx = FALSE;
6835 int i;
6836
6837 if (did_init_buf_opt_idx)
6838 return;
6839 did_init_buf_opt_idx = TRUE;
6840 for (i = 0; !istermoption_idx(i); i++)
6841 if (options[i].indir & PV_BUF)
6842 buf_opt_idx[options[i].indir & PV_MASK] = i;
6843}
6844#else
6845# define COPY_OPT_SCTX(buf, bv)
6846#endif
6847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848/*
6849 * Copy global option values to local options for one buffer.
6850 * Used when creating a new buffer and sometimes when entering a buffer.
6851 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006852 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6854 * appropriate.
6855 * BCO_NOHELP Don't copy the values to a help buffer.
6856 */
6857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006858buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006861 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 int dont_do_help;
6863 int did_isk = FALSE;
6864
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006865 // Skip this when the option defaults have not been set yet. Happens when
6866 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (p_cpo != NULL)
6868 {
6869 /*
6870 * Always copy when entering and 'cpo' contains 'S'.
6871 * Don't copy when already initialized.
6872 * Don't copy when 'cpo' contains 's' and not entering.
6873 * 'S' BCO_ENTER initialized 's' should_copy
6874 * yes yes X X TRUE
6875 * yes no yes X FALSE
6876 * no X yes X FALSE
6877 * X no no yes FALSE
6878 * X no no no TRUE
6879 * no yes no X TRUE
6880 */
6881 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6882 && (buf->b_p_initialized
6883 || (!(flags & BCO_ENTER)
6884 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6885 should_copy = FALSE;
6886
6887 if (should_copy || (flags & BCO_ALWAYS))
6888 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006889#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006890 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006891 init_buf_opt_idx();
6892#endif
6893 // Don't copy the options specific to a help buffer when
6894 // BCO_NOHELP is given or the options were initialized already
6895 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6897 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006898 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
6900 save_p_isk = buf->b_p_isk;
6901 buf->b_p_isk = NULL;
6902 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006903 // Always free the allocated strings. If not already initialized,
6904 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 if (!buf->b_p_initialized)
6906 {
6907 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006908 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006911 switch (*p_ffs)
6912 {
6913 case 'm':
6914 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6915 case 'd':
6916 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6917 case 'u':
6918 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6919 default:
6920 buf->b_p_ff = vim_strsave(p_ff);
6921 }
6922 if (buf->b_p_ff != NULL)
6923 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 buf->b_p_bh = empty_option;
6925 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 }
6927 else
6928 free_buf_options(buf, FALSE);
6929
6930 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006931 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 buf->b_p_ai_nopaste = p_ai_nopaste;
6933 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006934 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006936 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 buf->b_p_tw_nopaste = p_tw_nopaste;
6938 buf->b_p_tw_nobin = p_tw_nobin;
6939 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006940 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 buf->b_p_wm_nopaste = p_wm_nopaste;
6942 buf->b_p_wm_nobin = p_wm_nobin;
6943 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006944 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006945 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006946 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006947 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006948 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006950 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006952 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006954 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 buf->b_p_ml_nobin = p_ml_nobin;
6956 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006957 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006958 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006959 buf->b_p_swf = FALSE;
6960 else
6961 {
6962 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006963 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006966 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006967#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006968 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006971#ifdef FEAT_COMPL_FUNC
6972 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006973 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006974 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006975 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006976 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006977 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006978#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006979#ifdef FEAT_EVAL
6980 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006981 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006982 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006985 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006987#ifdef FEAT_VARTABS
6988 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006989 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006990 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006991 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006992 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006993 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006994 buf->b_p_vsts_nopaste = p_vsts_nopaste
6995 ? vim_strsave(p_vsts_nopaste) : NULL;
6996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006998 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007000 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#ifdef FEAT_FOLDING
7002 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007003 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004#endif
7005 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007006 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007007 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007008 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007009 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7010 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007012 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007018 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007021 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007023 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007025 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007026 buf->b_p_cinsd = vim_strsave(p_cinsd);
7027 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007028 buf->b_p_lop = vim_strsave(p_lop);
7029 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007030
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007031 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007034 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007036 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007038 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007040 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007042 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007043 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007044 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007045#endif
7046#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007047 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007048 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007049 (void)compile_cap_prog(&buf->b_s);
7050 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007051 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007052 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007053 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007054 buf->b_s.b_p_spo = vim_strsave(p_spo);
7055 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007057#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007059 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007061 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007063 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007064#if defined(FEAT_EVAL)
7065 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007066 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068#ifdef FEAT_CRYPT
7069 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007070 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007073 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#ifdef FEAT_KEYMAP
7075 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007076 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf->b_kmap_state |= KEYMAP_INIT;
7078#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007079#ifdef FEAT_TERMINAL
7080 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007081 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007082#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 // This isn't really an option, but copying the langmap and IME
7084 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007086 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007088 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 // options that are normally global but also have a local value
7091 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007093 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007094 buf->b_p_bkc = empty_option;
7095 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096#ifdef FEAT_QUICKFIX
7097 buf->b_p_gp = empty_option;
7098 buf->b_p_mp = empty_option;
7099 buf->b_p_efm = empty_option;
7100#endif
7101 buf->b_p_ep = empty_option;
7102 buf->b_p_kp = empty_option;
7103 buf->b_p_path = empty_option;
7104 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007105 buf->b_p_tc = empty_option;
7106 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107#ifdef FEAT_FIND_ID
7108 buf->b_p_def = empty_option;
7109 buf->b_p_inc = empty_option;
7110# ifdef FEAT_EVAL
7111 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007112 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113# endif
7114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 buf->b_p_dict = empty_option;
7116 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007117#ifdef FEAT_COMPL_FUNC
7118 buf->b_p_tsrfu = empty_option;
7119#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007120 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007121 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007122#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7123 buf->b_p_bexpr = empty_option;
7124#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007125#if defined(FEAT_CRYPT)
7126 buf->b_p_cm = empty_option;
7127#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007128#ifdef FEAT_PERSISTENT_UNDO
7129 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007130 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007131#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007132 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007133 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007135 // Don't copy the options set by ex_help(), use the saved values,
7136 // when going from a help buffer to a non-help buffer.
7137 // Don't touch these at all when BCO_NOHELP is used and going from
7138 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007142#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007143 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007144 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007145 else
7146 buf->b_p_vts_array = NULL;
7147#endif
7148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 else
7150 {
7151 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007152 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 did_isk = TRUE;
7154 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007155 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007156#ifdef FEAT_VARTABS
7157 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007158 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007159 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007160 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007161 else
7162 buf->b_p_vts_array = NULL;
7163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 if (buf->b_p_bt[0] == 'h')
7166 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007168 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 }
7170 }
7171
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007172 // When the options should be copied (ignoring BCO_ALWAYS), set the
7173 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if (should_copy)
7175 buf->b_p_initialized = TRUE;
7176 }
7177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007178 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (did_isk)
7180 (void)buf_init_chartab(buf, FALSE);
7181}
7182
7183/*
7184 * Reset the 'modifiable' option and its default value.
7185 */
7186 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007187reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 int opt_idx;
7190
7191 curbuf->b_p_ma = FALSE;
7192 p_ma = FALSE;
7193 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007194 if (opt_idx >= 0)
7195 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196}
7197
7198/*
7199 * Set the global value for 'iminsert' to the local value.
7200 */
7201 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007202set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 p_iminsert = curbuf->b_p_iminsert;
7205}
7206
7207/*
7208 * Set the global value for 'imsearch' to the local value.
7209 */
7210 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007211set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 p_imsearch = curbuf->b_p_imsearch;
7214}
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216static int expand_option_idx = -1;
7217static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7218static int expand_option_flags = 0;
7219
7220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007221set_context_in_set_cmd(
7222 expand_T *xp,
7223 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007227 long_u flags = 0; // init for GCC
7228 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 char_u *p;
7230 char_u *s;
7231 int is_term_option = FALSE;
7232 int key;
7233
7234 expand_option_flags = opt_flags;
7235
7236 xp->xp_context = EXPAND_SETTINGS;
7237 if (*arg == NUL)
7238 {
7239 xp->xp_pattern = arg;
7240 return;
7241 }
7242 p = arg + STRLEN(arg) - 1;
7243 if (*p == ' ' && *(p - 1) != '\\')
7244 {
7245 xp->xp_pattern = p + 1;
7246 return;
7247 }
7248 while (p > arg)
7249 {
7250 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007251 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (*p == ' ' || *p == ',')
7253 {
7254 while (s > arg && *(s - 1) == '\\')
7255 --s;
7256 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007257 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 if (*p == ' ' && ((p - s) & 1) == 0)
7259 {
7260 ++p;
7261 break;
7262 }
7263 --p;
7264 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007265 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 {
7267 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007268 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 p += 2;
7270 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007271 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {
7273 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007274 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 p += 3;
7276 }
7277 xp->xp_pattern = arg = p;
7278 if (*arg == '<')
7279 {
7280 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007281 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 return;
7283 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007284 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
7286 xp->xp_context = EXPAND_NOTHING;
7287 return;
7288 }
7289 nextchar = *++p;
7290 is_term_option = TRUE;
7291 expand_option_name[2] = KEY2TERMCAP0(key);
7292 expand_option_name[3] = KEY2TERMCAP1(key);
7293 }
7294 else
7295 {
7296 if (p[0] == 't' && p[1] == '_')
7297 {
7298 p += 2;
7299 if (*p != NUL)
7300 ++p;
7301 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007302 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 nextchar = *++p;
7304 is_term_option = TRUE;
7305 expand_option_name[2] = p[-2];
7306 expand_option_name[3] = p[-1];
7307 }
7308 else
7309 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007310 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7312 p++;
7313 if (*p == NUL)
7314 return;
7315 nextchar = *p;
7316 *p = NUL;
7317 opt_idx = findoption(arg);
7318 *p = nextchar;
7319 if (opt_idx == -1 || options[opt_idx].var == NULL)
7320 {
7321 xp->xp_context = EXPAND_NOTHING;
7322 return;
7323 }
7324 flags = options[opt_idx].flags;
7325 if (flags & P_BOOL)
7326 {
7327 xp->xp_context = EXPAND_NOTHING;
7328 return;
7329 }
7330 }
7331 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007332 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7334 {
7335 ++p;
7336 nextchar = '=';
7337 }
7338 if ((nextchar != '=' && nextchar != ':')
7339 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7340 {
7341 xp->xp_context = EXPAND_UNSUCCESSFUL;
7342 return;
7343 }
7344 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7345 {
7346 xp->xp_context = EXPAND_OLD_SETTING;
7347 if (is_term_option)
7348 expand_option_idx = -1;
7349 else
7350 expand_option_idx = opt_idx;
7351 xp->xp_pattern = p + 1;
7352 return;
7353 }
7354 xp->xp_context = EXPAND_NOTHING;
7355 if (is_term_option || (flags & P_NUM))
7356 return;
7357
7358 xp->xp_pattern = p + 1;
7359
7360 if (flags & P_EXPAND)
7361 {
7362 p = options[opt_idx].var;
7363 if (p == (char_u *)&p_bdir
7364 || p == (char_u *)&p_dir
7365 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007366 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#ifdef FEAT_SESSION
7370 || p == (char_u *)&p_vdir
7371#endif
7372 )
7373 {
7374 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007375 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 xp->xp_backslash = XP_BS_THREE;
7377 else
7378 xp->xp_backslash = XP_BS_ONE;
7379 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007380 else if (p == (char_u *)&p_ft)
7381 {
7382 xp->xp_context = EXPAND_FILETYPE;
7383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 else
7385 {
7386 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007387 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 if (p == (char_u *)&p_tags)
7389 xp->xp_backslash = XP_BS_THREE;
7390 else
7391 xp->xp_backslash = XP_BS_ONE;
7392 }
7393 }
7394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007395 // For an option that is a list of file names, find the start of the
7396 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7398 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007399 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (*p == ' ' || *p == ',')
7401 {
7402 s = p;
7403 while (s > xp->xp_pattern && *(s - 1) == '\\')
7404 --s;
7405 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7406 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7407 {
7408 xp->xp_pattern = p + 1;
7409 break;
7410 }
7411 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007412
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007413#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007414 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007415 if (options[opt_idx].var == (char_u *)&p_sps
7416 && STRNCMP(p, "file:", 5) == 0)
7417 {
7418 xp->xp_pattern = p + 5;
7419 break;
7420 }
7421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423}
7424
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007425/*
7426 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7427 *
7428 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7429 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7430 *
7431 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7432 * regular expression 'regmatch', then stores the match in matches[idx] and
7433 * returns TRUE.
7434 *
7435 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7436 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7437 *
7438 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7439 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7440 */
7441 static int
7442match_str(
7443 char_u *str,
7444 regmatch_T *regmatch,
7445 char_u **matches,
7446 int idx,
7447 int test_only,
7448 int fuzzy,
7449 char_u *fuzzystr,
7450 fuzmatch_str_T *fuzmatch)
7451{
7452 if (!fuzzy)
7453 {
7454 if (vim_regexec(regmatch, str, (colnr_T)0))
7455 {
7456 if (!test_only)
7457 matches[idx] = vim_strsave(str);
7458 return TRUE;
7459 }
7460 }
7461 else
7462 {
7463 int score;
7464
7465 score = fuzzy_match_str(str, fuzzystr);
7466 if (score != 0)
7467 {
7468 if (!test_only)
7469 {
7470 fuzmatch[idx].idx = idx;
7471 fuzmatch[idx].str = vim_strsave(str);
7472 fuzmatch[idx].score = score;
7473 }
7474
7475 return TRUE;
7476 }
7477 }
7478
7479 return FALSE;
7480}
7481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007483ExpandSettings(
7484 expand_T *xp,
7485 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007486 char_u *fuzzystr,
7487 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007488 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007489 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007491 int num_normal = 0; // Nr of matching non-term-code settings
7492 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 int opt_idx;
7494 int match;
7495 int count = 0;
7496 char_u *str;
7497 int loop;
7498 int is_term_opt;
7499 char_u name_buf[MAX_KEY_NAME_LEN];
7500 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007501 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007502 int fuzzy;
7503 fuzmatch_str_T *fuzmatch = NULL;
7504
Christian Brabandtcb747892022-05-08 21:10:56 +01007505 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007507 // do this loop twice:
7508 // loop == 0: count the number of matching options
7509 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 for (loop = 0; loop <= 1; ++loop)
7511 {
7512 regmatch->rm_ic = ic;
7513 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7514 {
K.Takataeeec2542021-06-02 13:28:16 +02007515 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007516 {
7517 if (match_str((char_u *)names[match], regmatch, *matches,
7518 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 {
7520 if (loop == 0)
7521 num_normal++;
7522 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007523 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 }
7527 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7528 opt_idx++)
7529 {
7530 if (options[opt_idx].var == NULL)
7531 continue;
7532 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007533 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007535 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (is_term_opt && num_normal > 0)
7537 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007538
7539 if (match_str(str, regmatch, *matches, count, (loop == 0),
7540 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
7542 if (loop == 0)
7543 {
7544 if (is_term_opt)
7545 num_term++;
7546 else
7547 num_normal++;
7548 }
7549 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007550 count++;
7551 }
7552 else if (!fuzzy && options[opt_idx].shortname != NULL
7553 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007554 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007555 {
7556 // Compare against the abbreviated option name (for regular
7557 // expression match). Fuzzy matching (previous if) already
7558 // matches against both the expanded and abbreviated names.
7559 if (loop == 0)
7560 {
7561 if (is_term_opt)
7562 num_term++;
7563 else
7564 num_normal++;
7565 }
7566 else
7567 (*matches)[count++] = vim_strsave(str);
7568 }
7569 else if (is_term_opt)
7570 {
7571 name_buf[0] = '<';
7572 name_buf[1] = 't';
7573 name_buf[2] = '_';
7574 name_buf[3] = str[2];
7575 name_buf[4] = str[3];
7576 name_buf[5] = '>';
7577 name_buf[6] = NUL;
7578
7579 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7580 fuzzy, fuzzystr, fuzmatch))
7581 {
7582 if (loop == 0)
7583 num_term++;
7584 else
7585 count++;
7586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007589
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007590 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7592 {
7593 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7594 {
7595 if (!isprint(str[0]) || !isprint(str[1]))
7596 continue;
7597
7598 name_buf[0] = 't';
7599 name_buf[1] = '_';
7600 name_buf[2] = str[0];
7601 name_buf[3] = str[1];
7602 name_buf[4] = NUL;
7603
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007604 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007605 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007606 {
7607 if (loop == 0)
7608 num_term++;
7609 else
7610 count++;
7611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 else
7613 {
7614 name_buf[0] = '<';
7615 name_buf[1] = 't';
7616 name_buf[2] = '_';
7617 name_buf[3] = str[0];
7618 name_buf[4] = str[1];
7619 name_buf[5] = '>';
7620 name_buf[6] = NUL;
7621
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007622 if (match_str(name_buf, regmatch, *matches, count,
7623 (loop == 0), fuzzy, fuzzystr,
7624 fuzmatch))
7625 {
7626 if (loop == 0)
7627 num_term++;
7628 else
7629 count++;
7630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632 }
7633
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007634 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007635 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7637 {
7638 name_buf[0] = '<';
7639 STRCPY(name_buf + 1, str);
7640 STRCAT(name_buf, ">");
7641
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007642 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7643 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {
7645 if (loop == 0)
7646 num_term++;
7647 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007648 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650 }
7651 }
7652 if (loop == 0)
7653 {
7654 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007655 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007657 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 else
7659 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007660 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007662 *matches = ALLOC_MULT(char_u *, *numMatches);
7663 if (*matches == NULL)
7664 {
7665 *matches = (char_u **)"";
7666 return FAIL;
7667 }
7668 }
7669 else
7670 {
7671 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7672 if (fuzmatch == NULL)
7673 {
7674 *matches = (char_u **)"";
7675 return FAIL;
7676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678 }
7679 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007680
7681 if (fuzzy &&
7682 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7683 return FAIL;
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 return OK;
7686}
7687
7688 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007689ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007691 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 char_u *buf;
7693
zeertzjqb0d45ec2023-01-25 15:04:22 +00007694 *numMatches = 0;
7695 *matches = ALLOC_ONE(char_u *);
7696 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 return FAIL;
7698
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007699 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (expand_option_idx < 0)
7701 {
7702 var = find_termcode(expand_option_name + 2);
7703 if (var == NULL)
7704 expand_option_idx = findoption(expand_option_name);
7705 }
7706
7707 if (expand_option_idx >= 0)
7708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007709 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 option_value2string(&options[expand_option_idx], expand_option_flags);
7711 var = NameBuff;
7712 }
7713 else if (var == NULL)
7714 var = (char_u *)"";
7715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007716 // A backslash is required before some characters. This is the reverse of
7717 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 buf = vim_strsave_escaped(var, escape_chars);
7719
7720 if (buf == NULL)
7721 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007722 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 return FAIL;
7724 }
7725
7726#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007727 // For MS-Windows et al. we don't double backslashes at the start and
7728 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007729 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 if (var[0] == '\\' && var[1] == '\\'
7731 && expand_option_idx >= 0
7732 && (options[expand_option_idx].flags & P_EXPAND)
7733 && vim_isfilec(var[2])
7734 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007735 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736#endif
7737
zeertzjqb0d45ec2023-01-25 15:04:22 +00007738 *matches[0] = buf;
7739 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 return OK;
7741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742
7743/*
7744 * Get the value for the numeric or string option *opp in a nice format into
7745 * NameBuff[]. Must not be called with a hidden option!
7746 */
7747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007748option_value2string(
7749 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007750 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 char_u *varp;
7753
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007754 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 if (opp->flags & P_NUM)
7757 {
7758 long wc = 0;
7759
7760 if (wc_use_keyname(varp, &wc))
7761 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7762 else if (wc != 0)
7763 STRCPY(NameBuff, transchar((int)wc));
7764 else
7765 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7766 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007767 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {
7769 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007770 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 NameBuff[0] = NUL;
7772#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007773 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007774 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 STRCPY(NameBuff, "*****");
7776#endif
7777 else if (opp->flags & P_EXPAND)
7778 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007779 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 else if ((char_u **)opp->var == &p_pt)
7781 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7782 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007783 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
7785}
7786
7787/*
7788 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7789 * printed as a keyname.
7790 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7791 */
7792 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007793wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7796 {
7797 *wcp = *(long *)varp;
7798 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7799 return TRUE;
7800 }
7801 return FALSE;
7802}
7803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 * Return TRUE if "x" is present in 'shortmess' option, or
7806 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7807 */
7808 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007809shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007811 return p_shm != NULL &&
7812 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 || (vim_strchr(p_shm, 'a') != NULL
7814 && vim_strchr((char_u *)SHM_A, x) != NULL));
7815}
7816
7817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7819 *
7820 * Reset 'compatible' and set the values for options that didn't get set yet
7821 * to the Vim defaults.
7822 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007823 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 */
7825 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007826vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007828 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007829 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007830 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831
7832 if (!option_was_set((char_u *)"cp"))
7833 {
7834 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007835 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7837 set_option_default(opt_idx, OPT_FREE, FALSE);
7838 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007839 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007841
7842 if (fname != NULL)
7843 {
7844 p = vim_getenv(envname, &dofree);
7845 if (p == NULL)
7846 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007847 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007848 p = FullName_save(fname, FALSE);
7849 if (p != NULL)
7850 {
7851 vim_setenv(envname, p);
7852 vim_free(p);
7853 }
7854 }
7855 else if (dofree)
7856 vim_free(p);
7857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858}
7859
7860/*
7861 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7862 */
7863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007864change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007866 int opt_idx;
7867
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 if (p_cp != on)
7869 {
7870 p_cp = on;
7871 compatible_set();
7872 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007873 opt_idx = findoption((char_u *)"cp");
7874 if (opt_idx >= 0)
7875 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876}
7877
7878/*
7879 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007880 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 */
7882 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007883option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 int idx;
7886
7887 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007888 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 return FALSE;
7890 if (options[idx].flags & P_WAS_SET)
7891 return TRUE;
7892 return FALSE;
7893}
7894
7895/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007896 * Reset the flag indicating option "name" was set.
7897 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007898 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007899reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007900{
7901 int idx = findoption(name);
7902
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007903 if (idx < 0)
7904 return FAIL;
7905
7906 options[idx].flags &= ~P_WAS_SET;
7907 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007908}
7909
7910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 * compatible_set() - Called when 'compatible' has been set or unset.
7912 *
7913 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7914 * flag) to a Vi compatible value.
7915 * When 'compatible' is unset: Set all options that have a different default
7916 * for Vim (without the P_VI_DEF flag) to that default.
7917 */
7918 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007919compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
7921 int opt_idx;
7922
Bram Moolenaardac13472019-09-16 21:06:21 +02007923 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7925 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7926 set_option_default(opt_idx, OPT_FREE, p_cp);
7927 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007928 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929}
7930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 * Check if backspacing over something is allowed.
7933 */
7934 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007935can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007936 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007938#ifdef FEAT_JOB_CHANNEL
7939 if (what == BS_START && bt_prompt(curbuf))
7940 return FALSE;
7941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 switch (*p_bs)
7943 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007944 case '3': return TRUE;
7945 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 case '1': return (what != BS_START);
7947 case '0': return FALSE;
7948 }
7949 return vim_strchr(p_bs, what) != NULL;
7950}
7951
7952/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007953 * Return the effective 'scrolloff' value for the current window, using the
7954 * global value when appropriate.
7955 */
7956 long
7957get_scrolloff_value(void)
7958{
7959 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7960}
7961
7962/*
7963 * Return the effective 'sidescrolloff' value for the current window, using the
7964 * global value when appropriate.
7965 */
7966 long
7967get_sidescrolloff_value(void)
7968{
7969 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7970}
7971
7972/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007973 * Get the local or global value of 'backupcopy'.
7974 */
7975 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007976get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007977{
7978 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7979}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007980
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007981#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007982/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007983 * Get the local or global value of 'formatlistpat'.
7984 */
7985 char_u *
7986get_flp_value(buf_T *buf)
7987{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007988 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7989 return p_flp;
7990 return buf->b_p_flp;
7991}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007992#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007993
7994/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007995 * Get the local or global value of the 'virtualedit' flags.
7996 */
7997 unsigned int
7998get_ve_flags(void)
7999{
Gary Johnson51ad8502021-08-03 18:33:08 +02008000 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8001 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008002}
8003
Bram Moolenaaree857022019-11-09 23:26:40 +01008004#if defined(FEAT_LINEBREAK) || defined(PROTO)
8005/*
8006 * Get the local or global value of 'showbreak'.
8007 */
8008 char_u *
8009get_showbreak_value(win_T *win)
8010{
8011 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8012 return p_sbr;
8013 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8014 return empty_option;
8015 return win->w_p_sbr;
8016}
8017#endif
8018
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008019#if defined(FEAT_EVAL) || defined(PROTO)
8020/*
8021 * Get window or buffer local options.
8022 */
8023 dict_T *
8024get_winbuf_options(int bufopt)
8025{
8026 dict_T *d;
8027 int opt_idx;
8028
8029 d = dict_alloc();
8030 if (d == NULL)
8031 return NULL;
8032
Bram Moolenaardac13472019-09-16 21:06:21 +02008033 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008034 {
8035 struct vimoption *opt = &options[opt_idx];
8036
8037 if ((bufopt && (opt->indir & PV_BUF))
8038 || (!bufopt && (opt->indir & PV_WIN)))
8039 {
8040 char_u *varp = get_varp(opt);
8041
8042 if (varp != NULL)
8043 {
8044 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008045 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008046 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008047 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008048 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008049 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008050 }
8051 }
8052 }
8053
8054 return d;
8055}
8056#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008057
Bram Moolenaardac13472019-09-16 21:06:21 +02008058#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008059/*
8060 * This is called when 'culopt' is changed
8061 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008062 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008063fill_culopt_flags(char_u *val, win_T *wp)
8064{
8065 char_u *p;
8066 char_u culopt_flags_new = 0;
8067
8068 if (val == NULL)
8069 p = wp->w_p_culopt;
8070 else
8071 p = val;
8072 while (*p != NUL)
8073 {
8074 if (STRNCMP(p, "line", 4) == 0)
8075 {
8076 p += 4;
8077 culopt_flags_new |= CULOPT_LINE;
8078 }
8079 else if (STRNCMP(p, "both", 4) == 0)
8080 {
8081 p += 4;
8082 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8083 }
8084 else if (STRNCMP(p, "number", 6) == 0)
8085 {
8086 p += 6;
8087 culopt_flags_new |= CULOPT_NBR;
8088 }
8089 else if (STRNCMP(p, "screenline", 10) == 0)
8090 {
8091 p += 10;
8092 culopt_flags_new |= CULOPT_SCRLINE;
8093 }
8094
8095 if (*p != ',' && *p != NUL)
8096 return FAIL;
8097 if (*p == ',')
8098 ++p;
8099 }
8100
8101 // Can't have both "line" and "screenline".
8102 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8103 return FAIL;
8104 wp->w_p_culopt_flags = culopt_flags_new;
8105
8106 return OK;
8107}
8108#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008109
8110/*
8111 * Get the value of 'magic' adjusted for Vim9 script.
8112 */
8113 int
8114magic_isset(void)
8115{
8116 switch (magic_overruled)
8117 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008118 case OPTION_MAGIC_ON: return TRUE;
8119 case OPTION_MAGIC_OFF: return FALSE;
8120 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008121 }
8122#ifdef FEAT_EVAL
8123 if (in_vim9script())
8124 return TRUE;
8125#endif
8126 return p_magic;
8127}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008128
8129/*
8130 * Set the callback function value for an option that accepts a function name,
8131 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8132 * Returns OK if the option is successfully set to a function, otherwise
8133 * returns FAIL.
8134 */
8135 int
8136option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8137{
8138#ifdef FEAT_EVAL
8139 typval_T *tv;
8140 callback_T cb;
8141
8142 if (optval == NULL || *optval == NUL)
8143 {
8144 free_callback(optcb);
8145 return OK;
8146 }
8147
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008148 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008149 || (STRNCMP(optval, "function(", 9) == 0)
8150 || (STRNCMP(optval, "funcref(", 8) == 0))
8151 // Lambda expression or a funcref
8152 tv = eval_expr(optval, NULL);
8153 else
8154 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008155 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008156 if (tv == NULL)
8157 return FAIL;
8158
8159 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008160 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008161 {
8162 free_tv(tv);
8163 return FAIL;
8164 }
8165
8166 free_callback(optcb);
8167 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008168 if (cb.cb_free_name)
8169 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008170 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008171
8172 // when using Vim9 style "import.funcname" it needs to be expanded to
8173 // "import#funcname".
8174 expand_autload_callback(optcb);
8175
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008176 return OK;
8177#else
8178 return FAIL;
8179#endif
8180}