blob: a2ab5c66635e9ac815db3b5eadf1092e0060dfa7 [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/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005249 * Returns TRUE if the option at 'opt_idx' is a global option
5250 */
5251 int
5252is_global_option(int opt_idx)
5253{
5254 return options[opt_idx].indir == PV_NONE;
5255}
5256
5257/*
5258 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5259 * local value.
5260 */
5261 int
5262is_global_local_option(int opt_idx)
5263{
5264 return options[opt_idx].indir & PV_BOTH;
5265}
5266
5267/*
5268 * Returns TRUE if the option at 'opt_idx' is a window-local option
5269 */
5270 int
5271is_window_local_option(int opt_idx)
5272{
5273 return options[opt_idx].var == VAR_WIN;
5274}
5275
5276/*
5277 * Returns TRUE if the option at 'opt_idx' is a hidden option
5278 */
5279 int
5280is_hidden_option(int opt_idx)
5281{
5282 return options[opt_idx].var == NULL;
5283}
5284
5285#if defined(FEAT_CRYPT) || defined(PROTO)
5286/*
5287 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5288 */
5289 int
5290is_crypt_key_option(int opt_idx)
5291{
5292 return options[opt_idx].indir == PV_KEY;
5293}
5294#endif
5295
5296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 * Set the value of option "name".
5298 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005299 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005300 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005302 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005303set_option_value(
5304 char_u *name,
5305 long number,
5306 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 int opt_idx;
5310 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005311 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005312 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005315 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005316 {
5317 int key;
5318
5319 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005320 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005321 {
5322 char_u key_name[2];
5323
5324 if (key < 0)
5325 {
5326 key_name[0] = KEY2TERMCAP0(key);
5327 key_name[1] = KEY2TERMCAP1(key);
5328 }
5329 else
5330 {
5331 key_name[0] = KS_KEY;
5332 key_name[1] = (key & 0xff);
5333 }
5334 add_termcode(key_name, string, FALSE);
5335 if (full_screen)
5336 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005337 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005338 return NULL;
5339 }
5340
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005341 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 else
5344 {
5345 flags = options[opt_idx].flags;
5346#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005347 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005349 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005350 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005351 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005354 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005355 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005356
5357 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5358 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005360 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005362 int idx;
5363
5364 // Either we are given a string or we are setting option
5365 // to zero.
5366 for (idx = 0; string[idx] == '0'; ++idx)
5367 ;
5368 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005369 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005370 // There's another character after zeros or the string
5371 // is empty. In both cases, we are trying to set a
5372 // num option using a string.
5373 semsg(_(e_number_required_after_str_equal_str),
5374 name, string);
5375 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005376
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005379 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005380 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005381 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005382 errbuf, sizeof(errbuf), opt_flags);
5383 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005384 else
5385 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005387
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005389 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390}
5391
5392/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005393 * Call set_option_value() and when an error is returned report it.
5394 */
5395 void
5396set_option_value_give_err(
5397 char_u *name,
5398 long number,
5399 char_u *string,
5400 int opt_flags) // OPT_LOCAL or 0 (both)
5401{
5402 char *errmsg = set_option_value(name, number, string, opt_flags);
5403
5404 if (errmsg != NULL)
5405 emsg(_(errmsg));
5406}
5407
5408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 * Get the terminal code for a terminal option.
5410 * Returns NULL when not found.
5411 */
5412 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005413get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 int opt_idx;
5416 char_u *varp;
5417
5418 if (tname[0] != 't' || tname[1] != '_' ||
5419 tname[2] == NUL || tname[3] == NUL)
5420 return NULL;
5421 if ((opt_idx = findoption(tname)) >= 0)
5422 {
5423 varp = get_varp(&(options[opt_idx]));
5424 if (varp != NULL)
5425 varp = *(char_u **)(varp);
5426 return varp;
5427 }
5428 return find_termcode(tname + 2);
5429}
5430
5431 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005432get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
5434 int i;
5435
5436 i = findoption((char_u *)"hl");
5437 if (i >= 0)
5438 return options[i].def_val[VI_DEFAULT];
5439 return (char_u *)NULL;
5440}
5441
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005442 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005443get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005444{
5445 int i;
5446
5447 i = findoption((char_u *)"enc");
5448 if (i >= 0)
5449 return options[i].def_val[VI_DEFAULT];
5450 return (char_u *)NULL;
5451}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005452
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005453#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005454 int
5455is_option_allocated(char *name)
5456{
5457 int idx = findoption((char_u *)name);
5458
5459 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5460}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005461#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005462
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005463#if defined(FEAT_EVAL) || defined(PROTO)
5464/*
5465 * Return TRUE if "name" is a string option.
5466 * Returns FALSE if option "name" does not exist.
5467 */
5468 int
5469is_string_option(char_u *name)
5470{
5471 int idx = findoption(name);
5472
5473 return idx >= 0 && (options[idx].flags & P_STRING);
5474}
5475#endif
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477/*
5478 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005479 * When "has_lt" is true there is a '<' before "*arg_arg".
5480 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 */
5482 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005483find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005485 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005487 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005489 // Don't use get_special_key_code() for t_xx, we don't want it to call
5490 // add_termcap_entry().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5492 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005493 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005495 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005497 key = find_special_key(&arg, &modifiers,
5498 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005499 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 key = 0;
5501 }
5502 return key;
5503}
5504
5505/*
5506 * if 'all' == 0: show changed options
5507 * if 'all' == 1: show all normal options
5508 * if 'all' == 2: show all terminal options
5509 */
5510 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005511showoptions(
5512 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005513 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 struct vimoption *p;
5516 int col;
5517 int isterm;
5518 char_u *varp;
5519 struct vimoption **items;
5520 int item_count;
5521 int run;
5522 int row, rows;
5523 int cols;
5524 int i;
5525 int len;
5526
5527#define INC 20
5528#define GAP 3
5529
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005530 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 if (items == NULL)
5532 return;
5533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005534 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005536 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005538 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005540 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005542 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005544 // Do the loop two times:
5545 // 1. display the short items
5546 // 2. display the long items (only strings and numbers)
5547 // When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 for (run = 1; run <= 2 && !got_int; ++run)
5549 {
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005550 // collect the items in items[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 item_count = 0;
5552 for (p = &options[0]; p->fullname != NULL; p++)
5553 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005554 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005555 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005556 continue;
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 varp = NULL;
5559 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005560 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 if (p->indir != PV_NONE && !isterm)
5563 varp = get_varp_scope(p, opt_flags);
5564 }
5565 else
5566 varp = get_varp(p);
5567 if (varp != NULL
5568 && ((all == 2 && isterm)
5569 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005570 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005572 if (opt_flags & OPT_ONECOLUMN)
5573 len = Columns;
5574 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 else
5577 {
5578 option_value2string(p, opt_flags);
5579 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5580 }
5581 if ((len <= INC - GAP && run == 1) ||
5582 (len > INC - GAP && run == 2))
5583 items[item_count++] = p;
5584 }
5585 }
5586
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005587 // display the items
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (run == 1)
5589 {
5590 cols = (Columns + GAP - 3) / INC;
5591 if (cols == 0)
5592 cols = 1;
5593 rows = (item_count + cols - 1) / cols;
5594 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005595 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 rows = item_count;
5597 for (row = 0; row < rows && !got_int; ++row)
5598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 msg_putchar('\n'); // go to next line
5600 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 break;
5602 col = 0;
5603 for (i = row; i < item_count; i += rows)
5604 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005605 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 showoneopt(items[i], opt_flags);
5607 col += INC;
5608 }
5609 out_flush();
5610 ui_breakcheck();
5611 }
5612 }
5613 vim_free(items);
5614}
5615
5616/*
5617 * Return TRUE if option "p" has its default value.
5618 */
5619 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005620optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 int dvi;
5623
5624 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005625 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005626 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005628 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 // the cast to long is required for Manx C, long_i is
5631 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005632 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5635}
5636
5637/*
5638 * showoneopt: show the value of one option
5639 * must not be called with a hidden option!
5640 */
5641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005642showoneopt(
5643 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005644 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005646 char_u *varp;
5647 int save_silent = silent_mode;
5648
5649 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005650 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652 varp = get_varp_scope(p, opt_flags);
5653
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005654 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5656 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005657 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005659 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005661 msg_puts(" ");
5662 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 if (!(p->flags & P_BOOL))
5664 {
5665 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005666 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 option_value2string(p, opt_flags);
5668 msg_outtrans(NameBuff);
5669 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005670
5671 silent_mode = save_silent;
5672 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673}
5674
5675/*
5676 * Write modified options as ":set" commands to a file.
5677 *
5678 * There are three values for "opt_flags":
5679 * OPT_GLOBAL: Write global option values and fresh values of
5680 * buffer-local options (used for start of a session
5681 * file).
5682 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5683 * curwin (used for a vimrc file).
5684 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5685 * and local values for window-local options of
5686 * curwin. Local values are also written when at the
5687 * default value, because a modeline or autocommand
5688 * may have set them when doing ":edit file" and the
5689 * user has set them back at the default or fresh
5690 * value.
5691 * When "local_only" is TRUE, don't write fresh
5692 * values, only local values (for ":mkview").
5693 * (fresh value = value used for a new buffer or window for a local option).
5694 *
5695 * Return FAIL on error, OK otherwise.
5696 */
5697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005698makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005701 char_u *varp; // currently used value
5702 char_u *varp_fresh; // local value
5703 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 char *cmd;
5705 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005706 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
5708 /*
5709 * The options that don't have a default (terminal name, columns, lines)
5710 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005711 * Do the loop over "options[]" twice: once for options with the
5712 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005714 for (pri = 1; pri >= 0; --pri)
5715 {
5716 for (p = &options[0]; !istermoption(p); p++)
5717 if (!(p->flags & P_NO_MKRC)
5718 && !istermoption(p)
5719 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005721 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5723 continue;
5724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005725 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5726 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5728 continue;
5729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005730 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005732 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 continue;
5734
Bram Moolenaard23b7142021-04-17 21:04:34 +02005735 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5736 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005737 continue;
5738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 round = 2;
5740 if (p->indir != PV_NONE)
5741 {
5742 if (p->var == VAR_WIN)
5743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005744 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if (!(opt_flags & OPT_LOCAL))
5746 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005747 // When fresh value of window-local option is not at the
5748 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5750 {
5751 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005752 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 round = 1;
5755 varp_local = varp;
5756 varp = varp_fresh;
5757 }
5758 }
5759 }
5760 }
5761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005762 // Round 1: fresh value for window-local options.
5763 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 for ( ; round <= 2; varp = varp_local, ++round)
5765 {
5766 if (round == 1 || (opt_flags & OPT_GLOBAL))
5767 cmd = "set";
5768 else
5769 cmd = "setlocal";
5770
5771 if (p->flags & P_BOOL)
5772 {
5773 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5774 return FAIL;
5775 }
5776 else if (p->flags & P_NUM)
5777 {
5778 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5779 return FAIL;
5780 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005781 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005783 int do_endif = FALSE;
5784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005785 // Don't set 'syntax' and 'filetype' again if the value is
5786 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005787 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005788#if defined(FEAT_SYN_HL)
5789 p->indir == PV_SYN ||
5790#endif
5791 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
5793 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5794 *(char_u **)(varp)) < 0
5795 || put_eol(fd) < 0)
5796 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005797 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
5799 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005800 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005802 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
5804 if (put_line(fd, "endif") == FAIL)
5805 return FAIL;
5806 }
5807 }
5808 }
5809 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 return OK;
5812}
5813
5814#if defined(FEAT_FOLDING) || defined(PROTO)
5815/*
5816 * Generate set commands for the local fold options only. Used when
5817 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5818 */
5819 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005820makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005822 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005824 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 == FAIL
5826# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005827 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005829 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 == FAIL
5831 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5832 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5833 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5834 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5835 )
5836 return FAIL;
5837
5838 return OK;
5839}
5840#endif
5841
5842 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005843put_setstring(
5844 FILE *fd,
5845 char *cmd,
5846 char *name,
5847 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005848 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005851 char_u *buf = NULL;
5852 char_u *part = NULL;
5853 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
5855 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5856 return FAIL;
5857 if (*valuep != NULL)
5858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005859 // Output 'pastetoggle' as key names. For other
5860 // options some characters have to be escaped with
5861 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (valuep == &p_pt)
5863 {
5864 s = *valuep;
5865 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005866 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 return FAIL;
5868 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005869 // expand the option value, replace $HOME by ~
5870 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005872 int size = (int)STRLEN(*valuep) + 1;
5873
5874 // replace home directory in the whole option value into "buf"
5875 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005876 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005877 goto fail;
5878 home_replace(NULL, *valuep, buf, size, FALSE);
5879
5880 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005881 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005882 // can be expanded when read back.
5883 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5884 && vim_strchr(*valuep, ',') != NULL)
5885 {
5886 part = alloc(size);
5887 if (part == NULL)
5888 goto fail;
5889
5890 // write line break to clear the option, e.g. ':set rtp='
5891 if (put_eol(fd) == FAIL)
5892 goto fail;
5893
5894 p = buf;
5895 while (*p != NUL)
5896 {
5897 // for each comma separated option part, append value to
5898 // the option, :set rtp+=value
5899 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5900 goto fail;
5901 (void)copy_option_part(&p, part, size, ",");
5902 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5903 goto fail;
5904 }
5905 vim_free(buf);
5906 vim_free(part);
5907 return OK;
5908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005910 {
5911 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005913 }
5914 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
5916 else if (put_escstr(fd, *valuep, 2) == FAIL)
5917 return FAIL;
5918 }
5919 if (put_eol(fd) < 0)
5920 return FAIL;
5921 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005922fail:
5923 vim_free(buf);
5924 vim_free(part);
5925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926}
5927
5928 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005929put_setnum(
5930 FILE *fd,
5931 char *cmd,
5932 char *name,
5933 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 long wc;
5936
5937 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5938 return FAIL;
5939 if (wc_use_keyname((char_u *)valuep, &wc))
5940 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005941 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5943 return FAIL;
5944 }
5945 else if (fprintf(fd, "%ld", *valuep) < 0)
5946 return FAIL;
5947 if (put_eol(fd) < 0)
5948 return FAIL;
5949 return OK;
5950}
5951
5952 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005953put_setbool(
5954 FILE *fd,
5955 char *cmd,
5956 char *name,
5957 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005959 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005960 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5962 || put_eol(fd) < 0)
5963 return FAIL;
5964 return OK;
5965}
5966
5967/*
5968 * Clear all the terminal options.
5969 * If the option has been allocated, free the memory.
5970 * Terminal options are never hidden or indirect.
5971 */
5972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005973clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00005975 // Reset a few things before clearing the old options. This may cause
5976 // outputting a few things that the terminal doesn't understand, but the
5977 // screen will be cleared later, so this is OK.
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005978 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005979 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005981 // When starting the GUI close the display opened for the clipboard.
5982 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 if (gui.starting)
5984 clear_xterm_clip();
5985#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005986 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005988 free_termoptions();
5989}
5990
5991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005992free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005993{
5994 struct vimoption *p;
5995
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005996 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (istermoption(p))
5998 {
5999 if (p->flags & P_ALLOCED)
6000 free_string_option(*(char_u **)(p->var));
6001 if (p->flags & P_DEF_ALLOCED)
6002 free_string_option(p->def_val[VI_DEFAULT]);
6003 *(char_u **)(p->var) = empty_option;
6004 p->def_val[VI_DEFAULT] = empty_option;
6005 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02006006#ifdef FEAT_EVAL
6007 // remember where the option was cleared
6008 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
6009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 }
6011 clear_termcodes();
6012}
6013
6014/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00006015 * Free the string for one term option, if it was allocated.
6016 * Set the string to empty_option and clear allocated flag.
6017 * "var" points to the option value.
6018 */
6019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006020free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00006021{
6022 struct vimoption *p;
6023
6024 for (p = &options[0]; p->fullname != NULL; p++)
6025 if (p->var == var)
6026 {
6027 if (p->flags & P_ALLOCED)
6028 free_string_option(*(char_u **)(p->var));
6029 *(char_u **)(p->var) = empty_option;
6030 p->flags &= ~P_ALLOCED;
6031 break;
6032 }
6033}
6034
6035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 * Set the terminal option defaults to the current value.
6037 * Used after setting the terminal name.
6038 */
6039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006040set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 struct vimoption *p;
6043
6044 for (p = &options[0]; p->fullname != NULL; p++)
6045 {
6046 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6047 {
6048 if (p->flags & P_DEF_ALLOCED)
6049 {
6050 free_string_option(p->def_val[VI_DEFAULT]);
6051 p->flags &= ~P_DEF_ALLOCED;
6052 }
6053 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6054 if (p->flags & P_ALLOCED)
6055 {
6056 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006057 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 }
6059 }
6060 }
6061}
6062
6063/*
6064 * return TRUE if 'p' starts with 't_'
6065 */
6066 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006067istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6070}
6071
Bram Moolenaardac13472019-09-16 21:06:21 +02006072/*
6073 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6074 */
6075 int
6076istermoption_idx(int opt_idx)
6077{
6078 return istermoption(&options[opt_idx]);
6079}
6080
Bram Moolenaar113e1072019-01-20 15:30:40 +01006081#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006083 * Unset local option value, similar to ":set opt<".
6084 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006085 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006086unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006087{
6088 struct vimoption *p;
6089 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006090 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006091
6092 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006093 if (opt_idx < 0)
6094 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006095 p = &(options[opt_idx]);
6096
6097 switch ((int)p->indir)
6098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006099 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006100 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006101 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006102 break;
6103 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006104 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006105 break;
6106 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006107 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006108 break;
6109 case PV_AR:
6110 buf->b_p_ar = -1;
6111 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006112 case PV_BKC:
6113 clear_string_option(&buf->b_p_bkc);
6114 buf->b_bkc_flags = 0;
6115 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006116 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006117 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006118 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006119 case PV_TC:
6120 clear_string_option(&buf->b_p_tc);
6121 buf->b_tc_flags = 0;
6122 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006123 case PV_SISO:
6124 curwin->w_p_siso = -1;
6125 break;
6126 case PV_SO:
6127 curwin->w_p_so = -1;
6128 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006129# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006130 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006131 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006132 break;
6133 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006134 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006135 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006136# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006137 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006138 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006139 break;
6140 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006141 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006142 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006143# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006144 case PV_TSRFU:
6145 clear_string_option(&buf->b_p_tsrfu);
6146 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006147# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006148 case PV_FP:
6149 clear_string_option(&buf->b_p_fp);
6150 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006151# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006152 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006153 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006154 break;
6155 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006156 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006157 break;
6158 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006159 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006160 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006161# endif
6162# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006163 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006164 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006165 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006166# endif
6167# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006168 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006169 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006170 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006171# endif
6172# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006173 case PV_SBR:
6174 clear_string_option(&((win_T *)from)->w_p_sbr);
6175 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006176# endif
6177# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006178 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006179 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006180 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006181# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006182 case PV_UL:
6183 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6184 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006185 case PV_LW:
6186 clear_string_option(&buf->b_p_lw);
6187 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006188 case PV_MENC:
6189 clear_string_option(&buf->b_p_menc);
6190 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006191 case PV_LCS:
6192 clear_string_option(&((win_T *)from)->w_p_lcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006193 set_listchars_option((win_T *)from, ((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006194 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006195 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006196 case PV_FCS:
6197 clear_string_option(&((win_T *)from)->w_p_fcs);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006198 set_fillchars_option((win_T *)from, ((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006199 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006200 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006201 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006202 clear_string_option(&((win_T *)from)->w_p_ve);
6203 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006204 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006205 }
6206}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006207#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006208
6209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006211 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 */
6213 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006214get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006216 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {
6218 if (p->var == VAR_WIN)
6219 return (char_u *)GLOBAL_WO(get_varp(p));
6220 return p->var;
6221 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006222 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 {
6224 switch ((int)p->indir)
6225 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006226 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006228 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6229 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6230 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006232 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6233 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6234 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006235 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006236 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006237 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006238 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6239 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006241 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6242 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006244 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6245 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006246#ifdef FEAT_COMPL_FUNC
6247 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6248#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006249#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6250 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6251#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006252#if defined(FEAT_CRYPT)
6253 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6254#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006255#ifdef FEAT_LINEBREAK
6256 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6257#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006258#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006259 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006260#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006261 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006262 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006263 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006264 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006265 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006266 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006267 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006268
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006270 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
6272 return get_varp(p);
6273}
6274
6275/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006276 * Get pointer to option variable at 'opt_idx', depending on local or global
6277 * scope.
6278 */
6279 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006280get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006281{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006282 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006283}
6284
6285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 * Get pointer to option variable.
6287 */
6288 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006289get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006291 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (p->var == NULL)
6293 return NULL;
6294
6295 switch ((int)p->indir)
6296 {
6297 case PV_NONE: return p->var;
6298
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006299 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006300 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006302 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006304 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006306 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006308 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006310 case PV_TC: return *curbuf->b_p_tc != NUL
6311 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006312 case PV_BKC: return *curbuf->b_p_bkc != NUL
6313 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006314 case PV_SISO: return curwin->w_p_siso >= 0
6315 ? (char_u *)&(curwin->w_p_siso) : p->var;
6316 case PV_SO: return curwin->w_p_so >= 0
6317 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006319 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006321 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6323#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006324 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006326 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006328#ifdef FEAT_COMPL_FUNC
6329 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6330 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6331#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006332 case PV_FP: return *curbuf->b_p_fp != NUL
6333 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006335 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006337 case PV_GP: return *curbuf->b_p_gp != NUL
6338 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6339 case PV_MP: return *curbuf->b_p_mp != NUL
6340 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006342#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6343 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6344 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6345#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006346#if defined(FEAT_CRYPT)
6347 case PV_CM: return *curbuf->b_p_cm != NUL
6348 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6349#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006350#ifdef FEAT_LINEBREAK
6351 case PV_SBR: return *curwin->w_p_sbr != NUL
6352 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6353#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006354#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006355 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006356 ? (char_u *)&(curwin->w_p_stl) : p->var;
6357#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006358 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6359 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006360 case PV_LW: return *curbuf->b_p_lw != NUL
6361 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006362 case PV_MENC: return *curbuf->b_p_menc != NUL
6363 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364#ifdef FEAT_ARABIC
6365 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6366#endif
6367 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006368 case PV_LCS: return *curwin->w_p_lcs != NUL
6369 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006370 case PV_FCS: return *curwin->w_p_fcs != NUL
6371 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006372 case PV_VE: return *curwin->w_p_ve != NUL
6373 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006374#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006375 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6376#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006377#ifdef FEAT_SYN_HL
6378 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6379 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006380 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006381 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383#ifdef FEAT_DIFF
6384 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6385#endif
6386#ifdef FEAT_FOLDING
6387 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6388 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6389 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6390 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6391 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6392 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6393 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6394# ifdef FEAT_EVAL
6395 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6396 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6397# endif
6398 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6399#endif
6400 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006401 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006402#ifdef FEAT_LINEBREAK
6403 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006406 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006407#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6409#endif
6410#ifdef FEAT_RIGHTLEFT
6411 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6412 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6413#endif
6414 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006415 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6417#ifdef FEAT_LINEBREAK
6418 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006419 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6420 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006422 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006424 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006425#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006426 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6427 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6428#endif
6429#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006430 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6431 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6432 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
6435 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6436 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6439 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6441 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6443 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6444 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006445 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448#ifdef FEAT_FOLDING
6449 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006452#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006453 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006455#ifdef FEAT_COMPL_FUNC
6456 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006457 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006458#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006459#ifdef FEAT_EVAL
6460 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6461#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006462 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006464 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006470 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6472 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6473 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6474 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6475#ifdef FEAT_FIND_ID
6476# ifdef FEAT_EVAL
6477 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6478# endif
6479#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006480#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6482 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6483#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006484#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006485 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487#ifdef FEAT_CRYPT
6488 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006491 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6493 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6494 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6495 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6496 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006498 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6505#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006506 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006507 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6508#endif
6509#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006510 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6511 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6512 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006513 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514#endif
6515 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6516 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6517 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6518 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006519#ifdef FEAT_PERSISTENT_UNDO
6520 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6523#ifdef FEAT_KEYMAP
6524 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6525#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006526#ifdef FEAT_SIGNS
6527 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6528#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006529#ifdef FEAT_VARTABS
6530 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6531 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6532#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006533 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006535 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 return (char_u *)&(curbuf->b_p_wm);
6537}
6538
6539/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006540 * Return a pointer to the variable for option at 'opt_idx'
6541 */
6542 char_u *
6543get_option_var(int opt_idx)
6544{
6545 return options[opt_idx].var;
6546}
6547
Dominique Pelle748b3082022-01-08 12:41:16 +00006548#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006549/*
6550 * Return the full name of the option at 'opt_idx'
6551 */
6552 char_u *
6553get_option_fullname(int opt_idx)
6554{
6555 return (char_u *)options[opt_idx].fullname;
6556}
Dominique Pelle748b3082022-01-08 12:41:16 +00006557#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006558
6559/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00006560 * Return the did_set callback function for the option at 'opt_idx'
6561 */
6562 opt_did_set_cb_T
6563get_option_did_set_cb(int opt_idx)
6564{
6565 return options[opt_idx].opt_did_set_cb;
6566}
6567
6568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 * Get the value of 'equalprg', either the buffer-local one or the global one.
6570 */
6571 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006572get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 if (*curbuf->b_p_ep == NUL)
6575 return p_ep;
6576 return curbuf->b_p_ep;
6577}
6578
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579/*
6580 * Copy options from one window to another.
6581 * Used when splitting a window.
6582 */
6583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006584win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6587 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006588 after_copy_winopt(wp_to);
6589}
6590
6591/*
6592 * After copying window options: update variables depending on options.
6593 */
6594 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006595after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006596{
6597#ifdef FEAT_LINEBREAK
6598 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006599#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006600#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006601 fill_culopt_flags(NULL, wp);
6602 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006603#endif
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006604 set_listchars_option(wp, wp->w_p_lcs, TRUE);
6605 set_fillchars_option(wp, wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006606}
6607
6608 static char_u *
6609copy_option_val(char_u *val)
6610{
6611 if (val == empty_option)
6612 return empty_option; // no need to allocate memory
6613 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616/*
6617 * Copy the options from one winopt_T to another.
6618 * Doesn't free the old option values in "to", use clear_winopt() for that.
6619 * The 'scroll' option is not copied, because it depends on the window height.
6620 * The 'previewwindow' option is reset, there can be only one preview window.
6621 */
6622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006623copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625#ifdef FEAT_ARABIC
6626 to->wo_arab = from->wo_arab;
6627#endif
6628 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006629 to->wo_lcs = copy_option_val(from->wo_lcs);
6630 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006632 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006633 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006634 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006635#ifdef FEAT_LINEBREAK
6636 to->wo_nuw = from->wo_nuw;
6637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638#ifdef FEAT_RIGHTLEFT
6639 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006640 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006642#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006643 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006644#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006645#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006646 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006649#ifdef FEAT_DIFF
6650 to->wo_wrap_save = from->wo_wrap_save;
6651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652#ifdef FEAT_LINEBREAK
6653 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006654 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006655 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006657 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006659 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006660 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006661 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006662 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006663#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006664 to->wo_spell = from->wo_spell;
6665#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006666#ifdef FEAT_SYN_HL
6667 to->wo_cuc = from->wo_cuc;
6668 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006669 to->wo_culopt = copy_option_val(from->wo_culopt);
6670 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672#ifdef FEAT_DIFF
6673 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006674 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006676#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006677 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006678 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006679#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006680#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006681 to->wo_twk = copy_option_val(from->wo_twk);
6682 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#ifdef FEAT_FOLDING
6685 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006686 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006688 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006689 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 to->wo_fml = from->wo_fml;
6691 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006692 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006693 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006694 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006695 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 to->wo_fdn = from->wo_fdn;
6697# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006698 to->wo_fde = copy_option_val(from->wo_fde);
6699 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006701 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006703#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006704 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006705#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006706
6707#ifdef FEAT_EVAL
6708 // Copy the script context so that we know where the value was last set.
6709 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6710 sizeof(to->wo_script_ctx));
6711#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006712 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713}
6714
6715/*
6716 * Check string options in a window for a NULL value.
6717 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006719check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
6721 check_winopt(&win->w_onebuf_opt);
6722 check_winopt(&win->w_allbuf_opt);
6723}
6724
6725/*
6726 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6727 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006728 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006729check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731#ifdef FEAT_FOLDING
6732 check_string_option(&wop->wo_fdi);
6733 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006734 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735# ifdef FEAT_EVAL
6736 check_string_option(&wop->wo_fde);
6737 check_string_option(&wop->wo_fdt);
6738# endif
6739 check_string_option(&wop->wo_fmr);
6740#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006741#ifdef FEAT_SIGNS
6742 check_string_option(&wop->wo_scl);
6743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744#ifdef FEAT_RIGHTLEFT
6745 check_string_option(&wop->wo_rlc);
6746#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006747#ifdef FEAT_LINEBREAK
6748 check_string_option(&wop->wo_sbr);
6749#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006750#ifdef FEAT_STL_OPT
6751 check_string_option(&wop->wo_stl);
6752#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006753#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006754 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006755 check_string_option(&wop->wo_cc);
6756#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006757#ifdef FEAT_CONCEAL
6758 check_string_option(&wop->wo_cocu);
6759#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006760#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006761 check_string_option(&wop->wo_twk);
6762 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006763#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006764#ifdef FEAT_LINEBREAK
6765 check_string_option(&wop->wo_briopt);
6766#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006767 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006768 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006769 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006770 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772
6773/*
6774 * Free the allocated memory inside a winopt_T.
6775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006777clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779#ifdef FEAT_FOLDING
6780 clear_string_option(&wop->wo_fdi);
6781 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006782 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783# ifdef FEAT_EVAL
6784 clear_string_option(&wop->wo_fde);
6785 clear_string_option(&wop->wo_fdt);
6786# endif
6787 clear_string_option(&wop->wo_fmr);
6788#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006789#ifdef FEAT_SIGNS
6790 clear_string_option(&wop->wo_scl);
6791#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006792#ifdef FEAT_LINEBREAK
6793 clear_string_option(&wop->wo_briopt);
6794#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006795 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796#ifdef FEAT_RIGHTLEFT
6797 clear_string_option(&wop->wo_rlc);
6798#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006799#ifdef FEAT_LINEBREAK
6800 clear_string_option(&wop->wo_sbr);
6801#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006802#ifdef FEAT_STL_OPT
6803 clear_string_option(&wop->wo_stl);
6804#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006805#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006806 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006807 clear_string_option(&wop->wo_cc);
6808#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006809#ifdef FEAT_CONCEAL
6810 clear_string_option(&wop->wo_cocu);
6811#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006812#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006813 clear_string_option(&wop->wo_twk);
6814 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006815#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006816 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006817 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006818 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819}
6820
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006821#ifdef FEAT_EVAL
6822// Index into the options table for a buffer-local option enum.
6823static int buf_opt_idx[BV_COUNT];
6824# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6825
6826/*
6827 * Initialize buf_opt_idx[] if not done already.
6828 */
6829 static void
6830init_buf_opt_idx(void)
6831{
6832 static int did_init_buf_opt_idx = FALSE;
6833 int i;
6834
6835 if (did_init_buf_opt_idx)
6836 return;
6837 did_init_buf_opt_idx = TRUE;
6838 for (i = 0; !istermoption_idx(i); i++)
6839 if (options[i].indir & PV_BUF)
6840 buf_opt_idx[options[i].indir & PV_MASK] = i;
6841}
6842#else
6843# define COPY_OPT_SCTX(buf, bv)
6844#endif
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846/*
6847 * Copy global option values to local options for one buffer.
6848 * Used when creating a new buffer and sometimes when entering a buffer.
6849 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006850 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6852 * appropriate.
6853 * BCO_NOHELP Don't copy the values to a help buffer.
6854 */
6855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006856buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006859 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 int dont_do_help;
6861 int did_isk = FALSE;
6862
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006863 // Skip this when the option defaults have not been set yet. Happens when
6864 // main() allocates the first buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (p_cpo != NULL)
6866 {
6867 /*
6868 * Always copy when entering and 'cpo' contains 'S'.
6869 * Don't copy when already initialized.
6870 * Don't copy when 'cpo' contains 's' and not entering.
6871 * 'S' BCO_ENTER initialized 's' should_copy
6872 * yes yes X X TRUE
6873 * yes no yes X FALSE
6874 * no X yes X FALSE
6875 * X no no yes FALSE
6876 * X no no no TRUE
6877 * no yes no X TRUE
6878 */
6879 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6880 && (buf->b_p_initialized
6881 || (!(flags & BCO_ENTER)
6882 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6883 should_copy = FALSE;
6884
6885 if (should_copy || (flags & BCO_ALWAYS))
6886 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006887#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006888 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006889 init_buf_opt_idx();
6890#endif
6891 // Don't copy the options specific to a help buffer when
6892 // BCO_NOHELP is given or the options were initialized already
6893 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6895 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006896 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {
6898 save_p_isk = buf->b_p_isk;
6899 buf->b_p_isk = NULL;
6900 }
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00006901 // Always free the allocated strings. If not already initialized,
6902 // reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 if (!buf->b_p_initialized)
6904 {
6905 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006906 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006909 switch (*p_ffs)
6910 {
6911 case 'm':
6912 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6913 case 'd':
6914 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6915 case 'u':
6916 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6917 default:
6918 buf->b_p_ff = vim_strsave(p_ff);
6919 }
6920 if (buf->b_p_ff != NULL)
6921 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 buf->b_p_bh = empty_option;
6923 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
6925 else
6926 free_buf_options(buf, FALSE);
6927
6928 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006929 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 buf->b_p_ai_nopaste = p_ai_nopaste;
6931 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006932 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006934 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 buf->b_p_tw_nopaste = p_tw_nopaste;
6936 buf->b_p_tw_nobin = p_tw_nobin;
6937 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006938 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 buf->b_p_wm_nopaste = p_wm_nopaste;
6940 buf->b_p_wm_nobin = p_wm_nobin;
6941 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006942 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006943 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006944 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006945 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006946 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006948 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006950 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006952 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 buf->b_p_ml_nobin = p_ml_nobin;
6954 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006955 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006956 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006957 buf->b_p_swf = FALSE;
6958 else
6959 {
6960 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006961 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006964 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006965#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006966 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006967 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006969#ifdef FEAT_COMPL_FUNC
6970 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006972 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006973 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006974 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006975 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006976#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006977#ifdef FEAT_EVAL
6978 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006979 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006980 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006983 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006985#ifdef FEAT_VARTABS
6986 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006987 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006988 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006989 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006990 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006991 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006992 buf->b_p_vsts_nopaste = p_vsts_nopaste
6993 ? vim_strsave(p_vsts_nopaste) : NULL;
6994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006996 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006998 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999#ifdef FEAT_FOLDING
7000 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007001 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002#endif
7003 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007004 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007005 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007006 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02007007 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
7008 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007010 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007012 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007019 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007021 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007023 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01007024 buf->b_p_cinsd = vim_strsave(p_cinsd);
7025 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01007026 buf->b_p_lop = vim_strsave(p_lop);
7027 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007028
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007029 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007032 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007034 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007036 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007038 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007040 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007041 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007042 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007043#endif
7044#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007045 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007046 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007047 (void)compile_cap_prog(&buf->b_s);
7048 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007050 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007051 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007052 buf->b_s.b_p_spo = vim_strsave(p_spo);
7053 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007055#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007057 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007059 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007061 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007062#if defined(FEAT_EVAL)
7063 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007064 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066#ifdef FEAT_CRYPT
7067 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007068 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007071 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#ifdef FEAT_KEYMAP
7073 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007074 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 buf->b_kmap_state |= KEYMAP_INIT;
7076#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007077#ifdef FEAT_TERMINAL
7078 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007079 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007080#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 // This isn't really an option, but copying the langmap and IME
7082 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007084 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007086 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 // options that are normally global but also have a local value
7089 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007091 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007092 buf->b_p_bkc = empty_option;
7093 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094#ifdef FEAT_QUICKFIX
7095 buf->b_p_gp = empty_option;
7096 buf->b_p_mp = empty_option;
7097 buf->b_p_efm = empty_option;
7098#endif
7099 buf->b_p_ep = empty_option;
7100 buf->b_p_kp = empty_option;
7101 buf->b_p_path = empty_option;
7102 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007103 buf->b_p_tc = empty_option;
7104 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105#ifdef FEAT_FIND_ID
7106 buf->b_p_def = empty_option;
7107 buf->b_p_inc = empty_option;
7108# ifdef FEAT_EVAL
7109 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007110 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111# endif
7112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 buf->b_p_dict = empty_option;
7114 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007115#ifdef FEAT_COMPL_FUNC
7116 buf->b_p_tsrfu = empty_option;
7117#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007118 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007119 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007120#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7121 buf->b_p_bexpr = empty_option;
7122#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007123#if defined(FEAT_CRYPT)
7124 buf->b_p_cm = empty_option;
7125#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007126#ifdef FEAT_PERSISTENT_UNDO
7127 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007128 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007129#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007130 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007131 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007133 // Don't copy the options set by ex_help(), use the saved values,
7134 // when going from a help buffer to a non-help buffer.
7135 // Don't touch these at all when BCO_NOHELP is used and going from
7136 // or to a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007140#ifdef FEAT_VARTABS
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007141 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007142 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007143 else
7144 buf->b_p_vts_array = NULL;
7145#endif
7146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 else
7148 {
7149 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007150 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 did_isk = TRUE;
7152 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007153 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007154#ifdef FEAT_VARTABS
7155 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007156 COPY_OPT_SCTX(buf, BV_VTS);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00007157 if (p_vts && *p_vts != NUL && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007158 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007159 else
7160 buf->b_p_vts_array = NULL;
7161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (buf->b_p_bt[0] == 'h')
7164 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007166 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168 }
7169
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007170 // When the options should be copied (ignoring BCO_ALWAYS), set the
7171 // flag that indicates that the options have been initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (should_copy)
7173 buf->b_p_initialized = TRUE;
7174 }
7175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (did_isk)
7178 (void)buf_init_chartab(buf, FALSE);
7179}
7180
7181/*
7182 * Reset the 'modifiable' option and its default value.
7183 */
7184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007185reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 int opt_idx;
7188
7189 curbuf->b_p_ma = FALSE;
7190 p_ma = FALSE;
7191 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007192 if (opt_idx >= 0)
7193 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194}
7195
7196/*
7197 * Set the global value for 'iminsert' to the local value.
7198 */
7199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007200set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202 p_iminsert = curbuf->b_p_iminsert;
7203}
7204
7205/*
7206 * Set the global value for 'imsearch' to the local value.
7207 */
7208 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007209set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 p_imsearch = curbuf->b_p_imsearch;
7212}
7213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214static int expand_option_idx = -1;
7215static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7216static int expand_option_flags = 0;
7217
7218 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007219set_context_in_set_cmd(
7220 expand_T *xp,
7221 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007222 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007225 long_u flags = 0; // init for GCC
7226 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 char_u *p;
7228 char_u *s;
7229 int is_term_option = FALSE;
7230 int key;
7231
7232 expand_option_flags = opt_flags;
7233
7234 xp->xp_context = EXPAND_SETTINGS;
7235 if (*arg == NUL)
7236 {
7237 xp->xp_pattern = arg;
7238 return;
7239 }
7240 p = arg + STRLEN(arg) - 1;
7241 if (*p == ' ' && *(p - 1) != '\\')
7242 {
7243 xp->xp_pattern = p + 1;
7244 return;
7245 }
7246 while (p > arg)
7247 {
7248 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007249 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 if (*p == ' ' || *p == ',')
7251 {
7252 while (s > arg && *(s - 1) == '\\')
7253 --s;
7254 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007255 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (*p == ' ' && ((p - s) & 1) == 0)
7257 {
7258 ++p;
7259 break;
7260 }
7261 --p;
7262 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007263 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {
7265 xp->xp_context = EXPAND_BOOL_SETTINGS;
7266 p += 2;
7267 }
7268 if (STRNCMP(p, "inv", 3) == 0)
7269 {
7270 xp->xp_context = EXPAND_BOOL_SETTINGS;
7271 p += 3;
7272 }
7273 xp->xp_pattern = arg = p;
7274 if (*arg == '<')
7275 {
7276 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007277 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 return;
7279 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007280 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
7282 xp->xp_context = EXPAND_NOTHING;
7283 return;
7284 }
7285 nextchar = *++p;
7286 is_term_option = TRUE;
7287 expand_option_name[2] = KEY2TERMCAP0(key);
7288 expand_option_name[3] = KEY2TERMCAP1(key);
7289 }
7290 else
7291 {
7292 if (p[0] == 't' && p[1] == '_')
7293 {
7294 p += 2;
7295 if (*p != NUL)
7296 ++p;
7297 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007298 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 nextchar = *++p;
7300 is_term_option = TRUE;
7301 expand_option_name[2] = p[-2];
7302 expand_option_name[3] = p[-1];
7303 }
7304 else
7305 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007306 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7308 p++;
7309 if (*p == NUL)
7310 return;
7311 nextchar = *p;
7312 *p = NUL;
7313 opt_idx = findoption(arg);
7314 *p = nextchar;
7315 if (opt_idx == -1 || options[opt_idx].var == NULL)
7316 {
7317 xp->xp_context = EXPAND_NOTHING;
7318 return;
7319 }
7320 flags = options[opt_idx].flags;
7321 if (flags & P_BOOL)
7322 {
7323 xp->xp_context = EXPAND_NOTHING;
7324 return;
7325 }
7326 }
7327 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007328 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7330 {
7331 ++p;
7332 nextchar = '=';
7333 }
7334 if ((nextchar != '=' && nextchar != ':')
7335 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7336 {
7337 xp->xp_context = EXPAND_UNSUCCESSFUL;
7338 return;
7339 }
7340 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7341 {
7342 xp->xp_context = EXPAND_OLD_SETTING;
7343 if (is_term_option)
7344 expand_option_idx = -1;
7345 else
7346 expand_option_idx = opt_idx;
7347 xp->xp_pattern = p + 1;
7348 return;
7349 }
7350 xp->xp_context = EXPAND_NOTHING;
7351 if (is_term_option || (flags & P_NUM))
7352 return;
7353
7354 xp->xp_pattern = p + 1;
7355
7356 if (flags & P_EXPAND)
7357 {
7358 p = options[opt_idx].var;
7359 if (p == (char_u *)&p_bdir
7360 || p == (char_u *)&p_dir
7361 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007362 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365#ifdef FEAT_SESSION
7366 || p == (char_u *)&p_vdir
7367#endif
7368 )
7369 {
7370 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007371 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 xp->xp_backslash = XP_BS_THREE;
7373 else
7374 xp->xp_backslash = XP_BS_ONE;
7375 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007376 else if (p == (char_u *)&p_ft)
7377 {
7378 xp->xp_context = EXPAND_FILETYPE;
7379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 else
7381 {
7382 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007383 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 if (p == (char_u *)&p_tags)
7385 xp->xp_backslash = XP_BS_THREE;
7386 else
7387 xp->xp_backslash = XP_BS_ONE;
7388 }
7389 }
7390
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007391 // For an option that is a list of file names, find the start of the
7392 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007395 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (*p == ' ' || *p == ',')
7397 {
7398 s = p;
7399 while (s > xp->xp_pattern && *(s - 1) == '\\')
7400 --s;
7401 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7402 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7403 {
7404 xp->xp_pattern = p + 1;
7405 break;
7406 }
7407 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007408
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007409#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007410 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007411 if (options[opt_idx].var == (char_u *)&p_sps
7412 && STRNCMP(p, "file:", 5) == 0)
7413 {
7414 xp->xp_pattern = p + 5;
7415 break;
7416 }
7417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419}
7420
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007421/*
7422 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7423 *
7424 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7425 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7426 *
7427 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7428 * regular expression 'regmatch', then stores the match in matches[idx] and
7429 * returns TRUE.
7430 *
7431 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7432 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7433 *
7434 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7435 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7436 */
7437 static int
7438match_str(
7439 char_u *str,
7440 regmatch_T *regmatch,
7441 char_u **matches,
7442 int idx,
7443 int test_only,
7444 int fuzzy,
7445 char_u *fuzzystr,
7446 fuzmatch_str_T *fuzmatch)
7447{
7448 if (!fuzzy)
7449 {
7450 if (vim_regexec(regmatch, str, (colnr_T)0))
7451 {
7452 if (!test_only)
7453 matches[idx] = vim_strsave(str);
7454 return TRUE;
7455 }
7456 }
7457 else
7458 {
7459 int score;
7460
7461 score = fuzzy_match_str(str, fuzzystr);
7462 if (score != 0)
7463 {
7464 if (!test_only)
7465 {
7466 fuzmatch[idx].idx = idx;
7467 fuzmatch[idx].str = vim_strsave(str);
7468 fuzmatch[idx].score = score;
7469 }
7470
7471 return TRUE;
7472 }
7473 }
7474
7475 return FALSE;
7476}
7477
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007479ExpandSettings(
7480 expand_T *xp,
7481 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007482 char_u *fuzzystr,
7483 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007484 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007485 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007487 int num_normal = 0; // Nr of matching non-term-code settings
7488 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 int opt_idx;
7490 int match;
7491 int count = 0;
7492 char_u *str;
7493 int loop;
7494 int is_term_opt;
7495 char_u name_buf[MAX_KEY_NAME_LEN];
7496 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007497 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007498 int fuzzy;
7499 fuzmatch_str_T *fuzmatch = NULL;
7500
Christian Brabandtcb747892022-05-08 21:10:56 +01007501 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007503 // do this loop twice:
7504 // loop == 0: count the number of matching options
7505 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 for (loop = 0; loop <= 1; ++loop)
7507 {
7508 regmatch->rm_ic = ic;
7509 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7510 {
K.Takataeeec2542021-06-02 13:28:16 +02007511 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007512 {
7513 if (match_str((char_u *)names[match], regmatch, *matches,
7514 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 {
7516 if (loop == 0)
7517 num_normal++;
7518 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007519 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7524 opt_idx++)
7525 {
7526 if (options[opt_idx].var == NULL)
7527 continue;
7528 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7529 && !(options[opt_idx].flags & P_BOOL))
7530 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007531 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 if (is_term_opt && num_normal > 0)
7533 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007534
7535 if (match_str(str, regmatch, *matches, count, (loop == 0),
7536 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 {
7538 if (loop == 0)
7539 {
7540 if (is_term_opt)
7541 num_term++;
7542 else
7543 num_normal++;
7544 }
7545 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007546 count++;
7547 }
7548 else if (!fuzzy && options[opt_idx].shortname != NULL
7549 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007550 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007551 {
7552 // Compare against the abbreviated option name (for regular
7553 // expression match). Fuzzy matching (previous if) already
7554 // matches against both the expanded and abbreviated names.
7555 if (loop == 0)
7556 {
7557 if (is_term_opt)
7558 num_term++;
7559 else
7560 num_normal++;
7561 }
7562 else
7563 (*matches)[count++] = vim_strsave(str);
7564 }
7565 else if (is_term_opt)
7566 {
7567 name_buf[0] = '<';
7568 name_buf[1] = 't';
7569 name_buf[2] = '_';
7570 name_buf[3] = str[2];
7571 name_buf[4] = str[3];
7572 name_buf[5] = '>';
7573 name_buf[6] = NUL;
7574
7575 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7576 fuzzy, fuzzystr, fuzmatch))
7577 {
7578 if (loop == 0)
7579 num_term++;
7580 else
7581 count++;
7582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
7584 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007585
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007586 // Check terminal key codes, these are not in the option table
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7588 {
7589 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7590 {
7591 if (!isprint(str[0]) || !isprint(str[1]))
7592 continue;
7593
7594 name_buf[0] = 't';
7595 name_buf[1] = '_';
7596 name_buf[2] = str[0];
7597 name_buf[3] = str[1];
7598 name_buf[4] = NUL;
7599
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007600 if (match_str(name_buf, regmatch, *matches, count,
7601 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7602 {
7603 if (loop == 0)
7604 num_term++;
7605 else
7606 count++;
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 else
7609 {
7610 name_buf[0] = '<';
7611 name_buf[1] = 't';
7612 name_buf[2] = '_';
7613 name_buf[3] = str[0];
7614 name_buf[4] = str[1];
7615 name_buf[5] = '>';
7616 name_buf[6] = NUL;
7617
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007618 if (match_str(name_buf, regmatch, *matches, count,
7619 (loop == 0), fuzzy, fuzzystr,
7620 fuzmatch))
7621 {
7622 if (loop == 0)
7623 num_term++;
7624 else
7625 count++;
7626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628 }
7629
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007630 // Check special key names.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007631 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7633 {
7634 name_buf[0] = '<';
7635 STRCPY(name_buf + 1, str);
7636 STRCAT(name_buf, ">");
7637
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007638 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7639 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {
7641 if (loop == 0)
7642 num_term++;
7643 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007644 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646 }
7647 }
7648 if (loop == 0)
7649 {
7650 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007651 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007653 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 else
7655 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007656 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007658 *matches = ALLOC_MULT(char_u *, *numMatches);
7659 if (*matches == NULL)
7660 {
7661 *matches = (char_u **)"";
7662 return FAIL;
7663 }
7664 }
7665 else
7666 {
7667 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7668 if (fuzmatch == NULL)
7669 {
7670 *matches = (char_u **)"";
7671 return FAIL;
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
7674 }
7675 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007676
7677 if (fuzzy &&
7678 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7679 return FAIL;
7680
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 return OK;
7682}
7683
7684 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007685ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007687 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 char_u *buf;
7689
zeertzjqb0d45ec2023-01-25 15:04:22 +00007690 *numMatches = 0;
7691 *matches = ALLOC_ONE(char_u *);
7692 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 return FAIL;
7694
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00007695 // For a terminal key code expand_option_idx is < 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (expand_option_idx < 0)
7697 {
7698 var = find_termcode(expand_option_name + 2);
7699 if (var == NULL)
7700 expand_option_idx = findoption(expand_option_name);
7701 }
7702
7703 if (expand_option_idx >= 0)
7704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007705 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 option_value2string(&options[expand_option_idx], expand_option_flags);
7707 var = NameBuff;
7708 }
7709 else if (var == NULL)
7710 var = (char_u *)"";
7711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007712 // A backslash is required before some characters. This is the reverse of
7713 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 buf = vim_strsave_escaped(var, escape_chars);
7715
7716 if (buf == NULL)
7717 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007718 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 return FAIL;
7720 }
7721
7722#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007723 // For MS-Windows et al. we don't double backslashes at the start and
7724 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007725 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (var[0] == '\\' && var[1] == '\\'
7727 && expand_option_idx >= 0
7728 && (options[expand_option_idx].flags & P_EXPAND)
7729 && vim_isfilec(var[2])
7730 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007731 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732#endif
7733
zeertzjqb0d45ec2023-01-25 15:04:22 +00007734 *matches[0] = buf;
7735 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 return OK;
7737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
7739/*
7740 * Get the value for the numeric or string option *opp in a nice format into
7741 * NameBuff[]. Must not be called with a hidden option!
7742 */
7743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007744option_value2string(
7745 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007746 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748 char_u *varp;
7749
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007750 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 if (opp->flags & P_NUM)
7753 {
7754 long wc = 0;
7755
7756 if (wc_use_keyname(varp, &wc))
7757 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7758 else if (wc != 0)
7759 STRCPY(NameBuff, transchar((int)wc));
7760 else
7761 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7762 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007763 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
7765 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007766 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 NameBuff[0] = NUL;
7768#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007769 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007770 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 STRCPY(NameBuff, "*****");
7772#endif
7773 else if (opp->flags & P_EXPAND)
7774 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007775 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 else if ((char_u **)opp->var == &p_pt)
7777 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7778 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007779 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781}
7782
7783/*
7784 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7785 * printed as a keyname.
7786 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7787 */
7788 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007789wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7792 {
7793 *wcp = *(long *)varp;
7794 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7795 return TRUE;
7796 }
7797 return FALSE;
7798}
7799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 * Return TRUE if "x" is present in 'shortmess' option, or
7802 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7803 */
7804 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007805shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007807 return p_shm != NULL &&
7808 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 || (vim_strchr(p_shm, 'a') != NULL
7810 && vim_strchr((char_u *)SHM_A, x) != NULL));
7811}
7812
7813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7815 *
7816 * Reset 'compatible' and set the values for options that didn't get set yet
7817 * to the Vim defaults.
7818 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007819 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 */
7821 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007822vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007824 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007825 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007826 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827
7828 if (!option_was_set((char_u *)"cp"))
7829 {
7830 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007831 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7833 set_option_default(opt_idx, OPT_FREE, FALSE);
7834 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007835 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007837
7838 if (fname != NULL)
7839 {
7840 p = vim_getenv(envname, &dofree);
7841 if (p == NULL)
7842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007843 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007844 p = FullName_save(fname, FALSE);
7845 if (p != NULL)
7846 {
7847 vim_setenv(envname, p);
7848 vim_free(p);
7849 }
7850 }
7851 else if (dofree)
7852 vim_free(p);
7853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854}
7855
7856/*
7857 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7858 */
7859 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007860change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007862 int opt_idx;
7863
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if (p_cp != on)
7865 {
7866 p_cp = on;
7867 compatible_set();
7868 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007869 opt_idx = findoption((char_u *)"cp");
7870 if (opt_idx >= 0)
7871 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874/*
7875 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007876 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 */
7878 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007879option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 int idx;
7882
7883 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007884 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 return FALSE;
7886 if (options[idx].flags & P_WAS_SET)
7887 return TRUE;
7888 return FALSE;
7889}
7890
7891/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007892 * Reset the flag indicating option "name" was set.
7893 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007894 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007895reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007896{
7897 int idx = findoption(name);
7898
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007899 if (idx < 0)
7900 return FAIL;
7901
7902 options[idx].flags &= ~P_WAS_SET;
7903 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007904}
7905
7906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 * compatible_set() - Called when 'compatible' has been set or unset.
7908 *
7909 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7910 * flag) to a Vi compatible value.
7911 * When 'compatible' is unset: Set all options that have a different default
7912 * for Vim (without the P_VI_DEF flag) to that default.
7913 */
7914 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007915compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
7917 int opt_idx;
7918
Bram Moolenaardac13472019-09-16 21:06:21 +02007919 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7921 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7922 set_option_default(opt_idx, OPT_FREE, p_cp);
7923 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007924 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925}
7926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 * Check if backspacing over something is allowed.
7929 */
7930 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007931can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007932 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007934#ifdef FEAT_JOB_CHANNEL
7935 if (what == BS_START && bt_prompt(curbuf))
7936 return FALSE;
7937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 switch (*p_bs)
7939 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007940 case '3': return TRUE;
7941 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 case '1': return (what != BS_START);
7943 case '0': return FALSE;
7944 }
7945 return vim_strchr(p_bs, what) != NULL;
7946}
7947
7948/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007949 * Return the effective 'scrolloff' value for the current window, using the
7950 * global value when appropriate.
7951 */
7952 long
7953get_scrolloff_value(void)
7954{
7955 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7956}
7957
7958/*
7959 * Return the effective 'sidescrolloff' value for the current window, using the
7960 * global value when appropriate.
7961 */
7962 long
7963get_sidescrolloff_value(void)
7964{
7965 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7966}
7967
7968/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007969 * Get the local or global value of 'backupcopy'.
7970 */
7971 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007972get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007973{
7974 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7975}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007976
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007977#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02007978/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007979 * Get the local or global value of 'formatlistpat'.
7980 */
7981 char_u *
7982get_flp_value(buf_T *buf)
7983{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007984 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7985 return p_flp;
7986 return buf->b_p_flp;
7987}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01007988#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00007989
7990/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007991 * Get the local or global value of the 'virtualedit' flags.
7992 */
7993 unsigned int
7994get_ve_flags(void)
7995{
Gary Johnson51ad8502021-08-03 18:33:08 +02007996 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7997 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007998}
7999
Bram Moolenaaree857022019-11-09 23:26:40 +01008000#if defined(FEAT_LINEBREAK) || defined(PROTO)
8001/*
8002 * Get the local or global value of 'showbreak'.
8003 */
8004 char_u *
8005get_showbreak_value(win_T *win)
8006{
8007 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8008 return p_sbr;
8009 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8010 return empty_option;
8011 return win->w_p_sbr;
8012}
8013#endif
8014
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008015#if defined(FEAT_EVAL) || defined(PROTO)
8016/*
8017 * Get window or buffer local options.
8018 */
8019 dict_T *
8020get_winbuf_options(int bufopt)
8021{
8022 dict_T *d;
8023 int opt_idx;
8024
8025 d = dict_alloc();
8026 if (d == NULL)
8027 return NULL;
8028
Bram Moolenaardac13472019-09-16 21:06:21 +02008029 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008030 {
8031 struct vimoption *opt = &options[opt_idx];
8032
8033 if ((bufopt && (opt->indir & PV_BUF))
8034 || (!bufopt && (opt->indir & PV_WIN)))
8035 {
8036 char_u *varp = get_varp(opt);
8037
8038 if (varp != NULL)
8039 {
8040 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008041 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008042 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008043 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008044 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008045 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008046 }
8047 }
8048 }
8049
8050 return d;
8051}
8052#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008053
Bram Moolenaardac13472019-09-16 21:06:21 +02008054#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008055/*
8056 * This is called when 'culopt' is changed
8057 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008058 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008059fill_culopt_flags(char_u *val, win_T *wp)
8060{
8061 char_u *p;
8062 char_u culopt_flags_new = 0;
8063
8064 if (val == NULL)
8065 p = wp->w_p_culopt;
8066 else
8067 p = val;
8068 while (*p != NUL)
8069 {
8070 if (STRNCMP(p, "line", 4) == 0)
8071 {
8072 p += 4;
8073 culopt_flags_new |= CULOPT_LINE;
8074 }
8075 else if (STRNCMP(p, "both", 4) == 0)
8076 {
8077 p += 4;
8078 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8079 }
8080 else if (STRNCMP(p, "number", 6) == 0)
8081 {
8082 p += 6;
8083 culopt_flags_new |= CULOPT_NBR;
8084 }
8085 else if (STRNCMP(p, "screenline", 10) == 0)
8086 {
8087 p += 10;
8088 culopt_flags_new |= CULOPT_SCRLINE;
8089 }
8090
8091 if (*p != ',' && *p != NUL)
8092 return FAIL;
8093 if (*p == ',')
8094 ++p;
8095 }
8096
8097 // Can't have both "line" and "screenline".
8098 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8099 return FAIL;
8100 wp->w_p_culopt_flags = culopt_flags_new;
8101
8102 return OK;
8103}
8104#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008105
8106/*
8107 * Get the value of 'magic' adjusted for Vim9 script.
8108 */
8109 int
8110magic_isset(void)
8111{
8112 switch (magic_overruled)
8113 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008114 case OPTION_MAGIC_ON: return TRUE;
8115 case OPTION_MAGIC_OFF: return FALSE;
8116 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008117 }
8118#ifdef FEAT_EVAL
8119 if (in_vim9script())
8120 return TRUE;
8121#endif
8122 return p_magic;
8123}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008124
8125/*
8126 * Set the callback function value for an option that accepts a function name,
8127 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8128 * Returns OK if the option is successfully set to a function, otherwise
8129 * returns FAIL.
8130 */
8131 int
8132option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8133{
8134#ifdef FEAT_EVAL
8135 typval_T *tv;
8136 callback_T cb;
8137
8138 if (optval == NULL || *optval == NUL)
8139 {
8140 free_callback(optcb);
8141 return OK;
8142 }
8143
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008144 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008145 || (STRNCMP(optval, "function(", 9) == 0)
8146 || (STRNCMP(optval, "funcref(", 8) == 0))
8147 // Lambda expression or a funcref
8148 tv = eval_expr(optval, NULL);
8149 else
8150 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008151 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008152 if (tv == NULL)
8153 return FAIL;
8154
8155 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008156 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008157 {
8158 free_tv(tv);
8159 return FAIL;
8160 }
8161
8162 free_callback(optcb);
8163 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008164 if (cb.cb_free_name)
8165 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008166 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008167
8168 // when using Vim9 style "import.funcname" it needs to be expanded to
8169 // "import#funcname".
8170 expand_autload_callback(optcb);
8171
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008172 return OK;
8173#else
8174 return FAIL;
8175#endif
8176}