blob: 717812c9ceffc36fa193ee307963fbd7135562ff [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000013 * - Put it in the options array in optiondefs.h (copy an existing entry).
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000016 * - Add a PV_XX macro definition to the optiondefs.h file.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000019 * - For a window string option, add code to check_win_options() and
20 * clear_winopt().
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 * - For a buffer option, add some code to buf_copy_options().
22 * - For a buffer string option, add code to check_buf_options().
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +000023 * - If it's a numeric option, add any necessary bounds checks to
24 * set_num_option().
25 * - If it's a list of flags, add some code in did_set_string_option(), search
26 * for WW_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027 * - When adding an option with expansion (P_EXPAND), but with a different
28 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020029 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 * options.txt, and any other related places.
31 * - Add an entry in runtime/optwin.vim.
32 * When making changes:
33 * - Adjust the help for the option in doc/option.txt.
34 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
35 * comment at the help for the 'compatible' option.
36 */
37
38#define IN_OPTION_C
39#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020040#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010043static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020044static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static char_u *option_expand(int opt_idx, char_u *val);
46static void didset_options(void);
47static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000050#else
51# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
52#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010053static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
54static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020055static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020057static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010059static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
61static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020062static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000063static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000066static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void check_winopt(winopt_T *wop);
68static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void paste_option_changed(void);
70static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000073 * Initialize the 'shell' option to a default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000075 static void
76set_init_default_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +000080 // Find default value for 'shell' option.
81 // Don't use it if it is empty.
Bram Moolenaar7c626922005-02-07 22:01:03 +000082 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010083#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +000084 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010085 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +000087 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +020088#if defined(MSWIN)
89 {
90 // For MS-Windows put the path in quotes instead of escaping spaces.
91 char_u *cmd;
92 size_t len;
93
94 if (vim_strchr(p, ' ') != NULL)
95 {
96 len = STRLEN(p) + 3; // two quotes and a trailing NUL
97 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +020098 if (cmd != NULL)
99 {
100 vim_snprintf((char *)cmd, len, "\"%s\"", p);
101 set_string_default("sh", cmd);
102 vim_free(cmd);
103 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200104 }
105 else
106 set_string_default("sh", p);
107 }
108#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100109 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200110#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000111}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000113/*
114 * Set the default for 'backupskip' to include environment variables for
115 * temp files.
116 */
117 static void
118set_init_default_backupskip(void)
119{
120 int opt_idx;
121 long_u n;
122 char_u *p;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100123#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000124 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100125#else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000126 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100127#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000128 int len;
129 garray_T ga;
130 int mustfree;
131 char_u *item;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200132
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000133 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000135 ga_init2(&ga, 1, 100);
136 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
137 {
138 mustfree = FALSE;
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100139#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000140 if (*names[n] == NUL)
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100141# ifdef MACOS_X
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000142 p = (char_u *)"/private/tmp";
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100143# else
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000144 p = (char_u *)"/tmp";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000146 else
Bram Moolenaar074fbd42022-08-26 16:41:14 +0100147#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000148 p = vim_getenv((char_u *)names[n], &mustfree);
149 if (p != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000151 // First time count the NUL, otherwise count the ','.
152 len = (int)STRLEN(p) + 3;
153 item = alloc(len);
154 STRCPY(item, p);
155 add_pathsep(item);
156 STRCAT(item, "*");
157 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
158 == NULL
159 && ga_grow(&ga, len) == OK)
160 {
161 if (ga.ga_len > 0)
162 STRCAT(ga.ga_data, ",");
163 STRCAT(ga.ga_data, item);
164 ga.ga_len += len;
165 }
166 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000168 if (mustfree)
169 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000171 if (ga.ga_data != NULL)
172 {
173 set_string_default("bsk", ga.ga_data);
174 vim_free(ga.ga_data);
175 }
176}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000178/*
179 * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
180 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
181 */
182 static void
183set_init_default_maxmemtot(void)
184{
185 int opt_idx;
186 long_u n;
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 opt_idx = findoption((char_u *)"maxmemtot");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000189 if (opt_idx < 0)
190 return;
191
192#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
193 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {
ichizok02560422022-04-05 14:18:44 +0100196#if defined(HAVE_AVAIL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000197 // Use amount of memory available at this moment.
198 n = (mch_avail_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100199#elif defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000200 // Use amount of memory available to Vim.
201 n = (mch_total_mem(FALSE) >> 1);
ichizok02560422022-04-05 14:18:44 +0100202#else
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000203 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000205 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
206 opt_idx = findoption((char_u *)"maxmem");
207 if (opt_idx >= 0)
208 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000210 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
211 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000212#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000213 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000216}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000218/*
219 * Initialize the 'cdpath' option to a default value.
220 */
221 static void
222set_init_default_cdpath(void)
223{
224 int opt_idx;
225 char_u *cdpath;
226 char_u *buf;
227 int i;
228 int j;
229 int mustfree = FALSE;
230
231 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
232 if (cdpath == NULL)
233 return;
234
235 buf = alloc((STRLEN(cdpath) << 1) + 2);
236 if (buf != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000238 buf[0] = ','; // start with ",", current dir first
239 j = 1;
240 for (i = 0; cdpath[i] != NUL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000242 if (vim_ispathlistsep(cdpath[i]))
243 buf[j++] = ',';
244 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000246 if (cdpath[i] == ' ' || cdpath[i] == ',')
247 buf[j++] = '\\';
248 buf[j++] = cdpath[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 }
250 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000251 buf[j] = NUL;
252 opt_idx = findoption((char_u *)"cdpath");
253 if (opt_idx >= 0)
254 {
255 options[opt_idx].def_val[VI_DEFAULT] = buf;
256 options[opt_idx].flags |= P_DEF_ALLOCED;
257 }
258 else
259 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000261 if (mustfree)
262 vim_free(cdpath);
263}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000265/*
266 * Initialize the 'printencoding' option to a default value.
267 */
268 static void
269set_init_default_printencoding(void)
270{
ichizok02560422022-04-05 14:18:44 +0100271#if defined(FEAT_POSTSCRIPT) && \
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000272 (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100273 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100275# if defined(MSWIN)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000276 (char_u *)"cp1252"
ichizok02560422022-04-05 14:18:44 +0100277# elif defined(VMS)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000278 (char_u *)"dec-mcs"
ichizok02560422022-04-05 14:18:44 +0100279# elif defined(MAC)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000280 (char_u *)"mac-roman"
ichizok02560422022-04-05 14:18:44 +0100281# else // HPUX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000282 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283# endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000284 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285#endif
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000286}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287
288#ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000289/*
290 * Initialize the 'printexpr' option to a default value.
291 */
292 static void
293set_init_default_printexpr(void)
294{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100295 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100297# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000298 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
ichizok02560422022-04-05 14:18:44 +0100299# elif defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
301
ichizok02560422022-04-05 14:18:44 +0100302# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# endif
305 );
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000306}
307#endif
308
309#ifdef UNIX
310/*
311 * Force restricted-mode on for "nologin" or "false" $SHELL
312 */
313 static void
314set_init_restricted_mode(void)
315{
316 char_u *p;
317
318 p = get_isolated_shell_name();
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000319 if (p == NULL)
320 return;
321 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
322 restricted = TRUE;
323 vim_free(p);
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000324}
325#endif
326
327#ifdef CLEAN_RUNTIMEPATH
328/*
329 * When Vim is started with the "--clean" argument, set the default value
330 * for the 'runtimepath' and 'packpath' options.
331 */
332 static void
333set_init_clean_rtp(void)
334{
335 int opt_idx;
336
337 opt_idx = findoption((char_u *)"runtimepath");
338 if (opt_idx >= 0)
339 {
340 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
341 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
342 }
343 opt_idx = findoption((char_u *)"packpath");
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +0000344 if (opt_idx < 0)
345 return;
346 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
347 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000348}
349#endif
350
351/*
352 * Expand environment variables and things like "~" for the defaults.
353 * If option_expand() returns non-NULL the variable is expanded. This can
354 * only happen for non-indirect options.
355 * Also set the default to the expanded value, so ":set" does not list
356 * them.
357 * Don't set the P_ALLOCED flag, because we don't want to free the
358 * default.
359 */
360 static void
361set_init_expand_env(void)
362{
363 int opt_idx;
364 char_u *p;
365
366 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
367 {
368 if ((options[opt_idx].flags & P_GETTEXT)
369 && options[opt_idx].var != NULL)
370 p = (char_u *)_(*(char **)options[opt_idx].var);
371 else
372 p = option_expand(opt_idx, NULL);
373 if (p != NULL && (p = vim_strsave(p)) != NULL)
374 {
375 *(char_u **)options[opt_idx].var = p;
376 // VIMEXP
377 // Defaults for all expanded options are currently the same for Vi
378 // and Vim. When this changes, add some code here! Also need to
379 // split P_DEF_ALLOCED in two.
380 if (options[opt_idx].flags & P_DEF_ALLOCED)
381 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
382 options[opt_idx].def_val[VI_DEFAULT] = p;
383 options[opt_idx].flags |= P_DEF_ALLOCED;
384 }
385 }
386}
387
388/*
389 * Initialize the 'LANG' environment variable to a default value.
390 */
391 static void
392set_init_lang_env(void)
393{
394#if defined(MSWIN) && defined(FEAT_GETTEXT)
395 // If $LANG isn't set, try to get a good value for it. This makes the
396 // right language be used automatically. Don't do this for English.
397 if (mch_getenv((char_u *)"LANG") == NULL)
398 {
399 char buf[20];
400 long_u n;
401
402 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
403 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
404 // only the first two.
405 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
406 (LPTSTR)buf, 20);
407 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
408 {
409 // There are a few exceptions (probably more)
410 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
411 STRCPY(buf, "zh_TW");
412 else if (STRNICMP(buf, "chs", 3) == 0
413 || STRNICMP(buf, "zhc", 3) == 0)
414 STRCPY(buf, "zh_CN");
415 else if (STRNICMP(buf, "jp", 2) == 0)
416 STRCPY(buf, "ja");
417 else
418 buf[2] = NUL; // truncate to two-letter code
419 vim_setenv((char_u *)"LANG", (char_u *)buf);
420 }
421 }
422#elif defined(MACOS_CONVERT)
423 // Moved to os_mac_conv.c to avoid dependency problems.
424 mac_lang_init();
425#endif
426}
427
428/*
429 * Initialize the 'encoding' option to a default value.
430 */
431 static void
432set_init_default_encoding(void)
433{
434 char_u *p;
435 int opt_idx;
436
437# ifdef MSWIN
438 // MS-Windows has builtin support for conversion to and from Unicode, using
439 // "utf-8" for 'encoding' should work best for most users.
440 p = vim_strsave((char_u *)ENC_DFLT);
441# else
442 // enc_locale() will try to find the encoding of the current locale.
443 // This works best for properly configured systems, old and new.
444 p = enc_locale();
445# endif
446 if (p == NULL)
447 return;
448
449 // Try setting 'encoding' and check if the value is valid.
450 // If not, go back to the default encoding.
451 char_u *save_enc = p_enc;
452 p_enc = p;
453 if (STRCMP(p_enc, "gb18030") == 0)
454 {
455 // We don't support "gb18030", but "cp936" is a good substitute
456 // for practical purposes, thus use that. It's not an alias to
457 // still support conversion between gb18030 and utf-8.
458 p_enc = vim_strsave((char_u *)"cp936");
459 vim_free(p);
460 }
461 if (mb_init() == NULL)
462 {
463 opt_idx = findoption((char_u *)"encoding");
464 if (opt_idx >= 0)
465 {
466 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
467 options[opt_idx].flags |= P_DEF_ALLOCED;
468 }
469
470#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
471 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
472 {
473 // Adjust the default for 'isprint' and 'iskeyword' to match
474 // latin1. Also set the defaults for when 'nocompatible' is
475 // set.
476 set_string_option_direct((char_u *)"isp", -1,
477 ISP_LATIN1, OPT_FREE, SID_NONE);
478 set_string_option_direct((char_u *)"isk", -1,
479 ISK_LATIN1, OPT_FREE, SID_NONE);
480 opt_idx = findoption((char_u *)"isp");
481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
483 opt_idx = findoption((char_u *)"isk");
484 if (opt_idx >= 0)
485 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
486 (void)init_chartab();
487 }
488#endif
489
490#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
491 // Win32 console: When GetACP() returns a different value from
492 // GetConsoleCP() set 'termencoding'.
493 if (
494# ifdef VIMDLL
495 (!gui.in_use && !gui.starting) &&
496# endif
497 GetACP() != GetConsoleCP())
498 {
499 char buf[50];
500
501 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
502 // Use an alternative value.
503 if (GetConsoleCP() == 0)
504 sprintf(buf, "cp%ld", (long)GetACP());
505 else
506 sprintf(buf, "cp%ld", (long)GetConsoleCP());
507 p_tenc = vim_strsave((char_u *)buf);
508 if (p_tenc != NULL)
509 {
510 opt_idx = findoption((char_u *)"termencoding");
511 if (opt_idx >= 0)
512 {
513 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
514 options[opt_idx].flags |= P_DEF_ALLOCED;
515 }
516 convert_setup(&input_conv, p_tenc, p_enc);
517 convert_setup(&output_conv, p_enc, p_tenc);
518 }
519 else
520 p_tenc = empty_option;
521 }
522#endif
523#if defined(MSWIN)
524 // $HOME may have characters in active code page.
525 init_homedir();
526#endif
527 }
528 else
529 {
530 vim_free(p_enc);
531 p_enc = save_enc;
532 }
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000533}
534
535/*
536 * Initialize the options, first part.
537 *
538 * Called only once from main(), just after creating the first buffer.
539 * If "clean_arg" is TRUE Vim was started with --clean.
540 */
541 void
542set_init_1(int clean_arg)
543{
544#ifdef FEAT_LANGMAP
545 langmap_init();
546#endif
547
548 // Be Vi compatible by default
549 p_cp = TRUE;
550
551 // Use POSIX compatibility when $VIM_POSIX is set.
552 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
553 {
554 set_string_default("cpo", (char_u *)CPO_ALL);
555 set_string_default("shm", (char_u *)SHM_POSIX);
556 }
557
558 set_init_default_shell();
559 set_init_default_backupskip();
560 set_init_default_maxmemtot();
561 set_init_default_cdpath();
562 set_init_default_printencoding();
563#ifdef FEAT_POSTSCRIPT
564 set_init_default_printexpr();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#endif
566
567 /*
568 * Set all the options (except the terminal options) to their default
569 * value. Also set the global value for local options.
570 */
571 set_options_default(0);
572
matveytadbb1bf2022-02-01 17:26:12 +0000573#ifdef UNIX
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000574 set_init_restricted_mode();
matveytadbb1bf2022-02-01 17:26:12 +0000575#endif
576
Bram Moolenaar07268702018-03-01 21:57:32 +0100577#ifdef CLEAN_RUNTIMEPATH
578 if (clean_arg)
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000579 set_init_clean_rtp();
Bram Moolenaar07268702018-03-01 21:57:32 +0100580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#ifdef FEAT_GUI
583 if (found_reverse_arg)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100584 set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
586
587 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100589 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 check_buf_options(curbuf);
591 check_win_options(curwin);
592 check_options();
593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 didset_options();
596
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000597#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // Use the current chartab for the generic chartab. This is not in
599 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000600 init_spell_chartab();
601#endif
602
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000603 // Expand environment variables and things like "~" for the defaults.
604 set_init_expand_env();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100606 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 // Detect use of mlterm.
610 // Mlterm is a terminal emulator akin to xterm that has some special
611 // abilities (bidi namely).
612 // NOTE: mlterm's author is being asked to 'set' a variable
613 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (mch_getenv((char_u *)"MLTERM") != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100615 set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#endif
617
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200618 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
Yegappan Lakshmanan6c41bed2023-02-10 14:50:31 +0000620 set_init_lang_env();
621 set_init_default_encoding();
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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100919 * 'scroll' defaults to half the window height. The stored default is zero,
920 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000922 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000923 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000924 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 comp_col();
926
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000927 /*
928 * 'window' is only for backwards compatibility with Vi.
929 * Default is Rows - 1.
930 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000931 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000932 p_window = Rows - 1;
933 set_number_default("window", Rows - 1);
934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100936#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000937 /*
938 * If 'background' wasn't set by the user, try guessing the value,
939 * depending on the terminal name. Only need to check for terminals
940 * with a dark background, that can handle color.
941 */
942 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000943 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
944 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000946 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 // don't mark it as set, when starting the GUI it may be
948 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000949 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000952
953#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100954 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000955#endif
956#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000958#endif
959#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100960 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964/*
965 * Initialize the options, part three: After reading the .vimrc
966 */
967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100968set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100970#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
973 * This is done after other initializations, where 'shell' might have been
974 * set, but only if they have not been set before.
975 */
976 char_u *p;
977 int idx_srr;
978 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100979# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 int idx_sp;
981 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
984 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000985 if (idx_srr < 0)
986 do_srr = FALSE;
987 else
988 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000991 if (idx_sp < 0)
992 do_sp = FALSE;
993 else
994 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100995# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200996 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (p != NULL)
998 {
999 /*
1000 * Default for p_sp is "| tee", for p_srr is ">".
1001 * For known shells it is changed here to include stderr.
1002 */
1003 if ( fnamecmp(p, "csh") == 0
1004 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001005# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 || fnamecmp(p, "csh.exe") == 0
1007 || fnamecmp(p, "tcsh.exe") == 0
1008# endif
1009 )
1010 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001011# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if (do_sp)
1013 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001014# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001016# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1020 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (do_srr)
1023 {
1024 p_srr = (char_u *)">&";
1025 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1026 }
1027 }
Mike Williams12795022021-06-28 20:53:58 +02001028# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +02001029 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
1030 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +02001031 else if ( fnamecmp(p, "powershell") == 0
1032 || fnamecmp(p, "powershell.exe") == 0
1033 )
1034 {
1035# if defined(FEAT_QUICKFIX)
1036 if (do_sp)
1037 {
1038 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
1039 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1040 }
1041# endif
1042 if (do_srr)
1043 {
1044 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
1045 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1046 }
1047 }
1048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 else
Natanael Copa56318362021-05-06 18:46:35 +02001050 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if ( fnamecmp(p, "sh") == 0
1052 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001053 || fnamecmp(p, "mksh") == 0
1054 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001056 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001058 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001059 || fnamecmp(p, "ash") == 0
1060 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001061 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01001062# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 || fnamecmp(p, "cmd") == 0
1064 || fnamecmp(p, "sh.exe") == 0
1065 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +02001066 || fnamecmp(p, "mksh.exe") == 0
1067 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001069 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 || fnamecmp(p, "bash.exe") == 0
1071 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +02001072 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +02001073 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001075 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001077# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (do_sp)
1079 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001080# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001082# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001083 if (fnamecmp(p, "pwsh") == 0)
1084 p_sp = (char_u *)">%s 2>&1";
1085 else
1086 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1089 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (do_srr)
1092 {
1093 p_srr = (char_u *)">%s 2>&1";
1094 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1095 }
1096 }
1097 vim_free(p);
1098 }
1099#endif
1100
Bram Moolenaar4f974752019-02-17 17:44:42 +01001101#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001103 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1104 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001106 * set, but only if they have not been set before.
1107 * Default values depend on shell (cmd.exe is default shell):
1108 *
1109 * p_shcf p_sxq
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001110 * cmd.exe - "/c" "("
Mike Williams12795022021-06-28 20:53:58 +02001111 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001112 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001113 * "sh" like shells - "-c" "\""
1114 *
1115 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
Mike Williams12795022021-06-28 20:53:58 +02001117 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1118 {
1119 int idx_opt;
1120
1121 idx_opt = findoption((char_u *)"shcf");
1122 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1123 {
1124 p_shcf = (char_u*)"-Command";
1125 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1126 }
1127
1128 idx_opt = findoption((char_u *)"sxq");
1129 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1130 {
1131 p_sxq = (char_u*)"\"";
1132 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1133 }
1134 }
1135 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {
1137 int idx3;
1138
1139 idx3 = findoption((char_u *)"shcf");
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_shcf = (char_u *)"-c";
1143 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1144 }
1145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001146 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001148 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
1150 p_sxq = (char_u *)"\"";
1151 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001154 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1155 {
1156 int idx3;
1157
1158 /*
1159 * cmd.exe on Windows will strip the first and last double quote given
1160 * on the command line, e.g. most of the time things like:
1161 * cmd /c "my path/to/echo" "my args to echo"
1162 * become:
1163 * my path/to/echo" "my args to echo
1164 * when executed.
1165 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001166 * To avoid this, set shellxquote to surround the command in
1167 * parenthesis. This appears to make most commands work, without
1168 * breaking commands that worked previously, such as
1169 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001170 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001171 idx3 = findoption((char_u *)"sxq");
1172 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1173 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001174 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001175 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1176 }
1177
Bram Moolenaara64ba222012-02-12 23:23:31 +01001178 idx3 = findoption((char_u *)"shcf");
1179 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1180 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001181 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001182 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1183 }
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001187 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001188 {
1189 int idx_ffs = findoption((char_u *)"ffs");
1190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001191 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001192 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1193 set_fileformat(default_fileformat(), OPT_LOCAL);
1194 }
1195
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197}
1198
1199#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1200/*
1201 * When 'helplang' is still at its default value, set it to "lang".
1202 * Only the first two characters of "lang" are used.
1203 */
1204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001205set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 int idx;
1208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001209 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 return;
1211 idx = findoption((char_u *)"hlg");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001212 if (idx < 0 || (options[idx].flags & P_WAS_SET))
1213 return;
1214
1215 if (options[idx].flags & P_ALLOCED)
1216 free_string_option(p_hlg);
1217 p_hlg = vim_strsave(lang);
1218 if (p_hlg == NULL)
1219 p_hlg = empty_option;
1220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001222 // zh_CN becomes "cn", zh_TW becomes "tw"
1223 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001224 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001225 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1226 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001227 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001228 // any C like setting, such as C.UTF-8, becomes "en"
1229 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1230 {
1231 p_hlg[0] = 'e';
1232 p_hlg[1] = 'n';
1233 }
1234 p_hlg[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001236 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237}
1238#endif
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240/*
1241 * 'title' and 'icon' only default to true if they have not been set or reset
1242 * in .vimrc and we can read the old value.
1243 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1244 * they can be reset. This reduces startup time when using X on a remote
1245 * machine.
1246 */
1247 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001248set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 int idx1;
1251 long val;
1252
1253 /*
1254 * If GUI is (going to be) used, we can always set the window title and
1255 * icon name. Saves a bit of time, because the X11 display server does
1256 * not need to be contacted.
1257 */
1258 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001259 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261#ifdef FEAT_GUI
1262 if (gui.starting || gui.in_use)
1263 val = TRUE;
1264 else
1265#endif
1266 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001267 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 p_title = val;
1269 }
1270 idx1 = findoption((char_u *)"icon");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001271 if (idx1 < 0 || (options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001273 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001275
1276#ifdef FEAT_GUI
1277 if (gui.starting || gui.in_use)
1278 val = TRUE;
1279 else
1280#endif
1281 val = mch_can_restore_icon();
1282 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
1283 p_icon = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001286 void
1287ex_set(exarg_T *eap)
1288{
1289 int flags = 0;
1290
1291 if (eap->cmdidx == CMD_setlocal)
1292 flags = OPT_LOCAL;
1293 else if (eap->cmdidx == CMD_setglobal)
1294 flags = OPT_GLOBAL;
1295#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001296 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001297 ex_options(eap);
1298 else
1299#endif
1300 {
1301 if (eap->forceit)
1302 flags |= OPT_ONECOLUMN;
1303 (void)do_set(eap->arg, flags);
1304 }
1305}
1306
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001307/*
1308 * :set operator types
1309 */
Bram Moolenaar47403942022-09-21 21:12:53 +01001310typedef enum {
1311 OP_NONE = 0,
1312 OP_ADDING, // "opt+=arg"
1313 OP_PREPENDING, // "opt^=arg"
1314 OP_REMOVING, // "opt-=arg"
1315} set_op_T;
1316
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00001317typedef enum {
1318 PREFIX_NO = 0, // "no" prefix
1319 PREFIX_NONE, // no prefix
1320 PREFIX_INV, // "inv" prefix
1321} set_prefix_T;
1322
1323/*
1324 * Return the prefix type for the option name in *argp.
1325 */
1326 static set_prefix_T
1327get_option_prefix(char_u **argp)
1328{
1329 int prefix = PREFIX_NONE;
1330 char_u *arg = *argp;
1331
1332 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
1333 {
1334 prefix = PREFIX_NO;
1335 arg += 2;
1336 }
1337 else if (STRNCMP(arg, "inv", 3) == 0)
1338 {
1339 prefix = PREFIX_INV;
1340 arg += 3;
1341 }
1342
1343 *argp = arg;
1344 return prefix;
1345}
1346
1347/*
1348 * Parse the option name in "arg" and return the option index in "*opt_idxp",
1349 * and the option name length in "*lenp". For a <t_xx> option, return the key
1350 * number in "*keyp".
1351 *
1352 * Returns FAIL if an option starting with "<" doesn't end with a ">",
1353 * otherwise returns OK.
1354 */
1355 static int
1356parse_option_name(char_u *arg, int *opt_idxp, int *lenp, int *keyp)
1357{
1358 int key = 0;
1359 int len;
1360 int opt_idx;
1361
1362 if (*arg == '<')
1363 {
1364 opt_idx = -1;
1365 // look out for <t_>;>
1366 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1367 len = 5;
1368 else
1369 {
1370 len = 1;
1371 while (arg[len] != NUL && arg[len] != '>')
1372 ++len;
1373 }
1374 if (arg[len] != '>')
1375 return FAIL;
1376
1377 arg[len] = NUL; // put NUL after name
1378 if (arg[1] == 't' && arg[2] == '_') // could be term code
1379 opt_idx = findoption(arg + 1);
1380 arg[len++] = '>'; // restore '>'
1381 if (opt_idx == -1)
1382 key = find_key_option(arg + 1, TRUE);
1383 }
1384 else
1385 {
1386 int nextchar; // next non-white char after option name
1387
1388 len = 0;
1389 /*
1390 * The two characters after "t_" may not be alphanumeric.
1391 */
1392 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1393 len = 4;
1394 else
1395 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1396 ++len;
1397 nextchar = arg[len];
1398 arg[len] = NUL; // put NUL after name
1399 opt_idx = findoption(arg);
1400 arg[len] = nextchar; // restore nextchar
1401 if (opt_idx == -1)
1402 key = find_key_option(arg, FALSE);
1403 }
1404
1405 *keyp = key;
1406 *lenp = len;
1407 *opt_idxp = opt_idx;
1408
1409 return OK;
1410}
1411
1412/*
1413 * Get the option operator (+=, ^=, -=).
1414 */
1415 static set_op_T
1416get_opt_op(char_u *arg)
1417{
1418 set_op_T op = OP_NONE;
1419
1420 if (*arg != NUL && *(arg + 1) == '=')
1421 {
1422 if (*arg == '+')
1423 op = OP_ADDING; // "+="
1424 else if (*arg == '^')
1425 op = OP_PREPENDING; // "^="
1426 else if (*arg == '-')
1427 op = OP_REMOVING; // "-="
1428 }
1429
1430 return op;
1431}
1432
1433/*
1434 * Validate whether the value of the option in "opt_idx" can be changed.
1435 * Returns FAIL if the option can be skipped or cannot be changed. Returns OK
1436 * if it can be changed.
1437 */
1438 static int
1439validate_opt_idx(int opt_idx, int opt_flags, long_u flags, char **errmsg)
1440{
1441 // Skip all options that are not window-local (used when showing
1442 // an already loaded buffer in a window).
1443 if ((opt_flags & OPT_WINONLY)
1444 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1445 return FAIL;
1446
1447 // Skip all options that are window-local (used for :vimgrep).
1448 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1449 && options[opt_idx].var == VAR_WIN)
1450 return FAIL;
1451
1452 // Disallow changing some options from modelines.
1453 if (opt_flags & OPT_MODELINE)
1454 {
1455 if (flags & (P_SECURE | P_NO_ML))
1456 {
1457 *errmsg = e_not_allowed_in_modeline;
1458 return FAIL;
1459 }
1460 if ((flags & P_MLE) && !p_mle)
1461 {
1462 *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off;
1463 return FAIL;
1464 }
1465#ifdef FEAT_DIFF
1466 // In diff mode some options are overruled. This avoids that
1467 // 'foldmethod' becomes "marker" instead of "diff" and that
1468 // "wrap" gets set.
1469 if (curwin->w_p_diff
1470 && opt_idx >= 0 // shut up coverity warning
1471 && (
1472# ifdef FEAT_FOLDING
1473 options[opt_idx].indir == PV_FDM ||
1474# endif
1475 options[opt_idx].indir == PV_WRAP))
1476 return FAIL;
1477#endif
1478 }
1479
1480#ifdef HAVE_SANDBOX
1481 // Disallow changing some options in the sandbox
1482 if (sandbox != 0 && (flags & P_SECURE))
1483 {
1484 *errmsg = e_not_allowed_in_sandbox;
1485 return FAIL;
1486 }
1487#endif
1488
1489 return OK;
1490}
1491
Bram Moolenaar47403942022-09-21 21:12:53 +01001492/*
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001493 * Get the Vim/Vi default value for a string option.
1494 */
1495 static char_u *
1496stropt_get_default_val(
1497 int opt_idx,
1498 char_u *varp,
1499 int flags,
1500 int cp_val)
1501{
1502 char_u *newval;
1503
1504 newval = options[opt_idx].def_val[((flags & P_VI_DEF) || cp_val)
1505 ? VI_DEFAULT : VIM_DEFAULT];
1506 if ((char_u **)varp == &p_bg)
1507 {
1508 // guess the value of 'background'
1509#ifdef FEAT_GUI
1510 if (gui.in_use)
1511 newval = gui_bg_default();
1512 else
1513#endif
1514 newval = term_bg_default();
1515 }
1516 else if ((char_u **)varp == &p_fencs && enc_utf8)
1517 newval = fencs_utf8_default;
1518
1519 // expand environment variables and ~ since the default value was
1520 // already expanded, only required when an environment variable was set
1521 // later
1522 if (newval == NULL)
1523 newval = empty_option;
1524 else
1525 {
1526 char_u *s = option_expand(opt_idx, newval);
1527 if (s == NULL)
1528 s = newval;
1529 newval = vim_strsave(s);
1530 }
1531
1532 return newval;
1533}
1534
1535/*
1536 * Convert the 'backspace' option number value to a string: for adding,
1537 * prepending and removing string.
1538 */
1539 static void
1540opt_backspace_nr2str(
1541 char_u *varp,
1542 char_u **origval_p,
1543 char_u **origval_l_p,
1544 char_u **origval_g_p,
1545 char_u **oldval_p)
1546{
1547 int i = getdigits((char_u **)varp);
1548
1549 switch (i)
1550 {
1551 case 0:
1552 *(char_u **)varp = empty_option;
1553 break;
1554 case 1:
1555 *(char_u **)varp = vim_strsave((char_u *)"indent,eol");
1556 break;
1557 case 2:
1558 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,start");
1559 break;
1560 case 3:
1561 *(char_u **)varp = vim_strsave((char_u *)"indent,eol,nostop");
1562 break;
1563 }
1564 vim_free(*oldval_p);
1565 if (*origval_p == *oldval_p)
1566 *origval_p = *(char_u **)varp;
1567 if (*origval_l_p == *oldval_p)
1568 *origval_l_p = *(char_u **)varp;
1569 if (*origval_g_p == *oldval_p)
1570 *origval_g_p = *(char_u **)varp;
1571 *oldval_p = *(char_u **)varp;
1572}
1573
1574/*
1575 * Convert the 'whichwrap' option number value to a string, for backwards
1576 * compatibility with Vim 3.0.
1577 * Note: 'argp' is a pointer to a char_u pointer and is updated.
1578 */
1579 static char_u *
1580opt_whichwrap_nr2str(char_u **argp, char_u *whichwrap)
1581{
1582 *whichwrap = NUL;
1583 int i = getdigits(argp);
1584 if (i & 1)
1585 STRCAT(whichwrap, "b,");
1586 if (i & 2)
1587 STRCAT(whichwrap, "s,");
1588 if (i & 4)
1589 STRCAT(whichwrap, "h,l,");
1590 if (i & 8)
1591 STRCAT(whichwrap, "<,>,");
1592 if (i & 16)
1593 STRCAT(whichwrap, "[,],");
1594 if (*whichwrap != NUL) // remove trailing ,
1595 whichwrap[STRLEN(whichwrap) - 1] = NUL;
1596
1597 return whichwrap;
1598}
1599
1600/*
1601 * Copy the new string value into allocated memory for the option.
1602 * Can't use set_string_option_direct(), because we need to remove the
1603 * backslashes.
1604 */
1605 static char_u *
1606stropt_copy_value(
1607 char_u *origval,
1608 char_u **argp,
1609 set_op_T op,
1610 int flags UNUSED)
1611{
1612 char_u *arg = *argp;
1613 unsigned newlen;
1614 char_u *newval;
1615 char_u *s = NULL;
1616
1617 // get a bit too much
1618 newlen = (unsigned)STRLEN(arg) + 1;
1619 if (op != OP_NONE)
1620 newlen += (unsigned)STRLEN(origval) + 1;
1621 newval = alloc(newlen);
1622 if (newval == NULL) // out of mem, don't change
1623 return NULL;
1624 s = newval;
1625
1626 // Copy the string, skip over escaped chars.
1627 // For MS-DOS and WIN32 backslashes before normal file name characters
1628 // are not removed, and keep backslash at start, for "\\machine\path",
1629 // but do remove it for "\\\\machine\\path".
1630 // The reverse is found in ExpandOldSetting().
1631 while (*arg != NUL && !VIM_ISWHITE(*arg))
1632 {
1633 int i;
1634
1635 if (*arg == '\\' && arg[1] != NUL
1636#ifdef BACKSLASH_IN_FILENAME
1637 && !((flags & P_EXPAND)
1638 && vim_isfilec(arg[1])
1639 && !VIM_ISWHITE(arg[1])
1640 && (arg[1] != '\\'
1641 || (s == newval && arg[2] != '\\')))
1642#endif
1643 )
1644 ++arg; // remove backslash
1645 if (has_mbyte && (i = (*mb_ptr2len)(arg)) > 1)
1646 {
1647 // copy multibyte char
1648 mch_memmove(s, arg, (size_t)i);
1649 arg += i;
1650 s += i;
1651 }
1652 else
1653 *s++ = *arg++;
1654 }
1655 *s = NUL;
1656
1657 *argp = arg;
1658 return newval;
1659}
1660
1661/*
1662 * Expand environment variables and ~ in string option value 'newval'.
1663 */
1664 static char_u *
1665stropt_expand_envvar(
1666 int opt_idx,
1667 char_u *origval,
1668 char_u *newval,
1669 set_op_T op)
1670{
1671 char_u *s = option_expand(opt_idx, newval);
1672 if (s == NULL)
1673 return newval;
1674
1675 vim_free(newval);
1676 unsigned newlen = (unsigned)STRLEN(s) + 1;
1677 if (op != OP_NONE)
1678 newlen += (unsigned)STRLEN(origval) + 1;
1679
1680 newval = alloc(newlen);
1681 if (newval == NULL)
1682 return NULL;
1683
1684 STRCPY(newval, s);
1685
1686 return newval;
1687}
1688
1689/*
1690 * Concatenate the original and new values of a string option, adding a "," if
1691 * needed.
1692 */
1693 static void
1694stropt_concat_with_comma(
1695 char_u *origval,
1696 char_u *newval,
1697 set_op_T op,
1698 int flags)
1699{
1700 int len = 0;
1701
1702 int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL);
1703 if (op == OP_ADDING)
1704 {
1705 len = (int)STRLEN(origval);
1706 // strip a trailing comma, would get 2
1707 if (comma && len > 1
1708 && (flags & P_ONECOMMA) == P_ONECOMMA
1709 && origval[len - 1] == ','
1710 && origval[len - 2] != '\\')
1711 len--;
1712 mch_memmove(newval + len + comma, newval, STRLEN(newval) + 1);
1713 mch_memmove(newval, origval, (size_t)len);
1714 }
1715 else
1716 {
1717 len = (int)STRLEN(newval);
1718 STRMOVE(newval + len + comma, origval);
1719 }
1720 if (comma)
1721 newval[len] = ',';
1722}
1723
1724/*
1725 * Remove a value from a string option. Copy string option value in "origval"
1726 * to "newval" and then remove the string "strval" of length "len".
1727 */
1728 static void
1729stropt_remove_val(
1730 char_u *origval,
1731 char_u *newval,
1732 int flags,
1733 char_u *strval,
1734 int len)
1735{
1736 // Remove newval[] from origval[]. (Note: "len" has been set above
1737 // and is used here).
1738 STRCPY(newval, origval);
1739 if (*strval)
1740 {
1741 // may need to remove a comma
1742 if (flags & P_COMMA)
1743 {
1744 if (strval == origval)
1745 {
1746 // include comma after string
1747 if (strval[len] == ',')
1748 ++len;
1749 }
1750 else
1751 {
1752 // include comma before string
1753 --strval;
1754 ++len;
1755 }
1756 }
1757 STRMOVE(newval + (strval - origval), strval + len);
1758 }
1759}
1760
1761/*
1762 * Remove flags that appear twice in the string option value 'newval'.
1763 */
1764 static void
1765stropt_remove_dupflags(char_u *newval, int flags)
1766{
1767 char_u *s = newval;
1768
1769 // Remove flags that appear twice.
1770 while (*s)
1771 {
1772 // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap'
1773 if (flags & P_ONECOMMA)
1774 {
1775 if (*s != ',' && *(s + 1) == ',' && vim_strchr(s + 2, *s) != NULL)
1776 {
1777 // Remove the duplicated value and the next comma.
1778 STRMOVE(s, s + 2);
1779 continue;
1780 }
1781 }
1782 else
1783 {
1784 if ((!(flags & P_COMMA) || *s != ',')
1785 && vim_strchr(s + 1, *s) != NULL)
1786 {
1787 STRMOVE(s, s + 1);
1788 continue;
1789 }
1790 }
1791 ++s;
1792 }
1793}
1794
1795/*
1796 * Get the string value specified for a ":set" command. The following set
1797 * options are supported:
1798 * set {opt}&
1799 * set {opt}<
1800 * set {opt}={val}
1801 * set {opt}:{val}
1802 */
1803 static char_u *
1804stropt_get_newval(
1805 int nextchar,
1806 int opt_idx,
1807 char_u **argp,
1808 char_u *varp,
1809 char_u **origval_arg,
1810 char_u **origval_l_arg,
1811 char_u **origval_g_arg,
1812 char_u **oldval_arg,
1813 set_op_T *op_arg,
1814 int flags,
1815 int cp_val)
1816{
1817 char_u *arg = *argp;
1818 char_u *origval = *origval_arg;
1819 char_u *origval_l = *origval_l_arg;
1820 char_u *origval_g = *origval_g_arg;
1821 char_u *oldval = *oldval_arg;
1822 set_op_T op = *op_arg;
1823 char_u *save_arg = NULL;
1824 char_u *newval;
1825 char_u *s = NULL;
1826 char_u whichwrap[80];
1827
1828 if (nextchar == '&') // set to default val
1829 newval = stropt_get_default_val(opt_idx, varp, flags, cp_val);
1830 else if (nextchar == '<') // set to global val
1831 newval = vim_strsave(*(char_u **)get_varp_scope(
1832 &(options[opt_idx]), OPT_GLOBAL));
1833 else
1834 {
1835 ++arg; // jump to after the '=' or ':'
1836
1837 // Set 'keywordprg' to ":help" if an empty
1838 // value was passed to :set by the user.
1839 if (varp == (char_u *)&p_kp && (*arg == NUL || *arg == ' '))
1840 {
1841 save_arg = arg;
1842 arg = (char_u *)":help";
1843 }
1844 // Convert 'backspace' number to string
1845 else if (varp == (char_u *)&p_bs && VIM_ISDIGIT(**(char_u **)varp))
1846 opt_backspace_nr2str(varp, &origval, &origval_l, &origval_g,
1847 &oldval);
1848 else if (varp == (char_u *)&p_ww && VIM_ISDIGIT(*arg))
1849 {
1850 // Convert 'whichwrap' number to string, for backwards
1851 // compatibility with Vim 3.0.
1852 char_u *t = opt_whichwrap_nr2str(&arg, whichwrap);
1853 save_arg = arg;
1854 arg = t;
1855 }
1856 // Remove '>' before 'dir' and 'bdir', for backwards compatibility with
1857 // version 3.0
1858 else if (*arg == '>' && (varp == (char_u *)&p_dir
1859 || varp == (char_u *)&p_bdir))
1860 ++arg;
1861
1862 // Copy the new string into allocated memory.
1863 newval = stropt_copy_value(origval, &arg, op, flags);
1864 if (newval == NULL)
1865 goto done;
1866
1867 // Expand environment variables and ~.
1868 // Don't do it when adding without inserting a comma.
1869 if (op == OP_NONE || (flags & P_COMMA))
1870 {
1871 newval = stropt_expand_envvar(opt_idx, origval, newval, op);
1872 if (newval == NULL)
1873 goto done;
1874 }
1875
1876 // locate newval[] in origval[] when removing it and when adding to
1877 // avoid duplicates
1878 int len = 0;
1879 if (op == OP_REMOVING || (flags & P_NODUP))
1880 {
1881 len = (int)STRLEN(newval);
1882 s = find_dup_item(origval, newval, flags);
1883
1884 // do not add if already there
1885 if ((op == OP_ADDING || op == OP_PREPENDING) && s != NULL)
1886 {
1887 op = OP_NONE;
1888 STRCPY(newval, origval);
1889 }
1890
1891 // if no duplicate, move pointer to end of original value
1892 if (s == NULL)
1893 s = origval + (int)STRLEN(origval);
1894 }
1895
1896 // concatenate the two strings; add a ',' if needed
1897 if (op == OP_ADDING || op == OP_PREPENDING)
1898 stropt_concat_with_comma(origval, newval, op, flags);
1899 else if (op == OP_REMOVING)
1900 // Remove newval[] from origval[]. (Note: "len" has been set above
1901 // and is used here).
1902 stropt_remove_val(origval, newval, flags, s, len);
1903
1904 if (flags & P_FLAGLIST)
1905 // Remove flags that appear twice.
1906 stropt_remove_dupflags(newval, flags);
1907 }
1908
1909done:
1910 if (save_arg != NULL)
1911 arg = save_arg; // arg was temporarily changed, restore it
1912 *argp = arg;
1913 *origval_arg = origval;
1914 *origval_l_arg = origval_l;
1915 *origval_g_arg = origval_g;
1916 *oldval_arg = oldval;
1917 *op_arg = op;
1918
1919 return newval;
1920}
1921
1922/*
Bram Moolenaar47403942022-09-21 21:12:53 +01001923 * Part of do_set() for string options.
1924 * Returns FAIL on failure, do not process further options.
1925 */
1926 static int
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001927do_set_option_string(
Bram Moolenaar47403942022-09-21 21:12:53 +01001928 int opt_idx,
1929 int opt_flags,
Bram Moolenaar6f981142022-09-22 12:48:58 +01001930 char_u **argp,
Bram Moolenaar47403942022-09-21 21:12:53 +01001931 int nextchar,
1932 set_op_T op_arg,
1933 int flags,
1934 int cp_val,
1935 char_u *varp_arg,
1936 char *errbuf,
1937 int *value_checked,
1938 char **errmsg)
1939{
Bram Moolenaar6f981142022-09-22 12:48:58 +01001940 char_u *arg = *argp;
Bram Moolenaar47403942022-09-21 21:12:53 +01001941 set_op_T op = op_arg;
1942 char_u *varp = varp_arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01001943 char_u *oldval = NULL; // previous value if *varp
1944 char_u *newval;
1945 char_u *origval = NULL;
1946 char_u *origval_l = NULL;
1947 char_u *origval_g = NULL;
1948#if defined(FEAT_EVAL)
1949 char_u *saved_origval = NULL;
1950 char_u *saved_origval_l = NULL;
1951 char_u *saved_origval_g = NULL;
1952 char_u *saved_newval = NULL;
1953#endif
Bram Moolenaar47403942022-09-21 21:12:53 +01001954
1955 // When using ":set opt=val" for a global option
1956 // with a local value the local value will be
1957 // reset, use the global value here.
1958 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
1959 && ((int)options[opt_idx].indir & PV_BOTH))
1960 varp = options[opt_idx].var;
1961
1962 // The old value is kept until we are sure that the new value is valid.
1963 oldval = *(char_u **)varp;
1964
1965 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1966 {
1967 origval_l = *(char_u **)get_varp_scope(
1968 &(options[opt_idx]), OPT_LOCAL);
1969 origval_g = *(char_u **)get_varp_scope(
1970 &(options[opt_idx]), OPT_GLOBAL);
1971
1972 // A global-local string option might have an empty option as value to
1973 // indicate that the global value should be used.
1974 if (((int)options[opt_idx].indir & PV_BOTH)
1975 && origval_l == empty_option)
1976 origval_l = origval_g;
1977 }
1978
1979 // When setting the local value of a global option, the old value may be
1980 // the global value.
1981 if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL))
1982 origval = *(char_u **)get_varp(&options[opt_idx]);
1983 else
1984 origval = oldval;
1985
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001986 // Get the new value for the option
1987 newval = stropt_get_newval(nextchar, opt_idx, &arg, varp, &origval,
1988 &origval_l, &origval_g, &oldval, &op, flags,
1989 cp_val);
Bram Moolenaar47403942022-09-21 21:12:53 +01001990
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00001991 // Set the new value.
Bram Moolenaar47403942022-09-21 21:12:53 +01001992 *(char_u **)(varp) = newval;
1993
1994#if defined(FEAT_EVAL)
1995 if (!starting
1996# ifdef FEAT_CRYPT
1997 && options[opt_idx].indir != PV_KEY
1998# endif
1999 && origval != NULL && newval != NULL)
2000 {
2001 // origval may be freed by did_set_string_option(), make a copy.
2002 saved_origval = vim_strsave(origval);
2003 // newval (and varp) may become invalid if the buffer is closed by
2004 // autocommands.
2005 saved_newval = vim_strsave(newval);
2006 if (origval_l != NULL)
2007 saved_origval_l = vim_strsave(origval_l);
2008 if (origval_g != NULL)
2009 saved_origval_g = vim_strsave(origval_g);
2010 }
2011#endif
2012
2013 {
2014 long_u *p = insecure_flag(opt_idx, opt_flags);
2015 int secure_saved = secure;
2016
2017 // When an option is set in the sandbox, from a modeline or in secure
2018 // mode, then deal with side effects in secure mode. Also when the
2019 // value was set with the P_INSECURE flag and is not completely
2020 // replaced.
2021 if ((opt_flags & OPT_MODELINE)
2022#ifdef HAVE_SANDBOX
2023 || sandbox != 0
2024#endif
2025 || (op != OP_NONE && (*p & P_INSECURE)))
2026 secure = 1;
2027
2028 // Handle side effects, and set the global value for ":set" on local
2029 // options. Note: when setting 'syntax' or 'filetype' autocommands may
2030 // be triggered that can cause havoc.
2031 *errmsg = did_set_string_option(
2032 opt_idx, (char_u **)varp, oldval, errbuf,
2033 opt_flags, value_checked);
2034
2035 secure = secure_saved;
2036 }
2037
2038#if defined(FEAT_EVAL)
2039 if (*errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +00002040 trigger_optionset_string(opt_idx, opt_flags, saved_origval,
Bram Moolenaar47403942022-09-21 21:12:53 +01002041 saved_origval_l, saved_origval_g, saved_newval);
2042 vim_free(saved_origval);
2043 vim_free(saved_origval_l);
2044 vim_free(saved_origval_g);
2045 vim_free(saved_newval);
2046#endif
2047
Bram Moolenaar6f981142022-09-22 12:48:58 +01002048 *argp = arg;
Bram Moolenaar47403942022-09-21 21:12:53 +01002049 return *errmsg == NULL ? OK : FAIL;
2050}
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002053 * Set a boolean option.
2054 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002055 */
2056 static char *
2057do_set_option_bool(
2058 int opt_idx,
2059 int opt_flags,
2060 set_prefix_T prefix,
2061 long_u flags,
2062 char_u *varp,
2063 int nextchar,
2064 int afterchar,
2065 int cp_val)
2066
2067{
2068 varnumber_T value;
2069
2070 if (nextchar == '=' || nextchar == ':')
2071 return e_invalid_argument;
Bram Moolenaar546933f2023-02-06 16:40:49 +00002072 if (opt_idx < 0 || varp == NULL)
2073 return NULL; // "cannot happen"
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002074
2075 /*
2076 * ":set opt!": invert
2077 * ":set opt&": reset to default value
2078 * ":set opt<": reset to global value
2079 */
2080 if (nextchar == '!')
2081 value = *(int *)(varp) ^ 1;
2082 else if (nextchar == '&')
2083 value = (int)(long)(long_i)options[opt_idx].def_val[
2084 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2085 else if (nextchar == '<')
2086 {
2087 // For 'autoread' -1 means to use global value.
2088 if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL)
2089 value = -1;
2090 else
2091 value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2092 }
2093 else
2094 {
2095 /*
2096 * ":set invopt": invert
2097 * ":set opt" or ":set noopt": set or reset
2098 */
2099 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
2100 return e_trailing_characters;
2101 if (prefix == PREFIX_INV)
2102 value = *(int *)(varp) ^ 1;
2103 else
2104 value = prefix == PREFIX_NO ? 0 : 1;
2105 }
2106
2107 return set_bool_option(opt_idx, varp, (int)value, opt_flags);
2108}
2109
2110/*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002111 * Set a numeric option.
2112 * Returns an untranslated error message or NULL.
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002113 */
2114 static char *
2115do_set_option_numeric(
2116 int opt_idx,
2117 int opt_flags,
2118 char_u **argp,
2119 int nextchar,
2120 set_op_T op,
2121 long_u flags,
2122 int cp_val,
2123 char_u *varp,
2124 char *errbuf,
2125 size_t errbuflen)
2126{
2127 char_u *arg = *argp;
2128 varnumber_T value;
2129 int i;
2130 char *errmsg = NULL;
2131
Bram Moolenaar546933f2023-02-06 16:40:49 +00002132 if (opt_idx < 0 || varp == NULL)
2133 return NULL; // "cannot happen"
2134 //
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002135 /*
2136 * Different ways to set a number option:
2137 * & set to default value
2138 * < set to global value
2139 * <xx> accept special key codes for 'wildchar'
2140 * c accept any non-digit for 'wildchar'
2141 * [-]0-9 set number
2142 * other error
2143 */
2144 ++arg;
2145 if (nextchar == '&')
2146 value = (long)(long_i)options[opt_idx].def_val[
2147 ((flags & P_VI_DEF) || cp_val) ? VI_DEFAULT : VIM_DEFAULT];
2148 else if (nextchar == '<')
2149 {
2150 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
2151 // use the global value.
2152 if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL)
2153 value = NO_LOCAL_UNDOLEVEL;
2154 else
2155 value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
2156 }
2157 else if (((long *)varp == &p_wc || (long *)varp == &p_wcm)
2158 && (*arg == '<'
2159 || *arg == '^'
2160 || (*arg != NUL
2161 && (!arg[1] || VIM_ISWHITE(arg[1]))
2162 && !VIM_ISDIGIT(*arg))))
2163 {
2164 value = string_to_key(arg, FALSE);
2165 if (value == 0 && (long *)varp != &p_wcm)
2166 {
2167 errmsg = e_invalid_argument;
2168 goto skip;
2169 }
2170 }
2171 else if (*arg == '-' || VIM_ISDIGIT(*arg))
2172 {
2173 // Allow negative (for 'undolevels'), octal and hex numbers.
2174 vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, TRUE);
2175 if (i == 0 || (arg[i] != NUL && !VIM_ISWHITE(arg[i])))
2176 {
2177 errmsg = e_number_required_after_equal;
2178 goto skip;
2179 }
2180 }
2181 else
2182 {
2183 errmsg = e_number_required_after_equal;
2184 goto skip;
2185 }
2186
2187 if (op == OP_ADDING)
2188 value = *(long *)varp + value;
2189 else if (op == OP_PREPENDING)
2190 value = *(long *)varp * value;
2191 else if (op == OP_REMOVING)
2192 value = *(long *)varp - value;
2193
2194 errmsg = set_num_option(opt_idx, varp, value, errbuf, errbuflen,
2195 opt_flags);
2196
2197skip:
2198 *argp = arg;
2199 return errmsg;
2200}
2201
2202/*
2203 * Set a key code (t_xx) option
2204 */
2205 static char *
2206do_set_option_keycode(char_u **argp, char_u *key_name, int nextchar)
2207{
2208 char_u *arg = *argp;
2209 char_u *p;
2210
2211 if (nextchar == '&')
2212 {
2213 if (add_termcap_entry(key_name, TRUE) == FAIL)
2214 return e_not_found_in_termcap;
2215 }
2216 else
2217 {
2218 ++arg; // jump to after the '=' or ':'
2219 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
2220 if (*p == '\\' && p[1] != NUL)
2221 ++p;
2222 nextchar = *p;
2223 *p = NUL;
2224 add_termcode(key_name, arg, FALSE);
2225 *p = nextchar;
2226 }
2227 if (full_screen)
2228 ttest(FALSE);
2229 redraw_all_later(UPD_CLEAR);
2230
2231 *argp = arg;
2232 return NULL;
2233}
2234
2235/*
2236 * Set an option to a new value.
2237 */
2238 static char *
2239do_set_option_value(
2240 int opt_idx,
2241 int opt_flags,
2242 char_u **argp,
2243 set_prefix_T prefix,
2244 set_op_T op,
2245 long_u flags,
2246 char_u *varp,
2247 char_u *key_name,
2248 int nextchar,
2249 int afterchar,
2250 int cp_val,
2251 int *stopopteval,
2252 char *errbuf,
2253 size_t errbuflen)
2254{
2255 int value_checked = FALSE;
2256 char *errmsg = NULL;
2257 char_u *arg = *argp;
2258
2259 if (flags & P_BOOL)
2260 {
2261 // boolean option
2262 errmsg = do_set_option_bool(opt_idx, opt_flags, prefix, flags, varp,
2263 nextchar, afterchar, cp_val);
2264 if (errmsg != NULL)
2265 goto skip;
2266 }
2267 else
2268 {
2269 // numeric or string option
2270 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
2271 || prefix != PREFIX_NONE)
2272 {
2273 errmsg = e_invalid_argument;
2274 goto skip;
2275 }
2276
2277 if (flags & P_NUM)
2278 {
2279 // numeric option
2280 errmsg = do_set_option_numeric(opt_idx, opt_flags, &arg, nextchar,
2281 op, flags, cp_val, varp,
2282 errbuf, errbuflen);
2283 if (errmsg != NULL)
2284 goto skip;
2285 }
2286 else if (opt_idx >= 0)
2287 {
2288 // string option
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00002289 if (do_set_option_string(opt_idx, opt_flags, &arg, nextchar, op,
2290 flags, cp_val, varp, errbuf,
2291 &value_checked, &errmsg) == FAIL)
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002292 {
2293 if (errmsg != NULL)
2294 goto skip;
2295 *stopopteval = TRUE;
2296 goto skip;
2297 }
2298 }
2299 else
2300 {
2301 // key code option
2302 errmsg = do_set_option_keycode(&arg, key_name, nextchar);
2303 if (errmsg != NULL)
2304 goto skip;
2305 }
2306 }
2307
2308 if (opt_idx >= 0)
2309 did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked);
2310
2311skip:
2312 *argp = arg;
2313 return errmsg;
2314}
2315
2316/*
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002317 * Set an option to a new value.
2318 * Return NULL if OK, return an untranslated error message when something is
2319 * wrong. "errbuf[errbuflen]" can be used to create the error message.
2320 */
2321 static char *
2322do_set_option(
2323 int opt_flags,
2324 char_u **argp,
2325 char_u *arg_start,
2326 char_u **startarg,
2327 int *did_show,
2328 int *stopopteval,
2329 char *errbuf,
2330 size_t errbuflen)
2331{
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002332 int opt_idx;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002333 char_u *arg;
2334 set_prefix_T prefix; // no prefix, "no" prefix or "inv" prefix
2335 set_op_T op;
2336 long_u flags; // flags for current option
2337 char_u *varp; // pointer to variable for current option
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002338 char_u key_name[2];
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002339 int nextchar; // next non-white char after option name
2340 int afterchar; // character just after option name
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002341 char *errmsg = NULL;
2342 int key;
2343 int len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002344
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002345 prefix = get_option_prefix(argp);
2346 arg = *argp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002347
2348 // find end of name
2349 key = 0;
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002350 if (parse_option_name(arg, &opt_idx, &len, &key) == FAIL)
2351 return e_invalid_argument;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002352
2353 // remember character after option name
2354 afterchar = arg[len];
2355
2356 if (in_vim9script())
2357 {
2358 char_u *p = skipwhite(arg + len);
2359
2360 // disallow white space before =val, +=val, -=val, ^=val
2361 if (p > arg + len && (p[0] == '='
2362 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
2363 && p[1] == '=')))
2364 {
2365 errmsg = e_no_white_space_allowed_between_option_and;
2366 arg = p;
2367 *startarg = p;
2368 goto skip;
2369 }
2370 }
2371 else
2372 // skip white space, allow ":set ai ?", ":set hlsearch !"
2373 while (VIM_ISWHITE(arg[len]))
2374 ++len;
2375
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002376 op = get_opt_op(arg + len);
2377 if (op != OP_NONE)
2378 len++;
2379
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002380 nextchar = arg[len];
2381
2382 if (opt_idx == -1 && key == 0) // found a mismatch: skip
2383 {
2384 if (in_vim9script() && arg > arg_start
2385 && vim_strchr((char_u *)"!&<", *arg) != NULL)
2386 errmsg = e_no_white_space_allowed_between_option_and;
2387 else
2388 errmsg = e_unknown_option;
2389 goto skip;
2390 }
2391
2392 if (opt_idx >= 0)
2393 {
2394 if (options[opt_idx].var == NULL) // hidden option: skip
2395 {
2396 // Only give an error message when requesting the value of
2397 // a hidden option, ignore setting it.
2398 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
2399 && (!(options[opt_idx].flags & P_BOOL)
2400 || nextchar == '?'))
2401 errmsg = e_option_not_supported;
2402 goto skip;
2403 }
2404
2405 flags = options[opt_idx].flags;
2406 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
2407 }
2408 else
2409 {
2410 flags = P_STRING;
Bram Moolenaar40b48722023-02-05 17:04:50 +00002411 varp = NULL;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002412 if (key < 0)
2413 {
2414 key_name[0] = KEY2TERMCAP0(key);
2415 key_name[1] = KEY2TERMCAP1(key);
2416 }
2417 else
2418 {
2419 key_name[0] = KS_KEY;
2420 key_name[1] = (key & 0xff);
2421 }
2422 }
2423
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002424 // Make sure the option value can be changed.
2425 if (validate_opt_idx(opt_idx, opt_flags, flags, &errmsg) == FAIL)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002426 goto skip;
2427
Bram Moolenaar40b48722023-02-05 17:04:50 +00002428 int cp_val = p_cp;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002429 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
2430 {
2431 arg += len;
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002432 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
2433 {
2434 if (arg[3] == 'm') // "opt&vim": set to Vim default
2435 {
2436 cp_val = FALSE;
2437 arg += 3;
2438 }
2439 else // "opt&vi": set to Vi default
2440 {
2441 cp_val = TRUE;
2442 arg += 2;
2443 }
2444 }
2445 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
2446 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
2447 {
2448 errmsg = e_trailing_characters;
2449 goto skip;
2450 }
2451 }
2452
2453 /*
Bram Moolenaar546933f2023-02-06 16:40:49 +00002454 * Allow '=' and ':' for historical reasons (MSDOS command.com).
2455 * Allows only one '=' character per "set" command line. grrr. (jw)
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002456 */
2457 if (nextchar == '?'
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002458 || (prefix == PREFIX_NONE
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002459 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
2460 && !(flags & P_BOOL)))
2461 {
2462 /*
2463 * print value
2464 */
2465 if (*did_show)
2466 msg_putchar('\n'); // cursor below last one
2467 else
2468 {
2469 gotocmdline(TRUE); // cursor at status line
2470 *did_show = TRUE; // remember that we did a line
2471 }
2472 if (opt_idx >= 0)
2473 {
2474 showoneopt(&options[opt_idx], opt_flags);
2475#ifdef FEAT_EVAL
2476 if (p_verbose > 0)
2477 {
2478 // Mention where the option was last set.
2479 if (varp == options[opt_idx].var)
2480 last_set_msg(options[opt_idx].script_ctx);
2481 else if ((int)options[opt_idx].indir & PV_WIN)
2482 last_set_msg(curwin->w_p_script_ctx[
2483 (int)options[opt_idx].indir & PV_MASK]);
2484 else if ((int)options[opt_idx].indir & PV_BUF)
2485 last_set_msg(curbuf->b_p_script_ctx[
2486 (int)options[opt_idx].indir & PV_MASK]);
2487 }
2488#endif
2489 }
2490 else
2491 {
2492 char_u *p;
2493
2494 p = find_termcode(key_name);
2495 if (p == NULL)
2496 {
2497 errmsg = e_key_code_not_set;
2498 goto skip;
2499 }
2500 else
2501 (void)show_one_termcode(key_name, p, TRUE);
2502 }
2503 if (nextchar != '?'
2504 && nextchar != NUL && !VIM_ISWHITE(afterchar))
2505 errmsg = e_trailing_characters;
2506 }
2507 else
2508 {
Yegappan Lakshmananc72078b2023-02-05 16:02:35 +00002509 errmsg = do_set_option_value(opt_idx, opt_flags, &arg, prefix, op,
2510 flags, varp, key_name, nextchar,
2511 afterchar, cp_val, stopopteval, errbuf,
2512 errbuflen);
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002513 }
2514
2515skip:
2516 *argp = arg;
2517 return errmsg;
2518}
2519
2520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * Parse 'arg' for option settings.
2522 *
2523 * 'arg' may be IObuff, but only when no errors can be present and option
2524 * does not need to be expanded with option_expand().
2525 * "opt_flags":
2526 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002527 * OPT_GLOBAL for ":setglobal"
2528 * OPT_LOCAL for ":setlocal" and a modeline
2529 * OPT_MODELINE for a modeline
2530 * OPT_WINONLY to only set window-local options
2531 * OPT_NOWIN to skip setting window-local options
2532 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 *
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002534 * Returns FAIL if an error is detected, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
2536 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002537do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02002538 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01002539 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaar1594f312021-07-08 16:40:13 +02002541 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 int i;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002543 int did_show = FALSE; // already showed one value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 if (*arg == NUL)
2546 {
2547 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002548 did_show = TRUE;
2549 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
2551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002552 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
zeertzjq0ef9a5c2023-01-17 21:38:25 +00002554 if (STRNCMP(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3])
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002555 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 /*
2558 * ":set all" show all options.
2559 * ":set all&" set all options to their default value.
2560 */
2561 arg += 3;
2562 if (*arg == '&')
2563 {
2564 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002565 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002567 didset_options();
2568 didset_options2();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002569 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002574 did_show = TRUE;
2575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002577 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00002580 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002581 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 arg += 7;
2583 }
2584 else
2585 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002586 int stopopteval = FALSE;
2587 char *errmsg = NULL;
2588 char errbuf[80];
2589 char_u *startarg = arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002591 errmsg = do_set_option(opt_flags, &arg, arg_start, &startarg,
2592 &did_show, &stopopteval, errbuf,
2593 sizeof(errbuf));
2594 if (stopopteval)
2595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 /*
2598 * Advance to next argument.
2599 * - skip until a blank found, taking care of backslashes
2600 * - skip blanks
2601 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2602 */
2603 for (i = 0; i < 2 ; ++i)
2604 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002605 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (*arg++ == '\\' && *arg != NUL)
2607 ++arg;
2608 arg = skipwhite(arg);
2609 if (*arg != '=')
2610 break;
2611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002613 if (errmsg != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +00002615 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
2616 i = (int)STRLEN(IObuff) + 2;
2617 if (i + (arg - startarg) < IOSIZE)
2618 {
2619 // append the argument with the error
2620 STRCAT(IObuff, ": ");
2621 mch_memmove(IObuff + i, startarg, (arg - startarg));
2622 IObuff[i + (arg - startarg)] = NUL;
2623 }
2624 // make sure all characters are printable
2625 trans_characters(IObuff, IOSIZE);
2626
2627 ++no_wait_return; // wait_return() done later
2628 emsg((char *)IObuff); // show error highlighted
2629 --no_wait_return;
2630
2631 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634
2635 arg = skipwhite(arg);
2636 }
2637
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638theend:
2639 if (silent_mode && did_show)
2640 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002641 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002642 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002643 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002645 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 out_flush();
2647 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002648 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002649 }
2650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 return OK;
2652}
2653
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002654/*
2655 * Call this when an option has been given a new value through a user command.
2656 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2657 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002659did_set_option(
2660 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002661 int opt_flags, // possibly with OPT_MODELINE
2662 int new_value, // value was replaced completely
2663 int value_checked) // value was checked to be safe, no need to set the
2664 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002665{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002666 long_u *p;
2667
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002668 options[opt_idx].flags |= P_WAS_SET;
2669
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 // When an option is set in the sandbox, from a modeline or in secure mode
2671 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2672 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002673 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002674 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002675#ifdef HAVE_SANDBOX
2676 || sandbox != 0
2677#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002678 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002679 *p = *p | P_INSECURE;
2680 else if (new_value)
2681 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002682}
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684/*
2685 * Convert a key name or string into a key value.
2686 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002687 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002689 int
2690string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002693 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (*arg == '^')
2695 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002696 if (multi_byte)
2697 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 return *arg;
2699}
2700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701/*
2702 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2703 * maketitle() to create and display it.
2704 * When switching the title or icon off, call mch_restore_title() to get
2705 * the old value back.
2706 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002707 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002708did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 if (starting != NO_SCREEN
2711#ifdef FEAT_GUI
2712 && !gui.starting
2713#endif
2714 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718/*
2719 * set_options_bin - called when 'bin' changes value.
2720 */
2721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002722set_options_bin(
2723 int oldval,
2724 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 /*
2728 * The option values that are changed when 'bin' changes are
2729 * copied when 'bin is set and restored when 'bin' is reset.
2730 */
2731 if (newval)
2732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
2735 if (!(opt_flags & OPT_GLOBAL))
2736 {
2737 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2738 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2739 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2740 curbuf->b_p_et_nobin = curbuf->b_p_et;
2741 }
2742 if (!(opt_flags & OPT_LOCAL))
2743 {
2744 p_tw_nobin = p_tw;
2745 p_wm_nobin = p_wm;
2746 p_ml_nobin = p_ml;
2747 p_et_nobin = p_et;
2748 }
2749 }
2750
2751 if (!(opt_flags & OPT_GLOBAL))
2752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 curbuf->b_p_tw = 0; // no automatic line wrap
2754 curbuf->b_p_wm = 0; // no automatic line wrap
2755 curbuf->b_p_ml = 0; // no modelines
2756 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 if (!(opt_flags & OPT_LOCAL))
2759 {
2760 p_tw = 0;
2761 p_wm = 0;
2762 p_ml = FALSE;
2763 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
2769 if (!(opt_flags & OPT_GLOBAL))
2770 {
2771 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2772 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2773 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2774 curbuf->b_p_et = curbuf->b_p_et_nobin;
2775 }
2776 if (!(opt_flags & OPT_LOCAL))
2777 {
2778 p_tw = p_tw_nobin;
2779 p_wm = p_wm_nobin;
2780 p_ml = p_ml_nobin;
2781 p_et = p_et_nobin;
2782 }
2783 }
2784}
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786/*
2787 * Expand environment variables for some string options.
2788 * These string options cannot be indirect!
2789 * If "val" is NULL expand the current value of the option.
2790 * Return pointer to NameBuff, or NULL when not expanded.
2791 */
2792 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002793option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2797 return NULL;
2798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 // If val is longer than MAXPATHL no meaningful expansion can be done,
2800 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (val != NULL && STRLEN(val) > MAXPATHL)
2802 return NULL;
2803
2804 if (val == NULL)
2805 val = *(char_u **)options[opt_idx].var;
2806
2807 /*
2808 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2809 * Escape spaces when expanding 'tags', they are used to separate file
2810 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002811 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 */
2813 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002814 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002815#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002816 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2817#endif
2818 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002819 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 return NULL;
2821
2822 return NameBuff;
2823}
2824
2825/*
2826 * After setting various option values: recompute variables that depend on
2827 * option values.
2828 */
2829 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002830didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 (void)init_chartab();
2834
Bram Moolenaardac13472019-09-16 21:06:21 +02002835 didset_string_options();
2836
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002837#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002838 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002839 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002840 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002841 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002842#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002843 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 (void)check_cedit();
Bram Moolenaar597a4222014-06-25 14:39:50 +02002845#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002847 fill_breakat_flags();
2848#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002849 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002850}
2851
2852/*
2853 * More side effects of setting options.
2854 */
2855 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002856didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002857{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002859 (void)highlight_changed();
2860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002862 check_opt_wim();
2863
Bram Moolenaareed9d462021-02-15 20:38:25 +01002864 // Parse default for 'listchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002865 (void)set_chars_option(curwin, &curwin->w_p_lcs, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // Parse default for 'fillchars'.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002868 (void)set_chars_option(curwin, &curwin->w_p_fcs, TRUE);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002869
2870#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002872 (void)check_clipboard_option();
2873#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002874#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002875 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002876 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002877 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002878 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880}
2881
2882/*
2883 * Check for string options that are NULL (normally only termcap options).
2884 */
2885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002886check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 int opt_idx;
2889
2890 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2891 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2892 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2893}
2894
2895/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002896 * Return the option index found by a pointer into term_strings[].
2897 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002899 int
2900get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002902 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2905 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002906 return opt_idx;
2907 return -1; // cannot happen: didn't find it!
2908}
2909
2910/*
2911 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2912 * Return the option index or -1 if not found.
2913 */
2914 int
2915set_term_option_alloced(char_u **p)
2916{
2917 int opt_idx = get_term_opt_idx(p);
2918
2919 if (opt_idx >= 0)
2920 options[opt_idx].flags |= P_ALLOCED;
2921 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922}
2923
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002924#if defined(FEAT_EVAL) || defined(PROTO)
2925/*
2926 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2927 * Return FALSE when it wasn't.
2928 * Return -1 for an unknown option.
2929 */
2930 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002931was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002932{
2933 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002934 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002935
2936 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002937 {
2938 flagp = insecure_flag(idx, opt_flags);
2939 return (*flagp & P_INSECURE) != 0;
2940 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002941 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002942 return -1;
2943}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002944
2945/*
2946 * Get a pointer to the flags used for the P_INSECURE flag of option
2947 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002948 * NOTE: Caller must make sure that "curwin" is set to the window from which
2949 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002950 */
2951 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002952insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002953{
2954 if (opt_flags & OPT_LOCAL)
2955 switch ((int)options[opt_idx].indir)
2956 {
2957#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002958 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002959#endif
2960#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002961# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002962 case PV_FDE: return &curwin->w_p_fde_flags;
2963 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002964# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002965# ifdef FEAT_BEVAL
2966 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2967# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002968 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002969 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002970# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002971 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002972# endif
2973#endif
2974 }
2975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002977 return &options[opt_idx].flags;
2978}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002979#endif
2980
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002981/*
2982 * Redraw the window title and/or tab page text later.
2983 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002984void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002985{
2986 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002987 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002988}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002991 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2992 * characters or characters in "allowed".
2993 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002994 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002995valid_name(char_u *val, char *allowed)
2996{
2997 char_u *s;
2998
2999 for (s = val; *s != NUL; ++s)
3000 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
3001 return FALSE;
3002 return TRUE;
3003}
3004
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003005#if defined(FEAT_EVAL) || defined(PROTO)
3006/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003007 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003008 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003009 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003010 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003011set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003012{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003013 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
3014 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003015 sctx_T new_script_ctx = script_ctx;
3016
Bram Moolenaar51258742020-05-03 17:19:33 +02003017 // Modeline already has the line number set.
3018 if (!(opt_flags & OPT_MODELINE))
3019 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 // Remember where the option was set. For local options need to do that
3022 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003023 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003024 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003025 if (both || (opt_flags & OPT_LOCAL))
3026 {
3027 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003028 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003029 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00003030 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003031 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00003032 if (both)
3033 // also setting the "all buffers" value
3034 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
3035 new_script_ctx;
3036 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003037 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003038}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003039
3040/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00003041 * Get the script context of global option "name".
3042 *
3043 */
3044 sctx_T *
3045get_option_sctx(char *name)
3046{
3047 int idx = findoption((char_u *)name);
3048
3049 if (idx >= 0)
3050 return &options[idx].script_ctx;
3051 siemsg("no such option: %s", name);
3052 return NULL;
3053}
3054
3055/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02003056 * Set the script_ctx for a termcap option.
3057 * "name" must be the two character code, e.g. "RV".
3058 * When "name" is NULL use "opt_idx".
3059 */
3060 void
3061set_term_option_sctx_idx(char *name, int opt_idx)
3062{
3063 char_u buf[5];
3064 int idx;
3065
3066 if (name == NULL)
3067 idx = opt_idx;
3068 else
3069 {
3070 buf[0] = 't';
3071 buf[1] = '_';
3072 buf[2] = name[0];
3073 buf[3] = name[1];
3074 buf[4] = 0;
3075 idx = findoption(buf);
3076 }
3077 if (idx >= 0)
3078 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
3079}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003080#endif
3081
Bram Moolenaarf4140482020-02-15 23:06:45 +01003082#if defined(FEAT_EVAL)
3083/*
3084 * Apply the OptionSet autocommand.
3085 */
3086 static void
3087apply_optionset_autocmd(
3088 int opt_idx,
3089 long opt_flags,
3090 long oldval,
3091 long oldval_g,
3092 long newval,
3093 char *errmsg)
3094{
3095 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
3096
3097 // Don't do this while starting up, failure or recursively.
3098 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
3099 return;
3100
3101 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
3102 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
3103 oldval_g);
3104 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
3105 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
3106 (opt_flags & OPT_LOCAL) ? "local" : "global");
3107 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3108 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3109 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3110 if (opt_flags & OPT_LOCAL)
3111 {
3112 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3113 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3114 }
3115 if (opt_flags & OPT_GLOBAL)
3116 {
3117 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3118 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3119 }
3120 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3121 {
3122 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3123 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3124 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3125 }
3126 if (opt_flags & OPT_MODELINE)
3127 {
3128 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3129 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3130 }
3131 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3132 NULL, FALSE, NULL);
3133 reset_v_option_vars();
3134}
3135#endif
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137/*
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003138 * Process the updated 'compatible' option value.
3139 */
3140 static void
3141did_set_compatible(void)
3142{
3143 compatible_set();
3144}
3145
3146#ifdef FEAT_LANGMAP
3147/*
3148 * Process the updated 'langremap' option value.
3149 */
3150 static void
3151did_set_langremap(void)
3152{
3153 // 'langremap' -> !'langnoremap'
3154 p_lnr = !p_lrm;
3155}
3156
3157/*
3158 * Process the updated 'langnoremap' option value.
3159 */
3160 static void
3161did_set_langnoremap(void)
3162{
3163 // 'langnoremap' -> !'langremap'
3164 p_lrm = !p_lnr;
3165}
3166#endif
3167
3168#ifdef FEAT_PERSISTENT_UNDO
3169/*
3170 * Process the updated 'undofile' option value.
3171 */
3172 static void
3173did_set_undofile(int opt_flags)
3174{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003175 // Only take action when the option was set.
3176 if (!curbuf->b_p_udf && !p_udf)
3177 return;
3178
3179 // When reset we do not delete the undo file, the option may be set again
3180 // without making any changes in between.
3181 char_u hash[UNDO_HASH_SIZE];
3182 buf_T *save_curbuf = curbuf;
3183
3184 FOR_ALL_BUFFERS(curbuf)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003185 {
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003186 // When 'undofile' is set globally: for every buffer, otherwise
3187 // only for the current buffer: Try to read in the undofile,
3188 // if one exists, the buffer wasn't changed and the buffer was
3189 // loaded
3190 if ((curbuf == save_curbuf
3191 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
3192 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003193 {
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003194#ifdef FEAT_CRYPT
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003195 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
3196 continue;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003197#endif
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003198 u_compute_hash(hash);
3199 u_read_undo(NULL, hash, curbuf->b_fname);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003200 }
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003201 }
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003202 curbuf = save_curbuf;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003203}
3204#endif
3205
3206/*
3207 * Process the updated 'readonly' option value.
3208 */
3209 static void
3210did_set_readonly(int opt_flags)
3211{
3212 // when 'readonly' is reset globally, also reset readonlymode
3213 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
3214 readonlymode = FALSE;
3215
3216 // when 'readonly' is set may give W10 again
3217 if (curbuf->b_p_ro)
3218 curbuf->b_did_warn = FALSE;
3219
3220 redraw_titles();
3221}
3222
3223#ifdef FEAT_GUI
3224/*
3225 * Process the updated 'mousehide' option value.
3226 */
3227 static void
3228did_set_mousehide(void)
3229{
3230 if (!p_mh)
3231 gui_mch_mousehide(FALSE);
3232}
3233#endif
3234
3235/*
3236 * Process the updated 'modifiable' option value.
3237 */
3238 static char *
3239did_set_modifiable(int *doskip UNUSED)
3240{
3241 // when 'modifiable' is changed, redraw the window title
3242
3243# ifdef FEAT_TERMINAL
3244 // Cannot set 'modifiable' when in Terminal mode.
3245 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
3246 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
3247 {
3248 curbuf->b_p_ma = FALSE;
3249 *doskip = TRUE;
3250 return e_cannot_make_terminal_with_running_job_modifiable;
3251 }
3252# endif
3253 redraw_titles();
3254
3255 return NULL;
3256}
3257
3258/*
3259 * Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb'
3260 * option value.
3261 */
3262 static void
3263did_set_eof_eol_fixeol_bomb(void)
3264{
3265 // redraw the window title and tab page text
3266 redraw_titles();
3267}
3268
3269/*
3270 * Process the updated 'binary' option value.
3271 */
3272 static void
3273did_set_binary(int opt_flags, long old_value)
3274{
3275 // when 'bin' is set also set some other options
3276 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
3277 redraw_titles();
3278}
3279
3280/*
3281 * Process the updated 'buflisted' option value.
3282 */
3283 static void
3284did_set_buflisted(long old_value)
3285{
3286 // when 'buflisted' changes, trigger autocommands
3287 if (old_value != curbuf->b_p_bl)
3288 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
3289 NULL, NULL, TRUE, curbuf);
3290}
3291
3292/*
3293 * Process the updated 'swapfile' option value.
3294 */
3295 static void
3296did_set_swapfile(void)
3297{
3298 // when 'swf' is set, create swapfile, when reset remove swapfile
3299 if (curbuf->b_p_swf && p_uc)
3300 ml_open_file(curbuf); // create the swap file
3301 else
3302 // no need to reset curbuf->b_may_swap, ml_open_file() will check
3303 // buf->b_p_swf
3304 mf_close_file(curbuf, TRUE); // remove the swap file
3305}
3306
3307/*
3308 * Process the updated 'terse' option value.
3309 */
3310 static void
3311did_set_terse(void)
3312{
3313 char_u *p;
3314
3315 // when 'terse' is set change 'shortmess'
3316 p = vim_strchr(p_shm, SHM_SEARCH);
3317
3318 // insert 's' in p_shm
3319 if (p_terse && p == NULL)
3320 {
3321 STRCPY(IObuff, p_shm);
3322 STRCAT(IObuff, "s");
3323 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
3324 }
3325 // remove 's' from p_shm
3326 else if (!p_terse && p != NULL)
3327 STRMOVE(p, p + 1);
3328}
3329
3330/*
3331 * Process the updated 'paste' option value.
3332 */
3333 static void
3334did_set_paste(void)
3335{
3336 // when 'paste' is set or reset also change other options
3337 paste_option_changed();
3338}
3339
3340/*
3341 * Process the updated 'insertmode' option value.
3342 */
3343 static void
3344did_set_insertmode(long old_value)
3345{
3346 // when 'insertmode' is set from an autocommand need to do work here
3347 if (p_im)
3348 {
3349 if ((State & MODE_INSERT) == 0)
3350 need_start_insertmode = TRUE;
3351 stop_insert_mode = FALSE;
3352 }
3353 // only reset if it was set previously
3354 else if (old_value)
3355 {
3356 need_start_insertmode = FALSE;
3357 stop_insert_mode = TRUE;
3358 if (restart_edit != 0 && mode_displayed)
3359 clear_cmdline = TRUE; // remove "(insert)"
3360 restart_edit = 0;
3361 }
3362}
3363
3364/*
3365 * Process the updated 'ignorecase' option value.
3366 */
3367 static void
3368did_set_ignorecase(void)
3369{
3370 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
3371 if (p_hls)
3372 redraw_all_later(UPD_SOME_VALID);
3373}
3374
3375#ifdef FEAT_SEARCH_EXTRA
3376/*
3377 * Process the updated 'hlsearch' option value.
3378 */
3379 static void
3380did_set_hlsearch(void)
3381{
3382 // when 'hlsearch' is set or reset: reset no_hlsearch
3383 set_no_hlsearch(FALSE);
3384}
3385#endif
3386
3387/*
3388 * Process the updated 'scrollbind' option value.
3389 */
3390 static void
3391did_set_scrollbind(void)
3392{
3393 // when 'scrollbind' is set: snapshot the current position to avoid a jump
3394 // at the end of normal_cmd()
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003395 if (!curwin->w_p_scb)
3396 return;
3397 do_check_scrollbind(FALSE);
3398 curwin->w_scbind_pos = curwin->w_topline;
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003399}
3400
3401#ifdef FEAT_QUICKFIX
3402/*
3403 * Process the updated 'previewwindow' option value.
3404 */
3405 static char *
3406did_set_previewwindow(int *doskip)
3407{
3408 if (!curwin->w_p_pvw)
3409 return NULL;
3410
3411 // There can be only one window with 'previewwindow' set.
3412 win_T *win;
3413
3414 FOR_ALL_WINDOWS(win)
3415 if (win->w_p_pvw && win != curwin)
3416 {
3417 curwin->w_p_pvw = FALSE;
3418 *doskip = TRUE;
3419 return e_preview_window_already_exists;
3420 }
3421
3422 return NULL;
3423}
3424#endif
3425
3426/*
3427 * Process the updated 'smoothscroll' option value.
3428 */
3429 static void
3430did_set_smoothscroll(void)
3431{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003432 if (curwin->w_p_sms)
3433 return;
3434 curwin->w_skipcol = 0;
3435 changed_line_abv_curs();
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003436}
3437
3438/*
3439 * Process the updated 'textmode' option value.
3440 */
3441 static void
3442did_set_textmode(int opt_flags)
3443{
3444 // when 'textmode' is set or reset also change 'fileformat'
3445 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
3446}
3447
3448/*
3449 * Process the updated 'textauto' option value.
3450 */
3451 static void
3452did_set_textauto(int opt_flags)
3453{
3454 // when 'textauto' is set or reset also change 'fileformats'
3455 set_string_option_direct((char_u *)"ffs", -1,
3456 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
3457 OPT_FREE | opt_flags, 0);
3458}
3459
3460/*
3461 * Process the updated 'lisp' option value.
3462 */
3463 static void
3464did_set_lisp(void)
3465{
3466 // When 'lisp' option changes include/exclude '-' in keyword characters.
3467 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
3468}
3469
3470/*
3471 * Process the updated 'title' or the 'icon' option value.
3472 */
3473 static void
3474did_set_title_icon(void)
3475{
3476 // when 'title' changed, may need to change the title; same for 'icon'
3477 did_set_title();
3478}
3479
3480/*
3481 * Process the updated 'modified' option value.
3482 */
3483 static void
3484did_set_modified(long value)
3485{
3486 if (!value)
3487 save_file_ff(curbuf); // Buffer is unchanged
3488 redraw_titles();
3489 modified_was_set = value;
3490}
3491
3492#ifdef BACKSLASH_IN_FILENAME
3493/*
3494 * Process the updated 'shellslash' option value.
3495 */
3496 static void
3497did_set_shellslash(void)
3498{
3499 if (p_ssl)
3500 {
3501 psepc = '/';
3502 psepcN = '\\';
3503 pseps[0] = '/';
3504 }
3505 else
3506 {
3507 psepc = '\\';
3508 psepcN = '/';
3509 pseps[0] = '\\';
3510 }
3511
3512 // need to adjust the file name arguments and buffer names.
3513 buflist_slash_adjust();
3514 alist_slash_adjust();
3515# ifdef FEAT_EVAL
3516 scriptnames_slash_adjust();
3517# endif
3518}
3519#endif
3520
3521/*
3522 * Process the updated 'wrap' option value.
3523 */
3524 static void
3525did_set_wrap(void)
3526{
3527 // If 'wrap' is set, set w_leftcol to zero.
3528 if (curwin->w_p_wrap)
3529 curwin->w_leftcol = 0;
3530}
3531
3532/*
3533 * Process the updated 'equalalways' option value.
3534 */
3535 static void
3536did_set_equalalways(long old_value)
3537{
3538 if (p_ea && !old_value)
3539 win_equal(curwin, FALSE, 0);
3540}
3541
3542/*
3543 * Process the updated 'weirdinvert' option value.
3544 */
3545 static void
3546did_set_weirdinvert(long old_value)
3547{
3548 // When 'weirdinvert' changed, set/reset 't_xs'.
3549 // Then set 'weirdinvert' according to value of 't_xs'.
3550 if (p_wiv && !old_value)
3551 T_XS = (char_u *)"y";
3552 else if (!p_wiv && old_value)
3553 T_XS = empty_option;
3554 p_wiv = (*T_XS != NUL);
3555}
3556
3557#ifdef FEAT_BEVAL_GUI
3558/*
3559 * Process the updated 'ballooneval' option value.
3560 */
3561 static void
3562did_set_ballooneval(long old_value)
3563{
Yegappan Lakshmanan1a647642023-02-14 13:07:18 +00003564 if (balloonEvalForTerm)
3565 return;
3566 if (p_beval && !old_value)
3567 gui_mch_enable_beval_area(balloonEval);
3568 else if (!p_beval && old_value)
3569 gui_mch_disable_beval_area(balloonEval);
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003570}
3571#endif
3572
3573#ifdef FEAT_BEVAL_TERM
3574/*
3575 * Process the updated 'balloonevalterm' option value.
3576 */
3577 static void
3578did_set_balloonevalterm(void)
3579{
3580 mch_bevalterm_changed();
3581}
3582#endif
3583
3584#ifdef FEAT_AUTOCHDIR
3585/*
3586 * Process the updated 'autochdir' option value.
3587 */
3588 static void
3589did_set_autochdir(void)
3590{
3591 // Change directories when the 'acd' option is set now.
3592 DO_AUTOCHDIR;
3593}
3594#endif
3595
3596#ifdef FEAT_DIFF
3597/*
3598 * Process the updated 'diff' option value.
3599 */
3600 static void
3601did_set_diff(void)
3602{
3603 // May add or remove the buffer from the list of diff buffers.
3604 diff_buf_adjust(curwin);
3605# ifdef FEAT_FOLDING
3606 if (foldmethodIsDiff(curwin))
3607 foldUpdateAll(curwin);
3608# endif
3609}
3610#endif
3611
3612#ifdef HAVE_INPUT_METHOD
3613/*
3614 * Process the updated 'imdisable' option value.
3615 */
3616 static void
3617did_set_imdisable(void)
3618{
3619 // Only de-activate it here, it will be enabled when changing mode.
3620 if (p_imdisable)
3621 im_set_active(FALSE);
3622 else if (State & MODE_INSERT)
3623 // When the option is set from an autocommand, it may need to take
3624 // effect right away.
3625 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
3626}
3627#endif
3628
3629#ifdef FEAT_SPELL
3630/*
3631 * Process the updated 'spell' option value.
3632 */
3633 static char *
3634did_set_spell(void)
3635{
3636 if (curwin->w_p_spell)
3637 return did_set_spelllang(curwin);
3638
3639 return NULL;
3640}
3641#endif
3642
3643#ifdef FEAT_ARABIC
3644/*
3645 * Process the updated 'arabic' option value.
3646 */
3647 static char *
3648did_set_arabic(void)
3649{
3650 char *errmsg = NULL;
3651
3652 if (curwin->w_p_arab)
3653 {
3654 /*
3655 * 'arabic' is set, handle various sub-settings.
3656 */
3657 if (!p_tbidi)
3658 {
3659 // set rightleft mode
3660 if (!curwin->w_p_rl)
3661 {
3662 curwin->w_p_rl = TRUE;
3663 changed_window_setting();
3664 }
3665
3666 // Enable Arabic shaping (major part of what Arabic requires)
3667 if (!p_arshape)
3668 {
3669 p_arshape = TRUE;
3670 redraw_later_clear();
3671 }
3672 }
3673
3674 // Arabic requires a utf-8 encoding, inform the user if it's not
3675 // set.
3676 if (STRCMP(p_enc, "utf-8") != 0)
3677 {
3678 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3679
3680 msg_source(HL_ATTR(HLF_W));
3681 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
3682#ifdef FEAT_EVAL
3683 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3684#endif
3685 }
3686
3687 // set 'delcombine'
3688 p_deco = TRUE;
3689
3690# ifdef FEAT_KEYMAP
3691 // Force-set the necessary keymap for arabic
3692 errmsg = set_option_value((char_u *)"keymap",
3693 0L, (char_u *)"arabic", OPT_LOCAL);
3694# endif
3695 }
3696 else
3697 {
3698 /*
3699 * 'arabic' is reset, handle various sub-settings.
3700 */
3701 if (!p_tbidi)
3702 {
3703 // reset rightleft mode
3704 if (curwin->w_p_rl)
3705 {
3706 curwin->w_p_rl = FALSE;
3707 changed_window_setting();
3708 }
3709
3710 // 'arabicshape' isn't reset, it is a global option and
3711 // another window may still need it "on".
3712 }
3713
3714 // 'delcombine' isn't reset, it is a global option and another
3715 // window may still want it "on".
3716
3717# ifdef FEAT_KEYMAP
3718 // Revert to the default keymap
3719 curbuf->b_p_iminsert = B_IMODE_NONE;
3720 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3721# endif
3722 }
3723
3724 return errmsg;
3725}
3726#endif
3727
3728#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3729/*
3730 * Process the updated 'number' or 'relativenumber' option value.
3731 */
3732 static void
3733did_set_number_relativenumber(char_u *varp)
3734{
3735 if (gui.in_use
3736 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3737 && curbuf->b_signlist != NULL)
3738 {
3739 // If the 'number' or 'relativenumber' options are modified and
3740 // 'signcolumn' is set to 'number', then clear the screen for a full
3741 // refresh. Otherwise the sign icons are not displayed properly in the
3742 // number column. If the 'number' option is set and only the
3743 // 'relativenumber' option is toggled, then don't refresh the screen
3744 // (optimization).
3745 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3746 redraw_all_later(UPD_CLEAR);
3747 }
3748}
3749#endif
3750
3751#ifdef FEAT_TERMGUICOLORS
3752 static char *
3753did_set_termguicolors(int *doskip UNUSED)
3754{
3755# ifdef FEAT_VTP
3756 // Do not turn on 'tgc' when 24-bit colors are not supported.
3757 if (
3758# ifdef VIMDLL
3759 !gui.in_use && !gui.starting &&
3760# endif
3761 !has_vtp_working())
3762 {
3763 p_tgc = 0;
3764 *doskip = TRUE;
3765 return e_24_bit_colors_are_not_supported_on_this_environment;
3766 }
3767 if (is_term_win32())
3768 swap_tcap();
3769# endif
3770# ifdef FEAT_GUI
3771 if (!gui.in_use && !gui.starting)
3772# endif
3773 highlight_gui_started();
3774# ifdef FEAT_VTP
3775 // reset t_Co
3776 if (is_term_win32())
3777 {
3778 control_console_color_rgb();
3779 set_termname(T_NAME);
3780 init_highlight(TRUE, FALSE);
3781 }
3782# endif
3783# ifdef FEAT_TERMINAL
3784 term_update_colors_all();
3785 term_update_palette_all();
3786 term_update_wincolor_all();
3787# endif
3788
3789 return NULL;
3790}
3791#endif
3792
3793/*
3794 * When some boolean options are changed, need to take some action.
3795 */
3796 static char *
3797did_set_bool_option(
3798 char_u *varp,
3799 int opt_flags,
3800 long value,
3801 long old_value,
3802 int *doskip)
3803{
3804 char *errmsg = NULL;
3805
3806 if ((int *)varp == &p_cp) // 'compatible'
3807 did_set_compatible();
3808#ifdef FEAT_LANGMAP
3809 else if ((int *)varp == &p_lrm) // 'langremap'
3810 did_set_langremap();
3811 else if ((int *)varp == &p_lnr) // 'langnoremap'
3812 did_set_langnoremap();
3813#endif
3814#ifdef FEAT_PERSISTENT_UNDO
3815 else if ((int *)varp == &curbuf->b_p_udf // buffer local 'undofile'
3816 || (int *)varp == &p_udf) // 'undofile'
3817 did_set_undofile(opt_flags);
3818#endif
3819 else if ((int *)varp == &curbuf->b_p_ro) // 'readonly'
3820 did_set_readonly(opt_flags);
3821#ifdef FEAT_GUI
3822 else if ((int *)varp == &p_mh) // 'mousehide'
3823 did_set_mousehide();
3824#endif
3825 else if ((int *)varp == &curbuf->b_p_ma)
3826 errmsg = did_set_modifiable(doskip); // 'modifiable'
3827 else if ((int *)varp == &curbuf->b_p_eof // 'endoffile'
3828 || (int *)varp == &curbuf->b_p_eol // 'endofline'
3829 || (int *)varp == &curbuf->b_p_fixeol // 'fixendofline'
3830 || (int *)varp == &curbuf->b_p_bomb) // 'bomb'
3831 did_set_eof_eol_fixeol_bomb();
3832 else if ((int *)varp == &curbuf->b_p_bin) // 'binary'
3833 did_set_binary(opt_flags, old_value);
3834 else if ((int *)varp == &curbuf->b_p_bl) // 'buflisted'
3835 did_set_buflisted(old_value);
3836 else if ((int *)varp == &curbuf->b_p_swf) // 'swapfile'
3837 did_set_swapfile();
3838 else if ((int *)varp == &p_terse) // 'terse'
3839 did_set_terse();
3840 else if ((int *)varp == &p_paste) // 'paste'
3841 did_set_paste();
3842 else if ((int *)varp == &p_im) // 'insertmode'
3843 did_set_insertmode(old_value);
3844 else if ((int *)varp == &p_ic) // 'ignorecase'
3845 did_set_ignorecase();
3846#ifdef FEAT_SEARCH_EXTRA
3847 else if ((int *)varp == &p_hls) // 'hlsearch'
3848 did_set_hlsearch();
3849#endif
3850 else if ((int *)varp == &curwin->w_p_scb) // 'scrollbind'
3851 did_set_scrollbind();
3852#ifdef FEAT_QUICKFIX
3853 else if ((int *)varp == &curwin->w_p_pvw) // 'previewwindow'
3854 errmsg = did_set_previewwindow(doskip);
3855#endif
3856 else if ((int *)varp == &curwin->w_p_sms) // 'smoothscroll'
3857 did_set_smoothscroll();
3858 else if ((int *)varp == &curbuf->b_p_tx) // 'textmode'
3859 did_set_textmode(opt_flags);
3860 else if ((int *)varp == &p_ta) // 'textauto'
3861 did_set_textauto(opt_flags);
3862 else if (varp == (char_u *)&(curbuf->b_p_lisp)) // 'lisp'
3863 did_set_lisp();
3864 else if ( (int *)varp == &p_title // 'title'
3865 || (int *)varp == &p_icon) // 'icon'
3866 did_set_title_icon();
3867 else if ((int *)varp == &curbuf->b_changed) // 'modified'
3868 did_set_modified(value);
3869#ifdef BACKSLASH_IN_FILENAME
3870 else if ((int *)varp == &p_ssl) // 'shellslash'
3871 did_set_shellslash();
3872#endif
3873 else if ((int *)varp == &curwin->w_p_wrap) // 'wrap'
3874 did_set_wrap();
3875 else if ((int *)varp == &p_ea) // 'equalalways'
3876 did_set_equalalways(old_value);
3877 else if ((int *)varp == &p_wiv) // weirdinvert'
3878 did_set_weirdinvert(old_value);
3879#ifdef FEAT_BEVAL_GUI
3880 else if ((int *)varp == &p_beval) // 'ballooneval'
3881 did_set_ballooneval(old_value);
3882#endif
3883#ifdef FEAT_BEVAL_TERM
3884 else if ((int *)varp == &p_bevalterm) // 'balloonevalterm'
3885 did_set_balloonevalterm();
3886#endif
3887#ifdef FEAT_AUTOCHDIR
3888 else if ((int *)varp == &p_acd) // 'autochdir'
3889 did_set_autochdir();
3890#endif
3891#ifdef FEAT_DIFF
3892 else if ((int *)varp == &curwin->w_p_diff) // 'diff'
3893 did_set_diff();
3894#endif
3895#ifdef HAVE_INPUT_METHOD
3896 else if ((int *)varp == &p_imdisable) // 'imdisable'
3897 did_set_imdisable();
3898#endif
3899#ifdef FEAT_SPELL
3900 else if ((int *)varp == &curwin->w_p_spell) // 'spell'
3901 errmsg = did_set_spell();
3902#endif
3903#ifdef FEAT_ARABIC
3904 else if ((int *)varp == &curwin->w_p_arab) // 'arabic'
3905 errmsg = did_set_arabic();
3906#endif
3907#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3908 else if ( (int *)varp == &curwin->w_p_nu // 'number'
3909 || (int *)varp == &curwin->w_p_rnu) // 'relativenumber'
3910 did_set_number_relativenumber(varp);
3911#endif
3912#ifdef FEAT_TERMGUICOLORS
3913 else if ((int *)varp == &p_tgc) // 'termguicolors'
3914 errmsg = did_set_termguicolors(doskip);
3915#endif
3916
3917 return errmsg;
3918}
3919
3920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * Set the value of a boolean option, and take care of side effects.
3922 * Returns NULL for success, or an error message for an error.
3923 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003924 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003925set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003926 int opt_idx, // index in options[] table
3927 char_u *varp, // pointer to the option variable
3928 int value, // new value
3929 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
3931 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003932#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003933 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003934#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003935 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if ((secure
3939#ifdef HAVE_SANDBOX
3940 || sandbox != 0
3941#endif
3942 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003943 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003945#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003946 // Save the global value before changing anything. This is needed as for
3947 // a global-only option setting the "local value" in fact sets the global
3948 // value (since there is only one value).
3949 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3950 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
3951 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003952#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003954 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003956 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003957 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#endif
3959
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003960#ifdef FEAT_GUI
3961 need_mouse_correct = TRUE;
3962#endif
3963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003964 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3966 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
3967
3968 /*
3969 * Handle side effects of changing a bool option.
3970 */
Yegappan Lakshmanan80b817b2023-02-09 22:08:52 +00003971 int doskip = FALSE;
3972 errmsg = did_set_bool_option(varp, opt_flags, value, old_value, &doskip);
3973 if (doskip)
3974 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003976 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 options[opt_idx].flags |= P_WAS_SET;
3979
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003980#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003981 apply_optionset_autocmd(opt_idx, opt_flags,
3982 (long)(old_value ? TRUE : FALSE),
3983 (long)(old_global_value ? TRUE : FALSE),
3984 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003985#endif
3986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003987 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003988 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003989 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003990 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003991
3992 if ((opt_flags & OPT_NO_REDRAW) == 0)
3993 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003995 return errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998/*
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00003999 * Process the new 'winheight' or the 'helpheight' option value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004001 static char *
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004002did_set_winheight_helpheight(long *pp, char *errmsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004004 if (p_wh < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004006 errmsg = e_argument_must_be_positive;
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004007 p_wh = 1;
4008 }
4009 if (p_wmh > p_wh)
4010 {
4011 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4012 p_wh = p_wmh;
4013 }
4014 if (p_hh < 0)
4015 {
4016 errmsg = e_argument_must_be_positive;
4017 p_hh = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004020 // Change window height NOW
4021 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004023 if (pp == &p_wh && curwin->w_height < p_wh)
4024 win_setheight((int)p_wh);
4025 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
4026 win_setheight((int)p_hh);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004029 return errmsg;
4030}
4031
4032/*
4033 * Process the new 'winminheight' option value.
4034 */
4035 static char *
4036did_set_winminheight(char *errmsg)
4037{
4038 if (p_wmh < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004040 errmsg = e_argument_must_be_positive;
4041 p_wmh = 0;
4042 }
4043 if (p_wmh > p_wh)
4044 {
4045 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
4046 p_wmh = p_wh;
4047 }
4048 win_setminheight();
4049
4050 return errmsg;
4051}
4052
4053/*
4054 * Process the new 'winwidth' option value.
4055 */
4056 static char *
4057did_set_winwidth(char *errmsg)
4058{
4059 if (p_wiw < 1)
4060 {
4061 errmsg = e_argument_must_be_positive;
4062 p_wiw = 1;
4063 }
4064 if (p_wmw > p_wiw)
4065 {
4066 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4067 p_wiw = p_wmw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004069
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004070 // Change window width NOW
4071 if (!ONE_WINDOW && curwin->w_width < p_wiw)
4072 win_setwidth((int)p_wiw);
4073
4074 return errmsg;
4075}
4076
4077/*
4078 * Process the new 'winminwidth' option value.
4079 */
4080 static char *
4081did_set_winminwidth(char *errmsg)
4082{
4083 if (p_wmw < 0)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004084 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004085 errmsg = e_argument_must_be_positive;
4086 p_wmw = 0;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00004087 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004088 if (p_wmw > p_wiw)
4089 {
4090 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
4091 p_wmw = p_wiw;
4092 }
4093 win_setminwidth();
4094
4095 return errmsg;
4096}
4097
4098/*
4099 * Process the new 'laststatus' option value.
4100 */
4101 static void
4102did_set_laststatus(void)
4103{
4104 last_status(FALSE); // (re)set last window status line
4105}
4106
4107/*
4108 * Process the new 'showtabline' option value.
4109 */
4110 static void
4111did_set_showtabline(void)
4112{
4113 shell_new_rows(); // recompute window positions and heights
4114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004117/*
4118 * Process the new 'linespace' option value.
4119 */
4120 static void
4121did_set_linespace(void)
4122{
4123 // Recompute gui.char_height and resize the Vim window to keep the
4124 // same number of lines.
4125 if (gui.in_use && gui_mch_adjust_charheight() == OK)
4126 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
4127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#endif
4129
4130#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004131/*
4132 * Process the new 'foldlevel' option value.
4133 */
4134 static void
4135did_set_foldlevel(void)
4136{
4137 if (curwin->w_p_fdl < 0)
4138 curwin->w_p_fdl = 0;
4139 newFoldLevel();
4140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004142/*
4143 * Process the new 'foldminlines' option value.
4144 */
4145 static void
4146did_set_foldminlines(void)
4147{
4148 foldUpdateAll(curwin);
4149}
4150
4151/*
4152 * Process the new 'foldnestmax' option value.
4153 */
4154 static void
4155did_set_foldnestmax(void)
4156{
4157 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 foldUpdateAll(curwin);
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004159}
4160
4161/*
4162 * Process the new 'foldcolumn' option value.
4163 */
4164 static char *
4165did_set_foldcolumn(char *errmsg)
4166{
4167 if (curwin->w_p_fdc < 0)
4168 {
4169 errmsg = e_argument_must_be_positive;
4170 curwin->w_p_fdc = 0;
4171 }
4172 else if (curwin->w_p_fdc > 12)
4173 {
4174 errmsg = e_invalid_argument;
4175 curwin->w_p_fdc = 12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004178 return errmsg;
4179}
4180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004182/*
4183 * Process the new 'shiftwidth' or the 'tabstop' option value.
4184 */
4185 static void
4186did_set_shiftwidth_tabstop(long *pp)
4187{
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004188#ifdef FEAT_FOLDING
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004189 if (foldmethodIsIndent(curwin))
4190 foldUpdateAll(curwin);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01004191#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004192 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
4193 // parse 'cinoptions'.
4194 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
4195 parse_cino(curbuf);
4196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004198/*
4199 * Process the new 'maxcombine' option value.
4200 */
4201 static void
4202did_set_maxcombine(void)
4203{
4204 if (p_mco > MAX_MCO)
4205 p_mco = MAX_MCO;
4206 else if (p_mco < 0)
4207 p_mco = 0;
4208 screenclear(); // will re-allocate the screen
4209}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004210
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004211/*
4212 * Process the new 'iminsert' option value.
4213 */
4214 static char *
4215did_set_iminsert(char *errmsg)
4216{
4217 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004219 errmsg = e_invalid_argument;
4220 curbuf->b_p_iminsert = B_IMODE_NONE;
4221 }
4222 p_iminsert = curbuf->b_p_iminsert;
4223 if (termcap_active) // don't do this in the alternate screen
4224 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004225#if defined(FEAT_KEYMAP)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004226 // Show/unshow value of 'keymap' in status lines.
4227 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004229
4230 return errmsg;
4231}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004233#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004234/*
4235 * Process the new 'imstyle' option value.
4236 */
4237 static char *
4238did_set_imstyle(char *errmsg)
4239{
4240 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
4241 errmsg = e_invalid_argument;
4242
4243 return errmsg;
4244}
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02004245#endif
4246
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004247/*
4248 * Process the new 'window' option value.
4249 */
4250 static void
4251did_set_window(void)
4252{
4253 if (p_window < 1)
4254 p_window = 1;
4255 else if (p_window >= Rows)
4256 p_window = Rows - 1;
4257}
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004258
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004259/*
4260 * Process the new 'imsearch' option value.
4261 */
4262 static char *
4263did_set_imsearch(char *errmsg)
4264{
4265 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004267 errmsg = e_invalid_argument;
4268 curbuf->b_p_imsearch = B_IMODE_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004270 p_imsearch = curbuf->b_p_imsearch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004272 return errmsg;
4273}
4274
4275/*
4276 * Process the new 'titlelen' option value.
4277 */
4278 static char *
4279did_set_titlelen(long old_value, char *errmsg)
4280{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004281 // if 'titlelen' has changed, redraw the title
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004282 if (p_titlelen < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004284 errmsg = e_argument_must_be_positive;
4285 p_titlelen = 85;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004287 if (starting != NO_SCREEN && old_value != p_titlelen)
4288 need_maketitle = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004290 return errmsg;
4291}
4292
4293/*
4294 * Process the new 'cmdheight' option value.
4295 */
4296 static char *
4297did_set_cmdheight(long old_value, char *errmsg)
4298{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004299 // if p_ch changed value, change the command line height
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004300 if (p_ch < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004302 errmsg = e_argument_must_be_positive;
4303 p_ch = 1;
4304 }
4305 if (p_ch > Rows - min_rows() + 1)
4306 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004308 // Only compute the new window layout when startup has been
4309 // completed. Otherwise the frame sizes may be wrong.
4310 if ((p_ch != old_value
4311 || tabline_height() + topframe->fr_height != Rows - p_ch)
4312 && full_screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313#ifdef FEAT_GUI
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004314 && !gui.starting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004316 )
4317 command_height();
4318
4319 return errmsg;
4320}
4321
4322/*
4323 * Process the new 'updatecount' option value.
4324 */
4325 static char *
4326did_set_updatecount(long old_value, char *errmsg)
4327{
4328 // when 'updatecount' changes from zero to non-zero, open swap files
4329 if (p_uc < 0)
4330 {
4331 errmsg = e_argument_must_be_positive;
4332 p_uc = 100;
4333 }
4334 if (p_uc && !old_value)
4335 ml_open_files();
4336
4337 return errmsg;
4338}
4339
4340#ifdef FEAT_CONCEAL
4341/*
4342 * Process the new 'conceallevel' option value.
4343 */
4344 static char *
4345did_set_conceallevel(char *errmsg)
4346{
4347 if (curwin->w_p_cole < 0)
4348 {
4349 errmsg = e_argument_must_be_positive;
4350 curwin->w_p_cole = 0;
4351 }
4352 else if (curwin->w_p_cole > 3)
4353 {
4354 errmsg = e_invalid_argument;
4355 curwin->w_p_cole = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004358 return errmsg;
4359}
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004362#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004363/*
4364 * Process the new 'pyxversion' option value.
4365 */
4366 static char *
4367did_set_pyxversion(char *errmsg)
4368{
4369 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
4370 errmsg = e_invalid_argument;
4371
4372 return errmsg;
4373}
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004374#endif
4375
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004376/*
4377 * Process the new global 'undolevels' option value.
4378 */
4379 static void
4380did_set_global_undolevels(long value, long old_value)
4381{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004382 // sync undo before 'undolevels' changes
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004383
4384 // use the old value, otherwise u_sync() may not work properly
4385 p_ul = old_value;
4386 u_sync(TRUE);
4387 p_ul = value;
4388}
4389
4390/*
4391 * Process the new buffer local 'undolevels' option value.
4392 */
4393 static void
4394did_set_buflocal_undolevels(long value, long old_value)
4395{
4396 // use the old value, otherwise u_sync() may not work properly
4397 curbuf->b_p_ul = old_value;
4398 u_sync(TRUE);
4399 curbuf->b_p_ul = value;
4400}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004402#ifdef FEAT_LINEBREAK
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004403/*
4404 * Process the new 'numberwidth' option value.
4405 */
4406 static char *
4407did_set_numberwidth(char *errmsg)
4408{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004409 // 'numberwidth' must be positive
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004410 if (curwin->w_p_nuw < 1)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004411 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004412 errmsg = e_argument_must_be_positive;
4413 curwin->w_p_nuw = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004414 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004415 if (curwin->w_p_nuw > 20)
4416 {
4417 errmsg = e_invalid_argument;
4418 curwin->w_p_nuw = 20;
4419 }
4420 curwin->w_nrwidth_line_count = 0; // trigger a redraw
4421
4422 return errmsg;
4423}
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004424#endif
4425
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004426/*
4427 * Process the new 'textwidth' option value.
4428 */
4429 static char *
4430did_set_textwidth(char *errmsg)
4431{
4432 if (curbuf->b_p_tw < 0)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004433 {
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004434 errmsg = e_argument_must_be_positive;
4435 curbuf->b_p_tw = 0;
4436 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02004437#ifdef FEAT_SYN_HL
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004438 {
4439 win_T *wp;
4440 tabpage_T *tp;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004441
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004442 FOR_ALL_TAB_WINDOWS(tp, wp)
4443 check_colorcolumn(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004444 }
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004445#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004446
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004447 return errmsg;
4448}
4449
4450/*
4451 * When some number options are changed, need to take some action.
4452 */
4453 static char *
4454did_set_num_option(long *pp, long value, long old_value, char *errmsg)
4455{
4456 if (pp == &p_wh // 'winheight'
4457 || pp == &p_hh) // 'helpheight'
4458 errmsg = did_set_winheight_helpheight(pp, errmsg);
4459 else if (pp == &p_wmh) // 'winminheight'
4460 errmsg = did_set_winminheight(errmsg);
4461 else if (pp == &p_wiw) // 'winwidth'
4462 errmsg = did_set_winwidth(errmsg);
4463 else if (pp == &p_wmw) // 'winminwidth'
4464 errmsg = did_set_winminwidth(errmsg);
4465 else if (pp == &p_ls)
4466 did_set_laststatus(); // 'laststatus'
4467 else if (pp == &p_stal)
4468 did_set_showtabline(); // 'showtabline'
4469#ifdef FEAT_GUI
4470 else if (pp == &p_linespace) // 'linespace'
4471 did_set_linespace();
4472#endif
4473#ifdef FEAT_FOLDING
4474 else if (pp == &curwin->w_p_fdl) // 'foldlevel'
4475 did_set_foldlevel();
4476 else if (pp == &curwin->w_p_fml) // 'foldminlines'
4477 did_set_foldminlines();
4478 else if (pp == &curwin->w_p_fdn) // 'foldnestmax'
4479 did_set_foldnestmax();
4480 else if (pp == &curwin->w_p_fdc) // 'foldcolumn'
4481 errmsg = did_set_foldcolumn(errmsg);
4482#endif // FEAT_FOLDING
4483 else if ( pp == &curbuf->b_p_sw // 'shiftwidth'
4484 || pp == &curbuf->b_p_ts) // 'tabstop'
4485 did_set_shiftwidth_tabstop(pp);
4486 else if (pp == &p_mco) // 'maxcombine'
4487 did_set_maxcombine();
4488 else if (pp == &curbuf->b_p_iminsert) // 'iminsert'
4489 errmsg = did_set_iminsert(errmsg);
4490#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
4491 else if (pp == &p_imst) // 'imstyle'
4492 errmsg = did_set_imstyle(errmsg);
4493#endif
4494 else if (pp == &p_window) // 'window'
4495 did_set_window();
4496 else if (pp == &curbuf->b_p_imsearch) // 'imsearch'
4497 errmsg = did_set_imsearch(errmsg);
4498 else if (pp == &p_titlelen) // 'titlelen'
4499 errmsg = did_set_titlelen(old_value, errmsg);
4500 else if (pp == &p_ch) // 'cmdheight'
4501 errmsg = did_set_cmdheight(old_value, errmsg);
4502 else if (pp == &p_uc) // 'updatecount'
4503 errmsg = did_set_updatecount(old_value, errmsg);
4504#ifdef FEAT_CONCEAL
4505 else if (pp == &curwin->w_p_cole) // 'conceallevel'
4506 errmsg = did_set_conceallevel(errmsg);
4507#endif
4508#ifdef MZSCHEME_GUI_THREADS
4509 else if (pp == &p_mzq) // 'mzquantum'
4510 mzvim_reset_timer();
4511#endif
4512#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
4513 else if (pp == &p_pyx) // 'pyxversion'
4514 errmsg = did_set_pyxversion(errmsg);
4515#endif
4516 else if (pp == &p_ul) // global 'undolevels'
4517 did_set_global_undolevels(value, old_value);
4518 else if (pp == &curbuf->b_p_ul) // buffer local 'undolevels'
4519 did_set_buflocal_undolevels(value, old_value);
4520#ifdef FEAT_LINEBREAK
4521 else if (pp == &curwin->w_p_nuw) // 'numberwidth'
4522 errmsg = did_set_numberwidth(errmsg);
4523#endif
4524 else if (pp == &curbuf->b_p_tw) // 'textwidth'
4525 errmsg = did_set_textwidth(errmsg);
4526
4527 return errmsg;
4528}
4529
4530/*
4531 * Check the bounds of numeric options.
4532 */
4533 static char *
4534check_num_option_bounds(
4535 long *pp,
4536 long old_value,
4537 long old_Rows,
4538 long old_Columns,
4539 char *errbuf,
4540 size_t errbuflen,
4541 char *errmsg)
4542{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (Rows < min_rows() && full_screen)
4544 {
4545 if (errbuf != NULL)
4546 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004547 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004548 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 errmsg = errbuf;
4550 }
4551 Rows = min_rows();
4552 }
4553 if (Columns < MIN_COLUMNS && full_screen)
4554 {
4555 if (errbuf != NULL)
4556 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004557 vim_snprintf(errbuf, errbuflen,
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004558 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 errmsg = errbuf;
4560 }
4561 Columns = MIN_COLUMNS;
4562 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004563 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 /*
4566 * If the screen (shell) height has been changed, assume it is the
4567 * physical screenheight.
4568 */
4569 if (old_Rows != Rows || old_Columns != Columns)
4570 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004571 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (updating_screen)
4573 *pp = old_value;
4574 else if (full_screen
4575#ifdef FEAT_GUI
4576 && !gui.starting
4577#endif
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004578 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 set_shellsize((int)Columns, (int)Rows, TRUE);
4580 else
4581 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004582 // Postpone the resizing; check the size and cmdline position for
4583 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 check_shellsize();
4585 if (cmdline_row > Rows - p_ch && Rows > p_ch)
4586 cmdline_row = Rows - p_ch;
4587 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00004588 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004589 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (curbuf->b_p_ts <= 0)
4593 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004594 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 curbuf->b_p_ts = 8;
4596 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00004597 else if (curbuf->b_p_ts > TABSTOP_MAX)
4598 {
4599 errmsg = e_invalid_argument;
4600 curbuf->b_p_ts = 8;
4601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (p_tm < 0)
4603 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004604 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 p_tm = 0;
4606 }
4607 if ((curwin->w_p_scr <= 0
4608 || (curwin->w_p_scr > curwin->w_height
4609 && curwin->w_height > 0))
4610 && full_screen)
4611 {
4612 if (pp == &(curwin->w_p_scr))
4613 {
4614 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02004615 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 win_comp_scroll(curwin);
4617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 // If 'scroll' became invalid because of a side effect silently adjust
4619 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 else if (curwin->w_p_scr <= 0)
4621 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004622 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 curwin->w_p_scr = curwin->w_height;
4624 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004625 if (p_hi < 0)
4626 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004627 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00004628 p_hi = 0;
4629 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004630 else if (p_hi > 10000)
4631 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004632 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02004633 p_hi = 10000;
4634 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004635 if (p_re < 0 || p_re > 2)
4636 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004637 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638 p_re = 0;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (p_report < 0)
4641 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004642 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 p_report = 1;
4644 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00004645 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004647 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 p_sj = Rows / 2;
4649 else
4650 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004651 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 p_sj = 1;
4653 }
4654 }
4655 if (p_so < 0 && full_screen)
4656 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004657 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 p_so = 0;
4659 }
4660 if (p_siso < 0 && full_screen)
4661 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004662 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 p_siso = 0;
4664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (p_cwh < 1)
4666 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004667 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 p_cwh = 1;
4669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (p_ut < 0)
4671 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004672 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 p_ut = 2000;
4674 }
4675 if (p_ss < 0)
4676 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004677 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 p_ss = 0;
4679 }
4680
Yegappan Lakshmanan0caaf1e2023-02-09 12:23:17 +00004681 return errmsg;
4682}
4683
4684/*
4685 * Set the value of a number option, and take care of side effects.
4686 * Returns NULL for success, or an error message for an error.
4687 */
4688 static char *
4689set_num_option(
4690 int opt_idx, // index in options[] table
4691 char_u *varp, // pointer to the option variable
4692 long value, // new value
4693 char *errbuf, // buffer for error messages
4694 size_t errbuflen, // length of "errbuf"
4695 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
4696 // OPT_MODELINE, etc.
4697{
4698 char *errmsg = NULL;
4699 long old_value = *(long *)varp;
4700#if defined(FEAT_EVAL)
4701 long old_global_value = 0; // only used when setting a local and
4702 // global option
4703#endif
4704 long old_Rows = Rows; // remember old Rows
4705 long old_Columns = Columns; // remember old Columns
4706 long *pp = (long *)varp;
4707
4708 // Disallow changing some options from secure mode.
4709 if ((secure
4710#ifdef HAVE_SANDBOX
4711 || sandbox != 0
4712#endif
4713 ) && (options[opt_idx].flags & P_SECURE))
4714 return e_not_allowed_here;
4715
4716#if defined(FEAT_EVAL)
4717 // Save the global value before changing anything. This is needed as for
4718 // a global-only option setting the "local value" in fact sets the global
4719 // value (since there is only one value).
4720 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4721 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
4722 OPT_GLOBAL);
4723#endif
4724
4725 *pp = value;
4726#ifdef FEAT_EVAL
4727 // Remember where the option was set.
4728 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4729#endif
4730#ifdef FEAT_GUI
4731 need_mouse_correct = TRUE;
4732#endif
4733
4734 if (curbuf->b_p_sw < 0)
4735 {
4736 errmsg = e_argument_must_be_positive;
4737#ifdef FEAT_VARTABS
4738 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
4739 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
4740 ? tabstop_first(curbuf->b_p_vts_array)
4741 : curbuf->b_p_ts;
4742#else
4743 curbuf->b_p_sw = curbuf->b_p_ts;
4744#endif
4745 }
4746
4747 /*
4748 * Number options that need some action when changed
4749 */
4750 errmsg = did_set_num_option(pp, value, old_value, errmsg);
4751
4752 /*
4753 * Check the bounds for numeric options here
4754 */
4755 errmsg = check_num_option_bounds(pp, old_value, old_Rows, old_Columns,
4756 errbuf, errbuflen, errmsg);
4757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004758 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
4760 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
4761
4762 options[opt_idx].flags |= P_WAS_SET;
4763
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004764#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01004765 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
4766 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02004767#endif
4768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02004770 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004771 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004772 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004773 if ((opt_flags & OPT_NO_REDRAW) == 0)
4774 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 return errmsg;
4777}
4778
4779/*
4780 * Called after an option changed: check if something needs to be redrawn.
4781 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004783check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004785 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004786 int doclear = (flags & P_RCLR) == P_RCLR;
4787 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
4793 changed_window_setting();
4794 if (flags & P_RBUF)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004795 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01004796 if (flags & P_RWINONLY)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004797 redraw_later(UPD_NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004798 if (doclear)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004799 redraw_all_later(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 else if (all)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004801 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802}
4803
4804/*
4805 * Find index for option 'arg'.
4806 * Return -1 if not found.
4807 */
Bram Moolenaardac13472019-09-16 21:06:21 +02004808 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004809findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 int opt_idx;
4812 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 int is_term_opt;
4815
4816 /*
4817 * For first call: Initialize the quick-access table.
4818 * It contains the index for the first option that starts with a certain
4819 * letter. There are 26 letters, plus the first "t_" option.
4820 */
4821 if (quick_tab[1] == 0)
4822 {
4823 p = options[0].fullname;
4824 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4825 {
4826 if (s[0] != p[0])
4827 {
4828 if (s[0] == 't' && s[1] == '_')
4829 quick_tab[26] = opt_idx;
4830 else
4831 quick_tab[CharOrdLow(s[0])] = opt_idx;
4832 }
4833 p = s;
4834 }
4835 }
4836
4837 /*
4838 * Check for name starting with an illegal character.
4839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return -1;
4842
4843 is_term_opt = (arg[0] == 't' && arg[1] == '_');
4844 if (is_term_opt)
4845 opt_idx = quick_tab[26];
4846 else
4847 opt_idx = quick_tab[CharOrdLow(arg[0])];
4848 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
4849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004850 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 break;
4852 }
4853 if (s == NULL && !is_term_opt)
4854 {
4855 opt_idx = quick_tab[CharOrdLow(arg[0])];
4856 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
4857 {
4858 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004859 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 break;
4861 s = NULL;
4862 }
4863 }
4864 if (s == NULL)
4865 opt_idx = -1;
4866 return opt_idx;
4867}
4868
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00004869#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME) \
4870 || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871/*
4872 * Get the value for an option.
4873 *
4874 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004875 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01004876 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004877 * String option: gov_string, *stringval gets allocated string.
4878 * Hidden Number option: gov_hidden_number.
4879 * Hidden Toggle option: gov_hidden_bool.
4880 * Hidden String option: gov_hidden_string.
4881 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004882 *
4883 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004885 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01004886get_option_value(
4887 char_u *name,
4888 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004889 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004890 int *flagsp,
4891 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892{
4893 int opt_idx;
4894 char_u *varp;
4895
4896 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004897 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01004898 {
4899 int key;
4900
4901 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004902 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004903 {
4904 char_u key_name[2];
4905 char_u *p;
4906
Bram Moolenaar10c75c42021-12-28 20:53:30 +00004907 if (flagsp != NULL)
4908 *flagsp = 0; // terminal option has no flags
4909
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004910 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004911 if (key < 0)
4912 {
4913 key_name[0] = KEY2TERMCAP0(key);
4914 key_name[1] = KEY2TERMCAP1(key);
4915 }
4916 else
4917 {
4918 key_name[0] = KS_KEY;
4919 key_name[1] = (key & 0xff);
4920 }
4921 p = find_termcode(key_name);
4922 if (p != NULL)
4923 {
4924 if (stringval != NULL)
4925 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004926 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004927 }
4928 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004929 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004932 varp = get_varp_scope(&(options[opt_idx]), scope);
4933
4934 if (flagsp != NULL)
4935 // Return the P_xxxx option flags.
4936 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 if (options[opt_idx].flags & P_STRING)
4939 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004940 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004941 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (stringval != NULL)
4943 {
zeertzjq51f0bc32022-05-09 13:33:39 +01004944 if ((char_u **)varp == &p_pt) // 'pastetoggle'
zeertzjqcdc83932022-09-12 13:38:41 +01004945 *stringval = str2special_save(*(char_u **)(varp), FALSE,
4946 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 // never return the value of the crypt key
zeertzjq51f0bc32022-05-09 13:33:39 +01004949 else if ((char_u **)varp == &curbuf->b_p_key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004950 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 *stringval = vim_strsave((char_u *)"*****");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952#endif
zeertzjq51f0bc32022-05-09 13:33:39 +01004953 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 *stringval = vim_strsave(*(char_u **)(varp));
4955 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004956 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004960 return (options[opt_idx].flags & P_NUM)
4961 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (options[opt_idx].flags & P_NUM)
4963 *numval = *(long *)varp;
4964 else
4965 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004966 // Special case: 'modified' is b_changed, but we also want to consider
4967 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if ((int *)varp == &curbuf->b_changed)
4969 *numval = curbufIsChanged();
4970 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004971 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004973 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974}
4975#endif
4976
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004977#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004978/*
4979 * Returns the option attributes and its value. Unlike the above function it
4980 * will return either global value or local value of the option depending on
4981 * what was requested, but it will never return global value if it was
4982 * requested to return local one and vice versa. Neither it will return
4983 * buffer-local value if it was requested to return window-local one.
4984 *
4985 * Pretends that option is absent if it is not present in the requested scope
4986 * (i.e. has no global, window-local or buffer-local value depending on
4987 * opt_type). Uses
4988 *
4989 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004990 * 0 hidden or unknown option, also option that does not have requested
4991 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004992 * see SOPT_* in vim.h for other flags
4993 *
4994 * Possible opt_type values: see SREQ_* in vim.h
4995 */
4996 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004997get_option_value_strict(
4998 char_u *name,
4999 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005000 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01005001 int opt_type,
5002 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005003{
5004 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02005005 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005006 struct vimoption *p;
5007 int r = 0;
5008
5009 opt_idx = findoption(name);
5010 if (opt_idx < 0)
5011 return 0;
5012
5013 p = &(options[opt_idx]);
5014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005015 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005016 if (p->var == NULL)
5017 return 0;
5018
5019 if (p->flags & P_BOOL)
5020 r |= SOPT_BOOL;
5021 else if (p->flags & P_NUM)
5022 r |= SOPT_NUM;
5023 else if (p->flags & P_STRING)
5024 r |= SOPT_STRING;
5025
5026 if (p->indir == PV_NONE)
5027 {
5028 if (opt_type == SREQ_GLOBAL)
5029 r |= SOPT_GLOBAL;
5030 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005031 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005032 }
5033 else
5034 {
5035 if (p->indir & PV_BOTH)
5036 r |= SOPT_GLOBAL;
5037 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005038 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005039
5040 if (p->indir & PV_WIN)
5041 {
5042 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005044 else
5045 r |= SOPT_WIN;
5046 }
5047 else if (p->indir & PV_BUF)
5048 {
5049 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005050 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005051 else
5052 r |= SOPT_BUF;
5053 }
5054 }
5055
5056 if (stringval == NULL)
5057 return r;
5058
5059 if (opt_type == SREQ_GLOBAL)
5060 varp = p->var;
5061 else
5062 {
5063 if (opt_type == SREQ_BUF)
5064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005065 // Special case: 'modified' is b_changed, but we also want to
5066 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005067 if (p->indir == PV_MOD)
5068 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005069 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005070 varp = NULL;
5071 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005072# ifdef FEAT_CRYPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073 else if (p->indir == PV_KEY)
5074 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005075 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005076 *stringval = NULL;
5077 varp = NULL;
5078 }
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00005079# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005080 else
5081 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005082 buf_T *save_curbuf = curbuf;
5083
5084 // only getting a pointer, no need to use aucmd_prepbuf()
5085 curbuf = (buf_T *)from;
5086 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005087 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02005088 curbuf = save_curbuf;
5089 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005090 }
5091 }
5092 else if (opt_type == SREQ_WIN)
5093 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02005094 win_T *save_curwin = curwin;
5095
5096 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 curbuf = curwin->w_buffer;
5098 varp = get_varp(p);
5099 curwin = save_curwin;
5100 curbuf = curwin->w_buffer;
5101 }
5102 if (varp == p->var)
5103 return (r | SOPT_UNSET);
5104 }
5105
5106 if (varp != NULL)
5107 {
5108 if (p->flags & P_STRING)
5109 *stringval = vim_strsave(*(char_u **)(varp));
5110 else if (p->flags & P_NUM)
5111 *numval = *(long *) varp;
5112 else
5113 *numval = *(int *)varp;
5114 }
5115
5116 return r;
5117}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005118
5119/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005120 * Iterate over options. First argument is a pointer to a pointer to a
5121 * structure inside options[] array, second is option type like in the above
5122 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005123 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005124 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005125 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005126 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005127 * first argument to NULL.
5128 *
5129 * Returns full option name for current option on each call.
5130 */
5131 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005132option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005133{
5134 struct vimoption *ret = NULL;
5135 do
5136 {
5137 if (*option == NULL)
5138 *option = (void *) options;
5139 else if (((struct vimoption *) (*option))->fullname == NULL)
5140 {
5141 *option = NULL;
5142 return NULL;
5143 }
5144 else
5145 *option = (void *) (((struct vimoption *) (*option)) + 1);
5146
5147 ret = ((struct vimoption *) (*option));
5148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005149 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005150 if (ret->var == NULL)
5151 {
5152 ret = NULL;
5153 continue;
5154 }
5155
5156 switch (opt_type)
5157 {
5158 case SREQ_GLOBAL:
5159 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
5160 ret = NULL;
5161 break;
5162 case SREQ_BUF:
5163 if (!(ret->indir & PV_BUF))
5164 ret = NULL;
5165 break;
5166 case SREQ_WIN:
5167 if (!(ret->indir & PV_WIN))
5168 ret = NULL;
5169 break;
5170 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005171 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01005172 return NULL;
5173 }
5174 }
5175 while (ret == NULL);
5176
5177 return (char_u *)ret->fullname;
5178}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005179#endif
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005182 * Return the flags for the option at 'opt_idx'.
5183 */
5184 long_u
5185get_option_flags(int opt_idx)
5186{
5187 return options[opt_idx].flags;
5188}
5189
5190/*
5191 * Set a flag for the option at 'opt_idx'.
5192 */
5193 void
5194set_option_flag(int opt_idx, long_u flag)
5195{
5196 options[opt_idx].flags |= flag;
5197}
5198
5199/*
5200 * Clear a flag for the option at 'opt_idx'.
5201 */
5202 void
5203clear_option_flag(int opt_idx, long_u flag)
5204{
5205 options[opt_idx].flags &= ~flag;
5206}
5207
5208/*
5209 * Returns TRUE if the option at 'opt_idx' is a global option
5210 */
5211 int
5212is_global_option(int opt_idx)
5213{
5214 return options[opt_idx].indir == PV_NONE;
5215}
5216
5217/*
5218 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
5219 * local value.
5220 */
5221 int
5222is_global_local_option(int opt_idx)
5223{
5224 return options[opt_idx].indir & PV_BOTH;
5225}
5226
5227/*
5228 * Returns TRUE if the option at 'opt_idx' is a window-local option
5229 */
5230 int
5231is_window_local_option(int opt_idx)
5232{
5233 return options[opt_idx].var == VAR_WIN;
5234}
5235
5236/*
5237 * Returns TRUE if the option at 'opt_idx' is a hidden option
5238 */
5239 int
5240is_hidden_option(int opt_idx)
5241{
5242 return options[opt_idx].var == NULL;
5243}
5244
5245#if defined(FEAT_CRYPT) || defined(PROTO)
5246/*
5247 * Returns TRUE if the option at 'opt_idx' is a crypt key option
5248 */
5249 int
5250is_crypt_key_option(int opt_idx)
5251{
5252 return options[opt_idx].indir == PV_KEY;
5253}
5254#endif
5255
5256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 * Set the value of option "name".
5258 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005259 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005260 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005262 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005263set_option_value(
5264 char_u *name,
5265 long number,
5266 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005267 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 int opt_idx;
5270 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005271 long_u flags;
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005272 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005275 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005276 {
5277 int key;
5278
5279 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005280 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01005281 {
5282 char_u key_name[2];
5283
5284 if (key < 0)
5285 {
5286 key_name[0] = KEY2TERMCAP0(key);
5287 key_name[1] = KEY2TERMCAP1(key);
5288 }
5289 else
5290 {
5291 key_name[0] = KS_KEY;
5292 key_name[1] = (key & 0xff);
5293 }
5294 add_termcode(key_name, string, FALSE);
5295 if (full_screen)
5296 ttest(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005297 redraw_all_later(UPD_CLEAR);
Bram Moolenaare353c402017-02-04 19:49:16 +01005298 return NULL;
5299 }
5300
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005301 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01005302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else
5304 {
5305 flags = options[opt_idx].flags;
5306#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005309 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02005310 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005311 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005314 if (flags & P_STRING)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005315 return set_string_option(opt_idx, string, opt_flags, errbuf);
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005316
5317 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
5318 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005320 if (number == 0 && string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005322 int idx;
5323
5324 // Either we are given a string or we are setting option
5325 // to zero.
5326 for (idx = 0; string[idx] == '0'; ++idx)
5327 ;
5328 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005329 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005330 // There's another character after zeros or the string
5331 // is empty. In both cases, we are trying to set a
5332 // num option using a string.
5333 semsg(_(e_number_required_after_str_equal_str),
5334 name, string);
5335 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005336
Bram Moolenaar96bb6212007-06-19 18:52:53 +00005337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005339 if (flags & P_NUM)
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005340 {
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005341 return set_num_option(opt_idx, varp, number,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +00005342 errbuf, sizeof(errbuf), opt_flags);
5343 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005344 else
5345 return set_bool_option(opt_idx, varp, (int)number, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
=?UTF-8?q?Dundar=20G=C3=B6c?=f26c1612022-04-07 13:26:34 +01005347
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02005349 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350}
5351
5352/*
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005353 * Call set_option_value() and when an error is returned report it.
5354 */
5355 void
5356set_option_value_give_err(
5357 char_u *name,
5358 long number,
5359 char_u *string,
5360 int opt_flags) // OPT_LOCAL or 0 (both)
5361{
5362 char *errmsg = set_option_value(name, number, string, opt_flags);
5363
5364 if (errmsg != NULL)
5365 emsg(_(errmsg));
5366}
5367
5368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 * Get the terminal code for a terminal option.
5370 * Returns NULL when not found.
5371 */
5372 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005373get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375 int opt_idx;
5376 char_u *varp;
5377
5378 if (tname[0] != 't' || tname[1] != '_' ||
5379 tname[2] == NUL || tname[3] == NUL)
5380 return NULL;
5381 if ((opt_idx = findoption(tname)) >= 0)
5382 {
5383 varp = get_varp(&(options[opt_idx]));
5384 if (varp != NULL)
5385 varp = *(char_u **)(varp);
5386 return varp;
5387 }
5388 return find_termcode(tname + 2);
5389}
5390
5391 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005392get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 int i;
5395
5396 i = findoption((char_u *)"hl");
5397 if (i >= 0)
5398 return options[i].def_val[VI_DEFAULT];
5399 return (char_u *)NULL;
5400}
5401
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005402 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005403get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005404{
5405 int i;
5406
5407 i = findoption((char_u *)"enc");
5408 if (i >= 0)
5409 return options[i].def_val[VI_DEFAULT];
5410 return (char_u *)NULL;
5411}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005412
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005413#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005414 int
5415is_option_allocated(char *name)
5416{
5417 int idx = findoption((char_u *)name);
5418
5419 return idx >= 0 && (options[idx].flags & P_ALLOCED);
5420}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01005421#endif
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00005422
Bram Moolenaar28f84e12022-07-27 12:30:13 +01005423#if defined(FEAT_EVAL) || defined(PROTO)
5424/*
5425 * Return TRUE if "name" is a string option.
5426 * Returns FALSE if option "name" does not exist.
5427 */
5428 int
5429is_string_option(char_u *name)
5430{
5431 int idx = findoption(name);
5432
5433 return idx >= 0 && (options[idx].flags & P_STRING);
5434}
5435#endif
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437/*
5438 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005439 * When "has_lt" is true there is a '<' before "*arg_arg".
5440 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 */
5442 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005443find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005445 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005447 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
5449 /*
5450 * Don't use get_special_key_code() for t_xx, we don't want it to call
5451 * add_termcap_entry().
5452 */
5453 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
5454 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02005455 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005459 key = find_special_key(&arg, &modifiers,
5460 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005461 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 key = 0;
5463 }
5464 return key;
5465}
5466
5467/*
5468 * if 'all' == 0: show changed options
5469 * if 'all' == 1: show all normal options
5470 * if 'all' == 2: show all terminal options
5471 */
5472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005473showoptions(
5474 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005475 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 struct vimoption *p;
5478 int col;
5479 int isterm;
5480 char_u *varp;
5481 struct vimoption **items;
5482 int item_count;
5483 int run;
5484 int row, rows;
5485 int cols;
5486 int i;
5487 int len;
5488
5489#define INC 20
5490#define GAP 3
5491
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005492 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (items == NULL)
5494 return;
5495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005496 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005498 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005500 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005502 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005504 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005507 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * 1. display the short items
5509 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005510 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 */
5512 for (run = 1; run <= 2 && !got_int; ++run)
5513 {
5514 /*
5515 * collect the items in items[]
5516 */
5517 item_count = 0;
5518 for (p = &options[0]; p->fullname != NULL; p++)
5519 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02005520 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005521 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02005522 continue;
5523
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 varp = NULL;
5525 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005526 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
5528 if (p->indir != PV_NONE && !isterm)
5529 varp = get_varp_scope(p, opt_flags);
5530 }
5531 else
5532 varp = get_varp(p);
5533 if (varp != NULL
5534 && ((all == 2 && isterm)
5535 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005536 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01005538 if (opt_flags & OPT_ONECOLUMN)
5539 len = Columns;
5540 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005541 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 else
5543 {
5544 option_value2string(p, opt_flags);
5545 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
5546 }
5547 if ((len <= INC - GAP && run == 1) ||
5548 (len > INC - GAP && run == 2))
5549 items[item_count++] = p;
5550 }
5551 }
5552
5553 /*
5554 * display the items
5555 */
5556 if (run == 1)
5557 {
5558 cols = (Columns + GAP - 3) / INC;
5559 if (cols == 0)
5560 cols = 1;
5561 rows = (item_count + cols - 1) / cols;
5562 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005563 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 rows = item_count;
5565 for (row = 0; row < rows && !got_int; ++row)
5566 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005567 msg_putchar('\n'); // go to next line
5568 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 break;
5570 col = 0;
5571 for (i = row; i < item_count; i += rows)
5572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 showoneopt(items[i], opt_flags);
5575 col += INC;
5576 }
5577 out_flush();
5578 ui_breakcheck();
5579 }
5580 }
5581 vim_free(items);
5582}
5583
5584/*
5585 * Return TRUE if option "p" has its default value.
5586 */
5587 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005588optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590 int dvi;
5591
5592 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005594 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005596 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 // the cast to long is required for Manx C, long_i is
5599 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005600 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005601 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
5603}
5604
5605/*
5606 * showoneopt: show the value of one option
5607 * must not be called with a hidden option!
5608 */
5609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005610showoneopt(
5611 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005612 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005614 char_u *varp;
5615 int save_silent = silent_mode;
5616
5617 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005618 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 varp = get_varp_scope(p, opt_flags);
5621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
5624 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01005625 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005627 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005629 msg_puts(" ");
5630 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (!(p->flags & P_BOOL))
5632 {
5633 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 option_value2string(p, opt_flags);
5636 msg_outtrans(NameBuff);
5637 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005638
5639 silent_mode = save_silent;
5640 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643/*
5644 * Write modified options as ":set" commands to a file.
5645 *
5646 * There are three values for "opt_flags":
5647 * OPT_GLOBAL: Write global option values and fresh values of
5648 * buffer-local options (used for start of a session
5649 * file).
5650 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
5651 * curwin (used for a vimrc file).
5652 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
5653 * and local values for window-local options of
5654 * curwin. Local values are also written when at the
5655 * default value, because a modeline or autocommand
5656 * may have set them when doing ":edit file" and the
5657 * user has set them back at the default or fresh
5658 * value.
5659 * When "local_only" is TRUE, don't write fresh
5660 * values, only local values (for ":mkview").
5661 * (fresh value = value used for a new buffer or window for a local option).
5662 *
5663 * Return FAIL on error, OK otherwise.
5664 */
5665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005666makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005669 char_u *varp; // currently used value
5670 char_u *varp_fresh; // local value
5671 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 char *cmd;
5673 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005674 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
5676 /*
5677 * The options that don't have a default (terminal name, columns, lines)
5678 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005679 * Do the loop over "options[]" twice: once for options with the
5680 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005682 for (pri = 1; pri >= 0; --pri)
5683 {
5684 for (p = &options[0]; !istermoption(p); p++)
5685 if (!(p->flags & P_NO_MKRC)
5686 && !istermoption(p)
5687 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005689 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
5691 continue;
5692
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005693 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
5694 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
5696 continue;
5697
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005698 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005700 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 continue;
5702
Bram Moolenaard23b7142021-04-17 21:04:34 +02005703 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
5704 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02005705 continue;
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 round = 2;
5708 if (p->indir != PV_NONE)
5709 {
5710 if (p->var == VAR_WIN)
5711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005712 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 if (!(opt_flags & OPT_LOCAL))
5714 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005715 // When fresh value of window-local option is not at the
5716 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (!(opt_flags & OPT_GLOBAL) && !local_only)
5718 {
5719 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02005720 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 round = 1;
5723 varp_local = varp;
5724 varp = varp_fresh;
5725 }
5726 }
5727 }
5728 }
5729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005730 // Round 1: fresh value for window-local options.
5731 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 for ( ; round <= 2; varp = varp_local, ++round)
5733 {
5734 if (round == 1 || (opt_flags & OPT_GLOBAL))
5735 cmd = "set";
5736 else
5737 cmd = "setlocal";
5738
5739 if (p->flags & P_BOOL)
5740 {
5741 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
5742 return FAIL;
5743 }
5744 else if (p->flags & P_NUM)
5745 {
5746 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
5747 return FAIL;
5748 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005749 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005751 int do_endif = FALSE;
5752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005753 // Don't set 'syntax' and 'filetype' again if the value is
5754 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005755 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005756#if defined(FEAT_SYN_HL)
5757 p->indir == PV_SYN ||
5758#endif
5759 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
5761 if (fprintf(fd, "if &%s != '%s'", p->fullname,
5762 *(char_u **)(varp)) < 0
5763 || put_eol(fd) < 0)
5764 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005765 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005768 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005770 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772 if (put_line(fd, "endif") == FAIL)
5773 return FAIL;
5774 }
5775 }
5776 }
5777 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00005778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 return OK;
5780}
5781
5782#if defined(FEAT_FOLDING) || defined(PROTO)
5783/*
5784 * Generate set commands for the local fold options only. Used when
5785 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
5786 */
5787 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005788makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005790 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005792 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 == FAIL
5794# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005795 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005797 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 == FAIL
5799 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
5800 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
5801 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
5802 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
5803 )
5804 return FAIL;
5805
5806 return OK;
5807}
5808#endif
5809
5810 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005811put_setstring(
5812 FILE *fd,
5813 char *cmd,
5814 char *name,
5815 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005816 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005819 char_u *buf = NULL;
5820 char_u *part = NULL;
5821 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822
5823 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5824 return FAIL;
5825 if (*valuep != NULL)
5826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005827 // Output 'pastetoggle' as key names. For other
5828 // options some characters have to be escaped with
5829 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (valuep == &p_pt)
5831 {
5832 s = *valuep;
5833 while (*s != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01005834 if (put_escstr(fd, str2special(&s, FALSE, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 return FAIL;
5836 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005837 // expand the option value, replace $HOME by ~
5838 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005840 int size = (int)STRLEN(*valuep) + 1;
5841
5842 // replace home directory in the whole option value into "buf"
5843 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02005844 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005845 goto fail;
5846 home_replace(NULL, *valuep, buf, size, FALSE);
5847
5848 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005849 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005850 // can be expanded when read back.
5851 if (size >= MAXPATHL && (flags & P_COMMA) != 0
5852 && vim_strchr(*valuep, ',') != NULL)
5853 {
5854 part = alloc(size);
5855 if (part == NULL)
5856 goto fail;
5857
5858 // write line break to clear the option, e.g. ':set rtp='
5859 if (put_eol(fd) == FAIL)
5860 goto fail;
5861
5862 p = buf;
5863 while (*p != NUL)
5864 {
5865 // for each comma separated option part, append value to
5866 // the option, :set rtp+=value
5867 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
5868 goto fail;
5869 (void)copy_option_part(&p, part, size, ",");
5870 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
5871 goto fail;
5872 }
5873 vim_free(buf);
5874 vim_free(part);
5875 return OK;
5876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02005878 {
5879 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02005881 }
5882 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 }
5884 else if (put_escstr(fd, *valuep, 2) == FAIL)
5885 return FAIL;
5886 }
5887 if (put_eol(fd) < 0)
5888 return FAIL;
5889 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01005890fail:
5891 vim_free(buf);
5892 vim_free(part);
5893 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894}
5895
5896 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005897put_setnum(
5898 FILE *fd,
5899 char *cmd,
5900 char *name,
5901 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 long wc;
5904
5905 if (fprintf(fd, "%s %s=", cmd, name) < 0)
5906 return FAIL;
5907 if (wc_use_keyname((char_u *)valuep, &wc))
5908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005909 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
5911 return FAIL;
5912 }
5913 else if (fprintf(fd, "%ld", *valuep) < 0)
5914 return FAIL;
5915 if (put_eol(fd) < 0)
5916 return FAIL;
5917 return OK;
5918}
5919
5920 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005921put_setbool(
5922 FILE *fd,
5923 char *cmd,
5924 char *name,
5925 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005927 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00005928 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
5930 || put_eol(fd) < 0)
5931 return FAIL;
5932 return OK;
5933}
5934
5935/*
5936 * Clear all the terminal options.
5937 * If the option has been allocated, free the memory.
5938 * Terminal options are never hidden or indirect.
5939 */
5940 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005941clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 /*
5944 * Reset a few things before clearing the old options. This may cause
5945 * outputting a few things that the terminal doesn't understand, but the
5946 * screen will be cleared later, so this is OK.
5947 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005948 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005949 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005951 // When starting the GUI close the display opened for the clipboard.
5952 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (gui.starting)
5954 clear_xterm_clip();
5955#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005956 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005958 free_termoptions();
5959}
5960
5961 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005962free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005963{
5964 struct vimoption *p;
5965
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005966 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 if (istermoption(p))
5968 {
5969 if (p->flags & P_ALLOCED)
5970 free_string_option(*(char_u **)(p->var));
5971 if (p->flags & P_DEF_ALLOCED)
5972 free_string_option(p->def_val[VI_DEFAULT]);
5973 *(char_u **)(p->var) = empty_option;
5974 p->def_val[VI_DEFAULT] = empty_option;
5975 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005976#ifdef FEAT_EVAL
5977 // remember where the option was cleared
5978 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 }
5981 clear_termcodes();
5982}
5983
5984/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005985 * Free the string for one term option, if it was allocated.
5986 * Set the string to empty_option and clear allocated flag.
5987 * "var" points to the option value.
5988 */
5989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005990free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005991{
5992 struct vimoption *p;
5993
5994 for (p = &options[0]; p->fullname != NULL; p++)
5995 if (p->var == var)
5996 {
5997 if (p->flags & P_ALLOCED)
5998 free_string_option(*(char_u **)(p->var));
5999 *(char_u **)(p->var) = empty_option;
6000 p->flags &= ~P_ALLOCED;
6001 break;
6002 }
6003}
6004
6005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 * Set the terminal option defaults to the current value.
6007 * Used after setting the terminal name.
6008 */
6009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006010set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
6012 struct vimoption *p;
6013
6014 for (p = &options[0]; p->fullname != NULL; p++)
6015 {
6016 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
6017 {
6018 if (p->flags & P_DEF_ALLOCED)
6019 {
6020 free_string_option(p->def_val[VI_DEFAULT]);
6021 p->flags &= ~P_DEF_ALLOCED;
6022 }
6023 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
6024 if (p->flags & P_ALLOCED)
6025 {
6026 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006027 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 }
6029 }
6030 }
6031}
6032
6033/*
6034 * return TRUE if 'p' starts with 't_'
6035 */
6036 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006037istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
6039 return (p->fullname[0] == 't' && p->fullname[1] == '_');
6040}
6041
Bram Moolenaardac13472019-09-16 21:06:21 +02006042/*
6043 * Returns TRUE if the option at 'opt_idx' starts with 't_'
6044 */
6045 int
6046istermoption_idx(int opt_idx)
6047{
6048 return istermoption(&options[opt_idx]);
6049}
6050
Bram Moolenaar113e1072019-01-20 15:30:40 +01006051#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006053 * Unset local option value, similar to ":set opt<".
6054 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006055 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006056unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006057{
6058 struct vimoption *p;
6059 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006060 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006061
6062 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02006063 if (opt_idx < 0)
6064 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006065 p = &(options[opt_idx]);
6066
6067 switch ((int)p->indir)
6068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006069 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006070 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006071 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006072 break;
6073 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006074 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006075 break;
6076 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006077 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006078 break;
6079 case PV_AR:
6080 buf->b_p_ar = -1;
6081 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006082 case PV_BKC:
6083 clear_string_option(&buf->b_p_bkc);
6084 buf->b_bkc_flags = 0;
6085 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006086 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006087 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006088 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006089 case PV_TC:
6090 clear_string_option(&buf->b_p_tc);
6091 buf->b_tc_flags = 0;
6092 break;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006093 case PV_SISO:
6094 curwin->w_p_siso = -1;
6095 break;
6096 case PV_SO:
6097 curwin->w_p_so = -1;
6098 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006099# ifdef FEAT_FIND_ID
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006100 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006101 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006102 break;
6103 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006104 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006105 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006106# endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006107 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006108 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006109 break;
6110 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006111 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006112 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006113# ifdef FEAT_COMPL_FUNC
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006114 case PV_TSRFU:
6115 clear_string_option(&buf->b_p_tsrfu);
6116 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006117# endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006118 case PV_FP:
6119 clear_string_option(&buf->b_p_fp);
6120 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006121# ifdef FEAT_QUICKFIX
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006122 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006123 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006124 break;
6125 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006126 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006127 break;
6128 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006129 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006130 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006131# endif
6132# if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006133 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006134 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006135 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006136# endif
6137# if defined(FEAT_CRYPT)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006138 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006139 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006140 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006141# endif
6142# ifdef FEAT_LINEBREAK
Bram Moolenaaree857022019-11-09 23:26:40 +01006143 case PV_SBR:
6144 clear_string_option(&((win_T *)from)->w_p_sbr);
6145 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006146# endif
6147# ifdef FEAT_STL_OPT
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006148 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02006149 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006150 break;
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00006151# endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006152 case PV_UL:
6153 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
6154 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006155 case PV_LW:
6156 clear_string_option(&buf->b_p_lw);
6157 break;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006158 case PV_MENC:
6159 clear_string_option(&buf->b_p_menc);
6160 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01006161 case PV_LCS:
6162 clear_string_option(&((win_T *)from)->w_p_lcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006163 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006164 redraw_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006165 break;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006166 case PV_FCS:
6167 clear_string_option(&((win_T *)from)->w_p_fcs);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006168 set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006169 redraw_later(UPD_NOT_VALID);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006170 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006171 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02006172 clear_string_option(&((win_T *)from)->w_p_ve);
6173 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006174 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006175 }
6176}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006177#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006178
6179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006181 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 */
6183 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006184get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006186 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188 if (p->var == VAR_WIN)
6189 return (char_u *)GLOBAL_WO(get_varp(p));
6190 return p->var;
6191 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006192 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
6194 switch ((int)p->indir)
6195 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006196 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006198 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
6199 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
6200 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006202 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
6203 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
6204 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006205 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006206 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006207 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006208 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
6209 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006211 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
6212 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006214 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
6215 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006216#ifdef FEAT_COMPL_FUNC
6217 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
6218#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006219#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6220 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
6221#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006222#if defined(FEAT_CRYPT)
6223 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
6224#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006225#ifdef FEAT_LINEBREAK
6226 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
6227#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006228#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006229 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006230#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006231 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006232 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006233 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006234 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02006235 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006236 case PV_FCS: return (char_u *)&(curwin->w_p_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006237 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006240 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
6242 return get_varp(p);
6243}
6244
6245/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006246 * Get pointer to option variable at 'opt_idx', depending on local or global
6247 * scope.
6248 */
6249 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006250get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02006251{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006252 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02006253}
6254
6255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 * Get pointer to option variable.
6257 */
6258 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006259get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006261 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (p->var == NULL)
6263 return NULL;
6264
6265 switch ((int)p->indir)
6266 {
6267 case PV_NONE: return p->var;
6268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006269 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006270 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006272 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006274 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00006276 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006278 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006280 case PV_TC: return *curbuf->b_p_tc != NUL
6281 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006282 case PV_BKC: return *curbuf->b_p_bkc != NUL
6283 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01006284 case PV_SISO: return curwin->w_p_siso >= 0
6285 ? (char_u *)&(curwin->w_p_siso) : p->var;
6286 case PV_SO: return curwin->w_p_so >= 0
6287 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006289 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006291 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 ? (char_u *)&(curbuf->b_p_inc) : p->var;
6293#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006294 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006296 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006298#ifdef FEAT_COMPL_FUNC
6299 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
6300 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
6301#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006302 case PV_FP: return *curbuf->b_p_fp != NUL
6303 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006305 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006307 case PV_GP: return *curbuf->b_p_gp != NUL
6308 ? (char_u *)&(curbuf->b_p_gp) : p->var;
6309 case PV_MP: return *curbuf->b_p_mp != NUL
6310 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006312#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6313 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
6314 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
6315#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006316#if defined(FEAT_CRYPT)
6317 case PV_CM: return *curbuf->b_p_cm != NUL
6318 ? (char_u *)&(curbuf->b_p_cm) : p->var;
6319#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006320#ifdef FEAT_LINEBREAK
6321 case PV_SBR: return *curwin->w_p_sbr != NUL
6322 ? (char_u *)&(curwin->w_p_sbr) : p->var;
6323#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006324#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006325 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006326 ? (char_u *)&(curwin->w_p_stl) : p->var;
6327#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006328 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
6329 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006330 case PV_LW: return *curbuf->b_p_lw != NUL
6331 ? (char_u *)&(curbuf->b_p_lw) : p->var;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006332 case PV_MENC: return *curbuf->b_p_menc != NUL
6333 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334#ifdef FEAT_ARABIC
6335 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
6336#endif
6337 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006338 case PV_LCS: return *curwin->w_p_lcs != NUL
6339 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006340 case PV_FCS: return *curwin->w_p_fcs != NUL
6341 ? (char_u *)&(curwin->w_p_fcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02006342 case PV_VE: return *curwin->w_p_ve != NUL
6343 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006344#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006345 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
6346#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006347#ifdef FEAT_SYN_HL
6348 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
6349 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006350 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006351 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353#ifdef FEAT_DIFF
6354 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
6355#endif
6356#ifdef FEAT_FOLDING
6357 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
6358 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
6359 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
6360 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
6361 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
6362 case PV_FML: return (char_u *)&(curwin->w_p_fml);
6363 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
6364# ifdef FEAT_EVAL
6365 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
6366 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
6367# endif
6368 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
6369#endif
6370 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02006371 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006372#ifdef FEAT_LINEBREAK
6373 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
6374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006376 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006377#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
6379#endif
6380#ifdef FEAT_RIGHTLEFT
6381 case PV_RL: return (char_u *)&(curwin->w_p_rl);
6382 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
6383#endif
6384 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
Bram Moolenaarf6196f42022-10-02 21:29:55 +01006385 case PV_SMS: return (char_u *)&(curwin->w_p_sms);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
6387#ifdef FEAT_LINEBREAK
6388 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006389 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
6390 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006392 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006394 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006395#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006396 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
6397 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
6398#endif
6399#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006400 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
6401 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
6402 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
6406 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
6409 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
6411 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
6413 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
6414 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01006415 case PV_CINSD: return (char_u *)&(curbuf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418#ifdef FEAT_FOLDING
6419 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
6420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006422#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006423 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006425#ifdef FEAT_COMPL_FUNC
6426 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006427 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006428#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006429#ifdef FEAT_EVAL
6430 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
6431#endif
Bram Moolenaar3f68a412022-10-28 17:04:21 +01006432 case PV_EOF: return (char_u *)&(curbuf->b_p_eof);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006434 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006440 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
6442 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
6443 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
6444 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
6445#ifdef FEAT_FIND_ID
6446# ifdef FEAT_EVAL
6447 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
6448# endif
6449#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006450#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
6452 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
6453#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006454#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006455 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
6456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457#ifdef FEAT_CRYPT
6458 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
6459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006461 case PV_LOP: return (char_u *)&(curbuf->b_p_lop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
6463 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
6464 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
6465 case PV_MOD: return (char_u *)&(curbuf->b_changed);
6466 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006468 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 case PV_SI: return (char_u *)&(curbuf->b_p_si);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
6475#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006476 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006477 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
6478#endif
6479#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02006480 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
6481 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
6482 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006483 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484#endif
6485 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
6486 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
6487 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
6488 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006489#ifdef FEAT_PERSISTENT_UNDO
6490 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
6491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
6493#ifdef FEAT_KEYMAP
6494 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
6495#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006496#ifdef FEAT_SIGNS
6497 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
6498#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006499#ifdef FEAT_VARTABS
6500 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
6501 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
6502#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00006503 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006505 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 return (char_u *)&(curbuf->b_p_wm);
6507}
6508
6509/*
Bram Moolenaardac13472019-09-16 21:06:21 +02006510 * Return a pointer to the variable for option at 'opt_idx'
6511 */
6512 char_u *
6513get_option_var(int opt_idx)
6514{
6515 return options[opt_idx].var;
6516}
6517
Dominique Pelle748b3082022-01-08 12:41:16 +00006518#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02006519/*
6520 * Return the full name of the option at 'opt_idx'
6521 */
6522 char_u *
6523get_option_fullname(int opt_idx)
6524{
6525 return (char_u *)options[opt_idx].fullname;
6526}
Dominique Pelle748b3082022-01-08 12:41:16 +00006527#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02006528
6529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 * Get the value of 'equalprg', either the buffer-local one or the global one.
6531 */
6532 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006533get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 if (*curbuf->b_p_ep == NUL)
6536 return p_ep;
6537 return curbuf->b_p_ep;
6538}
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540/*
6541 * Copy options from one window to another.
6542 * Used when splitting a window.
6543 */
6544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006545win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
6548 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02006549 after_copy_winopt(wp_to);
6550}
6551
6552/*
6553 * After copying window options: update variables depending on options.
6554 */
6555 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00006556after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02006557{
6558#ifdef FEAT_LINEBREAK
6559 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02006560#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006561#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02006562 fill_culopt_flags(NULL, wp);
6563 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02006564#endif
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01006565 set_chars_option(wp, &wp->w_p_lcs, TRUE);
6566 set_chars_option(wp, &wp->w_p_fcs, TRUE);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006567}
6568
6569 static char_u *
6570copy_option_val(char_u *val)
6571{
6572 if (val == empty_option)
6573 return empty_option; // no need to allocate memory
6574 return vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577/*
6578 * Copy the options from one winopt_T to another.
6579 * Doesn't free the old option values in "to", use clear_winopt() for that.
6580 * The 'scroll' option is not copied, because it depends on the window height.
6581 * The 'previewwindow' option is reset, there can be only one preview window.
6582 */
6583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006584copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586#ifdef FEAT_ARABIC
6587 to->wo_arab = from->wo_arab;
6588#endif
6589 to->wo_list = from->wo_list;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006590 to->wo_lcs = copy_option_val(from->wo_lcs);
6591 to->wo_fcs = copy_option_val(from->wo_fcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02006593 to->wo_rnu = from->wo_rnu;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006594 to->wo_ve = copy_option_val(from->wo_ve);
Gary Johnson51ad8502021-08-03 18:33:08 +02006595 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006596#ifdef FEAT_LINEBREAK
6597 to->wo_nuw = from->wo_nuw;
6598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599#ifdef FEAT_RIGHTLEFT
6600 to->wo_rl = from->wo_rl;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006601 to->wo_rlc = copy_option_val(from->wo_rlc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006603#ifdef FEAT_LINEBREAK
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006604 to->wo_sbr = copy_option_val(from->wo_sbr);
Bram Moolenaaree857022019-11-09 23:26:40 +01006605#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006606#ifdef FEAT_STL_OPT
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006607 to->wo_stl = copy_option_val(from->wo_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006610#ifdef FEAT_DIFF
6611 to->wo_wrap_save = from->wo_wrap_save;
6612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613#ifdef FEAT_LINEBREAK
6614 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02006615 to->wo_bri = from->wo_bri;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006616 to->wo_briopt = copy_option_val(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617#endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006618 to->wo_wcr = copy_option_val(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006620 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaarb1fd26d2022-10-03 11:23:02 +01006621 to->wo_sms = from->wo_sms;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01006622 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006623 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006624#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006625 to->wo_spell = from->wo_spell;
6626#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006627#ifdef FEAT_SYN_HL
6628 to->wo_cuc = from->wo_cuc;
6629 to->wo_cul = from->wo_cul;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006630 to->wo_culopt = copy_option_val(from->wo_culopt);
6631 to->wo_cc = copy_option_val(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#ifdef FEAT_DIFF
6634 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006635 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006637#ifdef FEAT_CONCEAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006638 to->wo_cocu = copy_option_val(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02006639 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006640#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006641#ifdef FEAT_TERMINAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006642 to->wo_twk = copy_option_val(from->wo_twk);
6643 to->wo_tws = copy_option_val(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645#ifdef FEAT_FOLDING
6646 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006647 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006649 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006650 to->wo_fdi = copy_option_val(from->wo_fdi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 to->wo_fml = from->wo_fml;
6652 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02006653 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006654 to->wo_fdm = copy_option_val(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006655 to->wo_fdm_save = from->wo_diff_saved
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006656 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 to->wo_fdn = from->wo_fdn;
6658# ifdef FEAT_EVAL
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006659 to->wo_fde = copy_option_val(from->wo_fde);
6660 to->wo_fdt = copy_option_val(from->wo_fdt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661# endif
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006662 to->wo_fmr = copy_option_val(from->wo_fmr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006664#ifdef FEAT_SIGNS
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006665 to->wo_scl = copy_option_val(from->wo_scl);
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006666#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006667
6668#ifdef FEAT_EVAL
6669 // Copy the script context so that we know where the value was last set.
6670 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
6671 sizeof(to->wo_script_ctx));
6672#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006673 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674}
6675
6676/*
6677 * Check string options in a window for a NULL value.
6678 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006679 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006680check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
6682 check_winopt(&win->w_onebuf_opt);
6683 check_winopt(&win->w_allbuf_opt);
6684}
6685
6686/*
6687 * Check for NULL pointers in a winopt_T and replace them with empty_option.
6688 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02006689 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006690check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692#ifdef FEAT_FOLDING
6693 check_string_option(&wop->wo_fdi);
6694 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006695 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696# ifdef FEAT_EVAL
6697 check_string_option(&wop->wo_fde);
6698 check_string_option(&wop->wo_fdt);
6699# endif
6700 check_string_option(&wop->wo_fmr);
6701#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006702#ifdef FEAT_SIGNS
6703 check_string_option(&wop->wo_scl);
6704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705#ifdef FEAT_RIGHTLEFT
6706 check_string_option(&wop->wo_rlc);
6707#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006708#ifdef FEAT_LINEBREAK
6709 check_string_option(&wop->wo_sbr);
6710#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006711#ifdef FEAT_STL_OPT
6712 check_string_option(&wop->wo_stl);
6713#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006714#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006715 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006716 check_string_option(&wop->wo_cc);
6717#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006718#ifdef FEAT_CONCEAL
6719 check_string_option(&wop->wo_cocu);
6720#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006721#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006722 check_string_option(&wop->wo_twk);
6723 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006724#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006725#ifdef FEAT_LINEBREAK
6726 check_string_option(&wop->wo_briopt);
6727#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006728 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01006729 check_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006730 check_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006731 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732}
6733
6734/*
6735 * Free the allocated memory inside a winopt_T.
6736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006738clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740#ifdef FEAT_FOLDING
6741 clear_string_option(&wop->wo_fdi);
6742 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02006743 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744# ifdef FEAT_EVAL
6745 clear_string_option(&wop->wo_fde);
6746 clear_string_option(&wop->wo_fdt);
6747# endif
6748 clear_string_option(&wop->wo_fmr);
6749#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006750#ifdef FEAT_SIGNS
6751 clear_string_option(&wop->wo_scl);
6752#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02006753#ifdef FEAT_LINEBREAK
6754 clear_string_option(&wop->wo_briopt);
6755#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006756 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757#ifdef FEAT_RIGHTLEFT
6758 clear_string_option(&wop->wo_rlc);
6759#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01006760#ifdef FEAT_LINEBREAK
6761 clear_string_option(&wop->wo_sbr);
6762#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006763#ifdef FEAT_STL_OPT
6764 clear_string_option(&wop->wo_stl);
6765#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02006766#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02006767 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006768 clear_string_option(&wop->wo_cc);
6769#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006770#ifdef FEAT_CONCEAL
6771 clear_string_option(&wop->wo_cocu);
6772#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006773#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006774 clear_string_option(&wop->wo_twk);
6775 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006776#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01006777 clear_string_option(&wop->wo_lcs);
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01006778 clear_string_option(&wop->wo_fcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02006779 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780}
6781
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006782#ifdef FEAT_EVAL
6783// Index into the options table for a buffer-local option enum.
6784static int buf_opt_idx[BV_COUNT];
6785# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
6786
6787/*
6788 * Initialize buf_opt_idx[] if not done already.
6789 */
6790 static void
6791init_buf_opt_idx(void)
6792{
6793 static int did_init_buf_opt_idx = FALSE;
6794 int i;
6795
6796 if (did_init_buf_opt_idx)
6797 return;
6798 did_init_buf_opt_idx = TRUE;
6799 for (i = 0; !istermoption_idx(i); i++)
6800 if (options[i].indir & PV_BUF)
6801 buf_opt_idx[options[i].indir & PV_MASK] = i;
6802}
6803#else
6804# define COPY_OPT_SCTX(buf, bv)
6805#endif
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807/*
6808 * Copy global option values to local options for one buffer.
6809 * Used when creating a new buffer and sometimes when entering a buffer.
6810 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006811 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
6813 * appropriate.
6814 * BCO_NOHELP Don't copy the values to a help buffer.
6815 */
6816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006817buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
6819 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006820 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 int dont_do_help;
6822 int did_isk = FALSE;
6823
6824 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 * Skip this when the option defaults have not been set yet. Happens when
6826 * main() allocates the first buffer.
6827 */
6828 if (p_cpo != NULL)
6829 {
6830 /*
6831 * Always copy when entering and 'cpo' contains 'S'.
6832 * Don't copy when already initialized.
6833 * Don't copy when 'cpo' contains 's' and not entering.
6834 * 'S' BCO_ENTER initialized 's' should_copy
6835 * yes yes X X TRUE
6836 * yes no yes X FALSE
6837 * no X yes X FALSE
6838 * X no no yes FALSE
6839 * X no no no TRUE
6840 * no yes no X TRUE
6841 */
6842 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
6843 && (buf->b_p_initialized
6844 || (!(flags & BCO_ENTER)
6845 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
6846 should_copy = FALSE;
6847
6848 if (should_copy || (flags & BCO_ALWAYS))
6849 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006850#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02006851 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006852 init_buf_opt_idx();
6853#endif
6854 // Don't copy the options specific to a help buffer when
6855 // BCO_NOHELP is given or the options were initialized already
6856 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
6858 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006859 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
6861 save_p_isk = buf->b_p_isk;
6862 buf->b_p_isk = NULL;
6863 }
6864 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02006865 * Always free the allocated strings. If not already initialized,
6866 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 */
6868 if (!buf->b_p_initialized)
6869 {
6870 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006871 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02006874 switch (*p_ffs)
6875 {
6876 case 'm':
6877 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
6878 case 'd':
6879 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
6880 case 'u':
6881 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
6882 default:
6883 buf->b_p_ff = vim_strsave(p_ff);
6884 }
6885 if (buf->b_p_ff != NULL)
6886 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 buf->b_p_bh = empty_option;
6888 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890 else
6891 free_buf_options(buf, FALSE);
6892
6893 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006894 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 buf->b_p_ai_nopaste = p_ai_nopaste;
6896 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006897 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006899 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 buf->b_p_tw_nopaste = p_tw_nopaste;
6901 buf->b_p_tw_nobin = p_tw_nobin;
6902 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006903 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 buf->b_p_wm_nopaste = p_wm_nopaste;
6905 buf->b_p_wm_nobin = p_wm_nobin;
6906 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006907 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00006908 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006909 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02006910 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006911 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006913 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006915 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006917 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 buf->b_p_ml_nobin = p_ml_nobin;
6919 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006920 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02006921 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006922 buf->b_p_swf = FALSE;
6923 else
6924 {
6925 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00006926 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006929 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02006930#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02006931 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006932 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006934#ifdef FEAT_COMPL_FUNC
6935 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006936 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006937 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00006938 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006939 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006940 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006941#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006942#ifdef FEAT_EVAL
6943 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006944 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00006945 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02006946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006948 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006950#ifdef FEAT_VARTABS
6951 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006952 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006953 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006954 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006955 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006956 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006957 buf->b_p_vsts_nopaste = p_vsts_nopaste
6958 ? vim_strsave(p_vsts_nopaste) : NULL;
6959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006961 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006963 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964#ifdef FEAT_FOLDING
6965 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006966 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967#endif
6968 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006969 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006970 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006971 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006972 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6973 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006975 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006977 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006979 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006981 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006982
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006984 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006986 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006988 COPY_OPT_SCTX(buf, BV_CINO);
Tom Praschan3506cf32022-04-07 12:39:08 +01006989 buf->b_p_cinsd = vim_strsave(p_cinsd);
6990 COPY_OPT_SCTX(buf, BV_CINSD);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01006991 buf->b_p_lop = vim_strsave(p_lop);
6992 COPY_OPT_SCTX(buf, BV_LOP);
Bram Moolenaar8e145b82022-05-21 20:17:31 +01006993
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006994 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006997 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006999 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007001 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007005 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007006 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01007007 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007008#endif
7009#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02007010 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007011 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007012 (void)compile_cap_prog(&buf->b_s);
7013 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007014 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007015 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007016 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02007017 buf->b_s.b_p_spo = vim_strsave(p_spo);
7018 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01007020#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007022 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007024 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007026 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007027#if defined(FEAT_EVAL)
7028 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007029 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031#ifdef FEAT_CRYPT
7032 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007033 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007036 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#ifdef FEAT_KEYMAP
7038 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007039 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 buf->b_kmap_state |= KEYMAP_INIT;
7041#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007042#ifdef FEAT_TERMINAL
7043 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007044 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007045#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007046 // This isn't really an option, but copying the langmap and IME
7047 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007049 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007051 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007053 // options that are normally global but also have a local value
7054 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007056 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007057 buf->b_p_bkc = empty_option;
7058 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059#ifdef FEAT_QUICKFIX
7060 buf->b_p_gp = empty_option;
7061 buf->b_p_mp = empty_option;
7062 buf->b_p_efm = empty_option;
7063#endif
7064 buf->b_p_ep = empty_option;
7065 buf->b_p_kp = empty_option;
7066 buf->b_p_path = empty_option;
7067 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007068 buf->b_p_tc = empty_option;
7069 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070#ifdef FEAT_FIND_ID
7071 buf->b_p_def = empty_option;
7072 buf->b_p_inc = empty_option;
7073# ifdef FEAT_EVAL
7074 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007075 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076# endif
7077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 buf->b_p_dict = empty_option;
7079 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01007080#ifdef FEAT_COMPL_FUNC
7081 buf->b_p_tsrfu = empty_option;
7082#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007083 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007084 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007085#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7086 buf->b_p_bexpr = empty_option;
7087#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007088#if defined(FEAT_CRYPT)
7089 buf->b_p_cm = empty_option;
7090#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007091#ifdef FEAT_PERSISTENT_UNDO
7092 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007093 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007094#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007095 buf->b_p_lw = empty_option;
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007096 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098 /*
7099 * Don't copy the options set by ex_help(), use the saved values,
7100 * when going from a help buffer to a non-help buffer.
7101 * Don't touch these at all when BCO_NOHELP is used and going from
7102 * or to a help buffer.
7103 */
7104 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007107#ifdef FEAT_VARTABS
7108 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007109 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007110 else
7111 buf->b_p_vts_array = NULL;
7112#endif
7113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 else
7115 {
7116 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007117 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 did_isk = TRUE;
7119 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00007120 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007121#ifdef FEAT_VARTABS
7122 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007123 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007124 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007125 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007126 else
7127 buf->b_p_vts_array = NULL;
7128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 if (buf->b_p_bt[0] == 'h')
7131 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02007133 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 }
7135 }
7136
7137 /*
7138 * When the options should be copied (ignoring BCO_ALWAYS), set the
7139 * flag that indicates that the options have been initialized.
7140 */
7141 if (should_copy)
7142 buf->b_p_initialized = TRUE;
7143 }
7144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007145 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (did_isk)
7147 (void)buf_init_chartab(buf, FALSE);
7148}
7149
7150/*
7151 * Reset the 'modifiable' option and its default value.
7152 */
7153 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007154reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156 int opt_idx;
7157
7158 curbuf->b_p_ma = FALSE;
7159 p_ma = FALSE;
7160 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007161 if (opt_idx >= 0)
7162 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
7165/*
7166 * Set the global value for 'iminsert' to the local value.
7167 */
7168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007169set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 p_iminsert = curbuf->b_p_iminsert;
7172}
7173
7174/*
7175 * Set the global value for 'imsearch' to the local value.
7176 */
7177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007178set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 p_imsearch = curbuf->b_p_imsearch;
7181}
7182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183static int expand_option_idx = -1;
7184static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
7185static int expand_option_flags = 0;
7186
7187 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007188set_context_in_set_cmd(
7189 expand_T *xp,
7190 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007191 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007194 long_u flags = 0; // init for GCC
7195 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 char_u *p;
7197 char_u *s;
7198 int is_term_option = FALSE;
7199 int key;
7200
7201 expand_option_flags = opt_flags;
7202
7203 xp->xp_context = EXPAND_SETTINGS;
7204 if (*arg == NUL)
7205 {
7206 xp->xp_pattern = arg;
7207 return;
7208 }
7209 p = arg + STRLEN(arg) - 1;
7210 if (*p == ' ' && *(p - 1) != '\\')
7211 {
7212 xp->xp_pattern = p + 1;
7213 return;
7214 }
7215 while (p > arg)
7216 {
7217 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (*p == ' ' || *p == ',')
7220 {
7221 while (s > arg && *(s - 1) == '\\')
7222 --s;
7223 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (*p == ' ' && ((p - s) & 1) == 0)
7226 {
7227 ++p;
7228 break;
7229 }
7230 --p;
7231 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00007232 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
7234 xp->xp_context = EXPAND_BOOL_SETTINGS;
7235 p += 2;
7236 }
7237 if (STRNCMP(p, "inv", 3) == 0)
7238 {
7239 xp->xp_context = EXPAND_BOOL_SETTINGS;
7240 p += 3;
7241 }
7242 xp->xp_pattern = arg = p;
7243 if (*arg == '<')
7244 {
7245 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007246 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 return;
7248 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007249 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
7251 xp->xp_context = EXPAND_NOTHING;
7252 return;
7253 }
7254 nextchar = *++p;
7255 is_term_option = TRUE;
7256 expand_option_name[2] = KEY2TERMCAP0(key);
7257 expand_option_name[3] = KEY2TERMCAP1(key);
7258 }
7259 else
7260 {
7261 if (p[0] == 't' && p[1] == '_')
7262 {
7263 p += 2;
7264 if (*p != NUL)
7265 ++p;
7266 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007267 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 nextchar = *++p;
7269 is_term_option = TRUE;
7270 expand_option_name[2] = p[-2];
7271 expand_option_name[3] = p[-1];
7272 }
7273 else
7274 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007275 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
7277 p++;
7278 if (*p == NUL)
7279 return;
7280 nextchar = *p;
7281 *p = NUL;
7282 opt_idx = findoption(arg);
7283 *p = nextchar;
7284 if (opt_idx == -1 || options[opt_idx].var == NULL)
7285 {
7286 xp->xp_context = EXPAND_NOTHING;
7287 return;
7288 }
7289 flags = options[opt_idx].flags;
7290 if (flags & P_BOOL)
7291 {
7292 xp->xp_context = EXPAND_NOTHING;
7293 return;
7294 }
7295 }
7296 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007297 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
7299 {
7300 ++p;
7301 nextchar = '=';
7302 }
7303 if ((nextchar != '=' && nextchar != ':')
7304 || xp->xp_context == EXPAND_BOOL_SETTINGS)
7305 {
7306 xp->xp_context = EXPAND_UNSUCCESSFUL;
7307 return;
7308 }
7309 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
7310 {
7311 xp->xp_context = EXPAND_OLD_SETTING;
7312 if (is_term_option)
7313 expand_option_idx = -1;
7314 else
7315 expand_option_idx = opt_idx;
7316 xp->xp_pattern = p + 1;
7317 return;
7318 }
7319 xp->xp_context = EXPAND_NOTHING;
7320 if (is_term_option || (flags & P_NUM))
7321 return;
7322
7323 xp->xp_pattern = p + 1;
7324
7325 if (flags & P_EXPAND)
7326 {
7327 p = options[opt_idx].var;
7328 if (p == (char_u *)&p_bdir
7329 || p == (char_u *)&p_dir
7330 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01007331 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 || p == (char_u *)&p_rtp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 || p == (char_u *)&p_cdpath
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334#ifdef FEAT_SESSION
7335 || p == (char_u *)&p_vdir
7336#endif
7337 )
7338 {
7339 xp->xp_context = EXPAND_DIRECTORIES;
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01007340 if (p == (char_u *)&p_path || p == (char_u *)&p_cdpath)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 xp->xp_backslash = XP_BS_THREE;
7342 else
7343 xp->xp_backslash = XP_BS_ONE;
7344 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01007345 else if (p == (char_u *)&p_ft)
7346 {
7347 xp->xp_context = EXPAND_FILETYPE;
7348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 else
7350 {
7351 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007352 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 if (p == (char_u *)&p_tags)
7354 xp->xp_backslash = XP_BS_THREE;
7355 else
7356 xp->xp_backslash = XP_BS_ONE;
7357 }
7358 }
7359
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007360 // For an option that is a list of file names, find the start of the
7361 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
7363 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (*p == ' ' || *p == ',')
7366 {
7367 s = p;
7368 while (s > xp->xp_pattern && *(s - 1) == '\\')
7369 --s;
7370 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
7371 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
7372 {
7373 xp->xp_pattern = p + 1;
7374 break;
7375 }
7376 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007377
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007378#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007379 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007380 if (options[opt_idx].var == (char_u *)&p_sps
7381 && STRNCMP(p, "file:", 5) == 0)
7382 {
7383 xp->xp_pattern = p + 5;
7384 break;
7385 }
7386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388}
7389
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007390/*
7391 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
7392 *
7393 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
7394 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
7395 *
7396 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
7397 * regular expression 'regmatch', then stores the match in matches[idx] and
7398 * returns TRUE.
7399 *
7400 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7401 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
7402 *
7403 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
7404 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
7405 */
7406 static int
7407match_str(
7408 char_u *str,
7409 regmatch_T *regmatch,
7410 char_u **matches,
7411 int idx,
7412 int test_only,
7413 int fuzzy,
7414 char_u *fuzzystr,
7415 fuzmatch_str_T *fuzmatch)
7416{
7417 if (!fuzzy)
7418 {
7419 if (vim_regexec(regmatch, str, (colnr_T)0))
7420 {
7421 if (!test_only)
7422 matches[idx] = vim_strsave(str);
7423 return TRUE;
7424 }
7425 }
7426 else
7427 {
7428 int score;
7429
7430 score = fuzzy_match_str(str, fuzzystr);
7431 if (score != 0)
7432 {
7433 if (!test_only)
7434 {
7435 fuzmatch[idx].idx = idx;
7436 fuzmatch[idx].str = vim_strsave(str);
7437 fuzmatch[idx].score = score;
7438 }
7439
7440 return TRUE;
7441 }
7442 }
7443
7444 return FALSE;
7445}
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007448ExpandSettings(
7449 expand_T *xp,
7450 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007451 char_u *fuzzystr,
7452 int *numMatches,
Christian Brabandtcb747892022-05-08 21:10:56 +01007453 char_u ***matches,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007454 int can_fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007456 int num_normal = 0; // Nr of matching non-term-code settings
7457 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 int opt_idx;
7459 int match;
7460 int count = 0;
7461 char_u *str;
7462 int loop;
7463 int is_term_opt;
7464 char_u name_buf[MAX_KEY_NAME_LEN];
7465 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007466 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007467 int fuzzy;
7468 fuzmatch_str_T *fuzmatch = NULL;
7469
Christian Brabandtcb747892022-05-08 21:10:56 +01007470 fuzzy = can_fuzzy && cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007472 // do this loop twice:
7473 // loop == 0: count the number of matching options
7474 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 for (loop = 0; loop <= 1; ++loop)
7476 {
7477 regmatch->rm_ic = ic;
7478 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
7479 {
K.Takataeeec2542021-06-02 13:28:16 +02007480 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007481 {
7482 if (match_str((char_u *)names[match], regmatch, *matches,
7483 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
7485 if (loop == 0)
7486 num_normal++;
7487 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007488 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
7492 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
7493 opt_idx++)
7494 {
7495 if (options[opt_idx].var == NULL)
7496 continue;
7497 if (xp->xp_context == EXPAND_BOOL_SETTINGS
7498 && !(options[opt_idx].flags & P_BOOL))
7499 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02007500 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 if (is_term_opt && num_normal > 0)
7502 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007503
7504 if (match_str(str, regmatch, *matches, count, (loop == 0),
7505 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 {
7507 if (loop == 0)
7508 {
7509 if (is_term_opt)
7510 num_term++;
7511 else
7512 num_normal++;
7513 }
7514 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007515 count++;
7516 }
7517 else if (!fuzzy && options[opt_idx].shortname != NULL
7518 && vim_regexec(regmatch,
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007519 (char_u *)options[opt_idx].shortname, (colnr_T)0))
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007520 {
7521 // Compare against the abbreviated option name (for regular
7522 // expression match). Fuzzy matching (previous if) already
7523 // matches against both the expanded and abbreviated names.
7524 if (loop == 0)
7525 {
7526 if (is_term_opt)
7527 num_term++;
7528 else
7529 num_normal++;
7530 }
7531 else
7532 (*matches)[count++] = vim_strsave(str);
7533 }
7534 else if (is_term_opt)
7535 {
7536 name_buf[0] = '<';
7537 name_buf[1] = 't';
7538 name_buf[2] = '_';
7539 name_buf[3] = str[2];
7540 name_buf[4] = str[3];
7541 name_buf[5] = '>';
7542 name_buf[6] = NUL;
7543
7544 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7545 fuzzy, fuzzystr, fuzmatch))
7546 {
7547 if (loop == 0)
7548 num_term++;
7549 else
7550 count++;
7551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007554
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 /*
7556 * Check terminal key codes, these are not in the option table
7557 */
7558 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
7559 {
7560 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
7561 {
7562 if (!isprint(str[0]) || !isprint(str[1]))
7563 continue;
7564
7565 name_buf[0] = 't';
7566 name_buf[1] = '_';
7567 name_buf[2] = str[0];
7568 name_buf[3] = str[1];
7569 name_buf[4] = NUL;
7570
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007571 if (match_str(name_buf, regmatch, *matches, count,
7572 (loop == 0), fuzzy, fuzzystr, fuzmatch))
7573 {
7574 if (loop == 0)
7575 num_term++;
7576 else
7577 count++;
7578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 else
7580 {
7581 name_buf[0] = '<';
7582 name_buf[1] = 't';
7583 name_buf[2] = '_';
7584 name_buf[3] = str[0];
7585 name_buf[4] = str[1];
7586 name_buf[5] = '>';
7587 name_buf[6] = NUL;
7588
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007589 if (match_str(name_buf, regmatch, *matches, count,
7590 (loop == 0), fuzzy, fuzzystr,
7591 fuzmatch))
7592 {
7593 if (loop == 0)
7594 num_term++;
7595 else
7596 count++;
7597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599 }
7600
7601 /*
7602 * Check special key names.
7603 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007604 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
7606 {
7607 name_buf[0] = '<';
7608 STRCPY(name_buf + 1, str);
7609 STRCAT(name_buf, ">");
7610
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007611 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
7612 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 {
7614 if (loop == 0)
7615 num_term++;
7616 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007617 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
7619 }
7620 }
7621 if (loop == 0)
7622 {
7623 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007624 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007626 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 else
7628 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007629 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007631 *matches = ALLOC_MULT(char_u *, *numMatches);
7632 if (*matches == NULL)
7633 {
7634 *matches = (char_u **)"";
7635 return FAIL;
7636 }
7637 }
7638 else
7639 {
7640 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
7641 if (fuzmatch == NULL)
7642 {
7643 *matches = (char_u **)"";
7644 return FAIL;
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 }
7647 }
7648 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00007649
7650 if (fuzzy &&
7651 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
7652 return FAIL;
7653
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 return OK;
7655}
7656
7657 int
zeertzjqb0d45ec2023-01-25 15:04:22 +00007658ExpandOldSetting(int *numMatches, char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007660 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 char_u *buf;
7662
zeertzjqb0d45ec2023-01-25 15:04:22 +00007663 *numMatches = 0;
7664 *matches = ALLOC_ONE(char_u *);
7665 if (*matches == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 return FAIL;
7667
7668 /*
7669 * For a terminal key code expand_option_idx is < 0.
7670 */
7671 if (expand_option_idx < 0)
7672 {
7673 var = find_termcode(expand_option_name + 2);
7674 if (var == NULL)
7675 expand_option_idx = findoption(expand_option_name);
7676 }
7677
7678 if (expand_option_idx >= 0)
7679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007680 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 option_value2string(&options[expand_option_idx], expand_option_flags);
7682 var = NameBuff;
7683 }
7684 else if (var == NULL)
7685 var = (char_u *)"";
7686
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007687 // A backslash is required before some characters. This is the reverse of
7688 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 buf = vim_strsave_escaped(var, escape_chars);
7690
7691 if (buf == NULL)
7692 {
zeertzjqb0d45ec2023-01-25 15:04:22 +00007693 VIM_CLEAR(*matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 return FAIL;
7695 }
7696
7697#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007698 // For MS-Windows et al. we don't double backslashes at the start and
7699 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007700 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (var[0] == '\\' && var[1] == '\\'
7702 && expand_option_idx >= 0
7703 && (options[expand_option_idx].flags & P_EXPAND)
7704 && vim_isfilec(var[2])
7705 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00007706 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707#endif
7708
zeertzjqb0d45ec2023-01-25 15:04:22 +00007709 *matches[0] = buf;
7710 *numMatches = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 return OK;
7712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
7714/*
7715 * Get the value for the numeric or string option *opp in a nice format into
7716 * NameBuff[]. Must not be called with a hidden option!
7717 */
7718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007719option_value2string(
7720 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007721 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
7723 char_u *varp;
7724
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007725 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
7727 if (opp->flags & P_NUM)
7728 {
7729 long wc = 0;
7730
7731 if (wc_use_keyname(varp, &wc))
7732 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
7733 else if (wc != 0)
7734 STRCPY(NameBuff, transchar((int)wc));
7735 else
7736 sprintf((char *)NameBuff, "%ld", *(long *)varp);
7737 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007738 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
7740 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007741 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 NameBuff[0] = NUL;
7743#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007744 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007745 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 STRCPY(NameBuff, "*****");
7747#endif
7748 else if (opp->flags & P_EXPAND)
7749 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007750 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 else if ((char_u **)opp->var == &p_pt)
7752 str2specialbuf(p_pt, NameBuff, MAXPATHL);
7753 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007754 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756}
7757
7758/*
7759 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
7760 * printed as a keyname.
7761 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
7762 */
7763 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007764wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
7767 {
7768 *wcp = *(long *)varp;
7769 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
7770 return TRUE;
7771 }
7772 return FALSE;
7773}
7774
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 * Return TRUE if "x" is present in 'shortmess' option, or
7777 * 'shortmess' contains 'a' and "x" is present in SHM_A.
7778 */
7779 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007780shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01007782 return p_shm != NULL &&
7783 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 || (vim_strchr(p_shm, 'a') != NULL
7785 && vim_strchr((char_u *)SHM_A, x) != NULL));
7786}
7787
7788/*
7789 * paste_option_changed() - Called after p_paste was set or reset.
7790 */
7791 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007792paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 static int old_p_paste = FALSE;
7795 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007796 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 static int save_ru = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798#ifdef FEAT_RIGHTLEFT
7799 static int save_ri = 0;
7800 static int save_hkmap = 0;
7801#endif
7802 buf_T *buf;
7803
7804 if (p_paste)
7805 {
7806 /*
7807 * Paste switched from off to on.
7808 * Save the current values, so they can be restored later.
7809 */
7810 if (!old_p_paste)
7811 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007812 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007813 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {
7815 buf->b_p_tw_nopaste = buf->b_p_tw;
7816 buf->b_p_wm_nopaste = buf->b_p_wm;
7817 buf->b_p_sts_nopaste = buf->b_p_sts;
7818 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007819 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007820#ifdef FEAT_VARTABS
7821 if (buf->b_p_vsts_nopaste)
7822 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007823 buf->b_p_vsts_nopaste =
7824 buf->b_p_vsts && buf->b_p_vsts != empty_option
7825 ? vim_strsave(buf->b_p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 }
7828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007829 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007831 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 save_ru = p_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833#ifdef FEAT_RIGHTLEFT
7834 save_ri = p_ri;
7835 save_hkmap = p_hkmap;
7836#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007837 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007838 p_ai_nopaste = p_ai;
7839 p_et_nopaste = p_et;
7840 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 p_tw_nopaste = p_tw;
7842 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007843#ifdef FEAT_VARTABS
7844 if (p_vsts_nopaste)
7845 vim_free(p_vsts_nopaste);
Bram Moolenaar9af2ea82022-11-22 18:18:38 +00007846 p_vsts_nopaste = p_vsts && p_vsts != empty_option
7847 ? vim_strsave(p_vsts) : NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850
7851 /*
7852 * Always set the option values, also when 'paste' is set when it is
7853 * already on.
7854 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007855 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007856 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007858 buf->b_p_tw = 0; // textwidth is 0
7859 buf->b_p_wm = 0; // wrapmargin is 0
7860 buf->b_p_sts = 0; // softtabstop is 0
7861 buf->b_p_ai = 0; // no auto-indent
7862 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007863#ifdef FEAT_VARTABS
7864 if (buf->b_p_vsts)
7865 free_string_option(buf->b_p_vsts);
7866 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007867 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007871 // set global options
7872 p_sm = 0; // no showmatch
7873 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007875 status_redraw_all(); // redraw to remove the ruler
7876 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007878 p_ri = 0; // no reverse insert
7879 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007881 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 p_tw = 0;
7883 p_wm = 0;
7884 p_sts = 0;
7885 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007886#ifdef FEAT_VARTABS
7887 if (p_vsts)
7888 free_string_option(p_vsts);
7889 p_vsts = empty_option;
7890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892
7893 /*
7894 * Paste switched from on to off: Restore saved values.
7895 */
7896 else if (old_p_paste)
7897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007898 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02007899 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
7901 buf->b_p_tw = buf->b_p_tw_nopaste;
7902 buf->b_p_wm = buf->b_p_wm_nopaste;
7903 buf->b_p_sts = buf->b_p_sts_nopaste;
7904 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007905 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007906#ifdef FEAT_VARTABS
7907 if (buf->b_p_vsts)
7908 free_string_option(buf->b_p_vsts);
7909 buf->b_p_vsts = buf->b_p_vsts_nopaste
7910 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00007911 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007912 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02007913 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007914 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00007915 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 }
7918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007919 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007921 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007923 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 p_ru = save_ru;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925#ifdef FEAT_RIGHTLEFT
7926 p_ri = save_ri;
7927 p_hkmap = save_hkmap;
7928#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007929 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02007930 p_ai = p_ai_nopaste;
7931 p_et = p_et_nopaste;
7932 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 p_tw = p_tw_nopaste;
7934 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007935#ifdef FEAT_VARTABS
7936 if (p_vsts)
7937 free_string_option(p_vsts);
7938 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
7939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 }
7941
7942 old_p_paste = p_paste;
7943}
7944
7945/*
7946 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7947 *
7948 * Reset 'compatible' and set the values for options that didn't get set yet
7949 * to the Vim defaults.
7950 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007951 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 */
7953 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007954vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007956 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007957 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007958 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959
7960 if (!option_was_set((char_u *)"cp"))
7961 {
7962 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007963 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7965 set_option_default(opt_idx, OPT_FREE, FALSE);
7966 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007967 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007969
7970 if (fname != NULL)
7971 {
7972 p = vim_getenv(envname, &dofree);
7973 if (p == NULL)
7974 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007975 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007976 p = FullName_save(fname, FALSE);
7977 if (p != NULL)
7978 {
7979 vim_setenv(envname, p);
7980 vim_free(p);
7981 }
7982 }
7983 else if (dofree)
7984 vim_free(p);
7985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986}
7987
7988/*
7989 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7990 */
7991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007992change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007994 int opt_idx;
7995
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 if (p_cp != on)
7997 {
7998 p_cp = on;
7999 compatible_set();
8000 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008001 opt_idx = findoption((char_u *)"cp");
8002 if (opt_idx >= 0)
8003 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004}
8005
8006/*
8007 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02008008 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 */
8010 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008011option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
8013 int idx;
8014
8015 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01008016 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 return FALSE;
8018 if (options[idx].flags & P_WAS_SET)
8019 return TRUE;
8020 return FALSE;
8021}
8022
8023/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008024 * Reset the flag indicating option "name" was set.
8025 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02008026 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008027reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008028{
8029 int idx = findoption(name);
8030
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00008031 if (idx < 0)
8032 return FAIL;
8033
8034 options[idx].flags &= ~P_WAS_SET;
8035 return OK;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01008036}
8037
8038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 * compatible_set() - Called when 'compatible' has been set or unset.
8040 *
8041 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
8042 * flag) to a Vi compatible value.
8043 * When 'compatible' is unset: Set all options that have a different default
8044 * for Vim (without the P_VI_DEF flag) to that default.
8045 */
8046 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008047compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
8049 int opt_idx;
8050
Bram Moolenaardac13472019-09-16 21:06:21 +02008051 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
8053 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
8054 set_option_default(opt_idx, OPT_FREE, p_cp);
8055 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008056 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057}
8058
Bram Moolenaardac13472019-09-16 21:06:21 +02008059#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061/*
8062 * fill_breakat_flags() -- called when 'breakat' changes value.
8063 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008064 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008065fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008067 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 int i;
8069
8070 for (i = 0; i < 256; i++)
8071 breakat_flags[i] = FALSE;
8072
8073 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008074 for (p = p_breakat; *p; p++)
8075 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077#endif
8078
8079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 * Check if backspacing over something is allowed.
8081 */
8082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008083can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008084 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02008086#ifdef FEAT_JOB_CHANNEL
8087 if (what == BS_START && bt_prompt(curbuf))
8088 return FALSE;
8089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 switch (*p_bs)
8091 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02008092 case '3': return TRUE;
8093 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 case '1': return (what != BS_START);
8095 case '0': return FALSE;
8096 }
8097 return vim_strchr(p_bs, what) != NULL;
8098}
8099
8100/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01008101 * Return the effective 'scrolloff' value for the current window, using the
8102 * global value when appropriate.
8103 */
8104 long
8105get_scrolloff_value(void)
8106{
8107 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
8108}
8109
8110/*
8111 * Return the effective 'sidescrolloff' value for the current window, using the
8112 * global value when appropriate.
8113 */
8114 long
8115get_sidescrolloff_value(void)
8116{
8117 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
8118}
8119
8120/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008121 * Get the local or global value of 'backupcopy'.
8122 */
8123 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008124get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008125{
8126 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
8127}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02008128
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008129#if defined(FEAT_LINEBREAK) || defined(PROTO)
Gary Johnson53ba05b2021-07-26 22:19:10 +02008130/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00008131 * Get the local or global value of 'formatlistpat'.
8132 */
8133 char_u *
8134get_flp_value(buf_T *buf)
8135{
Christian Brabandtc53b4672022-01-15 10:01:05 +00008136 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
8137 return p_flp;
8138 return buf->b_p_flp;
8139}
Dominique Pelle7765f5c2022-04-10 11:26:53 +01008140#endif
Christian Brabandtc53b4672022-01-15 10:01:05 +00008141
8142/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02008143 * Get the local or global value of the 'virtualedit' flags.
8144 */
8145 unsigned int
8146get_ve_flags(void)
8147{
Gary Johnson51ad8502021-08-03 18:33:08 +02008148 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
8149 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02008150}
8151
Bram Moolenaaree857022019-11-09 23:26:40 +01008152#if defined(FEAT_LINEBREAK) || defined(PROTO)
8153/*
8154 * Get the local or global value of 'showbreak'.
8155 */
8156 char_u *
8157get_showbreak_value(win_T *win)
8158{
8159 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
8160 return p_sbr;
8161 if (STRCMP(win->w_p_sbr, "NONE") == 0)
8162 return empty_option;
8163 return win->w_p_sbr;
8164}
8165#endif
8166
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008167#if defined(FEAT_EVAL) || defined(PROTO)
8168/*
8169 * Get window or buffer local options.
8170 */
8171 dict_T *
8172get_winbuf_options(int bufopt)
8173{
8174 dict_T *d;
8175 int opt_idx;
8176
8177 d = dict_alloc();
8178 if (d == NULL)
8179 return NULL;
8180
Bram Moolenaardac13472019-09-16 21:06:21 +02008181 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008182 {
8183 struct vimoption *opt = &options[opt_idx];
8184
8185 if ((bufopt && (opt->indir & PV_BUF))
8186 || (!bufopt && (opt->indir & PV_WIN)))
8187 {
8188 char_u *varp = get_varp(opt);
8189
8190 if (varp != NULL)
8191 {
8192 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008193 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02008194 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02008195 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008196 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02008197 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02008198 }
8199 }
8200 }
8201
8202 return d;
8203}
8204#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02008205
Bram Moolenaardac13472019-09-16 21:06:21 +02008206#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02008207/*
8208 * This is called when 'culopt' is changed
8209 */
Bram Moolenaardac13472019-09-16 21:06:21 +02008210 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02008211fill_culopt_flags(char_u *val, win_T *wp)
8212{
8213 char_u *p;
8214 char_u culopt_flags_new = 0;
8215
8216 if (val == NULL)
8217 p = wp->w_p_culopt;
8218 else
8219 p = val;
8220 while (*p != NUL)
8221 {
8222 if (STRNCMP(p, "line", 4) == 0)
8223 {
8224 p += 4;
8225 culopt_flags_new |= CULOPT_LINE;
8226 }
8227 else if (STRNCMP(p, "both", 4) == 0)
8228 {
8229 p += 4;
8230 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
8231 }
8232 else if (STRNCMP(p, "number", 6) == 0)
8233 {
8234 p += 6;
8235 culopt_flags_new |= CULOPT_NBR;
8236 }
8237 else if (STRNCMP(p, "screenline", 10) == 0)
8238 {
8239 p += 10;
8240 culopt_flags_new |= CULOPT_SCRLINE;
8241 }
8242
8243 if (*p != ',' && *p != NUL)
8244 return FAIL;
8245 if (*p == ',')
8246 ++p;
8247 }
8248
8249 // Can't have both "line" and "screenline".
8250 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
8251 return FAIL;
8252 wp->w_p_culopt_flags = culopt_flags_new;
8253
8254 return OK;
8255}
8256#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008257
8258/*
8259 * Get the value of 'magic' adjusted for Vim9 script.
8260 */
8261 int
8262magic_isset(void)
8263{
8264 switch (magic_overruled)
8265 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01008266 case OPTION_MAGIC_ON: return TRUE;
8267 case OPTION_MAGIC_OFF: return FALSE;
8268 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008269 }
8270#ifdef FEAT_EVAL
8271 if (in_vim9script())
8272 return TRUE;
8273#endif
8274 return p_magic;
8275}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008276
8277/*
8278 * Set the callback function value for an option that accepts a function name,
8279 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
8280 * Returns OK if the option is successfully set to a function, otherwise
8281 * returns FAIL.
8282 */
8283 int
8284option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
8285{
8286#ifdef FEAT_EVAL
8287 typval_T *tv;
8288 callback_T cb;
8289
8290 if (optval == NULL || *optval == NUL)
8291 {
8292 free_callback(optcb);
8293 return OK;
8294 }
8295
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00008296 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008297 || (STRNCMP(optval, "function(", 9) == 0)
8298 || (STRNCMP(optval, "funcref(", 8) == 0))
8299 // Lambda expression or a funcref
8300 tv = eval_expr(optval, NULL);
8301 else
8302 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00008303 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008304 if (tv == NULL)
8305 return FAIL;
8306
8307 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00008308 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008309 {
8310 free_tv(tv);
8311 return FAIL;
8312 }
8313
8314 free_callback(optcb);
8315 set_callback(optcb, &cb);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00008316 if (cb.cb_free_name)
8317 vim_free(cb.cb_name);
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008318 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00008319
8320 // when using Vim9 style "import.funcname" it needs to be expanded to
8321 // "import#funcname".
8322 expand_autload_callback(optcb);
8323
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00008324 return OK;
8325#else
8326 return FAIL;
8327#endif
8328}