blob: 3050dbc4170ebdc988e2fc035b580dcb0f0a33ec [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 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002138 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002139 // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002140 value = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarbf5f1892023-06-27 21:51:07 +01002141 else if (opt_flags == OPT_LOCAL
2142 && ((long *)varp == &curwin->w_p_siso
2143 || (long *)varp == &curwin->w_p_so))
2144 // for 'scrolloff'/'sidescrolloff' -1 means using the global value
2145 value = -1;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002146 else
2147 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2148 }
2149 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2150 && (*arg == '<'
2151 || *arg == '^'
2152 || (*arg != NUL
2153 && (!arg[1] || VIM_ISWHITE(arg[1]))
2154 && !VIM_ISDIGIT(*arg))))
2155 {
2156 value = string_to_key(arg, FALSE);
2157 if (value == 0 && (long *)varp != &p_wcm)
2158 {
2159 errmsg = e_invalid_argument;
2160 goto skip;
2161 }
2162 }
2163 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2164 {
2165 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002166 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002167 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2168 {
2169 errmsg = e_number_required_after_equal;
2170 goto skip;
2171 }
2172 }
2173 else
2174 {
2175 errmsg = e_number_required_after_equal;
2176 goto skip;
2177 }
2178
2179 if (op == OP_ADDING)
2180 value = *(long *)varp + value;
2181 else if (op == OP_PREPENDING)
2182 value = *(long *)varp * value;
2183 else if (op == OP_REMOVING)
2184 value = *(long *)varp - value;
2185
2186 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2187 opt_flags);
2188
2189skip:
2190 *argp = arg;
2191 return errmsg;
2192}
2193
2194/*
2195 * Set a key code (t_xx) option
2196 */
2197 static char *
2198do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2199{
2200 char_u *arg = *argp;
2201 char_u *p;
2202
2203 if (nextchar == '&')
2204 {
2205 if (add_termcap_entry(key_name, TRUE) == FAIL)
2206 return e_not_found_in_termcap;
2207 }
2208 else
2209 {
2210 ++arg; // jump to after the '=' or ':'
2211 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2212 if (*p == '\\' && p[1] != NUL)
2213 ++p;
2214 nextchar = *p;
2215 *p = NUL;
2216 add_termcode(key_name, arg, FALSE);
2217 *p = nextchar;
2218 }
2219 if (full_screen)
2220 ttest(FALSE);
2221 redraw_all_later(UPD_CLEAR);
2222
2223 *argp = arg;
2224 return NULL;
2225}
2226
2227/*
2228 * Set an option to a new value.
2229 */
2230 static char *
2231do_set_option_value(
2232 int opt_idx,
2233 int opt_flags,
2234 char_u **argp,
2235 set_prefix_T prefix,
2236 set_op_T op,
2237 long_u flags,
2238 char_u *varp,
2239 char_u *key_name,
2240 int nextchar,
2241 int afterchar,
2242 int cp_val,
2243 int *stopopteval,
2244 char *errbuf,
2245 size_t errbuflen)
2246{
2247 int value_checked = FALSE;
2248 char *errmsg = NULL;
2249 char_u *arg = *argp;
2250
2251 if (flags & P_BOOL)
2252 {
2253 // boolean option
2254 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2255 nextchar, afterchar, cp_val);
2256 if (errmsg != NULL)
2257 goto skip;
2258 }
2259 else
2260 {
2261 // numeric or string option
2262 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2263 || prefix != PREFIX_NONE)
2264 {
2265 errmsg = e_invalid_argument;
2266 goto skip;
2267 }
2268
2269 if (flags & P_NUM)
2270 {
2271 // numeric option
2272 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2273 op, flags, cp_val, varp,
2274 errbuf, errbuflen);
2275 if (errmsg != NULL)
2276 goto skip;
2277 }
2278 else if (opt_idx >= 0)
2279 {
2280 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002281 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2282 flags, cp_val, varp, errbuf,
2283 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002284 {
2285 if (errmsg != NULL)
2286 goto skip;
2287 *stopopteval = TRUE;
2288 goto skip;
2289 }
2290 }
2291 else
2292 {
2293 // key code option
2294 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2295 if (errmsg != NULL)
2296 goto skip;
2297 }
2298 }
2299
2300 if (opt_idx >= 0)
2301 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2302
2303skip:
2304 *argp = arg;
2305 return errmsg;
2306}
2307
2308/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002309 * Set an option to a new value.
2310 * Return NULL if OK, return an untranslated error message when something is
2311 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2312 */
2313 static char *
2314do_set_option(
2315 int opt_flags,
2316 char_u **argp,
2317 char_u *arg_start,
2318 char_u **startarg,
2319 int *did_show,
2320 int *stopopteval,
2321 char *errbuf,
2322 size_t errbuflen)
2323{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002324 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002325 char_u *arg;
2326 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2327 set_op_T op;
2328 long_u flags; // flags for current option
2329 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002330 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002331 int nextchar; // next non-white char after option name
2332 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002333 char *errmsg = NULL;
2334 int key;
2335 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002336
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002337 prefix = get_option_prefix(argp);
2338 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002339
2340 // find end of name
2341 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002342 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2343 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002344
2345 // remember character after option name
2346 afterchar = arg[len];
2347
2348 if (in_vim9script())
2349 {
2350 char_u *p = skipwhite(arg + len);
2351
2352 // disallow white space before =val, +=val, -=val, ^=val
2353 if (p > arg + len && (p[0] == '='
2354 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2355 && p[1] == '=')))
2356 {
2357 errmsg = e_no_white_space_allowed_between_option_and;
2358 arg = p;
2359 *startarg = p;
2360 goto skip;
2361 }
2362 }
2363 else
2364 // skip white space, allow ":set ai ?", ":set hlsearch !"
2365 while (VIM_ISWHITE(arg[len]))
2366 ++len;
2367
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002368 op = get_opt_op(arg + len);
2369 if (op != OP_NONE)
2370 len++;
2371
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002372 nextchar = arg[len];
2373
2374 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2375 {
2376 if (in_vim9script() && arg > arg_start
2377 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2378 errmsg = e_no_white_space_allowed_between_option_and;
2379 else
2380 errmsg = e_unknown_option;
2381 goto skip;
2382 }
2383
2384 if (opt_idx >= 0)
2385 {
2386 if (options[opt_idx].var == NULL) // hidden option: skip
2387 {
2388 // Only give an error message when requesting the value of
2389 // a hidden option, ignore setting it.
2390 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2391 && (!(options[opt_idx].flags & P_BOOL)
2392 || nextchar == '?'))
2393 errmsg = e_option_not_supported;
2394 goto skip;
2395 }
2396
2397 flags = options[opt_idx].flags;
2398 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2399 }
2400 else
2401 {
2402 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002403 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002404 if (key < 0)
2405 {
2406 key_name[0] = KEY2TERMCAP0(key);
2407 key_name[1] = KEY2TERMCAP1(key);
2408 }
2409 else
2410 {
2411 key_name[0] = KS_KEY;
2412 key_name[1] = (key & 0xff);
2413 }
2414 }
2415
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002416 // Make sure the option value can be changed.
2417 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002418 goto skip;
2419
Bram Moolenaar40b48722023-02-05 17:04:50 +00002420 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002421 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2422 {
2423 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002424 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2425 {
2426 if (arg[3] == 'm') // "opt&vim": set to Vim default
2427 {
2428 cp_val = FALSE;
2429 arg += 3;
2430 }
2431 else // "opt&vi": set to Vi default
2432 {
2433 cp_val = TRUE;
2434 arg += 2;
2435 }
2436 }
2437 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2438 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2439 {
2440 errmsg = e_trailing_characters;
2441 goto skip;
2442 }
2443 }
2444
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002445 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2446 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002447 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002448 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002449 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2450 && !(flags & P_BOOL)))
2451 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002452 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002453 if (*did_show)
2454 msg_putchar('\n'); // cursor below last one
2455 else
2456 {
2457 gotocmdline(TRUE); // cursor at status line
2458 *did_show = TRUE; // remember that we did a line
2459 }
2460 if (opt_idx >= 0)
2461 {
2462 showoneopt(&options[opt_idx], opt_flags);
2463#ifdef FEAT_EVAL
2464 if (p_verbose > 0)
2465 {
2466 // Mention where the option was last set.
2467 if (varp == options[opt_idx].var)
2468 last_set_msg(options[opt_idx].script_ctx);
2469 else if ((int)options[opt_idx].indir & PV_WIN)
2470 last_set_msg(curwin->w_p_script_ctx[
2471 (int)options[opt_idx].indir & PV_MASK]);
2472 else if ((int)options[opt_idx].indir & PV_BUF)
2473 last_set_msg(curbuf->b_p_script_ctx[
2474 (int)options[opt_idx].indir & PV_MASK]);
2475 }
2476#endif
2477 }
2478 else
2479 {
2480 char_u *p;
2481
2482 p = find_termcode(key_name);
2483 if (p == NULL)
2484 {
2485 errmsg = e_key_code_not_set;
2486 goto skip;
2487 }
2488 else
2489 (void)show_one_termcode(key_name, p, TRUE);
2490 }
2491 if (nextchar != '?'
2492 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2493 errmsg = e_trailing_characters;
2494 }
2495 else
2496 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002497 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2498 flags, varp, key_name, nextchar,
2499 afterchar, cp_val, stopopteval, errbuf,
2500 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002501 }
2502
2503skip:
2504 *argp = arg;
2505 return errmsg;
2506}
2507
2508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 * Parse 'arg' for option settings.
2510 *
2511 * 'arg' may be IObuff, but only when no errors can be present and option
2512 * does not need to be expanded with option_expand().
2513 * "opt_flags":
2514 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002515 * OPT_GLOBAL for ":setglobal"
2516 * OPT_LOCAL for ":setlocal" and a modeline
2517 * OPT_MODELINE for a modeline
2518 * OPT_WINONLY to only set window-local options
2519 * OPT_NOWIN to skip setting window-local options
2520 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002522 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
2524 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002525do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002526 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002527 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002529 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 if (*arg == NUL)
2534 {
2535 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002536 did_show = TRUE;
2537 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002542 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002543 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002545 // ":set all" show all options.
2546 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 arg += 3;
2548 if (*arg == '&')
2549 {
2550 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002551 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002553 didset_options();
2554 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002555 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
2557 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002560 did_show = TRUE;
2561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002563 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
2565 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002566 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002567 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 arg += 7;
2569 }
2570 else
2571 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002572 int stopopteval = FALSE;
2573 char *errmsg = NULL;
2574 char errbuf[80];
2575 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002577 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2578 &did_show, &stopopteval, errbuf,
2579 sizeof(errbuf));
2580 if (stopopteval)
2581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002583 // Advance to next argument.
2584 // - skip until a blank found, taking care of backslashes
2585 // - skip blanks
2586 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 for (i = 0; i < 2 ; ++i)
2588 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002589 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (*arg++ == '\\' && *arg != NUL)
2591 ++arg;
2592 arg = skipwhite(arg);
2593 if (*arg != '=')
2594 break;
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002597 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002599 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2600 i = (int)STRLEN(IObuff) + 2;
2601 if (i + (arg - startarg) < IOSIZE)
2602 {
2603 // append the argument with the error
2604 STRCAT(IObuff, ": ");
2605 mch_memmove(IObuff + i, startarg, (arg - startarg));
2606 IObuff[i + (arg - startarg)] = NUL;
2607 }
2608 // make sure all characters are printable
2609 trans_characters(IObuff, IOSIZE);
2610
2611 ++no_wait_return; // wait_return() done later
2612 emsg((char *)IObuff); // show error highlighted
2613 --no_wait_return;
2614
2615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618
2619 arg = skipwhite(arg);
2620 }
2621
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002622theend:
2623 if (silent_mode && did_show)
2624 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002627 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002628 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002629 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002630 out_flush();
2631 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002632 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002633 }
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return OK;
2636}
2637
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002638/*
2639 * Call this when an option has been given a new value through a user command.
2640 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2641 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002642 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002643did_set_option(
2644 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002645 int opt_flags, // possibly with OPT_MODELINE
2646 int new_value, // value was replaced completely
2647 int value_checked) // value was checked to be safe, no need to set the
2648 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002649{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002650 long_u *p;
2651
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002652 options[opt_idx].flags |= P_WAS_SET;
2653
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002654 // When an option is set in the sandbox, from a modeline or in secure mode
2655 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2656 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002658 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002659#ifdef HAVE_SANDBOX
2660 || sandbox != 0
2661#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002662 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002663 *p = *p | P_INSECURE;
2664 else if (new_value)
2665 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002666}
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668/*
2669 * Convert a key name or string into a key value.
2670 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002671 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002673 int
2674string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
2676 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002677 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (*arg == '^')
2679 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002680 if (multi_byte)
2681 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 return *arg;
2683}
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685/*
2686 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2687 * maketitle() to create and display it.
2688 * When switching the title or icon off, call mch_restore_title() to get
2689 * the old value back.
2690 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002691 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002692did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 if (starting != NO_SCREEN
2695#ifdef FEAT_GUI
2696 && !gui.starting
2697#endif
2698 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702/*
2703 * set_options_bin - called when 'bin' changes value.
2704 */
2705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002706set_options_bin(
2707 int oldval,
2708 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002711 // The option values that are changed when 'bin' changes are
2712 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (newval)
2714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
2717 if (!(opt_flags & OPT_GLOBAL))
2718 {
2719 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2720 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2721 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2722 curbuf->b_p_et_nobin = curbuf->b_p_et;
2723 }
2724 if (!(opt_flags & OPT_LOCAL))
2725 {
2726 p_tw_nobin = p_tw;
2727 p_wm_nobin = p_wm;
2728 p_ml_nobin = p_ml;
2729 p_et_nobin = p_et;
2730 }
2731 }
2732
2733 if (!(opt_flags & OPT_GLOBAL))
2734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 curbuf->b_p_tw = 0; // no automatic line wrap
2736 curbuf->b_p_wm = 0; // no automatic line wrap
2737 curbuf->b_p_ml = 0; // no modelines
2738 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740 if (!(opt_flags & OPT_LOCAL))
2741 {
2742 p_tw = 0;
2743 p_wm = 0;
2744 p_ml = FALSE;
2745 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
2751 if (!(opt_flags & OPT_GLOBAL))
2752 {
2753 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2754 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2755 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2756 curbuf->b_p_et = curbuf->b_p_et_nobin;
2757 }
2758 if (!(opt_flags & OPT_LOCAL))
2759 {
2760 p_tw = p_tw_nobin;
2761 p_wm = p_wm_nobin;
2762 p_ml = p_ml_nobin;
2763 p_et = p_et_nobin;
2764 }
2765 }
2766}
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768/*
2769 * Expand environment variables for some string options.
2770 * These string options cannot be indirect!
2771 * If "val" is NULL expand the current value of the option.
2772 * Return pointer to NameBuff, or NULL when not expanded.
2773 */
2774 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002775option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002777 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2779 return NULL;
2780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // If val is longer than MAXPATHL no meaningful expansion can be done,
2782 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 if (val != NULL && STRLEN(val) > MAXPATHL)
2784 return NULL;
2785
2786 if (val == NULL)
2787 val = *(char_u **)options[opt_idx].var;
2788
2789 /*
2790 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2791 * Escape spaces when expanding 'tags', they are used to separate file
2792 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002793 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 */
2795 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002796 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002797#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002798 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2799#endif
2800 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return NULL;
2803
2804 return NameBuff;
2805}
2806
2807/*
2808 * After setting various option values: recompute variables that depend on
2809 * option values.
2810 */
2811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002812didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 (void)init_chartab();
2816
Bram Moolenaardac13472019-09-16 21:06:21 +02002817 didset_string_options();
2818
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002819#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002820 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002821 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002822 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002823 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002824#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002825 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002826 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002827#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002829 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002830#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002831 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002832}
2833
2834/*
2835 * More side effects of setting options.
2836 */
2837 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002838didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002839{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002841 (void)highlight_changed();
2842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002844 check_opt_wim();
2845
Bram Moolenaareed9d462021-02-15 20:38:25 +01002846 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002847 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002850 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002851
2852#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002854 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002855#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002856#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002857 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002858 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002859 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002860 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862}
2863
2864/*
2865 * Check for string options that are NULL (normally only termcap options).
2866 */
2867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002868check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 int opt_idx;
2871
2872 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2873 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2874 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2875}
2876
2877/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002878 * Return the option index found by a pointer into term_strings[].
2879 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002881 int
2882get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002884 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
2886 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2887 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002888 return opt_idx;
2889 return -1; // cannot happen: didn't find it!
2890}
2891
2892/*
2893 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2894 * Return the option index or -1 if not found.
2895 */
2896 int
2897set_term_option_alloced(char_u **p)
2898{
2899 int opt_idx = get_term_opt_idx(p);
2900
2901 if (opt_idx >= 0)
2902 options[opt_idx].flags |= P_ALLOCED;
2903 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904}
2905
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002906#if defined(FEAT_EVAL) || defined(PROTO)
2907/*
2908 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2909 * Return FALSE when it wasn't.
2910 * Return -1 for an unknown option.
2911 */
2912 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002913was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002914{
2915 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002916 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002917
2918 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002919 {
2920 flagp = insecure_flag(idx, opt_flags);
2921 return (*flagp & P_INSECURE) != 0;
2922 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002923 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002924 return -1;
2925}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002926
2927/*
2928 * Get a pointer to the flags used for the P_INSECURE flag of option
2929 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002930 * NOTE: Caller must make sure that "curwin" is set to the window from which
2931 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002932 */
2933 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002934insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002935{
2936 if (opt_flags & OPT_LOCAL)
2937 switch ((int)options[opt_idx].indir)
2938 {
2939#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002940 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002941#endif
2942#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002943# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002944 case PV_FDE: return &curwin->w_p_fde_flags;
2945 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002946# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002947# ifdef FEAT_BEVAL
2948 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2949# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002950 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002951 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002952# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002953 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002954# endif
2955#endif
2956 }
2957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002959 return &options[opt_idx].flags;
2960}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002961#endif
2962
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002963/*
2964 * Redraw the window title and/or tab page text later.
2965 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002966void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002967{
2968 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002969 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002970}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002973 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2974 * characters or characters in "allowed".
2975 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002976 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002977valid_name(char_u *val, char *allowed)
2978{
2979 char_u *s;
2980
2981 for (s = val; *s != NUL; ++s)
2982 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2983 return FALSE;
2984 return TRUE;
2985}
2986
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002987#if defined(FEAT_EVAL) || defined(PROTO)
2988/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002989 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002990 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002991 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002992 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002993set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002994{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002995 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2996 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002997 sctx_T new_script_ctx = script_ctx;
2998
Bram Moolenaar51258742020-05-03 17:19:33 +02002999 // Modeline already has the line number set.
3000 if (!(opt_flags & OPT_MODELINE))
3001 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 // Remember where the option was set. For local options need to do that
3004 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003005 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003006 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003007 if (both || (opt_flags & OPT_LOCAL))
3008 {
3009 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003010 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003011 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003012 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003013 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003014 if (both)
3015 // also setting the "all buffers" value
3016 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3017 new_script_ctx;
3018 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003019 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003020}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003021
3022/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003023 * Get the script context of global option "name".
3024 *
3025 */
3026 sctx_T *
3027get_option_sctx(char *name)
3028{
3029 int idx = findoption((char_u *)name);
3030
3031 if (idx >= 0)
3032 return &options[idx].script_ctx;
3033 siemsg("no such option: %s", name);
3034 return NULL;
3035}
3036
3037/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003038 * Set the script_ctx for a termcap option.
3039 * "name" must be the two character code, e.g. "RV".
3040 * When "name" is NULL use "opt_idx".
3041 */
3042 void
3043set_term_option_sctx_idx(char *name, int opt_idx)
3044{
3045 char_u buf[5];
3046 int idx;
3047
3048 if (name == NULL)
3049 idx = opt_idx;
3050 else
3051 {
3052 buf[0] = 't';
3053 buf[1] = '_';
3054 buf[2] = name[0];
3055 buf[3] = name[1];
3056 buf[4] = 0;
3057 idx = findoption(buf);
3058 }
3059 if (idx >= 0)
3060 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3061}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003062#endif
3063
Bram Moolenaarf4140482020-02-15 23:06:45 +01003064#if defined(FEAT_EVAL)
3065/*
3066 * Apply the OptionSet autocommand.
3067 */
3068 static void
3069apply_optionset_autocmd(
3070 int opt_idx,
3071 long opt_flags,
3072 long oldval,
3073 long oldval_g,
3074 long newval,
3075 char *errmsg)
3076{
3077 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3078
3079 // Don't do this while starting up, failure or recursively.
3080 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3081 return;
3082
3083 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3084 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3085 oldval_g);
3086 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3087 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3088 (opt_flags & OPT_LOCAL) ? "local" : "global");
3089 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3090 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3091 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3092 if (opt_flags & OPT_LOCAL)
3093 {
3094 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3095 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3096 }
3097 if (opt_flags & OPT_GLOBAL)
3098 {
3099 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3100 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3101 }
3102 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3103 {
3104 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3105 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3106 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3107 }
3108 if (opt_flags & OPT_MODELINE)
3109 {
3110 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3111 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3112 }
3113 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3114 NULL, FALSE, NULL);
3115 reset_v_option_vars();
3116}
3117#endif
3118
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003119#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003120/*
3121 * Process the updated 'arabic' option value.
3122 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003123 char *
3124did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003125{
3126 char *errmsg = NULL;
3127
3128 if (curwin->w_p_arab)
3129 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003130 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003131 if (!p_tbidi)
3132 {
3133 // set rightleft mode
3134 if (!curwin->w_p_rl)
3135 {
3136 curwin->w_p_rl = TRUE;
3137 changed_window_setting();
3138 }
3139
3140 // Enable Arabic shaping (major part of what Arabic requires)
3141 if (!p_arshape)
3142 {
3143 p_arshape = TRUE;
3144 redraw_later_clear();
3145 }
3146 }
3147
3148 // Arabic requires a utf-8 encoding, inform the user if it's not
3149 // set.
3150 if (STRCMP(p_enc, "utf-8") != 0)
3151 {
3152 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3153
3154 msg_source(HL_ATTR(HLF_W));
3155 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3156#ifdef FEAT_EVAL
3157 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3158#endif
3159 }
3160
3161 // set 'delcombine'
3162 p_deco = TRUE;
3163
3164# ifdef FEAT_KEYMAP
3165 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003166 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3167 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003168# endif
3169 }
3170 else
3171 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003172 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003173 if (!p_tbidi)
3174 {
3175 // reset rightleft mode
3176 if (curwin->w_p_rl)
3177 {
3178 curwin->w_p_rl = FALSE;
3179 changed_window_setting();
3180 }
3181
3182 // 'arabicshape' isn't reset, it is a global option and
3183 // another window may still need it "on".
3184 }
3185
3186 // 'delcombine' isn't reset, it is a global option and another
3187 // window may still want it "on".
3188
3189# ifdef FEAT_KEYMAP
3190 // Revert to the default keymap
3191 curbuf->b_p_iminsert = B_IMODE_NONE;
3192 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3193# endif
3194 }
3195
3196 return errmsg;
3197}
3198#endif
3199
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003200#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3201/*
3202 * Process the updated 'autochdir' option value.
3203 */
3204 char *
3205did_set_autochdir(optset_T *args UNUSED)
3206{
3207 // Change directories when the 'acd' option is set now.
3208 DO_AUTOCHDIR;
3209 return NULL;
3210}
3211#endif
3212
3213#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3214/*
3215 * Process the updated 'ballooneval' option value.
3216 */
3217 char *
3218did_set_ballooneval(optset_T *args)
3219{
3220 if (balloonEvalForTerm)
3221 return NULL;
3222
3223 if (p_beval && !args->os_oldval.boolean)
3224 gui_mch_enable_beval_area(balloonEval);
3225 else if (!p_beval && args->os_oldval.boolean)
3226 gui_mch_disable_beval_area(balloonEval);
3227
3228 return NULL;
3229}
3230#endif
3231
3232#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3233/*
3234 * Process the updated 'balloonevalterm' option value.
3235 */
3236 char *
3237did_set_balloonevalterm(optset_T *args UNUSED)
3238{
3239 mch_bevalterm_changed();
3240 return NULL;
3241}
3242#endif
3243
3244/*
3245 * Process the updated 'binary' option value.
3246 */
3247 char *
3248did_set_binary(optset_T *args)
3249{
3250 // when 'bin' is set also set some other options
3251 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3252 redraw_titles();
3253
3254 return NULL;
3255}
3256
3257#if defined(FEAT_LINEBREAK) || defined(PROTO)
3258/*
3259 * Called when the 'breakat' option changes value.
3260 */
3261 char *
3262did_set_breakat(optset_T *args UNUSED)
3263{
3264 char_u *p;
3265 int i;
3266
3267 for (i = 0; i < 256; i++)
3268 breakat_flags[i] = FALSE;
3269
3270 if (p_breakat != NULL)
3271 for (p = p_breakat; *p; p++)
3272 breakat_flags[*p] = TRUE;
3273
3274 return NULL;
3275}
3276#endif
3277
3278/*
3279 * Process the updated 'buflisted' option value.
3280 */
3281 char *
3282did_set_buflisted(optset_T *args)
3283{
3284 // when 'buflisted' changes, trigger autocommands
3285 if (args->os_oldval.boolean != curbuf->b_p_bl)
3286 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3287 NULL, NULL, TRUE, curbuf);
3288 return NULL;
3289}
3290
3291/*
3292 * Process the new 'cmdheight' option value.
3293 */
3294 char *
3295did_set_cmdheight(optset_T *args)
3296{
3297 long old_value = args->os_oldval.number;
3298 char *errmsg = NULL;
3299
3300 // if p_ch changed value, change the command line height
3301 if (p_ch < 1)
3302 {
3303 errmsg = e_argument_must_be_positive;
3304 p_ch = 1;
3305 }
3306 if (p_ch > Rows - min_rows() + 1)
3307 p_ch = Rows - min_rows() + 1;
3308
3309 // Only compute the new window layout when startup has been
3310 // completed. Otherwise the frame sizes may be wrong.
3311 if ((p_ch != old_value
3312 || tabline_height() + topframe->fr_height != Rows - p_ch)
3313 && full_screen
3314#ifdef FEAT_GUI
3315 && !gui.starting
3316#endif
3317 )
3318 command_height();
3319
3320 return errmsg;
3321}
3322
3323/*
3324 * Process the updated 'compatible' option value.
3325 */
3326 char *
3327did_set_compatible(optset_T *args UNUSED)
3328{
3329 compatible_set();
3330 return NULL;
3331}
3332
3333#if defined(FEAT_CONCEAL) || defined(PROTO)
3334/*
3335 * Process the new 'conceallevel' option value.
3336 */
3337 char *
3338did_set_conceallevel(optset_T *args UNUSED)
3339{
3340 char *errmsg = NULL;
3341
3342 if (curwin->w_p_cole < 0)
3343 {
3344 errmsg = e_argument_must_be_positive;
3345 curwin->w_p_cole = 0;
3346 }
3347 else if (curwin->w_p_cole > 3)
3348 {
3349 errmsg = e_invalid_argument;
3350 curwin->w_p_cole = 3;
3351 }
3352
3353 return errmsg;
3354}
3355#endif
3356
3357#if defined(FEAT_DIFF) || defined(PROTO)
3358/*
3359 * Process the updated 'diff' option value.
3360 */
3361 char *
3362did_set_diff(optset_T *args UNUSED)
3363{
3364 // May add or remove the buffer from the list of diff buffers.
3365 diff_buf_adjust(curwin);
3366# ifdef FEAT_FOLDING
3367 if (foldmethodIsDiff(curwin))
3368 foldUpdateAll(curwin);
3369# endif
3370 return NULL;
3371}
3372#endif
3373
3374/*
3375 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3376 * option value.
3377 */
3378 char *
3379did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3380{
3381 // redraw the window title and tab page text
3382 redraw_titles();
3383 return NULL;
3384}
3385
3386/*
3387 * Process the updated 'equalalways' option value.
3388 */
3389 char *
3390did_set_equalalways(optset_T *args)
3391{
3392 if (p_ea && !args->os_oldval.boolean)
3393 win_equal(curwin, FALSE, 0);
3394
3395 return NULL;
3396}
3397
3398#if defined(FEAT_FOLDING) || defined(PROTO)
3399/*
3400 * Process the new 'foldcolumn' option value.
3401 */
3402 char *
3403did_set_foldcolumn(optset_T *args UNUSED)
3404{
3405 char *errmsg = NULL;
3406
3407 if (curwin->w_p_fdc < 0)
3408 {
3409 errmsg = e_argument_must_be_positive;
3410 curwin->w_p_fdc = 0;
3411 }
3412 else if (curwin->w_p_fdc > 12)
3413 {
3414 errmsg = e_invalid_argument;
3415 curwin->w_p_fdc = 12;
3416 }
3417
3418 return errmsg;
3419}
3420
3421/*
3422 * Process the new 'foldlevel' option value.
3423 */
3424 char *
3425did_set_foldlevel(optset_T *args UNUSED)
3426{
3427 if (curwin->w_p_fdl < 0)
3428 curwin->w_p_fdl = 0;
3429 newFoldLevel();
3430 return NULL;
3431}
3432
3433/*
3434 * Process the new 'foldminlines' option value.
3435 */
3436 char *
3437did_set_foldminlines(optset_T *args UNUSED)
3438{
3439 foldUpdateAll(curwin);
3440 return NULL;
3441}
3442
3443/*
3444 * Process the new 'foldnestmax' option value.
3445 */
3446 char *
3447did_set_foldnestmax(optset_T *args UNUSED)
3448{
3449 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3450 foldUpdateAll(curwin);
3451 return NULL;
3452}
3453#endif
3454
3455#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3456/*
3457 * Process the updated 'hlsearch' option value.
3458 */
3459 char *
3460did_set_hlsearch(optset_T *args UNUSED)
3461{
3462 // when 'hlsearch' is set or reset: reset no_hlsearch
3463 set_no_hlsearch(FALSE);
3464 return NULL;
3465}
3466#endif
3467
3468/*
3469 * Process the updated 'ignorecase' option value.
3470 */
3471 char *
3472did_set_ignorecase(optset_T *args UNUSED)
3473{
3474 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3475 if (p_hls)
3476 redraw_all_later(UPD_SOME_VALID);
3477 return NULL;
3478}
3479
3480#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3481/*
3482 * Process the updated 'imdisable' option value.
3483 */
3484 char *
3485did_set_imdisable(optset_T *args UNUSED)
3486{
3487 // Only de-activate it here, it will be enabled when changing mode.
3488 if (p_imdisable)
3489 im_set_active(FALSE);
3490 else if (State & MODE_INSERT)
3491 // When the option is set from an autocommand, it may need to take
3492 // effect right away.
3493 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3494 return NULL;
3495}
3496#endif
3497
3498/*
3499 * Process the new 'iminsert' option value.
3500 */
3501 char *
3502did_set_iminsert(optset_T *args UNUSED)
3503{
3504 char *errmsg = NULL;
3505
3506 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3507 {
3508 errmsg = e_invalid_argument;
3509 curbuf->b_p_iminsert = B_IMODE_NONE;
3510 }
3511 p_iminsert = curbuf->b_p_iminsert;
3512 if (termcap_active) // don't do this in the alternate screen
3513 showmode();
3514#if defined(FEAT_KEYMAP)
3515 // Show/unshow value of 'keymap' in status lines.
3516 status_redraw_curbuf();
3517#endif
3518
3519 return errmsg;
3520}
3521
3522/*
3523 * Process the new 'imsearch' option value.
3524 */
3525 char *
3526did_set_imsearch(optset_T *args UNUSED)
3527{
3528 char *errmsg = NULL;
3529
3530 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3531 {
3532 errmsg = e_invalid_argument;
3533 curbuf->b_p_imsearch = B_IMODE_NONE;
3534 }
3535 p_imsearch = curbuf->b_p_imsearch;
3536
3537 return errmsg;
3538}
3539
3540#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3541/*
3542 * Process the new 'imstyle' option value.
3543 */
3544 char *
3545did_set_imstyle(optset_T *args UNUSED)
3546{
3547 char *errmsg = NULL;
3548
3549 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3550 errmsg = e_invalid_argument;
3551
3552 return errmsg;
3553}
3554#endif
3555
3556/*
3557 * Process the updated 'insertmode' option value.
3558 */
3559 char *
3560did_set_insertmode(optset_T *args)
3561{
3562 // when 'insertmode' is set from an autocommand need to do work here
3563 if (p_im)
3564 {
3565 if ((State & MODE_INSERT) == 0)
3566 need_start_insertmode = TRUE;
3567 stop_insert_mode = FALSE;
3568 }
3569 // only reset if it was set previously
3570 else if (args->os_oldval.boolean)
3571 {
3572 need_start_insertmode = FALSE;
3573 stop_insert_mode = TRUE;
3574 if (restart_edit != 0 && mode_displayed)
3575 clear_cmdline = TRUE; // remove "(insert)"
3576 restart_edit = 0;
3577 }
3578
3579 return NULL;
3580}
3581
3582#if defined(FEAT_LANGMAP) || defined(PROTO)
3583/*
3584 * Process the updated 'langnoremap' option value.
3585 */
3586 char *
3587did_set_langnoremap(optset_T *args UNUSED)
3588{
3589 // 'langnoremap' -> !'langremap'
3590 p_lrm = !p_lnr;
3591 return NULL;
3592}
3593
3594/*
3595 * Process the updated 'langremap' option value.
3596 */
3597 char *
3598did_set_langremap(optset_T *args UNUSED)
3599{
3600 // 'langremap' -> !'langnoremap'
3601 p_lnr = !p_lrm;
3602 return NULL;
3603}
3604#endif
3605
3606/*
3607 * Process the new 'laststatus' option value.
3608 */
3609 char *
3610did_set_laststatus(optset_T *args UNUSED)
3611{
3612 last_status(FALSE); // (re)set last window status line
3613 return NULL;
3614}
3615
3616#if defined(FEAT_GUI) || defined(PROTO)
3617/*
3618 * Process the new 'linespace' option value.
3619 */
3620 char *
3621did_set_linespace(optset_T *args UNUSED)
3622{
3623 // Recompute gui.char_height and resize the Vim window to keep the
3624 // same number of lines.
3625 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3626 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3627 return NULL;
3628}
3629#endif
3630
3631/*
3632 * Process the updated 'lisp' option value.
3633 */
3634 char *
3635did_set_lisp(optset_T *args UNUSED)
3636{
3637 // When 'lisp' option changes include/exclude '-' in keyword characters.
3638 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3639 return NULL;
3640}
3641
3642/*
3643 * Process the new 'maxcombine' option value.
3644 */
3645 char *
3646did_set_maxcombine(optset_T *args UNUSED)
3647{
3648 if (p_mco > MAX_MCO)
3649 p_mco = MAX_MCO;
3650 else if (p_mco < 0)
3651 p_mco = 0;
3652 screenclear(); // will re-allocate the screen
3653 return NULL;
3654}
3655
3656/*
3657 * Process the updated 'modifiable' option value.
3658 */
3659 char *
3660did_set_modifiable(optset_T *args UNUSED)
3661{
3662 // when 'modifiable' is changed, redraw the window title
3663
3664# ifdef FEAT_TERMINAL
3665 // Cannot set 'modifiable' when in Terminal mode.
3666 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3667 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3668 {
3669 curbuf->b_p_ma = FALSE;
3670 args->os_doskip = TRUE;
3671 return e_cannot_make_terminal_with_running_job_modifiable;
3672 }
3673# endif
3674 redraw_titles();
3675
3676 return NULL;
3677}
3678
3679/*
3680 * Process the updated 'modified' option value.
3681 */
3682 char *
3683did_set_modified(optset_T *args)
3684{
3685 if (!args->os_newval.boolean)
3686 save_file_ff(curbuf); // Buffer is unchanged
3687 redraw_titles();
3688 modified_was_set = args->os_newval.boolean;
3689 return NULL;
3690}
3691
3692#if defined(FEAT_GUI) || defined(PROTO)
3693/*
3694 * Process the updated 'mousehide' option value.
3695 */
3696 char *
3697did_set_mousehide(optset_T *args UNUSED)
3698{
3699 if (!p_mh)
3700 gui_mch_mousehide(FALSE);
3701 return NULL;
3702}
3703#endif
3704
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003705/*
3706 * Process the updated 'number' or 'relativenumber' option value.
3707 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003708 char *
3709did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003710{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003711#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003712 if (gui.in_use
3713 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3714 && curbuf->b_signlist != NULL)
3715 {
3716 // If the 'number' or 'relativenumber' options are modified and
3717 // 'signcolumn' is set to 'number', then clear the screen for a full
3718 // refresh. Otherwise the sign icons are not displayed properly in the
3719 // number column. If the 'number' option is set and only the
3720 // 'relativenumber' option is toggled, then don't refresh the screen
3721 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003722 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003723 redraw_all_later(UPD_CLEAR);
3724 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003725#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003726 return NULL;
3727}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003728
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003729#if defined(FEAT_LINEBREAK) || defined(PROTO)
3730/*
3731 * Process the new 'numberwidth' option value.
3732 */
3733 char *
3734did_set_numberwidth(optset_T *args UNUSED)
3735{
3736 char *errmsg = NULL;
3737
3738 // 'numberwidth' must be positive
3739 if (curwin->w_p_nuw < 1)
3740 {
3741 errmsg = e_argument_must_be_positive;
3742 curwin->w_p_nuw = 1;
3743 }
3744 if (curwin->w_p_nuw > 20)
3745 {
3746 errmsg = e_invalid_argument;
3747 curwin->w_p_nuw = 20;
3748 }
3749 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3750
3751 return errmsg;
3752}
3753#endif
3754
3755/*
3756 * Process the updated 'paste' option value. Called after p_paste was set or
3757 * reset. When 'paste' is set or reset also change other options.
3758 */
3759 char *
3760did_set_paste(optset_T *args UNUSED)
3761{
3762 static int old_p_paste = FALSE;
3763 static int save_sm = 0;
3764 static int save_sta = 0;
3765 static int save_ru = 0;
3766#ifdef FEAT_RIGHTLEFT
3767 static int save_ri = 0;
3768 static int save_hkmap = 0;
3769#endif
3770 buf_T *buf;
3771
3772 if (p_paste)
3773 {
3774 // Paste switched from off to on.
3775 // Save the current values, so they can be restored later.
3776 if (!old_p_paste)
3777 {
3778 // save options for each buffer
3779 FOR_ALL_BUFFERS(buf)
3780 {
3781 buf->b_p_tw_nopaste = buf->b_p_tw;
3782 buf->b_p_wm_nopaste = buf->b_p_wm;
3783 buf->b_p_sts_nopaste = buf->b_p_sts;
3784 buf->b_p_ai_nopaste = buf->b_p_ai;
3785 buf->b_p_et_nopaste = buf->b_p_et;
3786#ifdef FEAT_VARTABS
3787 if (buf->b_p_vsts_nopaste)
3788 vim_free(buf->b_p_vsts_nopaste);
3789 buf->b_p_vsts_nopaste =
3790 buf->b_p_vsts && buf->b_p_vsts != empty_option
3791 ? vim_strsave(buf->b_p_vsts) : NULL;
3792#endif
3793 }
3794
3795 // save global options
3796 save_sm = p_sm;
3797 save_sta = p_sta;
3798 save_ru = p_ru;
3799#ifdef FEAT_RIGHTLEFT
3800 save_ri = p_ri;
3801 save_hkmap = p_hkmap;
3802#endif
3803 // save global values for local buffer options
3804 p_ai_nopaste = p_ai;
3805 p_et_nopaste = p_et;
3806 p_sts_nopaste = p_sts;
3807 p_tw_nopaste = p_tw;
3808 p_wm_nopaste = p_wm;
3809#ifdef FEAT_VARTABS
3810 if (p_vsts_nopaste)
3811 vim_free(p_vsts_nopaste);
3812 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3813 ? vim_strsave(p_vsts) : NULL;
3814#endif
3815 }
3816
3817 // Always set the option values, also when 'paste' is set when it is
3818 // already on. Set options for each buffer.
3819 FOR_ALL_BUFFERS(buf)
3820 {
3821 buf->b_p_tw = 0; // textwidth is 0
3822 buf->b_p_wm = 0; // wrapmargin is 0
3823 buf->b_p_sts = 0; // softtabstop is 0
3824 buf->b_p_ai = 0; // no auto-indent
3825 buf->b_p_et = 0; // no expandtab
3826#ifdef FEAT_VARTABS
3827 if (buf->b_p_vsts)
3828 free_string_option(buf->b_p_vsts);
3829 buf->b_p_vsts = empty_option;
3830 VIM_CLEAR(buf->b_p_vsts_array);
3831#endif
3832 }
3833
3834 // set global options
3835 p_sm = 0; // no showmatch
3836 p_sta = 0; // no smarttab
3837 if (p_ru)
3838 status_redraw_all(); // redraw to remove the ruler
3839 p_ru = 0; // no ruler
3840#ifdef FEAT_RIGHTLEFT
3841 p_ri = 0; // no reverse insert
3842 p_hkmap = 0; // no Hebrew keyboard
3843#endif
3844 // set global values for local buffer options
3845 p_tw = 0;
3846 p_wm = 0;
3847 p_sts = 0;
3848 p_ai = 0;
3849#ifdef FEAT_VARTABS
3850 if (p_vsts)
3851 free_string_option(p_vsts);
3852 p_vsts = empty_option;
3853#endif
3854 }
3855
3856 // Paste switched from on to off: Restore saved values.
3857 else if (old_p_paste)
3858 {
3859 // restore options for each buffer
3860 FOR_ALL_BUFFERS(buf)
3861 {
3862 buf->b_p_tw = buf->b_p_tw_nopaste;
3863 buf->b_p_wm = buf->b_p_wm_nopaste;
3864 buf->b_p_sts = buf->b_p_sts_nopaste;
3865 buf->b_p_ai = buf->b_p_ai_nopaste;
3866 buf->b_p_et = buf->b_p_et_nopaste;
3867#ifdef FEAT_VARTABS
3868 if (buf->b_p_vsts)
3869 free_string_option(buf->b_p_vsts);
3870 buf->b_p_vsts = buf->b_p_vsts_nopaste
3871 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3872 vim_free(buf->b_p_vsts_array);
3873 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3874 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3875 else
3876 buf->b_p_vsts_array = NULL;
3877#endif
3878 }
3879
3880 // restore global options
3881 p_sm = save_sm;
3882 p_sta = save_sta;
3883 if (p_ru != save_ru)
3884 status_redraw_all(); // redraw to draw the ruler
3885 p_ru = save_ru;
3886#ifdef FEAT_RIGHTLEFT
3887 p_ri = save_ri;
3888 p_hkmap = save_hkmap;
3889#endif
3890 // set global values for local buffer options
3891 p_ai = p_ai_nopaste;
3892 p_et = p_et_nopaste;
3893 p_sts = p_sts_nopaste;
3894 p_tw = p_tw_nopaste;
3895 p_wm = p_wm_nopaste;
3896#ifdef FEAT_VARTABS
3897 if (p_vsts)
3898 free_string_option(p_vsts);
3899 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3900#endif
3901 }
3902
3903 old_p_paste = p_paste;
3904
3905 return NULL;
3906}
3907
3908
3909#ifdef FEAT_QUICKFIX
3910/*
3911 * Process the updated 'previewwindow' option value.
3912 */
3913 char *
3914did_set_previewwindow(optset_T *args)
3915{
3916 if (!curwin->w_p_pvw)
3917 return NULL;
3918
3919 // There can be only one window with 'previewwindow' set.
3920 win_T *win;
3921
3922 FOR_ALL_WINDOWS(win)
3923 if (win->w_p_pvw && win != curwin)
3924 {
3925 curwin->w_p_pvw = FALSE;
3926 args->os_doskip = TRUE;
3927 return e_preview_window_already_exists;
3928 }
3929
3930 return NULL;
3931}
3932#endif
3933
3934#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3935/*
3936 * Process the new 'pyxversion' option value.
3937 */
3938 char *
3939did_set_pyxversion(optset_T *args UNUSED)
3940{
3941 char *errmsg = NULL;
3942
3943 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3944 errmsg = e_invalid_argument;
3945
3946 return errmsg;
3947}
3948#endif
3949
3950/*
3951 * Process the updated 'readonly' option value.
3952 */
3953 char *
3954did_set_readonly(optset_T *args)
3955{
3956 // when 'readonly' is reset globally, also reset readonlymode
3957 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3958 readonlymode = FALSE;
3959
3960 // when 'readonly' is set may give W10 again
3961 if (curbuf->b_p_ro)
3962 curbuf->b_did_warn = FALSE;
3963
3964 redraw_titles();
3965
3966 return NULL;
3967}
3968
3969/*
3970 * Process the updated 'scrollbind' option value.
3971 */
3972 char *
3973did_set_scrollbind(optset_T *args UNUSED)
3974{
3975 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3976 // at the end of normal_cmd()
3977 if (!curwin->w_p_scb)
3978 return NULL;
3979
3980 do_check_scrollbind(FALSE);
3981 curwin->w_scbind_pos = curwin->w_topline;
3982 return NULL;
3983}
3984
3985#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3986/*
3987 * Process the updated 'shellslash' option value.
3988 */
3989 char *
3990did_set_shellslash(optset_T *args UNUSED)
3991{
3992 if (p_ssl)
3993 {
3994 psepc = '/';
3995 psepcN = '\\';
3996 pseps[0] = '/';
3997 }
3998 else
3999 {
4000 psepc = '\\';
4001 psepcN = '/';
4002 pseps[0] = '\\';
4003 }
4004
4005 // need to adjust the file name arguments and buffer names.
4006 buflist_slash_adjust();
4007 alist_slash_adjust();
4008# ifdef FEAT_EVAL
4009 scriptnames_slash_adjust();
4010# endif
4011 return NULL;
4012}
4013#endif
4014
4015/*
4016 * Process the new 'shiftwidth' or the 'tabstop' option value.
4017 */
4018 char *
4019did_set_shiftwidth_tabstop(optset_T *args)
4020{
4021 long *pp = (long *)args->os_varp;
4022 char *errmsg = NULL;
4023
4024 if (curbuf->b_p_sw < 0)
4025 {
4026 errmsg = e_argument_must_be_positive;
4027#ifdef FEAT_VARTABS
4028 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4029 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4030 ? tabstop_first(curbuf->b_p_vts_array)
4031 : curbuf->b_p_ts;
4032#else
4033 curbuf->b_p_sw = curbuf->b_p_ts;
4034#endif
4035 }
4036
4037#ifdef FEAT_FOLDING
4038 if (foldmethodIsIndent(curwin))
4039 foldUpdateAll(curwin);
4040#endif
4041 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4042 // parse 'cinoptions'.
4043 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4044 parse_cino(curbuf);
4045
4046 return errmsg;
4047}
4048
4049/*
4050 * Process the new 'showtabline' option value.
4051 */
4052 char *
4053did_set_showtabline(optset_T *args UNUSED)
4054{
4055 shell_new_rows(); // recompute window positions and heights
4056 return NULL;
4057}
4058
4059/*
4060 * Process the updated 'smoothscroll' option value.
4061 */
4062 char *
4063did_set_smoothscroll(optset_T *args UNUSED)
4064{
4065 if (curwin->w_p_sms)
4066 return NULL;
4067
4068 curwin->w_skipcol = 0;
4069 changed_line_abv_curs();
4070 return NULL;
4071}
4072
4073#if defined(FEAT_SPELL) || defined(PROTO)
4074/*
4075 * Process the updated 'spell' option value.
4076 */
4077 char *
4078did_set_spell(optset_T *args UNUSED)
4079{
4080 if (curwin->w_p_spell)
4081 return parse_spelllang(curwin);
4082
4083 return NULL;
4084}
4085#endif
4086
4087/*
4088 * Process the updated 'swapfile' option value.
4089 */
4090 char *
4091did_set_swapfile(optset_T *args UNUSED)
4092{
4093 // when 'swf' is set, create swapfile, when reset remove swapfile
4094 if (curbuf->b_p_swf && p_uc)
4095 ml_open_file(curbuf); // create the swap file
4096 else
4097 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4098 // buf->b_p_swf
4099 mf_close_file(curbuf, TRUE); // remove the swap file
4100 return NULL;
4101}
4102
4103#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004104 char *
4105did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004106{
4107# ifdef FEAT_VTP
4108 // Do not turn on 'tgc' when 24-bit colors are not supported.
4109 if (
4110# ifdef VIMDLL
4111 !gui.in_use && !gui.starting &&
4112# endif
4113 !has_vtp_working())
4114 {
4115 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004116 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004117 return e_24_bit_colors_are_not_supported_on_this_environment;
4118 }
4119 if (is_term_win32())
4120 swap_tcap();
4121# endif
4122# ifdef FEAT_GUI
4123 if (!gui.in_use && !gui.starting)
4124# endif
4125 highlight_gui_started();
4126# ifdef FEAT_VTP
4127 // reset t_Co
4128 if (is_term_win32())
4129 {
4130 control_console_color_rgb();
4131 set_termname(T_NAME);
4132 init_highlight(TRUE, FALSE);
4133 }
4134# endif
4135# ifdef FEAT_TERMINAL
4136 term_update_colors_all();
4137 term_update_palette_all();
4138 term_update_wincolor_all();
4139# endif
4140
4141 return NULL;
4142}
4143#endif
4144
4145/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004146 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004148 char *
4149did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004151 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004153 // when 'terse' is set change 'shortmess'
4154 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004156 // insert 's' in p_shm
4157 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004158 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004159 STRCPY(IObuff, p_shm);
4160 STRCAT(IObuff, "s");
4161 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004162 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004163 // remove 's' from p_shm
4164 else if (!p_terse && p != NULL)
4165 STRMOVE(p, p + 1);
4166 return NULL;
4167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004169/*
4170 * Process the updated 'textauto' option value.
4171 */
4172 char *
4173did_set_textauto(optset_T *args)
4174{
4175 // when 'textauto' is set or reset also change 'fileformats'
4176 set_string_option_direct((char_u *)"ffs", -1,
4177 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4178 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004179
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004180 return NULL;
4181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004183/*
4184 * Process the updated 'textmode' option value.
4185 */
4186 char *
4187did_set_textmode(optset_T *args)
4188{
4189 // when 'textmode' is set or reset also change 'fileformat'
4190 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4191
4192 return NULL;
4193}
4194
4195/*
4196 * Process the new 'textwidth' option value.
4197 */
4198 char *
4199did_set_textwidth(optset_T *args UNUSED)
4200{
4201 char *errmsg = NULL;
4202
4203 if (curbuf->b_p_tw < 0)
4204 {
4205 errmsg = e_argument_must_be_positive;
4206 curbuf->b_p_tw = 0;
4207 }
4208#ifdef FEAT_SYN_HL
4209 {
4210 win_T *wp;
4211 tabpage_T *tp;
4212
4213 FOR_ALL_TAB_WINDOWS(tp, wp)
4214 check_colorcolumn(wp);
4215 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004216#endif
4217
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004218 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219}
4220
4221/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004222 * Process the updated 'title' or the 'icon' option value.
4223 */
4224 char *
4225did_set_title_icon(optset_T *args UNUSED)
4226{
4227 // when 'title' changed, may need to change the title; same for 'icon'
4228 did_set_title();
4229 return NULL;
4230}
4231
4232/*
4233 * Process the new 'titlelen' option value.
4234 */
4235 char *
4236did_set_titlelen(optset_T *args)
4237{
4238 long old_value = args->os_oldval.number;
4239 char *errmsg = NULL;
4240
4241 // if 'titlelen' has changed, redraw the title
4242 if (p_titlelen < 0)
4243 {
4244 errmsg = e_argument_must_be_positive;
4245 p_titlelen = 85;
4246 }
4247 if (starting != NO_SCREEN && old_value != p_titlelen)
4248 need_maketitle = TRUE;
4249
4250 return errmsg;
4251}
4252
4253#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4254/*
4255 * Process the updated 'undofile' option value.
4256 */
4257 char *
4258did_set_undofile(optset_T *args)
4259{
4260 // Only take action when the option was set.
4261 if (!curbuf->b_p_udf && !p_udf)
4262 return NULL;
4263
4264 // When reset we do not delete the undo file, the option may be set again
4265 // without making any changes in between.
4266 char_u hash[UNDO_HASH_SIZE];
4267 buf_T *save_curbuf = curbuf;
4268
4269 FOR_ALL_BUFFERS(curbuf)
4270 {
4271 // When 'undofile' is set globally: for every buffer, otherwise
4272 // only for the current buffer: Try to read in the undofile,
4273 // if one exists, the buffer wasn't changed and the buffer was
4274 // loaded
4275 if ((curbuf == save_curbuf
4276 || (args->os_flags & OPT_GLOBAL)
4277 || args->os_flags == 0)
4278 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4279 {
4280#ifdef FEAT_CRYPT
Christian Brabandtaae58342023-04-23 17:50:22 +01004281 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004282 continue;
4283#endif
4284 u_compute_hash(hash);
4285 u_read_undo(NULL, hash, curbuf->b_fname);
4286 }
4287 }
4288 curbuf = save_curbuf;
4289
4290 return NULL;
4291}
4292#endif
4293
4294/*
4295 * Process the new global 'undolevels' option value.
4296 */
4297 static void
4298update_global_undolevels(long value, long old_value)
4299{
4300 // sync undo before 'undolevels' changes
4301
4302 // use the old value, otherwise u_sync() may not work properly
4303 p_ul = old_value;
4304 u_sync(TRUE);
4305 p_ul = value;
4306}
4307
4308/*
4309 * Process the new buffer local 'undolevels' option value.
4310 */
4311 static void
4312update_buflocal_undolevels(long value, long old_value)
4313{
4314 // use the old value, otherwise u_sync() may not work properly
4315 curbuf->b_p_ul = old_value;
4316 u_sync(TRUE);
4317 curbuf->b_p_ul = value;
4318}
4319
4320/*
4321 * Process the new 'undolevels' option value.
4322 */
4323 char *
4324did_set_undolevels(optset_T *args)
4325{
4326 long *pp = (long *)args->os_varp;
4327
4328 if (pp == &p_ul) // global 'undolevels'
4329 update_global_undolevels(args->os_newval.number,
4330 args->os_oldval.number);
4331 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4332 update_buflocal_undolevels(args->os_newval.number,
4333 args->os_oldval.number);
4334
4335 return NULL;
4336}
4337
4338/*
4339 * Process the new 'updatecount' option value.
4340 */
4341 char *
4342did_set_updatecount(optset_T *args)
4343{
4344 long old_value = args->os_oldval.number;
4345 char *errmsg = NULL;
4346
4347 // when 'updatecount' changes from zero to non-zero, open swap files
4348 if (p_uc < 0)
4349 {
4350 errmsg = e_argument_must_be_positive;
4351 p_uc = 100;
4352 }
4353 if (p_uc && !old_value)
4354 ml_open_files();
4355
4356 return errmsg;
4357}
4358
4359/*
4360 * Process the updated 'weirdinvert' option value.
4361 */
4362 char *
4363did_set_weirdinvert(optset_T *args)
4364{
4365 // When 'weirdinvert' changed, set/reset 't_xs'.
4366 // Then set 'weirdinvert' according to value of 't_xs'.
4367 if (p_wiv && !args->os_oldval.boolean)
4368 T_XS = (char_u *)"y";
4369 else if (!p_wiv && args->os_oldval.boolean)
4370 T_XS = empty_option;
4371 p_wiv = (*T_XS != NUL);
4372
4373 return NULL;
4374}
4375
4376/*
4377 * Process the new 'window' option value.
4378 */
4379 char *
4380did_set_window(optset_T *args UNUSED)
4381{
4382 if (p_window < 1)
4383 p_window = 1;
4384 else if (p_window >= Rows)
4385 p_window = Rows - 1;
4386 return NULL;
4387}
4388
4389/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004390 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004392 char *
4393did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004395 long *pp = (long *)args->os_varp;
4396 char *errmsg = NULL;
4397
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004398 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004400 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004401 p_wh = 1;
4402 }
4403 if (p_wmh > p_wh)
4404 {
4405 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4406 p_wh = p_wmh;
4407 }
4408 if (p_hh < 0)
4409 {
4410 errmsg = e_argument_must_be_positive;
4411 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004414 // Change window height NOW
4415 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004417 if (pp == &p_wh && curwin->w_height < p_wh)
4418 win_setheight((int)p_wh);
4419 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4420 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004423 return errmsg;
4424}
4425
4426/*
4427 * Process the new 'winminheight' option value.
4428 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004429 char *
4430did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004431{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004432 char *errmsg = NULL;
4433
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004434 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004436 errmsg = e_argument_must_be_positive;
4437 p_wmh = 0;
4438 }
4439 if (p_wmh > p_wh)
4440 {
4441 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4442 p_wmh = p_wh;
4443 }
4444 win_setminheight();
4445
4446 return errmsg;
4447}
4448
4449/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004450 * Process the new 'winminwidth' option value.
4451 */
4452 char *
4453did_set_winminwidth(optset_T *args UNUSED)
4454{
4455 char *errmsg = NULL;
4456
4457 if (p_wmw < 0)
4458 {
4459 errmsg = e_argument_must_be_positive;
4460 p_wmw = 0;
4461 }
4462 if (p_wmw > p_wiw)
4463 {
4464 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4465 p_wmw = p_wiw;
4466 }
4467 win_setminwidth();
4468
4469 return errmsg;
4470}
4471
4472/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004473 * Process the new 'winwidth' option value.
4474 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004475 char *
4476did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004477{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004478 char *errmsg = NULL;
4479
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004480 if (p_wiw < 1)
4481 {
4482 errmsg = e_argument_must_be_positive;
4483 p_wiw = 1;
4484 }
4485 if (p_wmw > p_wiw)
4486 {
4487 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4488 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004490
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004491 // Change window width NOW
4492 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4493 win_setwidth((int)p_wiw);
4494
4495 return errmsg;
4496}
4497
4498/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004499 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004500 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004501 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004502did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004503{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004504 // If 'wrap' is set, set w_leftcol to zero.
4505 if (curwin->w_p_wrap)
4506 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004507 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004508}
4509
4510/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004511 * Set the value of a boolean option, and take care of side effects.
4512 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004513 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004514 static char *
4515set_bool_option(
4516 int opt_idx, // index in options[] table
4517 char_u *varp, // pointer to the option variable
4518 int value, // new value
4519 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004520{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004521 int old_value = *(int *)varp;
4522#if defined(FEAT_EVAL)
4523 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004525 char *errmsg = NULL;
4526
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004527 // Disallow changing some options from secure mode
4528 if ((secure
4529#ifdef HAVE_SANDBOX
4530 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004531#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004532 ) && (options[opt_idx].flags & P_SECURE))
4533 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004534
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004535#if defined(FEAT_EVAL)
4536 // Save the global value before changing anything. This is needed as for
4537 // a global-only option setting the "local value" in fact sets the global
4538 // value (since there is only one value).
4539 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4540 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4541 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004543
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004544 *(int *)varp = value; // set the new value
4545#ifdef FEAT_EVAL
4546 // Remember where the option was set.
4547 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004548#endif
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004551 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004554 // May set global value for local option.
4555 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4556 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004557
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004558 // Handle side effects of changing a bool option.
4559 if (options[opt_idx].opt_did_set_cb != NULL)
4560 {
4561 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004562
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004563 CLEAR_FIELD(args);
4564 args.os_varp = varp;
4565 args.os_flags = opt_flags;
4566 args.os_oldval.boolean = old_value;
4567 args.os_newval.boolean = value;
4568 args.os_errbuf = NULL;
4569 errmsg = options[opt_idx].opt_did_set_cb(&args);
4570 if (args.os_doskip)
4571 return errmsg;
4572 }
4573
4574 // after handling side effects, call autocommand
4575
4576 options[opt_idx].flags |= P_WAS_SET;
4577
4578#if defined(FEAT_EVAL)
4579 apply_optionset_autocmd(opt_idx, opt_flags,
4580 (long)(old_value ? TRUE : FALSE),
4581 (long)(old_global_value ? TRUE : FALSE),
4582 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004583#endif
4584
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004585 comp_col(); // in case 'ruler' or 'showcmd' changed
4586 if (curwin->w_curswant != MAXCOL
4587 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4588 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004589
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004590 if ((opt_flags & OPT_NO_REDRAW) == 0)
4591 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004592
4593 return errmsg;
4594}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004595
4596/*
4597 * Check the bounds of numeric options.
4598 */
4599 static char *
4600check_num_option_bounds(
4601 long *pp,
4602 long old_value,
4603 long old_Rows,
4604 long old_Columns,
4605 char *errbuf,
4606 size_t errbuflen,
4607 char *errmsg)
4608{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (Rows < min_rows() && full_screen)
4610 {
4611 if (errbuf != NULL)
4612 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004613 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004614 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 errmsg = errbuf;
4616 }
4617 Rows = min_rows();
4618 }
4619 if (Columns < MIN_COLUMNS && full_screen)
4620 {
4621 if (errbuf != NULL)
4622 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004623 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004624 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 errmsg = errbuf;
4626 }
4627 Columns = MIN_COLUMNS;
4628 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004629 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004631 // If the screen (shell) height has been changed, assume it is the
4632 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (old_Rows != Rows || old_Columns != Columns)
4634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004635 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (updating_screen)
4637 *pp = old_value;
4638 else if (full_screen
4639#ifdef FEAT_GUI
4640 && !gui.starting
4641#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004642 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 set_shellsize((int)Columns, (int)Rows, TRUE);
4644 else
4645 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004646 // Postpone the resizing; check the size and cmdline position for
4647 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 check_shellsize();
4649 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4650 cmdline_row = Rows - p_ch;
4651 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004652 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004653 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
4655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (curbuf->b_p_ts <= 0)
4657 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004658 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 curbuf->b_p_ts = 8;
4660 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004661 else if (curbuf->b_p_ts > TABSTOP_MAX)
4662 {
4663 errmsg = e_invalid_argument;
4664 curbuf->b_p_ts = 8;
4665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 if (p_tm < 0)
4667 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004668 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 p_tm = 0;
4670 }
4671 if ((curwin->w_p_scr <= 0
4672 || (curwin->w_p_scr > curwin->w_height
4673 && curwin->w_height > 0))
4674 && full_screen)
4675 {
4676 if (pp == &(curwin->w_p_scr))
4677 {
4678 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004679 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 win_comp_scroll(curwin);
4681 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004682 // If 'scroll' became invalid because of a side effect silently adjust
4683 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 else if (curwin->w_p_scr <= 0)
4685 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004686 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 curwin->w_p_scr = curwin->w_height;
4688 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004689 if (p_hi < 0)
4690 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004691 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004692 p_hi = 0;
4693 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004694 else if (p_hi > 10000)
4695 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004696 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004697 p_hi = 10000;
4698 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004699 if (p_re < 0 || p_re > 2)
4700 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004701 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004702 p_re = 0;
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if (p_report < 0)
4705 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004706 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 p_report = 1;
4708 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004709 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004711 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 p_sj = Rows / 2;
4713 else
4714 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004715 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 p_sj = 1;
4717 }
4718 }
4719 if (p_so < 0 && full_screen)
4720 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004721 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 p_so = 0;
4723 }
4724 if (p_siso < 0 && full_screen)
4725 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004726 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 p_siso = 0;
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (p_cwh < 1)
4730 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004731 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 p_cwh = 1;
4733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (p_ut < 0)
4735 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004736 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 p_ut = 2000;
4738 }
4739 if (p_ss < 0)
4740 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004741 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 p_ss = 0;
4743 }
4744
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004745 return errmsg;
4746}
4747
4748/*
4749 * Set the value of a number option, and take care of side effects.
4750 * Returns NULL for success, or an error message for an error.
4751 */
4752 static char *
4753set_num_option(
4754 int opt_idx, // index in options[] table
4755 char_u *varp, // pointer to the option variable
4756 long value, // new value
4757 char *errbuf, // buffer for error messages
4758 size_t errbuflen, // length of "errbuf"
4759 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4760 // OPT_MODELINE, etc.
4761{
4762 char *errmsg = NULL;
4763 long old_value = *(long *)varp;
4764#if defined(FEAT_EVAL)
4765 long old_global_value = 0; // only used when setting a local and
4766 // global option
4767#endif
4768 long old_Rows = Rows; // remember old Rows
4769 long old_Columns = Columns; // remember old Columns
4770 long *pp = (long *)varp;
4771
4772 // Disallow changing some options from secure mode.
4773 if ((secure
4774#ifdef HAVE_SANDBOX
4775 || sandbox != 0
4776#endif
4777 ) && (options[opt_idx].flags & P_SECURE))
4778 return e_not_allowed_here;
4779
4780#if defined(FEAT_EVAL)
4781 // Save the global value before changing anything. This is needed as for
4782 // a global-only option setting the "local value" in fact sets the global
4783 // value (since there is only one value).
4784 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4785 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4786 OPT_GLOBAL);
4787#endif
4788
4789 *pp = value;
4790#ifdef FEAT_EVAL
4791 // Remember where the option was set.
4792 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4793#endif
4794#ifdef FEAT_GUI
4795 need_mouse_correct = TRUE;
4796#endif
4797
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004798 // Invoke the option specific callback function to validate and apply the
4799 // new value.
4800 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004801 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004802 optset_T args;
4803
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004804 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004805 args.os_varp = varp;
4806 args.os_flags = opt_flags;
4807 args.os_oldval.number = old_value;
4808 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004809 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004810 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004811 }
4812
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004813 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004814 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4815 errbuf, errbuflen, errmsg);
4816
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004817 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4819 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4820
4821 options[opt_idx].flags |= P_WAS_SET;
4822
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004823#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004824 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4825 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004826#endif
4827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004828 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004829 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004830 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004831 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004832 if ((opt_flags & OPT_NO_REDRAW) == 0)
4833 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 return errmsg;
4836}
4837
4838/*
4839 * Called after an option changed: check if something needs to be redrawn.
4840 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004842check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004844 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004845 int doclear = (flags & P_RCLR) == P_RCLR;
4846 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004848 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4852 changed_window_setting();
4853 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004854 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004855 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004856 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004857 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004858 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004860 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861}
4862
4863/*
4864 * Find index for option 'arg'.
4865 * Return -1 if not found.
4866 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004867 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004868findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
4870 int opt_idx;
4871 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 int is_term_opt;
4874
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004875 // For first call: Initialize the quick-access table.
4876 // It contains the index for the first option that starts with a certain
4877 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (quick_tab[1] == 0)
4879 {
4880 p = options[0].fullname;
4881 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4882 {
4883 if (s[0] != p[0])
4884 {
4885 if (s[0] == 't' && s[1] == '_')
4886 quick_tab[26] = opt_idx;
4887 else
4888 quick_tab[CharOrdLow(s[0])] = opt_idx;
4889 }
4890 p = s;
4891 }
4892 }
4893
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004894 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 return -1;
4897
4898 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4899 if (is_term_opt)
4900 opt_idx = quick_tab[26];
4901 else
4902 opt_idx = quick_tab[CharOrdLow(arg[0])];
4903 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004905 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 break;
4907 }
4908 if (s == NULL && !is_term_opt)
4909 {
4910 opt_idx = quick_tab[CharOrdLow(arg[0])];
4911 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4912 {
4913 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004914 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 break;
4916 s = NULL;
4917 }
4918 }
4919 if (s == NULL)
4920 opt_idx = -1;
4921 return opt_idx;
4922}
4923
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004924#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4925 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926/*
4927 * Get the value for an option.
4928 *
4929 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004930 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004931 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004932 * String option: gov_string, *stringval gets allocated string.
4933 * Hidden Number option: gov_hidden_number.
4934 * Hidden Toggle option: gov_hidden_bool.
4935 * Hidden String option: gov_hidden_string.
4936 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004937 *
4938 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004940 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004941get_option_value(
4942 char_u *name,
4943 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004944 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004945 int *flagsp,
4946 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
4948 int opt_idx;
4949 char_u *varp;
4950
4951 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004952 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004953 {
4954 int key;
4955
4956 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004957 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004958 {
4959 char_u key_name[2];
4960 char_u *p;
4961
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004962 if (flagsp != NULL)
4963 *flagsp = 0; // terminal option has no flags
4964
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004965 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004966 if (key < 0)
4967 {
4968 key_name[0] = KEY2TERMCAP0(key);
4969 key_name[1] = KEY2TERMCAP1(key);
4970 }
4971 else
4972 {
4973 key_name[0] = KS_KEY;
4974 key_name[1] = (key & 0xff);
4975 }
4976 p = find_termcode(key_name);
4977 if (p != NULL)
4978 {
4979 if (stringval != NULL)
4980 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004981 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004982 }
4983 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004984 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004987 varp = get_varp_scope(&(options[opt_idx]), scope);
4988
4989 if (flagsp != NULL)
4990 // Return the P_xxxx option flags.
4991 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 if (options[opt_idx].flags & P_STRING)
4994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004995 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004996 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (stringval != NULL)
4998 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004999 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01005000 *stringval = str2special_save(*(char_u **)(varp), FALSE,
5001 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01005004 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005005 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 *stringval = vim_strsave(*(char_u **)(varp));
5010 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005011 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005014 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005015 return (options[opt_idx].flags & P_NUM)
5016 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (options[opt_idx].flags & P_NUM)
5018 *numval = *(long *)varp;
5019 else
5020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005021 // Special case: 'modified' is b_changed, but we also want to consider
5022 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if ((int *)varp == &curbuf->b_changed)
5024 *numval = curbufIsChanged();
5025 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005026 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005028 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029}
5030#endif
5031
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005032#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005033/*
5034 * Returns the option attributes and its value. Unlike the above function it
5035 * will return either global value or local value of the option depending on
5036 * what was requested, but it will never return global value if it was
5037 * requested to return local one and vice versa. Neither it will return
5038 * buffer-local value if it was requested to return window-local one.
5039 *
5040 * Pretends that option is absent if it is not present in the requested scope
5041 * (i.e. has no global, window-local or buffer-local value depending on
5042 * opt_type). Uses
5043 *
5044 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005045 * 0 hidden or unknown option, also option that does not have requested
5046 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005047 * see SOPT_* in vim.h for other flags
5048 *
5049 * Possible opt_type values: see SREQ_* in vim.h
5050 */
5051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005052get_option_value_strict(
5053 char_u *name,
5054 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005055 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005056 int opt_type,
5057 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005058{
5059 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005060 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005061 struct vimoption *p;
5062 int r = 0;
5063
5064 opt_idx = findoption(name);
5065 if (opt_idx < 0)
5066 return 0;
5067
5068 p = &(options[opt_idx]);
5069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005070 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005071 if (p->var == NULL)
5072 return 0;
5073
5074 if (p->flags & P_BOOL)
5075 r |= SOPT_BOOL;
5076 else if (p->flags & P_NUM)
5077 r |= SOPT_NUM;
5078 else if (p->flags & P_STRING)
5079 r |= SOPT_STRING;
5080
5081 if (p->indir == PV_NONE)
5082 {
5083 if (opt_type == SREQ_GLOBAL)
5084 r |= SOPT_GLOBAL;
5085 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005086 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005087 }
5088 else
5089 {
5090 if (p->indir & PV_BOTH)
5091 r |= SOPT_GLOBAL;
5092 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005093 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005094
5095 if (p->indir & PV_WIN)
5096 {
5097 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005098 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005099 else
5100 r |= SOPT_WIN;
5101 }
5102 else if (p->indir & PV_BUF)
5103 {
5104 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005105 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106 else
5107 r |= SOPT_BUF;
5108 }
5109 }
5110
5111 if (stringval == NULL)
5112 return r;
5113
5114 if (opt_type == SREQ_GLOBAL)
5115 varp = p->var;
5116 else
5117 {
5118 if (opt_type == SREQ_BUF)
5119 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005120 // Special case: 'modified' is b_changed, but we also want to
5121 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 if (p->indir == PV_MOD)
5123 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005124 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005125 varp = NULL;
5126 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005127# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005128 else if (p->indir == PV_KEY)
5129 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005130 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 *stringval = NULL;
5132 varp = NULL;
5133 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005134# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005135 else
5136 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005137 buf_T *save_curbuf = curbuf;
5138
5139 // only getting a pointer, no need to use aucmd_prepbuf()
5140 curbuf = (buf_T *)from;
5141 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005142 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005143 curbuf = save_curbuf;
5144 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005145 }
5146 }
5147 else if (opt_type == SREQ_WIN)
5148 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005149 win_T *save_curwin = curwin;
5150
5151 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005152 curbuf = curwin->w_buffer;
5153 varp = get_varp(p);
5154 curwin = save_curwin;
5155 curbuf = curwin->w_buffer;
5156 }
5157 if (varp == p->var)
5158 return (r | SOPT_UNSET);
5159 }
5160
5161 if (varp != NULL)
5162 {
5163 if (p->flags & P_STRING)
5164 *stringval = vim_strsave(*(char_u **)(varp));
5165 else if (p->flags & P_NUM)
5166 *numval = *(long *) varp;
5167 else
5168 *numval = *(int *)varp;
5169 }
5170
5171 return r;
5172}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005173
5174/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005175 * Iterate over options. First argument is a pointer to a pointer to a
5176 * structure inside options[] array, second is option type like in the above
5177 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005178 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005179 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005180 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005181 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005182 * first argument to NULL.
5183 *
5184 * Returns full option name for current option on each call.
5185 */
5186 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005187option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005188{
5189 struct vimoption *ret = NULL;
5190 do
5191 {
5192 if (*option == NULL)
5193 *option = (void *) options;
5194 else if (((struct vimoption *) (*option))->fullname == NULL)
5195 {
5196 *option = NULL;
5197 return NULL;
5198 }
5199 else
5200 *option = (void *) (((struct vimoption *) (*option)) + 1);
5201
5202 ret = ((struct vimoption *) (*option));
5203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005204 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005205 if (ret->var == NULL)
5206 {
5207 ret = NULL;
5208 continue;
5209 }
5210
5211 switch (opt_type)
5212 {
5213 case SREQ_GLOBAL:
5214 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5215 ret = NULL;
5216 break;
5217 case SREQ_BUF:
5218 if (!(ret->indir & PV_BUF))
5219 ret = NULL;
5220 break;
5221 case SREQ_WIN:
5222 if (!(ret->indir & PV_WIN))
5223 ret = NULL;
5224 break;
5225 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005226 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005227 return NULL;
5228 }
5229 }
5230 while (ret == NULL);
5231
5232 return (char_u *)ret->fullname;
5233}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005234#endif
5235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005237 * Return the flags for the option at 'opt_idx'.
5238 */
5239 long_u
5240get_option_flags(int opt_idx)
5241{
5242 return options[opt_idx].flags;
5243}
5244
5245/*
5246 * Set a flag for the option at 'opt_idx'.
5247 */
5248 void
5249set_option_flag(int opt_idx, long_u flag)
5250{
5251 options[opt_idx].flags |= flag;
5252}
5253
5254/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005255 * Returns TRUE if the option at 'opt_idx' is a global option
5256 */
5257 int
5258is_global_option(int opt_idx)
5259{
5260 return options[opt_idx].indir == PV_NONE;
5261}
5262
5263/*
5264 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5265 * local value.
5266 */
5267 int
5268is_global_local_option(int opt_idx)
5269{
5270 return options[opt_idx].indir & PV_BOTH;
5271}
5272
5273/*
5274 * Returns TRUE if the option at 'opt_idx' is a window-local option
5275 */
5276 int
5277is_window_local_option(int opt_idx)
5278{
5279 return options[opt_idx].var == VAR_WIN;
5280}
5281
5282/*
5283 * Returns TRUE if the option at 'opt_idx' is a hidden option
5284 */
5285 int
5286is_hidden_option(int opt_idx)
5287{
5288 return options[opt_idx].var == NULL;
5289}
5290
5291#if defined(FEAT_CRYPT) || defined(PROTO)
5292/*
5293 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5294 */
5295 int
5296is_crypt_key_option(int opt_idx)
5297{
5298 return options[opt_idx].indir == PV_KEY;
5299}
5300#endif
5301
5302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 * Set the value of option "name".
5304 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005305 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005306 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005308 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005309set_option_value(
5310 char_u *name,
5311 long number,
5312 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005313 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 int opt_idx;
5316 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005317 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005318 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005321 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005322 {
5323 int key;
5324
5325 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005326 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005327 {
5328 char_u key_name[2];
5329
5330 if (key < 0)
5331 {
5332 key_name[0] = KEY2TERMCAP0(key);
5333 key_name[1] = KEY2TERMCAP1(key);
5334 }
5335 else
5336 {
5337 key_name[0] = KS_KEY;
5338 key_name[1] = (key & 0xff);
5339 }
5340 add_termcode(key_name, string, FALSE);
5341 if (full_screen)
5342 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005343 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005344 return NULL;
5345 }
5346
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005347 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 else
5350 {
5351 flags = options[opt_idx].flags;
5352#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005353 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005355 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005356 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005357 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005360 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005361 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005362
5363 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5364 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005366 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005368 int idx;
5369
5370 // Either we are given a string or we are setting option
5371 // to zero.
5372 for (idx = 0; string[idx] == '0'; ++idx)
5373 ;
5374 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005375 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005376 // There's another character after zeros or the string
5377 // is empty. In both cases, we are trying to set a
5378 // num option using a string.
5379 semsg(_(e_number_required_after_str_equal_str),
5380 name, string);
5381 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005382
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005385 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005386 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005387 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005388 errbuf, sizeof(errbuf), opt_flags);
5389 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005390 else
5391 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005395 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396}
5397
5398/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005399 * Call set_option_value() and when an error is returned report it.
5400 */
5401 void
5402set_option_value_give_err(
5403 char_u *name,
5404 long number,
5405 char_u *string,
5406 int opt_flags) // OPT_LOCAL or 0 (both)
5407{
5408 char *errmsg = set_option_value(name, number, string, opt_flags);
5409
5410 if (errmsg != NULL)
5411 emsg(_(errmsg));
5412}
5413
5414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 * Get the terminal code for a terminal option.
5416 * Returns NULL when not found.
5417 */
5418 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005419get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 int opt_idx;
5422 char_u *varp;
5423
5424 if (tname[0] != 't' || tname[1] != '_' ||
5425 tname[2] == NUL || tname[3] == NUL)
5426 return NULL;
5427 if ((opt_idx = findoption(tname)) >= 0)
5428 {
5429 varp = get_varp(&(options[opt_idx]));
5430 if (varp != NULL)
5431 varp = *(char_u **)(varp);
5432 return varp;
5433 }
5434 return find_termcode(tname + 2);
5435}
5436
5437 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005438get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 int i;
5441
5442 i = findoption((char_u *)"hl");
5443 if (i >= 0)
5444 return options[i].def_val[VI_DEFAULT];
5445 return (char_u *)NULL;
5446}
5447
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005448 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005449get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005450{
5451 int i;
5452
5453 i = findoption((char_u *)"enc");
5454 if (i >= 0)
5455 return options[i].def_val[VI_DEFAULT];
5456 return (char_u *)NULL;
5457}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005458
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005459#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005460 int
5461is_option_allocated(char *name)
5462{
5463 int idx = findoption((char_u *)name);
5464
5465 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5466}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005467#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469/*
5470 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005471 * When "has_lt" is true there is a '<' before "*arg_arg".
5472 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 */
5474 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005475find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005477 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005479 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005481 // Don't use get_special_key_code() for t_xx, we don't want it to call
5482 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5484 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005485 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005487 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005489 key = find_special_key(&arg, &modifiers,
5490 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005491 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 key = 0;
5493 }
5494 return key;
5495}
5496
5497/*
5498 * if 'all' == 0: show changed options
5499 * if 'all' == 1: show all normal options
5500 * if 'all' == 2: show all terminal options
5501 */
5502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005503showoptions(
5504 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005505 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 struct vimoption *p;
5508 int col;
5509 int isterm;
5510 char_u *varp;
5511 struct vimoption **items;
5512 int item_count;
5513 int run;
5514 int row, rows;
5515 int cols;
5516 int i;
5517 int len;
5518
5519#define INC 20
5520#define GAP 3
5521
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005522 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (items == NULL)
5524 return;
5525
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005528 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005530 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005532 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005534 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005536 // Do the loop two times:
5537 // 1. display the short items
5538 // 2. display the long items (only strings and numbers)
5539 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 for (run = 1; run <= 2 && !got_int; ++run)
5541 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005542 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 item_count = 0;
5544 for (p = &options[0]; p->fullname != NULL; p++)
5545 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005546 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005547 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005548 continue;
5549
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 varp = NULL;
5551 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005552 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
5554 if (p->indir != PV_NONE && !isterm)
5555 varp = get_varp_scope(p, opt_flags);
5556 }
5557 else
5558 varp = get_varp(p);
5559 if (varp != NULL
5560 && ((all == 2 && isterm)
5561 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005562 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005564 if (opt_flags & OPT_ONECOLUMN)
5565 len = Columns;
5566 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005567 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else
5569 {
5570 option_value2string(p, opt_flags);
5571 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5572 }
5573 if ((len <= INC - GAP && run == 1) ||
5574 (len > INC - GAP && run == 2))
5575 items[item_count++] = p;
5576 }
5577 }
5578
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005579 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 if (run == 1)
5581 {
5582 cols = (Columns + GAP - 3) / INC;
5583 if (cols == 0)
5584 cols = 1;
5585 rows = (item_count + cols - 1) / cols;
5586 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005587 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 rows = item_count;
5589 for (row = 0; row < rows && !got_int; ++row)
5590 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005591 msg_putchar('\n'); // go to next line
5592 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 break;
5594 col = 0;
5595 for (i = row; i < item_count; i += rows)
5596 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 showoneopt(items[i], opt_flags);
5599 col += INC;
5600 }
5601 out_flush();
5602 ui_breakcheck();
5603 }
5604 }
5605 vim_free(items);
5606}
5607
5608/*
5609 * Return TRUE if option "p" has its default value.
5610 */
5611 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005612optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 int dvi;
5615
5616 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005617 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005618 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005620 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 // the cast to long is required for Manx C, long_i is
5623 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005624 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005625 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5627}
5628
5629/*
5630 * showoneopt: show the value of one option
5631 * must not be called with a hidden option!
5632 */
5633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005634showoneopt(
5635 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005636 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005638 char_u *varp;
5639 int save_silent = silent_mode;
5640
5641 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005642 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
5644 varp = get_varp_scope(p, opt_flags);
5645
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005646 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5648 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005649 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005651 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005653 msg_puts(" ");
5654 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (!(p->flags & P_BOOL))
5656 {
5657 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005658 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 option_value2string(p, opt_flags);
5660 msg_outtrans(NameBuff);
5661 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005662
5663 silent_mode = save_silent;
5664 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665}
5666
5667/*
5668 * Write modified options as ":set" commands to a file.
5669 *
5670 * There are three values for "opt_flags":
5671 * OPT_GLOBAL: Write global option values and fresh values of
5672 * buffer-local options (used for start of a session
5673 * file).
5674 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5675 * curwin (used for a vimrc file).
5676 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5677 * and local values for window-local options of
5678 * curwin. Local values are also written when at the
5679 * default value, because a modeline or autocommand
5680 * may have set them when doing ":edit file" and the
5681 * user has set them back at the default or fresh
5682 * value.
5683 * When "local_only" is TRUE, don't write fresh
5684 * values, only local values (for ":mkview").
5685 * (fresh value = value used for a new buffer or window for a local option).
5686 *
5687 * Return FAIL on error, OK otherwise.
5688 */
5689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005690makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005693 char_u *varp; // currently used value
5694 char_u *varp_fresh; // local value
5695 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 char *cmd;
5697 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005698 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 /*
5701 * The options that don't have a default (terminal name, columns, lines)
5702 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005703 * Do the loop over "options[]" twice: once for options with the
5704 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005706 for (pri = 1; pri >= 0; --pri)
5707 {
5708 for (p = &options[0]; !istermoption(p); p++)
5709 if (!(p->flags & P_NO_MKRC)
5710 && !istermoption(p)
5711 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005713 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5715 continue;
5716
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5718 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5720 continue;
5721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005722 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005724 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 continue;
5726
Bram Moolenaard23b7142021-04-17 21:04:34 +02005727 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5728 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005729 continue;
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 round = 2;
5732 if (p->indir != PV_NONE)
5733 {
5734 if (p->var == VAR_WIN)
5735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005736 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 if (!(opt_flags & OPT_LOCAL))
5738 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005739 // When fresh value of window-local option is not at the
5740 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5742 {
5743 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005744 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746 round = 1;
5747 varp_local = varp;
5748 varp = varp_fresh;
5749 }
5750 }
5751 }
5752 }
5753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005754 // Round 1: fresh value for window-local options.
5755 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 for ( ; round <= 2; varp = varp_local, ++round)
5757 {
5758 if (round == 1 || (opt_flags & OPT_GLOBAL))
5759 cmd = "set";
5760 else
5761 cmd = "setlocal";
5762
5763 if (p->flags & P_BOOL)
5764 {
5765 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5766 return FAIL;
5767 }
5768 else if (p->flags & P_NUM)
5769 {
5770 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5771 return FAIL;
5772 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005773 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005775 int do_endif = FALSE;
5776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005777 // Don't set 'syntax' and 'filetype' again if the value is
5778 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005779 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005780#if defined(FEAT_SYN_HL)
5781 p->indir == PV_SYN ||
5782#endif
5783 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
5785 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5786 *(char_u **)(varp)) < 0
5787 || put_eol(fd) < 0)
5788 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005789 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005792 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005794 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
5796 if (put_line(fd, "endif") == FAIL)
5797 return FAIL;
5798 }
5799 }
5800 }
5801 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 return OK;
5804}
5805
5806#if defined(FEAT_FOLDING) || defined(PROTO)
5807/*
5808 * Generate set commands for the local fold options only. Used when
5809 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5810 */
5811 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005812makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005814 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005816 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 == FAIL
5818# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005819 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005821 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 == FAIL
5823 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5824 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5825 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5826 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5827 )
5828 return FAIL;
5829
5830 return OK;
5831}
5832#endif
5833
5834 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005835put_setstring(
5836 FILE *fd,
5837 char *cmd,
5838 char *name,
5839 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005840 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005843 char_u *buf = NULL;
5844 char_u *part = NULL;
5845 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5848 return FAIL;
5849 if (*valuep != NULL)
5850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005851 // Output 'pastetoggle' as key names. For other
5852 // options some characters have to be escaped with
5853 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 if (valuep == &p_pt)
5855 {
5856 s = *valuep;
5857 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005858 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 return FAIL;
5860 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005861 // expand the option value, replace $HOME by ~
5862 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005864 int size = (int)STRLEN(*valuep) + 1;
5865
5866 // replace home directory in the whole option value into "buf"
5867 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005868 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005869 goto fail;
5870 home_replace(NULL, *valuep, buf, size, FALSE);
5871
5872 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005873 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005874 // can be expanded when read back.
5875 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5876 && vim_strchr(*valuep, ',') != NULL)
5877 {
5878 part = alloc(size);
5879 if (part == NULL)
5880 goto fail;
5881
5882 // write line break to clear the option, e.g. ':set rtp='
5883 if (put_eol(fd) == FAIL)
5884 goto fail;
5885
5886 p = buf;
5887 while (*p != NUL)
5888 {
5889 // for each comma separated option part, append value to
5890 // the option, :set rtp+=value
5891 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5892 goto fail;
5893 (void)copy_option_part(&p, part, size, ",");
5894 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5895 goto fail;
5896 }
5897 vim_free(buf);
5898 vim_free(part);
5899 return OK;
5900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005902 {
5903 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005905 }
5906 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 else if (put_escstr(fd, *valuep, 2) == FAIL)
5909 return FAIL;
5910 }
5911 if (put_eol(fd) < 0)
5912 return FAIL;
5913 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005914fail:
5915 vim_free(buf);
5916 vim_free(part);
5917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918}
5919
5920 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005921put_setnum(
5922 FILE *fd,
5923 char *cmd,
5924 char *name,
5925 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 long wc;
5928
5929 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5930 return FAIL;
5931 if (wc_use_keyname((char_u *)valuep, &wc))
5932 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005933 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5935 return FAIL;
5936 }
5937 else if (fprintf(fd, "%ld", *valuep) < 0)
5938 return FAIL;
5939 if (put_eol(fd) < 0)
5940 return FAIL;
5941 return OK;
5942}
5943
5944 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005945put_setbool(
5946 FILE *fd,
5947 char *cmd,
5948 char *name,
5949 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005951 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005952 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5954 || put_eol(fd) < 0)
5955 return FAIL;
5956 return OK;
5957}
5958
5959/*
5960 * Clear all the terminal options.
5961 * If the option has been allocated, free the memory.
5962 * Terminal options are never hidden or indirect.
5963 */
5964 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005965clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005967 // Reset a few things before clearing the old options. This may cause
5968 // outputting a few things that the terminal doesn't understand, but the
5969 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005970 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005971 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005973 // When starting the GUI close the display opened for the clipboard.
5974 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 if (gui.starting)
5976 clear_xterm_clip();
5977#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005978 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005980 free_termoptions();
5981}
5982
5983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005984free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005985{
5986 struct vimoption *p;
5987
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005988 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 if (istermoption(p))
5990 {
5991 if (p->flags & P_ALLOCED)
5992 free_string_option(*(char_u **)(p->var));
5993 if (p->flags & P_DEF_ALLOCED)
5994 free_string_option(p->def_val[VI_DEFAULT]);
5995 *(char_u **)(p->var) = empty_option;
5996 p->def_val[VI_DEFAULT] = empty_option;
5997 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005998#ifdef FEAT_EVAL
5999 // remember where the option was cleared
6000 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003 clear_termcodes();
6004}
6005
6006/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006007 * Free the string for one term option, if it was allocated.
6008 * Set the string to empty_option and clear allocated flag.
6009 * "var" points to the option value.
6010 */
6011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006012free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006013{
6014 struct vimoption *p;
6015
6016 for (p = &options[0]; p->fullname != NULL; p++)
6017 if (p->var == var)
6018 {
6019 if (p->flags & P_ALLOCED)
6020 free_string_option(*(char_u **)(p->var));
6021 *(char_u **)(p->var) = empty_option;
6022 p->flags &= ~P_ALLOCED;
6023 break;
6024 }
6025}
6026
6027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 * Set the terminal option defaults to the current value.
6029 * Used after setting the terminal name.
6030 */
6031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006032set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 struct vimoption *p;
6035
6036 for (p = &options[0]; p->fullname != NULL; p++)
6037 {
6038 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6039 {
6040 if (p->flags & P_DEF_ALLOCED)
6041 {
6042 free_string_option(p->def_val[VI_DEFAULT]);
6043 p->flags &= ~P_DEF_ALLOCED;
6044 }
6045 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6046 if (p->flags & P_ALLOCED)
6047 {
6048 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006049 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 }
6051 }
6052 }
6053}
6054
6055/*
6056 * return TRUE if 'p' starts with 't_'
6057 */
6058 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006059istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
6061 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6062}
6063
Bram Moolenaardac13472019-09-16 21:06:21 +02006064/*
6065 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6066 */
6067 int
6068istermoption_idx(int opt_idx)
6069{
6070 return istermoption(&options[opt_idx]);
6071}
6072
Bram Moolenaar113e1072019-01-20 15:30:40 +01006073#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006075 * Unset local option value, similar to ":set opt<".
6076 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006078unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006079{
6080 struct vimoption *p;
6081 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006082 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006083
6084 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006085 if (opt_idx < 0)
6086 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006087 p = &(options[opt_idx]);
6088
6089 switch ((int)p->indir)
6090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006091 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006092 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006093 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006094 break;
6095 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006096 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006097 break;
6098 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006099 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006100 break;
6101 case PV_AR:
6102 buf->b_p_ar = -1;
6103 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006104 case PV_BKC:
6105 clear_string_option(&buf->b_p_bkc);
6106 buf->b_bkc_flags = 0;
6107 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006108 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006109 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006110 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006111 case PV_TC:
6112 clear_string_option(&buf->b_p_tc);
6113 buf->b_tc_flags = 0;
6114 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006115 case PV_SISO:
6116 curwin->w_p_siso = -1;
6117 break;
6118 case PV_SO:
6119 curwin->w_p_so = -1;
6120 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006121# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006122 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006123 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006124 break;
6125 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006126 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006127 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006128# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006129 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006130 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006131 break;
6132 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006133 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006134 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006135# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006136 case PV_TSRFU:
6137 clear_string_option(&buf->b_p_tsrfu);
6138 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006139# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006140 case PV_FP:
6141 clear_string_option(&buf->b_p_fp);
6142 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006143# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006144 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006145 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006146 break;
6147 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006148 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006149 break;
6150 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006151 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006152 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006153# endif
6154# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006155 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006156 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006157 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006158# endif
6159# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006160 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006161 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006162 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006163# endif
6164# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006165 case PV_SBR:
6166 clear_string_option(&((win_T *)from)->w_p_sbr);
6167 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006168# endif
6169# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006170 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006171 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006172 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006173# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006174 case PV_UL:
6175 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6176 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006177 case PV_LW:
6178 clear_string_option(&buf->b_p_lw);
6179 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006180 case PV_MENC:
6181 clear_string_option(&buf->b_p_menc);
6182 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006183 case PV_LCS:
6184 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006185 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006186 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006187 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006188 case PV_FCS:
6189 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006190 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006191 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006192 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006193 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006194 clear_string_option(&((win_T *)from)->w_p_ve);
6195 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006196 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006197 }
6198}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006199#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006200
6201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006203 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 */
6205 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006206get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006208 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
6210 if (p->var == VAR_WIN)
6211 return (char_u *)GLOBAL_WO(get_varp(p));
6212 return p->var;
6213 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006214 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
6216 switch ((int)p->indir)
6217 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006218 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006220 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6221 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6222 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006224 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6225 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6226 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006227 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006228 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006229 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006230 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6231 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006233 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6234 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006236 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6237 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006238#ifdef FEAT_COMPL_FUNC
6239 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6240#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006241#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6242 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6243#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006244#if defined(FEAT_CRYPT)
6245 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6246#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006247#ifdef FEAT_LINEBREAK
6248 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6249#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006250#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006251 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006252#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006253 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006254 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006255 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006256 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006257 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006258 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006259 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006262 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 }
6264 return get_varp(p);
6265}
6266
6267/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006268 * Get pointer to option variable at 'opt_idx', depending on local or global
6269 * scope.
6270 */
6271 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006272get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006273{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006274 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006275}
6276
6277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 * Get pointer to option variable.
6279 */
6280 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006281get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006283 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (p->var == NULL)
6285 return NULL;
6286
6287 switch ((int)p->indir)
6288 {
6289 case PV_NONE: return p->var;
6290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006291 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006292 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006294 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006296 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006298 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006300 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006302 case PV_TC: return *curbuf->b_p_tc != NUL
6303 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006304 case PV_BKC: return *curbuf->b_p_bkc != NUL
6305 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006306 case PV_SISO: return curwin->w_p_siso >= 0
6307 ? (char_u *)&(curwin->w_p_siso) : p->var;
6308 case PV_SO: return curwin->w_p_so >= 0
6309 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006311 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006313 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6315#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006316 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006318 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006320#ifdef FEAT_COMPL_FUNC
6321 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6322 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6323#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006324 case PV_FP: return *curbuf->b_p_fp != NUL
6325 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006327 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006329 case PV_GP: return *curbuf->b_p_gp != NUL
6330 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6331 case PV_MP: return *curbuf->b_p_mp != NUL
6332 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006334#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6335 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6336 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6337#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006338#if defined(FEAT_CRYPT)
6339 case PV_CM: return *curbuf->b_p_cm != NUL
6340 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6341#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006342#ifdef FEAT_LINEBREAK
6343 case PV_SBR: return *curwin->w_p_sbr != NUL
6344 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6345#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006346#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006347 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006348 ? (char_u *)&(curwin->w_p_stl) : p->var;
6349#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006350 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6351 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006352 case PV_LW: return *curbuf->b_p_lw != NUL
6353 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006354 case PV_MENC: return *curbuf->b_p_menc != NUL
6355 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356#ifdef FEAT_ARABIC
6357 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6358#endif
6359 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006360 case PV_LCS: return *curwin->w_p_lcs != NUL
6361 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006362 case PV_FCS: return *curwin->w_p_fcs != NUL
6363 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006364 case PV_VE: return *curwin->w_p_ve != NUL
6365 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006366#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006367 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6368#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006369#ifdef FEAT_SYN_HL
6370 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6371 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006372 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006373 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375#ifdef FEAT_DIFF
6376 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6377#endif
6378#ifdef FEAT_FOLDING
6379 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6380 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6381 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6382 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6383 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6384 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6385 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6386# ifdef FEAT_EVAL
6387 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6388 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6389# endif
6390 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6391#endif
6392 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006393 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006394#ifdef FEAT_LINEBREAK
6395 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006398 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006399#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6401#endif
6402#ifdef FEAT_RIGHTLEFT
6403 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6404 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6405#endif
6406 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006407 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6409#ifdef FEAT_LINEBREAK
6410 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006411 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6412 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006414 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006416 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006417#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006418 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6419 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6420#endif
6421#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006422 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6423 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6424 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6428 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6431 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6433 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6435 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6436 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006437 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440#ifdef FEAT_FOLDING
6441 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006444#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006445 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006447#ifdef FEAT_COMPL_FUNC
6448 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006449 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006450#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006451#ifdef FEAT_EVAL
6452 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6453#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006454 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006456 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006462 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6464 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6465 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6466 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6467#ifdef FEAT_FIND_ID
6468# ifdef FEAT_EVAL
6469 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6470# endif
6471#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006472#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6474 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6475#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006476#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479#ifdef FEAT_CRYPT
6480 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006483 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6485 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6486 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6487 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6488 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006490 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6497#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006498 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006499 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6500#endif
6501#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006502 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6503 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6504 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006505 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506#endif
6507 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6508 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6509 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6510 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006511#ifdef FEAT_PERSISTENT_UNDO
6512 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6515#ifdef FEAT_KEYMAP
6516 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6517#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006518#ifdef FEAT_SIGNS
6519 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6520#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006521#ifdef FEAT_VARTABS
6522 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6523 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6524#endif
RestorerZ68ebcee2023-05-31 17:12:14 +01006525 default: iemsg(e_get_varp_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006527 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 return (char_u *)&(curbuf->b_p_wm);
6529}
6530
6531/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006532 * Return a pointer to the variable for option at 'opt_idx'
6533 */
6534 char_u *
6535get_option_var(int opt_idx)
6536{
6537 return options[opt_idx].var;
6538}
6539
Dominique Pelle748b3082022-01-08 12:41:16 +00006540#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006541/*
6542 * Return the full name of the option at 'opt_idx'
6543 */
6544 char_u *
6545get_option_fullname(int opt_idx)
6546{
6547 return (char_u *)options[opt_idx].fullname;
6548}
Dominique Pelle748b3082022-01-08 12:41:16 +00006549#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006550
6551/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006552 * Return the did_set callback function for the option at 'opt_idx'
6553 */
6554 opt_did_set_cb_T
6555get_option_did_set_cb(int opt_idx)
6556{
6557 return options[opt_idx].opt_did_set_cb;
6558}
6559
6560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 * Get the value of 'equalprg', either the buffer-local one or the global one.
6562 */
6563 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006564get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
6566 if (*curbuf->b_p_ep == NUL)
6567 return p_ep;
6568 return curbuf->b_p_ep;
6569}
6570
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571/*
6572 * Copy options from one window to another.
6573 * Used when splitting a window.
6574 */
6575 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006576win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6579 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006580 after_copy_winopt(wp_to);
6581}
6582
6583/*
6584 * After copying window options: update variables depending on options.
6585 */
6586 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006587after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006588{
6589#ifdef FEAT_LINEBREAK
6590 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006591#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006592#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006593 fill_culopt_flags(NULL, wp);
6594 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006595#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006596 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6597 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006598}
6599
6600 static char_u *
6601copy_option_val(char_u *val)
6602{
6603 if (val == empty_option)
6604 return empty_option; // no need to allocate memory
6605 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608/*
6609 * Copy the options from one winopt_T to another.
6610 * Doesn't free the old option values in "to", use clear_winopt() for that.
6611 * The 'scroll' option is not copied, because it depends on the window height.
6612 * The 'previewwindow' option is reset, there can be only one preview window.
6613 */
6614 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006615copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617#ifdef FEAT_ARABIC
6618 to->wo_arab = from->wo_arab;
6619#endif
6620 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006621 to->wo_lcs = copy_option_val(from->wo_lcs);
6622 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006624 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006625 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006626 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006627#ifdef FEAT_LINEBREAK
6628 to->wo_nuw = from->wo_nuw;
6629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630#ifdef FEAT_RIGHTLEFT
6631 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006632 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006634#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006635 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006636#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006637#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006638 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006641#ifdef FEAT_DIFF
6642 to->wo_wrap_save = from->wo_wrap_save;
6643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644#ifdef FEAT_LINEBREAK
6645 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006646 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006647 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006649 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006651 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006652 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006653 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006654 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006655#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006656 to->wo_spell = from->wo_spell;
6657#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006658#ifdef FEAT_SYN_HL
6659 to->wo_cuc = from->wo_cuc;
6660 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006661 to->wo_culopt = copy_option_val(from->wo_culopt);
6662 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664#ifdef FEAT_DIFF
6665 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006666 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006668#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006669 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006670 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006671#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006672#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006673 to->wo_twk = copy_option_val(from->wo_twk);
6674 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676#ifdef FEAT_FOLDING
6677 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006678 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006680 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006681 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 to->wo_fml = from->wo_fml;
6683 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006684 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006685 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006686 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006687 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 to->wo_fdn = from->wo_fdn;
6689# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006690 to->wo_fde = copy_option_val(from->wo_fde);
6691 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006693 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006695#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006696 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006697#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006698
6699#ifdef FEAT_EVAL
6700 // Copy the script context so that we know where the value was last set.
6701 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6702 sizeof(to->wo_script_ctx));
6703#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705}
6706
6707/*
6708 * Check string options in a window for a NULL value.
6709 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006711check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 check_winopt(&win->w_onebuf_opt);
6714 check_winopt(&win->w_allbuf_opt);
6715}
6716
6717/*
6718 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6719 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006721check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723#ifdef FEAT_FOLDING
6724 check_string_option(&wop->wo_fdi);
6725 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006726 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727# ifdef FEAT_EVAL
6728 check_string_option(&wop->wo_fde);
6729 check_string_option(&wop->wo_fdt);
6730# endif
6731 check_string_option(&wop->wo_fmr);
6732#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006733#ifdef FEAT_SIGNS
6734 check_string_option(&wop->wo_scl);
6735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736#ifdef FEAT_RIGHTLEFT
6737 check_string_option(&wop->wo_rlc);
6738#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006739#ifdef FEAT_LINEBREAK
6740 check_string_option(&wop->wo_sbr);
6741#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006742#ifdef FEAT_STL_OPT
6743 check_string_option(&wop->wo_stl);
6744#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006745#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006746 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006747 check_string_option(&wop->wo_cc);
6748#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006749#ifdef FEAT_CONCEAL
6750 check_string_option(&wop->wo_cocu);
6751#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006752#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006753 check_string_option(&wop->wo_twk);
6754 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006755#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006756#ifdef FEAT_LINEBREAK
6757 check_string_option(&wop->wo_briopt);
6758#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006759 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006760 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006761 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006762 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763}
6764
6765/*
6766 * Free the allocated memory inside a winopt_T.
6767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006769clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
6771#ifdef FEAT_FOLDING
6772 clear_string_option(&wop->wo_fdi);
6773 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006774 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775# ifdef FEAT_EVAL
6776 clear_string_option(&wop->wo_fde);
6777 clear_string_option(&wop->wo_fdt);
6778# endif
6779 clear_string_option(&wop->wo_fmr);
6780#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006781#ifdef FEAT_SIGNS
6782 clear_string_option(&wop->wo_scl);
6783#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006784#ifdef FEAT_LINEBREAK
6785 clear_string_option(&wop->wo_briopt);
6786#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006787 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788#ifdef FEAT_RIGHTLEFT
6789 clear_string_option(&wop->wo_rlc);
6790#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006791#ifdef FEAT_LINEBREAK
6792 clear_string_option(&wop->wo_sbr);
6793#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006794#ifdef FEAT_STL_OPT
6795 clear_string_option(&wop->wo_stl);
6796#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006797#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006798 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006799 clear_string_option(&wop->wo_cc);
6800#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006801#ifdef FEAT_CONCEAL
6802 clear_string_option(&wop->wo_cocu);
6803#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006804#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006805 clear_string_option(&wop->wo_twk);
6806 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006807#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006808 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006809 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006810 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811}
6812
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006813#ifdef FEAT_EVAL
6814// Index into the options table for a buffer-local option enum.
6815static int buf_opt_idx[BV_COUNT];
6816# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6817
6818/*
6819 * Initialize buf_opt_idx[] if not done already.
6820 */
6821 static void
6822init_buf_opt_idx(void)
6823{
6824 static int did_init_buf_opt_idx = FALSE;
6825 int i;
6826
6827 if (did_init_buf_opt_idx)
6828 return;
6829 did_init_buf_opt_idx = TRUE;
6830 for (i = 0; !istermoption_idx(i); i++)
6831 if (options[i].indir & PV_BUF)
6832 buf_opt_idx[options[i].indir & PV_MASK] = i;
6833}
6834#else
6835# define COPY_OPT_SCTX(buf, bv)
6836#endif
6837
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838/*
6839 * Copy global option values to local options for one buffer.
6840 * Used when creating a new buffer and sometimes when entering a buffer.
6841 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006842 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6844 * appropriate.
6845 * BCO_NOHELP Don't copy the values to a help buffer.
6846 */
6847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006848buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006851 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 int dont_do_help;
6853 int did_isk = FALSE;
6854
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006855 // Skip this when the option defaults have not been set yet. Happens when
6856 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (p_cpo != NULL)
6858 {
6859 /*
6860 * Always copy when entering and 'cpo' contains 'S'.
6861 * Don't copy when already initialized.
6862 * Don't copy when 'cpo' contains 's' and not entering.
6863 * 'S' BCO_ENTER initialized 's' should_copy
6864 * yes yes X X TRUE
6865 * yes no yes X FALSE
6866 * no X yes X FALSE
6867 * X no no yes FALSE
6868 * X no no no TRUE
6869 * no yes no X TRUE
6870 */
6871 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6872 && (buf->b_p_initialized
6873 || (!(flags & BCO_ENTER)
6874 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6875 should_copy = FALSE;
6876
6877 if (should_copy || (flags & BCO_ALWAYS))
6878 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006879#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006880 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006881 init_buf_opt_idx();
6882#endif
6883 // Don't copy the options specific to a help buffer when
6884 // BCO_NOHELP is given or the options were initialized already
6885 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6887 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006888 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 save_p_isk = buf->b_p_isk;
6891 buf->b_p_isk = NULL;
6892 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006893 // Always free the allocated strings. If not already initialized,
6894 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (!buf->b_p_initialized)
6896 {
6897 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006898 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006901 switch (*p_ffs)
6902 {
6903 case 'm':
6904 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6905 case 'd':
6906 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6907 case 'u':
6908 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6909 default:
6910 buf->b_p_ff = vim_strsave(p_ff);
6911 }
6912 if (buf->b_p_ff != NULL)
6913 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 buf->b_p_bh = empty_option;
6915 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 }
6917 else
6918 free_buf_options(buf, FALSE);
6919
6920 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006921 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 buf->b_p_ai_nopaste = p_ai_nopaste;
6923 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006924 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006926 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 buf->b_p_tw_nopaste = p_tw_nopaste;
6928 buf->b_p_tw_nobin = p_tw_nobin;
6929 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006930 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 buf->b_p_wm_nopaste = p_wm_nopaste;
6932 buf->b_p_wm_nobin = p_wm_nobin;
6933 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006934 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006935 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006936 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006937 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006938 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006940 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006942 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006944 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 buf->b_p_ml_nobin = p_ml_nobin;
6946 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006947 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006948 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006949 buf->b_p_swf = FALSE;
6950 else
6951 {
6952 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006953 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006956 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006957#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006958 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006959 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006961#ifdef FEAT_COMPL_FUNC
6962 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006963 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006964 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006965 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006966 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006967 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006968#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006969#ifdef FEAT_EVAL
6970 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006972 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006975 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006977#ifdef FEAT_VARTABS
6978 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006979 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006980 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006981 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006982 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006983 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006984 buf->b_p_vsts_nopaste = p_vsts_nopaste
6985 ? vim_strsave(p_vsts_nopaste) : NULL;
6986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006988 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006990 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991#ifdef FEAT_FOLDING
6992 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006993 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#endif
6995 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006996 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006997 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006998 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006999 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7000 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007002 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007004 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007006 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007008 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007011 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007013 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007015 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007016 buf->b_p_cinsd = vim_strsave(p_cinsd);
7017 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007018 buf->b_p_lop = vim_strsave(p_lop);
7019 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007020
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007021 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007024 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007026 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007028 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007030 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007032 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007034 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007035#endif
7036#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007037 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007038 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007039 (void)compile_cap_prog(&buf->b_s);
7040 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007042 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007043 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007044 buf->b_s.b_p_spo = vim_strsave(p_spo);
7045 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007047#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007051 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007053 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007054#if defined(FEAT_EVAL)
7055 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007056 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058#ifdef FEAT_CRYPT
7059 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007060 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007063 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064#ifdef FEAT_KEYMAP
7065 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007066 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 buf->b_kmap_state |= KEYMAP_INIT;
7068#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007069#ifdef FEAT_TERMINAL
7070 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007071 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007072#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 // This isn't really an option, but copying the langmap and IME
7074 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007076 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007078 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007080 // options that are normally global but also have a local value
7081 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007083 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007084 buf->b_p_bkc = empty_option;
7085 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086#ifdef FEAT_QUICKFIX
7087 buf->b_p_gp = empty_option;
7088 buf->b_p_mp = empty_option;
7089 buf->b_p_efm = empty_option;
7090#endif
7091 buf->b_p_ep = empty_option;
7092 buf->b_p_kp = empty_option;
7093 buf->b_p_path = empty_option;
7094 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007095 buf->b_p_tc = empty_option;
7096 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097#ifdef FEAT_FIND_ID
7098 buf->b_p_def = empty_option;
7099 buf->b_p_inc = empty_option;
7100# ifdef FEAT_EVAL
7101 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007102 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103# endif
7104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 buf->b_p_dict = empty_option;
7106 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007107#ifdef FEAT_COMPL_FUNC
7108 buf->b_p_tsrfu = empty_option;
7109#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007110 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007111 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007112#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7113 buf->b_p_bexpr = empty_option;
7114#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007115#if defined(FEAT_CRYPT)
7116 buf->b_p_cm = empty_option;
7117#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007118#ifdef FEAT_PERSISTENT_UNDO
7119 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007120 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007121#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007122 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007123 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007125 // Don't copy the options set by ex_help(), use the saved values,
7126 // when going from a help buffer to a non-help buffer.
7127 // Don't touch these at all when BCO_NOHELP is used and going from
7128 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007132#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007133 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007134 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007135 else
7136 buf->b_p_vts_array = NULL;
7137#endif
7138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 else
7140 {
7141 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007142 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 did_isk = TRUE;
7144 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007145 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007146#ifdef FEAT_VARTABS
7147 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007148 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007149 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007150 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007151 else
7152 buf->b_p_vts_array = NULL;
7153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 if (buf->b_p_bt[0] == 'h')
7156 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007158 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 }
7160 }
7161
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007162 // When the options should be copied (ignoring BCO_ALWAYS), set the
7163 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 if (should_copy)
7165 buf->b_p_initialized = TRUE;
7166 }
7167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007168 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 if (did_isk)
7170 (void)buf_init_chartab(buf, FALSE);
7171}
7172
7173/*
7174 * Reset the 'modifiable' option and its default value.
7175 */
7176 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007177reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 int opt_idx;
7180
7181 curbuf->b_p_ma = FALSE;
7182 p_ma = FALSE;
7183 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007184 if (opt_idx >= 0)
7185 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186}
7187
7188/*
7189 * Set the global value for 'iminsert' to the local value.
7190 */
7191 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007192set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 p_iminsert = curbuf->b_p_iminsert;
7195}
7196
7197/*
7198 * Set the global value for 'imsearch' to the local value.
7199 */
7200 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007201set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203 p_imsearch = curbuf->b_p_imsearch;
7204}
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206static int expand_option_idx = -1;
7207static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7208static int expand_option_flags = 0;
7209
7210 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007211set_context_in_set_cmd(
7212 expand_T *xp,
7213 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007214 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 long_u flags = 0; // init for GCC
7218 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 char_u *p;
7220 char_u *s;
7221 int is_term_option = FALSE;
7222 int key;
7223
7224 expand_option_flags = opt_flags;
7225
7226 xp->xp_context = EXPAND_SETTINGS;
7227 if (*arg == NUL)
7228 {
7229 xp->xp_pattern = arg;
7230 return;
7231 }
7232 p = arg + STRLEN(arg) - 1;
7233 if (*p == ' ' && *(p - 1) != '\\')
7234 {
7235 xp->xp_pattern = p + 1;
7236 return;
7237 }
7238 while (p > arg)
7239 {
7240 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007241 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 if (*p == ' ' || *p == ',')
7243 {
7244 while (s > arg && *(s - 1) == '\\')
7245 --s;
7246 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007247 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 if (*p == ' ' && ((p - s) & 1) == 0)
7249 {
7250 ++p;
7251 break;
7252 }
7253 --p;
7254 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007255 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
7257 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007258 xp->xp_prefix = XP_PREFIX_NO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 p += 2;
7260 }
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007261 else if (STRNCMP(p, "inv", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 {
7263 xp->xp_context = EXPAND_BOOL_SETTINGS;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007264 xp->xp_prefix = XP_PREFIX_INV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 p += 3;
7266 }
7267 xp->xp_pattern = arg = p;
7268 if (*arg == '<')
7269 {
7270 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007271 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 return;
7273 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007274 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
7276 xp->xp_context = EXPAND_NOTHING;
7277 return;
7278 }
7279 nextchar = *++p;
7280 is_term_option = TRUE;
7281 expand_option_name[2] = KEY2TERMCAP0(key);
7282 expand_option_name[3] = KEY2TERMCAP1(key);
7283 }
7284 else
7285 {
7286 if (p[0] == 't' && p[1] == '_')
7287 {
7288 p += 2;
7289 if (*p != NUL)
7290 ++p;
7291 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007292 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 nextchar = *++p;
7294 is_term_option = TRUE;
7295 expand_option_name[2] = p[-2];
7296 expand_option_name[3] = p[-1];
7297 }
7298 else
7299 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007300 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7302 p++;
7303 if (*p == NUL)
7304 return;
7305 nextchar = *p;
7306 *p = NUL;
7307 opt_idx = findoption(arg);
7308 *p = nextchar;
7309 if (opt_idx == -1 || options[opt_idx].var == NULL)
7310 {
7311 xp->xp_context = EXPAND_NOTHING;
7312 return;
7313 }
7314 flags = options[opt_idx].flags;
7315 if (flags & P_BOOL)
7316 {
7317 xp->xp_context = EXPAND_NOTHING;
7318 return;
7319 }
7320 }
7321 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7324 {
7325 ++p;
7326 nextchar = '=';
7327 }
7328 if ((nextchar != '=' && nextchar != ':')
7329 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7330 {
7331 xp->xp_context = EXPAND_UNSUCCESSFUL;
7332 return;
7333 }
7334 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7335 {
7336 xp->xp_context = EXPAND_OLD_SETTING;
7337 if (is_term_option)
7338 expand_option_idx = -1;
7339 else
7340 expand_option_idx = opt_idx;
7341 xp->xp_pattern = p + 1;
7342 return;
7343 }
7344 xp->xp_context = EXPAND_NOTHING;
7345 if (is_term_option || (flags & P_NUM))
7346 return;
7347
7348 xp->xp_pattern = p + 1;
7349
7350 if (flags & P_EXPAND)
7351 {
7352 p = options[opt_idx].var;
7353 if (p == (char_u *)&p_bdir
7354 || p == (char_u *)&p_dir
7355 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007356 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#ifdef FEAT_SESSION
7360 || p == (char_u *)&p_vdir
7361#endif
7362 )
7363 {
7364 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007365 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 xp->xp_backslash = XP_BS_THREE;
7367 else
7368 xp->xp_backslash = XP_BS_ONE;
7369 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007370 else if (p == (char_u *)&p_ft)
7371 {
7372 xp->xp_context = EXPAND_FILETYPE;
7373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 else
7375 {
7376 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007377 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (p == (char_u *)&p_tags)
7379 xp->xp_backslash = XP_BS_THREE;
7380 else
7381 xp->xp_backslash = XP_BS_ONE;
7382 }
7383 }
7384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007385 // For an option that is a list of file names, find the start of the
7386 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7388 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007389 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 if (*p == ' ' || *p == ',')
7391 {
7392 s = p;
7393 while (s > xp->xp_pattern && *(s - 1) == '\\')
7394 --s;
7395 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7396 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7397 {
7398 xp->xp_pattern = p + 1;
7399 break;
7400 }
7401 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007402
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007403#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007404 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007405 if (options[opt_idx].var == (char_u *)&p_sps
7406 && STRNCMP(p, "file:", 5) == 0)
7407 {
7408 xp->xp_pattern = p + 5;
7409 break;
7410 }
7411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413}
7414
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007415/*
7416 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7417 *
7418 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7419 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7420 *
7421 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7422 * regular expression 'regmatch', then stores the match in matches[idx] and
7423 * returns TRUE.
7424 *
7425 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7426 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7427 *
7428 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7429 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7430 */
7431 static int
7432match_str(
7433 char_u *str,
7434 regmatch_T *regmatch,
7435 char_u **matches,
7436 int idx,
7437 int test_only,
7438 int fuzzy,
7439 char_u *fuzzystr,
7440 fuzmatch_str_T *fuzmatch)
7441{
7442 if (!fuzzy)
7443 {
7444 if (vim_regexec(regmatch, str, (colnr_T)0))
7445 {
7446 if (!test_only)
7447 matches[idx] = vim_strsave(str);
7448 return TRUE;
7449 }
7450 }
7451 else
7452 {
7453 int score;
7454
7455 score = fuzzy_match_str(str, fuzzystr);
7456 if (score != 0)
7457 {
7458 if (!test_only)
7459 {
7460 fuzmatch[idx].idx = idx;
7461 fuzmatch[idx].str = vim_strsave(str);
7462 fuzmatch[idx].score = score;
7463 }
7464
7465 return TRUE;
7466 }
7467 }
7468
7469 return FALSE;
7470}
7471
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007473ExpandSettings(
7474 expand_T *xp,
7475 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007476 char_u *fuzzystr,
7477 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007478 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007479 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007481 int num_normal = 0; // Nr of matching non-term-code settings
7482 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 int opt_idx;
7484 int match;
7485 int count = 0;
7486 char_u *str;
7487 int loop;
7488 int is_term_opt;
7489 char_u name_buf[MAX_KEY_NAME_LEN];
7490 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007491 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007492 int fuzzy;
7493 fuzmatch_str_T *fuzmatch = NULL;
7494
Christian Brabandtcb747892022-05-08 21:10:56 +01007495 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007497 // do this loop twice:
7498 // loop == 0: count the number of matching options
7499 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 for (loop = 0; loop <= 1; ++loop)
7501 {
7502 regmatch->rm_ic = ic;
7503 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7504 {
K.Takataeeec2542021-06-02 13:28:16 +02007505 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007506 {
7507 if (match_str((char_u *)names[match], regmatch, *matches,
7508 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
7510 if (loop == 0)
7511 num_normal++;
7512 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007513 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7518 opt_idx++)
7519 {
7520 if (options[opt_idx].var == NULL)
7521 continue;
7522 if (xp->xp_context == EXPAND_BOOL_SETTINGS
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007523 && !(options[opt_idx].flags & P_BOOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007525 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 if (is_term_opt && num_normal > 0)
7527 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007528
7529 if (match_str(str, regmatch, *matches, count, (loop == 0),
7530 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {
7532 if (loop == 0)
7533 {
7534 if (is_term_opt)
7535 num_term++;
7536 else
7537 num_normal++;
7538 }
7539 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007540 count++;
7541 }
7542 else if (!fuzzy && options[opt_idx].shortname != NULL
7543 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007544 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007545 {
7546 // Compare against the abbreviated option name (for regular
7547 // expression match). Fuzzy matching (previous if) already
7548 // matches against both the expanded and abbreviated names.
7549 if (loop == 0)
7550 {
7551 if (is_term_opt)
7552 num_term++;
7553 else
7554 num_normal++;
7555 }
7556 else
7557 (*matches)[count++] = vim_strsave(str);
7558 }
7559 else if (is_term_opt)
7560 {
7561 name_buf[0] = '<';
7562 name_buf[1] = 't';
7563 name_buf[2] = '_';
7564 name_buf[3] = str[2];
7565 name_buf[4] = str[3];
7566 name_buf[5] = '>';
7567 name_buf[6] = NUL;
7568
7569 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7570 fuzzy, fuzzystr, fuzmatch))
7571 {
7572 if (loop == 0)
7573 num_term++;
7574 else
7575 count++;
7576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
7578 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007579
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007580 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7582 {
7583 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7584 {
7585 if (!isprint(str[0]) || !isprint(str[1]))
7586 continue;
7587
7588 name_buf[0] = 't';
7589 name_buf[1] = '_';
7590 name_buf[2] = str[0];
7591 name_buf[3] = str[1];
7592 name_buf[4] = NUL;
7593
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007594 if (match_str(name_buf, regmatch, *matches, count,
Bram Moolenaar048d9d22023-05-06 22:21:11 +01007595 (loop == 0), fuzzy, fuzzystr, fuzmatch))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007596 {
7597 if (loop == 0)
7598 num_term++;
7599 else
7600 count++;
7601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 else
7603 {
7604 name_buf[0] = '<';
7605 name_buf[1] = 't';
7606 name_buf[2] = '_';
7607 name_buf[3] = str[0];
7608 name_buf[4] = str[1];
7609 name_buf[5] = '>';
7610 name_buf[6] = NUL;
7611
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007612 if (match_str(name_buf, regmatch, *matches, count,
7613 (loop == 0), fuzzy, fuzzystr,
7614 fuzmatch))
7615 {
7616 if (loop == 0)
7617 num_term++;
7618 else
7619 count++;
7620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622 }
7623
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007624 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007625 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7627 {
7628 name_buf[0] = '<';
7629 STRCPY(name_buf + 1, str);
7630 STRCAT(name_buf, ">");
7631
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007632 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7633 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {
7635 if (loop == 0)
7636 num_term++;
7637 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007638 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640 }
7641 }
7642 if (loop == 0)
7643 {
7644 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007645 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007647 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 else
7649 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007650 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007652 *matches = ALLOC_MULT(char_u *, *numMatches);
7653 if (*matches == NULL)
7654 {
7655 *matches = (char_u **)"";
7656 return FAIL;
7657 }
7658 }
7659 else
7660 {
7661 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7662 if (fuzmatch == NULL)
7663 {
7664 *matches = (char_u **)"";
7665 return FAIL;
7666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 }
7668 }
7669 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007670
7671 if (fuzzy &&
7672 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7673 return FAIL;
7674
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 return OK;
7676}
7677
7678 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007679ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007681 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 char_u *buf;
7683
zeertzjqb0d45ec2023-01-25 15:04:22 +00007684 *numMatches = 0;
7685 *matches = ALLOC_ONE(char_u *);
7686 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 return FAIL;
7688
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007689 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (expand_option_idx < 0)
7691 {
7692 var = find_termcode(expand_option_name + 2);
7693 if (var == NULL)
7694 expand_option_idx = findoption(expand_option_name);
7695 }
7696
7697 if (expand_option_idx >= 0)
7698 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007699 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 option_value2string(&options[expand_option_idx], expand_option_flags);
7701 var = NameBuff;
7702 }
7703 else if (var == NULL)
7704 var = (char_u *)"";
7705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007706 // A backslash is required before some characters. This is the reverse of
7707 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 buf = vim_strsave_escaped(var, escape_chars);
7709
7710 if (buf == NULL)
7711 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007712 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 return FAIL;
7714 }
7715
7716#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007717 // For MS-Windows et al. we don't double backslashes at the start and
7718 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007719 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (var[0] == '\\' && var[1] == '\\'
7721 && expand_option_idx >= 0
7722 && (options[expand_option_idx].flags & P_EXPAND)
7723 && vim_isfilec(var[2])
7724 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007725 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726#endif
7727
zeertzjqb0d45ec2023-01-25 15:04:22 +00007728 *matches[0] = buf;
7729 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 return OK;
7731}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732
7733/*
7734 * Get the value for the numeric or string option *opp in a nice format into
7735 * NameBuff[]. Must not be called with a hidden option!
7736 */
7737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007738option_value2string(
7739 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007740 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
7742 char_u *varp;
7743
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007744 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745
7746 if (opp->flags & P_NUM)
7747 {
7748 long wc = 0;
7749
7750 if (wc_use_keyname(varp, &wc))
7751 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7752 else if (wc != 0)
7753 STRCPY(NameBuff, transchar((int)wc));
7754 else
7755 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7756 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007757 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
7759 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007760 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 NameBuff[0] = NUL;
7762#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007763 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007764 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 STRCPY(NameBuff, "*****");
7766#endif
7767 else if (opp->flags & P_EXPAND)
7768 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007769 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 else if ((char_u **)opp->var == &p_pt)
7771 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7772 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007773 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 }
7775}
7776
7777/*
7778 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7779 * printed as a keyname.
7780 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7781 */
7782 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007783wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7786 {
7787 *wcp = *(long *)varp;
7788 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7789 return TRUE;
7790 }
7791 return FALSE;
7792}
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 * Return TRUE if "x" is present in 'shortmess' option, or
7796 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7797 */
7798 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007799shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007801 return p_shm != NULL &&
7802 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 || (vim_strchr(p_shm, 'a') != NULL
7804 && vim_strchr((char_u *)SHM_A, x) != NULL));
7805}
7806
7807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7809 *
7810 * Reset 'compatible' and set the values for options that didn't get set yet
7811 * to the Vim defaults.
7812 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007813 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 */
7815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007816vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007818 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007819 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007820 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821
7822 if (!option_was_set((char_u *)"cp"))
7823 {
7824 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007825 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7827 set_option_default(opt_idx, OPT_FREE, FALSE);
7828 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007829 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007831
7832 if (fname != NULL)
7833 {
7834 p = vim_getenv(envname, &dofree);
7835 if (p == NULL)
7836 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007837 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007838 p = FullName_save(fname, FALSE);
7839 if (p != NULL)
7840 {
7841 vim_setenv(envname, p);
7842 vim_free(p);
7843 }
7844 }
7845 else if (dofree)
7846 vim_free(p);
7847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850/*
7851 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7852 */
7853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007854change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007856 int opt_idx;
7857
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 if (p_cp != on)
7859 {
7860 p_cp = on;
7861 compatible_set();
7862 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007863 opt_idx = findoption((char_u *)"cp");
7864 if (opt_idx >= 0)
7865 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866}
7867
7868/*
7869 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007870 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 */
7872 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007873option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 int idx;
7876
7877 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007878 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 return FALSE;
7880 if (options[idx].flags & P_WAS_SET)
7881 return TRUE;
7882 return FALSE;
7883}
7884
7885/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007886 * Reset the flag indicating option "name" was set.
7887 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007889reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007890{
7891 int idx = findoption(name);
7892
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007893 if (idx < 0)
7894 return FAIL;
7895
7896 options[idx].flags &= ~P_WAS_SET;
7897 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007898}
7899
7900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 * compatible_set() - Called when 'compatible' has been set or unset.
7902 *
7903 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7904 * flag) to a Vi compatible value.
7905 * When 'compatible' is unset: Set all options that have a different default
7906 * for Vim (without the P_VI_DEF flag) to that default.
7907 */
7908 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007909compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910{
7911 int opt_idx;
7912
Bram Moolenaardac13472019-09-16 21:06:21 +02007913 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7915 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7916 set_option_default(opt_idx, OPT_FREE, p_cp);
7917 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007918 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919}
7920
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 * Check if backspacing over something is allowed.
7923 */
7924 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007925can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007926 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007928#ifdef FEAT_JOB_CHANNEL
7929 if (what == BS_START && bt_prompt(curbuf))
7930 return FALSE;
7931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 switch (*p_bs)
7933 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007934 case '3': return TRUE;
7935 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 case '1': return (what != BS_START);
7937 case '0': return FALSE;
7938 }
7939 return vim_strchr(p_bs, what) != NULL;
7940}
7941
7942/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007943 * Return the effective 'scrolloff' value for the current window, using the
7944 * global value when appropriate.
7945 */
7946 long
7947get_scrolloff_value(void)
7948{
7949 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7950}
7951
7952/*
7953 * Return the effective 'sidescrolloff' value for the current window, using the
7954 * global value when appropriate.
7955 */
7956 long
7957get_sidescrolloff_value(void)
7958{
7959 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7960}
7961
7962/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007963 * Get the local or global value of 'backupcopy'.
7964 */
7965 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007966get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007967{
7968 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7969}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007970
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007971#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007972/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007973 * Get the local or global value of 'formatlistpat'.
7974 */
7975 char_u *
7976get_flp_value(buf_T *buf)
7977{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007978 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7979 return p_flp;
7980 return buf->b_p_flp;
7981}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007982#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007983
7984/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007985 * Get the local or global value of the 'virtualedit' flags.
7986 */
7987 unsigned int
7988get_ve_flags(void)
7989{
Gary Johnson51ad8502021-08-03 18:33:08 +02007990 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7991 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007992}
7993
Bram Moolenaaree857022019-11-09 23:26:40 +01007994#if defined(FEAT_LINEBREAK) || defined(PROTO)
7995/*
7996 * Get the local or global value of 'showbreak'.
7997 */
7998 char_u *
7999get_showbreak_value(win_T *win)
8000{
8001 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8002 return p_sbr;
8003 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8004 return empty_option;
8005 return win->w_p_sbr;
8006}
8007#endif
8008
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008009#if defined(FEAT_EVAL) || defined(PROTO)
8010/*
8011 * Get window or buffer local options.
8012 */
8013 dict_T *
8014get_winbuf_options(int bufopt)
8015{
8016 dict_T *d;
8017 int opt_idx;
8018
8019 d = dict_alloc();
8020 if (d == NULL)
8021 return NULL;
8022
Bram Moolenaardac13472019-09-16 21:06:21 +02008023 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008024 {
8025 struct vimoption *opt = &options[opt_idx];
8026
8027 if ((bufopt && (opt->indir & PV_BUF))
8028 || (!bufopt && (opt->indir & PV_WIN)))
8029 {
8030 char_u *varp = get_varp(opt);
8031
8032 if (varp != NULL)
8033 {
8034 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008035 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008036 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008037 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008038 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008039 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008040 }
8041 }
8042 }
8043
8044 return d;
8045}
8046#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008047
Bram Moolenaardac13472019-09-16 21:06:21 +02008048#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008049/*
8050 * This is called when 'culopt' is changed
8051 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008052 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008053fill_culopt_flags(char_u *val, win_T *wp)
8054{
8055 char_u *p;
8056 char_u culopt_flags_new = 0;
8057
8058 if (val == NULL)
8059 p = wp->w_p_culopt;
8060 else
8061 p = val;
8062 while (*p != NUL)
8063 {
8064 if (STRNCMP(p, "line", 4) == 0)
8065 {
8066 p += 4;
8067 culopt_flags_new |= CULOPT_LINE;
8068 }
8069 else if (STRNCMP(p, "both", 4) == 0)
8070 {
8071 p += 4;
8072 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8073 }
8074 else if (STRNCMP(p, "number", 6) == 0)
8075 {
8076 p += 6;
8077 culopt_flags_new |= CULOPT_NBR;
8078 }
8079 else if (STRNCMP(p, "screenline", 10) == 0)
8080 {
8081 p += 10;
8082 culopt_flags_new |= CULOPT_SCRLINE;
8083 }
8084
8085 if (*p != ',' && *p != NUL)
8086 return FAIL;
8087 if (*p == ',')
8088 ++p;
8089 }
8090
8091 // Can't have both "line" and "screenline".
8092 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8093 return FAIL;
8094 wp->w_p_culopt_flags = culopt_flags_new;
8095
8096 return OK;
8097}
8098#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008099
8100/*
8101 * Get the value of 'magic' adjusted for Vim9 script.
8102 */
8103 int
8104magic_isset(void)
8105{
8106 switch (magic_overruled)
8107 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008108 case OPTION_MAGIC_ON: return TRUE;
8109 case OPTION_MAGIC_OFF: return FALSE;
8110 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008111 }
8112#ifdef FEAT_EVAL
8113 if (in_vim9script())
8114 return TRUE;
8115#endif
8116 return p_magic;
8117}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008118
8119/*
8120 * Set the callback function value for an option that accepts a function name,
8121 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8122 * Returns OK if the option is successfully set to a function, otherwise
8123 * returns FAIL.
8124 */
8125 int
8126option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8127{
8128#ifdef FEAT_EVAL
8129 typval_T *tv;
8130 callback_T cb;
8131
8132 if (optval == NULL || *optval == NUL)
8133 {
8134 free_callback(optcb);
8135 return OK;
8136 }
8137
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008138 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008139 || (STRNCMP(optval, "function(", 9) == 0)
8140 || (STRNCMP(optval, "funcref(", 8) == 0))
8141 // Lambda expression or a funcref
8142 tv = eval_expr(optval, NULL);
8143 else
8144 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008145 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008146 if (tv == NULL)
8147 return FAIL;
8148
8149 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008150 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008151 {
8152 free_tv(tv);
8153 return FAIL;
8154 }
8155
8156 free_callback(optcb);
8157 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008158 if (cb.cb_free_name)
8159 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008160 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008161
8162 // when using Vim9 style "import.funcname" it needs to be expanded to
8163 // "import#funcname".
8164 expand_autload_callback(optcb);
8165
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008166 return OK;
8167#else
8168 return FAIL;
8169#endif
8170}