blob: 71b71ba53c5f99ee357624fc68a64e9b860f5794 [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:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static 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 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000059static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000062static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010063static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200148 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef HAVE_AVAIL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#else
205# ifdef HAVE_TOTAL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100206 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000207 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_SEARCHPATH
226 {
227 char_u *cdpath;
228 char_u *buf;
229 int i;
230 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000231 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100233 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000234 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (cdpath != NULL)
236 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200237 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (buf != NULL)
239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100240 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 j = 1;
242 for (i = 0; cdpath[i] != NUL; ++i)
243 {
244 if (vim_ispathlistsep(cdpath[i]))
245 buf[j++] = ',';
246 else
247 {
248 if (cdpath[i] == ' ' || cdpath[i] == ',')
249 buf[j++] = '\\';
250 buf[j++] = cdpath[i];
251 }
252 }
253 buf[j] = NUL;
254 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000255 if (opt_idx >= 0)
256 {
257 options[opt_idx].def_val[VI_DEFAULT] = buf;
258 options[opt_idx].flags |= P_DEF_ALLOCED;
259 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200260 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100261 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000263 if (mustfree)
264 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
267#endif
268
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100270 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100272# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 (char_u *)"cp1252"
274# else
275# ifdef VMS
276 (char_u *)"dec-mcs"
277# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000278# ifdef MAC
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 (char_u *)"mac-roman"
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000280# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# endif
283# endif
284# endif
285 );
286#endif
287
288#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100291# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000292 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293# else
294# ifdef VMS
295 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
296
297# else
298 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
299# endif
300# endif
301 );
302#endif
303
304 /*
305 * Set all the options (except the terminal options) to their default
306 * value. Also set the global value for local options.
307 */
308 set_options_default(0);
309
matveytadbb1bf2022-02-01 17:26:12 +0000310#ifdef UNIX
311 // Force restricted-mode on for "nologin" or "false" $SHELL
312 p = get_isolated_shell_name();
313 if (p != NULL)
314 {
315 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
316 restricted = TRUE;
317 vim_free(p);
318 }
319#endif
320
Bram Moolenaar07268702018-03-01 21:57:32 +0100321#ifdef CLEAN_RUNTIMEPATH
322 if (clean_arg)
323 {
324 opt_idx = findoption((char_u *)"runtimepath");
325 if (opt_idx >= 0)
326 {
327 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
328 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
329 }
330 opt_idx = findoption((char_u *)"packpath");
331 if (opt_idx >= 0)
332 {
333 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
334 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
335 }
336 }
337#endif
338
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#ifdef FEAT_GUI
340 if (found_reverse_arg)
341 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
342#endif
343
344 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100345 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100346 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 check_buf_options(curbuf);
348 check_win_options(curwin);
349 check_options();
350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100351 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 didset_options();
353
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000354#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100355 // Use the current chartab for the generic chartab. This is not in
356 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000357 init_spell_chartab();
358#endif
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 /*
361 * Expand environment variables and things like "~" for the defaults.
362 * If option_expand() returns non-NULL the variable is expanded. This can
363 * only happen for non-indirect options.
364 * Also set the default to the expanded value, so ":set" does not list
365 * them.
366 * Don't set the P_ALLOCED flag, because we don't want to free the
367 * default.
368 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200369 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
371 if ((options[opt_idx].flags & P_GETTEXT)
372 && options[opt_idx].var != NULL)
373 p = (char_u *)_(*(char **)options[opt_idx].var);
374 else
375 p = option_expand(opt_idx, NULL);
376 if (p != NULL && (p = vim_strsave(p)) != NULL)
377 {
378 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379 // VIMEXP
380 // Defaults for all expanded options are currently the same for Vi
381 // and Vim. When this changes, add some code here! Also need to
382 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 if (options[opt_idx].flags & P_DEF_ALLOCED)
384 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
385 options[opt_idx].def_val[VI_DEFAULT] = p;
386 options[opt_idx].flags |= P_DEF_ALLOCED;
387 }
388 }
389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100390 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100393 // Detect use of mlterm.
394 // Mlterm is a terminal emulator akin to xterm that has some special
395 // abilities (bidi namely).
396 // NOTE: mlterm's author is being asked to 'set' a variable
397 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (mch_getenv((char_u *)"MLTERM") != NULL)
399 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
400#endif
401
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200402 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
Bram Moolenaar4f974752019-02-17 17:44:42 +0100404# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 /*
406 * If $LANG isn't set, try to get a good value for it. This makes the
407 * right language be used automatically. Don't do this for English.
408 */
409 if (mch_getenv((char_u *)"LANG") == NULL)
410 {
411 char buf[20];
412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
414 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
415 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
417 (LPTSTR)buf, 20);
418 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
419 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100420 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
422 STRCPY(buf, "zh_TW");
423 else if (STRNICMP(buf, "chs", 3) == 0
424 || STRNICMP(buf, "zhc", 3) == 0)
425 STRCPY(buf, "zh_CN");
426 else if (STRNICMP(buf, "jp", 2) == 0)
427 STRCPY(buf, "ja");
428 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100429 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100430 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000433# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000434# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100435 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000436 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438# endif
439
K.Takataf883d902021-05-30 18:04:19 +0200440# ifdef MSWIN
441 // MS-Windows has builtin support for conversion to and from Unicode, using
442 // "utf-8" for 'encoding' should work best for most users.
443 p = vim_strsave((char_u *)ENC_DFLT);
444# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100445 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200446 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (p != NULL)
450 {
451 char_u *save_enc;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200454 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 save_enc = p_enc;
456 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000457 if (STRCMP(p_enc, "gb18030") == 0)
458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100459 // We don't support "gb18030", but "cp936" is a good substitute
460 // for practical purposes, thus use that. It's not an alias to
461 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000462 p_enc = vim_strsave((char_u *)"cp936");
463 vim_free(p);
464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (mb_init() == NULL)
466 {
467 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000468 if (opt_idx >= 0)
469 {
470 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
471 options[opt_idx].flags |= P_DEF_ALLOCED;
472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaard0573012017-10-28 21:11:06 +0200474#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100475 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100477 // Adjust the default for 'isprint' and 'iskeyword' to match
478 // latin1. Also set the defaults for when 'nocompatible' is
479 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000480 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000481 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000482 set_string_option_direct((char_u *)"isk", -1,
483 ISK_LATIN1, OPT_FREE, SID_NONE);
484 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000485 if (opt_idx >= 0)
486 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000487 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000488 if (opt_idx >= 0)
489 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000490 (void)init_chartab();
491 }
492#endif
493
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200494#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 // Win32 console: When GetACP() returns a different value from
496 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200497 if (
498# ifdef VIMDLL
499 (!gui.in_use && !gui.starting) &&
500# endif
501 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {
503 char buf[50];
504
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100505 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
506 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100507 if (GetConsoleCP() == 0)
508 sprintf(buf, "cp%ld", (long)GetACP());
509 else
510 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 p_tenc = vim_strsave((char_u *)buf);
512 if (p_tenc != NULL)
513 {
514 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000515 if (opt_idx >= 0)
516 {
517 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
518 options[opt_idx].flags |= P_DEF_ALLOCED;
519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 convert_setup(&input_conv, p_tenc, p_enc);
521 convert_setup(&output_conv, p_enc, p_tenc);
522 }
523 else
524 p_tenc = empty_option;
525 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100526#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100527#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100528 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000529 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 }
532 else
533 {
534 vim_free(p_enc);
535 p_enc = save_enc;
536 }
537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100540 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 set_helplang_default(get_mess_lang());
542#endif
543}
544
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200545static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
546
547/*
548 * Set the "fileencodings" option to the default value for when 'encoding' is
549 * utf-8.
550 */
551 void
552set_fencs_unicode()
553{
554 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
555 OPT_FREE, 0);
556}
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558/*
559 * Set an option to its default value.
560 * This does not take care of side effects!
561 */
562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100563set_option_default(
564 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100565 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
566 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100568 char_u *varp; // pointer to variable for current option
569 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000571 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
573
574 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
575 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100576 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
578 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
579 if (flags & P_STRING)
580 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200581 // 'fencs' default value depends on 'encoding'
582 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
583 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100584 // Use set_string_option_direct() for local options to handle
585 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200586 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200587 set_string_option_direct(NULL, opt_idx,
588 options[opt_idx].def_val[dvi], opt_flags, 0);
589 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200591 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
592 free_string_option(*(char_u **)(varp));
593 *(char_u **)varp = options[opt_idx].def_val[dvi];
594 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
596 }
597 else if (flags & P_NUM)
598 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000599 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 win_comp_scroll(curwin);
601 else
602 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100603 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
604
605 if ((long *)varp == &curwin->w_p_so
606 || (long *)varp == &curwin->w_p_siso)
607 // 'scrolloff' and 'sidescrolloff' local values have a
608 // different default value than the global default.
609 *(long *)varp = -1;
610 else
611 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100612 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (both)
614 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100615 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100618 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100620 // the cast to long is required for Manx C, long_i is needed for
621 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000622 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000623#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100624 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000625 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
626 *(int *)varp = FALSE;
627#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (both)
630 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
631 *(int *)varp;
632 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100634 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000635 flagsp = insecure_flag(opt_idx, opt_flags);
636 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638
639#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200640 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#endif
642}
643
644/*
645 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200646 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 */
648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100649set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100650 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000654 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaardac13472019-09-16 21:06:21 +0200656 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200657 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200658 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100659 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200660# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200661 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200662 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200663# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100664 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 set_option_default(i, opt_flags, p_cp);
666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000668 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200670#ifdef FEAT_CINDENT
671 parse_cino(curbuf);
672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673}
674
675/*
676 * Set the Vi-default value of a string option.
677 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100678 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100680 static void
681set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 char_u *p;
684 int opt_idx;
685
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100686 if (escape && vim_strchr(val, ' ') != NULL)
687 p = vim_strsave_escaped(val, (char_u *)" ");
688 else
689 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100690 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
692 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000693 if (opt_idx >= 0)
694 {
695 if (options[opt_idx].flags & P_DEF_ALLOCED)
696 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
697 options[opt_idx].def_val[VI_DEFAULT] = p;
698 options[opt_idx].flags |= P_DEF_ALLOCED;
699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
701}
702
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100703 void
704set_string_default(char *name, char_u *val)
705{
706 set_string_default_esc(name, val, FALSE);
707}
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200710 * For an option value that contains comma separated items, find "newval" in
711 * "origval". Return NULL if not found.
712 */
713 static char_u *
714find_dup_item(char_u *origval, char_u *newval, long_u flags)
715{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200716 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200717 size_t newlen;
718 char_u *s;
719
720 if (origval == NULL)
721 return NULL;
722
723 newlen = STRLEN(newval);
724 for (s = origval; *s != NUL; ++s)
725 {
726 if ((!(flags & P_COMMA)
727 || s == origval
728 || (s[-1] == ',' && !(bs & 1)))
729 && STRNCMP(s, newval, newlen) == 0
730 && (!(flags & P_COMMA)
731 || s[newlen] == ','
732 || s[newlen] == NUL))
733 return s;
734 // Count backslashes. Only a comma with an even number of backslashes
735 // or a single backslash preceded by a comma before it is recognized as
736 // a separator.
737 if ((s > origval + 1
738 && s[-1] == '\\'
739 && s[-2] != ',')
740 || (s == origval + 1
741 && s[-1] == '\\'))
742 ++bs;
743 else
744 bs = 0;
745 }
746 return NULL;
747}
748
749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 * Set the Vi-default value of a number option.
751 * Used for 'lines' and 'columns'.
752 */
753 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100754set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000756 int opt_idx;
757
758 opt_idx = findoption((char_u *)name);
759 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000760 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761}
762
Dominique Pelle748b3082022-01-08 12:41:16 +0000763#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200764/*
765 * Set all window-local and buffer-local options to the Vim default.
766 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200767 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200768 */
769 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200770set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200771{
772 win_T *save_curwin = curwin;
773 int i;
774
775 curwin = wp;
776 curbuf = curwin->w_buffer;
777 block_autocmds();
778
Bram Moolenaardac13472019-09-16 21:06:21 +0200779 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200780 {
781 struct vimoption *p = &(options[i]);
782 char_u *varp = get_varp_scope(p, OPT_LOCAL);
783
784 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200785 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200786 && !(options[i].flags & P_NODEFAULT)
787 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200788 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200789 }
790
791 unblock_autocmds();
792 curwin = save_curwin;
793 curbuf = curwin->w_buffer;
794}
Dominique Pelle748b3082022-01-08 12:41:16 +0000795#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200796
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000797#if defined(EXITFREE) || defined(PROTO)
798/*
799 * Free all options.
800 */
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000803{
804 int i;
805
Bram Moolenaardac13472019-09-16 21:06:21 +0200806 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000807 {
808 if (options[i].indir == PV_NONE)
809 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100810 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100811 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000812 free_string_option(*(char_u **)options[i].var);
813 if (options[i].flags & P_DEF_ALLOCED)
814 free_string_option(options[i].def_val[VI_DEFAULT]);
815 }
816 else if (options[i].var != VAR_WIN
817 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200819 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000820 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000821 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000822 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000823}
824#endif
825
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827/*
828 * Initialize the options, part two: After getting Rows and Columns and
829 * setting 'term'.
830 */
831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100832set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 int idx;
835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100837 * 'scroll' defaults to half the window height. The stored default is zero,
838 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000840 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000841 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000842 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 comp_col();
844
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000845 /*
846 * 'window' is only for backwards compatibility with Vi.
847 * Default is Rows - 1.
848 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000849 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000850 p_window = Rows - 1;
851 set_number_default("window", Rows - 1);
852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100854#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000855 /*
856 * If 'background' wasn't set by the user, try guessing the value,
857 * depending on the terminal name. Only need to check for terminals
858 * with a dark background, that can handle color.
859 */
860 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000861 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
862 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000864 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 // don't mark it as set, when starting the GUI it may be
866 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000867 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000870
871#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100872 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000873#endif
874#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000876#endif
877#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880}
881
882/*
883 * Initialize the options, part three: After reading the .vimrc
884 */
885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100886set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100888#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889/*
890 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
891 * This is done after other initializations, where 'shell' might have been
892 * set, but only if they have not been set before.
893 */
894 char_u *p;
895 int idx_srr;
896 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100897# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 int idx_sp;
899 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000903 if (idx_srr < 0)
904 do_srr = FALSE;
905 else
906 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100907# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000909 if (idx_sp < 0)
910 do_sp = FALSE;
911 else
912 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100913# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200914 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (p != NULL)
916 {
917 /*
918 * Default for p_sp is "| tee", for p_srr is ">".
919 * For known shells it is changed here to include stderr.
920 */
921 if ( fnamecmp(p, "csh") == 0
922 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100923# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 || fnamecmp(p, "csh.exe") == 0
925 || fnamecmp(p, "tcsh.exe") == 0
926# endif
927 )
928 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100929# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (do_sp)
931 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100932# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100934# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
938 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (do_srr)
941 {
942 p_srr = (char_u *)">&";
943 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
944 }
945 }
Mike Williams12795022021-06-28 20:53:58 +0200946# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200947 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
948 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200949 else if ( fnamecmp(p, "powershell") == 0
950 || fnamecmp(p, "powershell.exe") == 0
951 )
952 {
953# if defined(FEAT_QUICKFIX)
954 if (do_sp)
955 {
956 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
957 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
958 }
959# endif
960 if (do_srr)
961 {
962 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
963 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
964 }
965 }
966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 else
Natanael Copa56318362021-05-06 18:46:35 +0200968 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if ( fnamecmp(p, "sh") == 0
970 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200971 || fnamecmp(p, "mksh") == 0
972 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000974 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200976 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200977 || fnamecmp(p, "ash") == 0
978 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200979 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100980# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 || fnamecmp(p, "cmd") == 0
982 || fnamecmp(p, "sh.exe") == 0
983 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200984 || fnamecmp(p, "mksh.exe") == 0
985 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000987 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 || fnamecmp(p, "bash.exe") == 0
989 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200990 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200991 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100993 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100995# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (do_sp)
997 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100998# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001000# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001001 if (fnamecmp(p, "pwsh") == 0)
1002 p_sp = (char_u *)">%s 2>&1";
1003 else
1004 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1007 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (do_srr)
1010 {
1011 p_srr = (char_u *)">%s 2>&1";
1012 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1013 }
1014 }
1015 vim_free(p);
1016 }
1017#endif
1018
Bram Moolenaar4f974752019-02-17 17:44:42 +01001019#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001021 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1022 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001024 * set, but only if they have not been set before.
1025 * Default values depend on shell (cmd.exe is default shell):
1026 *
1027 * p_shcf p_sxq
1028 * cmd.exe - "/c" "("
1029 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001030 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001031 * "sh" like shells - "-c" "\""
1032 *
1033 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 */
Mike Williams12795022021-06-28 20:53:58 +02001035 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1036 {
1037 int idx_opt;
1038
1039 idx_opt = findoption((char_u *)"shcf");
1040 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1041 {
1042 p_shcf = (char_u*)"-Command";
1043 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1044 }
1045
1046 idx_opt = findoption((char_u *)"sxq");
1047 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1048 {
1049 p_sxq = (char_u*)"\"";
1050 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1051 }
1052 }
1053 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
1055 int idx3;
1056
1057 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001058 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 p_shcf = (char_u *)"-c";
1061 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1062 }
1063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001066 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 p_sxq = (char_u *)"\"";
1069 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001072 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1073 {
1074 int idx3;
1075
1076 /*
1077 * cmd.exe on Windows will strip the first and last double quote given
1078 * on the command line, e.g. most of the time things like:
1079 * cmd /c "my path/to/echo" "my args to echo"
1080 * become:
1081 * my path/to/echo" "my args to echo
1082 * when executed.
1083 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001084 * To avoid this, set shellxquote to surround the command in
1085 * parenthesis. This appears to make most commands work, without
1086 * breaking commands that worked previously, such as
1087 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001088 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 idx3 = findoption((char_u *)"sxq");
1090 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1091 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001092 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001093 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1094 }
1095
Bram Moolenaara64ba222012-02-12 23:23:31 +01001096 idx3 = findoption((char_u *)"shcf");
1097 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1098 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001099 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001100 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1101 }
1102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103#endif
1104
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001105 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001106 {
1107 int idx_ffs = findoption((char_u *)"ffs");
1108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001109 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001110 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1111 set_fileformat(default_fileformat(), OPT_LOCAL);
1112 }
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
1117#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1118/*
1119 * When 'helplang' is still at its default value, set it to "lang".
1120 * Only the first two characters of "lang" are used.
1121 */
1122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001123set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 int idx;
1126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001127 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 return;
1129 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001130 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
1132 if (options[idx].flags & P_ALLOCED)
1133 free_string_option(p_hlg);
1134 p_hlg = vim_strsave(lang);
1135 if (p_hlg == NULL)
1136 p_hlg = empty_option;
1137 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001138 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001139 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001140 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1141 {
1142 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1143 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1144 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001145 // any C like setting, such as C.UTF-8, becomes "en"
1146 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1147 {
1148 p_hlg[0] = 'e';
1149 p_hlg[1] = 'n';
1150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 options[idx].flags |= P_ALLOCED;
1154 }
1155}
1156#endif
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158/*
1159 * 'title' and 'icon' only default to true if they have not been set or reset
1160 * in .vimrc and we can read the old value.
1161 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1162 * they can be reset. This reduces startup time when using X on a remote
1163 * machine.
1164 */
1165 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001166set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168 int idx1;
1169 long val;
1170
1171 /*
1172 * If GUI is (going to be) used, we can always set the window title and
1173 * icon name. Saves a bit of time, because the X11 display server does
1174 * not need to be contacted.
1175 */
1176 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001177 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179#ifdef FEAT_GUI
1180 if (gui.starting || gui.in_use)
1181 val = TRUE;
1182 else
1183#endif
1184 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001185 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 p_title = val;
1187 }
1188 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001189 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
1191#ifdef FEAT_GUI
1192 if (gui.starting || gui.in_use)
1193 val = TRUE;
1194 else
1195#endif
1196 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001197 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 p_icon = val;
1199 }
1200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001202 void
1203ex_set(exarg_T *eap)
1204{
1205 int flags = 0;
1206
1207 if (eap->cmdidx == CMD_setlocal)
1208 flags = OPT_LOCAL;
1209 else if (eap->cmdidx == CMD_setglobal)
1210 flags = OPT_GLOBAL;
1211#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001212 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001213 ex_options(eap);
1214 else
1215#endif
1216 {
1217 if (eap->forceit)
1218 flags |= OPT_ONECOLUMN;
1219 (void)do_set(eap->arg, flags);
1220 }
1221}
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223/*
1224 * Parse 'arg' for option settings.
1225 *
1226 * 'arg' may be IObuff, but only when no errors can be present and option
1227 * does not need to be expanded with option_expand().
1228 * "opt_flags":
1229 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001230 * OPT_GLOBAL for ":setglobal"
1231 * OPT_LOCAL for ":setlocal" and a modeline
1232 * OPT_MODELINE for a modeline
1233 * OPT_WINONLY to only set window-local options
1234 * OPT_NOWIN to skip setting window-local options
1235 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 *
1237 * returns FAIL if an error is detected, OK otherwise
1238 */
1239 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001240do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001241 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001242 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001244 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001246 char *errmsg;
1247 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001249 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1250 int nextchar; // next non-white char after option name
1251 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 int len;
1253 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001254 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001256 long_u flags; // flags for current option
1257 char_u *varp = NULL; // pointer to variable for current option
1258 int did_show = FALSE; // already showed one value
1259 int adding; // "opt+=arg"
1260 int prepending; // "opt^=arg"
1261 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int cp_val = 0;
1263 char_u key_name[2];
1264
1265 if (*arg == NUL)
1266 {
1267 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001268 did_show = TRUE;
1269 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001272 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
1274 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001277 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1278 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
1280 /*
1281 * ":set all" show all options.
1282 * ":set all&" set all options to their default value.
1283 */
1284 arg += 3;
1285 if (*arg == '&')
1286 {
1287 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001288 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001290 didset_options();
1291 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001292 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001297 did_show = TRUE;
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001300 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
1302 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001303 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001304 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 arg += 7;
1306 }
1307 else
1308 {
1309 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001310 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
1312 prefix = 0;
1313 arg += 2;
1314 }
1315 else if (STRNCMP(arg, "inv", 3) == 0)
1316 {
1317 prefix = 2;
1318 arg += 3;
1319 }
1320
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001321 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 key = 0;
1323 if (*arg == '<')
1324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001326 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1328 len = 5;
1329 else
1330 {
1331 len = 1;
1332 while (arg[len] != NUL && arg[len] != '>')
1333 ++len;
1334 }
1335 if (arg[len] != '>')
1336 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001337 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 goto skip;
1339 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001340 arg[len] = NUL; // put NUL after name
1341 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001343 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001345 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
1347 else
1348 {
1349 len = 0;
1350 /*
1351 * The two characters after "t_" may not be alphanumeric.
1352 */
1353 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1354 len = 4;
1355 else
1356 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1357 ++len;
1358 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001359 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001361 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001363 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001366 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 afterchar = arg[len];
1368
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001369 if (in_vim9script())
1370 {
1371 char_u *p = skipwhite(arg + len);
1372
1373 // disallow white space before =val, +=val, -=val, ^=val
1374 if (p > arg + len && (p[0] == '='
1375 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1376 && p[1] == '=')))
1377 {
1378 errmsg = e_no_white_space_allowed_between_option_and;
1379 arg = p;
1380 startarg = p;
1381 goto skip;
1382 }
1383 }
1384 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001385 // skip white space, allow ":set ai ?", ":set hlsearch !"
1386 while (VIM_ISWHITE(arg[len]))
1387 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 adding = FALSE;
1390 prepending = FALSE;
1391 removing = FALSE;
1392 if (arg[len] != NUL && arg[len + 1] == '=')
1393 {
1394 if (arg[len] == '+')
1395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001396 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ++len;
1398 }
1399 else if (arg[len] == '^')
1400 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 ++len;
1403 }
1404 else if (arg[len] == '-')
1405 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 ++len;
1408 }
1409 }
1410 nextchar = arg[len];
1411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001412 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001414 if (in_vim9script() && arg > arg_start
1415 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1416 errmsg = e_no_white_space_allowed_between_option_and;
1417 else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001418 errmsg = N_(e_unknown_option);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 goto skip;
1420 }
1421
1422 if (opt_idx >= 0)
1423 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001424 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001426 // Only give an error message when requesting the value of
1427 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1429 && (!(options[opt_idx].flags & P_BOOL)
1430 || nextchar == '?'))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001431 errmsg = N_(e_option_not_supported);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 goto skip;
1433 }
1434
1435 flags = options[opt_idx].flags;
1436 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1437 }
1438 else
1439 {
1440 flags = P_STRING;
1441 if (key < 0)
1442 {
1443 key_name[0] = KEY2TERMCAP0(key);
1444 key_name[1] = KEY2TERMCAP1(key);
1445 }
1446 else
1447 {
1448 key_name[0] = KS_KEY;
1449 key_name[1] = (key & 0xff);
1450 }
1451 }
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // Skip all options that are not window-local (used when showing
1454 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001455 if ((opt_flags & OPT_WINONLY)
1456 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1457 goto skip;
1458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001459 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001460 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1461 && options[opt_idx].var == VAR_WIN)
1462 goto skip;
1463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001464 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001465 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001467 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001468 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001469 errmsg = N_(e_not_allowed_in_modeline);
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001470 goto skip;
1471 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001472 if ((flags & P_MLE) && !p_mle)
1473 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001474 errmsg = N_(e_not_allowed_in_modeline_when_modelineexpr_is_off);
Bram Moolenaar110289e2019-05-23 15:38:06 +02001475 goto skip;
1476 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001477#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001478 // In diff mode some options are overruled. This avoids that
1479 // 'foldmethod' becomes "marker" instead of "diff" and that
1480 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001481 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001482 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001483 && (
1484#ifdef FEAT_FOLDING
1485 options[opt_idx].indir == PV_FDM ||
1486#endif
1487 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001488 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491
1492#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001493 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001494 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001496 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 goto skip;
1498 }
1499#endif
1500
1501 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1502 {
1503 arg += len;
1504 cp_val = p_cp;
1505 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001507 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 {
1509 cp_val = FALSE;
1510 arg += 3;
1511 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001512 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 cp_val = TRUE;
1515 arg += 2;
1516 }
1517 }
1518 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001519 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001521 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 goto skip;
1523 }
1524 }
1525
1526 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001527 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001528 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
1530 if (nextchar == '?'
1531 || (prefix == 1
1532 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1533 && !(flags & P_BOOL)))
1534 {
1535 /*
1536 * print value
1537 */
1538 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001539 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 else
1541 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001542 gotocmdline(TRUE); // cursor at status line
1543 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545 if (opt_idx >= 0)
1546 {
1547 showoneopt(&options[opt_idx], opt_flags);
1548#ifdef FEAT_EVAL
1549 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001550 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001551 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001552 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001553 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001554 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001555 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001556 (int)options[opt_idx].indir & PV_MASK]);
1557 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001558 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001559 (int)options[opt_idx].indir & PV_MASK]);
1560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#endif
1562 }
1563 else
1564 {
1565 char_u *p;
1566
1567 p = find_termcode(key_name);
1568 if (p == NULL)
1569 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001570 errmsg = N_(e_key_code_not_set);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 goto skip;
1572 }
1573 else
1574 (void)show_one_termcode(key_name, p, TRUE);
1575 }
1576 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001577 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001578 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 else
1581 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001582 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001583 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001585 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
1587 if (nextchar == '=' || nextchar == ':')
1588 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001589 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 goto skip;
1591 }
1592
1593 /*
1594 * ":set opt!": invert
1595 * ":set opt&": reset to default value
1596 * ":set opt<": reset to global value
1597 */
1598 if (nextchar == '!')
1599 value = *(int *)(varp) ^ 1;
1600 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001601 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 ((flags & P_VI_DEF) || cp_val)
1603 ? VI_DEFAULT : VIM_DEFAULT];
1604 else if (nextchar == '<')
1605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001606 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if ((int *)varp == &curbuf->b_p_ar
1608 && opt_flags == OPT_LOCAL)
1609 value = -1;
1610 else
1611 value = *(int *)get_varp_scope(&(options[opt_idx]),
1612 OPT_GLOBAL);
1613 }
1614 else
1615 {
1616 /*
1617 * ":set invopt": invert
1618 * ":set opt" or ":set noopt": set or reset
1619 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001620 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001622 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 goto skip;
1624 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001625 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 value = *(int *)(varp) ^ 1;
1627 else
1628 value = prefix;
1629 }
1630
1631 errmsg = set_bool_option(opt_idx, varp, (int)value,
1632 opt_flags);
1633 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1637 || prefix != 1)
1638 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001639 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 goto skip;
1641 }
1642
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001643 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 /*
1646 * Different ways to set a number option:
1647 * & set to default value
1648 * < set to global value
1649 * <xx> accept special key codes for 'wildchar'
1650 * c accept any non-digit for 'wildchar'
1651 * [-]0-9 set number
1652 * other error
1653 */
1654 ++arg;
1655 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001656 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 ((flags & P_VI_DEF) || cp_val)
1658 ? VI_DEFAULT : VIM_DEFAULT];
1659 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001661 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1662 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001663 if ((long *)varp == &curbuf->b_p_ul
1664 && opt_flags == OPT_LOCAL)
1665 value = NO_LOCAL_UNDOLEVEL;
1666 else
1667 value = *(long *)get_varp_scope(
1668 &(options[opt_idx]), OPT_GLOBAL);
1669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 else if (((long *)varp == &p_wc
1671 || (long *)varp == &p_wcm)
1672 && (*arg == '<'
1673 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001674 || (*arg != NUL
1675 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 && !VIM_ISDIGIT(*arg))))
1677 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001678 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (value == 0 && (long *)varp != &p_wcm)
1680 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001681 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 goto skip;
1683 }
1684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1686 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001687 // Allow negative (for 'undolevels'), octal and
1688 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001689 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001690 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001691 if (i == 0 || (arg[i] != NUL
1692 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001694 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 goto skip;
1696 }
1697 }
1698 else
1699 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001700 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 goto skip;
1702 }
1703
1704 if (adding)
1705 value = *(long *)varp + value;
1706 if (prepending)
1707 value = *(long *)varp * value;
1708 if (removing)
1709 value = *(long *)varp - value;
1710 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001711 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001713 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001715 char_u *save_arg = NULL;
1716 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001717 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001718 char_u *newval;
1719 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001720 char_u *origval_l = NULL;
1721 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001722#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001723 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001724 char_u *saved_origval_l = NULL;
1725 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001726 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001727#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001728 unsigned newlen;
1729 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 int new_value_alloced; // new string option
1731 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001733 // When using ":set opt=val" for a global option
1734 // with a local value the local value will be
1735 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001737 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 varp = options[opt_idx].var;
1739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001740 // The old value is kept until we are sure that the
1741 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001743
Bram Moolenaard7c96872019-06-15 17:12:48 +02001744 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1745 {
1746 origval_l = *(char_u **)get_varp_scope(
1747 &(options[opt_idx]), OPT_LOCAL);
1748 origval_g = *(char_u **)get_varp_scope(
1749 &(options[opt_idx]), OPT_GLOBAL);
1750
1751 // A global-local string option might have an empty
1752 // option as value to indicate that the global
1753 // value should be used.
1754 if (((int)options[opt_idx].indir & PV_BOTH)
1755 && origval_l == empty_option)
1756 origval_l = origval_g;
1757 }
1758
1759 // When setting the local value of a global
1760 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001761 if (((int)options[opt_idx].indir & PV_BOTH)
1762 && (opt_flags & OPT_LOCAL))
1763 origval = *(char_u **)get_varp(
1764 &options[opt_idx]);
1765 else
1766 origval = oldval;
1767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001768 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770 newval = options[opt_idx].def_val[
1771 ((flags & P_VI_DEF) || cp_val)
1772 ? VI_DEFAULT : VIM_DEFAULT];
1773 if ((char_u **)varp == &p_bg)
1774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001775 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#ifdef FEAT_GUI
1777 if (gui.in_use)
1778 newval = gui_bg_default();
1779 else
1780#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001781 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001783 else if ((char_u **)varp == &p_fencs && enc_utf8)
1784 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001786 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001787 // default value was already expanded, only
1788 // required when an environment variable was set
1789 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (newval == NULL)
1791 newval = empty_option;
1792 else
1793 {
1794 s = option_expand(opt_idx, newval);
1795 if (s == NULL)
1796 s = newval;
1797 newval = vim_strsave(s);
1798 }
1799 new_value_alloced = TRUE;
1800 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001801 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 newval = vim_strsave(*(char_u **)get_varp_scope(
1804 &(options[opt_idx]), OPT_GLOBAL));
1805 new_value_alloced = TRUE;
1806 }
1807 else
1808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001809 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811 /*
1812 * Set 'keywordprg' to ":help" if an empty
1813 * value was passed to :set by the user.
1814 * Misuse errbuf[] for the resulting string.
1815 */
1816 if (varp == (char_u *)&p_kp
1817 && (*arg == NUL || *arg == ' '))
1818 {
1819 STRCPY(errbuf, ":help");
1820 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001821 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001824 * Convert 'backspace' number to string, for
1825 * adding, prepending and removing string.
1826 */
1827 else if (varp == (char_u *)&p_bs
1828 && VIM_ISDIGIT(**(char_u **)varp))
1829 {
1830 i = getdigits((char_u **)varp);
1831 switch (i)
1832 {
1833 case 0:
1834 *(char_u **)varp = empty_option;
1835 break;
1836 case 1:
1837 *(char_u **)varp = vim_strsave(
1838 (char_u *)"indent,eol");
1839 break;
1840 case 2:
1841 *(char_u **)varp = vim_strsave(
1842 (char_u *)"indent,eol,start");
1843 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001844 case 3:
1845 *(char_u **)varp = vim_strsave(
1846 (char_u *)"indent,eol,nostop");
1847 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001848 }
1849 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001850 if (origval == oldval)
1851 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001852 if (origval_l == oldval)
1853 origval_l = *(char_u **)varp;
1854 if (origval_g == oldval)
1855 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001856 oldval = *(char_u **)varp;
1857 }
1858 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 * Convert 'whichwrap' number to string, for
1860 * backwards compatibility with Vim 3.0.
1861 * Misuse errbuf[] for the resulting string.
1862 */
1863 else if (varp == (char_u *)&p_ww
1864 && VIM_ISDIGIT(*arg))
1865 {
1866 *errbuf = NUL;
1867 i = getdigits(&arg);
1868 if (i & 1)
1869 STRCAT(errbuf, "b,");
1870 if (i & 2)
1871 STRCAT(errbuf, "s,");
1872 if (i & 4)
1873 STRCAT(errbuf, "h,l,");
1874 if (i & 8)
1875 STRCAT(errbuf, "<,>,");
1876 if (i & 16)
1877 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001878 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 errbuf[STRLEN(errbuf) - 1] = NUL;
1880 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001881 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 /*
1884 * Remove '>' before 'dir' and 'bdir', for
1885 * backwards compatibility with version 3.0
1886 */
1887 else if ( *arg == '>'
1888 && (varp == (char_u *)&p_dir
1889 || varp == (char_u *)&p_bdir))
1890 {
1891 ++arg;
1892 }
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 /*
1895 * Copy the new string into allocated memory.
1896 * Can't use set_string_option_direct(), because
1897 * we need to remove the backslashes.
1898 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001899 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 newlen = (unsigned)STRLEN(arg) + 1;
1901 if (adding || prepending || removing)
1902 newlen += (unsigned)STRLEN(origval) + 1;
1903 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001904 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 break;
1906 s = newval;
1907
1908 /*
1909 * Copy the string, skip over escaped chars.
1910 * For MS-DOS and WIN32 backslashes before normal
1911 * file name characters are not removed, and keep
1912 * backslash at start, for "\\machine\path", but
1913 * do remove it for "\\\\machine\\path".
1914 * The reverse is found in ExpandOldSetting().
1915 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001916 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 if (*arg == '\\' && arg[1] != NUL
1919#ifdef BACKSLASH_IN_FILENAME
1920 && !((flags & P_EXPAND)
1921 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001922 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 && (arg[1] != '\\'
1924 || (s == newval
1925 && arg[2] != '\\')))
1926#endif
1927 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001928 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001930 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001932 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 mch_memmove(s, arg, (size_t)i);
1934 arg += i;
1935 s += i;
1936 }
1937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 *s++ = *arg++;
1939 }
1940 *s = NUL;
1941
1942 /*
1943 * Expand environment variables and ~.
1944 * Don't do it when adding without inserting a
1945 * comma.
1946 */
1947 if (!(adding || prepending || removing)
1948 || (flags & P_COMMA))
1949 {
1950 s = option_expand(opt_idx, newval);
1951 if (s != NULL)
1952 {
1953 vim_free(newval);
1954 newlen = (unsigned)STRLEN(s) + 1;
1955 if (adding || prepending || removing)
1956 newlen += (unsigned)STRLEN(origval) + 1;
1957 newval = alloc(newlen);
1958 if (newval == NULL)
1959 break;
1960 STRCPY(newval, s);
1961 }
1962 }
1963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 // locate newval[] in origval[] when removing it
1965 // and when adding to avoid duplicates
1966 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (removing || (flags & P_NODUP))
1968 {
1969 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001970 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001972 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001973 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 prepending = FALSE;
1976 adding = FALSE;
1977 STRCPY(newval, origval);
1978 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001979
1980 // if no duplicate, move pointer to end of
1981 // original value
1982 if (s == NULL)
1983 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001986 // concatenate the two strings; add a ',' if
1987 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (adding || prepending)
1989 {
1990 comma = ((flags & P_COMMA) && *origval != NUL
1991 && *newval != NUL);
1992 if (adding)
1993 {
1994 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001995 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001996 if (comma && i > 1
1997 && (flags & P_ONECOMMA) == P_ONECOMMA
1998 && origval[i - 1] == ','
1999 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02002000 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 mch_memmove(newval + i + comma, newval,
2002 STRLEN(newval) + 1);
2003 mch_memmove(newval, origval, (size_t)i);
2004 }
2005 else
2006 {
2007 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002008 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 if (comma)
2011 newval[i] = ',';
2012 }
2013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002014 // Remove newval[] from origval[]. (Note: "i" has
2015 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (removing)
2017 {
2018 STRCPY(newval, origval);
2019 if (*s)
2020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002021 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (flags & P_COMMA)
2023 {
2024 if (s == origval)
2025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002026 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (s[i] == ',')
2028 ++i;
2029 }
2030 else
2031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002032 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 --s;
2034 ++i;
2035 }
2036 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002037 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 }
2040
2041 if (flags & P_FLAGLIST)
2042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002044 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002046 // if options have P_FLAGLIST and
2047 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002050 if (*s != ',' && *(s + 1) == ','
2051 && vim_strchr(s + 2, *s) != NULL)
2052 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002053 // Remove the duplicated value and
2054 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002055 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002056 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002059 else
2060 {
2061 if ((!(flags & P_COMMA) || *s != ',')
2062 && vim_strchr(s + 1, *s) != NULL)
2063 {
2064 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002065 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002066 }
2067 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002068 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002072 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 arg = save_arg;
2074 new_value_alloced = TRUE;
2075 }
2076
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002077 /*
2078 * Set the new value.
2079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 *(char_u **)(varp) = newval;
2081
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002082#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002083 if (!starting
2084# ifdef FEAT_CRYPT
2085 && options[opt_idx].indir != PV_KEY
2086# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002087 && origval != NULL && newval != NULL)
2088 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002089 // origval may be freed by
2090 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002091 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 // newval (and varp) may become invalid if the
2093 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002094 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002095 if (origval_l != NULL)
2096 saved_origval_l = vim_strsave(origval_l);
2097 if (origval_g != NULL)
2098 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002099 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002100#endif
2101
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002102 {
2103 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002104 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002105
2106 // When an option is set in the sandbox, from a
2107 // modeline or in secure mode, then deal with side
2108 // effects in secure mode. Also when the value was
2109 // set with the P_INSECURE flag and is not
2110 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002111 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002112#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002113 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002114#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002115 || (!value_is_replaced && (*p & P_INSECURE)))
2116 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002117
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002118 // Handle side effects, and set the global value
2119 // for ":set" on local options. Note: when setting
2120 // 'syntax' or 'filetype' autocommands may be
2121 // triggered that can cause havoc.
2122 errmsg = did_set_string_option(
2123 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002124 new_value_alloced, oldval, errbuf,
2125 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002126
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002127 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002130#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002131 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002132 trigger_optionsset_string(
2133 opt_idx, opt_flags, saved_origval,
2134 saved_origval_l, saved_origval_g,
2135 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002136 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002137 vim_free(saved_origval_l);
2138 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002139 vim_free(saved_newval);
2140#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (errmsg != NULL)
2143 goto skip;
2144 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 char_u *p;
2148
2149 if (nextchar == '&')
2150 {
2151 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002152 errmsg = N_(e_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 else
2155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002156 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002157 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (*p == '\\' && p[1] != NUL)
2159 ++p;
2160 nextchar = *p;
2161 *p = NUL;
2162 add_termcode(key_name, arg, FALSE);
2163 *p = nextchar;
2164 }
2165 if (full_screen)
2166 ttest(FALSE);
2167 redraw_all_later(CLEAR);
2168 }
2169 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002172 did_set_option(
2173 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175
2176skip:
2177 /*
2178 * Advance to next argument.
2179 * - skip until a blank found, taking care of backslashes
2180 * - skip blanks
2181 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2182 */
2183 for (i = 0; i < 2 ; ++i)
2184 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002185 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (*arg++ == '\\' && *arg != NUL)
2187 ++arg;
2188 arg = skipwhite(arg);
2189 if (*arg != '=')
2190 break;
2191 }
2192 }
2193
2194 if (errmsg != NULL)
2195 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002196 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002197 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (i + (arg - startarg) < IOSIZE)
2199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 STRCAT(IObuff, ": ");
2202 mch_memmove(IObuff + i, startarg, (arg - startarg));
2203 IObuff[i + (arg - startarg)] = NUL;
2204 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002205 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 trans_characters(IObuff, IOSIZE);
2207
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002208 ++no_wait_return; // wait_return done later
2209 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 --no_wait_return;
2211
2212 return FAIL;
2213 }
2214
2215 arg = skipwhite(arg);
2216 }
2217
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218theend:
2219 if (silent_mode && did_show)
2220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002221 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002223 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002224 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002226 out_flush();
2227 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002228 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002229 }
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return OK;
2232}
2233
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234/*
2235 * Call this when an option has been given a new value through a user command.
2236 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2237 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239did_set_option(
2240 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002241 int opt_flags, // possibly with OPT_MODELINE
2242 int new_value, // value was replaced completely
2243 int value_checked) // value was checked to be safe, no need to set the
2244 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002245{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002246 long_u *p;
2247
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002248 options[opt_idx].flags |= P_WAS_SET;
2249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002250 // When an option is set in the sandbox, from a modeline or in secure mode
2251 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2252 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002253 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002254 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002255#ifdef HAVE_SANDBOX
2256 || sandbox != 0
2257#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002258 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002259 *p = *p | P_INSECURE;
2260 else if (new_value)
2261 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002262}
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264/*
2265 * Convert a key name or string into a key value.
2266 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002267 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002269 int
2270string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002273 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (*arg == '^')
2275 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002276 if (multi_byte)
2277 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return *arg;
2279}
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281/*
2282 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2283 * maketitle() to create and display it.
2284 * When switching the title or icon off, call mch_restore_title() to get
2285 * the old value back.
2286 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002287 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002288did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 if (starting != NO_SCREEN
2291#ifdef FEAT_GUI
2292 && !gui.starting
2293#endif
2294 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298/*
2299 * set_options_bin - called when 'bin' changes value.
2300 */
2301 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002302set_options_bin(
2303 int oldval,
2304 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002305 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 /*
2308 * The option values that are changed when 'bin' changes are
2309 * copied when 'bin is set and restored when 'bin' is reset.
2310 */
2311 if (newval)
2312 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002313 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 if (!(opt_flags & OPT_GLOBAL))
2316 {
2317 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2318 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2319 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2320 curbuf->b_p_et_nobin = curbuf->b_p_et;
2321 }
2322 if (!(opt_flags & OPT_LOCAL))
2323 {
2324 p_tw_nobin = p_tw;
2325 p_wm_nobin = p_wm;
2326 p_ml_nobin = p_ml;
2327 p_et_nobin = p_et;
2328 }
2329 }
2330
2331 if (!(opt_flags & OPT_GLOBAL))
2332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 curbuf->b_p_tw = 0; // no automatic line wrap
2334 curbuf->b_p_wm = 0; // no automatic line wrap
2335 curbuf->b_p_ml = 0; // no modelines
2336 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338 if (!(opt_flags & OPT_LOCAL))
2339 {
2340 p_tw = 0;
2341 p_wm = 0;
2342 p_ml = FALSE;
2343 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002344 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 if (!(opt_flags & OPT_GLOBAL))
2350 {
2351 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2352 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2353 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2354 curbuf->b_p_et = curbuf->b_p_et_nobin;
2355 }
2356 if (!(opt_flags & OPT_LOCAL))
2357 {
2358 p_tw = p_tw_nobin;
2359 p_wm = p_wm_nobin;
2360 p_ml = p_ml_nobin;
2361 p_et = p_et_nobin;
2362 }
2363 }
2364}
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366/*
2367 * Expand environment variables for some string options.
2368 * These string options cannot be indirect!
2369 * If "val" is NULL expand the current value of the option.
2370 * Return pointer to NameBuff, or NULL when not expanded.
2371 */
2372 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002373option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2377 return NULL;
2378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002379 // If val is longer than MAXPATHL no meaningful expansion can be done,
2380 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (val != NULL && STRLEN(val) > MAXPATHL)
2382 return NULL;
2383
2384 if (val == NULL)
2385 val = *(char_u **)options[opt_idx].var;
2386
2387 /*
2388 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2389 * Escape spaces when expanding 'tags', they are used to separate file
2390 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002391 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002394 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002395#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002396 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2397#endif
2398 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002399 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 return NULL;
2401
2402 return NameBuff;
2403}
2404
2405/*
2406 * After setting various option values: recompute variables that depend on
2407 * option values.
2408 */
2409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002410didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002412 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 (void)init_chartab();
2414
Bram Moolenaardac13472019-09-16 21:06:21 +02002415 didset_string_options();
2416
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002417#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002418 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002419 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002420 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002421 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002424 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 (void)check_cedit();
2426#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002427#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002428 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002429 fill_breakat_flags();
2430#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002431 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002432}
2433
2434/*
2435 * More side effects of setting options.
2436 */
2437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002439{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002440 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002441 (void)highlight_changed();
2442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002443 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002444 check_opt_wim();
2445
Bram Moolenaareed9d462021-02-15 20:38:25 +01002446 // Parse default for 'listchars'.
2447 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002449 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002450 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002451
2452#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002453 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002454 (void)check_clipboard_option();
2455#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002456#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002457 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002458 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002459 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002460 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462}
2463
2464/*
2465 * Check for string options that are NULL (normally only termcap options).
2466 */
2467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002468check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 int opt_idx;
2471
2472 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2473 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2474 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2475}
2476
2477/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002478 * Return the option index found by a pointer into term_strings[].
2479 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002481 int
2482get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002484 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2487 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002488 return opt_idx;
2489 return -1; // cannot happen: didn't find it!
2490}
2491
2492/*
2493 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2494 * Return the option index or -1 if not found.
2495 */
2496 int
2497set_term_option_alloced(char_u **p)
2498{
2499 int opt_idx = get_term_opt_idx(p);
2500
2501 if (opt_idx >= 0)
2502 options[opt_idx].flags |= P_ALLOCED;
2503 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504}
2505
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002506#if defined(FEAT_EVAL) || defined(PROTO)
2507/*
2508 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2509 * Return FALSE when it wasn't.
2510 * Return -1 for an unknown option.
2511 */
2512 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002513was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002514{
2515 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002516 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002517
2518 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002519 {
2520 flagp = insecure_flag(idx, opt_flags);
2521 return (*flagp & P_INSECURE) != 0;
2522 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002523 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002524 return -1;
2525}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002526
2527/*
2528 * Get a pointer to the flags used for the P_INSECURE flag of option
2529 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002530 * NOTE: Caller must make sure that "curwin" is set to the window from which
2531 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002532 */
2533 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002534insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535{
2536 if (opt_flags & OPT_LOCAL)
2537 switch ((int)options[opt_idx].indir)
2538 {
2539#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002540 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541#endif
2542#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002543# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002544 case PV_FDE: return &curwin->w_p_fde_flags;
2545 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002546# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002547# ifdef FEAT_BEVAL
2548 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2549# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002550# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002551 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002552# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002553 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002554# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002555 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002556# endif
2557#endif
2558 }
2559
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002560 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002561 return &options[opt_idx].flags;
2562}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002563#endif
2564
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002565/*
2566 * Redraw the window title and/or tab page text later.
2567 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002568void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002569{
2570 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002571 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002572}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002575 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2576 * characters or characters in "allowed".
2577 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002578 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002579valid_name(char_u *val, char *allowed)
2580{
2581 char_u *s;
2582
2583 for (s = val; *s != NUL; ++s)
2584 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2585 return FALSE;
2586 return TRUE;
2587}
2588
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002589#if defined(FEAT_EVAL) || defined(PROTO)
2590/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002591 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002592 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002593 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002594 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002595set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002596{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002597 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2598 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002599 sctx_T new_script_ctx = script_ctx;
2600
Bram Moolenaar51258742020-05-03 17:19:33 +02002601 // Modeline already has the line number set.
2602 if (!(opt_flags & OPT_MODELINE))
2603 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002604
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002605 // Remember where the option was set. For local options need to do that
2606 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002607 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002608 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002609 if (both || (opt_flags & OPT_LOCAL))
2610 {
2611 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002612 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002613 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002614 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002615 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002616 if (both)
2617 // also setting the "all buffers" value
2618 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2619 new_script_ctx;
2620 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002621 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002622}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002623
2624/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002625 * Get the script context of global option "name".
2626 *
2627 */
2628 sctx_T *
2629get_option_sctx(char *name)
2630{
2631 int idx = findoption((char_u *)name);
2632
2633 if (idx >= 0)
2634 return &options[idx].script_ctx;
2635 siemsg("no such option: %s", name);
2636 return NULL;
2637}
2638
2639/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002640 * Set the script_ctx for a termcap option.
2641 * "name" must be the two character code, e.g. "RV".
2642 * When "name" is NULL use "opt_idx".
2643 */
2644 void
2645set_term_option_sctx_idx(char *name, int opt_idx)
2646{
2647 char_u buf[5];
2648 int idx;
2649
2650 if (name == NULL)
2651 idx = opt_idx;
2652 else
2653 {
2654 buf[0] = 't';
2655 buf[1] = '_';
2656 buf[2] = name[0];
2657 buf[3] = name[1];
2658 buf[4] = 0;
2659 idx = findoption(buf);
2660 }
2661 if (idx >= 0)
2662 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2663}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002664#endif
2665
Bram Moolenaarf4140482020-02-15 23:06:45 +01002666#if defined(FEAT_EVAL)
2667/*
2668 * Apply the OptionSet autocommand.
2669 */
2670 static void
2671apply_optionset_autocmd(
2672 int opt_idx,
2673 long opt_flags,
2674 long oldval,
2675 long oldval_g,
2676 long newval,
2677 char *errmsg)
2678{
2679 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2680
2681 // Don't do this while starting up, failure or recursively.
2682 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2683 return;
2684
2685 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2686 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2687 oldval_g);
2688 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2689 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2690 (opt_flags & OPT_LOCAL) ? "local" : "global");
2691 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2692 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2693 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2694 if (opt_flags & OPT_LOCAL)
2695 {
2696 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2697 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2698 }
2699 if (opt_flags & OPT_GLOBAL)
2700 {
2701 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2702 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2703 }
2704 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2705 {
2706 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2707 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2708 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2709 }
2710 if (opt_flags & OPT_MODELINE)
2711 {
2712 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2713 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2714 }
2715 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2716 NULL, FALSE, NULL);
2717 reset_v_option_vars();
2718}
2719#endif
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721/*
2722 * Set the value of a boolean option, and take care of side effects.
2723 * Returns NULL for success, or an error message for an error.
2724 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002725 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002726set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 int opt_idx, // index in options[] table
2728 char_u *varp, // pointer to the option variable
2729 int value, // new value
2730 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002733#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002734 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if ((secure
2739#ifdef HAVE_SANDBOX
2740 || sandbox != 0
2741#endif
2742 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002743 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002745#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002746 // Save the global value before changing anything. This is needed as for
2747 // a global-only option setting the "local value" in fact sets the global
2748 // value (since there is only one value).
2749 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2750 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2751 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002752#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002757 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
2759
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002760#ifdef FEAT_GUI
2761 need_mouse_correct = TRUE;
2762#endif
2763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2766 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2767
2768 /*
2769 * Handle side effects of changing a bool option.
2770 */
2771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar920694c2016-08-21 17:45:02 +02002776#ifdef FEAT_LANGMAP
2777 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002779 p_lnr = !p_lrm;
2780 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002782 p_lrm = !p_lnr;
2783#endif
2784
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002785#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002787 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002789 // Only take action when the option was set. When reset we do not
2790 // delete the undo file, the option may be set again without making
2791 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002792 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002793 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002794 char_u hash[UNDO_HASH_SIZE];
2795 buf_T *save_curbuf = curbuf;
2796
Bram Moolenaar29323592016-07-24 22:04:11 +02002797 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 // When 'undofile' is set globally: for every buffer, otherwise
2800 // only for the current buffer: Try to read in the undofile,
2801 // if one exists, the buffer wasn't changed and the buffer was
2802 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002803 if ((curbuf == save_curbuf
2804 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2805 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2806 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002807#ifdef FEAT_CRYPT
2808 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2809 continue;
2810#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002811 u_compute_hash(hash);
2812 u_read_undo(NULL, hash, curbuf->b_fname);
2813 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002814 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002815 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002816 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002817 }
2818#endif
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 else if ((int *)varp == &curbuf->b_p_ro)
2821 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002822 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2824 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002826 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002827 if (curbuf->b_p_ro)
2828 curbuf->b_did_warn = FALSE;
2829
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002830 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832
Bram Moolenaar9c449722010-07-20 18:44:27 +02002833#ifdef FEAT_GUI
2834 else if ((int *)varp == &p_mh)
2835 {
2836 if (!p_mh)
2837 gui_mch_mousehide(FALSE);
2838 }
2839#endif
2840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002843 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002844# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002846 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2847 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002848 {
2849 curbuf->b_p_ma = FALSE;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002850 return N_(e_cannot_make_terminal_with_running_job_modifiable);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002851 }
2852# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002853 redraw_titles();
2854 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002857 {
2858 redraw_titles();
2859 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002861 else if ((int *)varp == &curbuf->b_p_fixeol)
2862 {
2863 redraw_titles();
2864 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002866 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002867 {
2868 redraw_titles();
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 else if ((int *)varp == &curbuf->b_p_bin)
2873 {
2874 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002875 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2880 {
2881 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2882 NULL, NULL, TRUE, curbuf);
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 else if ((int *)varp == &curbuf->b_p_swf)
2887 {
2888 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2892 // buf->b_p_swf
2893 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
2895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 else if ((int *)varp == &p_terse)
2898 {
2899 char_u *p;
2900
2901 p = vim_strchr(p_shm, SHM_SEARCH);
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (p_terse && p == NULL)
2905 {
2906 STRCPY(IObuff, p_shm);
2907 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002908 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002912 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 else if ((int *)varp == &p_paste)
2917 {
2918 paste_option_changed();
2919 }
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 else if ((int *)varp == &p_im)
2923 {
2924 if (p_im)
2925 {
2926 if ((State & INSERT) == 0)
2927 need_start_insertmode = TRUE;
2928 stop_insert_mode = FALSE;
2929 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002931 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 {
2933 need_start_insertmode = FALSE;
2934 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 restart_edit = 0;
2938 }
2939 }
2940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 else if ((int *)varp == &p_ic && p_hls)
2943 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002944 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946
2947#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 else if ((int *)varp == &p_hls)
2950 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002951 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953#endif
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2956 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 else if ((int *)varp == &curwin->w_p_scb)
2958 {
2959 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002962 curwin->w_scbind_pos = curwin->w_topline;
2963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaar4033c552017-09-16 20:54:51 +02002966#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 else if ((int *)varp == &curwin->w_p_pvw)
2969 {
2970 if (curwin->w_p_pvw)
2971 {
2972 win_T *win;
2973
Bram Moolenaar29323592016-07-24 22:04:11 +02002974 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (win->w_p_pvw && win != curwin)
2976 {
2977 curwin->w_p_pvw = FALSE;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002978 return N_(e_preview_window_already_exists);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 }
2981 }
2982#endif
2983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else if ((int *)varp == &curbuf->b_p_tx)
2986 {
2987 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2988 }
2989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 set_string_option_direct((char_u *)"ffs", -1,
2994 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002995 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 /*
2999 * When 'lisp' option changes include/exclude '-' in
3000 * keyword characters.
3001 */
3002#ifdef FEAT_LISP
3003 else if (varp == (char_u *)&(curbuf->b_p_lisp))
3004 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007#endif
3008
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003009 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003010 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003012 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 else if ((int *)varp == &curbuf->b_changed)
3016 {
3017 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003019 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022
3023#ifdef BACKSLASH_IN_FILENAME
3024 else if ((int *)varp == &p_ssl)
3025 {
3026 if (p_ssl)
3027 {
3028 psepc = '/';
3029 psepcN = '\\';
3030 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
3032 else
3033 {
3034 psepc = '\\';
3035 psepcN = '/';
3036 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
3038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 buflist_slash_adjust();
3041 alist_slash_adjust();
3042# ifdef FEAT_EVAL
3043 scriptnames_slash_adjust();
3044# endif
3045 }
3046#endif
3047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003048 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 else if ((int *)varp == &curwin->w_p_wrap)
3050 {
3051 if (curwin->w_p_wrap)
3052 curwin->w_leftcol = 0;
3053 }
3054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 else if ((int *)varp == &p_ea)
3056 {
3057 if (p_ea && !old_value)
3058 win_equal(curwin, FALSE, 0);
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
3061 else if ((int *)varp == &p_wiv)
3062 {
3063 /*
3064 * When 'weirdinvert' changed, set/reset 't_xs'.
3065 * Then set 'weirdinvert' according to value of 't_xs'.
3066 */
3067 if (p_wiv && !old_value)
3068 T_XS = (char_u *)"y";
3069 else if (!p_wiv && old_value)
3070 T_XS = empty_option;
3071 p_wiv = (*T_XS != NUL);
3072 }
3073
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003074#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 else if ((int *)varp == &p_beval)
3076 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003077 if (!balloonEvalForTerm)
3078 {
3079 if (p_beval && !old_value)
3080 gui_mch_enable_beval_area(balloonEval);
3081 else if (!p_beval && old_value)
3082 gui_mch_disable_beval_area(balloonEval);
3083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003085#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003086#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003087 else if ((int *)varp == &p_bevalterm)
3088 {
3089 mch_bevalterm_changed();
3090 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003093#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 else if ((int *)varp == &p_acd)
3095 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003097 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
3099#endif
3100
3101#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 else if ((int *)varp == &curwin->w_p_diff)
3104 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003105 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003106 diff_buf_adjust(curwin);
3107# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (foldmethodIsDiff(curwin))
3109 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112#endif
3113
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003114#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 else if ((int *)varp == &p_imdisable)
3117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (p_imdisable)
3120 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003121 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003122 // When the option is set from an autocommand, it may need to take
3123 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003124 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
3126#endif
3127
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003128#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003130 else if ((int *)varp == &curwin->w_p_spell)
3131 {
3132 if (curwin->w_p_spell)
3133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003134 char *errmsg = did_set_spelllang(curwin);
3135
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003136 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003137 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003138 }
3139 }
3140#endif
3141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142#ifdef FEAT_ARABIC
3143 if ((int *)varp == &curwin->w_p_arab)
3144 {
3145 if (curwin->w_p_arab)
3146 {
3147 /*
3148 * 'arabic' is set, handle various sub-settings.
3149 */
3150 if (!p_tbidi)
3151 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (!curwin->w_p_rl)
3154 {
3155 curwin->w_p_rl = TRUE;
3156 changed_window_setting();
3157 }
3158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (!p_arshape)
3161 {
3162 p_arshape = TRUE;
3163 redraw_later_clear();
3164 }
3165 }
3166
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003167 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003170 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003171 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3172
Bram Moolenaar8820b482017-03-16 17:23:31 +01003173 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003174 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003175#ifdef FEAT_EVAL
3176 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3177#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3186 OPT_LOCAL);
3187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
3189 else
3190 {
3191 /*
3192 * 'arabic' is reset, handle various sub-settings.
3193 */
3194 if (!p_tbidi)
3195 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003196 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (curwin->w_p_rl)
3198 {
3199 curwin->w_p_rl = FALSE;
3200 changed_window_setting();
3201 }
3202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 // 'arabicshape' isn't reset, it is a global option and
3204 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003207 // 'delcombine' isn't reset, it is a global option and another
3208 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
3210# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003211 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 curbuf->b_p_iminsert = B_IMODE_NONE;
3213 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3214# endif
3215 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003216 }
3217
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003218#endif
3219
Bram Moolenaar44826212019-08-22 21:23:20 +02003220#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3221 else if (((int *)varp == &curwin->w_p_nu
3222 || (int *)varp == &curwin->w_p_rnu)
3223 && gui.in_use
3224 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3225 && curbuf->b_signlist != NULL)
3226 {
3227 // If the 'number' or 'relativenumber' options are modified and
3228 // 'signcolumn' is set to 'number', then clear the screen for a full
3229 // refresh. Otherwise the sign icons are not displayed properly in the
3230 // number column. If the 'number' option is set and only the
3231 // 'relativenumber' option is toggled, then don't refresh the screen
3232 // (optimization).
3233 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3234 redraw_all_later(CLEAR);
3235 }
3236#endif
3237
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003238#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003239 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003240 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003241 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003242# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003243 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003244 if (
3245# ifdef VIMDLL
3246 !gui.in_use && !gui.starting &&
3247# endif
3248 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003249 {
3250 p_tgc = 0;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003251 return N_(e_24_bit_colors_are_not_supported_on_this_environment);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003252 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003253 if (is_term_win32())
3254 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003255# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003256# ifdef FEAT_GUI
3257 if (!gui.in_use && !gui.starting)
3258# endif
3259 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003260# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003261 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003262 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003263 {
3264 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003265 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003266 init_highlight(TRUE, FALSE);
3267 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003268# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003269# ifdef FEAT_TERMINAL
3270 term_update_colors_all();
3271 term_update_wincolor_all();
3272# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003273 }
3274#endif
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 /*
3277 * End of handling side effects for bool options.
3278 */
3279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003280 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 options[opt_idx].flags |= P_WAS_SET;
3283
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003284#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003285 apply_optionset_autocmd(opt_idx, opt_flags,
3286 (long)(old_value ? TRUE : FALSE),
3287 (long)(old_global_value ? TRUE : FALSE),
3288 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003289#endif
3290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003291 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003292 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003293 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003294 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003295
3296 if ((opt_flags & OPT_NO_REDRAW) == 0)
3297 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
3299 return NULL;
3300}
3301
3302/*
3303 * Set the value of a number option, and take care of side effects.
3304 * Returns NULL for success, or an error message for an error.
3305 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003306 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003307set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003308 int opt_idx, // index in options[] table
3309 char_u *varp, // pointer to the option variable
3310 long value, // new value
3311 char *errbuf, // buffer for error messages
3312 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003313 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3314 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003316 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003318#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003319 long old_global_value = 0; // only used when setting a local and
3320 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003321#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003322 long old_Rows = Rows; // remember old Rows
3323 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 long *pp = (long *)varp;
3325
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003326 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003327 if ((secure
3328#ifdef HAVE_SANDBOX
3329 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003331 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003332 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003334#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003335 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003336 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003337 // value (since there is only one value).
3338 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003339 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3340 OPT_GLOBAL);
3341#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 *pp = value;
3344#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003345 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003346 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003348#ifdef FEAT_GUI
3349 need_mouse_correct = TRUE;
3350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar14f24742012-08-08 18:01:05 +02003352 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003354 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003355#ifdef FEAT_VARTABS
3356 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3357 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3358 ? tabstop_first(curbuf->b_p_vts_array)
3359 : curbuf->b_p_ts;
3360#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
3364
3365 /*
3366 * Number options that need some action when changed
3367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (pp == &p_wh || pp == &p_hh)
3369 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003370 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (p_wh < 1)
3372 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003373 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 p_wh = 1;
3375 }
3376 if (p_wmh > p_wh)
3377 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003378 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 p_wh = p_wmh;
3380 }
3381 if (p_hh < 0)
3382 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003383 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 p_hh = 0;
3385 }
3386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003387 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003388 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
3390 if (pp == &p_wh && curwin->w_height < p_wh)
3391 win_setheight((int)p_wh);
3392 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3393 win_setheight((int)p_hh);
3394 }
3395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 else if (pp == &p_wmh)
3397 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003398 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (p_wmh < 0)
3400 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003401 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 p_wmh = 0;
3403 }
3404 if (p_wmh > p_wh)
3405 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003406 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 p_wmh = p_wh;
3408 }
3409 win_setminheight();
3410 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003411 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003413 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (p_wiw < 1)
3415 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003416 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 p_wiw = 1;
3418 }
3419 if (p_wmw > p_wiw)
3420 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003421 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 p_wiw = p_wmw;
3423 }
3424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003425 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003426 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 win_setwidth((int)p_wiw);
3428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 else if (pp == &p_wmw)
3430 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003431 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (p_wmw < 0)
3433 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003434 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 p_wmw = 0;
3436 }
3437 if (p_wmw > p_wiw)
3438 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003439 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 p_wmw = p_wiw;
3441 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003442 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003445 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 else if (pp == &p_ls)
3447 {
3448 last_status(FALSE);
3449 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003451 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003452 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457#ifdef FEAT_GUI
3458 else if (pp == &p_linespace)
3459 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 // Recompute gui.char_height and resize the Vim window to keep the
3461 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003462 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003463 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465#endif
3466
3467#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 else if (pp == &curwin->w_p_fdl)
3470 {
3471 if (curwin->w_p_fdl < 0)
3472 curwin->w_p_fdl = 0;
3473 newFoldLevel();
3474 }
3475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003476 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else if (pp == &curwin->w_p_fml)
3478 {
3479 foldUpdateAll(curwin);
3480 }
3481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 else if (pp == &curwin->w_p_fdn)
3484 {
3485 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3486 foldUpdateAll(curwin);
3487 }
3488
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003489 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 else if (pp == &curwin->w_p_fdc)
3491 {
3492 if (curwin->w_p_fdc < 0)
3493 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003494 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 curwin->w_p_fdc = 0;
3496 }
3497 else if (curwin->w_p_fdc > 12)
3498 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003499 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 curwin->w_p_fdc = 12;
3501 }
3502 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003503#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003505#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003506 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3508 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003509# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (foldmethodIsIndent(curwin))
3511 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003512# endif
3513# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3515 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003516 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3517 parse_cino(curbuf);
3518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003522 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003523 else if (pp == &p_mco)
3524 {
3525 if (p_mco > MAX_MCO)
3526 p_mco = MAX_MCO;
3527 else if (p_mco < 0)
3528 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003529 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003530 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 else if (pp == &curbuf->b_p_iminsert)
3533 {
3534 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3535 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003536 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 curbuf->b_p_iminsert = B_IMODE_NONE;
3538 }
3539 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003540 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003542#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003543 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 status_redraw_curbuf();
3545#endif
3546 }
3547
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003548#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003549 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003550 else if (pp == &p_imst)
3551 {
3552 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003553 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003554 }
3555#endif
3556
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003557 else if (pp == &p_window)
3558 {
3559 if (p_window < 1)
3560 p_window = 1;
3561 else if (p_window >= Rows)
3562 p_window = Rows - 1;
3563 }
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else if (pp == &curbuf->b_p_imsearch)
3566 {
3567 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3568 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003569 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 curbuf->b_p_imsearch = B_IMODE_NONE;
3571 }
3572 p_imsearch = curbuf->b_p_imsearch;
3573 }
3574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003575 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 else if (pp == &p_titlelen)
3577 {
3578 if (p_titlelen < 0)
3579 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003580 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 p_titlelen = 85;
3582 }
3583 if (starting != NO_SCREEN && old_value != p_titlelen)
3584 need_maketitle = TRUE;
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003587 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 else if (pp == &p_ch)
3589 {
3590 if (p_ch < 1)
3591 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003592 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 p_ch = 1;
3594 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003595 if (p_ch > Rows - min_rows() + 1)
3596 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003598 // Only compute the new window layout when startup has been
3599 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (p_ch != old_value && full_screen
3601#ifdef FEAT_GUI
3602 && !gui.starting
3603#endif
3604 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003605 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003608 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 else if (pp == &p_uc)
3610 {
3611 if (p_uc < 0)
3612 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003613 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 p_uc = 100;
3615 }
3616 if (p_uc && !old_value)
3617 ml_open_files();
3618 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003620 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003621 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003622 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003623 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003624 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003625 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003626 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003627 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003628 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003629 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003630 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003631 }
3632 }
3633#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003634#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003635 else if (pp == &p_mzq)
3636 mzvim_reset_timer();
3637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003639#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003640 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003641 else if (pp == &p_pyx)
3642 {
3643 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003644 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003645 }
3646#endif
3647
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003648 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 else if (pp == &p_ul)
3650 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003651 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003653 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 p_ul = value;
3655 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003656 else if (pp == &curbuf->b_p_ul)
3657 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003658 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003659 curbuf->b_p_ul = old_value;
3660 u_sync(TRUE);
3661 curbuf->b_p_ul = value;
3662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003664#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003665 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003666 else if (pp == &curwin->w_p_nuw)
3667 {
3668 if (curwin->w_p_nuw < 1)
3669 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003670 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003671 curwin->w_p_nuw = 1;
3672 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003673 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003674 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003675 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003676 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003677 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003678 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003679 }
3680#endif
3681
Bram Moolenaar1a384422010-07-14 19:53:30 +02003682 else if (pp == &curbuf->b_p_tw)
3683 {
3684 if (curbuf->b_p_tw < 0)
3685 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003686 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003687 curbuf->b_p_tw = 0;
3688 }
3689#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003690 {
3691 win_T *wp;
3692 tabpage_T *tp;
3693
3694 FOR_ALL_TAB_WINDOWS(tp, wp)
3695 check_colorcolumn(wp);
3696 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003697#endif
3698 }
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 /*
3701 * Check the bounds for numeric options here
3702 */
3703 if (Rows < min_rows() && full_screen)
3704 {
3705 if (errbuf != NULL)
3706 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003707 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003708 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 errmsg = errbuf;
3710 }
3711 Rows = min_rows();
3712 }
3713 if (Columns < MIN_COLUMNS && full_screen)
3714 {
3715 if (errbuf != NULL)
3716 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003717 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003718 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 errmsg = errbuf;
3720 }
3721 Columns = MIN_COLUMNS;
3722 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003723 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 /*
3726 * If the screen (shell) height has been changed, assume it is the
3727 * physical screenheight.
3728 */
3729 if (old_Rows != Rows || old_Columns != Columns)
3730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003731 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (updating_screen)
3733 *pp = old_value;
3734 else if (full_screen
3735#ifdef FEAT_GUI
3736 && !gui.starting
3737#endif
3738 )
3739 set_shellsize((int)Columns, (int)Rows, TRUE);
3740 else
3741 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 // Postpone the resizing; check the size and cmdline position for
3743 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 check_shellsize();
3745 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3746 cmdline_row = Rows - p_ch;
3747 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003748 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003749 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (curbuf->b_p_ts <= 0)
3753 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003754 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 curbuf->b_p_ts = 8;
3756 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003757 else if (curbuf->b_p_ts > TABSTOP_MAX)
3758 {
3759 errmsg = e_invalid_argument;
3760 curbuf->b_p_ts = 8;
3761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (p_tm < 0)
3763 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003764 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 p_tm = 0;
3766 }
3767 if ((curwin->w_p_scr <= 0
3768 || (curwin->w_p_scr > curwin->w_height
3769 && curwin->w_height > 0))
3770 && full_screen)
3771 {
3772 if (pp == &(curwin->w_p_scr))
3773 {
3774 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003775 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 win_comp_scroll(curwin);
3777 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003778 // If 'scroll' became invalid because of a side effect silently adjust
3779 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 else if (curwin->w_p_scr <= 0)
3781 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003782 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 curwin->w_p_scr = curwin->w_height;
3784 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003785 if (p_hi < 0)
3786 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003787 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003788 p_hi = 0;
3789 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003790 else if (p_hi > 10000)
3791 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003792 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003793 p_hi = 10000;
3794 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003795 if (p_re < 0 || p_re > 2)
3796 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003797 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003798 p_re = 0;
3799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 if (p_report < 0)
3801 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003802 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 p_report = 1;
3804 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003805 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003807 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 p_sj = Rows / 2;
3809 else
3810 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003811 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 p_sj = 1;
3813 }
3814 }
3815 if (p_so < 0 && full_screen)
3816 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003817 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 p_so = 0;
3819 }
3820 if (p_siso < 0 && full_screen)
3821 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003822 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 p_siso = 0;
3824 }
3825#ifdef FEAT_CMDWIN
3826 if (p_cwh < 1)
3827 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003828 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 p_cwh = 1;
3830 }
3831#endif
3832 if (p_ut < 0)
3833 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003834 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 p_ut = 2000;
3836 }
3837 if (p_ss < 0)
3838 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003839 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 p_ss = 0;
3841 }
3842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003843 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3845 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3846
3847 options[opt_idx].flags |= P_WAS_SET;
3848
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003849#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003850 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3851 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003852#endif
3853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003854 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003855 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003856 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003857 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003858 if ((opt_flags & OPT_NO_REDRAW) == 0)
3859 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 return errmsg;
3862}
3863
3864/*
3865 * Called after an option changed: check if something needs to be redrawn.
3866 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003868check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003870 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003871 int doclear = (flags & P_RCLR) == P_RCLR;
3872 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003874 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
3877 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3878 changed_window_setting();
3879 if (flags & P_RBUF)
3880 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003881 if (flags & P_RWINONLY)
3882 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003883 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 redraw_all_later(CLEAR);
3885 else if (all)
3886 redraw_all_later(NOT_VALID);
3887}
3888
3889/*
3890 * Find index for option 'arg'.
3891 * Return -1 if not found.
3892 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003894findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 int opt_idx;
3897 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003898 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 int is_term_opt;
3900
3901 /*
3902 * For first call: Initialize the quick-access table.
3903 * It contains the index for the first option that starts with a certain
3904 * letter. There are 26 letters, plus the first "t_" option.
3905 */
3906 if (quick_tab[1] == 0)
3907 {
3908 p = options[0].fullname;
3909 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3910 {
3911 if (s[0] != p[0])
3912 {
3913 if (s[0] == 't' && s[1] == '_')
3914 quick_tab[26] = opt_idx;
3915 else
3916 quick_tab[CharOrdLow(s[0])] = opt_idx;
3917 }
3918 p = s;
3919 }
3920 }
3921
3922 /*
3923 * Check for name starting with an illegal character.
3924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 return -1;
3927
3928 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3929 if (is_term_opt)
3930 opt_idx = quick_tab[26];
3931 else
3932 opt_idx = quick_tab[CharOrdLow(arg[0])];
3933 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003935 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 break;
3937 }
3938 if (s == NULL && !is_term_opt)
3939 {
3940 opt_idx = quick_tab[CharOrdLow(arg[0])];
3941 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3942 {
3943 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003944 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 break;
3946 s = NULL;
3947 }
3948 }
3949 if (s == NULL)
3950 opt_idx = -1;
3951 return opt_idx;
3952}
3953
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003954#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955/*
3956 * Get the value for an option.
3957 *
3958 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003959 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003960 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003961 * String option: gov_string, *stringval gets allocated string.
3962 * Hidden Number option: gov_hidden_number.
3963 * Hidden Toggle option: gov_hidden_bool.
3964 * Hidden String option: gov_hidden_string.
3965 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003966 *
3967 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003969 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003970get_option_value(
3971 char_u *name,
3972 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003973 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003974 int *flagsp,
3975 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
3977 int opt_idx;
3978 char_u *varp;
3979
3980 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003981 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003982 {
3983 int key;
3984
3985 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003986 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003987 {
3988 char_u key_name[2];
3989 char_u *p;
3990
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003991 if (flagsp != NULL)
3992 *flagsp = 0; // terminal option has no flags
3993
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003994 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003995 if (key < 0)
3996 {
3997 key_name[0] = KEY2TERMCAP0(key);
3998 key_name[1] = KEY2TERMCAP1(key);
3999 }
4000 else
4001 {
4002 key_name[0] = KS_KEY;
4003 key_name[1] = (key & 0xff);
4004 }
4005 p = find_termcode(key_name);
4006 if (p != NULL)
4007 {
4008 if (stringval != NULL)
4009 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004010 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004011 }
4012 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004013 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004016 varp = get_varp_scope(&(options[opt_idx]), scope);
4017
4018 if (flagsp != NULL)
4019 // Return the P_xxxx option flags.
4020 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 if (options[opt_idx].flags & P_STRING)
4023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004024 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004025 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 if (stringval != NULL)
4027 {
4028#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004029 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004030 if ((char_u **)varp == &curbuf->b_p_key
4031 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 *stringval = vim_strsave((char_u *)"*****");
4033 else
4034#endif
4035 *stringval = vim_strsave(*(char_u **)(varp));
4036 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004037 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004040 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004041 return (options[opt_idx].flags & P_NUM)
4042 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (options[opt_idx].flags & P_NUM)
4044 *numval = *(long *)varp;
4045 else
4046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004047 // Special case: 'modified' is b_changed, but we also want to consider
4048 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if ((int *)varp == &curbuf->b_changed)
4050 *numval = curbufIsChanged();
4051 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004052 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004054 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055}
4056#endif
4057
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004058#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004059/*
4060 * Returns the option attributes and its value. Unlike the above function it
4061 * will return either global value or local value of the option depending on
4062 * what was requested, but it will never return global value if it was
4063 * requested to return local one and vice versa. Neither it will return
4064 * buffer-local value if it was requested to return window-local one.
4065 *
4066 * Pretends that option is absent if it is not present in the requested scope
4067 * (i.e. has no global, window-local or buffer-local value depending on
4068 * opt_type). Uses
4069 *
4070 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004071 * 0 hidden or unknown option, also option that does not have requested
4072 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004073 * see SOPT_* in vim.h for other flags
4074 *
4075 * Possible opt_type values: see SREQ_* in vim.h
4076 */
4077 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004078get_option_value_strict(
4079 char_u *name,
4080 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004081 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082 int opt_type,
4083 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004084{
4085 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004086 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004087 struct vimoption *p;
4088 int r = 0;
4089
4090 opt_idx = findoption(name);
4091 if (opt_idx < 0)
4092 return 0;
4093
4094 p = &(options[opt_idx]);
4095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004096 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004097 if (p->var == NULL)
4098 return 0;
4099
4100 if (p->flags & P_BOOL)
4101 r |= SOPT_BOOL;
4102 else if (p->flags & P_NUM)
4103 r |= SOPT_NUM;
4104 else if (p->flags & P_STRING)
4105 r |= SOPT_STRING;
4106
4107 if (p->indir == PV_NONE)
4108 {
4109 if (opt_type == SREQ_GLOBAL)
4110 r |= SOPT_GLOBAL;
4111 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004112 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004113 }
4114 else
4115 {
4116 if (p->indir & PV_BOTH)
4117 r |= SOPT_GLOBAL;
4118 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004119 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004120
4121 if (p->indir & PV_WIN)
4122 {
4123 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004124 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004125 else
4126 r |= SOPT_WIN;
4127 }
4128 else if (p->indir & PV_BUF)
4129 {
4130 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004131 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004132 else
4133 r |= SOPT_BUF;
4134 }
4135 }
4136
4137 if (stringval == NULL)
4138 return r;
4139
4140 if (opt_type == SREQ_GLOBAL)
4141 varp = p->var;
4142 else
4143 {
4144 if (opt_type == SREQ_BUF)
4145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004146 // Special case: 'modified' is b_changed, but we also want to
4147 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004148 if (p->indir == PV_MOD)
4149 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004150 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004151 varp = NULL;
4152 }
4153#ifdef FEAT_CRYPT
4154 else if (p->indir == PV_KEY)
4155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004156 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004157 *stringval = NULL;
4158 varp = NULL;
4159 }
4160#endif
4161 else
4162 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004163 buf_T *save_curbuf = curbuf;
4164
4165 // only getting a pointer, no need to use aucmd_prepbuf()
4166 curbuf = (buf_T *)from;
4167 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004168 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004169 curbuf = save_curbuf;
4170 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004171 }
4172 }
4173 else if (opt_type == SREQ_WIN)
4174 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004175 win_T *save_curwin = curwin;
4176
4177 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004178 curbuf = curwin->w_buffer;
4179 varp = get_varp(p);
4180 curwin = save_curwin;
4181 curbuf = curwin->w_buffer;
4182 }
4183 if (varp == p->var)
4184 return (r | SOPT_UNSET);
4185 }
4186
4187 if (varp != NULL)
4188 {
4189 if (p->flags & P_STRING)
4190 *stringval = vim_strsave(*(char_u **)(varp));
4191 else if (p->flags & P_NUM)
4192 *numval = *(long *) varp;
4193 else
4194 *numval = *(int *)varp;
4195 }
4196
4197 return r;
4198}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004199
4200/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004201 * Iterate over options. First argument is a pointer to a pointer to a
4202 * structure inside options[] array, second is option type like in the above
4203 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004204 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004205 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004206 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004207 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004208 * first argument to NULL.
4209 *
4210 * Returns full option name for current option on each call.
4211 */
4212 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004213option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004214{
4215 struct vimoption *ret = NULL;
4216 do
4217 {
4218 if (*option == NULL)
4219 *option = (void *) options;
4220 else if (((struct vimoption *) (*option))->fullname == NULL)
4221 {
4222 *option = NULL;
4223 return NULL;
4224 }
4225 else
4226 *option = (void *) (((struct vimoption *) (*option)) + 1);
4227
4228 ret = ((struct vimoption *) (*option));
4229
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004230 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004231 if (ret->var == NULL)
4232 {
4233 ret = NULL;
4234 continue;
4235 }
4236
4237 switch (opt_type)
4238 {
4239 case SREQ_GLOBAL:
4240 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4241 ret = NULL;
4242 break;
4243 case SREQ_BUF:
4244 if (!(ret->indir & PV_BUF))
4245 ret = NULL;
4246 break;
4247 case SREQ_WIN:
4248 if (!(ret->indir & PV_WIN))
4249 ret = NULL;
4250 break;
4251 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004252 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004253 return NULL;
4254 }
4255 }
4256 while (ret == NULL);
4257
4258 return (char_u *)ret->fullname;
4259}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004260#endif
4261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004263 * Return the flags for the option at 'opt_idx'.
4264 */
4265 long_u
4266get_option_flags(int opt_idx)
4267{
4268 return options[opt_idx].flags;
4269}
4270
4271/*
4272 * Set a flag for the option at 'opt_idx'.
4273 */
4274 void
4275set_option_flag(int opt_idx, long_u flag)
4276{
4277 options[opt_idx].flags |= flag;
4278}
4279
4280/*
4281 * Clear a flag for the option at 'opt_idx'.
4282 */
4283 void
4284clear_option_flag(int opt_idx, long_u flag)
4285{
4286 options[opt_idx].flags &= ~flag;
4287}
4288
4289/*
4290 * Returns TRUE if the option at 'opt_idx' is a global option
4291 */
4292 int
4293is_global_option(int opt_idx)
4294{
4295 return options[opt_idx].indir == PV_NONE;
4296}
4297
4298/*
4299 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4300 * local value.
4301 */
4302 int
4303is_global_local_option(int opt_idx)
4304{
4305 return options[opt_idx].indir & PV_BOTH;
4306}
4307
4308/*
4309 * Returns TRUE if the option at 'opt_idx' is a window-local option
4310 */
4311 int
4312is_window_local_option(int opt_idx)
4313{
4314 return options[opt_idx].var == VAR_WIN;
4315}
4316
4317/*
4318 * Returns TRUE if the option at 'opt_idx' is a hidden option
4319 */
4320 int
4321is_hidden_option(int opt_idx)
4322{
4323 return options[opt_idx].var == NULL;
4324}
4325
4326#if defined(FEAT_CRYPT) || defined(PROTO)
4327/*
4328 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4329 */
4330 int
4331is_crypt_key_option(int opt_idx)
4332{
4333 return options[opt_idx].indir == PV_KEY;
4334}
4335#endif
4336
4337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 * Set the value of option "name".
4339 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004340 *
4341 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004343 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004344set_option_value(
4345 char_u *name,
4346 long number,
4347 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004348 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
4350 int opt_idx;
4351 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004352 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004355 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004356 {
4357 int key;
4358
4359 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004360 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004361 {
4362 char_u key_name[2];
4363
4364 if (key < 0)
4365 {
4366 key_name[0] = KEY2TERMCAP0(key);
4367 key_name[1] = KEY2TERMCAP1(key);
4368 }
4369 else
4370 {
4371 key_name[0] = KS_KEY;
4372 key_name[1] = (key & 0xff);
4373 }
4374 add_termcode(key_name, string, FALSE);
4375 if (full_screen)
4376 ttest(FALSE);
4377 redraw_all_later(CLEAR);
4378 return NULL;
4379 }
4380
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004381 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 else
4384 {
4385 flags = options[opt_idx].flags;
4386#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004387 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004389 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004390 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004391 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004394 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004395 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 else
4397 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004398 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004399 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004401 if (number == 0 && string != NULL)
4402 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004403 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004405 // Either we are given a string or we are setting option
4406 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004407 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004408 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004409 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004410 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004411 // There's another character after zeros or the string
4412 // is empty. In both cases, we are trying to set a
4413 // num option using a string.
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00004414 semsg(_(e_number_required_after_str_equal_str),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004415 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004416 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004417
4418 }
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004421 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004422 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004424 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004425 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427 }
4428 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004429 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430}
4431
4432/*
4433 * Get the terminal code for a terminal option.
4434 * Returns NULL when not found.
4435 */
4436 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004437get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
4439 int opt_idx;
4440 char_u *varp;
4441
4442 if (tname[0] != 't' || tname[1] != '_' ||
4443 tname[2] == NUL || tname[3] == NUL)
4444 return NULL;
4445 if ((opt_idx = findoption(tname)) >= 0)
4446 {
4447 varp = get_varp(&(options[opt_idx]));
4448 if (varp != NULL)
4449 varp = *(char_u **)(varp);
4450 return varp;
4451 }
4452 return find_termcode(tname + 2);
4453}
4454
4455 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004456get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
4458 int i;
4459
4460 i = findoption((char_u *)"hl");
4461 if (i >= 0)
4462 return options[i].def_val[VI_DEFAULT];
4463 return (char_u *)NULL;
4464}
4465
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004466 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004467get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004468{
4469 int i;
4470
4471 i = findoption((char_u *)"enc");
4472 if (i >= 0)
4473 return options[i].def_val[VI_DEFAULT];
4474 return (char_u *)NULL;
4475}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004476
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004477 int
4478is_option_allocated(char *name)
4479{
4480 int idx = findoption((char_u *)name);
4481
4482 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4483}
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485/*
4486 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004487 * When "has_lt" is true there is a '<' before "*arg_arg".
4488 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 */
4490 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004491find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004493 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004495 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 /*
4498 * Don't use get_special_key_code() for t_xx, we don't want it to call
4499 * add_termcap_entry().
4500 */
4501 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4502 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004503 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004507 key = find_special_key(&arg, &modifiers,
4508 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004509 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 key = 0;
4511 }
4512 return key;
4513}
4514
4515/*
4516 * if 'all' == 0: show changed options
4517 * if 'all' == 1: show all normal options
4518 * if 'all' == 2: show all terminal options
4519 */
4520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004521showoptions(
4522 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004523 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525 struct vimoption *p;
4526 int col;
4527 int isterm;
4528 char_u *varp;
4529 struct vimoption **items;
4530 int item_count;
4531 int run;
4532 int row, rows;
4533 int cols;
4534 int i;
4535 int len;
4536
4537#define INC 20
4538#define GAP 3
4539
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004540 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (items == NULL)
4542 return;
4543
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004544 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004546 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004548 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004550 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
4554 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004555 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 * 1. display the short items
4557 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004558 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 */
4560 for (run = 1; run <= 2 && !got_int; ++run)
4561 {
4562 /*
4563 * collect the items in items[]
4564 */
4565 item_count = 0;
4566 for (p = &options[0]; p->fullname != NULL; p++)
4567 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004568 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004569 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004570 continue;
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 varp = NULL;
4573 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004574 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
4576 if (p->indir != PV_NONE && !isterm)
4577 varp = get_varp_scope(p, opt_flags);
4578 }
4579 else
4580 varp = get_varp(p);
4581 if (varp != NULL
4582 && ((all == 2 && isterm)
4583 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004584 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004586 if (opt_flags & OPT_ONECOLUMN)
4587 len = Columns;
4588 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004589 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 else
4591 {
4592 option_value2string(p, opt_flags);
4593 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4594 }
4595 if ((len <= INC - GAP && run == 1) ||
4596 (len > INC - GAP && run == 2))
4597 items[item_count++] = p;
4598 }
4599 }
4600
4601 /*
4602 * display the items
4603 */
4604 if (run == 1)
4605 {
4606 cols = (Columns + GAP - 3) / INC;
4607 if (cols == 0)
4608 cols = 1;
4609 rows = (item_count + cols - 1) / cols;
4610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 rows = item_count;
4613 for (row = 0; row < rows && !got_int; ++row)
4614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 msg_putchar('\n'); // go to next line
4616 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618 col = 0;
4619 for (i = row; i < item_count; i += rows)
4620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004621 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 showoneopt(items[i], opt_flags);
4623 col += INC;
4624 }
4625 out_flush();
4626 ui_breakcheck();
4627 }
4628 }
4629 vim_free(items);
4630}
4631
4632/*
4633 * Return TRUE if option "p" has its default value.
4634 */
4635 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004636optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
4638 int dvi;
4639
4640 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004641 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004642 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004644 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004646 // the cast to long is required for Manx C, long_i is
4647 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004648 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004649 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4651}
4652
4653/*
4654 * showoneopt: show the value of one option
4655 * must not be called with a hidden option!
4656 */
4657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004658showoneopt(
4659 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004660 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004662 char_u *varp;
4663 int save_silent = silent_mode;
4664
4665 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004666 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668 varp = get_varp_scope(p, opt_flags);
4669
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004670 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4672 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004673 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004675 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004677 msg_puts(" ");
4678 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (!(p->flags & P_BOOL))
4680 {
4681 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004682 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 option_value2string(p, opt_flags);
4684 msg_outtrans(NameBuff);
4685 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004686
4687 silent_mode = save_silent;
4688 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689}
4690
4691/*
4692 * Write modified options as ":set" commands to a file.
4693 *
4694 * There are three values for "opt_flags":
4695 * OPT_GLOBAL: Write global option values and fresh values of
4696 * buffer-local options (used for start of a session
4697 * file).
4698 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4699 * curwin (used for a vimrc file).
4700 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4701 * and local values for window-local options of
4702 * curwin. Local values are also written when at the
4703 * default value, because a modeline or autocommand
4704 * may have set them when doing ":edit file" and the
4705 * user has set them back at the default or fresh
4706 * value.
4707 * When "local_only" is TRUE, don't write fresh
4708 * values, only local values (for ":mkview").
4709 * (fresh value = value used for a new buffer or window for a local option).
4710 *
4711 * Return FAIL on error, OK otherwise.
4712 */
4713 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004714makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004717 char_u *varp; // currently used value
4718 char_u *varp_fresh; // local value
4719 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 char *cmd;
4721 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004722 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
4724 /*
4725 * The options that don't have a default (terminal name, columns, lines)
4726 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004727 * Do the loop over "options[]" twice: once for options with the
4728 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004730 for (pri = 1; pri >= 0; --pri)
4731 {
4732 for (p = &options[0]; !istermoption(p); p++)
4733 if (!(p->flags & P_NO_MKRC)
4734 && !istermoption(p)
4735 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004737 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4739 continue;
4740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004741 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4742 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4744 continue;
4745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004746 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004748 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 continue;
4750
Bram Moolenaard23b7142021-04-17 21:04:34 +02004751 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4752 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004753 continue;
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 round = 2;
4756 if (p->indir != PV_NONE)
4757 {
4758 if (p->var == VAR_WIN)
4759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004760 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (!(opt_flags & OPT_LOCAL))
4762 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004763 // When fresh value of window-local option is not at the
4764 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4766 {
4767 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004768 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 round = 1;
4771 varp_local = varp;
4772 varp = varp_fresh;
4773 }
4774 }
4775 }
4776 }
4777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004778 // Round 1: fresh value for window-local options.
4779 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 for ( ; round <= 2; varp = varp_local, ++round)
4781 {
4782 if (round == 1 || (opt_flags & OPT_GLOBAL))
4783 cmd = "set";
4784 else
4785 cmd = "setlocal";
4786
4787 if (p->flags & P_BOOL)
4788 {
4789 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4790 return FAIL;
4791 }
4792 else if (p->flags & P_NUM)
4793 {
4794 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4795 return FAIL;
4796 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004797 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004799 int do_endif = FALSE;
4800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Don't set 'syntax' and 'filetype' again if the value is
4802 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004803 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004804#if defined(FEAT_SYN_HL)
4805 p->indir == PV_SYN ||
4806#endif
4807 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
4809 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4810 *(char_u **)(varp)) < 0
4811 || put_eol(fd) < 0)
4812 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004813 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
4815 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004816 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004818 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
4820 if (put_line(fd, "endif") == FAIL)
4821 return FAIL;
4822 }
4823 }
4824 }
4825 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return OK;
4828}
4829
4830#if defined(FEAT_FOLDING) || defined(PROTO)
4831/*
4832 * Generate set commands for the local fold options only. Used when
4833 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4834 */
4835 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004836makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004838 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004840 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 == FAIL
4842# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004843 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004845 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 == FAIL
4847 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4848 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4849 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4850 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4851 )
4852 return FAIL;
4853
4854 return OK;
4855}
4856#endif
4857
4858 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004859put_setstring(
4860 FILE *fd,
4861 char *cmd,
4862 char *name,
4863 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004864 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865{
4866 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004867 char_u *buf = NULL;
4868 char_u *part = NULL;
4869 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4872 return FAIL;
4873 if (*valuep != NULL)
4874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004875 // Output 'pastetoggle' as key names. For other
4876 // options some characters have to be escaped with
4877 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (valuep == &p_pt)
4879 {
4880 s = *valuep;
4881 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004882 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return FAIL;
4884 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004885 // expand the option value, replace $HOME by ~
4886 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004888 int size = (int)STRLEN(*valuep) + 1;
4889
4890 // replace home directory in the whole option value into "buf"
4891 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004892 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004893 goto fail;
4894 home_replace(NULL, *valuep, buf, size, FALSE);
4895
4896 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004897 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004898 // can be expanded when read back.
4899 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4900 && vim_strchr(*valuep, ',') != NULL)
4901 {
4902 part = alloc(size);
4903 if (part == NULL)
4904 goto fail;
4905
4906 // write line break to clear the option, e.g. ':set rtp='
4907 if (put_eol(fd) == FAIL)
4908 goto fail;
4909
4910 p = buf;
4911 while (*p != NUL)
4912 {
4913 // for each comma separated option part, append value to
4914 // the option, :set rtp+=value
4915 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4916 goto fail;
4917 (void)copy_option_part(&p, part, size, ",");
4918 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4919 goto fail;
4920 }
4921 vim_free(buf);
4922 vim_free(part);
4923 return OK;
4924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004926 {
4927 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004929 }
4930 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else if (put_escstr(fd, *valuep, 2) == FAIL)
4933 return FAIL;
4934 }
4935 if (put_eol(fd) < 0)
4936 return FAIL;
4937 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004938fail:
4939 vim_free(buf);
4940 vim_free(part);
4941 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942}
4943
4944 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004945put_setnum(
4946 FILE *fd,
4947 char *cmd,
4948 char *name,
4949 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 long wc;
4952
4953 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4954 return FAIL;
4955 if (wc_use_keyname((char_u *)valuep, &wc))
4956 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004957 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4959 return FAIL;
4960 }
4961 else if (fprintf(fd, "%ld", *valuep) < 0)
4962 return FAIL;
4963 if (put_eol(fd) < 0)
4964 return FAIL;
4965 return OK;
4966}
4967
4968 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004969put_setbool(
4970 FILE *fd,
4971 char *cmd,
4972 char *name,
4973 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004975 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004976 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4978 || put_eol(fd) < 0)
4979 return FAIL;
4980 return OK;
4981}
4982
4983/*
4984 * Clear all the terminal options.
4985 * If the option has been allocated, free the memory.
4986 * Terminal options are never hidden or indirect.
4987 */
4988 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004989clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 /*
4992 * Reset a few things before clearing the old options. This may cause
4993 * outputting a few things that the terminal doesn't understand, but the
4994 * screen will be cleared later, so this is OK.
4995 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004996 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004997 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 // When starting the GUI close the display opened for the clipboard.
5000 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (gui.starting)
5002 clear_xterm_clip();
5003#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005004 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005006 free_termoptions();
5007}
5008
5009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005010free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005011{
5012 struct vimoption *p;
5013
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005014 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (istermoption(p))
5016 {
5017 if (p->flags & P_ALLOCED)
5018 free_string_option(*(char_u **)(p->var));
5019 if (p->flags & P_DEF_ALLOCED)
5020 free_string_option(p->def_val[VI_DEFAULT]);
5021 *(char_u **)(p->var) = empty_option;
5022 p->def_val[VI_DEFAULT] = empty_option;
5023 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005024#ifdef FEAT_EVAL
5025 // remember where the option was cleared
5026 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 clear_termcodes();
5030}
5031
5032/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005033 * Free the string for one term option, if it was allocated.
5034 * Set the string to empty_option and clear allocated flag.
5035 * "var" points to the option value.
5036 */
5037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005038free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005039{
5040 struct vimoption *p;
5041
5042 for (p = &options[0]; p->fullname != NULL; p++)
5043 if (p->var == var)
5044 {
5045 if (p->flags & P_ALLOCED)
5046 free_string_option(*(char_u **)(p->var));
5047 *(char_u **)(p->var) = empty_option;
5048 p->flags &= ~P_ALLOCED;
5049 break;
5050 }
5051}
5052
5053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 * Set the terminal option defaults to the current value.
5055 * Used after setting the terminal name.
5056 */
5057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005058set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 struct vimoption *p;
5061
5062 for (p = &options[0]; p->fullname != NULL; p++)
5063 {
5064 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5065 {
5066 if (p->flags & P_DEF_ALLOCED)
5067 {
5068 free_string_option(p->def_val[VI_DEFAULT]);
5069 p->flags &= ~P_DEF_ALLOCED;
5070 }
5071 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5072 if (p->flags & P_ALLOCED)
5073 {
5074 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005075 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 }
5078 }
5079}
5080
5081/*
5082 * return TRUE if 'p' starts with 't_'
5083 */
5084 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005085istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5088}
5089
Bram Moolenaardac13472019-09-16 21:06:21 +02005090/*
5091 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5092 */
5093 int
5094istermoption_idx(int opt_idx)
5095{
5096 return istermoption(&options[opt_idx]);
5097}
5098
Bram Moolenaar113e1072019-01-20 15:30:40 +01005099#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005101 * Unset local option value, similar to ":set opt<".
5102 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005103 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005104unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005105{
5106 struct vimoption *p;
5107 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005108 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109
5110 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005111 if (opt_idx < 0)
5112 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113 p = &(options[opt_idx]);
5114
5115 switch ((int)p->indir)
5116 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005117 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005118 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005119 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120 break;
5121 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005122 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005123 break;
5124 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005125 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005126 break;
5127 case PV_AR:
5128 buf->b_p_ar = -1;
5129 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005130 case PV_BKC:
5131 clear_string_option(&buf->b_p_bkc);
5132 buf->b_bkc_flags = 0;
5133 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005134 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005135 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005136 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005137 case PV_TC:
5138 clear_string_option(&buf->b_p_tc);
5139 buf->b_tc_flags = 0;
5140 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005141 case PV_SISO:
5142 curwin->w_p_siso = -1;
5143 break;
5144 case PV_SO:
5145 curwin->w_p_so = -1;
5146 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005147#ifdef FEAT_FIND_ID
5148 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005149 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005150 break;
5151 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005152 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005153 break;
5154#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005155 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005156 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 break;
5158 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005159 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005160 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005161#ifdef FEAT_COMPL_FUNC
5162 case PV_TSRFU:
5163 clear_string_option(&buf->b_p_tsrfu);
5164 break;
5165#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005166 case PV_FP:
5167 clear_string_option(&buf->b_p_fp);
5168 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005169#ifdef FEAT_QUICKFIX
5170 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005171 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005172 break;
5173 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005174 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005175 break;
5176 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005177 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178 break;
5179#endif
5180#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5181 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005182 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005183 break;
5184#endif
5185#if defined(FEAT_CRYPT)
5186 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005187 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005188 break;
5189#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005190#ifdef FEAT_LINEBREAK
5191 case PV_SBR:
5192 clear_string_option(&((win_T *)from)->w_p_sbr);
5193 break;
5194#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005195#ifdef FEAT_STL_OPT
5196 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005197 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005198 break;
5199#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005200 case PV_UL:
5201 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5202 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005203#ifdef FEAT_LISP
5204 case PV_LW:
5205 clear_string_option(&buf->b_p_lw);
5206 break;
5207#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005208 case PV_MENC:
5209 clear_string_option(&buf->b_p_menc);
5210 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005211 case PV_LCS:
5212 clear_string_option(&((win_T *)from)->w_p_lcs);
5213 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5214 redraw_later(NOT_VALID);
5215 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005216 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005217 clear_string_option(&((win_T *)from)->w_p_ve);
5218 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005219 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005220 }
5221}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005222#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005223
5224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005226 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 */
5228 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005229get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005231 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 if (p->var == VAR_WIN)
5234 return (char_u *)GLOBAL_WO(get_varp(p));
5235 return p->var;
5236 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005237 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 switch ((int)p->indir)
5240 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005241 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005243 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5244 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5245 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005247 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5248 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5249 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005250 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005251 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005252 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005253 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5254 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005256 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5257 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005259 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5260 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005261#ifdef FEAT_COMPL_FUNC
5262 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5263#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005264#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5265 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5266#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005267#if defined(FEAT_CRYPT)
5268 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5269#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005270#ifdef FEAT_LINEBREAK
5271 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5272#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005273#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005274 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005275#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005276 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005277#ifdef FEAT_LISP
5278 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5279#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005280 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005281 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005282 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005283 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005284
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005286 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
5288 return get_varp(p);
5289}
5290
5291/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005292 * Get pointer to option variable at 'opt_idx', depending on local or global
5293 * scope.
5294 */
5295 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005296get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005297{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005298 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005299}
5300
5301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 * Get pointer to option variable.
5303 */
5304 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005305get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 if (p->var == NULL)
5309 return NULL;
5310
5311 switch ((int)p->indir)
5312 {
5313 case PV_NONE: return p->var;
5314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005315 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005316 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005318 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005320 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005322 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005324 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005326 case PV_TC: return *curbuf->b_p_tc != NUL
5327 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005328 case PV_BKC: return *curbuf->b_p_bkc != NUL
5329 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005330 case PV_SISO: return curwin->w_p_siso >= 0
5331 ? (char_u *)&(curwin->w_p_siso) : p->var;
5332 case PV_SO: return curwin->w_p_so >= 0
5333 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005335 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005337 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5339#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005340 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005342 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005344#ifdef FEAT_COMPL_FUNC
5345 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5346 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5347#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005348 case PV_FP: return *curbuf->b_p_fp != NUL
5349 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005351 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005353 case PV_GP: return *curbuf->b_p_gp != NUL
5354 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5355 case PV_MP: return *curbuf->b_p_mp != NUL
5356 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005358#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5359 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5360 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5361#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005362#if defined(FEAT_CRYPT)
5363 case PV_CM: return *curbuf->b_p_cm != NUL
5364 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5365#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005366#ifdef FEAT_LINEBREAK
5367 case PV_SBR: return *curwin->w_p_sbr != NUL
5368 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5369#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005370#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005371 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005372 ? (char_u *)&(curwin->w_p_stl) : p->var;
5373#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005374 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5375 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005376#ifdef FEAT_LISP
5377 case PV_LW: return *curbuf->b_p_lw != NUL
5378 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5379#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005380 case PV_MENC: return *curbuf->b_p_menc != NUL
5381 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382#ifdef FEAT_ARABIC
5383 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5384#endif
5385 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005386 case PV_LCS: return *curwin->w_p_lcs != NUL
5387 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005388 case PV_VE: return *curwin->w_p_ve != NUL
5389 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005390#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005391 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5392#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005393#ifdef FEAT_SYN_HL
5394 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5395 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005396 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005397 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#ifdef FEAT_DIFF
5400 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5401#endif
5402#ifdef FEAT_FOLDING
5403 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5404 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5405 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5406 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5407 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5408 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5409 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5410# ifdef FEAT_EVAL
5411 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5412 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5413# endif
5414 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5415#endif
5416 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005417 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005418#ifdef FEAT_LINEBREAK
5419 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005422 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005423#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5425#endif
5426#ifdef FEAT_RIGHTLEFT
5427 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5428 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5429#endif
5430 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5431 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5432#ifdef FEAT_LINEBREAK
5433 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005434 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5435 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005437 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005439 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005440#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005441 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5442 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5443#endif
5444#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005445 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5446 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5447 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5451 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5454 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5456 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5457#ifdef FEAT_CINDENT
5458 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5459 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5460 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5461#endif
5462#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5463 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#ifdef FEAT_FOLDING
5467 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005470#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005471 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005473#ifdef FEAT_COMPL_FUNC
5474 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005475 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005476#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005477#ifdef FEAT_EVAL
5478 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005481 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005487 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5489 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5490 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5491 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5492#ifdef FEAT_FIND_ID
5493# ifdef FEAT_EVAL
5494 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5495# endif
5496#endif
5497#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5498 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5499 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5500#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005501#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005502 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504#ifdef FEAT_CRYPT
5505 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5506#endif
5507#ifdef FEAT_LISP
5508 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5509#endif
5510 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5511 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5512 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5513 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5514 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005516#ifdef FEAT_TEXTOBJ
5517 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5520#ifdef FEAT_SMARTINDENT
5521 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5525#ifdef FEAT_SEARCHPATH
5526 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5527#endif
5528 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5529#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005530 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005531 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5532#endif
5533#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005534 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5535 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5536 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005537 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538#endif
5539 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5540 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5541 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5542 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005543#ifdef FEAT_PERSISTENT_UNDO
5544 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5547#ifdef FEAT_KEYMAP
5548 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5549#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005550#ifdef FEAT_SIGNS
5551 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5552#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005553#ifdef FEAT_VARTABS
5554 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5555 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5556#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005557 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005559 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 return (char_u *)&(curbuf->b_p_wm);
5561}
5562
5563/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005564 * Return a pointer to the variable for option at 'opt_idx'
5565 */
5566 char_u *
5567get_option_var(int opt_idx)
5568{
5569 return options[opt_idx].var;
5570}
5571
Dominique Pelle748b3082022-01-08 12:41:16 +00005572#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005573/*
5574 * Return the full name of the option at 'opt_idx'
5575 */
5576 char_u *
5577get_option_fullname(int opt_idx)
5578{
5579 return (char_u *)options[opt_idx].fullname;
5580}
Dominique Pelle748b3082022-01-08 12:41:16 +00005581#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005582
5583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 * Get the value of 'equalprg', either the buffer-local one or the global one.
5585 */
5586 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005587get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
5589 if (*curbuf->b_p_ep == NUL)
5590 return p_ep;
5591 return curbuf->b_p_ep;
5592}
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594/*
5595 * Copy options from one window to another.
5596 * Used when splitting a window.
5597 */
5598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005599win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5602 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005603 after_copy_winopt(wp_to);
5604}
5605
5606/*
5607 * After copying window options: update variables depending on options.
5608 */
5609 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005610after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005611{
5612#ifdef FEAT_LINEBREAK
5613 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005614#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005615#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005616 fill_culopt_flags(NULL, wp);
5617 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005618#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005619 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622/*
5623 * Copy the options from one winopt_T to another.
5624 * Doesn't free the old option values in "to", use clear_winopt() for that.
5625 * The 'scroll' option is not copied, because it depends on the window height.
5626 * The 'previewwindow' option is reset, there can be only one preview window.
5627 */
5628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005629copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631#ifdef FEAT_ARABIC
5632 to->wo_arab = from->wo_arab;
5633#endif
5634 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005635 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005637 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005638 to->wo_ve = vim_strsave(from->wo_ve);
5639 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005640#ifdef FEAT_LINEBREAK
5641 to->wo_nuw = from->wo_nuw;
5642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643#ifdef FEAT_RIGHTLEFT
5644 to->wo_rl = from->wo_rl;
5645 to->wo_rlc = vim_strsave(from->wo_rlc);
5646#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005647#ifdef FEAT_LINEBREAK
5648 to->wo_sbr = vim_strsave(from->wo_sbr);
5649#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005650#ifdef FEAT_STL_OPT
5651 to->wo_stl = vim_strsave(from->wo_stl);
5652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005654#ifdef FEAT_DIFF
5655 to->wo_wrap_save = from->wo_wrap_save;
5656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657#ifdef FEAT_LINEBREAK
5658 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005659 to->wo_bri = from->wo_bri;
5660 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005662 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005664 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005665 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005666 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005667#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005668 to->wo_spell = from->wo_spell;
5669#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005670#ifdef FEAT_SYN_HL
5671 to->wo_cuc = from->wo_cuc;
5672 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005673 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005674 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676#ifdef FEAT_DIFF
5677 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005678 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005680#ifdef FEAT_CONCEAL
5681 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005682 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005683#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005684#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005685 to->wo_twk = vim_strsave(from->wo_twk);
5686 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688#ifdef FEAT_FOLDING
5689 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005690 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005692 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 to->wo_fdi = vim_strsave(from->wo_fdi);
5694 to->wo_fml = from->wo_fml;
5695 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005696 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005698 to->wo_fdm_save = from->wo_diff_saved
5699 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 to->wo_fdn = from->wo_fdn;
5701# ifdef FEAT_EVAL
5702 to->wo_fde = vim_strsave(from->wo_fde);
5703 to->wo_fdt = vim_strsave(from->wo_fdt);
5704# endif
5705 to->wo_fmr = vim_strsave(from->wo_fmr);
5706#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005707#ifdef FEAT_SIGNS
5708 to->wo_scl = vim_strsave(from->wo_scl);
5709#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005710
5711#ifdef FEAT_EVAL
5712 // Copy the script context so that we know where the value was last set.
5713 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5714 sizeof(to->wo_script_ctx));
5715#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005716 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717}
5718
5719/*
5720 * Check string options in a window for a NULL value.
5721 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005723check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 check_winopt(&win->w_onebuf_opt);
5726 check_winopt(&win->w_allbuf_opt);
5727}
5728
5729/*
5730 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5731 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005733check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735#ifdef FEAT_FOLDING
5736 check_string_option(&wop->wo_fdi);
5737 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005738 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739# ifdef FEAT_EVAL
5740 check_string_option(&wop->wo_fde);
5741 check_string_option(&wop->wo_fdt);
5742# endif
5743 check_string_option(&wop->wo_fmr);
5744#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005745#ifdef FEAT_SIGNS
5746 check_string_option(&wop->wo_scl);
5747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748#ifdef FEAT_RIGHTLEFT
5749 check_string_option(&wop->wo_rlc);
5750#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005751#ifdef FEAT_LINEBREAK
5752 check_string_option(&wop->wo_sbr);
5753#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005754#ifdef FEAT_STL_OPT
5755 check_string_option(&wop->wo_stl);
5756#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005757#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005758 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005759 check_string_option(&wop->wo_cc);
5760#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005761#ifdef FEAT_CONCEAL
5762 check_string_option(&wop->wo_cocu);
5763#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005764#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005765 check_string_option(&wop->wo_twk);
5766 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005767#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005768#ifdef FEAT_LINEBREAK
5769 check_string_option(&wop->wo_briopt);
5770#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005771 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005772 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005773 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774}
5775
5776/*
5777 * Free the allocated memory inside a winopt_T.
5778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005780clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782#ifdef FEAT_FOLDING
5783 clear_string_option(&wop->wo_fdi);
5784 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005785 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786# ifdef FEAT_EVAL
5787 clear_string_option(&wop->wo_fde);
5788 clear_string_option(&wop->wo_fdt);
5789# endif
5790 clear_string_option(&wop->wo_fmr);
5791#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005792#ifdef FEAT_SIGNS
5793 clear_string_option(&wop->wo_scl);
5794#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005795#ifdef FEAT_LINEBREAK
5796 clear_string_option(&wop->wo_briopt);
5797#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005798 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799#ifdef FEAT_RIGHTLEFT
5800 clear_string_option(&wop->wo_rlc);
5801#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005802#ifdef FEAT_LINEBREAK
5803 clear_string_option(&wop->wo_sbr);
5804#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005805#ifdef FEAT_STL_OPT
5806 clear_string_option(&wop->wo_stl);
5807#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005808#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005809 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005810 clear_string_option(&wop->wo_cc);
5811#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005812#ifdef FEAT_CONCEAL
5813 clear_string_option(&wop->wo_cocu);
5814#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005815#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005816 clear_string_option(&wop->wo_twk);
5817 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005818#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005819 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005820 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821}
5822
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005823#ifdef FEAT_EVAL
5824// Index into the options table for a buffer-local option enum.
5825static int buf_opt_idx[BV_COUNT];
5826# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5827
5828/*
5829 * Initialize buf_opt_idx[] if not done already.
5830 */
5831 static void
5832init_buf_opt_idx(void)
5833{
5834 static int did_init_buf_opt_idx = FALSE;
5835 int i;
5836
5837 if (did_init_buf_opt_idx)
5838 return;
5839 did_init_buf_opt_idx = TRUE;
5840 for (i = 0; !istermoption_idx(i); i++)
5841 if (options[i].indir & PV_BUF)
5842 buf_opt_idx[options[i].indir & PV_MASK] = i;
5843}
5844#else
5845# define COPY_OPT_SCTX(buf, bv)
5846#endif
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848/*
5849 * Copy global option values to local options for one buffer.
5850 * Used when creating a new buffer and sometimes when entering a buffer.
5851 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005852 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5854 * appropriate.
5855 * BCO_NOHELP Don't copy the values to a help buffer.
5856 */
5857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005858buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005861 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 int dont_do_help;
5863 int did_isk = FALSE;
5864
5865 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 * Skip this when the option defaults have not been set yet. Happens when
5867 * main() allocates the first buffer.
5868 */
5869 if (p_cpo != NULL)
5870 {
5871 /*
5872 * Always copy when entering and 'cpo' contains 'S'.
5873 * Don't copy when already initialized.
5874 * Don't copy when 'cpo' contains 's' and not entering.
5875 * 'S' BCO_ENTER initialized 's' should_copy
5876 * yes yes X X TRUE
5877 * yes no yes X FALSE
5878 * no X yes X FALSE
5879 * X no no yes FALSE
5880 * X no no no TRUE
5881 * no yes no X TRUE
5882 */
5883 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5884 && (buf->b_p_initialized
5885 || (!(flags & BCO_ENTER)
5886 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5887 should_copy = FALSE;
5888
5889 if (should_copy || (flags & BCO_ALWAYS))
5890 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005891#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005892 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005893 init_buf_opt_idx();
5894#endif
5895 // Don't copy the options specific to a help buffer when
5896 // BCO_NOHELP is given or the options were initialized already
5897 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5899 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {
5902 save_p_isk = buf->b_p_isk;
5903 buf->b_p_isk = NULL;
5904 }
5905 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005906 * Always free the allocated strings. If not already initialized,
5907 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 */
5909 if (!buf->b_p_initialized)
5910 {
5911 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005912 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005915 switch (*p_ffs)
5916 {
5917 case 'm':
5918 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5919 case 'd':
5920 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5921 case 'u':
5922 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5923 default:
5924 buf->b_p_ff = vim_strsave(p_ff);
5925 }
5926 if (buf->b_p_ff != NULL)
5927 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 buf->b_p_bh = empty_option;
5929 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931 else
5932 free_buf_options(buf, FALSE);
5933
5934 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005935 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 buf->b_p_ai_nopaste = p_ai_nopaste;
5937 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005938 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 buf->b_p_tw_nopaste = p_tw_nopaste;
5942 buf->b_p_tw_nobin = p_tw_nobin;
5943 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 buf->b_p_wm_nopaste = p_wm_nopaste;
5946 buf->b_p_wm_nobin = p_wm_nobin;
5947 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005948 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005949 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005950 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005951 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005952 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005954 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005956 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005958 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 buf->b_p_ml_nobin = p_ml_nobin;
5960 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005961 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005962 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 buf->b_p_swf = FALSE;
5964 else
5965 {
5966 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005967 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005971#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005972 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005973 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005975#ifdef FEAT_COMPL_FUNC
5976 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005978 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005979 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005980 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005981 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005982#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005983#ifdef FEAT_EVAL
5984 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005985 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005986 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005989 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005991#ifdef FEAT_VARTABS
5992 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005993 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005994 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005995 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005996 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00005997 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005998 buf->b_p_vsts_nopaste = p_vsts_nopaste
5999 ? vim_strsave(p_vsts_nopaste) : NULL;
6000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006002 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006004 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005#ifdef FEAT_FOLDING
6006 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#endif
6009 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006010 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006011 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006012 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006013 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6014 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006016 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006018 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019#ifdef FEAT_SMARTINDENT
6020 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022#endif
6023 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006024 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025#ifdef FEAT_CINDENT
6026 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006027 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006029 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6038 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006039 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040#endif
6041#ifdef FEAT_LISP
6042 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006043 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044#endif
6045#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006046 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006048 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006049 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006050 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006051#endif
6052#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006053 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006054 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006055 (void)compile_cap_prog(&buf->b_s);
6056 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006057 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006058 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006059 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006060 buf->b_s.b_p_spo = vim_strsave(p_spo);
6061 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
6063#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6064 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006065 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006067 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006069 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006070#if defined(FEAT_EVAL)
6071 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006072 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074#ifdef FEAT_CRYPT
6075 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006076 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077#endif
6078#ifdef FEAT_SEARCHPATH
6079 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006080 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081#endif
6082#ifdef FEAT_KEYMAP
6083 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006084 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 buf->b_kmap_state |= KEYMAP_INIT;
6086#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006087#ifdef FEAT_TERMINAL
6088 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006089 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006090#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006091 // This isn't really an option, but copying the langmap and IME
6092 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006094 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006096 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006098 // options that are normally global but also have a local value
6099 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006101 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006102 buf->b_p_bkc = empty_option;
6103 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#ifdef FEAT_QUICKFIX
6105 buf->b_p_gp = empty_option;
6106 buf->b_p_mp = empty_option;
6107 buf->b_p_efm = empty_option;
6108#endif
6109 buf->b_p_ep = empty_option;
6110 buf->b_p_kp = empty_option;
6111 buf->b_p_path = empty_option;
6112 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006113 buf->b_p_tc = empty_option;
6114 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115#ifdef FEAT_FIND_ID
6116 buf->b_p_def = empty_option;
6117 buf->b_p_inc = empty_option;
6118# ifdef FEAT_EVAL
6119 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006120 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121# endif
6122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 buf->b_p_dict = empty_option;
6124 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006125#ifdef FEAT_COMPL_FUNC
6126 buf->b_p_tsrfu = empty_option;
6127#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006128#ifdef FEAT_TEXTOBJ
6129 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006130 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006131#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006132#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6133 buf->b_p_bexpr = empty_option;
6134#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006135#if defined(FEAT_CRYPT)
6136 buf->b_p_cm = empty_option;
6137#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006138#ifdef FEAT_PERSISTENT_UNDO
6139 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006140 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006141#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006142#ifdef FEAT_LISP
6143 buf->b_p_lw = empty_option;
6144#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006145 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 /*
6148 * Don't copy the options set by ex_help(), use the saved values,
6149 * when going from a help buffer to a non-help buffer.
6150 * Don't touch these at all when BCO_NOHELP is used and going from
6151 * or to a help buffer.
6152 */
6153 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006156#ifdef FEAT_VARTABS
6157 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006158 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006159 else
6160 buf->b_p_vts_array = NULL;
6161#endif
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 else
6164 {
6165 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006166 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 did_isk = TRUE;
6168 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006169 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006170#ifdef FEAT_VARTABS
6171 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006172 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006173 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006174 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006175 else
6176 buf->b_p_vts_array = NULL;
6177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (buf->b_p_bt[0] == 'h')
6180 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006182 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184 }
6185
6186 /*
6187 * When the options should be copied (ignoring BCO_ALWAYS), set the
6188 * flag that indicates that the options have been initialized.
6189 */
6190 if (should_copy)
6191 buf->b_p_initialized = TRUE;
6192 }
6193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006194 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (did_isk)
6196 (void)buf_init_chartab(buf, FALSE);
6197}
6198
6199/*
6200 * Reset the 'modifiable' option and its default value.
6201 */
6202 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006203reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
6205 int opt_idx;
6206
6207 curbuf->b_p_ma = FALSE;
6208 p_ma = FALSE;
6209 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006210 if (opt_idx >= 0)
6211 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212}
6213
6214/*
6215 * Set the global value for 'iminsert' to the local value.
6216 */
6217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006218set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220 p_iminsert = curbuf->b_p_iminsert;
6221}
6222
6223/*
6224 * Set the global value for 'imsearch' to the local value.
6225 */
6226 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006227set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 p_imsearch = curbuf->b_p_imsearch;
6230}
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232static int expand_option_idx = -1;
6233static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6234static int expand_option_flags = 0;
6235
6236 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006237set_context_in_set_cmd(
6238 expand_T *xp,
6239 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006240 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006243 long_u flags = 0; // init for GCC
6244 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 char_u *p;
6246 char_u *s;
6247 int is_term_option = FALSE;
6248 int key;
6249
6250 expand_option_flags = opt_flags;
6251
6252 xp->xp_context = EXPAND_SETTINGS;
6253 if (*arg == NUL)
6254 {
6255 xp->xp_pattern = arg;
6256 return;
6257 }
6258 p = arg + STRLEN(arg) - 1;
6259 if (*p == ' ' && *(p - 1) != '\\')
6260 {
6261 xp->xp_pattern = p + 1;
6262 return;
6263 }
6264 while (p > arg)
6265 {
6266 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006267 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (*p == ' ' || *p == ',')
6269 {
6270 while (s > arg && *(s - 1) == '\\')
6271 --s;
6272 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006273 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (*p == ' ' && ((p - s) & 1) == 0)
6275 {
6276 ++p;
6277 break;
6278 }
6279 --p;
6280 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006281 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
6283 xp->xp_context = EXPAND_BOOL_SETTINGS;
6284 p += 2;
6285 }
6286 if (STRNCMP(p, "inv", 3) == 0)
6287 {
6288 xp->xp_context = EXPAND_BOOL_SETTINGS;
6289 p += 3;
6290 }
6291 xp->xp_pattern = arg = p;
6292 if (*arg == '<')
6293 {
6294 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006295 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 return;
6297 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006298 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
6300 xp->xp_context = EXPAND_NOTHING;
6301 return;
6302 }
6303 nextchar = *++p;
6304 is_term_option = TRUE;
6305 expand_option_name[2] = KEY2TERMCAP0(key);
6306 expand_option_name[3] = KEY2TERMCAP1(key);
6307 }
6308 else
6309 {
6310 if (p[0] == 't' && p[1] == '_')
6311 {
6312 p += 2;
6313 if (*p != NUL)
6314 ++p;
6315 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006316 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 nextchar = *++p;
6318 is_term_option = TRUE;
6319 expand_option_name[2] = p[-2];
6320 expand_option_name[3] = p[-1];
6321 }
6322 else
6323 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006324 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6326 p++;
6327 if (*p == NUL)
6328 return;
6329 nextchar = *p;
6330 *p = NUL;
6331 opt_idx = findoption(arg);
6332 *p = nextchar;
6333 if (opt_idx == -1 || options[opt_idx].var == NULL)
6334 {
6335 xp->xp_context = EXPAND_NOTHING;
6336 return;
6337 }
6338 flags = options[opt_idx].flags;
6339 if (flags & P_BOOL)
6340 {
6341 xp->xp_context = EXPAND_NOTHING;
6342 return;
6343 }
6344 }
6345 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006346 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6348 {
6349 ++p;
6350 nextchar = '=';
6351 }
6352 if ((nextchar != '=' && nextchar != ':')
6353 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6354 {
6355 xp->xp_context = EXPAND_UNSUCCESSFUL;
6356 return;
6357 }
6358 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6359 {
6360 xp->xp_context = EXPAND_OLD_SETTING;
6361 if (is_term_option)
6362 expand_option_idx = -1;
6363 else
6364 expand_option_idx = opt_idx;
6365 xp->xp_pattern = p + 1;
6366 return;
6367 }
6368 xp->xp_context = EXPAND_NOTHING;
6369 if (is_term_option || (flags & P_NUM))
6370 return;
6371
6372 xp->xp_pattern = p + 1;
6373
6374 if (flags & P_EXPAND)
6375 {
6376 p = options[opt_idx].var;
6377 if (p == (char_u *)&p_bdir
6378 || p == (char_u *)&p_dir
6379 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006380 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 || p == (char_u *)&p_rtp
6382#ifdef FEAT_SEARCHPATH
6383 || p == (char_u *)&p_cdpath
6384#endif
6385#ifdef FEAT_SESSION
6386 || p == (char_u *)&p_vdir
6387#endif
6388 )
6389 {
6390 xp->xp_context = EXPAND_DIRECTORIES;
6391 if (p == (char_u *)&p_path
6392#ifdef FEAT_SEARCHPATH
6393 || p == (char_u *)&p_cdpath
6394#endif
6395 )
6396 xp->xp_backslash = XP_BS_THREE;
6397 else
6398 xp->xp_backslash = XP_BS_ONE;
6399 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006400 else if (p == (char_u *)&p_ft)
6401 {
6402 xp->xp_context = EXPAND_FILETYPE;
6403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 else
6405 {
6406 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006407 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (p == (char_u *)&p_tags)
6409 xp->xp_backslash = XP_BS_THREE;
6410 else
6411 xp->xp_backslash = XP_BS_ONE;
6412 }
6413 }
6414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006415 // For an option that is a list of file names, find the start of the
6416 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6418 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006419 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (*p == ' ' || *p == ',')
6421 {
6422 s = p;
6423 while (s > xp->xp_pattern && *(s - 1) == '\\')
6424 --s;
6425 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6426 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6427 {
6428 xp->xp_pattern = p + 1;
6429 break;
6430 }
6431 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006432
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006433#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006434 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006435 if (options[opt_idx].var == (char_u *)&p_sps
6436 && STRNCMP(p, "file:", 5) == 0)
6437 {
6438 xp->xp_pattern = p + 5;
6439 break;
6440 }
6441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443}
6444
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006445/*
6446 * Returns TRUE if 'str' either matches 'regmatch' or fuzzy matches 'pat'.
6447 *
6448 * If 'test_only' is TRUE and 'fuzzy' is FALSE and if 'str' matches the regular
6449 * expression 'regmatch', then returns TRUE. Otherwise returns FALSE.
6450 *
6451 * If 'test_only' is FALSE and 'fuzzy' is FALSE and if 'str' matches the
6452 * regular expression 'regmatch', then stores the match in matches[idx] and
6453 * returns TRUE.
6454 *
6455 * If 'test_only' is TRUE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6456 * 'fuzzystr', then returns TRUE. Otherwise returns FALSE.
6457 *
6458 * If 'test_only' is FALSE and 'fuzzy' is TRUE and if 'str' fuzzy matches
6459 * 'fuzzystr', then stores the match details in fuzmatch[idx] and returns TRUE.
6460 */
6461 static int
6462match_str(
6463 char_u *str,
6464 regmatch_T *regmatch,
6465 char_u **matches,
6466 int idx,
6467 int test_only,
6468 int fuzzy,
6469 char_u *fuzzystr,
6470 fuzmatch_str_T *fuzmatch)
6471{
6472 if (!fuzzy)
6473 {
6474 if (vim_regexec(regmatch, str, (colnr_T)0))
6475 {
6476 if (!test_only)
6477 matches[idx] = vim_strsave(str);
6478 return TRUE;
6479 }
6480 }
6481 else
6482 {
6483 int score;
6484
6485 score = fuzzy_match_str(str, fuzzystr);
6486 if (score != 0)
6487 {
6488 if (!test_only)
6489 {
6490 fuzmatch[idx].idx = idx;
6491 fuzmatch[idx].str = vim_strsave(str);
6492 fuzmatch[idx].score = score;
6493 }
6494
6495 return TRUE;
6496 }
6497 }
6498
6499 return FALSE;
6500}
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006503ExpandSettings(
6504 expand_T *xp,
6505 regmatch_T *regmatch,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006506 char_u *fuzzystr,
6507 int *numMatches,
6508 char_u ***matches)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006510 int num_normal = 0; // Nr of matching non-term-code settings
6511 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 int opt_idx;
6513 int match;
6514 int count = 0;
6515 char_u *str;
6516 int loop;
6517 int is_term_opt;
6518 char_u name_buf[MAX_KEY_NAME_LEN];
6519 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006520 int ic = regmatch->rm_ic; // remember the ignore-case flag
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006521 int fuzzy;
6522 fuzmatch_str_T *fuzmatch = NULL;
6523
6524 fuzzy = cmdline_fuzzy_complete(fuzzystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006526 // do this loop twice:
6527 // loop == 0: count the number of matching options
6528 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 for (loop = 0; loop <= 1; ++loop)
6530 {
6531 regmatch->rm_ic = ic;
6532 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6533 {
K.Takataeeec2542021-06-02 13:28:16 +02006534 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006535 {
6536 if (match_str((char_u *)names[match], regmatch, *matches,
6537 count, (loop == 0), fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
6539 if (loop == 0)
6540 num_normal++;
6541 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006542 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6547 opt_idx++)
6548 {
6549 if (options[opt_idx].var == NULL)
6550 continue;
6551 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6552 && !(options[opt_idx].flags & P_BOOL))
6553 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006554 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (is_term_opt && num_normal > 0)
6556 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006557
6558 if (match_str(str, regmatch, *matches, count, (loop == 0),
6559 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
6561 if (loop == 0)
6562 {
6563 if (is_term_opt)
6564 num_term++;
6565 else
6566 num_normal++;
6567 }
6568 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006569 count++;
6570 }
6571 else if (!fuzzy && options[opt_idx].shortname != NULL
6572 && vim_regexec(regmatch,
6573 (char_u *)options[opt_idx].shortname, (colnr_T)0))
6574 {
6575 // Compare against the abbreviated option name (for regular
6576 // expression match). Fuzzy matching (previous if) already
6577 // matches against both the expanded and abbreviated names.
6578 if (loop == 0)
6579 {
6580 if (is_term_opt)
6581 num_term++;
6582 else
6583 num_normal++;
6584 }
6585 else
6586 (*matches)[count++] = vim_strsave(str);
6587 }
6588 else if (is_term_opt)
6589 {
6590 name_buf[0] = '<';
6591 name_buf[1] = 't';
6592 name_buf[2] = '_';
6593 name_buf[3] = str[2];
6594 name_buf[4] = str[3];
6595 name_buf[5] = '>';
6596 name_buf[6] = NUL;
6597
6598 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6599 fuzzy, fuzzystr, fuzmatch))
6600 {
6601 if (loop == 0)
6602 num_term++;
6603 else
6604 count++;
6605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
6607 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006608
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 /*
6610 * Check terminal key codes, these are not in the option table
6611 */
6612 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6613 {
6614 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6615 {
6616 if (!isprint(str[0]) || !isprint(str[1]))
6617 continue;
6618
6619 name_buf[0] = 't';
6620 name_buf[1] = '_';
6621 name_buf[2] = str[0];
6622 name_buf[3] = str[1];
6623 name_buf[4] = NUL;
6624
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006625 if (match_str(name_buf, regmatch, *matches, count,
6626 (loop == 0), fuzzy, fuzzystr, fuzmatch))
6627 {
6628 if (loop == 0)
6629 num_term++;
6630 else
6631 count++;
6632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 else
6634 {
6635 name_buf[0] = '<';
6636 name_buf[1] = 't';
6637 name_buf[2] = '_';
6638 name_buf[3] = str[0];
6639 name_buf[4] = str[1];
6640 name_buf[5] = '>';
6641 name_buf[6] = NUL;
6642
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006643 if (match_str(name_buf, regmatch, *matches, count,
6644 (loop == 0), fuzzy, fuzzystr,
6645 fuzmatch))
6646 {
6647 if (loop == 0)
6648 num_term++;
6649 else
6650 count++;
6651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 }
6653 }
6654
6655 /*
6656 * Check special key names.
6657 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006658 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6660 {
6661 name_buf[0] = '<';
6662 STRCPY(name_buf + 1, str);
6663 STRCAT(name_buf, ">");
6664
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006665 if (match_str(name_buf, regmatch, *matches, count, (loop == 0),
6666 fuzzy, fuzzystr, fuzmatch))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 if (loop == 0)
6669 num_term++;
6670 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006671 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 }
6674 }
6675 if (loop == 0)
6676 {
6677 if (num_normal > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006678 *numMatches = num_normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 else if (num_term > 0)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006680 *numMatches = num_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 else
6682 return OK;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006683 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006685 *matches = ALLOC_MULT(char_u *, *numMatches);
6686 if (*matches == NULL)
6687 {
6688 *matches = (char_u **)"";
6689 return FAIL;
6690 }
6691 }
6692 else
6693 {
6694 fuzmatch = ALLOC_MULT(fuzmatch_str_T, *numMatches);
6695 if (fuzmatch == NULL)
6696 {
6697 *matches = (char_u **)"";
6698 return FAIL;
6699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701 }
6702 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00006703
6704 if (fuzzy &&
6705 fuzzymatches_to_strmatches(fuzmatch, matches, count, FALSE) == FAIL)
6706 return FAIL;
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 return OK;
6709}
6710
6711 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006712ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006714 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 char_u *buf;
6716
6717 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006718 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (*file == NULL)
6720 return FAIL;
6721
6722 /*
6723 * For a terminal key code expand_option_idx is < 0.
6724 */
6725 if (expand_option_idx < 0)
6726 {
6727 var = find_termcode(expand_option_name + 2);
6728 if (var == NULL)
6729 expand_option_idx = findoption(expand_option_name);
6730 }
6731
6732 if (expand_option_idx >= 0)
6733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006734 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 option_value2string(&options[expand_option_idx], expand_option_flags);
6736 var = NameBuff;
6737 }
6738 else if (var == NULL)
6739 var = (char_u *)"";
6740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006741 // A backslash is required before some characters. This is the reverse of
6742 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 buf = vim_strsave_escaped(var, escape_chars);
6744
6745 if (buf == NULL)
6746 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006747 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 return FAIL;
6749 }
6750
6751#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006752 // For MS-Windows et al. we don't double backslashes at the start and
6753 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006754 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (var[0] == '\\' && var[1] == '\\'
6756 && expand_option_idx >= 0
6757 && (options[expand_option_idx].flags & P_EXPAND)
6758 && vim_isfilec(var[2])
6759 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006760 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761#endif
6762
6763 *file[0] = buf;
6764 *num_file = 1;
6765 return OK;
6766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
6768/*
6769 * Get the value for the numeric or string option *opp in a nice format into
6770 * NameBuff[]. Must not be called with a hidden option!
6771 */
6772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006773option_value2string(
6774 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006775 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 char_u *varp;
6778
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006779 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
6781 if (opp->flags & P_NUM)
6782 {
6783 long wc = 0;
6784
6785 if (wc_use_keyname(varp, &wc))
6786 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6787 else if (wc != 0)
6788 STRCPY(NameBuff, transchar((int)wc));
6789 else
6790 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6791 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006792 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006795 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 NameBuff[0] = NUL;
6797#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006798 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006799 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 STRCPY(NameBuff, "*****");
6801#endif
6802 else if (opp->flags & P_EXPAND)
6803 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006804 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 else if ((char_u **)opp->var == &p_pt)
6806 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6807 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006808 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810}
6811
6812/*
6813 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6814 * printed as a keyname.
6815 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6816 */
6817 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006818wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
6820 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6821 {
6822 *wcp = *(long *)varp;
6823 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6824 return TRUE;
6825 }
6826 return FALSE;
6827}
6828
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 * Return TRUE if "x" is present in 'shortmess' option, or
6831 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6832 */
6833 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006834shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006836 return p_shm != NULL &&
6837 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 || (vim_strchr(p_shm, 'a') != NULL
6839 && vim_strchr((char_u *)SHM_A, x) != NULL));
6840}
6841
6842/*
6843 * paste_option_changed() - Called after p_paste was set or reset.
6844 */
6845 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006846paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 static int old_p_paste = FALSE;
6849 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006850 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851#ifdef FEAT_CMDL_INFO
6852 static int save_ru = 0;
6853#endif
6854#ifdef FEAT_RIGHTLEFT
6855 static int save_ri = 0;
6856 static int save_hkmap = 0;
6857#endif
6858 buf_T *buf;
6859
6860 if (p_paste)
6861 {
6862 /*
6863 * Paste switched from off to on.
6864 * Save the current values, so they can be restored later.
6865 */
6866 if (!old_p_paste)
6867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006869 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 {
6871 buf->b_p_tw_nopaste = buf->b_p_tw;
6872 buf->b_p_wm_nopaste = buf->b_p_wm;
6873 buf->b_p_sts_nopaste = buf->b_p_sts;
6874 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006875 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006876#ifdef FEAT_VARTABS
6877 if (buf->b_p_vsts_nopaste)
6878 vim_free(buf->b_p_vsts_nopaste);
6879 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6880 ? vim_strsave(buf->b_p_vsts) : NULL;
6881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 }
6883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006884 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006886 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887#ifdef FEAT_CMDL_INFO
6888 save_ru = p_ru;
6889#endif
6890#ifdef FEAT_RIGHTLEFT
6891 save_ri = p_ri;
6892 save_hkmap = p_hkmap;
6893#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006894 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006895 p_ai_nopaste = p_ai;
6896 p_et_nopaste = p_et;
6897 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 p_tw_nopaste = p_tw;
6899 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006900#ifdef FEAT_VARTABS
6901 if (p_vsts_nopaste)
6902 vim_free(p_vsts_nopaste);
6903 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906
6907 /*
6908 * Always set the option values, also when 'paste' is set when it is
6909 * already on.
6910 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006911 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006912 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006914 buf->b_p_tw = 0; // textwidth is 0
6915 buf->b_p_wm = 0; // wrapmargin is 0
6916 buf->b_p_sts = 0; // softtabstop is 0
6917 buf->b_p_ai = 0; // no auto-indent
6918 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006919#ifdef FEAT_VARTABS
6920 if (buf->b_p_vsts)
6921 free_string_option(buf->b_p_vsts);
6922 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006923 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 }
6926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 // set global options
6928 p_sm = 0; // no showmatch
6929 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006932 status_redraw_all(); // redraw to remove the ruler
6933 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934#endif
6935#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 p_ri = 0; // no reverse insert
6937 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006939 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 p_tw = 0;
6941 p_wm = 0;
6942 p_sts = 0;
6943 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006944#ifdef FEAT_VARTABS
6945 if (p_vsts)
6946 free_string_option(p_vsts);
6947 p_vsts = empty_option;
6948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 }
6950
6951 /*
6952 * Paste switched from on to off: Restore saved values.
6953 */
6954 else if (old_p_paste)
6955 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006956 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006957 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {
6959 buf->b_p_tw = buf->b_p_tw_nopaste;
6960 buf->b_p_wm = buf->b_p_wm_nopaste;
6961 buf->b_p_sts = buf->b_p_sts_nopaste;
6962 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006963 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006964#ifdef FEAT_VARTABS
6965 if (buf->b_p_vsts)
6966 free_string_option(buf->b_p_vsts);
6967 buf->b_p_vsts = buf->b_p_vsts_nopaste
6968 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006969 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006970 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006971 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006972 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006973 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 }
6976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006977 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006979 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006982 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 p_ru = save_ru;
6984#endif
6985#ifdef FEAT_RIGHTLEFT
6986 p_ri = save_ri;
6987 p_hkmap = save_hkmap;
6988#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006989 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006990 p_ai = p_ai_nopaste;
6991 p_et = p_et_nopaste;
6992 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 p_tw = p_tw_nopaste;
6994 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006995#ifdef FEAT_VARTABS
6996 if (p_vsts)
6997 free_string_option(p_vsts);
6998 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 }
7001
7002 old_p_paste = p_paste;
7003}
7004
7005/*
7006 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
7007 *
7008 * Reset 'compatible' and set the values for options that didn't get set yet
7009 * to the Vim defaults.
7010 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007011 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 */
7013 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007014vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007016 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00007017 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007018 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
7020 if (!option_was_set((char_u *)"cp"))
7021 {
7022 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02007023 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
7025 set_option_default(opt_idx, OPT_FREE, FALSE);
7026 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007027 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007029
7030 if (fname != NULL)
7031 {
7032 p = vim_getenv(envname, &dofree);
7033 if (p == NULL)
7034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007035 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007036 p = FullName_save(fname, FALSE);
7037 if (p != NULL)
7038 {
7039 vim_setenv(envname, p);
7040 vim_free(p);
7041 }
7042 }
7043 else if (dofree)
7044 vim_free(p);
7045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046}
7047
7048/*
7049 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
7050 */
7051 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007052change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007054 int opt_idx;
7055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 if (p_cp != on)
7057 {
7058 p_cp = on;
7059 compatible_set();
7060 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00007061 opt_idx = findoption((char_u *)"cp");
7062 if (opt_idx >= 0)
7063 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064}
7065
7066/*
7067 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007068 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 */
7070 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007071option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 int idx;
7074
7075 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007076 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 return FALSE;
7078 if (options[idx].flags & P_WAS_SET)
7079 return TRUE;
7080 return FALSE;
7081}
7082
7083/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007084 * Reset the flag indicating option "name" was set.
7085 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007087reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007088{
7089 int idx = findoption(name);
7090
7091 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007092 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007093 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02007094 return OK;
7095 }
7096 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01007097}
7098
7099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 * compatible_set() - Called when 'compatible' has been set or unset.
7101 *
7102 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7103 * flag) to a Vi compatible value.
7104 * When 'compatible' is unset: Set all options that have a different default
7105 * for Vim (without the P_VI_DEF flag) to that default.
7106 */
7107 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007108compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 int opt_idx;
7111
Bram Moolenaardac13472019-09-16 21:06:21 +02007112 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7114 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7115 set_option_default(opt_idx, OPT_FREE, p_cp);
7116 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007117 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
7119
Bram Moolenaardac13472019-09-16 21:06:21 +02007120#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122/*
7123 * fill_breakat_flags() -- called when 'breakat' changes value.
7124 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007125 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007126fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007128 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 int i;
7130
7131 for (i = 0; i < 256; i++)
7132 breakat_flags[i] = FALSE;
7133
7134 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007135 for (p = p_breakat; *p; p++)
7136 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138#endif
7139
7140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 * Check if backspacing over something is allowed.
7142 */
7143 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007144can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007145 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007147#ifdef FEAT_JOB_CHANNEL
7148 if (what == BS_START && bt_prompt(curbuf))
7149 return FALSE;
7150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 switch (*p_bs)
7152 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007153 case '3': return TRUE;
7154 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 case '1': return (what != BS_START);
7156 case '0': return FALSE;
7157 }
7158 return vim_strchr(p_bs, what) != NULL;
7159}
7160
7161/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007162 * Return the effective 'scrolloff' value for the current window, using the
7163 * global value when appropriate.
7164 */
7165 long
7166get_scrolloff_value(void)
7167{
7168 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7169}
7170
7171/*
7172 * Return the effective 'sidescrolloff' value for the current window, using the
7173 * global value when appropriate.
7174 */
7175 long
7176get_sidescrolloff_value(void)
7177{
7178 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7179}
7180
7181/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007182 * Get the local or global value of 'backupcopy'.
7183 */
7184 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007185get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007186{
7187 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7188}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007189
Gary Johnson53ba05b2021-07-26 22:19:10 +02007190/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007191 * Get the local or global value of 'formatlistpat'.
7192 */
7193 char_u *
7194get_flp_value(buf_T *buf)
7195{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007196 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7197 return p_flp;
7198 return buf->b_p_flp;
7199}
7200
7201/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007202 * Get the local or global value of the 'virtualedit' flags.
7203 */
7204 unsigned int
7205get_ve_flags(void)
7206{
Gary Johnson51ad8502021-08-03 18:33:08 +02007207 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7208 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007209}
7210
Bram Moolenaaree857022019-11-09 23:26:40 +01007211#if defined(FEAT_LINEBREAK) || defined(PROTO)
7212/*
7213 * Get the local or global value of 'showbreak'.
7214 */
7215 char_u *
7216get_showbreak_value(win_T *win)
7217{
7218 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7219 return p_sbr;
7220 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7221 return empty_option;
7222 return win->w_p_sbr;
7223}
7224#endif
7225
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007226#if defined(FEAT_EVAL) || defined(PROTO)
7227/*
7228 * Get window or buffer local options.
7229 */
7230 dict_T *
7231get_winbuf_options(int bufopt)
7232{
7233 dict_T *d;
7234 int opt_idx;
7235
7236 d = dict_alloc();
7237 if (d == NULL)
7238 return NULL;
7239
Bram Moolenaardac13472019-09-16 21:06:21 +02007240 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007241 {
7242 struct vimoption *opt = &options[opt_idx];
7243
7244 if ((bufopt && (opt->indir & PV_BUF))
7245 || (!bufopt && (opt->indir & PV_WIN)))
7246 {
7247 char_u *varp = get_varp(opt);
7248
7249 if (varp != NULL)
7250 {
7251 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007252 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007253 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007254 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007255 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007256 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007257 }
7258 }
7259 }
7260
7261 return d;
7262}
7263#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007264
Bram Moolenaardac13472019-09-16 21:06:21 +02007265#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007266/*
7267 * This is called when 'culopt' is changed
7268 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007269 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007270fill_culopt_flags(char_u *val, win_T *wp)
7271{
7272 char_u *p;
7273 char_u culopt_flags_new = 0;
7274
7275 if (val == NULL)
7276 p = wp->w_p_culopt;
7277 else
7278 p = val;
7279 while (*p != NUL)
7280 {
7281 if (STRNCMP(p, "line", 4) == 0)
7282 {
7283 p += 4;
7284 culopt_flags_new |= CULOPT_LINE;
7285 }
7286 else if (STRNCMP(p, "both", 4) == 0)
7287 {
7288 p += 4;
7289 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7290 }
7291 else if (STRNCMP(p, "number", 6) == 0)
7292 {
7293 p += 6;
7294 culopt_flags_new |= CULOPT_NBR;
7295 }
7296 else if (STRNCMP(p, "screenline", 10) == 0)
7297 {
7298 p += 10;
7299 culopt_flags_new |= CULOPT_SCRLINE;
7300 }
7301
7302 if (*p != ',' && *p != NUL)
7303 return FAIL;
7304 if (*p == ',')
7305 ++p;
7306 }
7307
7308 // Can't have both "line" and "screenline".
7309 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7310 return FAIL;
7311 wp->w_p_culopt_flags = culopt_flags_new;
7312
7313 return OK;
7314}
7315#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007316
7317/*
7318 * Get the value of 'magic' adjusted for Vim9 script.
7319 */
7320 int
7321magic_isset(void)
7322{
7323 switch (magic_overruled)
7324 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007325 case OPTION_MAGIC_ON: return TRUE;
7326 case OPTION_MAGIC_OFF: return FALSE;
7327 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007328 }
7329#ifdef FEAT_EVAL
7330 if (in_vim9script())
7331 return TRUE;
7332#endif
7333 return p_magic;
7334}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007335
7336/*
7337 * Set the callback function value for an option that accepts a function name,
7338 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7339 * Returns OK if the option is successfully set to a function, otherwise
7340 * returns FAIL.
7341 */
7342 int
7343option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7344{
7345#ifdef FEAT_EVAL
7346 typval_T *tv;
7347 callback_T cb;
7348
7349 if (optval == NULL || *optval == NUL)
7350 {
7351 free_callback(optcb);
7352 return OK;
7353 }
7354
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007355 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007356 || (STRNCMP(optval, "function(", 9) == 0)
7357 || (STRNCMP(optval, "funcref(", 8) == 0))
7358 // Lambda expression or a funcref
7359 tv = eval_expr(optval, NULL);
7360 else
7361 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007362 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007363 if (tv == NULL)
7364 return FAIL;
7365
7366 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007367 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007368 {
7369 free_tv(tv);
7370 return FAIL;
7371 }
7372
7373 free_callback(optcb);
7374 set_callback(optcb, &cb);
7375 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007376
7377 // when using Vim9 style "import.funcname" it needs to be expanded to
7378 // "import#funcname".
7379 expand_autload_callback(optcb);
7380
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007381 return OK;
7382#else
7383 return FAIL;
7384#endif
7385}