blob: e24181af10ee81c293b656b6eebc91a3e45a2290 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +010040static char_u *option_expand(int opt_idx, char_u *val);
41static void didset_options(void);
42static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000043#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010044static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000045#else
46# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
47#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010048static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
49static 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 +020050static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010051static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020052static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010053static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010054static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
56static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020057static int istermoption(struct vimoption *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
59static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020060static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static void option_value2string(struct vimoption *, int opt_flags);
62static void check_winopt(winopt_T *wop);
63static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void paste_option_changed(void);
65static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67/*
68 * Initialize the options, first part.
69 *
70 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010071 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 */
73 void
Bram Moolenaar07268702018-03-01 21:57:32 +010074set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000075{
76 char_u *p;
77 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000078 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80#ifdef FEAT_LANGMAP
81 langmap_init();
82#endif
83
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010084 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 p_cp = TRUE;
86
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010087 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000088 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000089 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000090 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020091 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000092 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000093
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 /*
95 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010099#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000100 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100101 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000103 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200104#if defined(MSWIN)
105 {
106 // For MS-Windows put the path in quotes instead of escaping spaces.
107 char_u *cmd;
108 size_t len;
109
110 if (vim_strchr(p, ' ') != NULL)
111 {
112 len = STRLEN(p) + 3; // two quotes and a trailing NUL
113 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200114 if (cmd != NULL)
115 {
116 vim_snprintf((char *)cmd, len, "\"%s\"", p);
117 set_string_default("sh", cmd);
118 vim_free(cmd);
119 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200120 }
121 else
122 set_string_default("sh", p);
123 }
124#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100125 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128#ifdef FEAT_WILDIGN
129 /*
130 * Set the default for 'backupskip' to include environment variables for
131 * temp files.
132 */
133 {
134# ifdef UNIX
135 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
136# else
137 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
138# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000139 int len;
140 garray_T ga;
141 int mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143 ga_init2(&ga, 1, 100);
144 for (n = 0; n < (long)(sizeof(names) / sizeof(char *)); ++n)
145 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000146 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# ifdef UNIX
148 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200149# ifdef MACOS_X
150 p = (char_u *)"/private/tmp";
151# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 else
155# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000156 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 if (p != NULL && *p != NUL)
158 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000160 len = (int)STRLEN(p) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (ga_grow(&ga, len) == OK)
162 {
163 if (ga.ga_len > 0)
164 STRCAT(ga.ga_data, ",");
165 STRCAT(ga.ga_data, p);
166 add_pathsep(ga.ga_data);
167 STRCAT(ga.ga_data, "*");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 ga.ga_len += len;
169 }
170 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000171 if (mustfree)
172 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 }
174 if (ga.ga_data != NULL)
175 {
176 set_string_default("bsk", ga.ga_data);
177 vim_free(ga.ga_data);
178 }
179 }
180#endif
181
182 /*
183 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
184 */
185 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000186 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000188#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
189 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
190#endif
191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#ifdef HAVE_AVAIL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100193 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200194 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#else
196# ifdef HAVE_TOTAL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100197 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000198 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000200 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201# endif
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000204 opt_idx = findoption((char_u *)"maxmem");
205 if (opt_idx >= 0)
206 {
207#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100208 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
209 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000210#endif
211 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
212 }
213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
215
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifdef FEAT_SEARCHPATH
217 {
218 char_u *cdpath;
219 char_u *buf;
220 int i;
221 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000222 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100224 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000225 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (cdpath != NULL)
227 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200228 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 if (buf != NULL)
230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100231 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 j = 1;
233 for (i = 0; cdpath[i] != NUL; ++i)
234 {
235 if (vim_ispathlistsep(cdpath[i]))
236 buf[j++] = ',';
237 else
238 {
239 if (cdpath[i] == ' ' || cdpath[i] == ',')
240 buf[j++] = '\\';
241 buf[j++] = cdpath[i];
242 }
243 }
244 buf[j] = NUL;
245 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000246 if (opt_idx >= 0)
247 {
248 options[opt_idx].def_val[VI_DEFAULT] = buf;
249 options[opt_idx].flags |= P_DEF_ALLOCED;
250 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200251 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100252 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254 if (mustfree)
255 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 }
257 }
258#endif
259
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100260#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100261 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100263# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 (char_u *)"cp1252"
265# else
266# ifdef VMS
267 (char_u *)"dec-mcs"
268# else
269# ifdef EBCDIC
270 (char_u *)"ebcdic-uk"
271# else
272# ifdef MAC
273 (char_u *)"mac-roman"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100274# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 (char_u *)"hp-roman8"
276# endif
277# endif
278# endif
279# endif
280 );
281#endif
282
283#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100284 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100286# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000287 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288# else
289# ifdef VMS
290 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
291
292# else
293 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
294# endif
295# endif
296 );
297#endif
298
299 /*
300 * Set all the options (except the terminal options) to their default
301 * value. Also set the global value for local options.
302 */
303 set_options_default(0);
304
Bram Moolenaar07268702018-03-01 21:57:32 +0100305#ifdef CLEAN_RUNTIMEPATH
306 if (clean_arg)
307 {
308 opt_idx = findoption((char_u *)"runtimepath");
309 if (opt_idx >= 0)
310 {
311 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
312 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
313 }
314 opt_idx = findoption((char_u *)"packpath");
315 if (opt_idx >= 0)
316 {
317 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
318 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
319 }
320 }
321#endif
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_GUI
324 if (found_reverse_arg)
325 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
326#endif
327
328 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100329 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100330 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 check_buf_options(curbuf);
332 check_win_options(curwin);
333 check_options();
334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100335 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 didset_options();
337
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000338#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100339 // Use the current chartab for the generic chartab. This is not in
340 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000341 init_spell_chartab();
342#endif
343
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 /*
345 * Expand environment variables and things like "~" for the defaults.
346 * If option_expand() returns non-NULL the variable is expanded. This can
347 * only happen for non-indirect options.
348 * Also set the default to the expanded value, so ":set" does not list
349 * them.
350 * Don't set the P_ALLOCED flag, because we don't want to free the
351 * default.
352 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200353 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 if ((options[opt_idx].flags & P_GETTEXT)
356 && options[opt_idx].var != NULL)
357 p = (char_u *)_(*(char **)options[opt_idx].var);
358 else
359 p = option_expand(opt_idx, NULL);
360 if (p != NULL && (p = vim_strsave(p)) != NULL)
361 {
362 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100363 // VIMEXP
364 // Defaults for all expanded options are currently the same for Vi
365 // and Vim. When this changes, add some code here! Also need to
366 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 if (options[opt_idx].flags & P_DEF_ALLOCED)
368 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
369 options[opt_idx].def_val[VI_DEFAULT] = p;
370 options[opt_idx].flags |= P_DEF_ALLOCED;
371 }
372 }
373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100374 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100377 // Detect use of mlterm.
378 // Mlterm is a terminal emulator akin to xterm that has some special
379 // abilities (bidi namely).
380 // NOTE: mlterm's author is being asked to 'set' a variable
381 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 if (mch_getenv((char_u *)"MLTERM") != NULL)
383 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
384#endif
385
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200386 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
Bram Moolenaar4f974752019-02-17 17:44:42 +0100388# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 /*
390 * If $LANG isn't set, try to get a good value for it. This makes the
391 * right language be used automatically. Don't do this for English.
392 */
393 if (mch_getenv((char_u *)"LANG") == NULL)
394 {
395 char buf[20];
396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100397 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
398 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
399 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
401 (LPTSTR)buf, 20);
402 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
403 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100404 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
406 STRCPY(buf, "zh_TW");
407 else if (STRNICMP(buf, "chs", 3) == 0
408 || STRNICMP(buf, "zhc", 3) == 0)
409 STRCPY(buf, "zh_CN");
410 else if (STRNICMP(buf, "jp", 2) == 0)
411 STRCPY(buf, "ja");
412 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100414 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 }
416 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000417# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000418# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100419 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000420 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422# endif
423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // enc_locale() will try to find the encoding of the current locale.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 p = enc_locale();
426 if (p != NULL)
427 {
428 char_u *save_enc;
429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100430 // Try setting 'encoding' and check if the value is valid.
431 // If not, go back to the default "latin1".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 save_enc = p_enc;
433 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000434 if (STRCMP(p_enc, "gb18030") == 0)
435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100436 // We don't support "gb18030", but "cp936" is a good substitute
437 // for practical purposes, thus use that. It's not an alias to
438 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000439 p_enc = vim_strsave((char_u *)"cp936");
440 vim_free(p);
441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (mb_init() == NULL)
443 {
444 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000445 if (opt_idx >= 0)
446 {
447 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
448 options[opt_idx].flags |= P_DEF_ALLOCED;
449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaard0573012017-10-28 21:11:06 +0200451#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100452 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100454 // Adjust the default for 'isprint' and 'iskeyword' to match
455 // latin1. Also set the defaults for when 'nocompatible' is
456 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000457 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000458 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000459 set_string_option_direct((char_u *)"isk", -1,
460 ISK_LATIN1, OPT_FREE, SID_NONE);
461 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000462 if (opt_idx >= 0)
463 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000464 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000465 if (opt_idx >= 0)
466 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000467 (void)init_chartab();
468 }
469#endif
470
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200471#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100472 // Win32 console: When GetACP() returns a different value from
473 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200474 if (
475# ifdef VIMDLL
476 (!gui.in_use && !gui.starting) &&
477# endif
478 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {
480 char buf[50];
481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100482 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
483 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100484 if (GetConsoleCP() == 0)
485 sprintf(buf, "cp%ld", (long)GetACP());
486 else
487 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 p_tenc = vim_strsave((char_u *)buf);
489 if (p_tenc != NULL)
490 {
491 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000492 if (opt_idx >= 0)
493 {
494 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
495 options[opt_idx].flags |= P_DEF_ALLOCED;
496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 convert_setup(&input_conv, p_tenc, p_enc);
498 convert_setup(&output_conv, p_enc, p_tenc);
499 }
500 else
501 p_tenc = empty_option;
502 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100503#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100504#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100505 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000506 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 }
509 else
510 {
511 vim_free(p_enc);
512 p_enc = save_enc;
513 }
514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100517 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 set_helplang_default(get_mess_lang());
519#endif
520}
521
522/*
523 * Set an option to its default value.
524 * This does not take care of side effects!
525 */
526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100527set_option_default(
528 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100529 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
530 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 char_u *varp; // pointer to variable for current option
533 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000535 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
537
538 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
539 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100540 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {
542 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
543 if (flags & P_STRING)
544 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100545 // Use set_string_option_direct() for local options to handle
546 // freeing and allocating the value.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200547 if (options[opt_idx].indir != PV_NONE)
548 set_string_option_direct(NULL, opt_idx,
549 options[opt_idx].def_val[dvi], opt_flags, 0);
550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200552 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
553 free_string_option(*(char_u **)(varp));
554 *(char_u **)varp = options[opt_idx].def_val[dvi];
555 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 }
557 }
558 else if (flags & P_NUM)
559 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000560 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 win_comp_scroll(curwin);
562 else
563 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100564 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
565
566 if ((long *)varp == &curwin->w_p_so
567 || (long *)varp == &curwin->w_p_siso)
568 // 'scrolloff' and 'sidescrolloff' local values have a
569 // different default value than the global default.
570 *(long *)varp = -1;
571 else
572 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100573 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (both)
575 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100576 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100579 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100581 // the cast to long is required for Manx C, long_i is needed for
582 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000583 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000584#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100585 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000586 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
587 *(int *)varp = FALSE;
588#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100589 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (both)
591 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
592 *(int *)varp;
593 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000596 flagsp = insecure_flag(opt_idx, opt_flags);
597 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 }
599
600#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200601 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602#endif
603}
604
605/*
606 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200607 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 */
609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100610set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000615 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaardac13472019-09-16 21:06:21 +0200617 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200618 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200619 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100620 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200621# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200622 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200623 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200624# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100625 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 set_option_default(i, opt_flags, p_cp);
627
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000629 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200631#ifdef FEAT_CINDENT
632 parse_cino(curbuf);
633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634}
635
636/*
637 * Set the Vi-default value of a string option.
638 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100639 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100641 static void
642set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
644 char_u *p;
645 int opt_idx;
646
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100647 if (escape && vim_strchr(val, ' ') != NULL)
648 p = vim_strsave_escaped(val, (char_u *)" ");
649 else
650 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100651 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000654 if (opt_idx >= 0)
655 {
656 if (options[opt_idx].flags & P_DEF_ALLOCED)
657 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
658 options[opt_idx].def_val[VI_DEFAULT] = p;
659 options[opt_idx].flags |= P_DEF_ALLOCED;
660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662}
663
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100664 void
665set_string_default(char *name, char_u *val)
666{
667 set_string_default_esc(name, val, FALSE);
668}
669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670/*
671 * Set the Vi-default value of a number option.
672 * Used for 'lines' and 'columns'.
673 */
674 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100675set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000677 int opt_idx;
678
679 opt_idx = findoption((char_u *)name);
680 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000681 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682}
683
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200684/*
685 * Set all window-local and buffer-local options to the Vim default.
686 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200687 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200688 */
689 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200690set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200691{
692 win_T *save_curwin = curwin;
693 int i;
694
695 curwin = wp;
696 curbuf = curwin->w_buffer;
697 block_autocmds();
698
Bram Moolenaardac13472019-09-16 21:06:21 +0200699 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200700 {
701 struct vimoption *p = &(options[i]);
702 char_u *varp = get_varp_scope(p, OPT_LOCAL);
703
704 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200705 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200706 && !(options[i].flags & P_NODEFAULT)
707 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200708 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200709 }
710
711 unblock_autocmds();
712 curwin = save_curwin;
713 curbuf = curwin->w_buffer;
714}
715
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000716#if defined(EXITFREE) || defined(PROTO)
717/*
718 * Free all options.
719 */
720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000722{
723 int i;
724
Bram Moolenaardac13472019-09-16 21:06:21 +0200725 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000726 {
727 if (options[i].indir == PV_NONE)
728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100729 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100730 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000731 free_string_option(*(char_u **)options[i].var);
732 if (options[i].flags & P_DEF_ALLOCED)
733 free_string_option(options[i].def_val[VI_DEFAULT]);
734 }
735 else if (options[i].var != VAR_WIN
736 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 // buffer-local option: free global value
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000738 free_string_option(*(char_u **)options[i].var);
739 }
740}
741#endif
742
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744/*
745 * Initialize the options, part two: After getting Rows and Columns and
746 * setting 'term'.
747 */
748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100749set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000751 int idx;
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100754 * 'scroll' defaults to half the window height. The stored default is zero,
755 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000757 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000758 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000759 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 comp_col();
761
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000762 /*
763 * 'window' is only for backwards compatibility with Vi.
764 * Default is Rows - 1.
765 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000766 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000767 p_window = Rows - 1;
768 set_number_default("window", Rows - 1);
769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100770 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100771#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000772 /*
773 * If 'background' wasn't set by the user, try guessing the value,
774 * depending on the terminal name. Only need to check for terminals
775 * with a dark background, that can handle color.
776 */
777 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000778 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
779 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000781 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100782 // don't mark it as set, when starting the GUI it may be
783 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000784 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000787
788#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000790#endif
791#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100792 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000793#endif
794#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100795 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
799/*
800 * Initialize the options, part three: After reading the .vimrc
801 */
802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100805#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806/*
807 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
808 * This is done after other initializations, where 'shell' might have been
809 * set, but only if they have not been set before.
810 */
811 char_u *p;
812 int idx_srr;
813 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100814# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 int idx_sp;
816 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000820 if (idx_srr < 0)
821 do_srr = FALSE;
822 else
823 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100824# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000826 if (idx_sp < 0)
827 do_sp = FALSE;
828 else
829 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100830# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200831 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (p != NULL)
833 {
834 /*
835 * Default for p_sp is "| tee", for p_srr is ">".
836 * For known shells it is changed here to include stderr.
837 */
838 if ( fnamecmp(p, "csh") == 0
839 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100840# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 || fnamecmp(p, "csh.exe") == 0
842 || fnamecmp(p, "tcsh.exe") == 0
843# endif
844 )
845 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100846# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (do_sp)
848 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100849# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100851# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
855 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (do_srr)
858 {
859 p_srr = (char_u *)">&";
860 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
861 }
862 }
863 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 // Always use bourne shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if ( fnamecmp(p, "sh") == 0
866 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200867 || fnamecmp(p, "mksh") == 0
868 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000870 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200872 || fnamecmp(p, "fish") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100873# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 || fnamecmp(p, "cmd") == 0
875 || fnamecmp(p, "sh.exe") == 0
876 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200877 || fnamecmp(p, "mksh.exe") == 0
878 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000880 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 || fnamecmp(p, "bash.exe") == 0
882 || fnamecmp(p, "cmd.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100884 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (do_sp)
888 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100889# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100891# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
895 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (do_srr)
898 {
899 p_srr = (char_u *)">%s 2>&1";
900 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
901 }
902 }
903 vim_free(p);
904 }
905#endif
906
Bram Moolenaar4f974752019-02-17 17:44:42 +0100907#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100909 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
910 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 * This is done after other initializations, where 'shell' might have been
912 * set, but only if they have not been set before. Default for p_shcf is
913 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100914 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000916 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 {
918 int idx3;
919
920 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000921 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
923 p_shcf = (char_u *)"-c";
924 options[idx3].def_val[VI_DEFAULT] = p_shcf;
925 }
926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100927 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000929 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
931 p_sxq = (char_u *)"\"";
932 options[idx3].def_val[VI_DEFAULT] = p_sxq;
933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100935 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
936 {
937 int idx3;
938
939 /*
940 * cmd.exe on Windows will strip the first and last double quote given
941 * on the command line, e.g. most of the time things like:
942 * cmd /c "my path/to/echo" "my args to echo"
943 * become:
944 * my path/to/echo" "my args to echo
945 * when executed.
946 *
Bram Moolenaar034b1152012-02-19 18:19:30 +0100947 * To avoid this, set shellxquote to surround the command in
948 * parenthesis. This appears to make most commands work, without
949 * breaking commands that worked previously, such as
950 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +0100951 */
Bram Moolenaara64ba222012-02-12 23:23:31 +0100952 idx3 = findoption((char_u *)"sxq");
953 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
954 {
Bram Moolenaar034b1152012-02-19 18:19:30 +0100955 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +0100956 options[idx3].def_val[VI_DEFAULT] = p_sxq;
957 }
958
Bram Moolenaara64ba222012-02-12 23:23:31 +0100959 idx3 = findoption((char_u *)"shcf");
960 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
961 {
Bram Moolenaar034b1152012-02-19 18:19:30 +0100962 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +0100963 options[idx3].def_val[VI_DEFAULT] = p_shcf;
964 }
965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#endif
967
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100968 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +0100969 {
970 int idx_ffs = findoption((char_u *)"ffs");
971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100972 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +0100973 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
974 set_fileformat(default_fileformat(), OPT_LOCAL);
975 }
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#ifdef FEAT_TITLE
978 set_title_defaults();
979#endif
980}
981
982#if defined(FEAT_MULTI_LANG) || defined(PROTO)
983/*
984 * When 'helplang' is still at its default value, set it to "lang".
985 * Only the first two characters of "lang" are used.
986 */
987 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100988set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 int idx;
991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100992 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 return;
994 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000995 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
997 if (options[idx].flags & P_ALLOCED)
998 free_string_option(p_hlg);
999 p_hlg = vim_strsave(lang);
1000 if (p_hlg == NULL)
1001 p_hlg = empty_option;
1002 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001003 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001004 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001005 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1006 {
1007 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1008 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1009 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001010 // any C like setting, such as C.UTF-8, becomes "en"
1011 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1012 {
1013 p_hlg[0] = 'e';
1014 p_hlg[1] = 'n';
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 options[idx].flags |= P_ALLOCED;
1019 }
1020}
1021#endif
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023#ifdef FEAT_TITLE
1024/*
1025 * 'title' and 'icon' only default to true if they have not been set or reset
1026 * in .vimrc and we can read the old value.
1027 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1028 * they can be reset. This reduces startup time when using X on a remote
1029 * machine.
1030 */
1031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001032set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 int idx1;
1035 long val;
1036
1037 /*
1038 * If GUI is (going to be) used, we can always set the window title and
1039 * icon name. Saves a bit of time, because the X11 display server does
1040 * not need to be contacted.
1041 */
1042 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001043 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045#ifdef FEAT_GUI
1046 if (gui.starting || gui.in_use)
1047 val = TRUE;
1048 else
1049#endif
1050 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001051 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 p_title = val;
1053 }
1054 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001055 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057#ifdef FEAT_GUI
1058 if (gui.starting || gui.in_use)
1059 val = TRUE;
1060 else
1061#endif
1062 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001063 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 p_icon = val;
1065 }
1066}
1067#endif
1068
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001069 void
1070ex_set(exarg_T *eap)
1071{
1072 int flags = 0;
1073
1074 if (eap->cmdidx == CMD_setlocal)
1075 flags = OPT_LOCAL;
1076 else if (eap->cmdidx == CMD_setglobal)
1077 flags = OPT_GLOBAL;
1078#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
1079 if (cmdmod.browse && flags == 0)
1080 ex_options(eap);
1081 else
1082#endif
1083 {
1084 if (eap->forceit)
1085 flags |= OPT_ONECOLUMN;
1086 (void)do_set(eap->arg, flags);
1087 }
1088}
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/*
1091 * Parse 'arg' for option settings.
1092 *
1093 * 'arg' may be IObuff, but only when no errors can be present and option
1094 * does not need to be expanded with option_expand().
1095 * "opt_flags":
1096 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001097 * OPT_GLOBAL for ":setglobal"
1098 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001100 * OPT_WINONLY to only set window-local options
1101 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 *
1103 * returns FAIL if an error is detected, OK otherwise
1104 */
1105 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001106do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001107 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001111 char *errmsg;
1112 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1115 int nextchar; // next non-white char after option name
1116 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int len;
1118 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001119 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001121 long_u flags; // flags for current option
1122 char_u *varp = NULL; // pointer to variable for current option
1123 int did_show = FALSE; // already showed one value
1124 int adding; // "opt+=arg"
1125 int prepending; // "opt^=arg"
1126 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 int cp_val = 0;
1128 char_u key_name[2];
1129
1130 if (*arg == NUL)
1131 {
1132 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001133 did_show = TRUE;
1134 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001137 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {
1139 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001140 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001142 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1143 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 /*
1146 * ":set all" show all options.
1147 * ":set all&" set all options to their default value.
1148 */
1149 arg += 3;
1150 if (*arg == '&')
1151 {
1152 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001153 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001155 didset_options();
1156 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001157 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001162 did_show = TRUE;
1163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001165 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
1167 showoptions(2, opt_flags);
1168 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001169 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 arg += 7;
1171 }
1172 else
1173 {
1174 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001175 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177 prefix = 0;
1178 arg += 2;
1179 }
1180 else if (STRNCMP(arg, "inv", 3) == 0)
1181 {
1182 prefix = 2;
1183 arg += 3;
1184 }
1185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001186 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 key = 0;
1188 if (*arg == '<')
1189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001191 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1193 len = 5;
1194 else
1195 {
1196 len = 1;
1197 while (arg[len] != NUL && arg[len] != '>')
1198 ++len;
1199 }
1200 if (arg[len] != '>')
1201 {
1202 errmsg = e_invarg;
1203 goto skip;
1204 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001205 arg[len] = NUL; // put NUL after name
1206 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001208 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001210 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212 else
1213 {
1214 len = 0;
1215 /*
1216 * The two characters after "t_" may not be alphanumeric.
1217 */
1218 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1219 len = 4;
1220 else
1221 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1222 ++len;
1223 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001224 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001226 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001228 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 afterchar = arg[len];
1233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001234 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001235 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 ++len;
1237
1238 adding = FALSE;
1239 prepending = FALSE;
1240 removing = FALSE;
1241 if (arg[len] != NUL && arg[len + 1] == '=')
1242 {
1243 if (arg[len] == '+')
1244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 ++len;
1247 }
1248 else if (arg[len] == '^')
1249 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 ++len;
1252 }
1253 else if (arg[len] == '-')
1254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001255 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 ++len;
1257 }
1258 }
1259 nextchar = arg[len];
1260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001263 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 goto skip;
1265 }
1266
1267 if (opt_idx >= 0)
1268 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001269 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001271 // Only give an error message when requesting the value of
1272 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1274 && (!(options[opt_idx].flags & P_BOOL)
1275 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001276 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 goto skip;
1278 }
1279
1280 flags = options[opt_idx].flags;
1281 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1282 }
1283 else
1284 {
1285 flags = P_STRING;
1286 if (key < 0)
1287 {
1288 key_name[0] = KEY2TERMCAP0(key);
1289 key_name[1] = KEY2TERMCAP1(key);
1290 }
1291 else
1292 {
1293 key_name[0] = KS_KEY;
1294 key_name[1] = (key & 0xff);
1295 }
1296 }
1297
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001298 // Skip all options that are not window-local (used when showing
1299 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001300 if ((opt_flags & OPT_WINONLY)
1301 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1302 goto skip;
1303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001304 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001305 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1306 && options[opt_idx].var == VAR_WIN)
1307 goto skip;
1308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001309 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001310 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001312 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001314 errmsg = _("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001315 goto skip;
1316 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001317 if ((flags & P_MLE) && !p_mle)
1318 {
1319 errmsg = _("E992: Not allowed in a modeline when 'modelineexpr' is off");
1320 goto skip;
1321 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001322#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001323 // In diff mode some options are overruled. This avoids that
1324 // 'foldmethod' becomes "marker" instead of "diff" and that
1325 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001326 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001327 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001328 && (
1329#ifdef FEAT_FOLDING
1330 options[opt_idx].indir == PV_FDM ||
1331#endif
1332 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001333 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336
1337#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001338 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001339 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001341 errmsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 goto skip;
1343 }
1344#endif
1345
1346 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1347 {
1348 arg += len;
1349 cp_val = p_cp;
1350 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1351 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001352 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
1354 cp_val = FALSE;
1355 arg += 3;
1356 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001357 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 cp_val = TRUE;
1360 arg += 2;
1361 }
1362 }
1363 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001364 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
1366 errmsg = e_trailing;
1367 goto skip;
1368 }
1369 }
1370
1371 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001372 * allow '=' and ':' for hystorical reasons (MSDOS command.com
1373 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
1375 if (nextchar == '?'
1376 || (prefix == 1
1377 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1378 && !(flags & P_BOOL)))
1379 {
1380 /*
1381 * print value
1382 */
1383 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001384 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 else
1386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001387 gotocmdline(TRUE); // cursor at status line
1388 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 if (opt_idx >= 0)
1391 {
1392 showoneopt(&options[opt_idx], opt_flags);
1393#ifdef FEAT_EVAL
1394 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001396 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001397 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001398 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001399 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001400 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001401 (int)options[opt_idx].indir & PV_MASK]);
1402 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001403 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001404 (int)options[opt_idx].indir & PV_MASK]);
1405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406#endif
1407 }
1408 else
1409 {
1410 char_u *p;
1411
1412 p = find_termcode(key_name);
1413 if (p == NULL)
1414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001415 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 goto skip;
1417 }
1418 else
1419 (void)show_one_termcode(key_name, p, TRUE);
1420 }
1421 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001422 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 errmsg = e_trailing;
1424 }
1425 else
1426 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001427 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001428 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001430 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
1432 if (nextchar == '=' || nextchar == ':')
1433 {
1434 errmsg = e_invarg;
1435 goto skip;
1436 }
1437
1438 /*
1439 * ":set opt!": invert
1440 * ":set opt&": reset to default value
1441 * ":set opt<": reset to global value
1442 */
1443 if (nextchar == '!')
1444 value = *(int *)(varp) ^ 1;
1445 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001446 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 ((flags & P_VI_DEF) || cp_val)
1448 ? VI_DEFAULT : VIM_DEFAULT];
1449 else if (nextchar == '<')
1450 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001451 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if ((int *)varp == &curbuf->b_p_ar
1453 && opt_flags == OPT_LOCAL)
1454 value = -1;
1455 else
1456 value = *(int *)get_varp_scope(&(options[opt_idx]),
1457 OPT_GLOBAL);
1458 }
1459 else
1460 {
1461 /*
1462 * ":set invopt": invert
1463 * ":set opt" or ":set noopt": set or reset
1464 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001465 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
1467 errmsg = e_trailing;
1468 goto skip;
1469 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001470 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 value = *(int *)(varp) ^ 1;
1472 else
1473 value = prefix;
1474 }
1475
1476 errmsg = set_bool_option(opt_idx, varp, (int)value,
1477 opt_flags);
1478 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001479 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1482 || prefix != 1)
1483 {
1484 errmsg = e_invarg;
1485 goto skip;
1486 }
1487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001488 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 {
1490 /*
1491 * Different ways to set a number option:
1492 * & set to default value
1493 * < set to global value
1494 * <xx> accept special key codes for 'wildchar'
1495 * c accept any non-digit for 'wildchar'
1496 * [-]0-9 set number
1497 * other error
1498 */
1499 ++arg;
1500 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001501 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 ((flags & P_VI_DEF) || cp_val)
1503 ? VI_DEFAULT : VIM_DEFAULT];
1504 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001506 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1507 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001508 if ((long *)varp == &curbuf->b_p_ul
1509 && opt_flags == OPT_LOCAL)
1510 value = NO_LOCAL_UNDOLEVEL;
1511 else
1512 value = *(long *)get_varp_scope(
1513 &(options[opt_idx]), OPT_GLOBAL);
1514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 else if (((long *)varp == &p_wc
1516 || (long *)varp == &p_wcm)
1517 && (*arg == '<'
1518 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001519 || (*arg != NUL
1520 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 && !VIM_ISDIGIT(*arg))))
1522 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001523 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (value == 0 && (long *)varp != &p_wcm)
1525 {
1526 errmsg = e_invarg;
1527 goto skip;
1528 }
1529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1531 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001532 // Allow negative (for 'undolevels'), octal and
1533 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001534 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001535 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001536 if (i == 0 || (arg[i] != NUL
1537 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001539 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 goto skip;
1541 }
1542 }
1543 else
1544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 goto skip;
1547 }
1548
1549 if (adding)
1550 value = *(long *)varp + value;
1551 if (prepending)
1552 value = *(long *)varp * value;
1553 if (removing)
1554 value = *(long *)varp - value;
1555 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001556 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001558 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001560 char_u *save_arg = NULL;
1561 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001562 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001563 char_u *newval;
1564 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001565 char_u *origval_l = NULL;
1566 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001567#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001568 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001569 char_u *saved_origval_l = NULL;
1570 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001571 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001572#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001573 unsigned newlen;
1574 int comma;
1575 int bs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001576 int new_value_alloced; // new string option
1577 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001579 // When using ":set opt=val" for a global option
1580 // with a local value the local value will be
1581 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001583 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 varp = options[opt_idx].var;
1585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001586 // The old value is kept until we are sure that the
1587 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001589
Bram Moolenaard7c96872019-06-15 17:12:48 +02001590 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1591 {
1592 origval_l = *(char_u **)get_varp_scope(
1593 &(options[opt_idx]), OPT_LOCAL);
1594 origval_g = *(char_u **)get_varp_scope(
1595 &(options[opt_idx]), OPT_GLOBAL);
1596
1597 // A global-local string option might have an empty
1598 // option as value to indicate that the global
1599 // value should be used.
1600 if (((int)options[opt_idx].indir & PV_BOTH)
1601 && origval_l == empty_option)
1602 origval_l = origval_g;
1603 }
1604
1605 // When setting the local value of a global
1606 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001607 if (((int)options[opt_idx].indir & PV_BOTH)
1608 && (opt_flags & OPT_LOCAL))
1609 origval = *(char_u **)get_varp(
1610 &options[opt_idx]);
1611 else
1612 origval = oldval;
1613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 newval = options[opt_idx].def_val[
1617 ((flags & P_VI_DEF) || cp_val)
1618 ? VI_DEFAULT : VIM_DEFAULT];
1619 if ((char_u **)varp == &p_bg)
1620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001621 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622#ifdef FEAT_GUI
1623 if (gui.in_use)
1624 newval = gui_bg_default();
1625 else
1626#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001627 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001630 // expand environment variables and ~ (since the
1631 // default value was already expanded, only
1632 // required when an environment variable was set
1633 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (newval == NULL)
1635 newval = empty_option;
1636 else
1637 {
1638 s = option_expand(opt_idx, newval);
1639 if (s == NULL)
1640 s = newval;
1641 newval = vim_strsave(s);
1642 }
1643 new_value_alloced = TRUE;
1644 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001645 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
1647 newval = vim_strsave(*(char_u **)get_varp_scope(
1648 &(options[opt_idx]), OPT_GLOBAL));
1649 new_value_alloced = TRUE;
1650 }
1651 else
1652 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001653 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655 /*
1656 * Set 'keywordprg' to ":help" if an empty
1657 * value was passed to :set by the user.
1658 * Misuse errbuf[] for the resulting string.
1659 */
1660 if (varp == (char_u *)&p_kp
1661 && (*arg == NUL || *arg == ' '))
1662 {
1663 STRCPY(errbuf, ":help");
1664 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001665 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001668 * Convert 'backspace' number to string, for
1669 * adding, prepending and removing string.
1670 */
1671 else if (varp == (char_u *)&p_bs
1672 && VIM_ISDIGIT(**(char_u **)varp))
1673 {
1674 i = getdigits((char_u **)varp);
1675 switch (i)
1676 {
1677 case 0:
1678 *(char_u **)varp = empty_option;
1679 break;
1680 case 1:
1681 *(char_u **)varp = vim_strsave(
1682 (char_u *)"indent,eol");
1683 break;
1684 case 2:
1685 *(char_u **)varp = vim_strsave(
1686 (char_u *)"indent,eol,start");
1687 break;
1688 }
1689 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001690 if (origval == oldval)
1691 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001692 if (origval_l == oldval)
1693 origval_l = *(char_u **)varp;
1694 if (origval_g == oldval)
1695 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001696 oldval = *(char_u **)varp;
1697 }
1698 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 * Convert 'whichwrap' number to string, for
1700 * backwards compatibility with Vim 3.0.
1701 * Misuse errbuf[] for the resulting string.
1702 */
1703 else if (varp == (char_u *)&p_ww
1704 && VIM_ISDIGIT(*arg))
1705 {
1706 *errbuf = NUL;
1707 i = getdigits(&arg);
1708 if (i & 1)
1709 STRCAT(errbuf, "b,");
1710 if (i & 2)
1711 STRCAT(errbuf, "s,");
1712 if (i & 4)
1713 STRCAT(errbuf, "h,l,");
1714 if (i & 8)
1715 STRCAT(errbuf, "<,>,");
1716 if (i & 16)
1717 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001718 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 errbuf[STRLEN(errbuf) - 1] = NUL;
1720 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001721 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723 /*
1724 * Remove '>' before 'dir' and 'bdir', for
1725 * backwards compatibility with version 3.0
1726 */
1727 else if ( *arg == '>'
1728 && (varp == (char_u *)&p_dir
1729 || varp == (char_u *)&p_bdir))
1730 {
1731 ++arg;
1732 }
1733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /*
1735 * Copy the new string into allocated memory.
1736 * Can't use set_string_option_direct(), because
1737 * we need to remove the backslashes.
1738 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001739 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 newlen = (unsigned)STRLEN(arg) + 1;
1741 if (adding || prepending || removing)
1742 newlen += (unsigned)STRLEN(origval) + 1;
1743 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001744 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 break;
1746 s = newval;
1747
1748 /*
1749 * Copy the string, skip over escaped chars.
1750 * For MS-DOS and WIN32 backslashes before normal
1751 * file name characters are not removed, and keep
1752 * backslash at start, for "\\machine\path", but
1753 * do remove it for "\\\\machine\\path".
1754 * The reverse is found in ExpandOldSetting().
1755 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001756 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
1758 if (*arg == '\\' && arg[1] != NUL
1759#ifdef BACKSLASH_IN_FILENAME
1760 && !((flags & P_EXPAND)
1761 && vim_isfilec(arg[1])
1762 && (arg[1] != '\\'
1763 || (s == newval
1764 && arg[2] != '\\')))
1765#endif
1766 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001767 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001769 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001771 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 mch_memmove(s, arg, (size_t)i);
1773 arg += i;
1774 s += i;
1775 }
1776 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 *s++ = *arg++;
1778 }
1779 *s = NUL;
1780
1781 /*
1782 * Expand environment variables and ~.
1783 * Don't do it when adding without inserting a
1784 * comma.
1785 */
1786 if (!(adding || prepending || removing)
1787 || (flags & P_COMMA))
1788 {
1789 s = option_expand(opt_idx, newval);
1790 if (s != NULL)
1791 {
1792 vim_free(newval);
1793 newlen = (unsigned)STRLEN(s) + 1;
1794 if (adding || prepending || removing)
1795 newlen += (unsigned)STRLEN(origval) + 1;
1796 newval = alloc(newlen);
1797 if (newval == NULL)
1798 break;
1799 STRCPY(newval, s);
1800 }
1801 }
1802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001803 // locate newval[] in origval[] when removing it
1804 // and when adding to avoid duplicates
1805 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (removing || (flags & P_NODUP))
1807 {
1808 i = (int)STRLEN(newval);
1809 bs = 0;
1810 for (s = origval; *s; ++s)
1811 {
1812 if ((!(flags & P_COMMA)
1813 || s == origval
1814 || (s[-1] == ',' && !(bs & 1)))
1815 && STRNCMP(s, newval, i) == 0
1816 && (!(flags & P_COMMA)
1817 || s[i] == ','
1818 || s[i] == NUL))
1819 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 // Count backslashes. Only a comma with an
1821 // even number of backslashes or a single
1822 // backslash preceded by a comma before it
1823 // is recognized as a separator
Bram Moolenaar8f79acd2016-01-01 14:48:20 +01001824 if ((s > origval + 1
1825 && s[-1] == '\\'
1826 && s[-2] != ',')
1827 || (s == origval + 1
1828 && s[-1] == '\\'))
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 ++bs;
1831 else
1832 bs = 0;
1833 }
1834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001835 // do not add if already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if ((adding || prepending) && *s)
1837 {
1838 prepending = FALSE;
1839 adding = FALSE;
1840 STRCPY(newval, origval);
1841 }
1842 }
1843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001844 // concatenate the two strings; add a ',' if
1845 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (adding || prepending)
1847 {
1848 comma = ((flags & P_COMMA) && *origval != NUL
1849 && *newval != NUL);
1850 if (adding)
1851 {
1852 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001853 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001854 if (comma && i > 1
1855 && (flags & P_ONECOMMA) == P_ONECOMMA
1856 && origval[i - 1] == ','
1857 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001858 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 mch_memmove(newval + i + comma, newval,
1860 STRLEN(newval) + 1);
1861 mch_memmove(newval, origval, (size_t)i);
1862 }
1863 else
1864 {
1865 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001866 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 if (comma)
1869 newval[i] = ',';
1870 }
1871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001872 // Remove newval[] from origval[]. (Note: "i" has
1873 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (removing)
1875 {
1876 STRCPY(newval, origval);
1877 if (*s)
1878 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001879 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (flags & P_COMMA)
1881 {
1882 if (s == origval)
1883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001884 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (s[i] == ',')
1886 ++i;
1887 }
1888 else
1889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001890 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 --s;
1892 ++i;
1893 }
1894 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001895 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898
1899 if (flags & P_FLAGLIST)
1900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001901 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001902 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001904 // if options have P_FLAGLIST and
1905 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001906 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001908 if (*s != ',' && *(s + 1) == ','
1909 && vim_strchr(s + 2, *s) != NULL)
1910 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001911 // Remove the duplicated value and
1912 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001913 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001914 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001917 else
1918 {
1919 if ((!(flags & P_COMMA) || *s != ',')
1920 && vim_strchr(s + 1, *s) != NULL)
1921 {
1922 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001923 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001924 }
1925 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001926 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
1929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001930 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 arg = save_arg;
1932 new_value_alloced = TRUE;
1933 }
1934
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001935 /*
1936 * Set the new value.
1937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 *(char_u **)(varp) = newval;
1939
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001940#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001941 if (!starting
1942# ifdef FEAT_CRYPT
1943 && options[opt_idx].indir != PV_KEY
1944# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001945 && origval != NULL && newval != NULL)
1946 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // origval may be freed by
1948 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02001949 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001950 // newval (and varp) may become invalid if the
1951 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001952 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001953 if (origval_l != NULL)
1954 saved_origval_l = vim_strsave(origval_l);
1955 if (origval_g != NULL)
1956 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001957 }
Bram Moolenaar53744302015-07-17 17:38:22 +02001958#endif
1959
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001960 {
1961 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001962 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001963
1964 // When an option is set in the sandbox, from a
1965 // modeline or in secure mode, then deal with side
1966 // effects in secure mode. Also when the value was
1967 // set with the P_INSECURE flag and is not
1968 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001969 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001970#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001971 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001972#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001973 || (!value_is_replaced && (*p & P_INSECURE)))
1974 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001975
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001976 // Handle side effects, and set the global value
1977 // for ":set" on local options. Note: when setting
1978 // 'syntax' or 'filetype' autocommands may be
1979 // triggered that can cause havoc.
1980 errmsg = did_set_string_option(
1981 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01001982 new_value_alloced, oldval, errbuf,
1983 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001984
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001985 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001988#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001989 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001990 trigger_optionsset_string(
1991 opt_idx, opt_flags, saved_origval,
1992 saved_origval_l, saved_origval_g,
1993 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001994 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001995 vim_free(saved_origval_l);
1996 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001997 vim_free(saved_newval);
1998#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001999 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (errmsg != NULL)
2001 goto skip;
2002 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002003 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
2005 char_u *p;
2006
2007 if (nextchar == '&')
2008 {
2009 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002010 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 else
2013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002014 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002015 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (*p == '\\' && p[1] != NUL)
2017 ++p;
2018 nextchar = *p;
2019 *p = NUL;
2020 add_termcode(key_name, arg, FALSE);
2021 *p = nextchar;
2022 }
2023 if (full_screen)
2024 ttest(FALSE);
2025 redraw_all_later(CLEAR);
2026 }
2027 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002030 did_set_option(
2031 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033
2034skip:
2035 /*
2036 * Advance to next argument.
2037 * - skip until a blank found, taking care of backslashes
2038 * - skip blanks
2039 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2040 */
2041 for (i = 0; i < 2 ; ++i)
2042 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002043 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (*arg++ == '\\' && *arg != NUL)
2045 ++arg;
2046 arg = skipwhite(arg);
2047 if (*arg != '=')
2048 break;
2049 }
2050 }
2051
2052 if (errmsg != NULL)
2053 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002054 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002055 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (i + (arg - startarg) < IOSIZE)
2057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 STRCAT(IObuff, ": ");
2060 mch_memmove(IObuff + i, startarg, (arg - startarg));
2061 IObuff[i + (arg - startarg)] = NUL;
2062 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002063 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 trans_characters(IObuff, IOSIZE);
2065
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002066 ++no_wait_return; // wait_return done later
2067 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 --no_wait_return;
2069
2070 return FAIL;
2071 }
2072
2073 arg = skipwhite(arg);
2074 }
2075
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002076theend:
2077 if (silent_mode && did_show)
2078 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002079 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002080 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002081 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002082 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002083 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002084 out_flush();
2085 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002086 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002087 }
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 return OK;
2090}
2091
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002092/*
2093 * Call this when an option has been given a new value through a user command.
2094 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2095 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002096 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002097did_set_option(
2098 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002099 int opt_flags, // possibly with OPT_MODELINE
2100 int new_value, // value was replaced completely
2101 int value_checked) // value was checked to be safe, no need to set the
2102 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002103{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002104 long_u *p;
2105
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002106 options[opt_idx].flags |= P_WAS_SET;
2107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002108 // When an option is set in the sandbox, from a modeline or in secure mode
2109 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2110 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002111 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002112 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002113#ifdef HAVE_SANDBOX
2114 || sandbox != 0
2115#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002116 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002117 *p = *p | P_INSECURE;
2118 else if (new_value)
2119 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002120}
2121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122/*
2123 * Convert a key name or string into a key value.
2124 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002125 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002127 int
2128string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002131 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (*arg == '^')
2133 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002134 if (multi_byte)
2135 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 return *arg;
2137}
2138
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139#ifdef FEAT_TITLE
2140/*
2141 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2142 * maketitle() to create and display it.
2143 * When switching the title or icon off, call mch_restore_title() to get
2144 * the old value back.
2145 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002146 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002147did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 if (starting != NO_SCREEN
2150#ifdef FEAT_GUI
2151 && !gui.starting
2152#endif
2153 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155}
2156#endif
2157
2158/*
2159 * set_options_bin - called when 'bin' changes value.
2160 */
2161 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002162set_options_bin(
2163 int oldval,
2164 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002165 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 /*
2168 * The option values that are changed when 'bin' changes are
2169 * copied when 'bin is set and restored when 'bin' is reset.
2170 */
2171 if (newval)
2172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002173 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
2175 if (!(opt_flags & OPT_GLOBAL))
2176 {
2177 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2178 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2179 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2180 curbuf->b_p_et_nobin = curbuf->b_p_et;
2181 }
2182 if (!(opt_flags & OPT_LOCAL))
2183 {
2184 p_tw_nobin = p_tw;
2185 p_wm_nobin = p_wm;
2186 p_ml_nobin = p_ml;
2187 p_et_nobin = p_et;
2188 }
2189 }
2190
2191 if (!(opt_flags & OPT_GLOBAL))
2192 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 curbuf->b_p_tw = 0; // no automatic line wrap
2194 curbuf->b_p_wm = 0; // no automatic line wrap
2195 curbuf->b_p_ml = 0; // no modelines
2196 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198 if (!(opt_flags & OPT_LOCAL))
2199 {
2200 p_tw = 0;
2201 p_wm = 0;
2202 p_ml = FALSE;
2203 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
2209 if (!(opt_flags & OPT_GLOBAL))
2210 {
2211 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2212 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2213 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2214 curbuf->b_p_et = curbuf->b_p_et_nobin;
2215 }
2216 if (!(opt_flags & OPT_LOCAL))
2217 {
2218 p_tw = p_tw_nobin;
2219 p_wm = p_wm_nobin;
2220 p_ml = p_ml_nobin;
2221 p_et = p_et_nobin;
2222 }
2223 }
2224}
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226/*
2227 * Expand environment variables for some string options.
2228 * These string options cannot be indirect!
2229 * If "val" is NULL expand the current value of the option.
2230 * Return pointer to NameBuff, or NULL when not expanded.
2231 */
2232 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002233option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002235 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2237 return NULL;
2238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 // If val is longer than MAXPATHL no meaningful expansion can be done,
2240 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (val != NULL && STRLEN(val) > MAXPATHL)
2242 return NULL;
2243
2244 if (val == NULL)
2245 val = *(char_u **)options[opt_idx].var;
2246
2247 /*
2248 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2249 * Escape spaces when expanding 'tags', they are used to separate file
2250 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002251 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
2253 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002254 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002255#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002256 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2257#endif
2258 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002259 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 return NULL;
2261
2262 return NameBuff;
2263}
2264
2265/*
2266 * After setting various option values: recompute variables that depend on
2267 * option values.
2268 */
2269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002270didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002272 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 (void)init_chartab();
2274
Bram Moolenaardac13472019-09-16 21:06:21 +02002275 didset_string_options();
2276
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002277#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002278 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002279 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002280 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002281 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002284 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 (void)check_cedit();
2286#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002287#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002288 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002289 fill_breakat_flags();
2290#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002291 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002292}
2293
2294/*
2295 * More side effects of setting options.
2296 */
2297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002298didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002299{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002300 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002301 (void)highlight_changed();
2302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002304 check_opt_wim();
2305
2306 (void)set_chars_option(&p_lcs);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002307 // Parse default for 'fillchars'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002308 (void)set_chars_option(&p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002309
2310#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002311 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002312 (void)check_clipboard_option();
2313#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002314#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002315 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002316 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002317 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002318 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320}
2321
2322/*
2323 * Check for string options that are NULL (normally only termcap options).
2324 */
2325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002326check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 int opt_idx;
2329
2330 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2331 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2332 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2333}
2334
2335/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002336 * Return the option index found by a pointer into term_strings[].
2337 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002339 int
2340get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002342 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2345 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002346 return opt_idx;
2347 return -1; // cannot happen: didn't find it!
2348}
2349
2350/*
2351 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2352 * Return the option index or -1 if not found.
2353 */
2354 int
2355set_term_option_alloced(char_u **p)
2356{
2357 int opt_idx = get_term_opt_idx(p);
2358
2359 if (opt_idx >= 0)
2360 options[opt_idx].flags |= P_ALLOCED;
2361 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362}
2363
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002364#if defined(FEAT_EVAL) || defined(PROTO)
2365/*
2366 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2367 * Return FALSE when it wasn't.
2368 * Return -1 for an unknown option.
2369 */
2370 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002371was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002372{
2373 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002374 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002375
2376 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002377 {
2378 flagp = insecure_flag(idx, opt_flags);
2379 return (*flagp & P_INSECURE) != 0;
2380 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002381 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002382 return -1;
2383}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002384
2385/*
2386 * Get a pointer to the flags used for the P_INSECURE flag of option
2387 * "opt_idx". For some local options a local flags field is used.
2388 */
2389 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002390insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002391{
2392 if (opt_flags & OPT_LOCAL)
2393 switch ((int)options[opt_idx].indir)
2394 {
2395#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002396 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002397#endif
2398#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002399# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002400 case PV_FDE: return &curwin->w_p_fde_flags;
2401 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002402# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002403# ifdef FEAT_BEVAL
2404 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2405# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002406# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002407 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002408# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002409 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002410# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002411 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002412# endif
2413#endif
2414 }
2415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002416 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002417 return &options[opt_idx].flags;
2418}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002419#endif
2420
Bram Moolenaardac13472019-09-16 21:06:21 +02002421#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002422/*
2423 * Redraw the window title and/or tab page text later.
2424 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002425void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002426{
2427 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002428 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002429}
2430#endif
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002433 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2434 * characters or characters in "allowed".
2435 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002436 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002437valid_name(char_u *val, char *allowed)
2438{
2439 char_u *s;
2440
2441 for (s = val; *s != NUL; ++s)
2442 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2443 return FALSE;
2444 return TRUE;
2445}
2446
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002447#if defined(FEAT_EVAL) || defined(PROTO)
2448/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002449 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002450 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002451 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002452 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002453set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002454{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002455 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2456 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002457 sctx_T new_script_ctx = script_ctx;
2458
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002459 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002461 // Remember where the option was set. For local options need to do that
2462 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002463 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002464 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002465 if (both || (opt_flags & OPT_LOCAL))
2466 {
2467 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002468 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002469 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002470 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002471 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002472}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002473
2474/*
2475 * Set the script_ctx for a termcap option.
2476 * "name" must be the two character code, e.g. "RV".
2477 * When "name" is NULL use "opt_idx".
2478 */
2479 void
2480set_term_option_sctx_idx(char *name, int opt_idx)
2481{
2482 char_u buf[5];
2483 int idx;
2484
2485 if (name == NULL)
2486 idx = opt_idx;
2487 else
2488 {
2489 buf[0] = 't';
2490 buf[1] = '_';
2491 buf[2] = name[0];
2492 buf[3] = name[1];
2493 buf[4] = 0;
2494 idx = findoption(buf);
2495 }
2496 if (idx >= 0)
2497 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2498}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002499#endif
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
2502 * Set the value of a boolean option, and take care of side effects.
2503 * Returns NULL for success, or an error message for an error.
2504 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002505 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002506set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002507 int opt_idx, // index in options[] table
2508 char_u *varp, // pointer to the option variable
2509 int value, // new value
2510 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002513#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002514 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002517 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 if ((secure
2519#ifdef HAVE_SANDBOX
2520 || sandbox != 0
2521#endif
2522 ) && (options[opt_idx].flags & P_SECURE))
2523 return e_secure;
2524
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002525#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002526 // Save the global value before changing anything. This is needed as for
2527 // a global-only option setting the "local value" in fact sets the global
2528 // value (since there is only one value).
2529 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2530 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2531 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002532#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002534 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002536 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002537 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538#endif
2539
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002540#ifdef FEAT_GUI
2541 need_mouse_correct = TRUE;
2542#endif
2543
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002544 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2546 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2547
2548 /*
2549 * Handle side effects of changing a bool option.
2550 */
2551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002552 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaar920694c2016-08-21 17:45:02 +02002556#ifdef FEAT_LANGMAP
2557 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002558 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002559 p_lnr = !p_lrm;
2560 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002561 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002562 p_lrm = !p_lnr;
2563#endif
2564
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002565#ifdef FEAT_SYN_HL
2566 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2567 reset_cursorline();
2568#endif
2569
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002570#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002571 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002572 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2573 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002574 // Only take action when the option was set. When reset we do not
2575 // delete the undo file, the option may be set again without making
2576 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002577 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002578 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002579 char_u hash[UNDO_HASH_SIZE];
2580 buf_T *save_curbuf = curbuf;
2581
Bram Moolenaar29323592016-07-24 22:04:11 +02002582 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002583 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002584 // When 'undofile' is set globally: for every buffer, otherwise
2585 // only for the current buffer: Try to read in the undofile,
2586 // if one exists, the buffer wasn't changed and the buffer was
2587 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002588 if ((curbuf == save_curbuf
2589 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2590 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2591 {
2592 u_compute_hash(hash);
2593 u_read_undo(NULL, hash, curbuf->b_fname);
2594 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002595 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002596 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002597 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002598 }
2599#endif
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 else if ((int *)varp == &curbuf->b_p_ro)
2602 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002603 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2605 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002607 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002608 if (curbuf->b_p_ro)
2609 curbuf->b_did_warn = FALSE;
2610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002612 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614 }
2615
Bram Moolenaar9c449722010-07-20 18:44:27 +02002616#ifdef FEAT_GUI
2617 else if ((int *)varp == &p_mh)
2618 {
2619 if (!p_mh)
2620 gui_mch_mousehide(FALSE);
2621 }
2622#endif
2623
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002624 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002626 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002627# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002628 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002629 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2630 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002631 {
2632 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002633 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002634 }
2635# endif
2636# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002637 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002638# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002639 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002640#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002641 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002643 {
2644 redraw_titles();
2645 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002646 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002647 else if ((int *)varp == &curbuf->b_p_fixeol)
2648 {
2649 redraw_titles();
2650 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002651 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002652 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002653 {
2654 redraw_titles();
2655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656#endif
2657
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 else if ((int *)varp == &curbuf->b_p_bin)
2660 {
2661 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2662#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002663 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665 }
2666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002667 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2669 {
2670 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2671 NULL, NULL, TRUE, curbuf);
2672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002674 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 else if ((int *)varp == &curbuf->b_p_swf)
2676 {
2677 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002680 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2681 // buf->b_p_swf
2682 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
2684
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002685 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 else if ((int *)varp == &p_terse)
2687 {
2688 char_u *p;
2689
2690 p = vim_strchr(p_shm, SHM_SEARCH);
2691
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (p_terse && p == NULL)
2694 {
2695 STRCPY(IObuff, p_shm);
2696 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002697 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002701 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else if ((int *)varp == &p_paste)
2706 {
2707 paste_option_changed();
2708 }
2709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002710 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else if ((int *)varp == &p_im)
2712 {
2713 if (p_im)
2714 {
2715 if ((State & INSERT) == 0)
2716 need_start_insertmode = TRUE;
2717 stop_insert_mode = FALSE;
2718 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002720 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 need_start_insertmode = FALSE;
2723 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002724 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 restart_edit = 0;
2727 }
2728 }
2729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 else if ((int *)varp == &p_ic && p_hls)
2732 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002733 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735
2736#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 else if ((int *)varp == &p_hls)
2739 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002740 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742#endif
2743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2745 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 else if ((int *)varp == &curwin->w_p_scb)
2747 {
2748 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002751 curwin->w_scbind_pos = curwin->w_topline;
2752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
Bram Moolenaar4033c552017-09-16 20:54:51 +02002755#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 else if ((int *)varp == &curwin->w_p_pvw)
2758 {
2759 if (curwin->w_p_pvw)
2760 {
2761 win_T *win;
2762
Bram Moolenaar29323592016-07-24 22:04:11 +02002763 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (win->w_p_pvw && win != curwin)
2765 {
2766 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002767 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 }
2770 }
2771#endif
2772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else if ((int *)varp == &curbuf->b_p_tx)
2775 {
2776 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2777 }
2778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 set_string_option_direct((char_u *)"ffs", -1,
2783 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002784 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /*
2788 * When 'lisp' option changes include/exclude '-' in
2789 * keyword characters.
2790 */
2791#ifdef FEAT_LISP
2792 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2793 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002794 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796#endif
2797
2798#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002800 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002802 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804#endif
2805
2806 else if ((int *)varp == &curbuf->b_changed)
2807 {
2808 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002809 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002811 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815
2816#ifdef BACKSLASH_IN_FILENAME
2817 else if ((int *)varp == &p_ssl)
2818 {
2819 if (p_ssl)
2820 {
2821 psepc = '/';
2822 psepcN = '\\';
2823 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 else
2826 {
2827 psepc = '\\';
2828 psepcN = '/';
2829 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
2831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 buflist_slash_adjust();
2834 alist_slash_adjust();
2835# ifdef FEAT_EVAL
2836 scriptnames_slash_adjust();
2837# endif
2838 }
2839#endif
2840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 else if ((int *)varp == &curwin->w_p_wrap)
2843 {
2844 if (curwin->w_p_wrap)
2845 curwin->w_leftcol = 0;
2846 }
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 else if ((int *)varp == &p_ea)
2849 {
2850 if (p_ea && !old_value)
2851 win_equal(curwin, FALSE, 0);
2852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 else if ((int *)varp == &p_wiv)
2855 {
2856 /*
2857 * When 'weirdinvert' changed, set/reset 't_xs'.
2858 * Then set 'weirdinvert' according to value of 't_xs'.
2859 */
2860 if (p_wiv && !old_value)
2861 T_XS = (char_u *)"y";
2862 else if (!p_wiv && old_value)
2863 T_XS = empty_option;
2864 p_wiv = (*T_XS != NUL);
2865 }
2866
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002867#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else if ((int *)varp == &p_beval)
2869 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002870 if (!balloonEvalForTerm)
2871 {
2872 if (p_beval && !old_value)
2873 gui_mch_enable_beval_area(balloonEval);
2874 else if (!p_beval && old_value)
2875 gui_mch_disable_beval_area(balloonEval);
2876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002878#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002879#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002880 else if ((int *)varp == &p_bevalterm)
2881 {
2882 mch_bevalterm_changed();
2883 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002886#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 else if ((int *)varp == &p_acd)
2888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002890 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892#endif
2893
2894#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 else if ((int *)varp == &curwin->w_p_diff)
2897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002899 diff_buf_adjust(curwin);
2900# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (foldmethodIsDiff(curwin))
2902 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
2905#endif
2906
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002907#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 else if ((int *)varp == &p_imdisable)
2910 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 if (p_imdisable)
2913 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02002914 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // When the option is set from an autocommand, it may need to take
2916 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02002917 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919#endif
2920
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002921#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002923 else if ((int *)varp == &curwin->w_p_spell)
2924 {
2925 if (curwin->w_p_spell)
2926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002927 char *errmsg = did_set_spelllang(curwin);
2928
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002929 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002930 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002931 }
2932 }
2933#endif
2934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#ifdef FEAT_ARABIC
2936 if ((int *)varp == &curwin->w_p_arab)
2937 {
2938 if (curwin->w_p_arab)
2939 {
2940 /*
2941 * 'arabic' is set, handle various sub-settings.
2942 */
2943 if (!p_tbidi)
2944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (!curwin->w_p_rl)
2947 {
2948 curwin->w_p_rl = TRUE;
2949 changed_window_setting();
2950 }
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (!p_arshape)
2954 {
2955 p_arshape = TRUE;
2956 redraw_later_clear();
2957 }
2958 }
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 // Arabic requires a utf-8 encoding, inform the user if its not
2961 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002963 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00002964 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
2965
Bram Moolenaar8820b482017-03-16 17:23:31 +01002966 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01002967 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00002968#ifdef FEAT_EVAL
2969 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
2970#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
2979 OPT_LOCAL);
2980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 else
2983 {
2984 /*
2985 * 'arabic' is reset, handle various sub-settings.
2986 */
2987 if (!p_tbidi)
2988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (curwin->w_p_rl)
2991 {
2992 curwin->w_p_rl = FALSE;
2993 changed_window_setting();
2994 }
2995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 // 'arabicshape' isn't reset, it is a global option and
2997 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
2999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // 'delcombine' isn't reset, it is a global option and another
3001 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 curbuf->b_p_iminsert = B_IMODE_NONE;
3006 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3007# endif
3008 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003009 }
3010
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003011#endif
3012
Bram Moolenaar44826212019-08-22 21:23:20 +02003013#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3014 else if (((int *)varp == &curwin->w_p_nu
3015 || (int *)varp == &curwin->w_p_rnu)
3016 && gui.in_use
3017 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3018 && curbuf->b_signlist != NULL)
3019 {
3020 // If the 'number' or 'relativenumber' options are modified and
3021 // 'signcolumn' is set to 'number', then clear the screen for a full
3022 // refresh. Otherwise the sign icons are not displayed properly in the
3023 // number column. If the 'number' option is set and only the
3024 // 'relativenumber' option is toggled, then don't refresh the screen
3025 // (optimization).
3026 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3027 redraw_all_later(CLEAR);
3028 }
3029#endif
3030
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003031#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003032 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003033 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003034 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003035# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003037 if (
3038# ifdef VIMDLL
3039 !gui.in_use && !gui.starting &&
3040# endif
3041 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003042 {
3043 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003044 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003045 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003046 if (is_term_win32())
3047 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003048# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003049# ifdef FEAT_GUI
3050 if (!gui.in_use && !gui.starting)
3051# endif
3052 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003053# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003054 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003055 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003056 {
3057 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003058 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003059 init_highlight(TRUE, FALSE);
3060 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003061# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003062 }
3063#endif
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 /*
3066 * End of handling side effects for bool options.
3067 */
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 options[opt_idx].flags |= P_WAS_SET;
3072
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003073#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003074 // Don't do this while starting up or recursively.
3075 if (!starting && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02003076 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02003077 char_u buf_old[2], buf_old_global[2], buf_new[2], buf_type[7];
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003078
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003079 vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003080 vim_snprintf((char *)buf_old_global, 2, "%d",
3081 old_global_value ? TRUE: FALSE);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003082 vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003083 vim_snprintf((char *)buf_type, 7, "%s",
3084 (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02003085 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3086 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3087 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003088 if (opt_flags & OPT_LOCAL)
3089 {
3090 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3091 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3092 }
3093 if (opt_flags & OPT_GLOBAL)
3094 {
3095 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3096 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3097 }
3098 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3099 {
3100 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3101 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3102 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3103 }
3104 if (opt_flags & OPT_MODELINE)
3105 {
3106 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3107 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3108 }
3109 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3110 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003111 reset_v_option_vars();
3112 }
3113#endif
3114
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003116 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003117 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003118 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 check_redraw(options[opt_idx].flags);
3120
3121 return NULL;
3122}
3123
3124/*
3125 * Set the value of a number option, and take care of side effects.
3126 * Returns NULL for success, or an error message for an error.
3127 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003129set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130 int opt_idx, // index in options[] table
3131 char_u *varp, // pointer to the option variable
3132 long value, // new value
3133 char *errbuf, // buffer for error messages
3134 size_t errbuflen, // length of "errbuf"
3135 int opt_flags) // OPT_LOCAL, OPT_GLOBAL and
3136 // OPT_MODELINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003138 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003140#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003141 long old_global_value = 0; // only used when setting a local and
3142 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003143#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003144 long old_Rows = Rows; // remember old Rows
3145 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 long *pp = (long *)varp;
3147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003148 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003149 if ((secure
3150#ifdef HAVE_SANDBOX
3151 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003153 ) && (options[opt_idx].flags & P_SECURE))
3154 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003156#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003157 // Save the global value before changing anything. This is needed as for
3158 // a global-only option setting the "local value" infact sets the global
3159 // value (since there is only one value).
3160 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003161 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3162 OPT_GLOBAL);
3163#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 *pp = value;
3166#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003168 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003170#ifdef FEAT_GUI
3171 need_mouse_correct = TRUE;
3172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaar14f24742012-08-08 18:01:05 +02003174 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003177#ifdef FEAT_VARTABS
3178 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3179 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3180 ? tabstop_first(curbuf->b_p_vts_array)
3181 : curbuf->b_p_ts;
3182#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186
3187 /*
3188 * Number options that need some action when changed
3189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (pp == &p_wh || pp == &p_hh)
3191 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003192 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (p_wh < 1)
3194 {
3195 errmsg = e_positive;
3196 p_wh = 1;
3197 }
3198 if (p_wmh > p_wh)
3199 {
3200 errmsg = e_winheight;
3201 p_wh = p_wmh;
3202 }
3203 if (p_hh < 0)
3204 {
3205 errmsg = e_positive;
3206 p_hh = 0;
3207 }
3208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003209 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003210 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 if (pp == &p_wh && curwin->w_height < p_wh)
3213 win_setheight((int)p_wh);
3214 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3215 win_setheight((int)p_hh);
3216 }
3217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 else if (pp == &p_wmh)
3219 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003220 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (p_wmh < 0)
3222 {
3223 errmsg = e_positive;
3224 p_wmh = 0;
3225 }
3226 if (p_wmh > p_wh)
3227 {
3228 errmsg = e_winheight;
3229 p_wmh = p_wh;
3230 }
3231 win_setminheight();
3232 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003233 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003235 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 if (p_wiw < 1)
3237 {
3238 errmsg = e_positive;
3239 p_wiw = 1;
3240 }
3241 if (p_wmw > p_wiw)
3242 {
3243 errmsg = e_winwidth;
3244 p_wiw = p_wmw;
3245 }
3246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003247 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003248 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 win_setwidth((int)p_wiw);
3250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 else if (pp == &p_wmw)
3252 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003253 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 if (p_wmw < 0)
3255 {
3256 errmsg = e_positive;
3257 p_wmw = 0;
3258 }
3259 if (p_wmw > p_wiw)
3260 {
3261 errmsg = e_winwidth;
3262 p_wmw = p_wiw;
3263 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003264 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 else if (pp == &p_ls)
3269 {
3270 last_status(FALSE);
3271 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003273 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003274 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003275 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003276 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
3279#ifdef FEAT_GUI
3280 else if (pp == &p_linespace)
3281 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 // Recompute gui.char_height and resize the Vim window to keep the
3283 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003284 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003285 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287#endif
3288
3289#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else if (pp == &curwin->w_p_fdl)
3292 {
3293 if (curwin->w_p_fdl < 0)
3294 curwin->w_p_fdl = 0;
3295 newFoldLevel();
3296 }
3297
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003298 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 else if (pp == &curwin->w_p_fml)
3300 {
3301 foldUpdateAll(curwin);
3302 }
3303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 else if (pp == &curwin->w_p_fdn)
3306 {
3307 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3308 foldUpdateAll(curwin);
3309 }
3310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003311 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 else if (pp == &curwin->w_p_fdc)
3313 {
3314 if (curwin->w_p_fdc < 0)
3315 {
3316 errmsg = e_positive;
3317 curwin->w_p_fdc = 0;
3318 }
3319 else if (curwin->w_p_fdc > 12)
3320 {
3321 errmsg = e_invarg;
3322 curwin->w_p_fdc = 12;
3323 }
3324 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003327#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003328 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3330 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003331# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (foldmethodIsIndent(curwin))
3333 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003334# endif
3335# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003336 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3337 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003338 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3339 parse_cino(curbuf);
3340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003344 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003345 else if (pp == &p_mco)
3346 {
3347 if (p_mco > MAX_MCO)
3348 p_mco = MAX_MCO;
3349 else if (p_mco < 0)
3350 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003351 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003352 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 else if (pp == &curbuf->b_p_iminsert)
3355 {
3356 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3357 {
3358 errmsg = e_invarg;
3359 curbuf->b_p_iminsert = B_IMODE_NONE;
3360 }
3361 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003362 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003364#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003365 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 status_redraw_curbuf();
3367#endif
3368 }
3369
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003370#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003371 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003372 else if (pp == &p_imst)
3373 {
3374 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3375 errmsg = e_invarg;
3376 }
3377#endif
3378
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003379 else if (pp == &p_window)
3380 {
3381 if (p_window < 1)
3382 p_window = 1;
3383 else if (p_window >= Rows)
3384 p_window = Rows - 1;
3385 }
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else if (pp == &curbuf->b_p_imsearch)
3388 {
3389 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3390 {
3391 errmsg = e_invarg;
3392 curbuf->b_p_imsearch = B_IMODE_NONE;
3393 }
3394 p_imsearch = curbuf->b_p_imsearch;
3395 }
3396
3397#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003398 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else if (pp == &p_titlelen)
3400 {
3401 if (p_titlelen < 0)
3402 {
3403 errmsg = e_positive;
3404 p_titlelen = 85;
3405 }
3406 if (starting != NO_SCREEN && old_value != p_titlelen)
3407 need_maketitle = TRUE;
3408 }
3409#endif
3410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003411 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 else if (pp == &p_ch)
3413 {
3414 if (p_ch < 1)
3415 {
3416 errmsg = e_positive;
3417 p_ch = 1;
3418 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003419 if (p_ch > Rows - min_rows() + 1)
3420 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003422 // Only compute the new window layout when startup has been
3423 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (p_ch != old_value && full_screen
3425#ifdef FEAT_GUI
3426 && !gui.starting
3427#endif
3428 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003429 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003432 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 else if (pp == &p_uc)
3434 {
3435 if (p_uc < 0)
3436 {
3437 errmsg = e_positive;
3438 p_uc = 100;
3439 }
3440 if (p_uc && !old_value)
3441 ml_open_files();
3442 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003443#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003444 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003445 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003446 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003447 {
3448 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003449 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003450 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003451 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003452 {
3453 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003454 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003455 }
3456 }
3457#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003458#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003459 else if (pp == &p_mzq)
3460 mzvim_reset_timer();
3461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003463#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003465 else if (pp == &p_pyx)
3466 {
3467 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3468 errmsg = e_invarg;
3469 }
3470#endif
3471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003472 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 else if (pp == &p_ul)
3474 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003475 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003477 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 p_ul = value;
3479 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003480 else if (pp == &curbuf->b_p_ul)
3481 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003483 curbuf->b_p_ul = old_value;
3484 u_sync(TRUE);
3485 curbuf->b_p_ul = value;
3486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003488#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003489 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003490 else if (pp == &curwin->w_p_nuw)
3491 {
3492 if (curwin->w_p_nuw < 1)
3493 {
3494 errmsg = e_positive;
3495 curwin->w_p_nuw = 1;
3496 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003497 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003498 {
3499 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003500 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003501 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003502 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003503 }
3504#endif
3505
Bram Moolenaar1a384422010-07-14 19:53:30 +02003506 else if (pp == &curbuf->b_p_tw)
3507 {
3508 if (curbuf->b_p_tw < 0)
3509 {
3510 errmsg = e_positive;
3511 curbuf->b_p_tw = 0;
3512 }
3513#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003514 {
3515 win_T *wp;
3516 tabpage_T *tp;
3517
3518 FOR_ALL_TAB_WINDOWS(tp, wp)
3519 check_colorcolumn(wp);
3520 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003521#endif
3522 }
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 /*
3525 * Check the bounds for numeric options here
3526 */
3527 if (Rows < min_rows() && full_screen)
3528 {
3529 if (errbuf != NULL)
3530 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003531 vim_snprintf((char *)errbuf, errbuflen,
3532 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 errmsg = errbuf;
3534 }
3535 Rows = min_rows();
3536 }
3537 if (Columns < MIN_COLUMNS && full_screen)
3538 {
3539 if (errbuf != NULL)
3540 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003541 vim_snprintf((char *)errbuf, errbuflen,
3542 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 errmsg = errbuf;
3544 }
3545 Columns = MIN_COLUMNS;
3546 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003547 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 /*
3550 * If the screen (shell) height has been changed, assume it is the
3551 * physical screenheight.
3552 */
3553 if (old_Rows != Rows || old_Columns != Columns)
3554 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (updating_screen)
3557 *pp = old_value;
3558 else if (full_screen
3559#ifdef FEAT_GUI
3560 && !gui.starting
3561#endif
3562 )
3563 set_shellsize((int)Columns, (int)Rows, TRUE);
3564 else
3565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 // Postpone the resizing; check the size and cmdline position for
3567 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 check_shellsize();
3569 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3570 cmdline_row = Rows - p_ch;
3571 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003572 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003573 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (curbuf->b_p_ts <= 0)
3577 {
3578 errmsg = e_positive;
3579 curbuf->b_p_ts = 8;
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (p_tm < 0)
3582 {
3583 errmsg = e_positive;
3584 p_tm = 0;
3585 }
3586 if ((curwin->w_p_scr <= 0
3587 || (curwin->w_p_scr > curwin->w_height
3588 && curwin->w_height > 0))
3589 && full_screen)
3590 {
3591 if (pp == &(curwin->w_p_scr))
3592 {
3593 if (curwin->w_p_scr != 0)
3594 errmsg = e_scroll;
3595 win_comp_scroll(curwin);
3596 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // If 'scroll' became invalid because of a side effect silently adjust
3598 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else if (curwin->w_p_scr <= 0)
3600 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003601 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 curwin->w_p_scr = curwin->w_height;
3603 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003604 if (p_hi < 0)
3605 {
3606 errmsg = e_positive;
3607 p_hi = 0;
3608 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003609 else if (p_hi > 10000)
3610 {
3611 errmsg = e_invarg;
3612 p_hi = 10000;
3613 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003614 if (p_re < 0 || p_re > 2)
3615 {
3616 errmsg = e_invarg;
3617 p_re = 0;
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (p_report < 0)
3620 {
3621 errmsg = e_positive;
3622 p_report = 1;
3623 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003624 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 p_sj = Rows / 2;
3628 else
3629 {
3630 errmsg = e_scroll;
3631 p_sj = 1;
3632 }
3633 }
3634 if (p_so < 0 && full_screen)
3635 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003636 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 p_so = 0;
3638 }
3639 if (p_siso < 0 && full_screen)
3640 {
3641 errmsg = e_positive;
3642 p_siso = 0;
3643 }
3644#ifdef FEAT_CMDWIN
3645 if (p_cwh < 1)
3646 {
3647 errmsg = e_positive;
3648 p_cwh = 1;
3649 }
3650#endif
3651 if (p_ut < 0)
3652 {
3653 errmsg = e_positive;
3654 p_ut = 2000;
3655 }
3656 if (p_ss < 0)
3657 {
3658 errmsg = e_positive;
3659 p_ss = 0;
3660 }
3661
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003662 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3664 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3665
3666 options[opt_idx].flags |= P_WAS_SET;
3667
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003668#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003669 // Don't do this while starting up, failure or recursively.
3670 if (!starting && errmsg == NULL && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02003671 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02003672 char_u buf_old[11], buf_old_global[11], buf_new[11], buf_type[7];
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003673 vim_snprintf((char *)buf_old, 10, "%ld", old_value);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003674 vim_snprintf((char *)buf_old_global, 10, "%ld", old_global_value);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003675 vim_snprintf((char *)buf_new, 10, "%ld", value);
3676 vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02003677 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3678 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3679 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003680 if (opt_flags & OPT_LOCAL)
3681 {
3682 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3683 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3684 }
3685 if (opt_flags & OPT_GLOBAL)
3686 {
3687 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3688 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3689 }
3690 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3691 {
3692 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3693 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3694 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3695 }
3696 if (opt_flags & OPT_MODELINE)
3697 {
3698 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3699 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3700 }
3701 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3702 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003703 reset_v_option_vars();
3704 }
3705#endif
3706
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003707 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003708 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003709 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003710 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 check_redraw(options[opt_idx].flags);
3712
3713 return errmsg;
3714}
3715
3716/*
3717 * Called after an option changed: check if something needs to be redrawn.
3718 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003719 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003720check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003722 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003723 int doclear = (flags & P_RCLR) == P_RCLR;
3724 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3730 changed_window_setting();
3731 if (flags & P_RBUF)
3732 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003733 if (flags & P_RWINONLY)
3734 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003735 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 redraw_all_later(CLEAR);
3737 else if (all)
3738 redraw_all_later(NOT_VALID);
3739}
3740
3741/*
3742 * Find index for option 'arg'.
3743 * Return -1 if not found.
3744 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003745 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003746findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
3748 int opt_idx;
3749 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003750 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 int is_term_opt;
3752
3753 /*
3754 * For first call: Initialize the quick-access table.
3755 * It contains the index for the first option that starts with a certain
3756 * letter. There are 26 letters, plus the first "t_" option.
3757 */
3758 if (quick_tab[1] == 0)
3759 {
3760 p = options[0].fullname;
3761 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3762 {
3763 if (s[0] != p[0])
3764 {
3765 if (s[0] == 't' && s[1] == '_')
3766 quick_tab[26] = opt_idx;
3767 else
3768 quick_tab[CharOrdLow(s[0])] = opt_idx;
3769 }
3770 p = s;
3771 }
3772 }
3773
3774 /*
3775 * Check for name starting with an illegal character.
3776 */
3777#ifdef EBCDIC
3778 if (!islower(arg[0]))
3779#else
3780 if (arg[0] < 'a' || arg[0] > 'z')
3781#endif
3782 return -1;
3783
3784 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3785 if (is_term_opt)
3786 opt_idx = quick_tab[26];
3787 else
3788 opt_idx = quick_tab[CharOrdLow(arg[0])];
3789 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003791 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 break;
3793 }
3794 if (s == NULL && !is_term_opt)
3795 {
3796 opt_idx = quick_tab[CharOrdLow(arg[0])];
3797 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3798 {
3799 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003800 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 break;
3802 s = NULL;
3803 }
3804 }
3805 if (s == NULL)
3806 opt_idx = -1;
3807 return opt_idx;
3808}
3809
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003810#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811/*
3812 * Get the value for an option.
3813 *
3814 * Returns:
3815 * Number or Toggle option: 1, *numval gets value.
3816 * String option: 0, *stringval gets allocated string.
3817 * Hidden Number or Toggle option: -1.
3818 * hidden String option: -2.
3819 * unknown option: -3.
3820 */
3821 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003822get_option_value(
3823 char_u *name,
3824 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003825 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003826 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
3828 int opt_idx;
3829 char_u *varp;
3830
3831 opt_idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003832 if (opt_idx < 0) // unknown option
Bram Moolenaare353c402017-02-04 19:49:16 +01003833 {
3834 int key;
3835
3836 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02003837 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003838 {
3839 char_u key_name[2];
3840 char_u *p;
3841
3842 if (key < 0)
3843 {
3844 key_name[0] = KEY2TERMCAP0(key);
3845 key_name[1] = KEY2TERMCAP1(key);
3846 }
3847 else
3848 {
3849 key_name[0] = KS_KEY;
3850 key_name[1] = (key & 0xff);
3851 }
3852 p = find_termcode(key_name);
3853 if (p != NULL)
3854 {
3855 if (stringval != NULL)
3856 *stringval = vim_strsave(p);
3857 return 0;
3858 }
3859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 return -3;
Bram Moolenaare353c402017-02-04 19:49:16 +01003861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3864
3865 if (options[opt_idx].flags & P_STRING)
3866 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003867 if (varp == NULL) // hidden option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return -2;
3869 if (stringval != NULL)
3870 {
3871#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003872 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003873 if ((char_u **)varp == &curbuf->b_p_key
3874 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 *stringval = vim_strsave((char_u *)"*****");
3876 else
3877#endif
3878 *stringval = vim_strsave(*(char_u **)(varp));
3879 }
3880 return 0;
3881 }
3882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003883 if (varp == NULL) // hidden option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 return -1;
3885 if (options[opt_idx].flags & P_NUM)
3886 *numval = *(long *)varp;
3887 else
3888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003889 // Special case: 'modified' is b_changed, but we also want to consider
3890 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if ((int *)varp == &curbuf->b_changed)
3892 *numval = curbufIsChanged();
3893 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003894 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 return 1;
3897}
3898#endif
3899
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003900#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003901/*
3902 * Returns the option attributes and its value. Unlike the above function it
3903 * will return either global value or local value of the option depending on
3904 * what was requested, but it will never return global value if it was
3905 * requested to return local one and vice versa. Neither it will return
3906 * buffer-local value if it was requested to return window-local one.
3907 *
3908 * Pretends that option is absent if it is not present in the requested scope
3909 * (i.e. has no global, window-local or buffer-local value depending on
3910 * opt_type). Uses
3911 *
3912 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003913 * 0 hidden or unknown option, also option that does not have requested
3914 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003915 * see SOPT_* in vim.h for other flags
3916 *
3917 * Possible opt_type values: see SREQ_* in vim.h
3918 */
3919 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003920get_option_value_strict(
3921 char_u *name,
3922 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003923 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003924 int opt_type,
3925 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003926{
3927 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003928 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003929 struct vimoption *p;
3930 int r = 0;
3931
3932 opt_idx = findoption(name);
3933 if (opt_idx < 0)
3934 return 0;
3935
3936 p = &(options[opt_idx]);
3937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003938 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003939 if (p->var == NULL)
3940 return 0;
3941
3942 if (p->flags & P_BOOL)
3943 r |= SOPT_BOOL;
3944 else if (p->flags & P_NUM)
3945 r |= SOPT_NUM;
3946 else if (p->flags & P_STRING)
3947 r |= SOPT_STRING;
3948
3949 if (p->indir == PV_NONE)
3950 {
3951 if (opt_type == SREQ_GLOBAL)
3952 r |= SOPT_GLOBAL;
3953 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003954 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003955 }
3956 else
3957 {
3958 if (p->indir & PV_BOTH)
3959 r |= SOPT_GLOBAL;
3960 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003961 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003962
3963 if (p->indir & PV_WIN)
3964 {
3965 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003967 else
3968 r |= SOPT_WIN;
3969 }
3970 else if (p->indir & PV_BUF)
3971 {
3972 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003973 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003974 else
3975 r |= SOPT_BUF;
3976 }
3977 }
3978
3979 if (stringval == NULL)
3980 return r;
3981
3982 if (opt_type == SREQ_GLOBAL)
3983 varp = p->var;
3984 else
3985 {
3986 if (opt_type == SREQ_BUF)
3987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003988 // Special case: 'modified' is b_changed, but we also want to
3989 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003990 if (p->indir == PV_MOD)
3991 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02003992 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003993 varp = NULL;
3994 }
3995#ifdef FEAT_CRYPT
3996 else if (p->indir == PV_KEY)
3997 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003998 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003999 *stringval = NULL;
4000 varp = NULL;
4001 }
4002#endif
4003 else
4004 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004005 buf_T *save_curbuf = curbuf;
4006
4007 // only getting a pointer, no need to use aucmd_prepbuf()
4008 curbuf = (buf_T *)from;
4009 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004010 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004011 curbuf = save_curbuf;
4012 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004013 }
4014 }
4015 else if (opt_type == SREQ_WIN)
4016 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004017 win_T *save_curwin = curwin;
4018
4019 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004020 curbuf = curwin->w_buffer;
4021 varp = get_varp(p);
4022 curwin = save_curwin;
4023 curbuf = curwin->w_buffer;
4024 }
4025 if (varp == p->var)
4026 return (r | SOPT_UNSET);
4027 }
4028
4029 if (varp != NULL)
4030 {
4031 if (p->flags & P_STRING)
4032 *stringval = vim_strsave(*(char_u **)(varp));
4033 else if (p->flags & P_NUM)
4034 *numval = *(long *) varp;
4035 else
4036 *numval = *(int *)varp;
4037 }
4038
4039 return r;
4040}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004041
4042/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004043 * Iterate over options. First argument is a pointer to a pointer to a
4044 * structure inside options[] array, second is option type like in the above
4045 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004046 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004047 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004048 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004049 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004050 * first argument to NULL.
4051 *
4052 * Returns full option name for current option on each call.
4053 */
4054 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004055option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004056{
4057 struct vimoption *ret = NULL;
4058 do
4059 {
4060 if (*option == NULL)
4061 *option = (void *) options;
4062 else if (((struct vimoption *) (*option))->fullname == NULL)
4063 {
4064 *option = NULL;
4065 return NULL;
4066 }
4067 else
4068 *option = (void *) (((struct vimoption *) (*option)) + 1);
4069
4070 ret = ((struct vimoption *) (*option));
4071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004072 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004073 if (ret->var == NULL)
4074 {
4075 ret = NULL;
4076 continue;
4077 }
4078
4079 switch (opt_type)
4080 {
4081 case SREQ_GLOBAL:
4082 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4083 ret = NULL;
4084 break;
4085 case SREQ_BUF:
4086 if (!(ret->indir & PV_BUF))
4087 ret = NULL;
4088 break;
4089 case SREQ_WIN:
4090 if (!(ret->indir & PV_WIN))
4091 ret = NULL;
4092 break;
4093 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004094 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004095 return NULL;
4096 }
4097 }
4098 while (ret == NULL);
4099
4100 return (char_u *)ret->fullname;
4101}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004102#endif
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004105 * Return the flags for the option at 'opt_idx'.
4106 */
4107 long_u
4108get_option_flags(int opt_idx)
4109{
4110 return options[opt_idx].flags;
4111}
4112
4113/*
4114 * Set a flag for the option at 'opt_idx'.
4115 */
4116 void
4117set_option_flag(int opt_idx, long_u flag)
4118{
4119 options[opt_idx].flags |= flag;
4120}
4121
4122/*
4123 * Clear a flag for the option at 'opt_idx'.
4124 */
4125 void
4126clear_option_flag(int opt_idx, long_u flag)
4127{
4128 options[opt_idx].flags &= ~flag;
4129}
4130
4131/*
4132 * Returns TRUE if the option at 'opt_idx' is a global option
4133 */
4134 int
4135is_global_option(int opt_idx)
4136{
4137 return options[opt_idx].indir == PV_NONE;
4138}
4139
4140/*
4141 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4142 * local value.
4143 */
4144 int
4145is_global_local_option(int opt_idx)
4146{
4147 return options[opt_idx].indir & PV_BOTH;
4148}
4149
4150/*
4151 * Returns TRUE if the option at 'opt_idx' is a window-local option
4152 */
4153 int
4154is_window_local_option(int opt_idx)
4155{
4156 return options[opt_idx].var == VAR_WIN;
4157}
4158
4159/*
4160 * Returns TRUE if the option at 'opt_idx' is a hidden option
4161 */
4162 int
4163is_hidden_option(int opt_idx)
4164{
4165 return options[opt_idx].var == NULL;
4166}
4167
4168#if defined(FEAT_CRYPT) || defined(PROTO)
4169/*
4170 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4171 */
4172 int
4173is_crypt_key_option(int opt_idx)
4174{
4175 return options[opt_idx].indir == PV_KEY;
4176}
4177#endif
4178
4179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * Set the value of option "name".
4181 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004182 *
4183 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004186set_option_value(
4187 char_u *name,
4188 long number,
4189 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004190 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 int opt_idx;
4193 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004194 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004197 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004198 {
4199 int key;
4200
4201 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004202 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004203 {
4204 char_u key_name[2];
4205
4206 if (key < 0)
4207 {
4208 key_name[0] = KEY2TERMCAP0(key);
4209 key_name[1] = KEY2TERMCAP1(key);
4210 }
4211 else
4212 {
4213 key_name[0] = KS_KEY;
4214 key_name[1] = (key & 0xff);
4215 }
4216 add_termcode(key_name, string, FALSE);
4217 if (full_screen)
4218 ttest(FALSE);
4219 redraw_all_later(CLEAR);
4220 return NULL;
4221 }
4222
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004223 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 else
4226 {
4227 flags = options[opt_idx].flags;
4228#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004229 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004232 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004233 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004236 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004237 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 else
4239 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004240 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004241 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004243 if (number == 0 && string != NULL)
4244 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004245 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004247 // Either we are given a string or we are setting option
4248 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004249 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004250 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004251 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004252 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004253 // There's another character after zeros or the string
4254 // is empty. In both cases, we are trying to set a
4255 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004256 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004257 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004258 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004259
4260 }
4261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004263 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004264 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004266 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004267 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004271 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272}
4273
4274/*
4275 * Get the terminal code for a terminal option.
4276 * Returns NULL when not found.
4277 */
4278 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004279get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280{
4281 int opt_idx;
4282 char_u *varp;
4283
4284 if (tname[0] != 't' || tname[1] != '_' ||
4285 tname[2] == NUL || tname[3] == NUL)
4286 return NULL;
4287 if ((opt_idx = findoption(tname)) >= 0)
4288 {
4289 varp = get_varp(&(options[opt_idx]));
4290 if (varp != NULL)
4291 varp = *(char_u **)(varp);
4292 return varp;
4293 }
4294 return find_termcode(tname + 2);
4295}
4296
4297 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004298get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
4300 int i;
4301
4302 i = findoption((char_u *)"hl");
4303 if (i >= 0)
4304 return options[i].def_val[VI_DEFAULT];
4305 return (char_u *)NULL;
4306}
4307
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004308 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004309get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004310{
4311 int i;
4312
4313 i = findoption((char_u *)"enc");
4314 if (i >= 0)
4315 return options[i].def_val[VI_DEFAULT];
4316 return (char_u *)NULL;
4317}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004318
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319/*
4320 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004321 * When "has_lt" is true there is a '<' before "*arg_arg".
4322 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 */
4324 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004325find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004327 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004329 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 /*
4332 * Don't use get_special_key_code() for t_xx, we don't want it to call
4333 * add_termcap_entry().
4334 */
4335 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4336 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004337 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004339 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 modifiers = 0;
Bram Moolenaar459fd782019-10-13 16:43:39 +02004341 key = find_special_key(&arg, &modifiers, TRUE, TRUE, FALSE, TRUE, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004342 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 key = 0;
4344 }
4345 return key;
4346}
4347
4348/*
4349 * if 'all' == 0: show changed options
4350 * if 'all' == 1: show all normal options
4351 * if 'all' == 2: show all terminal options
4352 */
4353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004354showoptions(
4355 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004356 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 struct vimoption *p;
4359 int col;
4360 int isterm;
4361 char_u *varp;
4362 struct vimoption **items;
4363 int item_count;
4364 int run;
4365 int row, rows;
4366 int cols;
4367 int i;
4368 int len;
4369
4370#define INC 20
4371#define GAP 3
4372
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004373 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 if (items == NULL)
4375 return;
4376
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004377 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004379 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004381 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004383 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004385 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004388 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 * 1. display the short items
4390 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004391 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 */
4393 for (run = 1; run <= 2 && !got_int; ++run)
4394 {
4395 /*
4396 * collect the items in items[]
4397 */
4398 item_count = 0;
4399 for (p = &options[0]; p->fullname != NULL; p++)
4400 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004401 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004402 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004403 continue;
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 varp = NULL;
4406 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004407 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
4409 if (p->indir != PV_NONE && !isterm)
4410 varp = get_varp_scope(p, opt_flags);
4411 }
4412 else
4413 varp = get_varp(p);
4414 if (varp != NULL
4415 && ((all == 2 && isterm)
4416 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004417 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004419 if (opt_flags & OPT_ONECOLUMN)
4420 len = Columns;
4421 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004422 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else
4424 {
4425 option_value2string(p, opt_flags);
4426 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4427 }
4428 if ((len <= INC - GAP && run == 1) ||
4429 (len > INC - GAP && run == 2))
4430 items[item_count++] = p;
4431 }
4432 }
4433
4434 /*
4435 * display the items
4436 */
4437 if (run == 1)
4438 {
4439 cols = (Columns + GAP - 3) / INC;
4440 if (cols == 0)
4441 cols = 1;
4442 rows = (item_count + cols - 1) / cols;
4443 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004444 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 rows = item_count;
4446 for (row = 0; row < rows && !got_int; ++row)
4447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004448 msg_putchar('\n'); // go to next line
4449 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 break;
4451 col = 0;
4452 for (i = row; i < item_count; i += rows)
4453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004454 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 showoneopt(items[i], opt_flags);
4456 col += INC;
4457 }
4458 out_flush();
4459 ui_breakcheck();
4460 }
4461 }
4462 vim_free(items);
4463}
4464
4465/*
4466 * Return TRUE if option "p" has its default value.
4467 */
4468 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004469optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
4471 int dvi;
4472
4473 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004474 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004475 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004477 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004479 // the cast to long is required for Manx C, long_i is
4480 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004481 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004482 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4484}
4485
4486/*
4487 * showoneopt: show the value of one option
4488 * must not be called with a hidden option!
4489 */
4490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004491showoneopt(
4492 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004493 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004495 char_u *varp;
4496 int save_silent = silent_mode;
4497
4498 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004499 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 varp = get_varp_scope(p, opt_flags);
4502
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004503 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4505 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004506 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004508 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004510 msg_puts(" ");
4511 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (!(p->flags & P_BOOL))
4513 {
4514 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004515 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 option_value2string(p, opt_flags);
4517 msg_outtrans(NameBuff);
4518 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004519
4520 silent_mode = save_silent;
4521 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522}
4523
4524/*
4525 * Write modified options as ":set" commands to a file.
4526 *
4527 * There are three values for "opt_flags":
4528 * OPT_GLOBAL: Write global option values and fresh values of
4529 * buffer-local options (used for start of a session
4530 * file).
4531 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4532 * curwin (used for a vimrc file).
4533 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4534 * and local values for window-local options of
4535 * curwin. Local values are also written when at the
4536 * default value, because a modeline or autocommand
4537 * may have set them when doing ":edit file" and the
4538 * user has set them back at the default or fresh
4539 * value.
4540 * When "local_only" is TRUE, don't write fresh
4541 * values, only local values (for ":mkview").
4542 * (fresh value = value used for a new buffer or window for a local option).
4543 *
4544 * Return FAIL on error, OK otherwise.
4545 */
4546 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004547makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548{
4549 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004550 char_u *varp; // currently used value
4551 char_u *varp_fresh; // local value
4552 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 char *cmd;
4554 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004555 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 /*
4558 * The options that don't have a default (terminal name, columns, lines)
4559 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004560 * Do the loop over "options[]" twice: once for options with the
4561 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004563 for (pri = 1; pri >= 0; --pri)
4564 {
4565 for (p = &options[0]; !istermoption(p); p++)
4566 if (!(p->flags & P_NO_MKRC)
4567 && !istermoption(p)
4568 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004570 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4572 continue;
4573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004574 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4575 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4577 continue;
4578
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004579 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004581 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 continue;
4583
4584 round = 2;
4585 if (p->indir != PV_NONE)
4586 {
4587 if (p->var == VAR_WIN)
4588 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004589 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (!(opt_flags & OPT_LOCAL))
4591 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004592 // When fresh value of window-local option is not at the
4593 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4595 {
4596 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004597 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 round = 1;
4600 varp_local = varp;
4601 varp = varp_fresh;
4602 }
4603 }
4604 }
4605 }
4606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004607 // Round 1: fresh value for window-local options.
4608 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 for ( ; round <= 2; varp = varp_local, ++round)
4610 {
4611 if (round == 1 || (opt_flags & OPT_GLOBAL))
4612 cmd = "set";
4613 else
4614 cmd = "setlocal";
4615
4616 if (p->flags & P_BOOL)
4617 {
4618 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4619 return FAIL;
4620 }
4621 else if (p->flags & P_NUM)
4622 {
4623 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4624 return FAIL;
4625 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004626 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004628 int do_endif = FALSE;
4629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004630 // Don't set 'syntax' and 'filetype' again if the value is
4631 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004632 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004633#if defined(FEAT_SYN_HL)
4634 p->indir == PV_SYN ||
4635#endif
4636 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4639 *(char_u **)(varp)) < 0
4640 || put_eol(fd) < 0)
4641 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004642 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004645 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004647 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
4649 if (put_line(fd, "endif") == FAIL)
4650 return FAIL;
4651 }
4652 }
4653 }
4654 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 return OK;
4657}
4658
4659#if defined(FEAT_FOLDING) || defined(PROTO)
4660/*
4661 * Generate set commands for the local fold options only. Used when
4662 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4663 */
4664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004665makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004667 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004669 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 == FAIL
4671# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004672 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004674 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 == FAIL
4676 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4677 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4678 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4679 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4680 )
4681 return FAIL;
4682
4683 return OK;
4684}
4685#endif
4686
4687 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004688put_setstring(
4689 FILE *fd,
4690 char *cmd,
4691 char *name,
4692 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004693 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004696 char_u *buf = NULL;
4697 char_u *part = NULL;
4698 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
4700 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4701 return FAIL;
4702 if (*valuep != NULL)
4703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004704 // Output 'pastetoggle' as key names. For other
4705 // options some characters have to be escaped with
4706 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 if (valuep == &p_pt)
4708 {
4709 s = *valuep;
4710 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004711 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 return FAIL;
4713 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004714 // expand the option value, replace $HOME by ~
4715 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004717 int size = (int)STRLEN(*valuep) + 1;
4718
4719 // replace home directory in the whole option value into "buf"
4720 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004721 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004722 goto fail;
4723 home_replace(NULL, *valuep, buf, size, FALSE);
4724
4725 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004726 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004727 // can be expanded when read back.
4728 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4729 && vim_strchr(*valuep, ',') != NULL)
4730 {
4731 part = alloc(size);
4732 if (part == NULL)
4733 goto fail;
4734
4735 // write line break to clear the option, e.g. ':set rtp='
4736 if (put_eol(fd) == FAIL)
4737 goto fail;
4738
4739 p = buf;
4740 while (*p != NUL)
4741 {
4742 // for each comma separated option part, append value to
4743 // the option, :set rtp+=value
4744 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4745 goto fail;
4746 (void)copy_option_part(&p, part, size, ",");
4747 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4748 goto fail;
4749 }
4750 vim_free(buf);
4751 vim_free(part);
4752 return OK;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004755 {
4756 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004758 }
4759 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 else if (put_escstr(fd, *valuep, 2) == FAIL)
4762 return FAIL;
4763 }
4764 if (put_eol(fd) < 0)
4765 return FAIL;
4766 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004767fail:
4768 vim_free(buf);
4769 vim_free(part);
4770 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771}
4772
4773 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004774put_setnum(
4775 FILE *fd,
4776 char *cmd,
4777 char *name,
4778 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780 long wc;
4781
4782 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4783 return FAIL;
4784 if (wc_use_keyname((char_u *)valuep, &wc))
4785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004786 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4788 return FAIL;
4789 }
4790 else if (fprintf(fd, "%ld", *valuep) < 0)
4791 return FAIL;
4792 if (put_eol(fd) < 0)
4793 return FAIL;
4794 return OK;
4795}
4796
4797 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004798put_setbool(
4799 FILE *fd,
4800 char *cmd,
4801 char *name,
4802 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004804 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004805 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4807 || put_eol(fd) < 0)
4808 return FAIL;
4809 return OK;
4810}
4811
4812/*
4813 * Clear all the terminal options.
4814 * If the option has been allocated, free the memory.
4815 * Terminal options are never hidden or indirect.
4816 */
4817 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004818clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 /*
4821 * Reset a few things before clearing the old options. This may cause
4822 * outputting a few things that the terminal doesn't understand, but the
4823 * screen will be cleared later, so this is OK.
4824 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004825 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004827 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#endif
4829#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 // When starting the GUI close the display opened for the clipboard.
4831 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (gui.starting)
4833 clear_xterm_clip();
4834#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004837 free_termoptions();
4838}
4839
4840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004841free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004842{
4843 struct vimoption *p;
4844
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004845 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 if (istermoption(p))
4847 {
4848 if (p->flags & P_ALLOCED)
4849 free_string_option(*(char_u **)(p->var));
4850 if (p->flags & P_DEF_ALLOCED)
4851 free_string_option(p->def_val[VI_DEFAULT]);
4852 *(char_u **)(p->var) = empty_option;
4853 p->def_val[VI_DEFAULT] = empty_option;
4854 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004855#ifdef FEAT_EVAL
4856 // remember where the option was cleared
4857 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 clear_termcodes();
4861}
4862
4863/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004864 * Free the string for one term option, if it was allocated.
4865 * Set the string to empty_option and clear allocated flag.
4866 * "var" points to the option value.
4867 */
4868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004869free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004870{
4871 struct vimoption *p;
4872
4873 for (p = &options[0]; p->fullname != NULL; p++)
4874 if (p->var == var)
4875 {
4876 if (p->flags & P_ALLOCED)
4877 free_string_option(*(char_u **)(p->var));
4878 *(char_u **)(p->var) = empty_option;
4879 p->flags &= ~P_ALLOCED;
4880 break;
4881 }
4882}
4883
4884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * Set the terminal option defaults to the current value.
4886 * Used after setting the terminal name.
4887 */
4888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004889set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
4891 struct vimoption *p;
4892
4893 for (p = &options[0]; p->fullname != NULL; p++)
4894 {
4895 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4896 {
4897 if (p->flags & P_DEF_ALLOCED)
4898 {
4899 free_string_option(p->def_val[VI_DEFAULT]);
4900 p->flags &= ~P_DEF_ALLOCED;
4901 }
4902 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4903 if (p->flags & P_ALLOCED)
4904 {
4905 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004906 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 }
4909 }
4910}
4911
4912/*
4913 * return TRUE if 'p' starts with 't_'
4914 */
4915 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004916istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4919}
4920
Bram Moolenaardac13472019-09-16 21:06:21 +02004921/*
4922 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4923 */
4924 int
4925istermoption_idx(int opt_idx)
4926{
4927 return istermoption(&options[opt_idx]);
4928}
4929
Bram Moolenaar113e1072019-01-20 15:30:40 +01004930#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004932 * Unset local option value, similar to ":set opt<".
4933 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004934 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004935unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004936{
4937 struct vimoption *p;
4938 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004939 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004940
4941 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004942 if (opt_idx < 0)
4943 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004944 p = &(options[opt_idx]);
4945
4946 switch ((int)p->indir)
4947 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004949 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004950 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004951 break;
4952 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004953 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004954 break;
4955 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004956 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004957 break;
4958 case PV_AR:
4959 buf->b_p_ar = -1;
4960 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004961 case PV_BKC:
4962 clear_string_option(&buf->b_p_bkc);
4963 buf->b_bkc_flags = 0;
4964 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004965 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004966 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004967 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01004968 case PV_TC:
4969 clear_string_option(&buf->b_p_tc);
4970 buf->b_tc_flags = 0;
4971 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01004972 case PV_SISO:
4973 curwin->w_p_siso = -1;
4974 break;
4975 case PV_SO:
4976 curwin->w_p_so = -1;
4977 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004978#ifdef FEAT_FIND_ID
4979 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004980 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004981 break;
4982 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004983 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004984 break;
4985#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004986 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004987 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004988 break;
4989 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004990 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004991 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01004992 case PV_FP:
4993 clear_string_option(&buf->b_p_fp);
4994 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004995#ifdef FEAT_QUICKFIX
4996 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004997 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004998 break;
4999 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005000 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005001 break;
5002 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005003 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005004 break;
5005#endif
5006#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5007 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005008 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005009 break;
5010#endif
5011#if defined(FEAT_CRYPT)
5012 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005013 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005014 break;
5015#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005016#ifdef FEAT_LINEBREAK
5017 case PV_SBR:
5018 clear_string_option(&((win_T *)from)->w_p_sbr);
5019 break;
5020#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005021#ifdef FEAT_STL_OPT
5022 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005023 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005024 break;
5025#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005026 case PV_UL:
5027 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5028 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005029#ifdef FEAT_LISP
5030 case PV_LW:
5031 clear_string_option(&buf->b_p_lw);
5032 break;
5033#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005034 case PV_MENC:
5035 clear_string_option(&buf->b_p_menc);
5036 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005037 }
5038}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005039#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005040
5041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 * Get pointer to option variable, depending on local or global scope.
5043 */
5044 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005045get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
5047 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5048 {
5049 if (p->var == VAR_WIN)
5050 return (char_u *)GLOBAL_WO(get_varp(p));
5051 return p->var;
5052 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005053 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 switch ((int)p->indir)
5056 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005057 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005059 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5060 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5061 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005063 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5064 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5065 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005066 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005067 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005068 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005069 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5070 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005072 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5073 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005075 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5076 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005077#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5078 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5079#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005080#if defined(FEAT_CRYPT)
5081 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5082#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005083#ifdef FEAT_LINEBREAK
5084 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5085#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005086#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005087 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005088#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005089 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005090#ifdef FEAT_LISP
5091 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5092#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005093 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005094 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005096 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 return get_varp(p);
5099}
5100
5101/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005102 * Get pointer to option variable at 'opt_idx', depending on local or global
5103 * scope.
5104 */
5105 char_u *
5106get_option_varp_scope(int opt_idx, int opt_flags)
5107{
5108 return get_varp_scope(&(options[opt_idx]), opt_flags);
5109}
5110
5111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 * Get pointer to option variable.
5113 */
5114 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005115get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005117 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (p->var == NULL)
5119 return NULL;
5120
5121 switch ((int)p->indir)
5122 {
5123 case PV_NONE: return p->var;
5124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005125 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005126 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005128 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005130 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005132 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005134 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005136 case PV_TC: return *curbuf->b_p_tc != NUL
5137 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005138 case PV_BKC: return *curbuf->b_p_bkc != NUL
5139 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005140 case PV_SISO: return curwin->w_p_siso >= 0
5141 ? (char_u *)&(curwin->w_p_siso) : p->var;
5142 case PV_SO: return curwin->w_p_so >= 0
5143 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005145 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005147 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5149#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005150 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005152 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005154 case PV_FP: return *curbuf->b_p_fp != NUL
5155 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005157 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005159 case PV_GP: return *curbuf->b_p_gp != NUL
5160 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5161 case PV_MP: return *curbuf->b_p_mp != NUL
5162 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005164#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5165 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5166 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5167#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005168#if defined(FEAT_CRYPT)
5169 case PV_CM: return *curbuf->b_p_cm != NUL
5170 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5171#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005172#ifdef FEAT_LINEBREAK
5173 case PV_SBR: return *curwin->w_p_sbr != NUL
5174 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5175#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005176#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005177 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005178 ? (char_u *)&(curwin->w_p_stl) : p->var;
5179#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005180 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5181 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005182#ifdef FEAT_LISP
5183 case PV_LW: return *curbuf->b_p_lw != NUL
5184 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5185#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005186 case PV_MENC: return *curbuf->b_p_menc != NUL
5187 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#ifdef FEAT_ARABIC
5189 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5190#endif
5191 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005192#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005193 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5194#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005195#ifdef FEAT_SYN_HL
5196 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5197 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005198 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005199 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#ifdef FEAT_DIFF
5202 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5203#endif
5204#ifdef FEAT_FOLDING
5205 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5206 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5207 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5208 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5209 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5210 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5211 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5212# ifdef FEAT_EVAL
5213 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5214 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5215# endif
5216 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5217#endif
5218 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005219 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005220#ifdef FEAT_LINEBREAK
5221 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005224 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005225#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5227#endif
5228#ifdef FEAT_RIGHTLEFT
5229 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5230 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5231#endif
5232 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5233 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5234#ifdef FEAT_LINEBREAK
5235 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005236 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5237 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005239 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005241 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005242#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005243 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5244 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5245#endif
5246#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005247 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5248 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5249 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
5252 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5253 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5256 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5258 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5259#ifdef FEAT_CINDENT
5260 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5261 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5262 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5263#endif
5264#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5265 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268#ifdef FEAT_FOLDING
5269 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005272#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005273 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005275#ifdef FEAT_COMPL_FUNC
5276 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005277 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005278#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005279#ifdef FEAT_EVAL
5280 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005283 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005289 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5291 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5292 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5293 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5294#ifdef FEAT_FIND_ID
5295# ifdef FEAT_EVAL
5296 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5297# endif
5298#endif
5299#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5300 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5301 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5302#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005303#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005304 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306#ifdef FEAT_CRYPT
5307 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5308#endif
5309#ifdef FEAT_LISP
5310 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5311#endif
5312 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5313 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5314 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5315 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5316 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005318#ifdef FEAT_TEXTOBJ
5319 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5322#ifdef FEAT_SMARTINDENT
5323 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5327#ifdef FEAT_SEARCHPATH
5328 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5329#endif
5330 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5331#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005332 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005333 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5334#endif
5335#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005336 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5337 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5338 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#endif
5340 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5341 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5342 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5343 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005344#ifdef FEAT_PERSISTENT_UNDO
5345 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5348#ifdef FEAT_KEYMAP
5349 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5350#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005351#ifdef FEAT_SIGNS
5352 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5353#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005354#ifdef FEAT_VARTABS
5355 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5356 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5357#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005358 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005360 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 return (char_u *)&(curbuf->b_p_wm);
5362}
5363
5364/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005365 * Return a pointer to the variable for option at 'opt_idx'
5366 */
5367 char_u *
5368get_option_var(int opt_idx)
5369{
5370 return options[opt_idx].var;
5371}
5372
5373/*
5374 * Return the full name of the option at 'opt_idx'
5375 */
5376 char_u *
5377get_option_fullname(int opt_idx)
5378{
5379 return (char_u *)options[opt_idx].fullname;
5380}
5381
5382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 * Get the value of 'equalprg', either the buffer-local one or the global one.
5384 */
5385 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005386get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 if (*curbuf->b_p_ep == NUL)
5389 return p_ep;
5390 return curbuf->b_p_ep;
5391}
5392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393/*
5394 * Copy options from one window to another.
5395 * Used when splitting a window.
5396 */
5397 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005398win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
5400 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5401 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005402 after_copy_winopt(wp_to);
5403}
5404
5405/*
5406 * After copying window options: update variables depending on options.
5407 */
5408 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005409after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005410{
5411#ifdef FEAT_LINEBREAK
5412 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005413#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005414#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005415 fill_culopt_flags(NULL, wp);
5416 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420/*
5421 * Copy the options from one winopt_T to another.
5422 * Doesn't free the old option values in "to", use clear_winopt() for that.
5423 * The 'scroll' option is not copied, because it depends on the window height.
5424 * The 'previewwindow' option is reset, there can be only one preview window.
5425 */
5426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005427copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429#ifdef FEAT_ARABIC
5430 to->wo_arab = from->wo_arab;
5431#endif
5432 to->wo_list = from->wo_list;
5433 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005434 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005435#ifdef FEAT_LINEBREAK
5436 to->wo_nuw = from->wo_nuw;
5437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438#ifdef FEAT_RIGHTLEFT
5439 to->wo_rl = from->wo_rl;
5440 to->wo_rlc = vim_strsave(from->wo_rlc);
5441#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005442#ifdef FEAT_LINEBREAK
5443 to->wo_sbr = vim_strsave(from->wo_sbr);
5444#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005445#ifdef FEAT_STL_OPT
5446 to->wo_stl = vim_strsave(from->wo_stl);
5447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005449#ifdef FEAT_DIFF
5450 to->wo_wrap_save = from->wo_wrap_save;
5451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452#ifdef FEAT_LINEBREAK
5453 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005454 to->wo_bri = from->wo_bri;
5455 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005457 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005459 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005460 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005461 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005462#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005463 to->wo_spell = from->wo_spell;
5464#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005465#ifdef FEAT_SYN_HL
5466 to->wo_cuc = from->wo_cuc;
5467 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005468 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005469 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471#ifdef FEAT_DIFF
5472 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005473 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005475#ifdef FEAT_CONCEAL
5476 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005477 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005478#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005479#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005480 to->wo_twk = vim_strsave(from->wo_twk);
5481 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483#ifdef FEAT_FOLDING
5484 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005485 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005487 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 to->wo_fdi = vim_strsave(from->wo_fdi);
5489 to->wo_fml = from->wo_fml;
5490 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005491 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005493 to->wo_fdm_save = from->wo_diff_saved
5494 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 to->wo_fdn = from->wo_fdn;
5496# ifdef FEAT_EVAL
5497 to->wo_fde = vim_strsave(from->wo_fde);
5498 to->wo_fdt = vim_strsave(from->wo_fdt);
5499# endif
5500 to->wo_fmr = vim_strsave(from->wo_fmr);
5501#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005502#ifdef FEAT_SIGNS
5503 to->wo_scl = vim_strsave(from->wo_scl);
5504#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005505
5506#ifdef FEAT_EVAL
5507 // Copy the script context so that we know where the value was last set.
5508 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5509 sizeof(to->wo_script_ctx));
5510#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005511 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512}
5513
5514/*
5515 * Check string options in a window for a NULL value.
5516 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005517 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005518check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 check_winopt(&win->w_onebuf_opt);
5521 check_winopt(&win->w_allbuf_opt);
5522}
5523
5524/*
5525 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5526 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005528check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530#ifdef FEAT_FOLDING
5531 check_string_option(&wop->wo_fdi);
5532 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005533 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534# ifdef FEAT_EVAL
5535 check_string_option(&wop->wo_fde);
5536 check_string_option(&wop->wo_fdt);
5537# endif
5538 check_string_option(&wop->wo_fmr);
5539#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005540#ifdef FEAT_SIGNS
5541 check_string_option(&wop->wo_scl);
5542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#ifdef FEAT_RIGHTLEFT
5544 check_string_option(&wop->wo_rlc);
5545#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005546#ifdef FEAT_LINEBREAK
5547 check_string_option(&wop->wo_sbr);
5548#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005549#ifdef FEAT_STL_OPT
5550 check_string_option(&wop->wo_stl);
5551#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005552#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005553 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005554 check_string_option(&wop->wo_cc);
5555#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005556#ifdef FEAT_CONCEAL
5557 check_string_option(&wop->wo_cocu);
5558#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005559#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005560 check_string_option(&wop->wo_twk);
5561 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005562#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005563#ifdef FEAT_LINEBREAK
5564 check_string_option(&wop->wo_briopt);
5565#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005566 check_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
5568
5569/*
5570 * Free the allocated memory inside a winopt_T.
5571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005573clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575#ifdef FEAT_FOLDING
5576 clear_string_option(&wop->wo_fdi);
5577 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005578 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579# ifdef FEAT_EVAL
5580 clear_string_option(&wop->wo_fde);
5581 clear_string_option(&wop->wo_fdt);
5582# endif
5583 clear_string_option(&wop->wo_fmr);
5584#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005585#ifdef FEAT_SIGNS
5586 clear_string_option(&wop->wo_scl);
5587#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005588#ifdef FEAT_LINEBREAK
5589 clear_string_option(&wop->wo_briopt);
5590#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005591 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592#ifdef FEAT_RIGHTLEFT
5593 clear_string_option(&wop->wo_rlc);
5594#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005595#ifdef FEAT_LINEBREAK
5596 clear_string_option(&wop->wo_sbr);
5597#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005598#ifdef FEAT_STL_OPT
5599 clear_string_option(&wop->wo_stl);
5600#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005601#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005602 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005603 clear_string_option(&wop->wo_cc);
5604#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005605#ifdef FEAT_CONCEAL
5606 clear_string_option(&wop->wo_cocu);
5607#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005608#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005609 clear_string_option(&wop->wo_twk);
5610 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
5613
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005614#ifdef FEAT_EVAL
5615// Index into the options table for a buffer-local option enum.
5616static int buf_opt_idx[BV_COUNT];
5617# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5618
5619/*
5620 * Initialize buf_opt_idx[] if not done already.
5621 */
5622 static void
5623init_buf_opt_idx(void)
5624{
5625 static int did_init_buf_opt_idx = FALSE;
5626 int i;
5627
5628 if (did_init_buf_opt_idx)
5629 return;
5630 did_init_buf_opt_idx = TRUE;
5631 for (i = 0; !istermoption_idx(i); i++)
5632 if (options[i].indir & PV_BUF)
5633 buf_opt_idx[options[i].indir & PV_MASK] = i;
5634}
5635#else
5636# define COPY_OPT_SCTX(buf, bv)
5637#endif
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639/*
5640 * Copy global option values to local options for one buffer.
5641 * Used when creating a new buffer and sometimes when entering a buffer.
5642 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005643 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5645 * appropriate.
5646 * BCO_NOHELP Don't copy the values to a help buffer.
5647 */
5648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005649buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005652 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 int dont_do_help;
5654 int did_isk = FALSE;
5655
5656 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 * Skip this when the option defaults have not been set yet. Happens when
5658 * main() allocates the first buffer.
5659 */
5660 if (p_cpo != NULL)
5661 {
5662 /*
5663 * Always copy when entering and 'cpo' contains 'S'.
5664 * Don't copy when already initialized.
5665 * Don't copy when 'cpo' contains 's' and not entering.
5666 * 'S' BCO_ENTER initialized 's' should_copy
5667 * yes yes X X TRUE
5668 * yes no yes X FALSE
5669 * no X yes X FALSE
5670 * X no no yes FALSE
5671 * X no no no TRUE
5672 * no yes no X TRUE
5673 */
5674 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5675 && (buf->b_p_initialized
5676 || (!(flags & BCO_ENTER)
5677 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5678 should_copy = FALSE;
5679
5680 if (should_copy || (flags & BCO_ALWAYS))
5681 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005682#ifdef FEAT_EVAL
Bram Moolenaar7eed9642019-10-19 20:57:28 +02005683 vim_memset(buf->b_p_script_ctx, 0, sizeof(buf->b_p_script_ctx));
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005684 init_buf_opt_idx();
5685#endif
5686 // Don't copy the options specific to a help buffer when
5687 // BCO_NOHELP is given or the options were initialized already
5688 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5690 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005691 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
5693 save_p_isk = buf->b_p_isk;
5694 buf->b_p_isk = NULL;
5695 }
5696 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005697 * Always free the allocated strings. If not already initialized,
5698 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
5700 if (!buf->b_p_initialized)
5701 {
5702 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005703 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005706 switch (*p_ffs)
5707 {
5708 case 'm':
5709 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5710 case 'd':
5711 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5712 case 'u':
5713 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5714 default:
5715 buf->b_p_ff = vim_strsave(p_ff);
5716 }
5717 if (buf->b_p_ff != NULL)
5718 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 buf->b_p_bh = empty_option;
5720 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 else
5723 free_buf_options(buf, FALSE);
5724
5725 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005726 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 buf->b_p_ai_nopaste = p_ai_nopaste;
5728 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005729 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005731 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 buf->b_p_tw_nopaste = p_tw_nopaste;
5733 buf->b_p_tw_nobin = p_tw_nobin;
5734 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005735 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 buf->b_p_wm_nopaste = p_wm_nopaste;
5737 buf->b_p_wm_nobin = p_wm_nobin;
5738 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005739 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005740 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005741 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005742 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005743 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005745 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005747 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005749 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 buf->b_p_ml_nobin = p_ml_nobin;
5751 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005752 COPY_OPT_SCTX(buf, BV_INF);
5753 if (cmdmod.noswapfile)
5754 buf->b_p_swf = FALSE;
5755 else
5756 {
5757 buf->b_p_swf = p_swf;
5758 COPY_OPT_SCTX(buf, BV_INF);
5759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005761 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005762#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005763 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005764 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005766#ifdef FEAT_COMPL_FUNC
5767 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005768 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005769 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005770 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005771#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005772#ifdef FEAT_EVAL
5773 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005774 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005777 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005779#ifdef FEAT_VARTABS
5780 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005781 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005782 if (p_vsts && p_vsts != empty_option)
5783 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5784 else
5785 buf->b_p_vsts_array = 0;
5786 buf->b_p_vsts_nopaste = p_vsts_nopaste
5787 ? vim_strsave(p_vsts_nopaste) : NULL;
5788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005790 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005792 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793#ifdef FEAT_FOLDING
5794 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005795 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#endif
5797 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005798 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005799 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005800 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005801 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5802 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005804 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005806 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807#ifdef FEAT_SMARTINDENT
5808 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005809 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#endif
5811 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005812 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813#ifdef FEAT_CINDENT
5814 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005815 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005817 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005819 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005821 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005824 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5826 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005827 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#endif
5829#ifdef FEAT_LISP
5830 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005831 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832#endif
5833#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005834 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005836 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005837 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005838 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005839#endif
5840#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005841 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005842 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005843 (void)compile_cap_prog(&buf->b_s);
5844 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005845 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005846 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005847 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#endif
5849#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5850 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005851 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005853 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005855 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005856#if defined(FEAT_EVAL)
5857 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005858 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860#ifdef FEAT_CRYPT
5861 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005862 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863#endif
5864#ifdef FEAT_SEARCHPATH
5865 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005866 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#endif
5868#ifdef FEAT_KEYMAP
5869 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005870 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 buf->b_kmap_state |= KEYMAP_INIT;
5872#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005873#ifdef FEAT_TERMINAL
5874 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005875 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005876#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005877 // This isn't really an option, but copying the langmap and IME
5878 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005880 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005882 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005884 // options that are normally global but also have a local value
5885 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005887 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005888 buf->b_p_bkc = empty_option;
5889 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#ifdef FEAT_QUICKFIX
5891 buf->b_p_gp = empty_option;
5892 buf->b_p_mp = empty_option;
5893 buf->b_p_efm = empty_option;
5894#endif
5895 buf->b_p_ep = empty_option;
5896 buf->b_p_kp = empty_option;
5897 buf->b_p_path = empty_option;
5898 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005899 buf->b_p_tc = empty_option;
5900 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901#ifdef FEAT_FIND_ID
5902 buf->b_p_def = empty_option;
5903 buf->b_p_inc = empty_option;
5904# ifdef FEAT_EVAL
5905 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005906 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907# endif
5908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 buf->b_p_dict = empty_option;
5910 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005911#ifdef FEAT_TEXTOBJ
5912 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005913 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005914#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005915#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5916 buf->b_p_bexpr = empty_option;
5917#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005918#if defined(FEAT_CRYPT)
5919 buf->b_p_cm = empty_option;
5920#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005921#ifdef FEAT_PERSISTENT_UNDO
5922 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005923 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005924#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005925#ifdef FEAT_LISP
5926 buf->b_p_lw = empty_option;
5927#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005928 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
5930 /*
5931 * Don't copy the options set by ex_help(), use the saved values,
5932 * when going from a help buffer to a non-help buffer.
5933 * Don't touch these at all when BCO_NOHELP is used and going from
5934 * or to a help buffer.
5935 */
5936 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005939#ifdef FEAT_VARTABS
5940 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5941 tabstop_set(p_vts, &buf->b_p_vts_array);
5942 else
5943 buf->b_p_vts_array = NULL;
5944#endif
5945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 else
5947 {
5948 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 did_isk = TRUE;
5951 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005952#ifdef FEAT_VARTABS
5953 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005954 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005955 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5956 tabstop_set(p_vts, &buf->b_p_vts_array);
5957 else
5958 buf->b_p_vts_array = NULL;
5959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 if (buf->b_p_bt[0] == 'h')
5962 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005964 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 }
5966 }
5967
5968 /*
5969 * When the options should be copied (ignoring BCO_ALWAYS), set the
5970 * flag that indicates that the options have been initialized.
5971 */
5972 if (should_copy)
5973 buf->b_p_initialized = TRUE;
5974 }
5975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005976 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 if (did_isk)
5978 (void)buf_init_chartab(buf, FALSE);
5979}
5980
5981/*
5982 * Reset the 'modifiable' option and its default value.
5983 */
5984 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005985reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 int opt_idx;
5988
5989 curbuf->b_p_ma = FALSE;
5990 p_ma = FALSE;
5991 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005992 if (opt_idx >= 0)
5993 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994}
5995
5996/*
5997 * Set the global value for 'iminsert' to the local value.
5998 */
5999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006000set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 p_iminsert = curbuf->b_p_iminsert;
6003}
6004
6005/*
6006 * Set the global value for 'imsearch' to the local value.
6007 */
6008 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006009set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 p_imsearch = curbuf->b_p_imsearch;
6012}
6013
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014static int expand_option_idx = -1;
6015static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6016static int expand_option_flags = 0;
6017
6018 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006019set_context_in_set_cmd(
6020 expand_T *xp,
6021 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006022 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006025 long_u flags = 0; // init for GCC
6026 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 char_u *p;
6028 char_u *s;
6029 int is_term_option = FALSE;
6030 int key;
6031
6032 expand_option_flags = opt_flags;
6033
6034 xp->xp_context = EXPAND_SETTINGS;
6035 if (*arg == NUL)
6036 {
6037 xp->xp_pattern = arg;
6038 return;
6039 }
6040 p = arg + STRLEN(arg) - 1;
6041 if (*p == ' ' && *(p - 1) != '\\')
6042 {
6043 xp->xp_pattern = p + 1;
6044 return;
6045 }
6046 while (p > arg)
6047 {
6048 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006049 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (*p == ' ' || *p == ',')
6051 {
6052 while (s > arg && *(s - 1) == '\\')
6053 --s;
6054 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006055 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (*p == ' ' && ((p - s) & 1) == 0)
6057 {
6058 ++p;
6059 break;
6060 }
6061 --p;
6062 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006063 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 {
6065 xp->xp_context = EXPAND_BOOL_SETTINGS;
6066 p += 2;
6067 }
6068 if (STRNCMP(p, "inv", 3) == 0)
6069 {
6070 xp->xp_context = EXPAND_BOOL_SETTINGS;
6071 p += 3;
6072 }
6073 xp->xp_pattern = arg = p;
6074 if (*arg == '<')
6075 {
6076 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006077 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 return;
6079 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006080 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
6082 xp->xp_context = EXPAND_NOTHING;
6083 return;
6084 }
6085 nextchar = *++p;
6086 is_term_option = TRUE;
6087 expand_option_name[2] = KEY2TERMCAP0(key);
6088 expand_option_name[3] = KEY2TERMCAP1(key);
6089 }
6090 else
6091 {
6092 if (p[0] == 't' && p[1] == '_')
6093 {
6094 p += 2;
6095 if (*p != NUL)
6096 ++p;
6097 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006098 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 nextchar = *++p;
6100 is_term_option = TRUE;
6101 expand_option_name[2] = p[-2];
6102 expand_option_name[3] = p[-1];
6103 }
6104 else
6105 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6108 p++;
6109 if (*p == NUL)
6110 return;
6111 nextchar = *p;
6112 *p = NUL;
6113 opt_idx = findoption(arg);
6114 *p = nextchar;
6115 if (opt_idx == -1 || options[opt_idx].var == NULL)
6116 {
6117 xp->xp_context = EXPAND_NOTHING;
6118 return;
6119 }
6120 flags = options[opt_idx].flags;
6121 if (flags & P_BOOL)
6122 {
6123 xp->xp_context = EXPAND_NOTHING;
6124 return;
6125 }
6126 }
6127 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006128 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6130 {
6131 ++p;
6132 nextchar = '=';
6133 }
6134 if ((nextchar != '=' && nextchar != ':')
6135 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6136 {
6137 xp->xp_context = EXPAND_UNSUCCESSFUL;
6138 return;
6139 }
6140 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6141 {
6142 xp->xp_context = EXPAND_OLD_SETTING;
6143 if (is_term_option)
6144 expand_option_idx = -1;
6145 else
6146 expand_option_idx = opt_idx;
6147 xp->xp_pattern = p + 1;
6148 return;
6149 }
6150 xp->xp_context = EXPAND_NOTHING;
6151 if (is_term_option || (flags & P_NUM))
6152 return;
6153
6154 xp->xp_pattern = p + 1;
6155
6156 if (flags & P_EXPAND)
6157 {
6158 p = options[opt_idx].var;
6159 if (p == (char_u *)&p_bdir
6160 || p == (char_u *)&p_dir
6161 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006162 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 || p == (char_u *)&p_rtp
6164#ifdef FEAT_SEARCHPATH
6165 || p == (char_u *)&p_cdpath
6166#endif
6167#ifdef FEAT_SESSION
6168 || p == (char_u *)&p_vdir
6169#endif
6170 )
6171 {
6172 xp->xp_context = EXPAND_DIRECTORIES;
6173 if (p == (char_u *)&p_path
6174#ifdef FEAT_SEARCHPATH
6175 || p == (char_u *)&p_cdpath
6176#endif
6177 )
6178 xp->xp_backslash = XP_BS_THREE;
6179 else
6180 xp->xp_backslash = XP_BS_ONE;
6181 }
6182 else
6183 {
6184 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006185 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (p == (char_u *)&p_tags)
6187 xp->xp_backslash = XP_BS_THREE;
6188 else
6189 xp->xp_backslash = XP_BS_ONE;
6190 }
6191 }
6192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006193 // For an option that is a list of file names, find the start of the
6194 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6196 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006197 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 if (*p == ' ' || *p == ',')
6199 {
6200 s = p;
6201 while (s > xp->xp_pattern && *(s - 1) == '\\')
6202 --s;
6203 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6204 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6205 {
6206 xp->xp_pattern = p + 1;
6207 break;
6208 }
6209 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006210
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006211#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006212 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006213 if (options[opt_idx].var == (char_u *)&p_sps
6214 && STRNCMP(p, "file:", 5) == 0)
6215 {
6216 xp->xp_pattern = p + 5;
6217 break;
6218 }
6219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 }
6221
6222 return;
6223}
6224
6225 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006226ExpandSettings(
6227 expand_T *xp,
6228 regmatch_T *regmatch,
6229 int *num_file,
6230 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006232 int num_normal = 0; // Nr of matching non-term-code settings
6233 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int opt_idx;
6235 int match;
6236 int count = 0;
6237 char_u *str;
6238 int loop;
6239 int is_term_opt;
6240 char_u name_buf[MAX_KEY_NAME_LEN];
6241 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006242 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006244 // do this loop twice:
6245 // loop == 0: count the number of matching options
6246 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 for (loop = 0; loop <= 1; ++loop)
6248 {
6249 regmatch->rm_ic = ic;
6250 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6251 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006252 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6253 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6255 {
6256 if (loop == 0)
6257 num_normal++;
6258 else
6259 (*file)[count++] = vim_strsave((char_u *)names[match]);
6260 }
6261 }
6262 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6263 opt_idx++)
6264 {
6265 if (options[opt_idx].var == NULL)
6266 continue;
6267 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6268 && !(options[opt_idx].flags & P_BOOL))
6269 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006270 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if (is_term_opt && num_normal > 0)
6272 continue;
6273 match = FALSE;
6274 if (vim_regexec(regmatch, str, (colnr_T)0)
6275 || (options[opt_idx].shortname != NULL
6276 && vim_regexec(regmatch,
6277 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6278 match = TRUE;
6279 else if (is_term_opt)
6280 {
6281 name_buf[0] = '<';
6282 name_buf[1] = 't';
6283 name_buf[2] = '_';
6284 name_buf[3] = str[2];
6285 name_buf[4] = str[3];
6286 name_buf[5] = '>';
6287 name_buf[6] = NUL;
6288 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6289 {
6290 match = TRUE;
6291 str = name_buf;
6292 }
6293 }
6294 if (match)
6295 {
6296 if (loop == 0)
6297 {
6298 if (is_term_opt)
6299 num_term++;
6300 else
6301 num_normal++;
6302 }
6303 else
6304 (*file)[count++] = vim_strsave(str);
6305 }
6306 }
6307 /*
6308 * Check terminal key codes, these are not in the option table
6309 */
6310 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6311 {
6312 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6313 {
6314 if (!isprint(str[0]) || !isprint(str[1]))
6315 continue;
6316
6317 name_buf[0] = 't';
6318 name_buf[1] = '_';
6319 name_buf[2] = str[0];
6320 name_buf[3] = str[1];
6321 name_buf[4] = NUL;
6322
6323 match = FALSE;
6324 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6325 match = TRUE;
6326 else
6327 {
6328 name_buf[0] = '<';
6329 name_buf[1] = 't';
6330 name_buf[2] = '_';
6331 name_buf[3] = str[0];
6332 name_buf[4] = str[1];
6333 name_buf[5] = '>';
6334 name_buf[6] = NUL;
6335
6336 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6337 match = TRUE;
6338 }
6339 if (match)
6340 {
6341 if (loop == 0)
6342 num_term++;
6343 else
6344 (*file)[count++] = vim_strsave(name_buf);
6345 }
6346 }
6347
6348 /*
6349 * Check special key names.
6350 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006351 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6353 {
6354 name_buf[0] = '<';
6355 STRCPY(name_buf + 1, str);
6356 STRCAT(name_buf, ">");
6357
6358 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6359 {
6360 if (loop == 0)
6361 num_term++;
6362 else
6363 (*file)[count++] = vim_strsave(name_buf);
6364 }
6365 }
6366 }
6367 if (loop == 0)
6368 {
6369 if (num_normal > 0)
6370 *num_file = num_normal;
6371 else if (num_term > 0)
6372 *num_file = num_term;
6373 else
6374 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006375 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 if (*file == NULL)
6377 {
6378 *file = (char_u **)"";
6379 return FAIL;
6380 }
6381 }
6382 }
6383 return OK;
6384}
6385
6386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006387ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006389 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 char_u *buf;
6391
6392 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006393 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (*file == NULL)
6395 return FAIL;
6396
6397 /*
6398 * For a terminal key code expand_option_idx is < 0.
6399 */
6400 if (expand_option_idx < 0)
6401 {
6402 var = find_termcode(expand_option_name + 2);
6403 if (var == NULL)
6404 expand_option_idx = findoption(expand_option_name);
6405 }
6406
6407 if (expand_option_idx >= 0)
6408 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006409 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 option_value2string(&options[expand_option_idx], expand_option_flags);
6411 var = NameBuff;
6412 }
6413 else if (var == NULL)
6414 var = (char_u *)"";
6415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 // A backslash is required before some characters. This is the reverse of
6417 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 buf = vim_strsave_escaped(var, escape_chars);
6419
6420 if (buf == NULL)
6421 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006422 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 return FAIL;
6424 }
6425
6426#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006427 // For MS-Windows et al. we don't double backslashes at the start and
6428 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006429 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (var[0] == '\\' && var[1] == '\\'
6431 && expand_option_idx >= 0
6432 && (options[expand_option_idx].flags & P_EXPAND)
6433 && vim_isfilec(var[2])
6434 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006435 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436#endif
6437
6438 *file[0] = buf;
6439 *num_file = 1;
6440 return OK;
6441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443/*
6444 * Get the value for the numeric or string option *opp in a nice format into
6445 * NameBuff[]. Must not be called with a hidden option!
6446 */
6447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006448option_value2string(
6449 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006450 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 char_u *varp;
6453
6454 varp = get_varp_scope(opp, opt_flags);
6455
6456 if (opp->flags & P_NUM)
6457 {
6458 long wc = 0;
6459
6460 if (wc_use_keyname(varp, &wc))
6461 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6462 else if (wc != 0)
6463 STRCPY(NameBuff, transchar((int)wc));
6464 else
6465 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6466 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
6469 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006470 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 NameBuff[0] = NUL;
6472#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006473 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006474 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 STRCPY(NameBuff, "*****");
6476#endif
6477 else if (opp->flags & P_EXPAND)
6478 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006479 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 else if ((char_u **)opp->var == &p_pt)
6481 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6482 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006483 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485}
6486
6487/*
6488 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6489 * printed as a keyname.
6490 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6491 */
6492 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006493wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6496 {
6497 *wcp = *(long *)varp;
6498 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6499 return TRUE;
6500 }
6501 return FALSE;
6502}
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504/*
6505 * Return TRUE if format option 'x' is in effect.
6506 * Take care of no formatting when 'paste' is set.
6507 */
6508 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509has_format_option(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 if (p_paste)
6512 return FALSE;
6513 return (vim_strchr(curbuf->b_p_fo, x) != NULL);
6514}
6515
6516/*
6517 * Return TRUE if "x" is present in 'shortmess' option, or
6518 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6519 */
6520 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006521shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006523 return p_shm != NULL &&
6524 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 || (vim_strchr(p_shm, 'a') != NULL
6526 && vim_strchr((char_u *)SHM_A, x) != NULL));
6527}
6528
6529/*
6530 * paste_option_changed() - Called after p_paste was set or reset.
6531 */
6532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006533paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 static int old_p_paste = FALSE;
6536 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006537 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538#ifdef FEAT_CMDL_INFO
6539 static int save_ru = 0;
6540#endif
6541#ifdef FEAT_RIGHTLEFT
6542 static int save_ri = 0;
6543 static int save_hkmap = 0;
6544#endif
6545 buf_T *buf;
6546
6547 if (p_paste)
6548 {
6549 /*
6550 * Paste switched from off to on.
6551 * Save the current values, so they can be restored later.
6552 */
6553 if (!old_p_paste)
6554 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006555 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006556 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
6558 buf->b_p_tw_nopaste = buf->b_p_tw;
6559 buf->b_p_wm_nopaste = buf->b_p_wm;
6560 buf->b_p_sts_nopaste = buf->b_p_sts;
6561 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006562 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006563#ifdef FEAT_VARTABS
6564 if (buf->b_p_vsts_nopaste)
6565 vim_free(buf->b_p_vsts_nopaste);
6566 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6567 ? vim_strsave(buf->b_p_vsts) : NULL;
6568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 }
6570
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006571 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006573 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574#ifdef FEAT_CMDL_INFO
6575 save_ru = p_ru;
6576#endif
6577#ifdef FEAT_RIGHTLEFT
6578 save_ri = p_ri;
6579 save_hkmap = p_hkmap;
6580#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006581 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006582 p_ai_nopaste = p_ai;
6583 p_et_nopaste = p_et;
6584 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 p_tw_nopaste = p_tw;
6586 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006587#ifdef FEAT_VARTABS
6588 if (p_vsts_nopaste)
6589 vim_free(p_vsts_nopaste);
6590 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 }
6593
6594 /*
6595 * Always set the option values, also when 'paste' is set when it is
6596 * already on.
6597 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006598 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006599 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 buf->b_p_tw = 0; // textwidth is 0
6602 buf->b_p_wm = 0; // wrapmargin is 0
6603 buf->b_p_sts = 0; // softtabstop is 0
6604 buf->b_p_ai = 0; // no auto-indent
6605 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006606#ifdef FEAT_VARTABS
6607 if (buf->b_p_vsts)
6608 free_string_option(buf->b_p_vsts);
6609 buf->b_p_vsts = empty_option;
6610 if (buf->b_p_vsts_array)
6611 vim_free(buf->b_p_vsts_array);
6612 buf->b_p_vsts_array = 0;
6613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006616 // set global options
6617 p_sm = 0; // no showmatch
6618 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006621 status_redraw_all(); // redraw to remove the ruler
6622 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623#endif
6624#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006625 p_ri = 0; // no reverse insert
6626 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006628 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 p_tw = 0;
6630 p_wm = 0;
6631 p_sts = 0;
6632 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006633#ifdef FEAT_VARTABS
6634 if (p_vsts)
6635 free_string_option(p_vsts);
6636 p_vsts = empty_option;
6637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639
6640 /*
6641 * Paste switched from on to off: Restore saved values.
6642 */
6643 else if (old_p_paste)
6644 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006645 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006646 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
6648 buf->b_p_tw = buf->b_p_tw_nopaste;
6649 buf->b_p_wm = buf->b_p_wm_nopaste;
6650 buf->b_p_sts = buf->b_p_sts_nopaste;
6651 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006652 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006653#ifdef FEAT_VARTABS
6654 if (buf->b_p_vsts)
6655 free_string_option(buf->b_p_vsts);
6656 buf->b_p_vsts = buf->b_p_vsts_nopaste
6657 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6658 if (buf->b_p_vsts_array)
6659 vim_free(buf->b_p_vsts_array);
6660 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6661 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6662 else
6663 buf->b_p_vsts_array = 0;
6664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 }
6666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006669 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006672 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 p_ru = save_ru;
6674#endif
6675#ifdef FEAT_RIGHTLEFT
6676 p_ri = save_ri;
6677 p_hkmap = save_hkmap;
6678#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006679 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006680 p_ai = p_ai_nopaste;
6681 p_et = p_et_nopaste;
6682 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 p_tw = p_tw_nopaste;
6684 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006685#ifdef FEAT_VARTABS
6686 if (p_vsts)
6687 free_string_option(p_vsts);
6688 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691
6692 old_p_paste = p_paste;
6693}
6694
6695/*
6696 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6697 *
6698 * Reset 'compatible' and set the values for options that didn't get set yet
6699 * to the Vim defaults.
6700 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006701 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 */
6703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006704vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006706 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006707 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006708 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 if (!option_was_set((char_u *)"cp"))
6711 {
6712 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006713 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6715 set_option_default(opt_idx, OPT_FREE, FALSE);
6716 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006717 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006719
6720 if (fname != NULL)
6721 {
6722 p = vim_getenv(envname, &dofree);
6723 if (p == NULL)
6724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006725 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006726 p = FullName_save(fname, FALSE);
6727 if (p != NULL)
6728 {
6729 vim_setenv(envname, p);
6730 vim_free(p);
6731 }
6732 }
6733 else if (dofree)
6734 vim_free(p);
6735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736}
6737
6738/*
6739 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6740 */
6741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006744 int opt_idx;
6745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 if (p_cp != on)
6747 {
6748 p_cp = on;
6749 compatible_set();
6750 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006751 opt_idx = findoption((char_u *)"cp");
6752 if (opt_idx >= 0)
6753 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
6755
6756/*
6757 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006758 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 */
6760 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 int idx;
6764
6765 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006766 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 return FALSE;
6768 if (options[idx].flags & P_WAS_SET)
6769 return TRUE;
6770 return FALSE;
6771}
6772
6773/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006774 * Reset the flag indicating option "name" was set.
6775 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006776 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006777reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006778{
6779 int idx = findoption(name);
6780
6781 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006782 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006783 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006784 return OK;
6785 }
6786 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006787}
6788
6789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 * compatible_set() - Called when 'compatible' has been set or unset.
6791 *
6792 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6793 * flag) to a Vi compatible value.
6794 * When 'compatible' is unset: Set all options that have a different default
6795 * for Vim (without the P_VI_DEF flag) to that default.
6796 */
6797 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006798compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 int opt_idx;
6801
Bram Moolenaardac13472019-09-16 21:06:21 +02006802 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6804 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6805 set_option_default(opt_idx, OPT_FREE, p_cp);
6806 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006807 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808}
6809
Bram Moolenaardac13472019-09-16 21:06:21 +02006810#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812/*
6813 * fill_breakat_flags() -- called when 'breakat' changes value.
6814 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006816fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006818 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 int i;
6820
6821 for (i = 0; i < 256; i++)
6822 breakat_flags[i] = FALSE;
6823
6824 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006825 for (p = p_breakat; *p; p++)
6826 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828#endif
6829
6830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 * Check if backspacing over something is allowed.
6832 */
6833 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006834can_bs(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006835 int what) // BS_INDENT, BS_EOL or BS_START
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006837#ifdef FEAT_JOB_CHANNEL
6838 if (what == BS_START && bt_prompt(curbuf))
6839 return FALSE;
6840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 switch (*p_bs)
6842 {
6843 case '2': return TRUE;
6844 case '1': return (what != BS_START);
6845 case '0': return FALSE;
6846 }
6847 return vim_strchr(p_bs, what) != NULL;
6848}
6849
6850/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006851 * Return the effective 'scrolloff' value for the current window, using the
6852 * global value when appropriate.
6853 */
6854 long
6855get_scrolloff_value(void)
6856{
6857 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6858}
6859
6860/*
6861 * Return the effective 'sidescrolloff' value for the current window, using the
6862 * global value when appropriate.
6863 */
6864 long
6865get_sidescrolloff_value(void)
6866{
6867 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6868}
6869
6870/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006871 * Get the local or global value of 'backupcopy'.
6872 */
6873 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006874get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006875{
6876 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6877}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006878
Bram Moolenaaree857022019-11-09 23:26:40 +01006879#if defined(FEAT_LINEBREAK) || defined(PROTO)
6880/*
6881 * Get the local or global value of 'showbreak'.
6882 */
6883 char_u *
6884get_showbreak_value(win_T *win)
6885{
6886 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6887 return p_sbr;
6888 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6889 return empty_option;
6890 return win->w_p_sbr;
6891}
6892#endif
6893
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006894#if defined(FEAT_EVAL) || defined(PROTO)
6895/*
6896 * Get window or buffer local options.
6897 */
6898 dict_T *
6899get_winbuf_options(int bufopt)
6900{
6901 dict_T *d;
6902 int opt_idx;
6903
6904 d = dict_alloc();
6905 if (d == NULL)
6906 return NULL;
6907
Bram Moolenaardac13472019-09-16 21:06:21 +02006908 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006909 {
6910 struct vimoption *opt = &options[opt_idx];
6911
6912 if ((bufopt && (opt->indir & PV_BUF))
6913 || (!bufopt && (opt->indir & PV_WIN)))
6914 {
6915 char_u *varp = get_varp(opt);
6916
6917 if (varp != NULL)
6918 {
6919 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006920 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006921 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006922 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006923 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006924 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006925 }
6926 }
6927 }
6928
6929 return d;
6930}
6931#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006932
Bram Moolenaardac13472019-09-16 21:06:21 +02006933#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006934/*
6935 * This is called when 'culopt' is changed
6936 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006937 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006938fill_culopt_flags(char_u *val, win_T *wp)
6939{
6940 char_u *p;
6941 char_u culopt_flags_new = 0;
6942
6943 if (val == NULL)
6944 p = wp->w_p_culopt;
6945 else
6946 p = val;
6947 while (*p != NUL)
6948 {
6949 if (STRNCMP(p, "line", 4) == 0)
6950 {
6951 p += 4;
6952 culopt_flags_new |= CULOPT_LINE;
6953 }
6954 else if (STRNCMP(p, "both", 4) == 0)
6955 {
6956 p += 4;
6957 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
6958 }
6959 else if (STRNCMP(p, "number", 6) == 0)
6960 {
6961 p += 6;
6962 culopt_flags_new |= CULOPT_NBR;
6963 }
6964 else if (STRNCMP(p, "screenline", 10) == 0)
6965 {
6966 p += 10;
6967 culopt_flags_new |= CULOPT_SCRLINE;
6968 }
6969
6970 if (*p != ',' && *p != NUL)
6971 return FAIL;
6972 if (*p == ',')
6973 ++p;
6974 }
6975
6976 // Can't have both "line" and "screenline".
6977 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
6978 return FAIL;
6979 wp->w_p_culopt_flags = culopt_flags_new;
6980
6981 return OK;
6982}
6983#endif