blob: f47b51c8746722f7370d72c49260e488ab735754 [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;
129 int mustfree;
130 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200131
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000132 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000134 ga_init2(&ga, 1, 100);
135 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
136 {
137 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100138#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000139 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100140# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000141 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100142# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000143 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000145 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100146#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000147 p = vim_getenv((char_u *)names[n], &mustfree);
148 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000150 // First time count the NUL, otherwise count the ','.
151 len = (int)STRLEN(p) + 3;
152 item = alloc(len);
153 STRCPY(item, p);
154 add_pathsep(item);
155 STRCAT(item, "*");
156 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
157 == NULL
158 && ga_grow(&ga, len) == OK)
159 {
160 if (ga.ga_len > 0)
161 STRCAT(ga.ga_data, ",");
162 STRCAT(ga.ga_data, item);
163 ga.ga_len += len;
164 }
165 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000167 if (mustfree)
168 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000170 if (ga.ga_data != NULL)
171 {
172 set_string_default("bsk", ga.ga_data);
173 vim_free(ga.ga_data);
174 }
175}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000177/*
178 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
179 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
180 */
181 static void
182set_init_default_maxmemtot(void)
183{
184 int opt_idx;
185 long_u n;
186
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000188 if (opt_idx < 0)
189 return;
190
191#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
192 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {
ichizok02560422022-04-05 14:18:44 +0100195#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000196 // Use amount of memory available at this moment.
197 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100198#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000199 // Use amount of memory available to Vim.
200 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100201#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000202 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000204 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
205 opt_idx = findoption((char_u *)"maxmem");
206 if (opt_idx >= 0)
207 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000208#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000209 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
210 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000211#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000215}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000217/*
218 * Initialize the 'cdpath' option to a default value.
219 */
220 static void
221set_init_default_cdpath(void)
222{
223 int opt_idx;
224 char_u *cdpath;
225 char_u *buf;
226 int i;
227 int j;
228 int mustfree = FALSE;
229
230 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
231 if (cdpath == NULL)
232 return;
233
234 buf = alloc((STRLEN(cdpath) << 1) + 2);
235 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000237 buf[0] = ','; // start with ",", current dir first
238 j = 1;
239 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000241 if (vim_ispathlistsep(cdpath[i]))
242 buf[j++] = ',';
243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000245 if (cdpath[i] == ' ' || cdpath[i] == ',')
246 buf[j++] = '\\';
247 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 }
249 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000250 buf[j] = NUL;
251 opt_idx = findoption((char_u *)"cdpath");
252 if (opt_idx >= 0)
253 {
254 options[opt_idx].def_val[VI_DEFAULT] = buf;
255 options[opt_idx].flags |= P_DEF_ALLOCED;
256 }
257 else
258 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000260 if (mustfree)
261 vim_free(cdpath);
262}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000264/*
265 * Initialize the 'printencoding' option to a default value.
266 */
267 static void
268set_init_default_printencoding(void)
269{
ichizok02560422022-04-05 14:18:44 +0100270#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000271 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100272 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100274# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000275 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100276# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000277 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100278# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000279 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100280# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000281 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000283 );
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
287#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000288/*
289 * Initialize the 'printexpr' option to a default value.
290 */
291 static void
292set_init_default_printexpr(void)
293{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100294 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100296# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000297 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100298# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
300
ichizok02560422022-04-05 14:18:44 +0100301# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# endif
304 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000305}
306#endif
307
308#ifdef UNIX
309/*
310 * Force restricted-mode on for "nologin" or "false" $SHELL
311 */
312 static void
313set_init_restricted_mode(void)
314{
315 char_u *p;
316
317 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000318 if (p == NULL)
319 return;
320 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
321 restricted = TRUE;
322 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000323}
324#endif
325
326#ifdef CLEAN_RUNTIMEPATH
327/*
328 * When Vim is started with the "--clean" argument, set the default value
329 * for the 'runtimepath' and 'packpath' options.
330 */
331 static void
332set_init_clean_rtp(void)
333{
334 int opt_idx;
335
336 opt_idx = findoption((char_u *)"runtimepath");
337 if (opt_idx >= 0)
338 {
339 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
340 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
341 }
342 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000343 if (opt_idx < 0)
344 return;
345 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
346 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000347}
348#endif
349
350/*
351 * Expand environment variables and things like "~" for the defaults.
352 * If option_expand() returns non-NULL the variable is expanded. This can
353 * only happen for non-indirect options.
354 * Also set the default to the expanded value, so ":set" does not list
355 * them.
356 * Don't set the P_ALLOCED flag, because we don't want to free the
357 * default.
358 */
359 static void
360set_init_expand_env(void)
361{
362 int opt_idx;
363 char_u *p;
364
365 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
366 {
367 if ((options[opt_idx].flags & P_GETTEXT)
368 && options[opt_idx].var != NULL)
369 p = (char_u *)_(*(char **)options[opt_idx].var);
370 else
371 p = option_expand(opt_idx, NULL);
372 if (p != NULL && (p = vim_strsave(p)) != NULL)
373 {
374 *(char_u **)options[opt_idx].var = p;
375 // VIMEXP
376 // Defaults for all expanded options are currently the same for Vi
377 // and Vim. When this changes, add some code here! Also need to
378 // split P_DEF_ALLOCED in two.
379 if (options[opt_idx].flags & P_DEF_ALLOCED)
380 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
381 options[opt_idx].def_val[VI_DEFAULT] = p;
382 options[opt_idx].flags |= P_DEF_ALLOCED;
383 }
384 }
385}
386
387/*
388 * Initialize the 'LANG' environment variable to a default value.
389 */
390 static void
391set_init_lang_env(void)
392{
393#if defined(MSWIN) && defined(FEAT_GETTEXT)
394 // If $LANG isn't set, try to get a good value for it. This makes the
395 // right language be used automatically. Don't do this for English.
396 if (mch_getenv((char_u *)"LANG") == NULL)
397 {
398 char buf[20];
399 long_u n;
400
401 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
402 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
403 // only the first two.
404 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
405 (LPTSTR)buf, 20);
406 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
407 {
408 // There are a few exceptions (probably more)
409 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
410 STRCPY(buf, "zh_TW");
411 else if (STRNICMP(buf, "chs", 3) == 0
412 || STRNICMP(buf, "zhc", 3) == 0)
413 STRCPY(buf, "zh_CN");
414 else if (STRNICMP(buf, "jp", 2) == 0)
415 STRCPY(buf, "ja");
416 else
417 buf[2] = NUL; // truncate to two-letter code
418 vim_setenv((char_u *)"LANG", (char_u *)buf);
419 }
420 }
421#elif defined(MACOS_CONVERT)
422 // Moved to os_mac_conv.c to avoid dependency problems.
423 mac_lang_init();
424#endif
425}
426
427/*
428 * Initialize the 'encoding' option to a default value.
429 */
430 static void
431set_init_default_encoding(void)
432{
433 char_u *p;
434 int opt_idx;
435
436# ifdef MSWIN
437 // MS-Windows has builtin support for conversion to and from Unicode, using
438 // "utf-8" for 'encoding' should work best for most users.
439 p = vim_strsave((char_u *)ENC_DFLT);
440# else
441 // enc_locale() will try to find the encoding of the current locale.
442 // This works best for properly configured systems, old and new.
443 p = enc_locale();
444# endif
445 if (p == NULL)
446 return;
447
448 // Try setting 'encoding' and check if the value is valid.
449 // If not, go back to the default encoding.
450 char_u *save_enc = p_enc;
451 p_enc = p;
452 if (STRCMP(p_enc, "gb18030") == 0)
453 {
454 // We don't support "gb18030", but "cp936" is a good substitute
455 // for practical purposes, thus use that. It's not an alias to
456 // still support conversion between gb18030 and utf-8.
457 p_enc = vim_strsave((char_u *)"cp936");
458 vim_free(p);
459 }
460 if (mb_init() == NULL)
461 {
462 opt_idx = findoption((char_u *)"encoding");
463 if (opt_idx >= 0)
464 {
465 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
466 options[opt_idx].flags |= P_DEF_ALLOCED;
467 }
468
469#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
470 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
471 {
472 // Adjust the default for 'isprint' and 'iskeyword' to match
473 // latin1. Also set the defaults for when 'nocompatible' is
474 // set.
475 set_string_option_direct((char_u *)"isp", -1,
476 ISP_LATIN1, OPT_FREE, SID_NONE);
477 set_string_option_direct((char_u *)"isk", -1,
478 ISK_LATIN1, OPT_FREE, SID_NONE);
479 opt_idx = findoption((char_u *)"isp");
480 if (opt_idx >= 0)
481 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
482 opt_idx = findoption((char_u *)"isk");
483 if (opt_idx >= 0)
484 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
485 (void)init_chartab();
486 }
487#endif
488
489#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
490 // Win32 console: When GetACP() returns a different value from
491 // GetConsoleCP() set 'termencoding'.
492 if (
493# ifdef VIMDLL
494 (!gui.in_use && !gui.starting) &&
495# endif
496 GetACP() != GetConsoleCP())
497 {
498 char buf[50];
499
500 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
501 // Use an alternative value.
502 if (GetConsoleCP() == 0)
503 sprintf(buf, "cp%ld", (long)GetACP());
504 else
505 sprintf(buf, "cp%ld", (long)GetConsoleCP());
506 p_tenc = vim_strsave((char_u *)buf);
507 if (p_tenc != NULL)
508 {
509 opt_idx = findoption((char_u *)"termencoding");
510 if (opt_idx >= 0)
511 {
512 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
513 options[opt_idx].flags |= P_DEF_ALLOCED;
514 }
515 convert_setup(&input_conv, p_tenc, p_enc);
516 convert_setup(&output_conv, p_enc, p_tenc);
517 }
518 else
519 p_tenc = empty_option;
520 }
521#endif
522#if defined(MSWIN)
523 // $HOME may have characters in active code page.
524 init_homedir();
525#endif
526 }
527 else
528 {
529 vim_free(p_enc);
530 p_enc = save_enc;
531 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000532}
533
534/*
535 * Initialize the options, first part.
536 *
537 * Called only once from main(), just after creating the first buffer.
538 * If "clean_arg" is TRUE Vim was started with --clean.
539 */
540 void
541set_init_1(int clean_arg)
542{
543#ifdef FEAT_LANGMAP
544 langmap_init();
545#endif
546
547 // Be Vi compatible by default
548 p_cp = TRUE;
549
550 // Use POSIX compatibility when $VIM_POSIX is set.
551 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
552 {
553 set_string_default("cpo", (char_u *)CPO_ALL);
554 set_string_default("shm", (char_u *)SHM_POSIX);
555 }
556
557 set_init_default_shell();
558 set_init_default_backupskip();
559 set_init_default_maxmemtot();
560 set_init_default_cdpath();
561 set_init_default_printencoding();
562#ifdef FEAT_POSTSCRIPT
563 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564#endif
565
566 /*
567 * Set all the options (except the terminal options) to their default
568 * value. Also set the global value for local options.
569 */
570 set_options_default(0);
571
matveytadbb1bf2022-02-01 17:26:12 +0000572#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000573 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000574#endif
575
Bram Moolenaar07268702018-03-01 21:57:32 +0100576#ifdef CLEAN_RUNTIMEPATH
577 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000578 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100579#endif
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#ifdef FEAT_GUI
582 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100583 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584#endif
585
586 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100587 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100588 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 check_buf_options(curbuf);
590 check_win_options(curwin);
591 check_options();
592
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100593 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 didset_options();
595
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000596#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100597 // Use the current chartab for the generic chartab. This is not in
598 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000599 init_spell_chartab();
600#endif
601
K.Takatace3189d2023-02-15 19:13:43 +0000602 set_init_default_encoding();
603
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000604 // Expand environment variables and things like "~" for the defaults.
605 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100607 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100610 // Detect use of mlterm.
611 // Mlterm is a terminal emulator akin to xterm that has some special
612 // abilities (bidi namely).
613 // NOTE: mlterm's author is being asked to 'set' a variable
614 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100616 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617#endif
618
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200619 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000621 set_init_lang_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100624 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 set_helplang_default(get_mess_lang());
626#endif
627}
628
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200629static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
630
631/*
632 * Set the "fileencodings" option to the default value for when 'encoding' is
633 * utf-8.
634 */
635 void
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +0000636set_fencs_unicode(void)
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200637{
638 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
639 OPT_FREE, 0);
640}
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642/*
643 * Set an option to its default value.
644 * This does not take care of side effects!
645 */
646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100647set_option_default(
648 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100649 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
650 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100652 char_u *varp; // pointer to variable for current option
653 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000655 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
657
658 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
659 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
663 if (flags & P_STRING)
664 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200665 // 'fencs' default value depends on 'encoding'
666 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
667 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 // Use set_string_option_direct() for local options to handle
669 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200670 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200671 set_string_option_direct(NULL, opt_idx,
672 options[opt_idx].def_val[dvi], opt_flags, 0);
673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200675 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
676 free_string_option(*(char_u **)(varp));
677 *(char_u **)varp = options[opt_idx].def_val[dvi];
678 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681 else if (flags & P_NUM)
682 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000683 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 win_comp_scroll(curwin);
685 else
686 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100687 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
688
689 if ((long *)varp == &curwin->w_p_so
690 || (long *)varp == &curwin->w_p_siso)
691 // 'scrolloff' and 'sidescrolloff' local values have a
692 // different default value than the global default.
693 *(long *)varp = -1;
694 else
695 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100696 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (both)
698 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100699 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
701 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100702 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 // the cast to long is required for Manx C, long_i is needed for
705 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000706 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000707#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100708 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000709 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
710 *(int *)varp = FALSE;
711#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (both)
714 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
715 *(int *)varp;
716 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100718 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000719 flagsp = insecure_flag(opt_idx, opt_flags);
720 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
722
723#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200724 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#endif
726}
727
728/*
729 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200730 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100733set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100734 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
736 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000738 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaardac13472019-09-16 21:06:21 +0200740 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200741 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200742 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100743 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200744# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200745 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200746 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200747# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100748 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 set_option_default(i, opt_flags, p_cp);
750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100751 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000752 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200754 parse_cino(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755}
756
757/*
758 * Set the Vi-default value of a string option.
759 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100760 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100762 static void
763set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 char_u *p;
766 int opt_idx;
767
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100768 if (escape && vim_strchr(val, ' ') != NULL)
769 p = vim_strsave_escaped(val, (char_u *)" ");
770 else
771 p = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000772 if (p == NULL) // we don't want a NULL
773 return;
774
775 opt_idx = findoption((char_u *)name);
776 if (opt_idx < 0)
777 return;
778
779 if (options[opt_idx].flags & P_DEF_ALLOCED)
780 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
781 options[opt_idx].def_val[VI_DEFAULT] = p;
782 options[opt_idx].flags |= P_DEF_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783}
784
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100785 void
786set_string_default(char *name, char_u *val)
787{
788 set_string_default_esc(name, val, FALSE);
789}
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200792 * For an option value that contains comma separated items, find "newval" in
793 * "origval". Return NULL if not found.
794 */
795 static char_u *
796find_dup_item(char_u *origval, char_u *newval, long_u flags)
797{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200798 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200799 size_t newlen;
800 char_u *s;
801
802 if (origval == NULL)
803 return NULL;
804
805 newlen = STRLEN(newval);
806 for (s = origval; *s != NUL; ++s)
807 {
808 if ((!(flags & P_COMMA)
809 || s == origval
810 || (s[-1] == ',' && !(bs & 1)))
811 && STRNCMP(s, newval, newlen) == 0
812 && (!(flags & P_COMMA)
813 || s[newlen] == ','
814 || s[newlen] == NUL))
815 return s;
816 // Count backslashes. Only a comma with an even number of backslashes
817 // or a single backslash preceded by a comma before it is recognized as
818 // a separator.
819 if ((s > origval + 1
820 && s[-1] == '\\'
821 && s[-2] != ',')
822 || (s == origval + 1
823 && s[-1] == '\\'))
824 ++bs;
825 else
826 bs = 0;
827 }
828 return NULL;
829}
830
831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 * Set the Vi-default value of a number option.
833 * Used for 'lines' and 'columns'.
834 */
835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100836set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000838 int opt_idx;
839
840 opt_idx = findoption((char_u *)name);
841 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000842 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
Dominique Pelle748b3082022-01-08 12:41:16 +0000845#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200846/*
847 * Set all window-local and buffer-local options to the Vim default.
848 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200849 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200850 */
851 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200852set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200853{
854 win_T *save_curwin = curwin;
855 int i;
856
857 curwin = wp;
858 curbuf = curwin->w_buffer;
859 block_autocmds();
860
Bram Moolenaardac13472019-09-16 21:06:21 +0200861 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200862 {
863 struct vimoption *p = &(options[i]);
864 char_u *varp = get_varp_scope(p, OPT_LOCAL);
865
866 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200867 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200868 && !(options[i].flags & P_NODEFAULT)
869 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200870 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200871 }
872
873 unblock_autocmds();
874 curwin = save_curwin;
875 curbuf = curwin->w_buffer;
876}
Dominique Pelle748b3082022-01-08 12:41:16 +0000877#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200878
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000879#if defined(EXITFREE) || defined(PROTO)
880/*
881 * Free all options.
882 */
883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100884free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000885{
886 int i;
887
Bram Moolenaardac13472019-09-16 21:06:21 +0200888 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000889 {
890 if (options[i].indir == PV_NONE)
891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100892 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100893 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000894 free_string_option(*(char_u **)options[i].var);
895 if (options[i].flags & P_DEF_ALLOCED)
896 free_string_option(options[i].def_val[VI_DEFAULT]);
897 }
898 else if (options[i].var != VAR_WIN
899 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100900 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200901 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000902 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000903 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000904 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000905}
906#endif
907
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909/*
910 * Initialize the options, part two: After getting Rows and Columns and
911 * setting 'term'.
912 */
913 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100914set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000916 int idx;
917
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000918 // 'scroll' defaults to half the window height. The stored default is zero,
919 // which results in the actual value computed from the window height.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000920 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000921 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000922 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 comp_col();
924
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000925 // 'window' is only for backwards compatibility with Vi.
926 // Default is Rows - 1.
Bram Moolenaard68071d2006-05-02 22:08:30 +0000927 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000928 p_window = Rows - 1;
929 set_number_default("window", Rows - 1);
930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100932#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000933 // If 'background' wasn't set by the user, try guessing the value,
934 // depending on the terminal name. Only need to check for terminals
935 // with a dark background, that can handle color.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000936 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000937 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
938 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000940 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100941 // don't mark it as set, when starting the GUI it may be
942 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000943 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000946
947#ifdef CURSOR_SHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000948 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000949#endif
950#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000951 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000952#endif
953#ifdef FEAT_PRINTER
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000954 (void)parse_printoptions(NULL); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Initialize the options, part three: After reading the .vimrc
960 */
961 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100962set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100964#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965/*
966 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
967 * This is done after other initializations, where 'shell' might have been
968 * set, but only if they have not been set before.
969 */
970 char_u *p;
971 int idx_srr;
972 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100973# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 int idx_sp;
975 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000979 if (idx_srr < 0)
980 do_srr = FALSE;
981 else
982 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100983# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000985 if (idx_sp < 0)
986 do_sp = FALSE;
987 else
988 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200990 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (p != NULL)
992 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +0000993 // Default for p_sp is "| tee", for p_srr is ">".
994 // For known shells it is changed here to include stderr.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if ( fnamecmp(p, "csh") == 0
996 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100997# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 || fnamecmp(p, "csh.exe") == 0
999 || fnamecmp(p, "tcsh.exe") == 0
1000# endif
1001 )
1002 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001003# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (do_sp)
1005 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001006# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001008# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1012 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 if (do_srr)
1015 {
1016 p_srr = (char_u *)">&";
1017 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1018 }
1019 }
Mike Williams12795022021-06-28 20:53:58 +02001020# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001021 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1022 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001023 else if ( fnamecmp(p, "powershell") == 0
1024 || fnamecmp(p, "powershell.exe") == 0
1025 )
1026 {
1027# if defined(FEAT_QUICKFIX)
1028 if (do_sp)
1029 {
1030 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1031 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1032 }
1033# endif
1034 if (do_srr)
1035 {
1036 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1037 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1038 }
1039 }
1040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 else
Natanael Copa56318362021-05-06 18:46:35 +02001042 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if ( fnamecmp(p, "sh") == 0
1044 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001045 || fnamecmp(p, "mksh") == 0
1046 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001048 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001050 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001051 || fnamecmp(p, "ash") == 0
1052 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001053 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001054# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 || fnamecmp(p, "cmd") == 0
1056 || fnamecmp(p, "sh.exe") == 0
1057 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001058 || fnamecmp(p, "mksh.exe") == 0
1059 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001061 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 || fnamecmp(p, "bash.exe") == 0
1063 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001064 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001065 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001067 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001069# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (do_sp)
1071 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001072# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001074# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001075 if (fnamecmp(p, "pwsh") == 0)
1076 p_sp = (char_u *)">%s 2>&1";
1077 else
1078 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1081 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (do_srr)
1084 {
1085 p_srr = (char_u *)">%s 2>&1";
1086 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1087 }
1088 }
1089 vim_free(p);
1090 }
1091#endif
1092
Bram Moolenaar4f974752019-02-17 17:44:42 +01001093#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001095 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1096 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001098 * set, but only if they have not been set before.
1099 * Default values depend on shell (cmd.exe is default shell):
1100 *
1101 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001102 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001103 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001104 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001105 * "sh" like shells - "-c" "\""
1106 *
1107 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 */
Mike Williams12795022021-06-28 20:53:58 +02001109 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1110 {
1111 int idx_opt;
1112
1113 idx_opt = findoption((char_u *)"shcf");
1114 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1115 {
1116 p_shcf = (char_u*)"-Command";
1117 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1118 }
1119
1120 idx_opt = findoption((char_u *)"sxq");
1121 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1122 {
1123 p_sxq = (char_u*)"\"";
1124 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1125 }
1126 }
1127 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
1129 int idx3;
1130
1131 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001132 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
1134 p_shcf = (char_u *)"-c";
1135 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1136 }
1137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001138 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001140 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 p_sxq = (char_u *)"\"";
1143 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001146 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1147 {
1148 int idx3;
1149
1150 /*
1151 * cmd.exe on Windows will strip the first and last double quote given
1152 * on the command line, e.g. most of the time things like:
1153 * cmd /c "my path/to/echo" "my args to echo"
1154 * become:
1155 * my path/to/echo" "my args to echo
1156 * when executed.
1157 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001158 * To avoid this, set shellxquote to surround the command in
1159 * parenthesis. This appears to make most commands work, without
1160 * breaking commands that worked previously, such as
1161 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001162 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001163 idx3 = findoption((char_u *)"sxq");
1164 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1165 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001166 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001167 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1168 }
1169
Bram Moolenaara64ba222012-02-12 23:23:31 +01001170 idx3 = findoption((char_u *)"shcf");
1171 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1172 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001173 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001174 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1175 }
1176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#endif
1178
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001179 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001180 {
1181 int idx_ffs = findoption((char_u *)"ffs");
1182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001183 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001184 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1185 set_fileformat(default_fileformat(), OPT_LOCAL);
1186 }
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189}
1190
1191#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1192/*
1193 * When 'helplang' is still at its default value, set it to "lang".
1194 * Only the first two characters of "lang" are used.
1195 */
1196 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001197set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198{
1199 int idx;
1200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 return;
1203 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001204 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1205 return;
1206
1207 if (options[idx].flags & P_ALLOCED)
1208 free_string_option(p_hlg);
1209 p_hlg = vim_strsave(lang);
1210 if (p_hlg == NULL)
1211 p_hlg = empty_option;
1212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001214 // zh_CN becomes "cn", zh_TW becomes "tw"
1215 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001216 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001217 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1218 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001219 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001220 // any C like setting, such as C.UTF-8, becomes "en"
1221 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1222 {
1223 p_hlg[0] = 'e';
1224 p_hlg[1] = 'n';
1225 }
1226 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001228 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229}
1230#endif
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * 'title' and 'icon' only default to true if they have not been set or reset
1234 * in .vimrc and we can read the old value.
1235 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1236 * they can be reset. This reduces startup time when using X on a remote
1237 * machine.
1238 */
1239 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001240set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
1242 int idx1;
1243 long val;
1244
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001245 // If GUI is (going to be) used, we can always set the window title and
1246 // icon name. Saves a bit of time, because the X11 display server does
1247 // not need to be contacted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001249 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
1251#ifdef FEAT_GUI
1252 if (gui.starting || gui.in_use)
1253 val = TRUE;
1254 else
1255#endif
1256 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001257 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 p_title = val;
1259 }
1260 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001261 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001263 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001265
1266#ifdef FEAT_GUI
1267 if (gui.starting || gui.in_use)
1268 val = TRUE;
1269 else
1270#endif
1271 val = mch_can_restore_icon();
1272 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1273 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001276 void
1277ex_set(exarg_T *eap)
1278{
1279 int flags = 0;
1280
1281 if (eap->cmdidx == CMD_setlocal)
1282 flags = OPT_LOCAL;
1283 else if (eap->cmdidx == CMD_setglobal)
1284 flags = OPT_GLOBAL;
1285#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001286 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001287 ex_options(eap);
1288 else
1289#endif
1290 {
1291 if (eap->forceit)
1292 flags |= OPT_ONECOLUMN;
1293 (void)do_set(eap->arg, flags);
1294 }
1295}
1296
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001297/*
1298 * :set operator types
1299 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001300typedef enum {
1301 OP_NONE = 0,
1302 OP_ADDING, // "opt+=arg"
1303 OP_PREPENDING, // "opt^=arg"
1304 OP_REMOVING, // "opt-=arg"
1305} set_op_T;
1306
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001307typedef enum {
1308 PREFIX_NO = 0, // "no" prefix
1309 PREFIX_NONE, // no prefix
1310 PREFIX_INV, // "inv" prefix
1311} set_prefix_T;
1312
1313/*
1314 * Return the prefix type for the option name in *argp.
1315 */
1316 static set_prefix_T
1317get_option_prefix(char_u **argp)
1318{
1319 int prefix = PREFIX_NONE;
1320 char_u *arg = *argp;
1321
1322 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1323 {
1324 prefix = PREFIX_NO;
1325 arg += 2;
1326 }
1327 else if (STRNCMP(arg, "inv", 3) == 0)
1328 {
1329 prefix = PREFIX_INV;
1330 arg += 3;
1331 }
1332
1333 *argp = arg;
1334 return prefix;
1335}
1336
1337/*
1338 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1339 * and the option name length in "*lenp". For a <t_xx> option, return the key
1340 * number in "*keyp".
1341 *
1342 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1343 * otherwise returns OK.
1344 */
1345 static int
1346parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1347{
1348 int key = 0;
1349 int len;
1350 int opt_idx;
1351
1352 if (*arg == '<')
1353 {
1354 opt_idx = -1;
1355 // look out for <t_>;>
1356 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1357 len = 5;
1358 else
1359 {
1360 len = 1;
1361 while (arg[len] != NUL && arg[len] != '>')
1362 ++len;
1363 }
1364 if (arg[len] != '>')
1365 return FAIL;
1366
1367 arg[len] = NUL; // put NUL after name
1368 if (arg[1] == 't' && arg[2] == '_') // could be term code
1369 opt_idx = findoption(arg + 1);
1370 arg[len++] = '>'; // restore '>'
1371 if (opt_idx == -1)
1372 key = find_key_option(arg + 1, TRUE);
1373 }
1374 else
1375 {
1376 int nextchar; // next non-white char after option name
1377
1378 len = 0;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001379 // The two characters after "t_" may not be alphanumeric.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001380 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1381 len = 4;
1382 else
1383 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1384 ++len;
1385 nextchar = arg[len];
1386 arg[len] = NUL; // put NUL after name
1387 opt_idx = findoption(arg);
1388 arg[len] = nextchar; // restore nextchar
1389 if (opt_idx == -1)
1390 key = find_key_option(arg, FALSE);
1391 }
1392
1393 *keyp = key;
1394 *lenp = len;
1395 *opt_idxp = opt_idx;
1396
1397 return OK;
1398}
1399
1400/*
1401 * Get the option operator (+=, ^=, -=).
1402 */
1403 static set_op_T
1404get_opt_op(char_u *arg)
1405{
1406 set_op_T op = OP_NONE;
1407
1408 if (*arg != NUL && *(arg + 1) == '=')
1409 {
1410 if (*arg == '+')
1411 op = OP_ADDING; // "+="
1412 else if (*arg == '^')
1413 op = OP_PREPENDING; // "^="
1414 else if (*arg == '-')
1415 op = OP_REMOVING; // "-="
1416 }
1417
1418 return op;
1419}
1420
1421/*
1422 * Validate whether the value of the option in "opt_idx" can be changed.
1423 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1424 * if it can be changed.
1425 */
1426 static int
1427validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1428{
1429 // Skip all options that are not window-local (used when showing
1430 // an already loaded buffer in a window).
1431 if ((opt_flags & OPT_WINONLY)
1432 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1433 return FAIL;
1434
1435 // Skip all options that are window-local (used for :vimgrep).
1436 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1437 && options[opt_idx].var == VAR_WIN)
1438 return FAIL;
1439
1440 // Disallow changing some options from modelines.
1441 if (opt_flags & OPT_MODELINE)
1442 {
1443 if (flags & (P_SECURE | P_NO_ML))
1444 {
1445 *errmsg = e_not_allowed_in_modeline;
1446 return FAIL;
1447 }
1448 if ((flags & P_MLE) && !p_mle)
1449 {
1450 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1451 return FAIL;
1452 }
1453#ifdef FEAT_DIFF
1454 // In diff mode some options are overruled. This avoids that
1455 // 'foldmethod' becomes "marker" instead of "diff" and that
1456 // "wrap" gets set.
1457 if (curwin->w_p_diff
1458 && opt_idx >= 0 // shut up coverity warning
1459 && (
1460# ifdef FEAT_FOLDING
1461 options[opt_idx].indir == PV_FDM ||
1462# endif
1463 options[opt_idx].indir == PV_WRAP))
1464 return FAIL;
1465#endif
1466 }
1467
1468#ifdef HAVE_SANDBOX
1469 // Disallow changing some options in the sandbox
1470 if (sandbox != 0 && (flags & P_SECURE))
1471 {
1472 *errmsg = e_not_allowed_in_sandbox;
1473 return FAIL;
1474 }
1475#endif
1476
1477 return OK;
1478}
1479
Bram Moolenaar47403942022-09-21 21:12:53 +01001480/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001481 * Get the Vim/Vi default value for a string option.
1482 */
1483 static char_u *
1484stropt_get_default_val(
1485 int opt_idx,
1486 char_u *varp,
1487 int flags,
1488 int cp_val)
1489{
1490 char_u *newval;
1491
1492 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1493 ? VI_DEFAULT : VIM_DEFAULT];
1494 if ((char_u **)varp == &p_bg)
1495 {
1496 // guess the value of 'background'
1497#ifdef FEAT_GUI
1498 if (gui.in_use)
1499 newval = gui_bg_default();
1500 else
1501#endif
1502 newval = term_bg_default();
1503 }
1504 else if ((char_u **)varp == &p_fencs && enc_utf8)
1505 newval = fencs_utf8_default;
1506
1507 // expand environment variables and ~ since the default value was
1508 // already expanded, only required when an environment variable was set
1509 // later
1510 if (newval == NULL)
1511 newval = empty_option;
1512 else
1513 {
1514 char_u *s = option_expand(opt_idx, newval);
1515 if (s == NULL)
1516 s = newval;
1517 newval = vim_strsave(s);
1518 }
1519
1520 return newval;
1521}
1522
1523/*
1524 * Convert the 'backspace' option number value to a string: for adding,
1525 * prepending and removing string.
1526 */
1527 static void
1528opt_backspace_nr2str(
1529 char_u *varp,
1530 char_u **origval_p,
1531 char_u **origval_l_p,
1532 char_u **origval_g_p,
1533 char_u **oldval_p)
1534{
1535 int i = getdigits((char_u **)varp);
1536
1537 switch (i)
1538 {
1539 case 0:
1540 *(char_u **)varp = empty_option;
1541 break;
1542 case 1:
1543 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1544 break;
1545 case 2:
1546 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1547 break;
1548 case 3:
1549 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1550 break;
1551 }
1552 vim_free(*oldval_p);
1553 if (*origval_p == *oldval_p)
1554 *origval_p = *(char_u **)varp;
1555 if (*origval_l_p == *oldval_p)
1556 *origval_l_p = *(char_u **)varp;
1557 if (*origval_g_p == *oldval_p)
1558 *origval_g_p = *(char_u **)varp;
1559 *oldval_p = *(char_u **)varp;
1560}
1561
1562/*
1563 * Convert the 'whichwrap' option number value to a string, for backwards
1564 * compatibility with Vim 3.0.
1565 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1566 */
1567 static char_u *
1568opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1569{
1570 *whichwrap = NUL;
1571 int i = getdigits(argp);
1572 if (i & 1)
1573 STRCAT(whichwrap, "b,");
1574 if (i & 2)
1575 STRCAT(whichwrap, "s,");
1576 if (i & 4)
1577 STRCAT(whichwrap, "h,l,");
1578 if (i & 8)
1579 STRCAT(whichwrap, "<,>,");
1580 if (i & 16)
1581 STRCAT(whichwrap, "[,],");
1582 if (*whichwrap != NUL) // remove trailing ,
1583 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1584
1585 return whichwrap;
1586}
1587
1588/*
1589 * Copy the new string value into allocated memory for the option.
1590 * Can't use set_string_option_direct(), because we need to remove the
1591 * backslashes.
1592 */
1593 static char_u *
1594stropt_copy_value(
1595 char_u *origval,
1596 char_u **argp,
1597 set_op_T op,
1598 int flags UNUSED)
1599{
1600 char_u *arg = *argp;
1601 unsigned newlen;
1602 char_u *newval;
1603 char_u *s = NULL;
1604
1605 // get a bit too much
1606 newlen = (unsigned)STRLEN(arg) + 1;
1607 if (op != OP_NONE)
1608 newlen += (unsigned)STRLEN(origval) + 1;
1609 newval = alloc(newlen);
1610 if (newval == NULL) // out of mem, don't change
1611 return NULL;
1612 s = newval;
1613
1614 // Copy the string, skip over escaped chars.
1615 // For MS-DOS and WIN32 backslashes before normal file name characters
1616 // are not removed, and keep backslash at start, for "\\machine\path",
1617 // but do remove it for "\\\\machine\\path".
1618 // The reverse is found in ExpandOldSetting().
1619 while (*arg != NUL && !VIM_ISWHITE(*arg))
1620 {
1621 int i;
1622
1623 if (*arg == '\\' && arg[1] != NUL
1624#ifdef BACKSLASH_IN_FILENAME
1625 && !((flags & P_EXPAND)
1626 && vim_isfilec(arg[1])
1627 && !VIM_ISWHITE(arg[1])
1628 && (arg[1] != '\\'
1629 || (s == newval && arg[2] != '\\')))
1630#endif
1631 )
1632 ++arg; // remove backslash
1633 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1634 {
1635 // copy multibyte char
1636 mch_memmove(s, arg, (size_t)i);
1637 arg += i;
1638 s += i;
1639 }
1640 else
1641 *s++ = *arg++;
1642 }
1643 *s = NUL;
1644
1645 *argp = arg;
1646 return newval;
1647}
1648
1649/*
1650 * Expand environment variables and ~ in string option value 'newval'.
1651 */
1652 static char_u *
1653stropt_expand_envvar(
1654 int opt_idx,
1655 char_u *origval,
1656 char_u *newval,
1657 set_op_T op)
1658{
1659 char_u *s = option_expand(opt_idx, newval);
1660 if (s == NULL)
1661 return newval;
1662
1663 vim_free(newval);
1664 unsigned newlen = (unsigned)STRLEN(s) + 1;
1665 if (op != OP_NONE)
1666 newlen += (unsigned)STRLEN(origval) + 1;
1667
1668 newval = alloc(newlen);
1669 if (newval == NULL)
1670 return NULL;
1671
1672 STRCPY(newval, s);
1673
1674 return newval;
1675}
1676
1677/*
1678 * Concatenate the original and new values of a string option, adding a "," if
1679 * needed.
1680 */
1681 static void
1682stropt_concat_with_comma(
1683 char_u *origval,
1684 char_u *newval,
1685 set_op_T op,
1686 int flags)
1687{
1688 int len = 0;
1689
1690 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1691 if (op == OP_ADDING)
1692 {
1693 len = (int)STRLEN(origval);
1694 // strip a trailing comma, would get 2
1695 if (comma && len > 1
1696 && (flags & P_ONECOMMA) == P_ONECOMMA
1697 && origval[len - 1] == ','
1698 && origval[len - 2] != '\\')
1699 len--;
1700 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1701 mch_memmove(newval, origval, (size_t)len);
1702 }
1703 else
1704 {
1705 len = (int)STRLEN(newval);
1706 STRMOVE(newval + len + comma, origval);
1707 }
1708 if (comma)
1709 newval[len] = ',';
1710}
1711
1712/*
1713 * Remove a value from a string option. Copy string option value in "origval"
1714 * to "newval" and then remove the string "strval" of length "len".
1715 */
1716 static void
1717stropt_remove_val(
1718 char_u *origval,
1719 char_u *newval,
1720 int flags,
1721 char_u *strval,
1722 int len)
1723{
1724 // Remove newval[] from origval[]. (Note: "len" has been set above
1725 // and is used here).
1726 STRCPY(newval, origval);
1727 if (*strval)
1728 {
1729 // may need to remove a comma
1730 if (flags & P_COMMA)
1731 {
1732 if (strval == origval)
1733 {
1734 // include comma after string
1735 if (strval[len] == ',')
1736 ++len;
1737 }
1738 else
1739 {
1740 // include comma before string
1741 --strval;
1742 ++len;
1743 }
1744 }
1745 STRMOVE(newval + (strval - origval), strval + len);
1746 }
1747}
1748
1749/*
1750 * Remove flags that appear twice in the string option value 'newval'.
1751 */
1752 static void
1753stropt_remove_dupflags(char_u *newval, int flags)
1754{
1755 char_u *s = newval;
1756
1757 // Remove flags that appear twice.
1758 while (*s)
1759 {
1760 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1761 if (flags & P_ONECOMMA)
1762 {
1763 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1764 {
1765 // Remove the duplicated value and the next comma.
1766 STRMOVE(s, s + 2);
1767 continue;
1768 }
1769 }
1770 else
1771 {
1772 if ((!(flags & P_COMMA) || *s != ',')
1773 && vim_strchr(s + 1, *s) != NULL)
1774 {
1775 STRMOVE(s, s + 1);
1776 continue;
1777 }
1778 }
1779 ++s;
1780 }
1781}
1782
1783/*
1784 * Get the string value specified for a ":set" command. The following set
1785 * options are supported:
1786 * set {opt}&
1787 * set {opt}<
1788 * set {opt}={val}
1789 * set {opt}:{val}
1790 */
1791 static char_u *
1792stropt_get_newval(
1793 int nextchar,
1794 int opt_idx,
1795 char_u **argp,
1796 char_u *varp,
1797 char_u **origval_arg,
1798 char_u **origval_l_arg,
1799 char_u **origval_g_arg,
1800 char_u **oldval_arg,
1801 set_op_T *op_arg,
1802 int flags,
1803 int cp_val)
1804{
1805 char_u *arg = *argp;
1806 char_u *origval = *origval_arg;
1807 char_u *origval_l = *origval_l_arg;
1808 char_u *origval_g = *origval_g_arg;
1809 char_u *oldval = *oldval_arg;
1810 set_op_T op = *op_arg;
1811 char_u *save_arg = NULL;
1812 char_u *newval;
1813 char_u *s = NULL;
1814 char_u whichwrap[80];
1815
1816 if (nextchar == '&') // set to default val
1817 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1818 else if (nextchar == '<') // set to global val
1819 newval = vim_strsave(*(char_u **)get_varp_scope(
1820 &(options[opt_idx]), OPT_GLOBAL));
1821 else
1822 {
1823 ++arg; // jump to after the '=' or ':'
1824
1825 // Set 'keywordprg' to ":help" if an empty
1826 // value was passed to :set by the user.
1827 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1828 {
1829 save_arg = arg;
1830 arg = (char_u *)":help";
1831 }
1832 // Convert 'backspace' number to string
1833 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1834 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1835 &oldval);
1836 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1837 {
1838 // Convert 'whichwrap' number to string, for backwards
1839 // compatibility with Vim 3.0.
1840 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1841 save_arg = arg;
1842 arg = t;
1843 }
1844 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1845 // version 3.0
1846 else if (*arg == '>' && (varp == (char_u *)&p_dir
1847 || varp == (char_u *)&p_bdir))
1848 ++arg;
1849
1850 // Copy the new string into allocated memory.
1851 newval = stropt_copy_value(origval, &arg, op, flags);
1852 if (newval == NULL)
1853 goto done;
1854
1855 // Expand environment variables and ~.
1856 // Don't do it when adding without inserting a comma.
1857 if (op == OP_NONE || (flags & P_COMMA))
1858 {
1859 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1860 if (newval == NULL)
1861 goto done;
1862 }
1863
1864 // locate newval[] in origval[] when removing it and when adding to
1865 // avoid duplicates
1866 int len = 0;
1867 if (op == OP_REMOVING || (flags & P_NODUP))
1868 {
1869 len = (int)STRLEN(newval);
1870 s = find_dup_item(origval, newval, flags);
1871
1872 // do not add if already there
1873 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1874 {
1875 op = OP_NONE;
1876 STRCPY(newval, origval);
1877 }
1878
1879 // if no duplicate, move pointer to end of original value
1880 if (s == NULL)
1881 s = origval + (int)STRLEN(origval);
1882 }
1883
1884 // concatenate the two strings; add a ',' if needed
1885 if (op == OP_ADDING || op == OP_PREPENDING)
1886 stropt_concat_with_comma(origval, newval, op, flags);
1887 else if (op == OP_REMOVING)
1888 // Remove newval[] from origval[]. (Note: "len" has been set above
1889 // and is used here).
1890 stropt_remove_val(origval, newval, flags, s, len);
1891
1892 if (flags & P_FLAGLIST)
1893 // Remove flags that appear twice.
1894 stropt_remove_dupflags(newval, flags);
1895 }
1896
1897done:
1898 if (save_arg != NULL)
1899 arg = save_arg; // arg was temporarily changed, restore it
1900 *argp = arg;
1901 *origval_arg = origval;
1902 *origval_l_arg = origval_l;
1903 *origval_g_arg = origval_g;
1904 *oldval_arg = oldval;
1905 *op_arg = op;
1906
1907 return newval;
1908}
1909
1910/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001911 * Part of do_set() for string options.
1912 * Returns FAIL on failure, do not process further options.
1913 */
1914 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001915do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001916 int opt_idx,
1917 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001918 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001919 int nextchar,
1920 set_op_T op_arg,
1921 int flags,
1922 int cp_val,
1923 char_u *varp_arg,
1924 char *errbuf,
1925 int *value_checked,
1926 char **errmsg)
1927{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001928 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001929 set_op_T op = op_arg;
1930 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001931 char_u *oldval = NULL; // previous value if *varp
1932 char_u *newval;
1933 char_u *origval = NULL;
1934 char_u *origval_l = NULL;
1935 char_u *origval_g = NULL;
1936#if defined(FEAT_EVAL)
1937 char_u *saved_origval = NULL;
1938 char_u *saved_origval_l = NULL;
1939 char_u *saved_origval_g = NULL;
1940 char_u *saved_newval = NULL;
1941#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001942
1943 // When using ":set opt=val" for a global option
1944 // with a local value the local value will be
1945 // reset, use the global value here.
1946 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1947 && ((int)options[opt_idx].indir & PV_BOTH))
1948 varp = options[opt_idx].var;
1949
1950 // The old value is kept until we are sure that the new value is valid.
1951 oldval = *(char_u **)varp;
1952
1953 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1954 {
1955 origval_l = *(char_u **)get_varp_scope(
1956 &(options[opt_idx]), OPT_LOCAL);
1957 origval_g = *(char_u **)get_varp_scope(
1958 &(options[opt_idx]), OPT_GLOBAL);
1959
1960 // A global-local string option might have an empty option as value to
1961 // indicate that the global value should be used.
1962 if (((int)options[opt_idx].indir & PV_BOTH)
1963 && origval_l == empty_option)
1964 origval_l = origval_g;
1965 }
1966
1967 // When setting the local value of a global option, the old value may be
1968 // the global value.
1969 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1970 origval = *(char_u **)get_varp(&options[opt_idx]);
1971 else
1972 origval = oldval;
1973
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001974 // Get the new value for the option
1975 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1976 &origval_l, &origval_g, &oldval, &op, flags,
1977 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001978
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001979 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001980 *(char_u **)(varp) = newval;
Bram Moolenaar339e1142023-02-15 14:26:25 +00001981 if (newval == NULL)
1982 *(char_u **)(varp) = empty_option;
Bram Moolenaar47403942022-09-21 21:12:53 +01001983
1984#if defined(FEAT_EVAL)
1985 if (!starting
1986# ifdef FEAT_CRYPT
1987 && options[opt_idx].indir != PV_KEY
1988# endif
1989 && origval != NULL && newval != NULL)
1990 {
1991 // origval may be freed by did_set_string_option(), make a copy.
1992 saved_origval = vim_strsave(origval);
1993 // newval (and varp) may become invalid if the buffer is closed by
1994 // autocommands.
1995 saved_newval = vim_strsave(newval);
1996 if (origval_l != NULL)
1997 saved_origval_l = vim_strsave(origval_l);
1998 if (origval_g != NULL)
1999 saved_origval_g = vim_strsave(origval_g);
2000 }
2001#endif
2002
2003 {
2004 long_u *p = insecure_flag(opt_idx, opt_flags);
2005 int secure_saved = secure;
2006
2007 // When an option is set in the sandbox, from a modeline or in secure
2008 // mode, then deal with side effects in secure mode. Also when the
2009 // value was set with the P_INSECURE flag and is not completely
2010 // replaced.
2011 if ((opt_flags & OPT_MODELINE)
2012#ifdef HAVE_SANDBOX
2013 || sandbox != 0
2014#endif
2015 || (op != OP_NONE && (*p & P_INSECURE)))
2016 secure = 1;
2017
2018 // Handle side effects, and set the global value for ":set" on local
2019 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2020 // be triggered that can cause havoc.
2021 *errmsg = did_set_string_option(
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002022 opt_idx, (char_u **)varp, oldval, newval, errbuf,
Bram Moolenaar47403942022-09-21 21:12:53 +01002023 opt_flags, value_checked);
2024
2025 secure = secure_saved;
2026 }
2027
2028#if defined(FEAT_EVAL)
2029 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002030 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002031 saved_origval_l, saved_origval_g, saved_newval);
2032 vim_free(saved_origval);
2033 vim_free(saved_origval_l);
2034 vim_free(saved_origval_g);
2035 vim_free(saved_newval);
2036#endif
2037
Bram Moolenaar6f981142022-09-22 12:48:58 +01002038 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002039 return *errmsg == NULL ? OK : FAIL;
2040}
2041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002043 * Set a boolean option.
2044 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002045 */
2046 static char *
2047do_set_option_bool(
2048 int opt_idx,
2049 int opt_flags,
2050 set_prefix_T prefix,
2051 long_u flags,
2052 char_u *varp,
2053 int nextchar,
2054 int afterchar,
2055 int cp_val)
2056
2057{
2058 varnumber_T value;
2059
2060 if (nextchar == '=' || nextchar == ':')
2061 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002062 if (opt_idx < 0 || varp == NULL)
2063 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002064
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002065 // ":set opt!": invert
2066 // ":set opt&": reset to default value
2067 // ":set opt<": reset to global value
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002068 if (nextchar == '!')
2069 value = *(int *)(varp) ^ 1;
2070 else if (nextchar == '&')
2071 value = (int)(long)(long_i)options[opt_idx].def_val[
2072 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2073 else if (nextchar == '<')
2074 {
2075 // For 'autoread' -1 means to use global value.
2076 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2077 value = -1;
2078 else
2079 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2080 }
2081 else
2082 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002083 // ":set invopt": invert
2084 // ":set opt" or ":set noopt": set or reset
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002085 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2086 return e_trailing_characters;
2087 if (prefix == PREFIX_INV)
2088 value = *(int *)(varp) ^ 1;
2089 else
2090 value = prefix == PREFIX_NO ? 0 : 1;
2091 }
2092
2093 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2094}
2095
2096/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002097 * Set a numeric option.
2098 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002099 */
2100 static char *
2101do_set_option_numeric(
2102 int opt_idx,
2103 int opt_flags,
2104 char_u **argp,
2105 int nextchar,
2106 set_op_T op,
2107 long_u flags,
2108 int cp_val,
2109 char_u *varp,
2110 char *errbuf,
2111 size_t errbuflen)
2112{
2113 char_u *arg = *argp;
2114 varnumber_T value;
2115 int i;
2116 char *errmsg = NULL;
2117
Bram Moolenaar546933f2023-02-06 16:40:49 +00002118 if (opt_idx < 0 || varp == NULL)
2119 return NULL; // "cannot happen"
2120 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002121 /*
2122 * Different ways to set a number option:
2123 * & set to default value
2124 * < set to global value
2125 * <xx> accept special key codes for 'wildchar'
2126 * c accept any non-digit for 'wildchar'
2127 * [-]0-9 set number
2128 * other error
2129 */
2130 ++arg;
2131 if (nextchar == '&')
2132 value = (long)(long_i)options[opt_idx].def_val[
2133 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2134 else if (nextchar == '<')
2135 {
2136 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2137 // use the global value.
2138 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2139 value = NO_LOCAL_UNDOLEVEL;
2140 else
2141 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2142 }
2143 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2144 && (*arg == '<'
2145 || *arg == '^'
2146 || (*arg != NUL
2147 && (!arg[1] || VIM_ISWHITE(arg[1]))
2148 && !VIM_ISDIGIT(*arg))))
2149 {
2150 value = string_to_key(arg, FALSE);
2151 if (value == 0 && (long *)varp != &p_wcm)
2152 {
2153 errmsg = e_invalid_argument;
2154 goto skip;
2155 }
2156 }
2157 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2158 {
2159 // Allow negative (for 'undolevels'), octal and hex numbers.
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002160 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE, NULL);
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002161 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2162 {
2163 errmsg = e_number_required_after_equal;
2164 goto skip;
2165 }
2166 }
2167 else
2168 {
2169 errmsg = e_number_required_after_equal;
2170 goto skip;
2171 }
2172
2173 if (op == OP_ADDING)
2174 value = *(long *)varp + value;
2175 else if (op == OP_PREPENDING)
2176 value = *(long *)varp * value;
2177 else if (op == OP_REMOVING)
2178 value = *(long *)varp - value;
2179
2180 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2181 opt_flags);
2182
2183skip:
2184 *argp = arg;
2185 return errmsg;
2186}
2187
2188/*
2189 * Set a key code (t_xx) option
2190 */
2191 static char *
2192do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2193{
2194 char_u *arg = *argp;
2195 char_u *p;
2196
2197 if (nextchar == '&')
2198 {
2199 if (add_termcap_entry(key_name, TRUE) == FAIL)
2200 return e_not_found_in_termcap;
2201 }
2202 else
2203 {
2204 ++arg; // jump to after the '=' or ':'
2205 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2206 if (*p == '\\' && p[1] != NUL)
2207 ++p;
2208 nextchar = *p;
2209 *p = NUL;
2210 add_termcode(key_name, arg, FALSE);
2211 *p = nextchar;
2212 }
2213 if (full_screen)
2214 ttest(FALSE);
2215 redraw_all_later(UPD_CLEAR);
2216
2217 *argp = arg;
2218 return NULL;
2219}
2220
2221/*
2222 * Set an option to a new value.
2223 */
2224 static char *
2225do_set_option_value(
2226 int opt_idx,
2227 int opt_flags,
2228 char_u **argp,
2229 set_prefix_T prefix,
2230 set_op_T op,
2231 long_u flags,
2232 char_u *varp,
2233 char_u *key_name,
2234 int nextchar,
2235 int afterchar,
2236 int cp_val,
2237 int *stopopteval,
2238 char *errbuf,
2239 size_t errbuflen)
2240{
2241 int value_checked = FALSE;
2242 char *errmsg = NULL;
2243 char_u *arg = *argp;
2244
2245 if (flags & P_BOOL)
2246 {
2247 // boolean option
2248 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2249 nextchar, afterchar, cp_val);
2250 if (errmsg != NULL)
2251 goto skip;
2252 }
2253 else
2254 {
2255 // numeric or string option
2256 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2257 || prefix != PREFIX_NONE)
2258 {
2259 errmsg = e_invalid_argument;
2260 goto skip;
2261 }
2262
2263 if (flags & P_NUM)
2264 {
2265 // numeric option
2266 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2267 op, flags, cp_val, varp,
2268 errbuf, errbuflen);
2269 if (errmsg != NULL)
2270 goto skip;
2271 }
2272 else if (opt_idx >= 0)
2273 {
2274 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002275 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2276 flags, cp_val, varp, errbuf,
2277 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002278 {
2279 if (errmsg != NULL)
2280 goto skip;
2281 *stopopteval = TRUE;
2282 goto skip;
2283 }
2284 }
2285 else
2286 {
2287 // key code option
2288 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2289 if (errmsg != NULL)
2290 goto skip;
2291 }
2292 }
2293
2294 if (opt_idx >= 0)
2295 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2296
2297skip:
2298 *argp = arg;
2299 return errmsg;
2300}
2301
2302/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002303 * Set an option to a new value.
2304 * Return NULL if OK, return an untranslated error message when something is
2305 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2306 */
2307 static char *
2308do_set_option(
2309 int opt_flags,
2310 char_u **argp,
2311 char_u *arg_start,
2312 char_u **startarg,
2313 int *did_show,
2314 int *stopopteval,
2315 char *errbuf,
2316 size_t errbuflen)
2317{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002318 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002319 char_u *arg;
2320 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2321 set_op_T op;
2322 long_u flags; // flags for current option
2323 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002324 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002325 int nextchar; // next non-white char after option name
2326 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002327 char *errmsg = NULL;
2328 int key;
2329 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002330
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002331 prefix = get_option_prefix(argp);
2332 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002333
2334 // find end of name
2335 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002336 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2337 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002338
2339 // remember character after option name
2340 afterchar = arg[len];
2341
2342 if (in_vim9script())
2343 {
2344 char_u *p = skipwhite(arg + len);
2345
2346 // disallow white space before =val, +=val, -=val, ^=val
2347 if (p > arg + len && (p[0] == '='
2348 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2349 && p[1] == '=')))
2350 {
2351 errmsg = e_no_white_space_allowed_between_option_and;
2352 arg = p;
2353 *startarg = p;
2354 goto skip;
2355 }
2356 }
2357 else
2358 // skip white space, allow ":set ai ?", ":set hlsearch !"
2359 while (VIM_ISWHITE(arg[len]))
2360 ++len;
2361
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002362 op = get_opt_op(arg + len);
2363 if (op != OP_NONE)
2364 len++;
2365
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002366 nextchar = arg[len];
2367
2368 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2369 {
2370 if (in_vim9script() && arg > arg_start
2371 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2372 errmsg = e_no_white_space_allowed_between_option_and;
2373 else
2374 errmsg = e_unknown_option;
2375 goto skip;
2376 }
2377
2378 if (opt_idx >= 0)
2379 {
2380 if (options[opt_idx].var == NULL) // hidden option: skip
2381 {
2382 // Only give an error message when requesting the value of
2383 // a hidden option, ignore setting it.
2384 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2385 && (!(options[opt_idx].flags & P_BOOL)
2386 || nextchar == '?'))
2387 errmsg = e_option_not_supported;
2388 goto skip;
2389 }
2390
2391 flags = options[opt_idx].flags;
2392 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2393 }
2394 else
2395 {
2396 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002397 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002398 if (key < 0)
2399 {
2400 key_name[0] = KEY2TERMCAP0(key);
2401 key_name[1] = KEY2TERMCAP1(key);
2402 }
2403 else
2404 {
2405 key_name[0] = KS_KEY;
2406 key_name[1] = (key & 0xff);
2407 }
2408 }
2409
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002410 // Make sure the option value can be changed.
2411 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002412 goto skip;
2413
Bram Moolenaar40b48722023-02-05 17:04:50 +00002414 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002415 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2416 {
2417 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002418 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2419 {
2420 if (arg[3] == 'm') // "opt&vim": set to Vim default
2421 {
2422 cp_val = FALSE;
2423 arg += 3;
2424 }
2425 else // "opt&vi": set to Vi default
2426 {
2427 cp_val = TRUE;
2428 arg += 2;
2429 }
2430 }
2431 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2432 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2433 {
2434 errmsg = e_trailing_characters;
2435 goto skip;
2436 }
2437 }
2438
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002439 // Allow '=' and ':' for historical reasons (MSDOS command.com).
2440 // Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002441 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002442 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002443 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2444 && !(flags & P_BOOL)))
2445 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002446 // print value
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002447 if (*did_show)
2448 msg_putchar('\n'); // cursor below last one
2449 else
2450 {
2451 gotocmdline(TRUE); // cursor at status line
2452 *did_show = TRUE; // remember that we did a line
2453 }
2454 if (opt_idx >= 0)
2455 {
2456 showoneopt(&options[opt_idx], opt_flags);
2457#ifdef FEAT_EVAL
2458 if (p_verbose > 0)
2459 {
2460 // Mention where the option was last set.
2461 if (varp == options[opt_idx].var)
2462 last_set_msg(options[opt_idx].script_ctx);
2463 else if ((int)options[opt_idx].indir & PV_WIN)
2464 last_set_msg(curwin->w_p_script_ctx[
2465 (int)options[opt_idx].indir & PV_MASK]);
2466 else if ((int)options[opt_idx].indir & PV_BUF)
2467 last_set_msg(curbuf->b_p_script_ctx[
2468 (int)options[opt_idx].indir & PV_MASK]);
2469 }
2470#endif
2471 }
2472 else
2473 {
2474 char_u *p;
2475
2476 p = find_termcode(key_name);
2477 if (p == NULL)
2478 {
2479 errmsg = e_key_code_not_set;
2480 goto skip;
2481 }
2482 else
2483 (void)show_one_termcode(key_name, p, TRUE);
2484 }
2485 if (nextchar != '?'
2486 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2487 errmsg = e_trailing_characters;
2488 }
2489 else
2490 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002491 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2492 flags, varp, key_name, nextchar,
2493 afterchar, cp_val, stopopteval, errbuf,
2494 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002495 }
2496
2497skip:
2498 *argp = arg;
2499 return errmsg;
2500}
2501
2502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * Parse 'arg' for option settings.
2504 *
2505 * 'arg' may be IObuff, but only when no errors can be present and option
2506 * does not need to be expanded with option_expand().
2507 * "opt_flags":
2508 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002509 * OPT_GLOBAL for ":setglobal"
2510 * OPT_LOCAL for ":setlocal" and a modeline
2511 * OPT_MODELINE for a modeline
2512 * OPT_WINONLY to only set window-local options
2513 * OPT_NOWIN to skip setting window-local options
2514 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002516 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 */
2518 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002519do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002520 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002521 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002523 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002525 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527 if (*arg == NUL)
2528 {
2529 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002530 did_show = TRUE;
2531 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
2533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002534 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002536 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002537 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002539 // ":set all" show all options.
2540 // ":set all&" set all options to their default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 arg += 3;
2542 if (*arg == '&')
2543 {
2544 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002545 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002547 didset_options();
2548 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002549 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
2551 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002554 did_show = TRUE;
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002557 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002560 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002561 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 arg += 7;
2563 }
2564 else
2565 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002566 int stopopteval = FALSE;
2567 char *errmsg = NULL;
2568 char errbuf[80];
2569 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002571 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2572 &did_show, &stopopteval, errbuf,
2573 sizeof(errbuf));
2574 if (stopopteval)
2575 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002577 // Advance to next argument.
2578 // - skip until a blank found, taking care of backslashes
2579 // - skip blanks
2580 // - skip one "=val" argument (for hidden options ":set gfn =xx")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 for (i = 0; i < 2 ; ++i)
2582 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002583 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (*arg++ == '\\' && *arg != NUL)
2585 ++arg;
2586 arg = skipwhite(arg);
2587 if (*arg != '=')
2588 break;
2589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002591 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002593 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2594 i = (int)STRLEN(IObuff) + 2;
2595 if (i + (arg - startarg) < IOSIZE)
2596 {
2597 // append the argument with the error
2598 STRCAT(IObuff, ": ");
2599 mch_memmove(IObuff + i, startarg, (arg - startarg));
2600 IObuff[i + (arg - startarg)] = NUL;
2601 }
2602 // make sure all characters are printable
2603 trans_characters(IObuff, IOSIZE);
2604
2605 ++no_wait_return; // wait_return() done later
2606 emsg((char *)IObuff); // show error highlighted
2607 --no_wait_return;
2608
2609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
2612
2613 arg = skipwhite(arg);
2614 }
2615
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002616theend:
2617 if (silent_mode && did_show)
2618 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002619 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002620 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002621 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002622 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002623 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002624 out_flush();
2625 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002626 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002627 }
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 return OK;
2630}
2631
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002632/*
2633 * Call this when an option has been given a new value through a user command.
2634 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2635 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002636 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002637did_set_option(
2638 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002639 int opt_flags, // possibly with OPT_MODELINE
2640 int new_value, // value was replaced completely
2641 int value_checked) // value was checked to be safe, no need to set the
2642 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002643{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002644 long_u *p;
2645
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002646 options[opt_idx].flags |= P_WAS_SET;
2647
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002648 // When an option is set in the sandbox, from a modeline or in secure mode
2649 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2650 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002651 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002652 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002653#ifdef HAVE_SANDBOX
2654 || sandbox != 0
2655#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002656 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657 *p = *p | P_INSECURE;
2658 else if (new_value)
2659 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002660}
2661
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662/*
2663 * Convert a key name or string into a key value.
2664 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002665 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002667 int
2668string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
2670 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002671 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (*arg == '^')
2673 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002674 if (multi_byte)
2675 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return *arg;
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
2680 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2681 * maketitle() to create and display it.
2682 * When switching the title or icon off, call mch_restore_title() to get
2683 * the old value back.
2684 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002685 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002686did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 if (starting != NO_SCREEN
2689#ifdef FEAT_GUI
2690 && !gui.starting
2691#endif
2692 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696/*
2697 * set_options_bin - called when 'bin' changes value.
2698 */
2699 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002700set_options_bin(
2701 int oldval,
2702 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002705 // The option values that are changed when 'bin' changes are
2706 // copied when 'bin is set and restored when 'bin' is reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (newval)
2708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
2711 if (!(opt_flags & OPT_GLOBAL))
2712 {
2713 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2714 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2715 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2716 curbuf->b_p_et_nobin = curbuf->b_p_et;
2717 }
2718 if (!(opt_flags & OPT_LOCAL))
2719 {
2720 p_tw_nobin = p_tw;
2721 p_wm_nobin = p_wm;
2722 p_ml_nobin = p_ml;
2723 p_et_nobin = p_et;
2724 }
2725 }
2726
2727 if (!(opt_flags & OPT_GLOBAL))
2728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 curbuf->b_p_tw = 0; // no automatic line wrap
2730 curbuf->b_p_wm = 0; // no automatic line wrap
2731 curbuf->b_p_ml = 0; // no modelines
2732 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 if (!(opt_flags & OPT_LOCAL))
2735 {
2736 p_tw = 0;
2737 p_wm = 0;
2738 p_ml = FALSE;
2739 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
2745 if (!(opt_flags & OPT_GLOBAL))
2746 {
2747 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2748 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2749 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2750 curbuf->b_p_et = curbuf->b_p_et_nobin;
2751 }
2752 if (!(opt_flags & OPT_LOCAL))
2753 {
2754 p_tw = p_tw_nobin;
2755 p_wm = p_wm_nobin;
2756 p_ml = p_ml_nobin;
2757 p_et = p_et_nobin;
2758 }
2759 }
2760}
2761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762/*
2763 * Expand environment variables for some string options.
2764 * These string options cannot be indirect!
2765 * If "val" is NULL expand the current value of the option.
2766 * Return pointer to NameBuff, or NULL when not expanded.
2767 */
2768 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002769option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2773 return NULL;
2774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 // If val is longer than MAXPATHL no meaningful expansion can be done,
2776 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (val != NULL && STRLEN(val) > MAXPATHL)
2778 return NULL;
2779
2780 if (val == NULL)
2781 val = *(char_u **)options[opt_idx].var;
2782
2783 /*
2784 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2785 * Escape spaces when expanding 'tags', they are used to separate file
2786 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002787 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
2789 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002790 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002791#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002792 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2793#endif
2794 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return NULL;
2797
2798 return NameBuff;
2799}
2800
2801/*
2802 * After setting various option values: recompute variables that depend on
2803 * option values.
2804 */
2805 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002806didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002808 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 (void)init_chartab();
2810
Bram Moolenaardac13472019-09-16 21:06:21 +02002811 didset_string_options();
2812
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002813#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002814 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002815 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002816 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002817 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002818#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002819 // set cedit_key
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002820 (void)did_set_cedit(NULL);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002821#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002822 // initialize the table for 'breakat'.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002823 did_set_breakat(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002824#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002825 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002826}
2827
2828/*
2829 * More side effects of setting options.
2830 */
2831 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002832didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002833{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002834 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002835 (void)highlight_changed();
2836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002838 check_opt_wim();
2839
Bram Moolenaareed9d462021-02-15 20:38:25 +01002840 // Parse default for 'listchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002841 (void)set_listchars_option(curwin, curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // Parse default for 'fillchars'.
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00002844 (void)set_fillchars_option(curwin, curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002845
2846#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // Parse default for 'clipboard'
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002848 (void)did_set_clipboard(NULL);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002849#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002850#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002851 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002852 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002853 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002854 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856}
2857
2858/*
2859 * Check for string options that are NULL (normally only termcap options).
2860 */
2861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 int opt_idx;
2865
2866 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2867 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2868 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2869}
2870
2871/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002872 * Return the option index found by a pointer into term_strings[].
2873 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002875 int
2876get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002878 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2881 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002882 return opt_idx;
2883 return -1; // cannot happen: didn't find it!
2884}
2885
2886/*
2887 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2888 * Return the option index or -1 if not found.
2889 */
2890 int
2891set_term_option_alloced(char_u **p)
2892{
2893 int opt_idx = get_term_opt_idx(p);
2894
2895 if (opt_idx >= 0)
2896 options[opt_idx].flags |= P_ALLOCED;
2897 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898}
2899
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002900#if defined(FEAT_EVAL) || defined(PROTO)
2901/*
2902 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2903 * Return FALSE when it wasn't.
2904 * Return -1 for an unknown option.
2905 */
2906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002907was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002908{
2909 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002910 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002911
2912 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002913 {
2914 flagp = insecure_flag(idx, opt_flags);
2915 return (*flagp & P_INSECURE) != 0;
2916 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002917 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002918 return -1;
2919}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002920
2921/*
2922 * Get a pointer to the flags used for the P_INSECURE flag of option
2923 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002924 * NOTE: Caller must make sure that "curwin" is set to the window from which
2925 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002926 */
2927 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002928insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002929{
2930 if (opt_flags & OPT_LOCAL)
2931 switch ((int)options[opt_idx].indir)
2932 {
2933#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002934 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002935#endif
2936#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002937# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002938 case PV_FDE: return &curwin->w_p_fde_flags;
2939 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002940# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002941# ifdef FEAT_BEVAL
2942 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2943# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002944 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002945 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002946# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002947 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002948# endif
2949#endif
2950 }
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002953 return &options[opt_idx].flags;
2954}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002955#endif
2956
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002957/*
2958 * Redraw the window title and/or tab page text later.
2959 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002960void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002961{
2962 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002963 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002964}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002967 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2968 * characters or characters in "allowed".
2969 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002970 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002971valid_name(char_u *val, char *allowed)
2972{
2973 char_u *s;
2974
2975 for (s = val; *s != NUL; ++s)
2976 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2977 return FALSE;
2978 return TRUE;
2979}
2980
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002981#if defined(FEAT_EVAL) || defined(PROTO)
2982/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002983 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002984 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002985 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002986 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002987set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002988{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002989 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2990 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002991 sctx_T new_script_ctx = script_ctx;
2992
Bram Moolenaar51258742020-05-03 17:19:33 +02002993 // Modeline already has the line number set.
2994 if (!(opt_flags & OPT_MODELINE))
2995 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // Remember where the option was set. For local options need to do that
2998 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002999 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003000 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003001 if (both || (opt_flags & OPT_LOCAL))
3002 {
3003 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003004 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003005 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003006 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003007 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003008 if (both)
3009 // also setting the "all buffers" value
3010 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3011 new_script_ctx;
3012 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003013 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003014}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003015
3016/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003017 * Get the script context of global option "name".
3018 *
3019 */
3020 sctx_T *
3021get_option_sctx(char *name)
3022{
3023 int idx = findoption((char_u *)name);
3024
3025 if (idx >= 0)
3026 return &options[idx].script_ctx;
3027 siemsg("no such option: %s", name);
3028 return NULL;
3029}
3030
3031/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003032 * Set the script_ctx for a termcap option.
3033 * "name" must be the two character code, e.g. "RV".
3034 * When "name" is NULL use "opt_idx".
3035 */
3036 void
3037set_term_option_sctx_idx(char *name, int opt_idx)
3038{
3039 char_u buf[5];
3040 int idx;
3041
3042 if (name == NULL)
3043 idx = opt_idx;
3044 else
3045 {
3046 buf[0] = 't';
3047 buf[1] = '_';
3048 buf[2] = name[0];
3049 buf[3] = name[1];
3050 buf[4] = 0;
3051 idx = findoption(buf);
3052 }
3053 if (idx >= 0)
3054 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3055}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003056#endif
3057
Bram Moolenaarf4140482020-02-15 23:06:45 +01003058#if defined(FEAT_EVAL)
3059/*
3060 * Apply the OptionSet autocommand.
3061 */
3062 static void
3063apply_optionset_autocmd(
3064 int opt_idx,
3065 long opt_flags,
3066 long oldval,
3067 long oldval_g,
3068 long newval,
3069 char *errmsg)
3070{
3071 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3072
3073 // Don't do this while starting up, failure or recursively.
3074 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3075 return;
3076
3077 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3078 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3079 oldval_g);
3080 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3081 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3082 (opt_flags & OPT_LOCAL) ? "local" : "global");
3083 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3084 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3085 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3086 if (opt_flags & OPT_LOCAL)
3087 {
3088 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3089 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3090 }
3091 if (opt_flags & OPT_GLOBAL)
3092 {
3093 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3094 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3095 }
3096 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3097 {
3098 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3099 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3100 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3101 }
3102 if (opt_flags & OPT_MODELINE)
3103 {
3104 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3105 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3106 }
3107 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3108 NULL, FALSE, NULL);
3109 reset_v_option_vars();
3110}
3111#endif
3112
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003113#if defined(FEAT_ARABIC) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003114/*
3115 * Process the updated 'arabic' option value.
3116 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003117 char *
3118did_set_arabic(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003119{
3120 char *errmsg = NULL;
3121
3122 if (curwin->w_p_arab)
3123 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003124 // 'arabic' is set, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003125 if (!p_tbidi)
3126 {
3127 // set rightleft mode
3128 if (!curwin->w_p_rl)
3129 {
3130 curwin->w_p_rl = TRUE;
3131 changed_window_setting();
3132 }
3133
3134 // Enable Arabic shaping (major part of what Arabic requires)
3135 if (!p_arshape)
3136 {
3137 p_arshape = TRUE;
3138 redraw_later_clear();
3139 }
3140 }
3141
3142 // Arabic requires a utf-8 encoding, inform the user if it's not
3143 // set.
3144 if (STRCMP(p_enc, "utf-8") != 0)
3145 {
3146 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3147
3148 msg_source(HL_ATTR(HLF_W));
3149 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3150#ifdef FEAT_EVAL
3151 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3152#endif
3153 }
3154
3155 // set 'delcombine'
3156 p_deco = TRUE;
3157
3158# ifdef FEAT_KEYMAP
3159 // Force-set the necessary keymap for arabic
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003160 errmsg = set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3161 OPT_LOCAL);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003162# endif
3163 }
3164 else
3165 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00003166 // 'arabic' is reset, handle various sub-settings.
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003167 if (!p_tbidi)
3168 {
3169 // reset rightleft mode
3170 if (curwin->w_p_rl)
3171 {
3172 curwin->w_p_rl = FALSE;
3173 changed_window_setting();
3174 }
3175
3176 // 'arabicshape' isn't reset, it is a global option and
3177 // another window may still need it "on".
3178 }
3179
3180 // 'delcombine' isn't reset, it is a global option and another
3181 // window may still want it "on".
3182
3183# ifdef FEAT_KEYMAP
3184 // Revert to the default keymap
3185 curbuf->b_p_iminsert = B_IMODE_NONE;
3186 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3187# endif
3188 }
3189
3190 return errmsg;
3191}
3192#endif
3193
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003194#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
3195/*
3196 * Process the updated 'autochdir' option value.
3197 */
3198 char *
3199did_set_autochdir(optset_T *args UNUSED)
3200{
3201 // Change directories when the 'acd' option is set now.
3202 DO_AUTOCHDIR;
3203 return NULL;
3204}
3205#endif
3206
3207#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
3208/*
3209 * Process the updated 'ballooneval' option value.
3210 */
3211 char *
3212did_set_ballooneval(optset_T *args)
3213{
3214 if (balloonEvalForTerm)
3215 return NULL;
3216
3217 if (p_beval && !args->os_oldval.boolean)
3218 gui_mch_enable_beval_area(balloonEval);
3219 else if (!p_beval && args->os_oldval.boolean)
3220 gui_mch_disable_beval_area(balloonEval);
3221
3222 return NULL;
3223}
3224#endif
3225
3226#if defined(FEAT_BEVAL_TERM) || defined(PROTO)
3227/*
3228 * Process the updated 'balloonevalterm' option value.
3229 */
3230 char *
3231did_set_balloonevalterm(optset_T *args UNUSED)
3232{
3233 mch_bevalterm_changed();
3234 return NULL;
3235}
3236#endif
3237
3238/*
3239 * Process the updated 'binary' option value.
3240 */
3241 char *
3242did_set_binary(optset_T *args)
3243{
3244 // when 'bin' is set also set some other options
3245 set_options_bin(args->os_oldval.boolean, curbuf->b_p_bin, args->os_flags);
3246 redraw_titles();
3247
3248 return NULL;
3249}
3250
3251#if defined(FEAT_LINEBREAK) || defined(PROTO)
3252/*
3253 * Called when the 'breakat' option changes value.
3254 */
3255 char *
3256did_set_breakat(optset_T *args UNUSED)
3257{
3258 char_u *p;
3259 int i;
3260
3261 for (i = 0; i < 256; i++)
3262 breakat_flags[i] = FALSE;
3263
3264 if (p_breakat != NULL)
3265 for (p = p_breakat; *p; p++)
3266 breakat_flags[*p] = TRUE;
3267
3268 return NULL;
3269}
3270#endif
3271
3272/*
3273 * Process the updated 'buflisted' option value.
3274 */
3275 char *
3276did_set_buflisted(optset_T *args)
3277{
3278 // when 'buflisted' changes, trigger autocommands
3279 if (args->os_oldval.boolean != curbuf->b_p_bl)
3280 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3281 NULL, NULL, TRUE, curbuf);
3282 return NULL;
3283}
3284
3285/*
3286 * Process the new 'cmdheight' option value.
3287 */
3288 char *
3289did_set_cmdheight(optset_T *args)
3290{
3291 long old_value = args->os_oldval.number;
3292 char *errmsg = NULL;
3293
3294 // if p_ch changed value, change the command line height
3295 if (p_ch < 1)
3296 {
3297 errmsg = e_argument_must_be_positive;
3298 p_ch = 1;
3299 }
3300 if (p_ch > Rows - min_rows() + 1)
3301 p_ch = Rows - min_rows() + 1;
3302
3303 // Only compute the new window layout when startup has been
3304 // completed. Otherwise the frame sizes may be wrong.
3305 if ((p_ch != old_value
3306 || tabline_height() + topframe->fr_height != Rows - p_ch)
3307 && full_screen
3308#ifdef FEAT_GUI
3309 && !gui.starting
3310#endif
3311 )
3312 command_height();
3313
3314 return errmsg;
3315}
3316
3317/*
3318 * Process the updated 'compatible' option value.
3319 */
3320 char *
3321did_set_compatible(optset_T *args UNUSED)
3322{
3323 compatible_set();
3324 return NULL;
3325}
3326
3327#if defined(FEAT_CONCEAL) || defined(PROTO)
3328/*
3329 * Process the new 'conceallevel' option value.
3330 */
3331 char *
3332did_set_conceallevel(optset_T *args UNUSED)
3333{
3334 char *errmsg = NULL;
3335
3336 if (curwin->w_p_cole < 0)
3337 {
3338 errmsg = e_argument_must_be_positive;
3339 curwin->w_p_cole = 0;
3340 }
3341 else if (curwin->w_p_cole > 3)
3342 {
3343 errmsg = e_invalid_argument;
3344 curwin->w_p_cole = 3;
3345 }
3346
3347 return errmsg;
3348}
3349#endif
3350
3351#if defined(FEAT_DIFF) || defined(PROTO)
3352/*
3353 * Process the updated 'diff' option value.
3354 */
3355 char *
3356did_set_diff(optset_T *args UNUSED)
3357{
3358 // May add or remove the buffer from the list of diff buffers.
3359 diff_buf_adjust(curwin);
3360# ifdef FEAT_FOLDING
3361 if (foldmethodIsDiff(curwin))
3362 foldUpdateAll(curwin);
3363# endif
3364 return NULL;
3365}
3366#endif
3367
3368/*
3369 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3370 * option value.
3371 */
3372 char *
3373did_set_eof_eol_fixeol_bomb(optset_T *args UNUSED)
3374{
3375 // redraw the window title and tab page text
3376 redraw_titles();
3377 return NULL;
3378}
3379
3380/*
3381 * Process the updated 'equalalways' option value.
3382 */
3383 char *
3384did_set_equalalways(optset_T *args)
3385{
3386 if (p_ea && !args->os_oldval.boolean)
3387 win_equal(curwin, FALSE, 0);
3388
3389 return NULL;
3390}
3391
3392#if defined(FEAT_FOLDING) || defined(PROTO)
3393/*
3394 * Process the new 'foldcolumn' option value.
3395 */
3396 char *
3397did_set_foldcolumn(optset_T *args UNUSED)
3398{
3399 char *errmsg = NULL;
3400
3401 if (curwin->w_p_fdc < 0)
3402 {
3403 errmsg = e_argument_must_be_positive;
3404 curwin->w_p_fdc = 0;
3405 }
3406 else if (curwin->w_p_fdc > 12)
3407 {
3408 errmsg = e_invalid_argument;
3409 curwin->w_p_fdc = 12;
3410 }
3411
3412 return errmsg;
3413}
3414
3415/*
3416 * Process the new 'foldlevel' option value.
3417 */
3418 char *
3419did_set_foldlevel(optset_T *args UNUSED)
3420{
3421 if (curwin->w_p_fdl < 0)
3422 curwin->w_p_fdl = 0;
3423 newFoldLevel();
3424 return NULL;
3425}
3426
3427/*
3428 * Process the new 'foldminlines' option value.
3429 */
3430 char *
3431did_set_foldminlines(optset_T *args UNUSED)
3432{
3433 foldUpdateAll(curwin);
3434 return NULL;
3435}
3436
3437/*
3438 * Process the new 'foldnestmax' option value.
3439 */
3440 char *
3441did_set_foldnestmax(optset_T *args UNUSED)
3442{
3443 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3444 foldUpdateAll(curwin);
3445 return NULL;
3446}
3447#endif
3448
3449#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
3450/*
3451 * Process the updated 'hlsearch' option value.
3452 */
3453 char *
3454did_set_hlsearch(optset_T *args UNUSED)
3455{
3456 // when 'hlsearch' is set or reset: reset no_hlsearch
3457 set_no_hlsearch(FALSE);
3458 return NULL;
3459}
3460#endif
3461
3462/*
3463 * Process the updated 'ignorecase' option value.
3464 */
3465 char *
3466did_set_ignorecase(optset_T *args UNUSED)
3467{
3468 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3469 if (p_hls)
3470 redraw_all_later(UPD_SOME_VALID);
3471 return NULL;
3472}
3473
3474#if defined(HAVE_INPUT_METHOD) || defined(PROTO)
3475/*
3476 * Process the updated 'imdisable' option value.
3477 */
3478 char *
3479did_set_imdisable(optset_T *args UNUSED)
3480{
3481 // Only de-activate it here, it will be enabled when changing mode.
3482 if (p_imdisable)
3483 im_set_active(FALSE);
3484 else if (State & MODE_INSERT)
3485 // When the option is set from an autocommand, it may need to take
3486 // effect right away.
3487 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3488 return NULL;
3489}
3490#endif
3491
3492/*
3493 * Process the new 'iminsert' option value.
3494 */
3495 char *
3496did_set_iminsert(optset_T *args UNUSED)
3497{
3498 char *errmsg = NULL;
3499
3500 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3501 {
3502 errmsg = e_invalid_argument;
3503 curbuf->b_p_iminsert = B_IMODE_NONE;
3504 }
3505 p_iminsert = curbuf->b_p_iminsert;
3506 if (termcap_active) // don't do this in the alternate screen
3507 showmode();
3508#if defined(FEAT_KEYMAP)
3509 // Show/unshow value of 'keymap' in status lines.
3510 status_redraw_curbuf();
3511#endif
3512
3513 return errmsg;
3514}
3515
3516/*
3517 * Process the new 'imsearch' option value.
3518 */
3519 char *
3520did_set_imsearch(optset_T *args UNUSED)
3521{
3522 char *errmsg = NULL;
3523
3524 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3525 {
3526 errmsg = e_invalid_argument;
3527 curbuf->b_p_imsearch = B_IMODE_NONE;
3528 }
3529 p_imsearch = curbuf->b_p_imsearch;
3530
3531 return errmsg;
3532}
3533
3534#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3535/*
3536 * Process the new 'imstyle' option value.
3537 */
3538 char *
3539did_set_imstyle(optset_T *args UNUSED)
3540{
3541 char *errmsg = NULL;
3542
3543 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3544 errmsg = e_invalid_argument;
3545
3546 return errmsg;
3547}
3548#endif
3549
3550/*
3551 * Process the updated 'insertmode' option value.
3552 */
3553 char *
3554did_set_insertmode(optset_T *args)
3555{
3556 // when 'insertmode' is set from an autocommand need to do work here
3557 if (p_im)
3558 {
3559 if ((State & MODE_INSERT) == 0)
3560 need_start_insertmode = TRUE;
3561 stop_insert_mode = FALSE;
3562 }
3563 // only reset if it was set previously
3564 else if (args->os_oldval.boolean)
3565 {
3566 need_start_insertmode = FALSE;
3567 stop_insert_mode = TRUE;
3568 if (restart_edit != 0 && mode_displayed)
3569 clear_cmdline = TRUE; // remove "(insert)"
3570 restart_edit = 0;
3571 }
3572
3573 return NULL;
3574}
3575
3576#if defined(FEAT_LANGMAP) || defined(PROTO)
3577/*
3578 * Process the updated 'langnoremap' option value.
3579 */
3580 char *
3581did_set_langnoremap(optset_T *args UNUSED)
3582{
3583 // 'langnoremap' -> !'langremap'
3584 p_lrm = !p_lnr;
3585 return NULL;
3586}
3587
3588/*
3589 * Process the updated 'langremap' option value.
3590 */
3591 char *
3592did_set_langremap(optset_T *args UNUSED)
3593{
3594 // 'langremap' -> !'langnoremap'
3595 p_lnr = !p_lrm;
3596 return NULL;
3597}
3598#endif
3599
3600/*
3601 * Process the new 'laststatus' option value.
3602 */
3603 char *
3604did_set_laststatus(optset_T *args UNUSED)
3605{
3606 last_status(FALSE); // (re)set last window status line
3607 return NULL;
3608}
3609
3610#if defined(FEAT_GUI) || defined(PROTO)
3611/*
3612 * Process the new 'linespace' option value.
3613 */
3614 char *
3615did_set_linespace(optset_T *args UNUSED)
3616{
3617 // Recompute gui.char_height and resize the Vim window to keep the
3618 // same number of lines.
3619 if (gui.in_use && gui_mch_adjust_charheight() == OK)
3620 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
3621 return NULL;
3622}
3623#endif
3624
3625/*
3626 * Process the updated 'lisp' option value.
3627 */
3628 char *
3629did_set_lisp(optset_T *args UNUSED)
3630{
3631 // When 'lisp' option changes include/exclude '-' in keyword characters.
3632 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3633 return NULL;
3634}
3635
3636/*
3637 * Process the new 'maxcombine' option value.
3638 */
3639 char *
3640did_set_maxcombine(optset_T *args UNUSED)
3641{
3642 if (p_mco > MAX_MCO)
3643 p_mco = MAX_MCO;
3644 else if (p_mco < 0)
3645 p_mco = 0;
3646 screenclear(); // will re-allocate the screen
3647 return NULL;
3648}
3649
3650/*
3651 * Process the updated 'modifiable' option value.
3652 */
3653 char *
3654did_set_modifiable(optset_T *args UNUSED)
3655{
3656 // when 'modifiable' is changed, redraw the window title
3657
3658# ifdef FEAT_TERMINAL
3659 // Cannot set 'modifiable' when in Terminal mode.
3660 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3661 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3662 {
3663 curbuf->b_p_ma = FALSE;
3664 args->os_doskip = TRUE;
3665 return e_cannot_make_terminal_with_running_job_modifiable;
3666 }
3667# endif
3668 redraw_titles();
3669
3670 return NULL;
3671}
3672
3673/*
3674 * Process the updated 'modified' option value.
3675 */
3676 char *
3677did_set_modified(optset_T *args)
3678{
3679 if (!args->os_newval.boolean)
3680 save_file_ff(curbuf); // Buffer is unchanged
3681 redraw_titles();
3682 modified_was_set = args->os_newval.boolean;
3683 return NULL;
3684}
3685
3686#if defined(FEAT_GUI) || defined(PROTO)
3687/*
3688 * Process the updated 'mousehide' option value.
3689 */
3690 char *
3691did_set_mousehide(optset_T *args UNUSED)
3692{
3693 if (!p_mh)
3694 gui_mch_mousehide(FALSE);
3695 return NULL;
3696}
3697#endif
3698
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003699/*
3700 * Process the updated 'number' or 'relativenumber' option value.
3701 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003702 char *
3703did_set_number_relativenumber(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003704{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003705#if (defined(FEAT_SIGNS) && defined(FEAT_GUI)) || defined(PROTO)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003706 if (gui.in_use
3707 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3708 && curbuf->b_signlist != NULL)
3709 {
3710 // If the 'number' or 'relativenumber' options are modified and
3711 // 'signcolumn' is set to 'number', then clear the screen for a full
3712 // refresh. Otherwise the sign icons are not displayed properly in the
3713 // number column. If the 'number' option is set and only the
3714 // 'relativenumber' option is toggled, then don't refresh the screen
3715 // (optimization).
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003716 if (!(curwin->w_p_nu && ((int *)args->os_varp == &curwin->w_p_rnu)))
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003717 redraw_all_later(UPD_CLEAR);
3718 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003719#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003720 return NULL;
3721}
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003722
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003723#if defined(FEAT_LINEBREAK) || defined(PROTO)
3724/*
3725 * Process the new 'numberwidth' option value.
3726 */
3727 char *
3728did_set_numberwidth(optset_T *args UNUSED)
3729{
3730 char *errmsg = NULL;
3731
3732 // 'numberwidth' must be positive
3733 if (curwin->w_p_nuw < 1)
3734 {
3735 errmsg = e_argument_must_be_positive;
3736 curwin->w_p_nuw = 1;
3737 }
3738 if (curwin->w_p_nuw > 20)
3739 {
3740 errmsg = e_invalid_argument;
3741 curwin->w_p_nuw = 20;
3742 }
3743 curwin->w_nrwidth_line_count = 0; // trigger a redraw
3744
3745 return errmsg;
3746}
3747#endif
3748
3749/*
3750 * Process the updated 'paste' option value. Called after p_paste was set or
3751 * reset. When 'paste' is set or reset also change other options.
3752 */
3753 char *
3754did_set_paste(optset_T *args UNUSED)
3755{
3756 static int old_p_paste = FALSE;
3757 static int save_sm = 0;
3758 static int save_sta = 0;
3759 static int save_ru = 0;
3760#ifdef FEAT_RIGHTLEFT
3761 static int save_ri = 0;
3762 static int save_hkmap = 0;
3763#endif
3764 buf_T *buf;
3765
3766 if (p_paste)
3767 {
3768 // Paste switched from off to on.
3769 // Save the current values, so they can be restored later.
3770 if (!old_p_paste)
3771 {
3772 // save options for each buffer
3773 FOR_ALL_BUFFERS(buf)
3774 {
3775 buf->b_p_tw_nopaste = buf->b_p_tw;
3776 buf->b_p_wm_nopaste = buf->b_p_wm;
3777 buf->b_p_sts_nopaste = buf->b_p_sts;
3778 buf->b_p_ai_nopaste = buf->b_p_ai;
3779 buf->b_p_et_nopaste = buf->b_p_et;
3780#ifdef FEAT_VARTABS
3781 if (buf->b_p_vsts_nopaste)
3782 vim_free(buf->b_p_vsts_nopaste);
3783 buf->b_p_vsts_nopaste =
3784 buf->b_p_vsts && buf->b_p_vsts != empty_option
3785 ? vim_strsave(buf->b_p_vsts) : NULL;
3786#endif
3787 }
3788
3789 // save global options
3790 save_sm = p_sm;
3791 save_sta = p_sta;
3792 save_ru = p_ru;
3793#ifdef FEAT_RIGHTLEFT
3794 save_ri = p_ri;
3795 save_hkmap = p_hkmap;
3796#endif
3797 // save global values for local buffer options
3798 p_ai_nopaste = p_ai;
3799 p_et_nopaste = p_et;
3800 p_sts_nopaste = p_sts;
3801 p_tw_nopaste = p_tw;
3802 p_wm_nopaste = p_wm;
3803#ifdef FEAT_VARTABS
3804 if (p_vsts_nopaste)
3805 vim_free(p_vsts_nopaste);
3806 p_vsts_nopaste = p_vsts && p_vsts != empty_option
3807 ? vim_strsave(p_vsts) : NULL;
3808#endif
3809 }
3810
3811 // Always set the option values, also when 'paste' is set when it is
3812 // already on. Set options for each buffer.
3813 FOR_ALL_BUFFERS(buf)
3814 {
3815 buf->b_p_tw = 0; // textwidth is 0
3816 buf->b_p_wm = 0; // wrapmargin is 0
3817 buf->b_p_sts = 0; // softtabstop is 0
3818 buf->b_p_ai = 0; // no auto-indent
3819 buf->b_p_et = 0; // no expandtab
3820#ifdef FEAT_VARTABS
3821 if (buf->b_p_vsts)
3822 free_string_option(buf->b_p_vsts);
3823 buf->b_p_vsts = empty_option;
3824 VIM_CLEAR(buf->b_p_vsts_array);
3825#endif
3826 }
3827
3828 // set global options
3829 p_sm = 0; // no showmatch
3830 p_sta = 0; // no smarttab
3831 if (p_ru)
3832 status_redraw_all(); // redraw to remove the ruler
3833 p_ru = 0; // no ruler
3834#ifdef FEAT_RIGHTLEFT
3835 p_ri = 0; // no reverse insert
3836 p_hkmap = 0; // no Hebrew keyboard
3837#endif
3838 // set global values for local buffer options
3839 p_tw = 0;
3840 p_wm = 0;
3841 p_sts = 0;
3842 p_ai = 0;
3843#ifdef FEAT_VARTABS
3844 if (p_vsts)
3845 free_string_option(p_vsts);
3846 p_vsts = empty_option;
3847#endif
3848 }
3849
3850 // Paste switched from on to off: Restore saved values.
3851 else if (old_p_paste)
3852 {
3853 // restore options for each buffer
3854 FOR_ALL_BUFFERS(buf)
3855 {
3856 buf->b_p_tw = buf->b_p_tw_nopaste;
3857 buf->b_p_wm = buf->b_p_wm_nopaste;
3858 buf->b_p_sts = buf->b_p_sts_nopaste;
3859 buf->b_p_ai = buf->b_p_ai_nopaste;
3860 buf->b_p_et = buf->b_p_et_nopaste;
3861#ifdef FEAT_VARTABS
3862 if (buf->b_p_vsts)
3863 free_string_option(buf->b_p_vsts);
3864 buf->b_p_vsts = buf->b_p_vsts_nopaste
3865 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
3866 vim_free(buf->b_p_vsts_array);
3867 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
3868 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
3869 else
3870 buf->b_p_vsts_array = NULL;
3871#endif
3872 }
3873
3874 // restore global options
3875 p_sm = save_sm;
3876 p_sta = save_sta;
3877 if (p_ru != save_ru)
3878 status_redraw_all(); // redraw to draw the ruler
3879 p_ru = save_ru;
3880#ifdef FEAT_RIGHTLEFT
3881 p_ri = save_ri;
3882 p_hkmap = save_hkmap;
3883#endif
3884 // set global values for local buffer options
3885 p_ai = p_ai_nopaste;
3886 p_et = p_et_nopaste;
3887 p_sts = p_sts_nopaste;
3888 p_tw = p_tw_nopaste;
3889 p_wm = p_wm_nopaste;
3890#ifdef FEAT_VARTABS
3891 if (p_vsts)
3892 free_string_option(p_vsts);
3893 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
3894#endif
3895 }
3896
3897 old_p_paste = p_paste;
3898
3899 return NULL;
3900}
3901
3902
3903#ifdef FEAT_QUICKFIX
3904/*
3905 * Process the updated 'previewwindow' option value.
3906 */
3907 char *
3908did_set_previewwindow(optset_T *args)
3909{
3910 if (!curwin->w_p_pvw)
3911 return NULL;
3912
3913 // There can be only one window with 'previewwindow' set.
3914 win_T *win;
3915
3916 FOR_ALL_WINDOWS(win)
3917 if (win->w_p_pvw && win != curwin)
3918 {
3919 curwin->w_p_pvw = FALSE;
3920 args->os_doskip = TRUE;
3921 return e_preview_window_already_exists;
3922 }
3923
3924 return NULL;
3925}
3926#endif
3927
3928#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
3929/*
3930 * Process the new 'pyxversion' option value.
3931 */
3932 char *
3933did_set_pyxversion(optset_T *args UNUSED)
3934{
3935 char *errmsg = NULL;
3936
3937 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3938 errmsg = e_invalid_argument;
3939
3940 return errmsg;
3941}
3942#endif
3943
3944/*
3945 * Process the updated 'readonly' option value.
3946 */
3947 char *
3948did_set_readonly(optset_T *args)
3949{
3950 // when 'readonly' is reset globally, also reset readonlymode
3951 if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0)
3952 readonlymode = FALSE;
3953
3954 // when 'readonly' is set may give W10 again
3955 if (curbuf->b_p_ro)
3956 curbuf->b_did_warn = FALSE;
3957
3958 redraw_titles();
3959
3960 return NULL;
3961}
3962
3963/*
3964 * Process the updated 'scrollbind' option value.
3965 */
3966 char *
3967did_set_scrollbind(optset_T *args UNUSED)
3968{
3969 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3970 // at the end of normal_cmd()
3971 if (!curwin->w_p_scb)
3972 return NULL;
3973
3974 do_check_scrollbind(FALSE);
3975 curwin->w_scbind_pos = curwin->w_topline;
3976 return NULL;
3977}
3978
3979#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3980/*
3981 * Process the updated 'shellslash' option value.
3982 */
3983 char *
3984did_set_shellslash(optset_T *args UNUSED)
3985{
3986 if (p_ssl)
3987 {
3988 psepc = '/';
3989 psepcN = '\\';
3990 pseps[0] = '/';
3991 }
3992 else
3993 {
3994 psepc = '\\';
3995 psepcN = '/';
3996 pseps[0] = '\\';
3997 }
3998
3999 // need to adjust the file name arguments and buffer names.
4000 buflist_slash_adjust();
4001 alist_slash_adjust();
4002# ifdef FEAT_EVAL
4003 scriptnames_slash_adjust();
4004# endif
4005 return NULL;
4006}
4007#endif
4008
4009/*
4010 * Process the new 'shiftwidth' or the 'tabstop' option value.
4011 */
4012 char *
4013did_set_shiftwidth_tabstop(optset_T *args)
4014{
4015 long *pp = (long *)args->os_varp;
4016 char *errmsg = NULL;
4017
4018 if (curbuf->b_p_sw < 0)
4019 {
4020 errmsg = e_argument_must_be_positive;
4021#ifdef FEAT_VARTABS
4022 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4023 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4024 ? tabstop_first(curbuf->b_p_vts_array)
4025 : curbuf->b_p_ts;
4026#else
4027 curbuf->b_p_sw = curbuf->b_p_ts;
4028#endif
4029 }
4030
4031#ifdef FEAT_FOLDING
4032 if (foldmethodIsIndent(curwin))
4033 foldUpdateAll(curwin);
4034#endif
4035 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4036 // parse 'cinoptions'.
4037 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4038 parse_cino(curbuf);
4039
4040 return errmsg;
4041}
4042
4043/*
4044 * Process the new 'showtabline' option value.
4045 */
4046 char *
4047did_set_showtabline(optset_T *args UNUSED)
4048{
4049 shell_new_rows(); // recompute window positions and heights
4050 return NULL;
4051}
4052
4053/*
4054 * Process the updated 'smoothscroll' option value.
4055 */
4056 char *
4057did_set_smoothscroll(optset_T *args UNUSED)
4058{
4059 if (curwin->w_p_sms)
4060 return NULL;
4061
4062 curwin->w_skipcol = 0;
4063 changed_line_abv_curs();
4064 return NULL;
4065}
4066
4067#if defined(FEAT_SPELL) || defined(PROTO)
4068/*
4069 * Process the updated 'spell' option value.
4070 */
4071 char *
4072did_set_spell(optset_T *args UNUSED)
4073{
4074 if (curwin->w_p_spell)
4075 return parse_spelllang(curwin);
4076
4077 return NULL;
4078}
4079#endif
4080
4081/*
4082 * Process the updated 'swapfile' option value.
4083 */
4084 char *
4085did_set_swapfile(optset_T *args UNUSED)
4086{
4087 // when 'swf' is set, create swapfile, when reset remove swapfile
4088 if (curbuf->b_p_swf && p_uc)
4089 ml_open_file(curbuf); // create the swap file
4090 else
4091 // no need to reset curbuf->b_may_swap, ml_open_file() will check
4092 // buf->b_p_swf
4093 mf_close_file(curbuf, TRUE); // remove the swap file
4094 return NULL;
4095}
4096
4097#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004098 char *
4099did_set_termguicolors(optset_T *args UNUSED)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004100{
4101# ifdef FEAT_VTP
4102 // Do not turn on 'tgc' when 24-bit colors are not supported.
4103 if (
4104# ifdef VIMDLL
4105 !gui.in_use && !gui.starting &&
4106# endif
4107 !has_vtp_working())
4108 {
4109 p_tgc = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004110 args->os_doskip = TRUE;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00004111 return e_24_bit_colors_are_not_supported_on_this_environment;
4112 }
4113 if (is_term_win32())
4114 swap_tcap();
4115# endif
4116# ifdef FEAT_GUI
4117 if (!gui.in_use && !gui.starting)
4118# endif
4119 highlight_gui_started();
4120# ifdef FEAT_VTP
4121 // reset t_Co
4122 if (is_term_win32())
4123 {
4124 control_console_color_rgb();
4125 set_termname(T_NAME);
4126 init_highlight(TRUE, FALSE);
4127 }
4128# endif
4129# ifdef FEAT_TERMINAL
4130 term_update_colors_all();
4131 term_update_palette_all();
4132 term_update_wincolor_all();
4133# endif
4134
4135 return NULL;
4136}
4137#endif
4138
4139/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004140 * Process the updated 'terse' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004142 char *
4143did_set_terse(optset_T *args UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004145 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004147 // when 'terse' is set change 'shortmess'
4148 p = vim_strchr(p_shm, SHM_SEARCH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004150 // insert 's' in p_shm
4151 if (p_terse && p == NULL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004152 {
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004153 STRCPY(IObuff, p_shm);
4154 STRCAT(IObuff, "s");
4155 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004156 }
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004157 // remove 's' from p_shm
4158 else if (!p_terse && p != NULL)
4159 STRMOVE(p, p + 1);
4160 return NULL;
4161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004163/*
4164 * Process the updated 'textauto' option value.
4165 */
4166 char *
4167did_set_textauto(optset_T *args)
4168{
4169 // when 'textauto' is set or reset also change 'fileformats'
4170 set_string_option_direct((char_u *)"ffs", -1,
4171 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
4172 OPT_FREE | args->os_flags, 0);
Bram Moolenaar53744302015-07-17 17:38:22 +02004173
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004174 return NULL;
4175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004177/*
4178 * Process the updated 'textmode' option value.
4179 */
4180 char *
4181did_set_textmode(optset_T *args)
4182{
4183 // when 'textmode' is set or reset also change 'fileformat'
4184 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, args->os_flags);
4185
4186 return NULL;
4187}
4188
4189/*
4190 * Process the new 'textwidth' option value.
4191 */
4192 char *
4193did_set_textwidth(optset_T *args UNUSED)
4194{
4195 char *errmsg = NULL;
4196
4197 if (curbuf->b_p_tw < 0)
4198 {
4199 errmsg = e_argument_must_be_positive;
4200 curbuf->b_p_tw = 0;
4201 }
4202#ifdef FEAT_SYN_HL
4203 {
4204 win_T *wp;
4205 tabpage_T *tp;
4206
4207 FOR_ALL_TAB_WINDOWS(tp, wp)
4208 check_colorcolumn(wp);
4209 }
Bram Moolenaar53744302015-07-17 17:38:22 +02004210#endif
4211
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004212 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213}
4214
4215/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004216 * Process the updated 'title' or the 'icon' option value.
4217 */
4218 char *
4219did_set_title_icon(optset_T *args UNUSED)
4220{
4221 // when 'title' changed, may need to change the title; same for 'icon'
4222 did_set_title();
4223 return NULL;
4224}
4225
4226/*
4227 * Process the new 'titlelen' option value.
4228 */
4229 char *
4230did_set_titlelen(optset_T *args)
4231{
4232 long old_value = args->os_oldval.number;
4233 char *errmsg = NULL;
4234
4235 // if 'titlelen' has changed, redraw the title
4236 if (p_titlelen < 0)
4237 {
4238 errmsg = e_argument_must_be_positive;
4239 p_titlelen = 85;
4240 }
4241 if (starting != NO_SCREEN && old_value != p_titlelen)
4242 need_maketitle = TRUE;
4243
4244 return errmsg;
4245}
4246
4247#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
4248/*
4249 * Process the updated 'undofile' option value.
4250 */
4251 char *
4252did_set_undofile(optset_T *args)
4253{
4254 // Only take action when the option was set.
4255 if (!curbuf->b_p_udf && !p_udf)
4256 return NULL;
4257
4258 // When reset we do not delete the undo file, the option may be set again
4259 // without making any changes in between.
4260 char_u hash[UNDO_HASH_SIZE];
4261 buf_T *save_curbuf = curbuf;
4262
4263 FOR_ALL_BUFFERS(curbuf)
4264 {
4265 // When 'undofile' is set globally: for every buffer, otherwise
4266 // only for the current buffer: Try to read in the undofile,
4267 // if one exists, the buffer wasn't changed and the buffer was
4268 // loaded
4269 if ((curbuf == save_curbuf
4270 || (args->os_flags & OPT_GLOBAL)
4271 || args->os_flags == 0)
4272 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
4273 {
4274#ifdef FEAT_CRYPT
4275 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
4276 continue;
4277#endif
4278 u_compute_hash(hash);
4279 u_read_undo(NULL, hash, curbuf->b_fname);
4280 }
4281 }
4282 curbuf = save_curbuf;
4283
4284 return NULL;
4285}
4286#endif
4287
4288/*
4289 * Process the new global 'undolevels' option value.
4290 */
4291 static void
4292update_global_undolevels(long value, long old_value)
4293{
4294 // sync undo before 'undolevels' changes
4295
4296 // use the old value, otherwise u_sync() may not work properly
4297 p_ul = old_value;
4298 u_sync(TRUE);
4299 p_ul = value;
4300}
4301
4302/*
4303 * Process the new buffer local 'undolevels' option value.
4304 */
4305 static void
4306update_buflocal_undolevels(long value, long old_value)
4307{
4308 // use the old value, otherwise u_sync() may not work properly
4309 curbuf->b_p_ul = old_value;
4310 u_sync(TRUE);
4311 curbuf->b_p_ul = value;
4312}
4313
4314/*
4315 * Process the new 'undolevels' option value.
4316 */
4317 char *
4318did_set_undolevels(optset_T *args)
4319{
4320 long *pp = (long *)args->os_varp;
4321
4322 if (pp == &p_ul) // global 'undolevels'
4323 update_global_undolevels(args->os_newval.number,
4324 args->os_oldval.number);
4325 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4326 update_buflocal_undolevels(args->os_newval.number,
4327 args->os_oldval.number);
4328
4329 return NULL;
4330}
4331
4332/*
4333 * Process the new 'updatecount' option value.
4334 */
4335 char *
4336did_set_updatecount(optset_T *args)
4337{
4338 long old_value = args->os_oldval.number;
4339 char *errmsg = NULL;
4340
4341 // when 'updatecount' changes from zero to non-zero, open swap files
4342 if (p_uc < 0)
4343 {
4344 errmsg = e_argument_must_be_positive;
4345 p_uc = 100;
4346 }
4347 if (p_uc && !old_value)
4348 ml_open_files();
4349
4350 return errmsg;
4351}
4352
4353/*
4354 * Process the updated 'weirdinvert' option value.
4355 */
4356 char *
4357did_set_weirdinvert(optset_T *args)
4358{
4359 // When 'weirdinvert' changed, set/reset 't_xs'.
4360 // Then set 'weirdinvert' according to value of 't_xs'.
4361 if (p_wiv && !args->os_oldval.boolean)
4362 T_XS = (char_u *)"y";
4363 else if (!p_wiv && args->os_oldval.boolean)
4364 T_XS = empty_option;
4365 p_wiv = (*T_XS != NUL);
4366
4367 return NULL;
4368}
4369
4370/*
4371 * Process the new 'window' option value.
4372 */
4373 char *
4374did_set_window(optset_T *args UNUSED)
4375{
4376 if (p_window < 1)
4377 p_window = 1;
4378 else if (p_window >= Rows)
4379 p_window = Rows - 1;
4380 return NULL;
4381}
4382
4383/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004384 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004386 char *
4387did_set_winheight_helpheight(optset_T *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004389 long *pp = (long *)args->os_varp;
4390 char *errmsg = NULL;
4391
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004392 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004394 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004395 p_wh = 1;
4396 }
4397 if (p_wmh > p_wh)
4398 {
4399 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4400 p_wh = p_wmh;
4401 }
4402 if (p_hh < 0)
4403 {
4404 errmsg = e_argument_must_be_positive;
4405 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004408 // Change window height NOW
4409 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004411 if (pp == &p_wh && curwin->w_height < p_wh)
4412 win_setheight((int)p_wh);
4413 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4414 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004417 return errmsg;
4418}
4419
4420/*
4421 * Process the new 'winminheight' option value.
4422 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004423 char *
4424did_set_winminheight(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004425{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004426 char *errmsg = NULL;
4427
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004428 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004430 errmsg = e_argument_must_be_positive;
4431 p_wmh = 0;
4432 }
4433 if (p_wmh > p_wh)
4434 {
4435 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4436 p_wmh = p_wh;
4437 }
4438 win_setminheight();
4439
4440 return errmsg;
4441}
4442
4443/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004444 * Process the new 'winminwidth' option value.
4445 */
4446 char *
4447did_set_winminwidth(optset_T *args UNUSED)
4448{
4449 char *errmsg = NULL;
4450
4451 if (p_wmw < 0)
4452 {
4453 errmsg = e_argument_must_be_positive;
4454 p_wmw = 0;
4455 }
4456 if (p_wmw > p_wiw)
4457 {
4458 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4459 p_wmw = p_wiw;
4460 }
4461 win_setminwidth();
4462
4463 return errmsg;
4464}
4465
4466/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004467 * Process the new 'winwidth' option value.
4468 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004469 char *
4470did_set_winwidth(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004471{
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004472 char *errmsg = NULL;
4473
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004474 if (p_wiw < 1)
4475 {
4476 errmsg = e_argument_must_be_positive;
4477 p_wiw = 1;
4478 }
4479 if (p_wmw > p_wiw)
4480 {
4481 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4482 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004484
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004485 // Change window width NOW
4486 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4487 win_setwidth((int)p_wiw);
4488
4489 return errmsg;
4490}
4491
4492/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004493 * Process the updated 'wrap' option value.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004494 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004495 char *
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004496did_set_wrap(optset_T *args UNUSED)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004497{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004498 // If 'wrap' is set, set w_leftcol to zero.
4499 if (curwin->w_p_wrap)
4500 curwin->w_leftcol = 0;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004501 return NULL;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004502}
4503
4504/*
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004505 * Set the value of a boolean option, and take care of side effects.
4506 * Returns NULL for success, or an error message for an error.
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004507 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004508 static char *
4509set_bool_option(
4510 int opt_idx, // index in options[] table
4511 char_u *varp, // pointer to the option variable
4512 int value, // new value
4513 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004514{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004515 int old_value = *(int *)varp;
4516#if defined(FEAT_EVAL)
4517 int old_global_value = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004519 char *errmsg = NULL;
4520
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004521 // Disallow changing some options from secure mode
4522 if ((secure
4523#ifdef HAVE_SANDBOX
4524 || sandbox != 0
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004525#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004526 ) && (options[opt_idx].flags & P_SECURE))
4527 return e_not_allowed_here;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004528
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004529#if defined(FEAT_EVAL)
4530 // Save the global value before changing anything. This is needed as for
4531 // a global-only option setting the "local value" in fact sets the global
4532 // value (since there is only one value).
4533 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4534 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
4535 OPT_GLOBAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004537
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004538 *(int *)varp = value; // set the new value
4539#ifdef FEAT_EVAL
4540 // Remember where the option was set.
4541 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004542#endif
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004545 need_mouse_correct = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004548 // May set global value for local option.
4549 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4550 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004551
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004552 // Handle side effects of changing a bool option.
4553 if (options[opt_idx].opt_did_set_cb != NULL)
4554 {
4555 optset_T args;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004556
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004557 CLEAR_FIELD(args);
4558 args.os_varp = varp;
4559 args.os_flags = opt_flags;
4560 args.os_oldval.boolean = old_value;
4561 args.os_newval.boolean = value;
4562 args.os_errbuf = NULL;
4563 errmsg = options[opt_idx].opt_did_set_cb(&args);
4564 if (args.os_doskip)
4565 return errmsg;
4566 }
4567
4568 // after handling side effects, call autocommand
4569
4570 options[opt_idx].flags |= P_WAS_SET;
4571
4572#if defined(FEAT_EVAL)
4573 apply_optionset_autocmd(opt_idx, opt_flags,
4574 (long)(old_value ? TRUE : FALSE),
4575 (long)(old_global_value ? TRUE : FALSE),
4576 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004577#endif
4578
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004579 comp_col(); // in case 'ruler' or 'showcmd' changed
4580 if (curwin->w_curswant != MAXCOL
4581 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
4582 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004583
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004584 if ((opt_flags & OPT_NO_REDRAW) == 0)
4585 check_redraw(options[opt_idx].flags);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004586
4587 return errmsg;
4588}
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004589
4590/*
4591 * Check the bounds of numeric options.
4592 */
4593 static char *
4594check_num_option_bounds(
4595 long *pp,
4596 long old_value,
4597 long old_Rows,
4598 long old_Columns,
4599 char *errbuf,
4600 size_t errbuflen,
4601 char *errmsg)
4602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (Rows < min_rows() && full_screen)
4604 {
4605 if (errbuf != NULL)
4606 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004607 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004608 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 errmsg = errbuf;
4610 }
4611 Rows = min_rows();
4612 }
4613 if (Columns < MIN_COLUMNS && full_screen)
4614 {
4615 if (errbuf != NULL)
4616 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004617 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004618 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 errmsg = errbuf;
4620 }
4621 Columns = MIN_COLUMNS;
4622 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004623 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004625 // If the screen (shell) height has been changed, assume it is the
4626 // physical screenheight.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (old_Rows != Rows || old_Columns != Columns)
4628 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004629 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (updating_screen)
4631 *pp = old_value;
4632 else if (full_screen
4633#ifdef FEAT_GUI
4634 && !gui.starting
4635#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004636 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 set_shellsize((int)Columns, (int)Rows, TRUE);
4638 else
4639 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004640 // Postpone the resizing; check the size and cmdline position for
4641 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 check_shellsize();
4643 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4644 cmdline_row = Rows - p_ch;
4645 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004646 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004647 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (curbuf->b_p_ts <= 0)
4651 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004652 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 curbuf->b_p_ts = 8;
4654 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004655 else if (curbuf->b_p_ts > TABSTOP_MAX)
4656 {
4657 errmsg = e_invalid_argument;
4658 curbuf->b_p_ts = 8;
4659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (p_tm < 0)
4661 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004662 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 p_tm = 0;
4664 }
4665 if ((curwin->w_p_scr <= 0
4666 || (curwin->w_p_scr > curwin->w_height
4667 && curwin->w_height > 0))
4668 && full_screen)
4669 {
4670 if (pp == &(curwin->w_p_scr))
4671 {
4672 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004673 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 win_comp_scroll(curwin);
4675 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004676 // If 'scroll' became invalid because of a side effect silently adjust
4677 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else if (curwin->w_p_scr <= 0)
4679 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004680 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 curwin->w_p_scr = curwin->w_height;
4682 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004683 if (p_hi < 0)
4684 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004685 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004686 p_hi = 0;
4687 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004688 else if (p_hi > 10000)
4689 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004690 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004691 p_hi = 10000;
4692 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004693 if (p_re < 0 || p_re > 2)
4694 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004695 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004696 p_re = 0;
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (p_report < 0)
4699 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004700 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 p_report = 1;
4702 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004703 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004705 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 p_sj = Rows / 2;
4707 else
4708 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004709 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 p_sj = 1;
4711 }
4712 }
4713 if (p_so < 0 && full_screen)
4714 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004715 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 p_so = 0;
4717 }
4718 if (p_siso < 0 && full_screen)
4719 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004720 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 p_siso = 0;
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (p_cwh < 1)
4724 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004725 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 p_cwh = 1;
4727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (p_ut < 0)
4729 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004730 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 p_ut = 2000;
4732 }
4733 if (p_ss < 0)
4734 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004735 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 p_ss = 0;
4737 }
4738
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004739 return errmsg;
4740}
4741
4742/*
4743 * Set the value of a number option, and take care of side effects.
4744 * Returns NULL for success, or an error message for an error.
4745 */
4746 static char *
4747set_num_option(
4748 int opt_idx, // index in options[] table
4749 char_u *varp, // pointer to the option variable
4750 long value, // new value
4751 char *errbuf, // buffer for error messages
4752 size_t errbuflen, // length of "errbuf"
4753 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4754 // OPT_MODELINE, etc.
4755{
4756 char *errmsg = NULL;
4757 long old_value = *(long *)varp;
4758#if defined(FEAT_EVAL)
4759 long old_global_value = 0; // only used when setting a local and
4760 // global option
4761#endif
4762 long old_Rows = Rows; // remember old Rows
4763 long old_Columns = Columns; // remember old Columns
4764 long *pp = (long *)varp;
4765
4766 // Disallow changing some options from secure mode.
4767 if ((secure
4768#ifdef HAVE_SANDBOX
4769 || sandbox != 0
4770#endif
4771 ) && (options[opt_idx].flags & P_SECURE))
4772 return e_not_allowed_here;
4773
4774#if defined(FEAT_EVAL)
4775 // Save the global value before changing anything. This is needed as for
4776 // a global-only option setting the "local value" in fact sets the global
4777 // value (since there is only one value).
4778 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4779 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4780 OPT_GLOBAL);
4781#endif
4782
4783 *pp = value;
4784#ifdef FEAT_EVAL
4785 // Remember where the option was set.
4786 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4787#endif
4788#ifdef FEAT_GUI
4789 need_mouse_correct = TRUE;
4790#endif
4791
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004792 // Invoke the option specific callback function to validate and apply the
4793 // new value.
4794 if (options[opt_idx].opt_did_set_cb != NULL)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004795 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004796 optset_T args;
4797
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004798 CLEAR_FIELD(args);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004799 args.os_varp = varp;
4800 args.os_flags = opt_flags;
4801 args.os_oldval.number = old_value;
4802 args.os_newval.number = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004803 args.os_errbuf = NULL;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004804 errmsg = options[opt_idx].opt_did_set_cb(&args);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004805 }
4806
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004807 // Check the bounds for numeric options here
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004808 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4809 errbuf, errbuflen, errmsg);
4810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004811 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4813 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4814
4815 options[opt_idx].flags |= P_WAS_SET;
4816
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004817#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004818 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4819 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004820#endif
4821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004822 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004823 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004824 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004825 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004826 if ((opt_flags & OPT_NO_REDRAW) == 0)
4827 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
4829 return errmsg;
4830}
4831
4832/*
4833 * Called after an option changed: check if something needs to be redrawn.
4834 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004836check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004838 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004839 int doclear = (flags & P_RCLR) == P_RCLR;
4840 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004842 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4846 changed_window_setting();
4847 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004848 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004849 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004850 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004851 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004852 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004854 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855}
4856
4857/*
4858 * Find index for option 'arg'.
4859 * Return -1 if not found.
4860 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004862findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 int opt_idx;
4865 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004866 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 int is_term_opt;
4868
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004869 // For first call: Initialize the quick-access table.
4870 // It contains the index for the first option that starts with a certain
4871 // letter. There are 26 letters, plus the first "t_" option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (quick_tab[1] == 0)
4873 {
4874 p = options[0].fullname;
4875 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4876 {
4877 if (s[0] != p[0])
4878 {
4879 if (s[0] == 't' && s[1] == '_')
4880 quick_tab[26] = opt_idx;
4881 else
4882 quick_tab[CharOrdLow(s[0])] = opt_idx;
4883 }
4884 p = s;
4885 }
4886 }
4887
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00004888 // Check for name starting with an illegal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return -1;
4891
4892 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4893 if (is_term_opt)
4894 opt_idx = quick_tab[26];
4895 else
4896 opt_idx = quick_tab[CharOrdLow(arg[0])];
4897 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004899 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 break;
4901 }
4902 if (s == NULL && !is_term_opt)
4903 {
4904 opt_idx = quick_tab[CharOrdLow(arg[0])];
4905 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4906 {
4907 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004908 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 break;
4910 s = NULL;
4911 }
4912 }
4913 if (s == NULL)
4914 opt_idx = -1;
4915 return opt_idx;
4916}
4917
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004918#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4919 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920/*
4921 * Get the value for an option.
4922 *
4923 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004924 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004925 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004926 * String option: gov_string, *stringval gets allocated string.
4927 * Hidden Number option: gov_hidden_number.
4928 * Hidden Toggle option: gov_hidden_bool.
4929 * Hidden String option: gov_hidden_string.
4930 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004931 *
4932 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004934 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004935get_option_value(
4936 char_u *name,
4937 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004938 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004939 int *flagsp,
4940 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 int opt_idx;
4943 char_u *varp;
4944
4945 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004946 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004947 {
4948 int key;
4949
4950 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004951 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004952 {
4953 char_u key_name[2];
4954 char_u *p;
4955
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004956 if (flagsp != NULL)
4957 *flagsp = 0; // terminal option has no flags
4958
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004959 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004960 if (key < 0)
4961 {
4962 key_name[0] = KEY2TERMCAP0(key);
4963 key_name[1] = KEY2TERMCAP1(key);
4964 }
4965 else
4966 {
4967 key_name[0] = KS_KEY;
4968 key_name[1] = (key & 0xff);
4969 }
4970 p = find_termcode(key_name);
4971 if (p != NULL)
4972 {
4973 if (stringval != NULL)
4974 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004975 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004976 }
4977 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004978 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004981 varp = get_varp_scope(&(options[opt_idx]), scope);
4982
4983 if (flagsp != NULL)
4984 // Return the P_xxxx option flags.
4985 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
4987 if (options[opt_idx].flags & P_STRING)
4988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004989 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004990 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 if (stringval != NULL)
4992 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004993 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004994 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4995 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004997 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004998 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004999 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01005002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 *stringval = vim_strsave(*(char_u **)(varp));
5004 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005005 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005008 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005009 return (options[opt_idx].flags & P_NUM)
5010 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (options[opt_idx].flags & P_NUM)
5012 *numval = *(long *)varp;
5013 else
5014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005015 // Special case: 'modified' is b_changed, but we also want to consider
5016 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if ((int *)varp == &curbuf->b_changed)
5018 *numval = curbufIsChanged();
5019 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02005020 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01005022 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023}
5024#endif
5025
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005026#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005027/*
5028 * Returns the option attributes and its value. Unlike the above function it
5029 * will return either global value or local value of the option depending on
5030 * what was requested, but it will never return global value if it was
5031 * requested to return local one and vice versa. Neither it will return
5032 * buffer-local value if it was requested to return window-local one.
5033 *
5034 * Pretends that option is absent if it is not present in the requested scope
5035 * (i.e. has no global, window-local or buffer-local value depending on
5036 * opt_type). Uses
5037 *
5038 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02005039 * 0 hidden or unknown option, also option that does not have requested
5040 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005041 * see SOPT_* in vim.h for other flags
5042 *
5043 * Possible opt_type values: see SREQ_* in vim.h
5044 */
5045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005046get_option_value_strict(
5047 char_u *name,
5048 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005049 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005050 int opt_type,
5051 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052{
5053 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005054 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005055 struct vimoption *p;
5056 int r = 0;
5057
5058 opt_idx = findoption(name);
5059 if (opt_idx < 0)
5060 return 0;
5061
5062 p = &(options[opt_idx]);
5063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005064 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005065 if (p->var == NULL)
5066 return 0;
5067
5068 if (p->flags & P_BOOL)
5069 r |= SOPT_BOOL;
5070 else if (p->flags & P_NUM)
5071 r |= SOPT_NUM;
5072 else if (p->flags & P_STRING)
5073 r |= SOPT_STRING;
5074
5075 if (p->indir == PV_NONE)
5076 {
5077 if (opt_type == SREQ_GLOBAL)
5078 r |= SOPT_GLOBAL;
5079 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005080 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005081 }
5082 else
5083 {
5084 if (p->indir & PV_BOTH)
5085 r |= SOPT_GLOBAL;
5086 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005087 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005088
5089 if (p->indir & PV_WIN)
5090 {
5091 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005092 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005093 else
5094 r |= SOPT_WIN;
5095 }
5096 else if (p->indir & PV_BUF)
5097 {
5098 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005099 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005100 else
5101 r |= SOPT_BUF;
5102 }
5103 }
5104
5105 if (stringval == NULL)
5106 return r;
5107
5108 if (opt_type == SREQ_GLOBAL)
5109 varp = p->var;
5110 else
5111 {
5112 if (opt_type == SREQ_BUF)
5113 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005114 // Special case: 'modified' is b_changed, but we also want to
5115 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005116 if (p->indir == PV_MOD)
5117 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005118 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005119 varp = NULL;
5120 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005121# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 else if (p->indir == PV_KEY)
5123 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005124 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005125 *stringval = NULL;
5126 varp = NULL;
5127 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005128# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005129 else
5130 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005131 buf_T *save_curbuf = curbuf;
5132
5133 // only getting a pointer, no need to use aucmd_prepbuf()
5134 curbuf = (buf_T *)from;
5135 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005136 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005137 curbuf = save_curbuf;
5138 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005139 }
5140 }
5141 else if (opt_type == SREQ_WIN)
5142 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005143 win_T *save_curwin = curwin;
5144
5145 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005146 curbuf = curwin->w_buffer;
5147 varp = get_varp(p);
5148 curwin = save_curwin;
5149 curbuf = curwin->w_buffer;
5150 }
5151 if (varp == p->var)
5152 return (r | SOPT_UNSET);
5153 }
5154
5155 if (varp != NULL)
5156 {
5157 if (p->flags & P_STRING)
5158 *stringval = vim_strsave(*(char_u **)(varp));
5159 else if (p->flags & P_NUM)
5160 *numval = *(long *) varp;
5161 else
5162 *numval = *(int *)varp;
5163 }
5164
5165 return r;
5166}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005167
5168/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005169 * Iterate over options. First argument is a pointer to a pointer to a
5170 * structure inside options[] array, second is option type like in the above
5171 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005172 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005173 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005174 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005175 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005176 * first argument to NULL.
5177 *
5178 * Returns full option name for current option on each call.
5179 */
5180 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005182{
5183 struct vimoption *ret = NULL;
5184 do
5185 {
5186 if (*option == NULL)
5187 *option = (void *) options;
5188 else if (((struct vimoption *) (*option))->fullname == NULL)
5189 {
5190 *option = NULL;
5191 return NULL;
5192 }
5193 else
5194 *option = (void *) (((struct vimoption *) (*option)) + 1);
5195
5196 ret = ((struct vimoption *) (*option));
5197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005198 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005199 if (ret->var == NULL)
5200 {
5201 ret = NULL;
5202 continue;
5203 }
5204
5205 switch (opt_type)
5206 {
5207 case SREQ_GLOBAL:
5208 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5209 ret = NULL;
5210 break;
5211 case SREQ_BUF:
5212 if (!(ret->indir & PV_BUF))
5213 ret = NULL;
5214 break;
5215 case SREQ_WIN:
5216 if (!(ret->indir & PV_WIN))
5217 ret = NULL;
5218 break;
5219 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005220 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005221 return NULL;
5222 }
5223 }
5224 while (ret == NULL);
5225
5226 return (char_u *)ret->fullname;
5227}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005228#endif
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005231 * Return the flags for the option at 'opt_idx'.
5232 */
5233 long_u
5234get_option_flags(int opt_idx)
5235{
5236 return options[opt_idx].flags;
5237}
5238
5239/*
5240 * Set a flag for the option at 'opt_idx'.
5241 */
5242 void
5243set_option_flag(int opt_idx, long_u flag)
5244{
5245 options[opt_idx].flags |= flag;
5246}
5247
5248/*
5249 * Clear a flag for the option at 'opt_idx'.
5250 */
5251 void
5252clear_option_flag(int opt_idx, long_u flag)
5253{
5254 options[opt_idx].flags &= ~flag;
5255}
5256
5257/*
5258 * Returns TRUE if the option at 'opt_idx' is a global option
5259 */
5260 int
5261is_global_option(int opt_idx)
5262{
5263 return options[opt_idx].indir == PV_NONE;
5264}
5265
5266/*
5267 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5268 * local value.
5269 */
5270 int
5271is_global_local_option(int opt_idx)
5272{
5273 return options[opt_idx].indir & PV_BOTH;
5274}
5275
5276/*
5277 * Returns TRUE if the option at 'opt_idx' is a window-local option
5278 */
5279 int
5280is_window_local_option(int opt_idx)
5281{
5282 return options[opt_idx].var == VAR_WIN;
5283}
5284
5285/*
5286 * Returns TRUE if the option at 'opt_idx' is a hidden option
5287 */
5288 int
5289is_hidden_option(int opt_idx)
5290{
5291 return options[opt_idx].var == NULL;
5292}
5293
5294#if defined(FEAT_CRYPT) || defined(PROTO)
5295/*
5296 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5297 */
5298 int
5299is_crypt_key_option(int opt_idx)
5300{
5301 return options[opt_idx].indir == PV_KEY;
5302}
5303#endif
5304
5305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 * Set the value of option "name".
5307 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005308 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005309 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005311 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005312set_option_value(
5313 char_u *name,
5314 long number,
5315 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005316 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 int opt_idx;
5319 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005320 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005321 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005324 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005325 {
5326 int key;
5327
5328 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005329 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005330 {
5331 char_u key_name[2];
5332
5333 if (key < 0)
5334 {
5335 key_name[0] = KEY2TERMCAP0(key);
5336 key_name[1] = KEY2TERMCAP1(key);
5337 }
5338 else
5339 {
5340 key_name[0] = KS_KEY;
5341 key_name[1] = (key & 0xff);
5342 }
5343 add_termcode(key_name, string, FALSE);
5344 if (full_screen)
5345 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005346 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005347 return NULL;
5348 }
5349
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005350 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 else
5353 {
5354 flags = options[opt_idx].flags;
5355#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005356 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005358 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005359 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005360 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005363 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005364 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005365
5366 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5367 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005369 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005371 int idx;
5372
5373 // Either we are given a string or we are setting option
5374 // to zero.
5375 for (idx = 0; string[idx] == '0'; ++idx)
5376 ;
5377 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005378 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005379 // There's another character after zeros or the string
5380 // is empty. In both cases, we are trying to set a
5381 // num option using a string.
5382 semsg(_(e_number_required_after_str_equal_str),
5383 name, string);
5384 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005385
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005388 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005389 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005390 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005391 errbuf, sizeof(errbuf), opt_flags);
5392 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005393 else
5394 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005398 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399}
5400
5401/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005402 * Call set_option_value() and when an error is returned report it.
5403 */
5404 void
5405set_option_value_give_err(
5406 char_u *name,
5407 long number,
5408 char_u *string,
5409 int opt_flags) // OPT_LOCAL or 0 (both)
5410{
5411 char *errmsg = set_option_value(name, number, string, opt_flags);
5412
5413 if (errmsg != NULL)
5414 emsg(_(errmsg));
5415}
5416
5417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 * Get the terminal code for a terminal option.
5419 * Returns NULL when not found.
5420 */
5421 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005422get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 int opt_idx;
5425 char_u *varp;
5426
5427 if (tname[0] != 't' || tname[1] != '_' ||
5428 tname[2] == NUL || tname[3] == NUL)
5429 return NULL;
5430 if ((opt_idx = findoption(tname)) >= 0)
5431 {
5432 varp = get_varp(&(options[opt_idx]));
5433 if (varp != NULL)
5434 varp = *(char_u **)(varp);
5435 return varp;
5436 }
5437 return find_termcode(tname + 2);
5438}
5439
5440 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005441get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 int i;
5444
5445 i = findoption((char_u *)"hl");
5446 if (i >= 0)
5447 return options[i].def_val[VI_DEFAULT];
5448 return (char_u *)NULL;
5449}
5450
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005451 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005452get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005453{
5454 int i;
5455
5456 i = findoption((char_u *)"enc");
5457 if (i >= 0)
5458 return options[i].def_val[VI_DEFAULT];
5459 return (char_u *)NULL;
5460}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005461
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005462#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005463 int
5464is_option_allocated(char *name)
5465{
5466 int idx = findoption((char_u *)name);
5467
5468 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5469}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005470#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005471
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005472#if defined(FEAT_EVAL) || defined(PROTO)
5473/*
5474 * Return TRUE if "name" is a string option.
5475 * Returns FALSE if option "name" does not exist.
5476 */
5477 int
5478is_string_option(char_u *name)
5479{
5480 int idx = findoption(name);
5481
5482 return idx >= 0 && (options[idx].flags & P_STRING);
5483}
5484#endif
5485
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486/*
5487 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005488 * When "has_lt" is true there is a '<' before "*arg_arg".
5489 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 */
5491 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005492find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005494 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005496 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005498 // Don't use get_special_key_code() for t_xx, we don't want it to call
5499 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5501 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005502 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005504 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005506 key = find_special_key(&arg, &modifiers,
5507 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005508 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 key = 0;
5510 }
5511 return key;
5512}
5513
5514/*
5515 * if 'all' == 0: show changed options
5516 * if 'all' == 1: show all normal options
5517 * if 'all' == 2: show all terminal options
5518 */
5519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005520showoptions(
5521 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005522 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 struct vimoption *p;
5525 int col;
5526 int isterm;
5527 char_u *varp;
5528 struct vimoption **items;
5529 int item_count;
5530 int run;
5531 int row, rows;
5532 int cols;
5533 int i;
5534 int len;
5535
5536#define INC 20
5537#define GAP 3
5538
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005539 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 if (items == NULL)
5541 return;
5542
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005543 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005545 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005547 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005549 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005551 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005553 // Do the loop two times:
5554 // 1. display the short items
5555 // 2. display the long items (only strings and numbers)
5556 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 for (run = 1; run <= 2 && !got_int; ++run)
5558 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005559 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 item_count = 0;
5561 for (p = &options[0]; p->fullname != NULL; p++)
5562 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005563 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005564 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005565 continue;
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 varp = NULL;
5568 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005569 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
5571 if (p->indir != PV_NONE && !isterm)
5572 varp = get_varp_scope(p, opt_flags);
5573 }
5574 else
5575 varp = get_varp(p);
5576 if (varp != NULL
5577 && ((all == 2 && isterm)
5578 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005579 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005581 if (opt_flags & OPT_ONECOLUMN)
5582 len = Columns;
5583 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005584 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 else
5586 {
5587 option_value2string(p, opt_flags);
5588 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5589 }
5590 if ((len <= INC - GAP && run == 1) ||
5591 (len > INC - GAP && run == 2))
5592 items[item_count++] = p;
5593 }
5594 }
5595
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005596 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (run == 1)
5598 {
5599 cols = (Columns + GAP - 3) / INC;
5600 if (cols == 0)
5601 cols = 1;
5602 rows = (item_count + cols - 1) / cols;
5603 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 rows = item_count;
5606 for (row = 0; row < rows && !got_int; ++row)
5607 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 msg_putchar('\n'); // go to next line
5609 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 break;
5611 col = 0;
5612 for (i = row; i < item_count; i += rows)
5613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 showoneopt(items[i], opt_flags);
5616 col += INC;
5617 }
5618 out_flush();
5619 ui_breakcheck();
5620 }
5621 }
5622 vim_free(items);
5623}
5624
5625/*
5626 * Return TRUE if option "p" has its default value.
5627 */
5628 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005629optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 int dvi;
5632
5633 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005635 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005637 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 // the cast to long is required for Manx C, long_i is
5640 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005641 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005642 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5644}
5645
5646/*
5647 * showoneopt: show the value of one option
5648 * must not be called with a hidden option!
5649 */
5650 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005651showoneopt(
5652 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005653 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005655 char_u *varp;
5656 int save_silent = silent_mode;
5657
5658 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005659 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 varp = get_varp_scope(p, opt_flags);
5662
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005663 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5665 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005666 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005668 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005670 msg_puts(" ");
5671 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 if (!(p->flags & P_BOOL))
5673 {
5674 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005675 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 option_value2string(p, opt_flags);
5677 msg_outtrans(NameBuff);
5678 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005679
5680 silent_mode = save_silent;
5681 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682}
5683
5684/*
5685 * Write modified options as ":set" commands to a file.
5686 *
5687 * There are three values for "opt_flags":
5688 * OPT_GLOBAL: Write global option values and fresh values of
5689 * buffer-local options (used for start of a session
5690 * file).
5691 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5692 * curwin (used for a vimrc file).
5693 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5694 * and local values for window-local options of
5695 * curwin. Local values are also written when at the
5696 * default value, because a modeline or autocommand
5697 * may have set them when doing ":edit file" and the
5698 * user has set them back at the default or fresh
5699 * value.
5700 * When "local_only" is TRUE, don't write fresh
5701 * values, only local values (for ":mkview").
5702 * (fresh value = value used for a new buffer or window for a local option).
5703 *
5704 * Return FAIL on error, OK otherwise.
5705 */
5706 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005707makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 char_u *varp; // currently used value
5711 char_u *varp_fresh; // local value
5712 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 char *cmd;
5714 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005715 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 /*
5718 * The options that don't have a default (terminal name, columns, lines)
5719 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005720 * Do the loop over "options[]" twice: once for options with the
5721 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005723 for (pri = 1; pri >= 0; --pri)
5724 {
5725 for (p = &options[0]; !istermoption(p); p++)
5726 if (!(p->flags & P_NO_MKRC)
5727 && !istermoption(p)
5728 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005730 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5732 continue;
5733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005734 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5735 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5737 continue;
5738
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005739 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005741 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 continue;
5743
Bram Moolenaard23b7142021-04-17 21:04:34 +02005744 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5745 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005746 continue;
5747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 round = 2;
5749 if (p->indir != PV_NONE)
5750 {
5751 if (p->var == VAR_WIN)
5752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005753 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (!(opt_flags & OPT_LOCAL))
5755 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005756 // When fresh value of window-local option is not at the
5757 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5759 {
5760 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005761 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 round = 1;
5764 varp_local = varp;
5765 varp = varp_fresh;
5766 }
5767 }
5768 }
5769 }
5770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005771 // Round 1: fresh value for window-local options.
5772 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 for ( ; round <= 2; varp = varp_local, ++round)
5774 {
5775 if (round == 1 || (opt_flags & OPT_GLOBAL))
5776 cmd = "set";
5777 else
5778 cmd = "setlocal";
5779
5780 if (p->flags & P_BOOL)
5781 {
5782 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5783 return FAIL;
5784 }
5785 else if (p->flags & P_NUM)
5786 {
5787 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5788 return FAIL;
5789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005790 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005792 int do_endif = FALSE;
5793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005794 // Don't set 'syntax' and 'filetype' again if the value is
5795 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005796 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005797#if defined(FEAT_SYN_HL)
5798 p->indir == PV_SYN ||
5799#endif
5800 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 {
5802 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5803 *(char_u **)(varp)) < 0
5804 || put_eol(fd) < 0)
5805 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005806 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005809 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005811 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
5813 if (put_line(fd, "endif") == FAIL)
5814 return FAIL;
5815 }
5816 }
5817 }
5818 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 return OK;
5821}
5822
5823#if defined(FEAT_FOLDING) || defined(PROTO)
5824/*
5825 * Generate set commands for the local fold options only. Used when
5826 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5827 */
5828 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005829makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005831 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005833 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 == FAIL
5835# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005836 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005838 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 == FAIL
5840 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5841 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5842 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5843 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5844 )
5845 return FAIL;
5846
5847 return OK;
5848}
5849#endif
5850
5851 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005852put_setstring(
5853 FILE *fd,
5854 char *cmd,
5855 char *name,
5856 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005857 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005860 char_u *buf = NULL;
5861 char_u *part = NULL;
5862 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
5864 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5865 return FAIL;
5866 if (*valuep != NULL)
5867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005868 // Output 'pastetoggle' as key names. For other
5869 // options some characters have to be escaped with
5870 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 if (valuep == &p_pt)
5872 {
5873 s = *valuep;
5874 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005875 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 return FAIL;
5877 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005878 // expand the option value, replace $HOME by ~
5879 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005881 int size = (int)STRLEN(*valuep) + 1;
5882
5883 // replace home directory in the whole option value into "buf"
5884 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005885 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005886 goto fail;
5887 home_replace(NULL, *valuep, buf, size, FALSE);
5888
5889 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005890 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005891 // can be expanded when read back.
5892 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5893 && vim_strchr(*valuep, ',') != NULL)
5894 {
5895 part = alloc(size);
5896 if (part == NULL)
5897 goto fail;
5898
5899 // write line break to clear the option, e.g. ':set rtp='
5900 if (put_eol(fd) == FAIL)
5901 goto fail;
5902
5903 p = buf;
5904 while (*p != NUL)
5905 {
5906 // for each comma separated option part, append value to
5907 // the option, :set rtp+=value
5908 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5909 goto fail;
5910 (void)copy_option_part(&p, part, size, ",");
5911 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5912 goto fail;
5913 }
5914 vim_free(buf);
5915 vim_free(part);
5916 return OK;
5917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005919 {
5920 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005922 }
5923 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 }
5925 else if (put_escstr(fd, *valuep, 2) == FAIL)
5926 return FAIL;
5927 }
5928 if (put_eol(fd) < 0)
5929 return FAIL;
5930 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005931fail:
5932 vim_free(buf);
5933 vim_free(part);
5934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935}
5936
5937 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005938put_setnum(
5939 FILE *fd,
5940 char *cmd,
5941 char *name,
5942 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 long wc;
5945
5946 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5947 return FAIL;
5948 if (wc_use_keyname((char_u *)valuep, &wc))
5949 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005950 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5952 return FAIL;
5953 }
5954 else if (fprintf(fd, "%ld", *valuep) < 0)
5955 return FAIL;
5956 if (put_eol(fd) < 0)
5957 return FAIL;
5958 return OK;
5959}
5960
5961 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005962put_setbool(
5963 FILE *fd,
5964 char *cmd,
5965 char *name,
5966 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005968 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005969 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5971 || put_eol(fd) < 0)
5972 return FAIL;
5973 return OK;
5974}
5975
5976/*
5977 * Clear all the terminal options.
5978 * If the option has been allocated, free the memory.
5979 * Terminal options are never hidden or indirect.
5980 */
5981 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005982clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005984 // Reset a few things before clearing the old options. This may cause
5985 // outputting a few things that the terminal doesn't understand, but the
5986 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005987 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005988 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005990 // When starting the GUI close the display opened for the clipboard.
5991 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 if (gui.starting)
5993 clear_xterm_clip();
5994#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005995 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005997 free_termoptions();
5998}
5999
6000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006001free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006002{
6003 struct vimoption *p;
6004
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006005 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 if (istermoption(p))
6007 {
6008 if (p->flags & P_ALLOCED)
6009 free_string_option(*(char_u **)(p->var));
6010 if (p->flags & P_DEF_ALLOCED)
6011 free_string_option(p->def_val[VI_DEFAULT]);
6012 *(char_u **)(p->var) = empty_option;
6013 p->def_val[VI_DEFAULT] = empty_option;
6014 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006015#ifdef FEAT_EVAL
6016 // remember where the option was cleared
6017 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 }
6020 clear_termcodes();
6021}
6022
6023/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006024 * Free the string for one term option, if it was allocated.
6025 * Set the string to empty_option and clear allocated flag.
6026 * "var" points to the option value.
6027 */
6028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006029free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006030{
6031 struct vimoption *p;
6032
6033 for (p = &options[0]; p->fullname != NULL; p++)
6034 if (p->var == var)
6035 {
6036 if (p->flags & P_ALLOCED)
6037 free_string_option(*(char_u **)(p->var));
6038 *(char_u **)(p->var) = empty_option;
6039 p->flags &= ~P_ALLOCED;
6040 break;
6041 }
6042}
6043
6044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 * Set the terminal option defaults to the current value.
6046 * Used after setting the terminal name.
6047 */
6048 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006049set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 struct vimoption *p;
6052
6053 for (p = &options[0]; p->fullname != NULL; p++)
6054 {
6055 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6056 {
6057 if (p->flags & P_DEF_ALLOCED)
6058 {
6059 free_string_option(p->def_val[VI_DEFAULT]);
6060 p->flags &= ~P_DEF_ALLOCED;
6061 }
6062 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6063 if (p->flags & P_ALLOCED)
6064 {
6065 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006066 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 }
6068 }
6069 }
6070}
6071
6072/*
6073 * return TRUE if 'p' starts with 't_'
6074 */
6075 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006076istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6079}
6080
Bram Moolenaardac13472019-09-16 21:06:21 +02006081/*
6082 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6083 */
6084 int
6085istermoption_idx(int opt_idx)
6086{
6087 return istermoption(&options[opt_idx]);
6088}
6089
Bram Moolenaar113e1072019-01-20 15:30:40 +01006090#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006092 * Unset local option value, similar to ":set opt<".
6093 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006095unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006096{
6097 struct vimoption *p;
6098 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006099 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006100
6101 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006102 if (opt_idx < 0)
6103 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006104 p = &(options[opt_idx]);
6105
6106 switch ((int)p->indir)
6107 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006108 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006109 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006110 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006111 break;
6112 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006113 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006114 break;
6115 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006116 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006117 break;
6118 case PV_AR:
6119 buf->b_p_ar = -1;
6120 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006121 case PV_BKC:
6122 clear_string_option(&buf->b_p_bkc);
6123 buf->b_bkc_flags = 0;
6124 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006125 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006126 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006127 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006128 case PV_TC:
6129 clear_string_option(&buf->b_p_tc);
6130 buf->b_tc_flags = 0;
6131 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006132 case PV_SISO:
6133 curwin->w_p_siso = -1;
6134 break;
6135 case PV_SO:
6136 curwin->w_p_so = -1;
6137 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006138# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006139 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006140 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006141 break;
6142 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006143 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006144 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006145# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006146 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006147 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006148 break;
6149 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006150 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006151 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006152# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006153 case PV_TSRFU:
6154 clear_string_option(&buf->b_p_tsrfu);
6155 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006156# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006157 case PV_FP:
6158 clear_string_option(&buf->b_p_fp);
6159 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006160# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006161 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006162 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006163 break;
6164 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006165 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006166 break;
6167 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006168 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006169 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006170# endif
6171# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006172 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006173 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006174 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006175# endif
6176# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006177 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006178 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006179 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006180# endif
6181# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006182 case PV_SBR:
6183 clear_string_option(&((win_T *)from)->w_p_sbr);
6184 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006185# endif
6186# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006187 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006188 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006189 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006190# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006191 case PV_UL:
6192 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6193 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006194 case PV_LW:
6195 clear_string_option(&buf->b_p_lw);
6196 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006197 case PV_MENC:
6198 clear_string_option(&buf->b_p_menc);
6199 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006200 case PV_LCS:
6201 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006202 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006203 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006204 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006205 case PV_FCS:
6206 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006207 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006208 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006209 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006210 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006211 clear_string_option(&((win_T *)from)->w_p_ve);
6212 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006213 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006214 }
6215}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006216#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006217
6218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006220 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 */
6222 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006223get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006225 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
6227 if (p->var == VAR_WIN)
6228 return (char_u *)GLOBAL_WO(get_varp(p));
6229 return p->var;
6230 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006231 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
6233 switch ((int)p->indir)
6234 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006235 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006237 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6238 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6239 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006241 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6242 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6243 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006244 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006245 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006246 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006247 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6248 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006250 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6251 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006253 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6254 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006255#ifdef FEAT_COMPL_FUNC
6256 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6257#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006258#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6259 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6260#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006261#if defined(FEAT_CRYPT)
6262 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6263#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006264#ifdef FEAT_LINEBREAK
6265 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6266#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006267#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006268 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006269#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006270 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006271 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006272 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006273 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006274 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006275 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006276 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006279 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281 return get_varp(p);
6282}
6283
6284/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006285 * Get pointer to option variable at 'opt_idx', depending on local or global
6286 * scope.
6287 */
6288 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006289get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006290{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006291 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006292}
6293
6294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * Get pointer to option variable.
6296 */
6297 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006298get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006300 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (p->var == NULL)
6302 return NULL;
6303
6304 switch ((int)p->indir)
6305 {
6306 case PV_NONE: return p->var;
6307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006308 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006309 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006311 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006313 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006315 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006317 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006319 case PV_TC: return *curbuf->b_p_tc != NUL
6320 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006321 case PV_BKC: return *curbuf->b_p_bkc != NUL
6322 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006323 case PV_SISO: return curwin->w_p_siso >= 0
6324 ? (char_u *)&(curwin->w_p_siso) : p->var;
6325 case PV_SO: return curwin->w_p_so >= 0
6326 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006328 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006330 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6332#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006333 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006335 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006337#ifdef FEAT_COMPL_FUNC
6338 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6339 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6340#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006341 case PV_FP: return *curbuf->b_p_fp != NUL
6342 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006344 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006346 case PV_GP: return *curbuf->b_p_gp != NUL
6347 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6348 case PV_MP: return *curbuf->b_p_mp != NUL
6349 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006351#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6352 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6353 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6354#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006355#if defined(FEAT_CRYPT)
6356 case PV_CM: return *curbuf->b_p_cm != NUL
6357 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6358#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006359#ifdef FEAT_LINEBREAK
6360 case PV_SBR: return *curwin->w_p_sbr != NUL
6361 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6362#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006363#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006364 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006365 ? (char_u *)&(curwin->w_p_stl) : p->var;
6366#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006367 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6368 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006369 case PV_LW: return *curbuf->b_p_lw != NUL
6370 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006371 case PV_MENC: return *curbuf->b_p_menc != NUL
6372 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373#ifdef FEAT_ARABIC
6374 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6375#endif
6376 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006377 case PV_LCS: return *curwin->w_p_lcs != NUL
6378 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006379 case PV_FCS: return *curwin->w_p_fcs != NUL
6380 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006381 case PV_VE: return *curwin->w_p_ve != NUL
6382 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006383#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006384 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6385#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006386#ifdef FEAT_SYN_HL
6387 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6388 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006389 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006390 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392#ifdef FEAT_DIFF
6393 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6394#endif
6395#ifdef FEAT_FOLDING
6396 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6397 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6398 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6399 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6400 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6401 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6402 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6403# ifdef FEAT_EVAL
6404 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6405 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6406# endif
6407 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6408#endif
6409 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006410 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006411#ifdef FEAT_LINEBREAK
6412 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006415 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006416#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6418#endif
6419#ifdef FEAT_RIGHTLEFT
6420 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6421 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6422#endif
6423 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006424 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6426#ifdef FEAT_LINEBREAK
6427 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006428 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6429 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006431 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006433 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006434#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006435 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6436 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6437#endif
6438#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006439 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6440 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6441 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443
6444 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6445 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6448 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6450 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6452 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6453 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006454 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457#ifdef FEAT_FOLDING
6458 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006461#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006462 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006464#ifdef FEAT_COMPL_FUNC
6465 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006466 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006467#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006468#ifdef FEAT_EVAL
6469 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6470#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006471 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006473 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006479 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6481 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6482 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6483 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6484#ifdef FEAT_FIND_ID
6485# ifdef FEAT_EVAL
6486 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6487# endif
6488#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006489#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6491 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6492#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006493#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006494 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496#ifdef FEAT_CRYPT
6497 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006500 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6502 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6503 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6504 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6505 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006507 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6514#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006515 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006516 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6517#endif
6518#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006519 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6520 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6521 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006522 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523#endif
6524 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6525 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6526 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6527 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006528#ifdef FEAT_PERSISTENT_UNDO
6529 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6532#ifdef FEAT_KEYMAP
6533 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6534#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006535#ifdef FEAT_SIGNS
6536 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6537#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006538#ifdef FEAT_VARTABS
6539 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6540 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6541#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006542 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006544 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 return (char_u *)&(curbuf->b_p_wm);
6546}
6547
6548/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006549 * Return a pointer to the variable for option at 'opt_idx'
6550 */
6551 char_u *
6552get_option_var(int opt_idx)
6553{
6554 return options[opt_idx].var;
6555}
6556
Dominique Pelle748b3082022-01-08 12:41:16 +00006557#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006558/*
6559 * Return the full name of the option at 'opt_idx'
6560 */
6561 char_u *
6562get_option_fullname(int opt_idx)
6563{
6564 return (char_u *)options[opt_idx].fullname;
6565}
Dominique Pelle748b3082022-01-08 12:41:16 +00006566#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006567
6568/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006569 * Return the did_set callback function for the option at 'opt_idx'
6570 */
6571 opt_did_set_cb_T
6572get_option_did_set_cb(int opt_idx)
6573{
6574 return options[opt_idx].opt_did_set_cb;
6575}
6576
6577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 * Get the value of 'equalprg', either the buffer-local one or the global one.
6579 */
6580 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006581get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 if (*curbuf->b_p_ep == NUL)
6584 return p_ep;
6585 return curbuf->b_p_ep;
6586}
6587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588/*
6589 * Copy options from one window to another.
6590 * Used when splitting a window.
6591 */
6592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006593win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6596 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006597 after_copy_winopt(wp_to);
6598}
6599
6600/*
6601 * After copying window options: update variables depending on options.
6602 */
6603 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006604after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006605{
6606#ifdef FEAT_LINEBREAK
6607 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006608#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006609#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006610 fill_culopt_flags(NULL, wp);
6611 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006612#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006613 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6614 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006615}
6616
6617 static char_u *
6618copy_option_val(char_u *val)
6619{
6620 if (val == empty_option)
6621 return empty_option; // no need to allocate memory
6622 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625/*
6626 * Copy the options from one winopt_T to another.
6627 * Doesn't free the old option values in "to", use clear_winopt() for that.
6628 * The 'scroll' option is not copied, because it depends on the window height.
6629 * The 'previewwindow' option is reset, there can be only one preview window.
6630 */
6631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006632copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633{
6634#ifdef FEAT_ARABIC
6635 to->wo_arab = from->wo_arab;
6636#endif
6637 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006638 to->wo_lcs = copy_option_val(from->wo_lcs);
6639 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006641 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006642 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006643 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006644#ifdef FEAT_LINEBREAK
6645 to->wo_nuw = from->wo_nuw;
6646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647#ifdef FEAT_RIGHTLEFT
6648 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006649 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006651#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006652 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006653#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006654#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006655 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006658#ifdef FEAT_DIFF
6659 to->wo_wrap_save = from->wo_wrap_save;
6660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661#ifdef FEAT_LINEBREAK
6662 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006663 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006664 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006666 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006668 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006669 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006670 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006671 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006672#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006673 to->wo_spell = from->wo_spell;
6674#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006675#ifdef FEAT_SYN_HL
6676 to->wo_cuc = from->wo_cuc;
6677 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006678 to->wo_culopt = copy_option_val(from->wo_culopt);
6679 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681#ifdef FEAT_DIFF
6682 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006683 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006685#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006686 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006687 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006688#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006689#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006690 to->wo_twk = copy_option_val(from->wo_twk);
6691 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693#ifdef FEAT_FOLDING
6694 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006695 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006697 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006698 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 to->wo_fml = from->wo_fml;
6700 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006701 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006702 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006703 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006704 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 to->wo_fdn = from->wo_fdn;
6706# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006707 to->wo_fde = copy_option_val(from->wo_fde);
6708 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006710 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006712#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006713 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006714#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006715
6716#ifdef FEAT_EVAL
6717 // Copy the script context so that we know where the value was last set.
6718 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6719 sizeof(to->wo_script_ctx));
6720#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006721 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722}
6723
6724/*
6725 * Check string options in a window for a NULL value.
6726 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006727 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006728check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 check_winopt(&win->w_onebuf_opt);
6731 check_winopt(&win->w_allbuf_opt);
6732}
6733
6734/*
6735 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6736 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006738check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740#ifdef FEAT_FOLDING
6741 check_string_option(&wop->wo_fdi);
6742 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006743 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744# ifdef FEAT_EVAL
6745 check_string_option(&wop->wo_fde);
6746 check_string_option(&wop->wo_fdt);
6747# endif
6748 check_string_option(&wop->wo_fmr);
6749#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006750#ifdef FEAT_SIGNS
6751 check_string_option(&wop->wo_scl);
6752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753#ifdef FEAT_RIGHTLEFT
6754 check_string_option(&wop->wo_rlc);
6755#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006756#ifdef FEAT_LINEBREAK
6757 check_string_option(&wop->wo_sbr);
6758#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006759#ifdef FEAT_STL_OPT
6760 check_string_option(&wop->wo_stl);
6761#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006762#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006763 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006764 check_string_option(&wop->wo_cc);
6765#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006766#ifdef FEAT_CONCEAL
6767 check_string_option(&wop->wo_cocu);
6768#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006769#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006770 check_string_option(&wop->wo_twk);
6771 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006772#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006773#ifdef FEAT_LINEBREAK
6774 check_string_option(&wop->wo_briopt);
6775#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006776 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006777 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006778 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006779 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780}
6781
6782/*
6783 * Free the allocated memory inside a winopt_T.
6784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006786clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787{
6788#ifdef FEAT_FOLDING
6789 clear_string_option(&wop->wo_fdi);
6790 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006791 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792# ifdef FEAT_EVAL
6793 clear_string_option(&wop->wo_fde);
6794 clear_string_option(&wop->wo_fdt);
6795# endif
6796 clear_string_option(&wop->wo_fmr);
6797#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006798#ifdef FEAT_SIGNS
6799 clear_string_option(&wop->wo_scl);
6800#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006801#ifdef FEAT_LINEBREAK
6802 clear_string_option(&wop->wo_briopt);
6803#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006804 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805#ifdef FEAT_RIGHTLEFT
6806 clear_string_option(&wop->wo_rlc);
6807#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006808#ifdef FEAT_LINEBREAK
6809 clear_string_option(&wop->wo_sbr);
6810#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006811#ifdef FEAT_STL_OPT
6812 clear_string_option(&wop->wo_stl);
6813#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006814#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006815 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006816 clear_string_option(&wop->wo_cc);
6817#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006818#ifdef FEAT_CONCEAL
6819 clear_string_option(&wop->wo_cocu);
6820#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006821#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006822 clear_string_option(&wop->wo_twk);
6823 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006824#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006825 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006826 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006827 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828}
6829
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006830#ifdef FEAT_EVAL
6831// Index into the options table for a buffer-local option enum.
6832static int buf_opt_idx[BV_COUNT];
6833# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6834
6835/*
6836 * Initialize buf_opt_idx[] if not done already.
6837 */
6838 static void
6839init_buf_opt_idx(void)
6840{
6841 static int did_init_buf_opt_idx = FALSE;
6842 int i;
6843
6844 if (did_init_buf_opt_idx)
6845 return;
6846 did_init_buf_opt_idx = TRUE;
6847 for (i = 0; !istermoption_idx(i); i++)
6848 if (options[i].indir & PV_BUF)
6849 buf_opt_idx[options[i].indir & PV_MASK] = i;
6850}
6851#else
6852# define COPY_OPT_SCTX(buf, bv)
6853#endif
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855/*
6856 * Copy global option values to local options for one buffer.
6857 * Used when creating a new buffer and sometimes when entering a buffer.
6858 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006859 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6861 * appropriate.
6862 * BCO_NOHELP Don't copy the values to a help buffer.
6863 */
6864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006865buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 int dont_do_help;
6870 int did_isk = FALSE;
6871
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006872 // Skip this when the option defaults have not been set yet. Happens when
6873 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 if (p_cpo != NULL)
6875 {
6876 /*
6877 * Always copy when entering and 'cpo' contains 'S'.
6878 * Don't copy when already initialized.
6879 * Don't copy when 'cpo' contains 's' and not entering.
6880 * 'S' BCO_ENTER initialized 's' should_copy
6881 * yes yes X X TRUE
6882 * yes no yes X FALSE
6883 * no X yes X FALSE
6884 * X no no yes FALSE
6885 * X no no no TRUE
6886 * no yes no X TRUE
6887 */
6888 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6889 && (buf->b_p_initialized
6890 || (!(flags & BCO_ENTER)
6891 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6892 should_copy = FALSE;
6893
6894 if (should_copy || (flags & BCO_ALWAYS))
6895 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006896#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006897 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006898 init_buf_opt_idx();
6899#endif
6900 // Don't copy the options specific to a help buffer when
6901 // BCO_NOHELP is given or the options were initialized already
6902 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6904 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006905 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
6907 save_p_isk = buf->b_p_isk;
6908 buf->b_p_isk = NULL;
6909 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006910 // Always free the allocated strings. If not already initialized,
6911 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 if (!buf->b_p_initialized)
6913 {
6914 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006915 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006918 switch (*p_ffs)
6919 {
6920 case 'm':
6921 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6922 case 'd':
6923 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6924 case 'u':
6925 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6926 default:
6927 buf->b_p_ff = vim_strsave(p_ff);
6928 }
6929 if (buf->b_p_ff != NULL)
6930 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 buf->b_p_bh = empty_option;
6932 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
6934 else
6935 free_buf_options(buf, FALSE);
6936
6937 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006938 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 buf->b_p_ai_nopaste = p_ai_nopaste;
6940 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006941 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006943 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 buf->b_p_tw_nopaste = p_tw_nopaste;
6945 buf->b_p_tw_nobin = p_tw_nobin;
6946 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006947 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 buf->b_p_wm_nopaste = p_wm_nopaste;
6949 buf->b_p_wm_nobin = p_wm_nobin;
6950 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006951 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006952 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006953 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006954 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006955 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006957 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006959 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006961 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 buf->b_p_ml_nobin = p_ml_nobin;
6963 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006965 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006966 buf->b_p_swf = FALSE;
6967 else
6968 {
6969 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006970 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006973 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006974#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006975 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006976 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006978#ifdef FEAT_COMPL_FUNC
6979 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006980 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006981 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006982 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006984 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006985#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006986#ifdef FEAT_EVAL
6987 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006988 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006989 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006992 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006994#ifdef FEAT_VARTABS
6995 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006996 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006997 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006998 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006999 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007000 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007001 buf->b_p_vsts_nopaste = p_vsts_nopaste
7002 ? vim_strsave(p_vsts_nopaste) : NULL;
7003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007005 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007007 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008#ifdef FEAT_FOLDING
7009 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007010 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011#endif
7012 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007013 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007014 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007015 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007016 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7017 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007019 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007021 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007023 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007025 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007028 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007030 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007032 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007033 buf->b_p_cinsd = vim_strsave(p_cinsd);
7034 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007035 buf->b_p_lop = vim_strsave(p_lop);
7036 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007037
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007038 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007043 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007045 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007047 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007049 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007050 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007051 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007052#endif
7053#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007054 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007055 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007056 (void)compile_cap_prog(&buf->b_s);
7057 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007058 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007059 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007060 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007061 buf->b_s.b_p_spo = vim_strsave(p_spo);
7062 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007064#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007066 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007068 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007070 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007071#if defined(FEAT_EVAL)
7072 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007073 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#ifdef FEAT_CRYPT
7076 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007077 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007080 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081#ifdef FEAT_KEYMAP
7082 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007083 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 buf->b_kmap_state |= KEYMAP_INIT;
7085#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007086#ifdef FEAT_TERMINAL
7087 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007088 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007089#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 // This isn't really an option, but copying the langmap and IME
7091 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007093 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007095 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 // options that are normally global but also have a local value
7098 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007100 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007101 buf->b_p_bkc = empty_option;
7102 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#ifdef FEAT_QUICKFIX
7104 buf->b_p_gp = empty_option;
7105 buf->b_p_mp = empty_option;
7106 buf->b_p_efm = empty_option;
7107#endif
7108 buf->b_p_ep = empty_option;
7109 buf->b_p_kp = empty_option;
7110 buf->b_p_path = empty_option;
7111 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007112 buf->b_p_tc = empty_option;
7113 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114#ifdef FEAT_FIND_ID
7115 buf->b_p_def = empty_option;
7116 buf->b_p_inc = empty_option;
7117# ifdef FEAT_EVAL
7118 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007119 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120# endif
7121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 buf->b_p_dict = empty_option;
7123 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007124#ifdef FEAT_COMPL_FUNC
7125 buf->b_p_tsrfu = empty_option;
7126#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007127 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007128 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007129#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7130 buf->b_p_bexpr = empty_option;
7131#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007132#if defined(FEAT_CRYPT)
7133 buf->b_p_cm = empty_option;
7134#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007135#ifdef FEAT_PERSISTENT_UNDO
7136 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007137 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007138#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007139 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007140 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007142 // Don't copy the options set by ex_help(), use the saved values,
7143 // when going from a help buffer to a non-help buffer.
7144 // Don't touch these at all when BCO_NOHELP is used and going from
7145 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007149#ifdef FEAT_VARTABS
7150 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007151 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007152 else
7153 buf->b_p_vts_array = NULL;
7154#endif
7155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 else
7157 {
7158 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007159 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 did_isk = TRUE;
7161 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007162 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007163#ifdef FEAT_VARTABS
7164 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007165 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007166 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007167 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007168 else
7169 buf->b_p_vts_array = NULL;
7170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (buf->b_p_bt[0] == 'h')
7173 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007175 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
7177 }
7178
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007179 // When the options should be copied (ignoring BCO_ALWAYS), set the
7180 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 if (should_copy)
7182 buf->b_p_initialized = TRUE;
7183 }
7184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007185 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (did_isk)
7187 (void)buf_init_chartab(buf, FALSE);
7188}
7189
7190/*
7191 * Reset the 'modifiable' option and its default value.
7192 */
7193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007194reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 int opt_idx;
7197
7198 curbuf->b_p_ma = FALSE;
7199 p_ma = FALSE;
7200 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007201 if (opt_idx >= 0)
7202 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203}
7204
7205/*
7206 * Set the global value for 'iminsert' to the local value.
7207 */
7208 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007209set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 p_iminsert = curbuf->b_p_iminsert;
7212}
7213
7214/*
7215 * Set the global value for 'imsearch' to the local value.
7216 */
7217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007218set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
7220 p_imsearch = curbuf->b_p_imsearch;
7221}
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223static int expand_option_idx = -1;
7224static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7225static int expand_option_flags = 0;
7226
7227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007228set_context_in_set_cmd(
7229 expand_T *xp,
7230 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007231 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007234 long_u flags = 0; // init for GCC
7235 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 char_u *p;
7237 char_u *s;
7238 int is_term_option = FALSE;
7239 int key;
7240
7241 expand_option_flags = opt_flags;
7242
7243 xp->xp_context = EXPAND_SETTINGS;
7244 if (*arg == NUL)
7245 {
7246 xp->xp_pattern = arg;
7247 return;
7248 }
7249 p = arg + STRLEN(arg) - 1;
7250 if (*p == ' ' && *(p - 1) != '\\')
7251 {
7252 xp->xp_pattern = p + 1;
7253 return;
7254 }
7255 while (p > arg)
7256 {
7257 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007258 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (*p == ' ' || *p == ',')
7260 {
7261 while (s > arg && *(s - 1) == '\\')
7262 --s;
7263 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007264 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (*p == ' ' && ((p - s) & 1) == 0)
7266 {
7267 ++p;
7268 break;
7269 }
7270 --p;
7271 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007272 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
7274 xp->xp_context = EXPAND_BOOL_SETTINGS;
7275 p += 2;
7276 }
7277 if (STRNCMP(p, "inv", 3) == 0)
7278 {
7279 xp->xp_context = EXPAND_BOOL_SETTINGS;
7280 p += 3;
7281 }
7282 xp->xp_pattern = arg = p;
7283 if (*arg == '<')
7284 {
7285 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007286 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 return;
7288 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007289 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291 xp->xp_context = EXPAND_NOTHING;
7292 return;
7293 }
7294 nextchar = *++p;
7295 is_term_option = TRUE;
7296 expand_option_name[2] = KEY2TERMCAP0(key);
7297 expand_option_name[3] = KEY2TERMCAP1(key);
7298 }
7299 else
7300 {
7301 if (p[0] == 't' && p[1] == '_')
7302 {
7303 p += 2;
7304 if (*p != NUL)
7305 ++p;
7306 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007307 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 nextchar = *++p;
7309 is_term_option = TRUE;
7310 expand_option_name[2] = p[-2];
7311 expand_option_name[3] = p[-1];
7312 }
7313 else
7314 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007315 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7317 p++;
7318 if (*p == NUL)
7319 return;
7320 nextchar = *p;
7321 *p = NUL;
7322 opt_idx = findoption(arg);
7323 *p = nextchar;
7324 if (opt_idx == -1 || options[opt_idx].var == NULL)
7325 {
7326 xp->xp_context = EXPAND_NOTHING;
7327 return;
7328 }
7329 flags = options[opt_idx].flags;
7330 if (flags & P_BOOL)
7331 {
7332 xp->xp_context = EXPAND_NOTHING;
7333 return;
7334 }
7335 }
7336 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007337 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7339 {
7340 ++p;
7341 nextchar = '=';
7342 }
7343 if ((nextchar != '=' && nextchar != ':')
7344 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7345 {
7346 xp->xp_context = EXPAND_UNSUCCESSFUL;
7347 return;
7348 }
7349 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7350 {
7351 xp->xp_context = EXPAND_OLD_SETTING;
7352 if (is_term_option)
7353 expand_option_idx = -1;
7354 else
7355 expand_option_idx = opt_idx;
7356 xp->xp_pattern = p + 1;
7357 return;
7358 }
7359 xp->xp_context = EXPAND_NOTHING;
7360 if (is_term_option || (flags & P_NUM))
7361 return;
7362
7363 xp->xp_pattern = p + 1;
7364
7365 if (flags & P_EXPAND)
7366 {
7367 p = options[opt_idx].var;
7368 if (p == (char_u *)&p_bdir
7369 || p == (char_u *)&p_dir
7370 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007371 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374#ifdef FEAT_SESSION
7375 || p == (char_u *)&p_vdir
7376#endif
7377 )
7378 {
7379 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007380 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 xp->xp_backslash = XP_BS_THREE;
7382 else
7383 xp->xp_backslash = XP_BS_ONE;
7384 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007385 else if (p == (char_u *)&p_ft)
7386 {
7387 xp->xp_context = EXPAND_FILETYPE;
7388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 else
7390 {
7391 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007392 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 if (p == (char_u *)&p_tags)
7394 xp->xp_backslash = XP_BS_THREE;
7395 else
7396 xp->xp_backslash = XP_BS_ONE;
7397 }
7398 }
7399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007400 // For an option that is a list of file names, find the start of the
7401 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7403 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007404 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if (*p == ' ' || *p == ',')
7406 {
7407 s = p;
7408 while (s > xp->xp_pattern && *(s - 1) == '\\')
7409 --s;
7410 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7411 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7412 {
7413 xp->xp_pattern = p + 1;
7414 break;
7415 }
7416 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007417
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007418#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007419 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007420 if (options[opt_idx].var == (char_u *)&p_sps
7421 && STRNCMP(p, "file:", 5) == 0)
7422 {
7423 xp->xp_pattern = p + 5;
7424 break;
7425 }
7426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007430/*
7431 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7432 *
7433 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7434 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7435 *
7436 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7437 * regular expression 'regmatch', then stores the match in matches[idx] and
7438 * returns TRUE.
7439 *
7440 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7441 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7442 *
7443 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7444 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7445 */
7446 static int
7447match_str(
7448 char_u *str,
7449 regmatch_T *regmatch,
7450 char_u **matches,
7451 int idx,
7452 int test_only,
7453 int fuzzy,
7454 char_u *fuzzystr,
7455 fuzmatch_str_T *fuzmatch)
7456{
7457 if (!fuzzy)
7458 {
7459 if (vim_regexec(regmatch, str, (colnr_T)0))
7460 {
7461 if (!test_only)
7462 matches[idx] = vim_strsave(str);
7463 return TRUE;
7464 }
7465 }
7466 else
7467 {
7468 int score;
7469
7470 score = fuzzy_match_str(str, fuzzystr);
7471 if (score != 0)
7472 {
7473 if (!test_only)
7474 {
7475 fuzmatch[idx].idx = idx;
7476 fuzmatch[idx].str = vim_strsave(str);
7477 fuzmatch[idx].score = score;
7478 }
7479
7480 return TRUE;
7481 }
7482 }
7483
7484 return FALSE;
7485}
7486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007488ExpandSettings(
7489 expand_T *xp,
7490 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007491 char_u *fuzzystr,
7492 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007493 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007494 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007496 int num_normal = 0; // Nr of matching non-term-code settings
7497 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 int opt_idx;
7499 int match;
7500 int count = 0;
7501 char_u *str;
7502 int loop;
7503 int is_term_opt;
7504 char_u name_buf[MAX_KEY_NAME_LEN];
7505 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007506 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007507 int fuzzy;
7508 fuzmatch_str_T *fuzmatch = NULL;
7509
Christian Brabandtcb747892022-05-08 21:10:56 +01007510 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007512 // do this loop twice:
7513 // loop == 0: count the number of matching options
7514 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 for (loop = 0; loop <= 1; ++loop)
7516 {
7517 regmatch->rm_ic = ic;
7518 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7519 {
K.Takataeeec2542021-06-02 13:28:16 +02007520 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007521 {
7522 if (match_str((char_u *)names[match], regmatch, *matches,
7523 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
7525 if (loop == 0)
7526 num_normal++;
7527 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007528 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 }
7532 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7533 opt_idx++)
7534 {
7535 if (options[opt_idx].var == NULL)
7536 continue;
7537 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7538 && !(options[opt_idx].flags & P_BOOL))
7539 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007540 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 if (is_term_opt && num_normal > 0)
7542 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007543
7544 if (match_str(str, regmatch, *matches, count, (loop == 0),
7545 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {
7547 if (loop == 0)
7548 {
7549 if (is_term_opt)
7550 num_term++;
7551 else
7552 num_normal++;
7553 }
7554 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007555 count++;
7556 }
7557 else if (!fuzzy && options[opt_idx].shortname != NULL
7558 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007559 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007560 {
7561 // Compare against the abbreviated option name (for regular
7562 // expression match). Fuzzy matching (previous if) already
7563 // matches against both the expanded and abbreviated names.
7564 if (loop == 0)
7565 {
7566 if (is_term_opt)
7567 num_term++;
7568 else
7569 num_normal++;
7570 }
7571 else
7572 (*matches)[count++] = vim_strsave(str);
7573 }
7574 else if (is_term_opt)
7575 {
7576 name_buf[0] = '<';
7577 name_buf[1] = 't';
7578 name_buf[2] = '_';
7579 name_buf[3] = str[2];
7580 name_buf[4] = str[3];
7581 name_buf[5] = '>';
7582 name_buf[6] = NUL;
7583
7584 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7585 fuzzy, fuzzystr, fuzmatch))
7586 {
7587 if (loop == 0)
7588 num_term++;
7589 else
7590 count++;
7591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
7593 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007594
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007595 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7597 {
7598 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7599 {
7600 if (!isprint(str[0]) || !isprint(str[1]))
7601 continue;
7602
7603 name_buf[0] = 't';
7604 name_buf[1] = '_';
7605 name_buf[2] = str[0];
7606 name_buf[3] = str[1];
7607 name_buf[4] = NUL;
7608
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007609 if (match_str(name_buf, regmatch, *matches, count,
7610 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7611 {
7612 if (loop == 0)
7613 num_term++;
7614 else
7615 count++;
7616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 else
7618 {
7619 name_buf[0] = '<';
7620 name_buf[1] = 't';
7621 name_buf[2] = '_';
7622 name_buf[3] = str[0];
7623 name_buf[4] = str[1];
7624 name_buf[5] = '>';
7625 name_buf[6] = NUL;
7626
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007627 if (match_str(name_buf, regmatch, *matches, count,
7628 (loop == 0), fuzzy, fuzzystr,
7629 fuzmatch))
7630 {
7631 if (loop == 0)
7632 num_term++;
7633 else
7634 count++;
7635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 }
7637 }
7638
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007639 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007640 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7642 {
7643 name_buf[0] = '<';
7644 STRCPY(name_buf + 1, str);
7645 STRCAT(name_buf, ">");
7646
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007647 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7648 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {
7650 if (loop == 0)
7651 num_term++;
7652 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007653 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655 }
7656 }
7657 if (loop == 0)
7658 {
7659 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007660 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007662 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 else
7664 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007665 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007667 *matches = ALLOC_MULT(char_u *, *numMatches);
7668 if (*matches == NULL)
7669 {
7670 *matches = (char_u **)"";
7671 return FAIL;
7672 }
7673 }
7674 else
7675 {
7676 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7677 if (fuzmatch == NULL)
7678 {
7679 *matches = (char_u **)"";
7680 return FAIL;
7681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
7683 }
7684 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007685
7686 if (fuzzy &&
7687 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7688 return FAIL;
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 return OK;
7691}
7692
7693 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007694ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007696 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 char_u *buf;
7698
zeertzjqb0d45ec2023-01-25 15:04:22 +00007699 *numMatches = 0;
7700 *matches = ALLOC_ONE(char_u *);
7701 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 return FAIL;
7703
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007704 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (expand_option_idx < 0)
7706 {
7707 var = find_termcode(expand_option_name + 2);
7708 if (var == NULL)
7709 expand_option_idx = findoption(expand_option_name);
7710 }
7711
7712 if (expand_option_idx >= 0)
7713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007714 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 option_value2string(&options[expand_option_idx], expand_option_flags);
7716 var = NameBuff;
7717 }
7718 else if (var == NULL)
7719 var = (char_u *)"";
7720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007721 // A backslash is required before some characters. This is the reverse of
7722 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 buf = vim_strsave_escaped(var, escape_chars);
7724
7725 if (buf == NULL)
7726 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007727 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 return FAIL;
7729 }
7730
7731#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007732 // For MS-Windows et al. we don't double backslashes at the start and
7733 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007734 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (var[0] == '\\' && var[1] == '\\'
7736 && expand_option_idx >= 0
7737 && (options[expand_option_idx].flags & P_EXPAND)
7738 && vim_isfilec(var[2])
7739 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007740 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741#endif
7742
zeertzjqb0d45ec2023-01-25 15:04:22 +00007743 *matches[0] = buf;
7744 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 return OK;
7746}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747
7748/*
7749 * Get the value for the numeric or string option *opp in a nice format into
7750 * NameBuff[]. Must not be called with a hidden option!
7751 */
7752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007753option_value2string(
7754 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007755 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 char_u *varp;
7758
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007759 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
7761 if (opp->flags & P_NUM)
7762 {
7763 long wc = 0;
7764
7765 if (wc_use_keyname(varp, &wc))
7766 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7767 else if (wc != 0)
7768 STRCPY(NameBuff, transchar((int)wc));
7769 else
7770 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7771 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007772 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {
7774 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007775 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 NameBuff[0] = NUL;
7777#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007778 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007779 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 STRCPY(NameBuff, "*****");
7781#endif
7782 else if (opp->flags & P_EXPAND)
7783 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007784 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 else if ((char_u **)opp->var == &p_pt)
7786 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7787 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007788 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
7790}
7791
7792/*
7793 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7794 * printed as a keyname.
7795 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7796 */
7797 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007798wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7801 {
7802 *wcp = *(long *)varp;
7803 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7804 return TRUE;
7805 }
7806 return FALSE;
7807}
7808
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 * Return TRUE if "x" is present in 'shortmess' option, or
7811 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7812 */
7813 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007814shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007816 return p_shm != NULL &&
7817 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 || (vim_strchr(p_shm, 'a') != NULL
7819 && vim_strchr((char_u *)SHM_A, x) != NULL));
7820}
7821
7822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7824 *
7825 * Reset 'compatible' and set the values for options that didn't get set yet
7826 * to the Vim defaults.
7827 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007828 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 */
7830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007831vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007833 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007834 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007835 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 if (!option_was_set((char_u *)"cp"))
7838 {
7839 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007840 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7842 set_option_default(opt_idx, OPT_FREE, FALSE);
7843 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007844 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007846
7847 if (fname != NULL)
7848 {
7849 p = vim_getenv(envname, &dofree);
7850 if (p == NULL)
7851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007852 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007853 p = FullName_save(fname, FALSE);
7854 if (p != NULL)
7855 {
7856 vim_setenv(envname, p);
7857 vim_free(p);
7858 }
7859 }
7860 else if (dofree)
7861 vim_free(p);
7862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
7865/*
7866 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7867 */
7868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007869change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007871 int opt_idx;
7872
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (p_cp != on)
7874 {
7875 p_cp = on;
7876 compatible_set();
7877 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007878 opt_idx = findoption((char_u *)"cp");
7879 if (opt_idx >= 0)
7880 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881}
7882
7883/*
7884 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007885 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 */
7887 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007888option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 int idx;
7891
7892 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007893 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 return FALSE;
7895 if (options[idx].flags & P_WAS_SET)
7896 return TRUE;
7897 return FALSE;
7898}
7899
7900/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007901 * Reset the flag indicating option "name" was set.
7902 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007903 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007904reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007905{
7906 int idx = findoption(name);
7907
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007908 if (idx < 0)
7909 return FAIL;
7910
7911 options[idx].flags &= ~P_WAS_SET;
7912 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007913}
7914
7915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 * compatible_set() - Called when 'compatible' has been set or unset.
7917 *
7918 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7919 * flag) to a Vi compatible value.
7920 * When 'compatible' is unset: Set all options that have a different default
7921 * for Vim (without the P_VI_DEF flag) to that default.
7922 */
7923 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007924compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
7926 int opt_idx;
7927
Bram Moolenaardac13472019-09-16 21:06:21 +02007928 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7930 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7931 set_option_default(opt_idx, OPT_FREE, p_cp);
7932 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007933 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934}
7935
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 * Check if backspacing over something is allowed.
7938 */
7939 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007940can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007941 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007943#ifdef FEAT_JOB_CHANNEL
7944 if (what == BS_START && bt_prompt(curbuf))
7945 return FALSE;
7946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 switch (*p_bs)
7948 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007949 case '3': return TRUE;
7950 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 case '1': return (what != BS_START);
7952 case '0': return FALSE;
7953 }
7954 return vim_strchr(p_bs, what) != NULL;
7955}
7956
7957/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007958 * Return the effective 'scrolloff' value for the current window, using the
7959 * global value when appropriate.
7960 */
7961 long
7962get_scrolloff_value(void)
7963{
7964 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7965}
7966
7967/*
7968 * Return the effective 'sidescrolloff' value for the current window, using the
7969 * global value when appropriate.
7970 */
7971 long
7972get_sidescrolloff_value(void)
7973{
7974 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7975}
7976
7977/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007978 * Get the local or global value of 'backupcopy'.
7979 */
7980 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007981get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007982{
7983 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7984}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007985
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007986#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007987/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007988 * Get the local or global value of 'formatlistpat'.
7989 */
7990 char_u *
7991get_flp_value(buf_T *buf)
7992{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007993 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7994 return p_flp;
7995 return buf->b_p_flp;
7996}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007997#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007998
7999/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008000 * Get the local or global value of the 'virtualedit' flags.
8001 */
8002 unsigned int
8003get_ve_flags(void)
8004{
Gary Johnson51ad8502021-08-03 18:33:08 +02008005 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8006 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008007}
8008
Bram Moolenaaree857022019-11-09 23:26:40 +01008009#if defined(FEAT_LINEBREAK) || defined(PROTO)
8010/*
8011 * Get the local or global value of 'showbreak'.
8012 */
8013 char_u *
8014get_showbreak_value(win_T *win)
8015{
8016 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8017 return p_sbr;
8018 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8019 return empty_option;
8020 return win->w_p_sbr;
8021}
8022#endif
8023
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008024#if defined(FEAT_EVAL) || defined(PROTO)
8025/*
8026 * Get window or buffer local options.
8027 */
8028 dict_T *
8029get_winbuf_options(int bufopt)
8030{
8031 dict_T *d;
8032 int opt_idx;
8033
8034 d = dict_alloc();
8035 if (d == NULL)
8036 return NULL;
8037
Bram Moolenaardac13472019-09-16 21:06:21 +02008038 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008039 {
8040 struct vimoption *opt = &options[opt_idx];
8041
8042 if ((bufopt && (opt->indir & PV_BUF))
8043 || (!bufopt && (opt->indir & PV_WIN)))
8044 {
8045 char_u *varp = get_varp(opt);
8046
8047 if (varp != NULL)
8048 {
8049 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008050 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008051 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008052 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008053 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008054 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008055 }
8056 }
8057 }
8058
8059 return d;
8060}
8061#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008062
Bram Moolenaardac13472019-09-16 21:06:21 +02008063#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008064/*
8065 * This is called when 'culopt' is changed
8066 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008067 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008068fill_culopt_flags(char_u *val, win_T *wp)
8069{
8070 char_u *p;
8071 char_u culopt_flags_new = 0;
8072
8073 if (val == NULL)
8074 p = wp->w_p_culopt;
8075 else
8076 p = val;
8077 while (*p != NUL)
8078 {
8079 if (STRNCMP(p, "line", 4) == 0)
8080 {
8081 p += 4;
8082 culopt_flags_new |= CULOPT_LINE;
8083 }
8084 else if (STRNCMP(p, "both", 4) == 0)
8085 {
8086 p += 4;
8087 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8088 }
8089 else if (STRNCMP(p, "number", 6) == 0)
8090 {
8091 p += 6;
8092 culopt_flags_new |= CULOPT_NBR;
8093 }
8094 else if (STRNCMP(p, "screenline", 10) == 0)
8095 {
8096 p += 10;
8097 culopt_flags_new |= CULOPT_SCRLINE;
8098 }
8099
8100 if (*p != ',' && *p != NUL)
8101 return FAIL;
8102 if (*p == ',')
8103 ++p;
8104 }
8105
8106 // Can't have both "line" and "screenline".
8107 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8108 return FAIL;
8109 wp->w_p_culopt_flags = culopt_flags_new;
8110
8111 return OK;
8112}
8113#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008114
8115/*
8116 * Get the value of 'magic' adjusted for Vim9 script.
8117 */
8118 int
8119magic_isset(void)
8120{
8121 switch (magic_overruled)
8122 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008123 case OPTION_MAGIC_ON: return TRUE;
8124 case OPTION_MAGIC_OFF: return FALSE;
8125 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008126 }
8127#ifdef FEAT_EVAL
8128 if (in_vim9script())
8129 return TRUE;
8130#endif
8131 return p_magic;
8132}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008133
8134/*
8135 * Set the callback function value for an option that accepts a function name,
8136 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8137 * Returns OK if the option is successfully set to a function, otherwise
8138 * returns FAIL.
8139 */
8140 int
8141option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8142{
8143#ifdef FEAT_EVAL
8144 typval_T *tv;
8145 callback_T cb;
8146
8147 if (optval == NULL || *optval == NUL)
8148 {
8149 free_callback(optcb);
8150 return OK;
8151 }
8152
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008153 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008154 || (STRNCMP(optval, "function(", 9) == 0)
8155 || (STRNCMP(optval, "funcref(", 8) == 0))
8156 // Lambda expression or a funcref
8157 tv = eval_expr(optval, NULL);
8158 else
8159 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008160 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008161 if (tv == NULL)
8162 return FAIL;
8163
8164 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008165 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008166 {
8167 free_tv(tv);
8168 return FAIL;
8169 }
8170
8171 free_callback(optcb);
8172 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008173 if (cb.cb_free_name)
8174 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008175 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008176
8177 // when using Vim9 style "import.funcname" it needs to be expanded to
8178 // "import#funcname".
8179 expand_autload_callback(optcb);
8180
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008181 return OK;
8182#else
8183 return FAIL;
8184#endif
8185}