blob: 7c37326f931b62a73589b39e8f5f5030d3e2269c [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
1069/*
1070 * Parse 'arg' for option settings.
1071 *
1072 * 'arg' may be IObuff, but only when no errors can be present and option
1073 * does not need to be expanded with option_expand().
1074 * "opt_flags":
1075 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001076 * OPT_GLOBAL for ":setglobal"
1077 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001079 * OPT_WINONLY to only set window-local options
1080 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 *
1082 * returns FAIL if an error is detected, OK otherwise
1083 */
1084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001085do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001086 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001087 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001090 char *errmsg;
1091 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001093 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1094 int nextchar; // next non-white char after option name
1095 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 int len;
1097 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001098 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 long_u flags; // flags for current option
1101 char_u *varp = NULL; // pointer to variable for current option
1102 int did_show = FALSE; // already showed one value
1103 int adding; // "opt+=arg"
1104 int prepending; // "opt^=arg"
1105 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 int cp_val = 0;
1107 char_u key_name[2];
1108
1109 if (*arg == NUL)
1110 {
1111 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001112 did_show = TRUE;
1113 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
1118 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001119 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001121 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1122 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 /*
1125 * ":set all" show all options.
1126 * ":set all&" set all options to their default value.
1127 */
1128 arg += 3;
1129 if (*arg == '&')
1130 {
1131 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001132 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001134 didset_options();
1135 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001136 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
1138 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001141 did_show = TRUE;
1142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001144 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 showoptions(2, opt_flags);
1147 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001148 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 arg += 7;
1150 }
1151 else
1152 {
1153 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001154 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 {
1156 prefix = 0;
1157 arg += 2;
1158 }
1159 else if (STRNCMP(arg, "inv", 3) == 0)
1160 {
1161 prefix = 2;
1162 arg += 3;
1163 }
1164
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001165 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 key = 0;
1167 if (*arg == '<')
1168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1172 len = 5;
1173 else
1174 {
1175 len = 1;
1176 while (arg[len] != NUL && arg[len] != '>')
1177 ++len;
1178 }
1179 if (arg[len] != '>')
1180 {
1181 errmsg = e_invarg;
1182 goto skip;
1183 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001184 arg[len] = NUL; // put NUL after name
1185 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001187 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001189 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 else
1192 {
1193 len = 0;
1194 /*
1195 * The two characters after "t_" may not be alphanumeric.
1196 */
1197 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1198 len = 4;
1199 else
1200 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1201 ++len;
1202 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001203 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001205 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001207 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
1209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001210 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 afterchar = arg[len];
1212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001213 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001214 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 ++len;
1216
1217 adding = FALSE;
1218 prepending = FALSE;
1219 removing = FALSE;
1220 if (arg[len] != NUL && arg[len + 1] == '=')
1221 {
1222 if (arg[len] == '+')
1223 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001224 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 ++len;
1226 }
1227 else if (arg[len] == '^')
1228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ++len;
1231 }
1232 else if (arg[len] == '-')
1233 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001234 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 ++len;
1236 }
1237 }
1238 nextchar = arg[len];
1239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001242 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 goto skip;
1244 }
1245
1246 if (opt_idx >= 0)
1247 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001248 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 // Only give an error message when requesting the value of
1251 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1253 && (!(options[opt_idx].flags & P_BOOL)
1254 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001255 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 goto skip;
1257 }
1258
1259 flags = options[opt_idx].flags;
1260 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1261 }
1262 else
1263 {
1264 flags = P_STRING;
1265 if (key < 0)
1266 {
1267 key_name[0] = KEY2TERMCAP0(key);
1268 key_name[1] = KEY2TERMCAP1(key);
1269 }
1270 else
1271 {
1272 key_name[0] = KS_KEY;
1273 key_name[1] = (key & 0xff);
1274 }
1275 }
1276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001277 // Skip all options that are not window-local (used when showing
1278 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001279 if ((opt_flags & OPT_WINONLY)
1280 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1281 goto skip;
1282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001284 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1285 && options[opt_idx].var == VAR_WIN)
1286 goto skip;
1287
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001288 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001289 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001291 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001293 errmsg = _("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001294 goto skip;
1295 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001296 if ((flags & P_MLE) && !p_mle)
1297 {
1298 errmsg = _("E992: Not allowed in a modeline when 'modelineexpr' is off");
1299 goto skip;
1300 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001301#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001302 // In diff mode some options are overruled. This avoids that
1303 // 'foldmethod' becomes "marker" instead of "diff" and that
1304 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001305 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001307 && (
1308#ifdef FEAT_FOLDING
1309 options[opt_idx].indir == PV_FDM ||
1310#endif
1311 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001312 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315
1316#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001317 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001318 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001320 errmsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 goto skip;
1322 }
1323#endif
1324
1325 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1326 {
1327 arg += len;
1328 cp_val = p_cp;
1329 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
1333 cp_val = FALSE;
1334 arg += 3;
1335 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001336 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 cp_val = TRUE;
1339 arg += 2;
1340 }
1341 }
1342 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001343 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
1345 errmsg = e_trailing;
1346 goto skip;
1347 }
1348 }
1349
1350 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001351 * allow '=' and ':' for hystorical reasons (MSDOS command.com
1352 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 */
1354 if (nextchar == '?'
1355 || (prefix == 1
1356 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1357 && !(flags & P_BOOL)))
1358 {
1359 /*
1360 * print value
1361 */
1362 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001363 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 else
1365 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001366 gotocmdline(TRUE); // cursor at status line
1367 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 if (opt_idx >= 0)
1370 {
1371 showoneopt(&options[opt_idx], opt_flags);
1372#ifdef FEAT_EVAL
1373 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001375 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001376 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001377 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001378 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001379 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001380 (int)options[opt_idx].indir & PV_MASK]);
1381 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001382 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001383 (int)options[opt_idx].indir & PV_MASK]);
1384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#endif
1386 }
1387 else
1388 {
1389 char_u *p;
1390
1391 p = find_termcode(key_name);
1392 if (p == NULL)
1393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001394 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 goto skip;
1396 }
1397 else
1398 (void)show_one_termcode(key_name, p, TRUE);
1399 }
1400 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001401 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 errmsg = e_trailing;
1403 }
1404 else
1405 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001406 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001407 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001409 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
1411 if (nextchar == '=' || nextchar == ':')
1412 {
1413 errmsg = e_invarg;
1414 goto skip;
1415 }
1416
1417 /*
1418 * ":set opt!": invert
1419 * ":set opt&": reset to default value
1420 * ":set opt<": reset to global value
1421 */
1422 if (nextchar == '!')
1423 value = *(int *)(varp) ^ 1;
1424 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001425 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 ((flags & P_VI_DEF) || cp_val)
1427 ? VI_DEFAULT : VIM_DEFAULT];
1428 else if (nextchar == '<')
1429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001430 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if ((int *)varp == &curbuf->b_p_ar
1432 && opt_flags == OPT_LOCAL)
1433 value = -1;
1434 else
1435 value = *(int *)get_varp_scope(&(options[opt_idx]),
1436 OPT_GLOBAL);
1437 }
1438 else
1439 {
1440 /*
1441 * ":set invopt": invert
1442 * ":set opt" or ":set noopt": set or reset
1443 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001444 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
1446 errmsg = e_trailing;
1447 goto skip;
1448 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001449 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 value = *(int *)(varp) ^ 1;
1451 else
1452 value = prefix;
1453 }
1454
1455 errmsg = set_bool_option(opt_idx, varp, (int)value,
1456 opt_flags);
1457 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001458 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
1460 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1461 || prefix != 1)
1462 {
1463 errmsg = e_invarg;
1464 goto skip;
1465 }
1466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
1469 /*
1470 * Different ways to set a number option:
1471 * & set to default value
1472 * < set to global value
1473 * <xx> accept special key codes for 'wildchar'
1474 * c accept any non-digit for 'wildchar'
1475 * [-]0-9 set number
1476 * other error
1477 */
1478 ++arg;
1479 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001480 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 ((flags & P_VI_DEF) || cp_val)
1482 ? VI_DEFAULT : VIM_DEFAULT];
1483 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001484 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001485 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1486 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001487 if ((long *)varp == &curbuf->b_p_ul
1488 && opt_flags == OPT_LOCAL)
1489 value = NO_LOCAL_UNDOLEVEL;
1490 else
1491 value = *(long *)get_varp_scope(
1492 &(options[opt_idx]), OPT_GLOBAL);
1493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 else if (((long *)varp == &p_wc
1495 || (long *)varp == &p_wcm)
1496 && (*arg == '<'
1497 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001498 || (*arg != NUL
1499 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 && !VIM_ISDIGIT(*arg))))
1501 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001502 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (value == 0 && (long *)varp != &p_wcm)
1504 {
1505 errmsg = e_invarg;
1506 goto skip;
1507 }
1508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001511 // Allow negative (for 'undolevels'), octal and
1512 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001513 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001514 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001515 if (i == 0 || (arg[i] != NUL
1516 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001518 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 goto skip;
1520 }
1521 }
1522 else
1523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001524 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 goto skip;
1526 }
1527
1528 if (adding)
1529 value = *(long *)varp + value;
1530 if (prepending)
1531 value = *(long *)varp * value;
1532 if (removing)
1533 value = *(long *)varp - value;
1534 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001535 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001537 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001539 char_u *save_arg = NULL;
1540 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001541 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001542 char_u *newval;
1543 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001544 char_u *origval_l = NULL;
1545 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001546#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001547 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001548 char_u *saved_origval_l = NULL;
1549 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001550 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001551#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001552 unsigned newlen;
1553 int comma;
1554 int bs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001555 int new_value_alloced; // new string option
1556 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001558 // When using ":set opt=val" for a global option
1559 // with a local value the local value will be
1560 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001562 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 varp = options[opt_idx].var;
1564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001565 // The old value is kept until we are sure that the
1566 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001568
Bram Moolenaard7c96872019-06-15 17:12:48 +02001569 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1570 {
1571 origval_l = *(char_u **)get_varp_scope(
1572 &(options[opt_idx]), OPT_LOCAL);
1573 origval_g = *(char_u **)get_varp_scope(
1574 &(options[opt_idx]), OPT_GLOBAL);
1575
1576 // A global-local string option might have an empty
1577 // option as value to indicate that the global
1578 // value should be used.
1579 if (((int)options[opt_idx].indir & PV_BOTH)
1580 && origval_l == empty_option)
1581 origval_l = origval_g;
1582 }
1583
1584 // When setting the local value of a global
1585 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001586 if (((int)options[opt_idx].indir & PV_BOTH)
1587 && (opt_flags & OPT_LOCAL))
1588 origval = *(char_u **)get_varp(
1589 &options[opt_idx]);
1590 else
1591 origval = oldval;
1592
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001593 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 newval = options[opt_idx].def_val[
1596 ((flags & P_VI_DEF) || cp_val)
1597 ? VI_DEFAULT : VIM_DEFAULT];
1598 if ((char_u **)varp == &p_bg)
1599 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001600 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601#ifdef FEAT_GUI
1602 if (gui.in_use)
1603 newval = gui_bg_default();
1604 else
1605#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001606 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001609 // expand environment variables and ~ (since the
1610 // default value was already expanded, only
1611 // required when an environment variable was set
1612 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (newval == NULL)
1614 newval = empty_option;
1615 else
1616 {
1617 s = option_expand(opt_idx, newval);
1618 if (s == NULL)
1619 s = newval;
1620 newval = vim_strsave(s);
1621 }
1622 new_value_alloced = TRUE;
1623 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001624 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
1626 newval = vim_strsave(*(char_u **)get_varp_scope(
1627 &(options[opt_idx]), OPT_GLOBAL));
1628 new_value_alloced = TRUE;
1629 }
1630 else
1631 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001632 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 /*
1635 * Set 'keywordprg' to ":help" if an empty
1636 * value was passed to :set by the user.
1637 * Misuse errbuf[] for the resulting string.
1638 */
1639 if (varp == (char_u *)&p_kp
1640 && (*arg == NUL || *arg == ' '))
1641 {
1642 STRCPY(errbuf, ":help");
1643 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001644 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
1646 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001647 * Convert 'backspace' number to string, for
1648 * adding, prepending and removing string.
1649 */
1650 else if (varp == (char_u *)&p_bs
1651 && VIM_ISDIGIT(**(char_u **)varp))
1652 {
1653 i = getdigits((char_u **)varp);
1654 switch (i)
1655 {
1656 case 0:
1657 *(char_u **)varp = empty_option;
1658 break;
1659 case 1:
1660 *(char_u **)varp = vim_strsave(
1661 (char_u *)"indent,eol");
1662 break;
1663 case 2:
1664 *(char_u **)varp = vim_strsave(
1665 (char_u *)"indent,eol,start");
1666 break;
1667 }
1668 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001669 if (origval == oldval)
1670 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001671 if (origval_l == oldval)
1672 origval_l = *(char_u **)varp;
1673 if (origval_g == oldval)
1674 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001675 oldval = *(char_u **)varp;
1676 }
1677 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 * Convert 'whichwrap' number to string, for
1679 * backwards compatibility with Vim 3.0.
1680 * Misuse errbuf[] for the resulting string.
1681 */
1682 else if (varp == (char_u *)&p_ww
1683 && VIM_ISDIGIT(*arg))
1684 {
1685 *errbuf = NUL;
1686 i = getdigits(&arg);
1687 if (i & 1)
1688 STRCAT(errbuf, "b,");
1689 if (i & 2)
1690 STRCAT(errbuf, "s,");
1691 if (i & 4)
1692 STRCAT(errbuf, "h,l,");
1693 if (i & 8)
1694 STRCAT(errbuf, "<,>,");
1695 if (i & 16)
1696 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001697 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 errbuf[STRLEN(errbuf) - 1] = NUL;
1699 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001700 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 /*
1703 * Remove '>' before 'dir' and 'bdir', for
1704 * backwards compatibility with version 3.0
1705 */
1706 else if ( *arg == '>'
1707 && (varp == (char_u *)&p_dir
1708 || varp == (char_u *)&p_bdir))
1709 {
1710 ++arg;
1711 }
1712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 /*
1714 * Copy the new string into allocated memory.
1715 * Can't use set_string_option_direct(), because
1716 * we need to remove the backslashes.
1717 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001718 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 newlen = (unsigned)STRLEN(arg) + 1;
1720 if (adding || prepending || removing)
1721 newlen += (unsigned)STRLEN(origval) + 1;
1722 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001723 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 break;
1725 s = newval;
1726
1727 /*
1728 * Copy the string, skip over escaped chars.
1729 * For MS-DOS and WIN32 backslashes before normal
1730 * file name characters are not removed, and keep
1731 * backslash at start, for "\\machine\path", but
1732 * do remove it for "\\\\machine\\path".
1733 * The reverse is found in ExpandOldSetting().
1734 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001735 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
1737 if (*arg == '\\' && arg[1] != NUL
1738#ifdef BACKSLASH_IN_FILENAME
1739 && !((flags & P_EXPAND)
1740 && vim_isfilec(arg[1])
1741 && (arg[1] != '\\'
1742 || (s == newval
1743 && arg[2] != '\\')))
1744#endif
1745 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001746 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001748 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001750 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 mch_memmove(s, arg, (size_t)i);
1752 arg += i;
1753 s += i;
1754 }
1755 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 *s++ = *arg++;
1757 }
1758 *s = NUL;
1759
1760 /*
1761 * Expand environment variables and ~.
1762 * Don't do it when adding without inserting a
1763 * comma.
1764 */
1765 if (!(adding || prepending || removing)
1766 || (flags & P_COMMA))
1767 {
1768 s = option_expand(opt_idx, newval);
1769 if (s != NULL)
1770 {
1771 vim_free(newval);
1772 newlen = (unsigned)STRLEN(s) + 1;
1773 if (adding || prepending || removing)
1774 newlen += (unsigned)STRLEN(origval) + 1;
1775 newval = alloc(newlen);
1776 if (newval == NULL)
1777 break;
1778 STRCPY(newval, s);
1779 }
1780 }
1781
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001782 // locate newval[] in origval[] when removing it
1783 // and when adding to avoid duplicates
1784 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (removing || (flags & P_NODUP))
1786 {
1787 i = (int)STRLEN(newval);
1788 bs = 0;
1789 for (s = origval; *s; ++s)
1790 {
1791 if ((!(flags & P_COMMA)
1792 || s == origval
1793 || (s[-1] == ',' && !(bs & 1)))
1794 && STRNCMP(s, newval, i) == 0
1795 && (!(flags & P_COMMA)
1796 || s[i] == ','
1797 || s[i] == NUL))
1798 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 // Count backslashes. Only a comma with an
1800 // even number of backslashes or a single
1801 // backslash preceded by a comma before it
1802 // is recognized as a separator
Bram Moolenaar8f79acd2016-01-01 14:48:20 +01001803 if ((s > origval + 1
1804 && s[-1] == '\\'
1805 && s[-2] != ',')
1806 || (s == origval + 1
1807 && s[-1] == '\\'))
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 ++bs;
1810 else
1811 bs = 0;
1812 }
1813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001814 // do not add if already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if ((adding || prepending) && *s)
1816 {
1817 prepending = FALSE;
1818 adding = FALSE;
1819 STRCPY(newval, origval);
1820 }
1821 }
1822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001823 // concatenate the two strings; add a ',' if
1824 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (adding || prepending)
1826 {
1827 comma = ((flags & P_COMMA) && *origval != NUL
1828 && *newval != NUL);
1829 if (adding)
1830 {
1831 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001832 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001833 if (comma && i > 1
1834 && (flags & P_ONECOMMA) == P_ONECOMMA
1835 && origval[i - 1] == ','
1836 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001837 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 mch_memmove(newval + i + comma, newval,
1839 STRLEN(newval) + 1);
1840 mch_memmove(newval, origval, (size_t)i);
1841 }
1842 else
1843 {
1844 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001845 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847 if (comma)
1848 newval[i] = ',';
1849 }
1850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001851 // Remove newval[] from origval[]. (Note: "i" has
1852 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (removing)
1854 {
1855 STRCPY(newval, origval);
1856 if (*s)
1857 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001858 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (flags & P_COMMA)
1860 {
1861 if (s == origval)
1862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001863 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (s[i] == ',')
1865 ++i;
1866 }
1867 else
1868 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001869 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 --s;
1871 ++i;
1872 }
1873 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001874 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 }
1877
1878 if (flags & P_FLAGLIST)
1879 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001880 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001881 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001883 // if options have P_FLAGLIST and
1884 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001885 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001887 if (*s != ',' && *(s + 1) == ','
1888 && vim_strchr(s + 2, *s) != NULL)
1889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001890 // Remove the duplicated value and
1891 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001892 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001893 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001896 else
1897 {
1898 if ((!(flags & P_COMMA) || *s != ',')
1899 && vim_strchr(s + 1, *s) != NULL)
1900 {
1901 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001902 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001903 }
1904 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001905 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001909 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 arg = save_arg;
1911 new_value_alloced = TRUE;
1912 }
1913
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001914 /*
1915 * Set the new value.
1916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 *(char_u **)(varp) = newval;
1918
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001919#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001920 if (!starting
1921# ifdef FEAT_CRYPT
1922 && options[opt_idx].indir != PV_KEY
1923# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001924 && origval != NULL && newval != NULL)
1925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001926 // origval may be freed by
1927 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02001928 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001929 // newval (and varp) may become invalid if the
1930 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001931 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001932 if (origval_l != NULL)
1933 saved_origval_l = vim_strsave(origval_l);
1934 if (origval_g != NULL)
1935 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001936 }
Bram Moolenaar53744302015-07-17 17:38:22 +02001937#endif
1938
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001939 {
1940 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001941 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001942
1943 // When an option is set in the sandbox, from a
1944 // modeline or in secure mode, then deal with side
1945 // effects in secure mode. Also when the value was
1946 // set with the P_INSECURE flag and is not
1947 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001948 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001949#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001950 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001951#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001952 || (!value_is_replaced && (*p & P_INSECURE)))
1953 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001954
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001955 // Handle side effects, and set the global value
1956 // for ":set" on local options. Note: when setting
1957 // 'syntax' or 'filetype' autocommands may be
1958 // triggered that can cause havoc.
1959 errmsg = did_set_string_option(
1960 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01001961 new_value_alloced, oldval, errbuf,
1962 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001963
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001964 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001967#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001968 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001969 trigger_optionsset_string(
1970 opt_idx, opt_flags, saved_origval,
1971 saved_origval_l, saved_origval_g,
1972 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001973 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001974 vim_free(saved_origval_l);
1975 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001976 vim_free(saved_newval);
1977#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001978 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (errmsg != NULL)
1980 goto skip;
1981 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001982 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 char_u *p;
1985
1986 if (nextchar == '&')
1987 {
1988 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991 else
1992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001993 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001994 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (*p == '\\' && p[1] != NUL)
1996 ++p;
1997 nextchar = *p;
1998 *p = NUL;
1999 add_termcode(key_name, arg, FALSE);
2000 *p = nextchar;
2001 }
2002 if (full_screen)
2003 ttest(FALSE);
2004 redraw_all_later(CLEAR);
2005 }
2006 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002009 did_set_option(
2010 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012
2013skip:
2014 /*
2015 * Advance to next argument.
2016 * - skip until a blank found, taking care of backslashes
2017 * - skip blanks
2018 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2019 */
2020 for (i = 0; i < 2 ; ++i)
2021 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002022 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (*arg++ == '\\' && *arg != NUL)
2024 ++arg;
2025 arg = skipwhite(arg);
2026 if (*arg != '=')
2027 break;
2028 }
2029 }
2030
2031 if (errmsg != NULL)
2032 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002033 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002034 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (i + (arg - startarg) < IOSIZE)
2036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 STRCAT(IObuff, ": ");
2039 mch_memmove(IObuff + i, startarg, (arg - startarg));
2040 IObuff[i + (arg - startarg)] = NUL;
2041 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002042 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 trans_characters(IObuff, IOSIZE);
2044
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 ++no_wait_return; // wait_return done later
2046 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 --no_wait_return;
2048
2049 return FAIL;
2050 }
2051
2052 arg = skipwhite(arg);
2053 }
2054
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002055theend:
2056 if (silent_mode && did_show)
2057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002059 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002060 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002061 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002062 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002063 out_flush();
2064 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002065 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002066 }
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 return OK;
2069}
2070
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002071/*
2072 * Call this when an option has been given a new value through a user command.
2073 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2074 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002076did_set_option(
2077 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002078 int opt_flags, // possibly with OPT_MODELINE
2079 int new_value, // value was replaced completely
2080 int value_checked) // value was checked to be safe, no need to set the
2081 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002082{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002083 long_u *p;
2084
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002085 options[opt_idx].flags |= P_WAS_SET;
2086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002087 // When an option is set in the sandbox, from a modeline or in secure mode
2088 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2089 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002090 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002091 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002092#ifdef HAVE_SANDBOX
2093 || sandbox != 0
2094#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002095 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002096 *p = *p | P_INSECURE;
2097 else if (new_value)
2098 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002099}
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101/*
2102 * Convert a key name or string into a key value.
2103 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002104 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002106 int
2107string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002110 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (*arg == '^')
2112 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002113 if (multi_byte)
2114 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 return *arg;
2116}
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#ifdef FEAT_TITLE
2119/*
2120 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2121 * maketitle() to create and display it.
2122 * When switching the title or icon off, call mch_restore_title() to get
2123 * the old value back.
2124 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002125 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002126did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 if (starting != NO_SCREEN
2129#ifdef FEAT_GUI
2130 && !gui.starting
2131#endif
2132 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134}
2135#endif
2136
2137/*
2138 * set_options_bin - called when 'bin' changes value.
2139 */
2140 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002141set_options_bin(
2142 int oldval,
2143 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002144 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 /*
2147 * The option values that are changed when 'bin' changes are
2148 * copied when 'bin is set and restored when 'bin' is reset.
2149 */
2150 if (newval)
2151 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002152 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 if (!(opt_flags & OPT_GLOBAL))
2155 {
2156 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2157 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2158 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2159 curbuf->b_p_et_nobin = curbuf->b_p_et;
2160 }
2161 if (!(opt_flags & OPT_LOCAL))
2162 {
2163 p_tw_nobin = p_tw;
2164 p_wm_nobin = p_wm;
2165 p_ml_nobin = p_ml;
2166 p_et_nobin = p_et;
2167 }
2168 }
2169
2170 if (!(opt_flags & OPT_GLOBAL))
2171 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002172 curbuf->b_p_tw = 0; // no automatic line wrap
2173 curbuf->b_p_wm = 0; // no automatic line wrap
2174 curbuf->b_p_ml = 0; // no modelines
2175 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177 if (!(opt_flags & OPT_LOCAL))
2178 {
2179 p_tw = 0;
2180 p_wm = 0;
2181 p_ml = FALSE;
2182 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002183 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002186 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
2188 if (!(opt_flags & OPT_GLOBAL))
2189 {
2190 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2191 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2192 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2193 curbuf->b_p_et = curbuf->b_p_et_nobin;
2194 }
2195 if (!(opt_flags & OPT_LOCAL))
2196 {
2197 p_tw = p_tw_nobin;
2198 p_wm = p_wm_nobin;
2199 p_ml = p_ml_nobin;
2200 p_et = p_et_nobin;
2201 }
2202 }
2203}
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205/*
2206 * Expand environment variables for some string options.
2207 * These string options cannot be indirect!
2208 * If "val" is NULL expand the current value of the option.
2209 * Return pointer to NameBuff, or NULL when not expanded.
2210 */
2211 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002212option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2216 return NULL;
2217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // If val is longer than MAXPATHL no meaningful expansion can be done,
2219 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 if (val != NULL && STRLEN(val) > MAXPATHL)
2221 return NULL;
2222
2223 if (val == NULL)
2224 val = *(char_u **)options[opt_idx].var;
2225
2226 /*
2227 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2228 * Escape spaces when expanding 'tags', they are used to separate file
2229 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002230 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 */
2232 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002233 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002234#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002235 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2236#endif
2237 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return NULL;
2240
2241 return NameBuff;
2242}
2243
2244/*
2245 * After setting various option values: recompute variables that depend on
2246 * option values.
2247 */
2248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002249didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 (void)init_chartab();
2253
Bram Moolenaardac13472019-09-16 21:06:21 +02002254 didset_string_options();
2255
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002256#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002257 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002258 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002259 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002260 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002263 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 (void)check_cedit();
2265#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002266#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002267 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002268 fill_breakat_flags();
2269#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002270 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002271}
2272
2273/*
2274 * More side effects of setting options.
2275 */
2276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002277didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002278{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002279 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002280 (void)highlight_changed();
2281
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002282 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002283 check_opt_wim();
2284
2285 (void)set_chars_option(&p_lcs);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002286 // Parse default for 'fillchars'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002287 (void)set_chars_option(&p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002288
2289#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002290 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002291 (void)check_clipboard_option();
2292#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002293#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002294 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002295 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002296 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002297 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299}
2300
2301/*
2302 * Check for string options that are NULL (normally only termcap options).
2303 */
2304 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002305check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 int opt_idx;
2308
2309 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2310 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2311 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2312}
2313
2314/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002315 * Return the option index found by a pointer into term_strings[].
2316 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002318 int
2319get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002321 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2324 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002325 return opt_idx;
2326 return -1; // cannot happen: didn't find it!
2327}
2328
2329/*
2330 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2331 * Return the option index or -1 if not found.
2332 */
2333 int
2334set_term_option_alloced(char_u **p)
2335{
2336 int opt_idx = get_term_opt_idx(p);
2337
2338 if (opt_idx >= 0)
2339 options[opt_idx].flags |= P_ALLOCED;
2340 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341}
2342
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002343#if defined(FEAT_EVAL) || defined(PROTO)
2344/*
2345 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2346 * Return FALSE when it wasn't.
2347 * Return -1 for an unknown option.
2348 */
2349 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002350was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002351{
2352 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002353 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002354
2355 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002356 {
2357 flagp = insecure_flag(idx, opt_flags);
2358 return (*flagp & P_INSECURE) != 0;
2359 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002360 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002361 return -1;
2362}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002363
2364/*
2365 * Get a pointer to the flags used for the P_INSECURE flag of option
2366 * "opt_idx". For some local options a local flags field is used.
2367 */
2368 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002369insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002370{
2371 if (opt_flags & OPT_LOCAL)
2372 switch ((int)options[opt_idx].indir)
2373 {
2374#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002375 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002376#endif
2377#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002378# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002379 case PV_FDE: return &curwin->w_p_fde_flags;
2380 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002381# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002382# ifdef FEAT_BEVAL
2383 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2384# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002385# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002386 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002387# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002388 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002389# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002390 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002391# endif
2392#endif
2393 }
2394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002396 return &options[opt_idx].flags;
2397}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002398#endif
2399
Bram Moolenaardac13472019-09-16 21:06:21 +02002400#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002401/*
2402 * Redraw the window title and/or tab page text later.
2403 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002404void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002405{
2406 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002407 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002408}
2409#endif
2410
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002412 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2413 * characters or characters in "allowed".
2414 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002415 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002416valid_name(char_u *val, char *allowed)
2417{
2418 char_u *s;
2419
2420 for (s = val; *s != NUL; ++s)
2421 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2422 return FALSE;
2423 return TRUE;
2424}
2425
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002426#if defined(FEAT_EVAL) || defined(PROTO)
2427/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002428 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002429 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002430 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002431 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002432set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002433{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002434 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2435 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002436 sctx_T new_script_ctx = script_ctx;
2437
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002438 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002440 // Remember where the option was set. For local options need to do that
2441 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002442 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002443 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002444 if (both || (opt_flags & OPT_LOCAL))
2445 {
2446 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002447 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002448 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002449 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002450 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002451}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002452
2453/*
2454 * Set the script_ctx for a termcap option.
2455 * "name" must be the two character code, e.g. "RV".
2456 * When "name" is NULL use "opt_idx".
2457 */
2458 void
2459set_term_option_sctx_idx(char *name, int opt_idx)
2460{
2461 char_u buf[5];
2462 int idx;
2463
2464 if (name == NULL)
2465 idx = opt_idx;
2466 else
2467 {
2468 buf[0] = 't';
2469 buf[1] = '_';
2470 buf[2] = name[0];
2471 buf[3] = name[1];
2472 buf[4] = 0;
2473 idx = findoption(buf);
2474 }
2475 if (idx >= 0)
2476 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2477}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002478#endif
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480/*
2481 * Set the value of a boolean option, and take care of side effects.
2482 * Returns NULL for success, or an error message for an error.
2483 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002484 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002485set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002486 int opt_idx, // index in options[] table
2487 char_u *varp, // pointer to the option variable
2488 int value, // new value
2489 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002492#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002493 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002496 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 if ((secure
2498#ifdef HAVE_SANDBOX
2499 || sandbox != 0
2500#endif
2501 ) && (options[opt_idx].flags & P_SECURE))
2502 return e_secure;
2503
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002504#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002505 // Save the global value before changing anything. This is needed as for
2506 // a global-only option setting the "local value" in fact sets the global
2507 // value (since there is only one value).
2508 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2509 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2510 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002511#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002512
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002513 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002515 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002516 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517#endif
2518
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002519#ifdef FEAT_GUI
2520 need_mouse_correct = TRUE;
2521#endif
2522
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002523 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2525 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2526
2527 /*
2528 * Handle side effects of changing a bool option.
2529 */
2530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar920694c2016-08-21 17:45:02 +02002535#ifdef FEAT_LANGMAP
2536 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002537 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002538 p_lnr = !p_lrm;
2539 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002541 p_lrm = !p_lnr;
2542#endif
2543
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002544#ifdef FEAT_SYN_HL
2545 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2546 reset_cursorline();
2547#endif
2548
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002549#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002550 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002551 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2552 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002553 // Only take action when the option was set. When reset we do not
2554 // delete the undo file, the option may be set again without making
2555 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002556 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002557 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002558 char_u hash[UNDO_HASH_SIZE];
2559 buf_T *save_curbuf = curbuf;
2560
Bram Moolenaar29323592016-07-24 22:04:11 +02002561 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002562 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002563 // When 'undofile' is set globally: for every buffer, otherwise
2564 // only for the current buffer: Try to read in the undofile,
2565 // if one exists, the buffer wasn't changed and the buffer was
2566 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002567 if ((curbuf == save_curbuf
2568 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2569 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2570 {
2571 u_compute_hash(hash);
2572 u_read_undo(NULL, hash, curbuf->b_fname);
2573 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002574 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002575 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002576 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002577 }
2578#endif
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 else if ((int *)varp == &curbuf->b_p_ro)
2581 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002582 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2584 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002586 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002587 if (curbuf->b_p_ro)
2588 curbuf->b_did_warn = FALSE;
2589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002591 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#endif
2593 }
2594
Bram Moolenaar9c449722010-07-20 18:44:27 +02002595#ifdef FEAT_GUI
2596 else if ((int *)varp == &p_mh)
2597 {
2598 if (!p_mh)
2599 gui_mch_mousehide(FALSE);
2600 }
2601#endif
2602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002603 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002605 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002606# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002607 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002608 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2609 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002610 {
2611 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002612 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002613 }
2614# endif
2615# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002616 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002617# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002618 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002619#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002620 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002622 {
2623 redraw_titles();
2624 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002626 else if ((int *)varp == &curbuf->b_p_fixeol)
2627 {
2628 redraw_titles();
2629 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002630 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002631 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002632 {
2633 redraw_titles();
2634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635#endif
2636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 else if ((int *)varp == &curbuf->b_p_bin)
2639 {
2640 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2641#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002642 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#endif
2644 }
2645
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002646 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2648 {
2649 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2650 NULL, NULL, TRUE, curbuf);
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002653 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 else if ((int *)varp == &curbuf->b_p_swf)
2655 {
2656 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002657 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2660 // buf->b_p_swf
2661 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 else if ((int *)varp == &p_terse)
2666 {
2667 char_u *p;
2668
2669 p = vim_strchr(p_shm, SHM_SEARCH);
2670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (p_terse && p == NULL)
2673 {
2674 STRCPY(IObuff, p_shm);
2675 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002676 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002680 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002683 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 else if ((int *)varp == &p_paste)
2685 {
2686 paste_option_changed();
2687 }
2688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 else if ((int *)varp == &p_im)
2691 {
2692 if (p_im)
2693 {
2694 if ((State & INSERT) == 0)
2695 need_start_insertmode = TRUE;
2696 stop_insert_mode = FALSE;
2697 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002699 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 need_start_insertmode = FALSE;
2702 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002703 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 restart_edit = 0;
2706 }
2707 }
2708
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 else if ((int *)varp == &p_ic && p_hls)
2711 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002712 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714
2715#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 else if ((int *)varp == &p_hls)
2718 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002719 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721#endif
2722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2724 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 else if ((int *)varp == &curwin->w_p_scb)
2726 {
2727 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002730 curwin->w_scbind_pos = curwin->w_topline;
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaar4033c552017-09-16 20:54:51 +02002734#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else if ((int *)varp == &curwin->w_p_pvw)
2737 {
2738 if (curwin->w_p_pvw)
2739 {
2740 win_T *win;
2741
Bram Moolenaar29323592016-07-24 22:04:11 +02002742 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (win->w_p_pvw && win != curwin)
2744 {
2745 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002746 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748 }
2749 }
2750#endif
2751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 else if ((int *)varp == &curbuf->b_p_tx)
2754 {
2755 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2756 }
2757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 set_string_option_direct((char_u *)"ffs", -1,
2762 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002763 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 /*
2767 * When 'lisp' option changes include/exclude '-' in
2768 * keyword characters.
2769 */
2770#ifdef FEAT_LISP
2771 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775#endif
2776
2777#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002779 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002781 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
2783#endif
2784
2785 else if ((int *)varp == &curbuf->b_changed)
2786 {
2787 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002788 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002790 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794
2795#ifdef BACKSLASH_IN_FILENAME
2796 else if ((int *)varp == &p_ssl)
2797 {
2798 if (p_ssl)
2799 {
2800 psepc = '/';
2801 psepcN = '\\';
2802 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
2805 {
2806 psepc = '\\';
2807 psepcN = '/';
2808 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002811 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 buflist_slash_adjust();
2813 alist_slash_adjust();
2814# ifdef FEAT_EVAL
2815 scriptnames_slash_adjust();
2816# endif
2817 }
2818#endif
2819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 else if ((int *)varp == &curwin->w_p_wrap)
2822 {
2823 if (curwin->w_p_wrap)
2824 curwin->w_leftcol = 0;
2825 }
2826
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 else if ((int *)varp == &p_ea)
2828 {
2829 if (p_ea && !old_value)
2830 win_equal(curwin, FALSE, 0);
2831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 else if ((int *)varp == &p_wiv)
2834 {
2835 /*
2836 * When 'weirdinvert' changed, set/reset 't_xs'.
2837 * Then set 'weirdinvert' according to value of 't_xs'.
2838 */
2839 if (p_wiv && !old_value)
2840 T_XS = (char_u *)"y";
2841 else if (!p_wiv && old_value)
2842 T_XS = empty_option;
2843 p_wiv = (*T_XS != NUL);
2844 }
2845
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002846#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else if ((int *)varp == &p_beval)
2848 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002849 if (!balloonEvalForTerm)
2850 {
2851 if (p_beval && !old_value)
2852 gui_mch_enable_beval_area(balloonEval);
2853 else if (!p_beval && old_value)
2854 gui_mch_disable_beval_area(balloonEval);
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002857#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002858#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002859 else if ((int *)varp == &p_bevalterm)
2860 {
2861 mch_bevalterm_changed();
2862 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002865#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 else if ((int *)varp == &p_acd)
2867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002869 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871#endif
2872
2873#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else if ((int *)varp == &curwin->w_p_diff)
2876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002878 diff_buf_adjust(curwin);
2879# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 if (foldmethodIsDiff(curwin))
2881 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884#endif
2885
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002886#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002887 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 else if ((int *)varp == &p_imdisable)
2889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (p_imdisable)
2892 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02002893 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 // When the option is set from an autocommand, it may need to take
2895 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02002896 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898#endif
2899
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002900#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002902 else if ((int *)varp == &curwin->w_p_spell)
2903 {
2904 if (curwin->w_p_spell)
2905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906 char *errmsg = did_set_spelllang(curwin);
2907
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002908 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002910 }
2911 }
2912#endif
2913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#ifdef FEAT_ARABIC
2915 if ((int *)varp == &curwin->w_p_arab)
2916 {
2917 if (curwin->w_p_arab)
2918 {
2919 /*
2920 * 'arabic' is set, handle various sub-settings.
2921 */
2922 if (!p_tbidi)
2923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 if (!curwin->w_p_rl)
2926 {
2927 curwin->w_p_rl = TRUE;
2928 changed_window_setting();
2929 }
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (!p_arshape)
2933 {
2934 p_arshape = TRUE;
2935 redraw_later_clear();
2936 }
2937 }
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // Arabic requires a utf-8 encoding, inform the user if its not
2940 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002942 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00002943 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
2944
Bram Moolenaar8820b482017-03-16 17:23:31 +01002945 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01002946 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00002947#ifdef FEAT_EVAL
2948 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
2949#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
2958 OPT_LOCAL);
2959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 else
2962 {
2963 /*
2964 * 'arabic' is reset, handle various sub-settings.
2965 */
2966 if (!p_tbidi)
2967 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 if (curwin->w_p_rl)
2970 {
2971 curwin->w_p_rl = FALSE;
2972 changed_window_setting();
2973 }
2974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 // 'arabicshape' isn't reset, it is a global option and
2976 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // 'delcombine' isn't reset, it is a global option and another
2980 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
2982# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 curbuf->b_p_iminsert = B_IMODE_NONE;
2985 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2986# endif
2987 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00002988 }
2989
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00002990#endif
2991
Bram Moolenaar44826212019-08-22 21:23:20 +02002992#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
2993 else if (((int *)varp == &curwin->w_p_nu
2994 || (int *)varp == &curwin->w_p_rnu)
2995 && gui.in_use
2996 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
2997 && curbuf->b_signlist != NULL)
2998 {
2999 // If the 'number' or 'relativenumber' options are modified and
3000 // 'signcolumn' is set to 'number', then clear the screen for a full
3001 // refresh. Otherwise the sign icons are not displayed properly in the
3002 // number column. If the 'number' option is set and only the
3003 // 'relativenumber' option is toggled, then don't refresh the screen
3004 // (optimization).
3005 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3006 redraw_all_later(CLEAR);
3007 }
3008#endif
3009
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003010#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003011 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003012 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003013 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003014# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003016 if (
3017# ifdef VIMDLL
3018 !gui.in_use && !gui.starting &&
3019# endif
3020 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003021 {
3022 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003023 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003024 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003025 if (is_term_win32())
3026 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003027# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003028# ifdef FEAT_GUI
3029 if (!gui.in_use && !gui.starting)
3030# endif
3031 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003032# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003034 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003035 {
3036 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003037 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003038 init_highlight(TRUE, FALSE);
3039 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003040# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003041 }
3042#endif
3043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 /*
3045 * End of handling side effects for bool options.
3046 */
3047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003048 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 options[opt_idx].flags |= P_WAS_SET;
3051
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003052#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003053 // Don't do this while starting up or recursively.
3054 if (!starting && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02003055 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02003056 char_u buf_old[2], buf_old_global[2], buf_new[2], buf_type[7];
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003057
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003058 vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003059 vim_snprintf((char *)buf_old_global, 2, "%d",
3060 old_global_value ? TRUE: FALSE);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003061 vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003062 vim_snprintf((char *)buf_type, 7, "%s",
3063 (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02003064 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3065 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3066 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003067 if (opt_flags & OPT_LOCAL)
3068 {
3069 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3070 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3071 }
3072 if (opt_flags & OPT_GLOBAL)
3073 {
3074 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3075 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3076 }
3077 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3078 {
3079 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3080 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3081 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3082 }
3083 if (opt_flags & OPT_MODELINE)
3084 {
3085 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3086 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3087 }
3088 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3089 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003090 reset_v_option_vars();
3091 }
3092#endif
3093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003095 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003096 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003097 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 check_redraw(options[opt_idx].flags);
3099
3100 return NULL;
3101}
3102
3103/*
3104 * Set the value of a number option, and take care of side effects.
3105 * Returns NULL for success, or an error message for an error.
3106 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003107 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003108set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 int opt_idx, // index in options[] table
3110 char_u *varp, // pointer to the option variable
3111 long value, // new value
3112 char *errbuf, // buffer for error messages
3113 size_t errbuflen, // length of "errbuf"
3114 int opt_flags) // OPT_LOCAL, OPT_GLOBAL and
3115 // OPT_MODELINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003117 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003119#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003120 long old_global_value = 0; // only used when setting a local and
3121 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003122#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003123 long old_Rows = Rows; // remember old Rows
3124 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 long *pp = (long *)varp;
3126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003128 if ((secure
3129#ifdef HAVE_SANDBOX
3130 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003132 ) && (options[opt_idx].flags & P_SECURE))
3133 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003135#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003136 // Save the global value before changing anything. This is needed as for
3137 // a global-only option setting the "local value" infact sets the global
3138 // value (since there is only one value).
3139 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003140 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3141 OPT_GLOBAL);
3142#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 *pp = value;
3145#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003147 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003149#ifdef FEAT_GUI
3150 need_mouse_correct = TRUE;
3151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
Bram Moolenaar14f24742012-08-08 18:01:05 +02003153 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003156#ifdef FEAT_VARTABS
3157 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3158 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3159 ? tabstop_first(curbuf->b_p_vts_array)
3160 : curbuf->b_p_ts;
3161#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
3165
3166 /*
3167 * Number options that need some action when changed
3168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (pp == &p_wh || pp == &p_hh)
3170 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003171 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (p_wh < 1)
3173 {
3174 errmsg = e_positive;
3175 p_wh = 1;
3176 }
3177 if (p_wmh > p_wh)
3178 {
3179 errmsg = e_winheight;
3180 p_wh = p_wmh;
3181 }
3182 if (p_hh < 0)
3183 {
3184 errmsg = e_positive;
3185 p_hh = 0;
3186 }
3187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003188 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003189 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 if (pp == &p_wh && curwin->w_height < p_wh)
3192 win_setheight((int)p_wh);
3193 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3194 win_setheight((int)p_hh);
3195 }
3196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 else if (pp == &p_wmh)
3198 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003199 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (p_wmh < 0)
3201 {
3202 errmsg = e_positive;
3203 p_wmh = 0;
3204 }
3205 if (p_wmh > p_wh)
3206 {
3207 errmsg = e_winheight;
3208 p_wmh = p_wh;
3209 }
3210 win_setminheight();
3211 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003212 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003214 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (p_wiw < 1)
3216 {
3217 errmsg = e_positive;
3218 p_wiw = 1;
3219 }
3220 if (p_wmw > p_wiw)
3221 {
3222 errmsg = e_winwidth;
3223 p_wiw = p_wmw;
3224 }
3225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003227 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 win_setwidth((int)p_wiw);
3229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 else if (pp == &p_wmw)
3231 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003232 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (p_wmw < 0)
3234 {
3235 errmsg = e_positive;
3236 p_wmw = 0;
3237 }
3238 if (p_wmw > p_wiw)
3239 {
3240 errmsg = e_winwidth;
3241 p_wmw = p_wiw;
3242 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003243 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003246 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 else if (pp == &p_ls)
3248 {
3249 last_status(FALSE);
3250 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003251
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003252 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003253 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003255 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258#ifdef FEAT_GUI
3259 else if (pp == &p_linespace)
3260 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003261 // Recompute gui.char_height and resize the Vim window to keep the
3262 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003263 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003264 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
3266#endif
3267
3268#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003269 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 else if (pp == &curwin->w_p_fdl)
3271 {
3272 if (curwin->w_p_fdl < 0)
3273 curwin->w_p_fdl = 0;
3274 newFoldLevel();
3275 }
3276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003277 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else if (pp == &curwin->w_p_fml)
3279 {
3280 foldUpdateAll(curwin);
3281 }
3282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003283 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 else if (pp == &curwin->w_p_fdn)
3285 {
3286 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3287 foldUpdateAll(curwin);
3288 }
3289
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else if (pp == &curwin->w_p_fdc)
3292 {
3293 if (curwin->w_p_fdc < 0)
3294 {
3295 errmsg = e_positive;
3296 curwin->w_p_fdc = 0;
3297 }
3298 else if (curwin->w_p_fdc > 12)
3299 {
3300 errmsg = e_invarg;
3301 curwin->w_p_fdc = 12;
3302 }
3303 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003306#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003307 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3309 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003310# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (foldmethodIsIndent(curwin))
3312 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003313# endif
3314# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003315 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3316 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003317 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3318 parse_cino(curbuf);
3319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003323 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003324 else if (pp == &p_mco)
3325 {
3326 if (p_mco > MAX_MCO)
3327 p_mco = MAX_MCO;
3328 else if (p_mco < 0)
3329 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003330 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003331 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 else if (pp == &curbuf->b_p_iminsert)
3334 {
3335 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3336 {
3337 errmsg = e_invarg;
3338 curbuf->b_p_iminsert = B_IMODE_NONE;
3339 }
3340 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003341 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003343#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003344 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 status_redraw_curbuf();
3346#endif
3347 }
3348
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003349#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003350 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003351 else if (pp == &p_imst)
3352 {
3353 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3354 errmsg = e_invarg;
3355 }
3356#endif
3357
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003358 else if (pp == &p_window)
3359 {
3360 if (p_window < 1)
3361 p_window = 1;
3362 else if (p_window >= Rows)
3363 p_window = Rows - 1;
3364 }
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 else if (pp == &curbuf->b_p_imsearch)
3367 {
3368 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3369 {
3370 errmsg = e_invarg;
3371 curbuf->b_p_imsearch = B_IMODE_NONE;
3372 }
3373 p_imsearch = curbuf->b_p_imsearch;
3374 }
3375
3376#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003377 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else if (pp == &p_titlelen)
3379 {
3380 if (p_titlelen < 0)
3381 {
3382 errmsg = e_positive;
3383 p_titlelen = 85;
3384 }
3385 if (starting != NO_SCREEN && old_value != p_titlelen)
3386 need_maketitle = TRUE;
3387 }
3388#endif
3389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003390 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else if (pp == &p_ch)
3392 {
3393 if (p_ch < 1)
3394 {
3395 errmsg = e_positive;
3396 p_ch = 1;
3397 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003398 if (p_ch > Rows - min_rows() + 1)
3399 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003401 // Only compute the new window layout when startup has been
3402 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (p_ch != old_value && full_screen
3404#ifdef FEAT_GUI
3405 && !gui.starting
3406#endif
3407 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003408 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
3410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003411 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 else if (pp == &p_uc)
3413 {
3414 if (p_uc < 0)
3415 {
3416 errmsg = e_positive;
3417 p_uc = 100;
3418 }
3419 if (p_uc && !old_value)
3420 ml_open_files();
3421 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003422#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003423 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003424 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003425 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003426 {
3427 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003428 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003429 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003430 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003431 {
3432 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003433 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003434 }
3435 }
3436#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003437#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003438 else if (pp == &p_mzq)
3439 mzvim_reset_timer();
3440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003442#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003444 else if (pp == &p_pyx)
3445 {
3446 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3447 errmsg = e_invarg;
3448 }
3449#endif
3450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003451 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 else if (pp == &p_ul)
3453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003456 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 p_ul = value;
3458 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003459 else if (pp == &curbuf->b_p_ul)
3460 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003461 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003462 curbuf->b_p_ul = old_value;
3463 u_sync(TRUE);
3464 curbuf->b_p_ul = value;
3465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003467#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003469 else if (pp == &curwin->w_p_nuw)
3470 {
3471 if (curwin->w_p_nuw < 1)
3472 {
3473 errmsg = e_positive;
3474 curwin->w_p_nuw = 1;
3475 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003476 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003477 {
3478 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003479 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003480 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003481 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003482 }
3483#endif
3484
Bram Moolenaar1a384422010-07-14 19:53:30 +02003485 else if (pp == &curbuf->b_p_tw)
3486 {
3487 if (curbuf->b_p_tw < 0)
3488 {
3489 errmsg = e_positive;
3490 curbuf->b_p_tw = 0;
3491 }
3492#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003493 {
3494 win_T *wp;
3495 tabpage_T *tp;
3496
3497 FOR_ALL_TAB_WINDOWS(tp, wp)
3498 check_colorcolumn(wp);
3499 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003500#endif
3501 }
3502
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 /*
3504 * Check the bounds for numeric options here
3505 */
3506 if (Rows < min_rows() && full_screen)
3507 {
3508 if (errbuf != NULL)
3509 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003510 vim_snprintf((char *)errbuf, errbuflen,
3511 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 errmsg = errbuf;
3513 }
3514 Rows = min_rows();
3515 }
3516 if (Columns < MIN_COLUMNS && full_screen)
3517 {
3518 if (errbuf != NULL)
3519 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003520 vim_snprintf((char *)errbuf, errbuflen,
3521 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 errmsg = errbuf;
3523 }
3524 Columns = MIN_COLUMNS;
3525 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003526 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 /*
3529 * If the screen (shell) height has been changed, assume it is the
3530 * physical screenheight.
3531 */
3532 if (old_Rows != Rows || old_Columns != Columns)
3533 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003534 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (updating_screen)
3536 *pp = old_value;
3537 else if (full_screen
3538#ifdef FEAT_GUI
3539 && !gui.starting
3540#endif
3541 )
3542 set_shellsize((int)Columns, (int)Rows, TRUE);
3543 else
3544 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003545 // Postpone the resizing; check the size and cmdline position for
3546 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 check_shellsize();
3548 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3549 cmdline_row = Rows - p_ch;
3550 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003551 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003552 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (curbuf->b_p_ts <= 0)
3556 {
3557 errmsg = e_positive;
3558 curbuf->b_p_ts = 8;
3559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (p_tm < 0)
3561 {
3562 errmsg = e_positive;
3563 p_tm = 0;
3564 }
3565 if ((curwin->w_p_scr <= 0
3566 || (curwin->w_p_scr > curwin->w_height
3567 && curwin->w_height > 0))
3568 && full_screen)
3569 {
3570 if (pp == &(curwin->w_p_scr))
3571 {
3572 if (curwin->w_p_scr != 0)
3573 errmsg = e_scroll;
3574 win_comp_scroll(curwin);
3575 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003576 // If 'scroll' became invalid because of a side effect silently adjust
3577 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 else if (curwin->w_p_scr <= 0)
3579 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003580 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 curwin->w_p_scr = curwin->w_height;
3582 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003583 if (p_hi < 0)
3584 {
3585 errmsg = e_positive;
3586 p_hi = 0;
3587 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003588 else if (p_hi > 10000)
3589 {
3590 errmsg = e_invarg;
3591 p_hi = 10000;
3592 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003593 if (p_re < 0 || p_re > 2)
3594 {
3595 errmsg = e_invarg;
3596 p_re = 0;
3597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (p_report < 0)
3599 {
3600 errmsg = e_positive;
3601 p_report = 1;
3602 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003603 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003605 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 p_sj = Rows / 2;
3607 else
3608 {
3609 errmsg = e_scroll;
3610 p_sj = 1;
3611 }
3612 }
3613 if (p_so < 0 && full_screen)
3614 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003615 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 p_so = 0;
3617 }
3618 if (p_siso < 0 && full_screen)
3619 {
3620 errmsg = e_positive;
3621 p_siso = 0;
3622 }
3623#ifdef FEAT_CMDWIN
3624 if (p_cwh < 1)
3625 {
3626 errmsg = e_positive;
3627 p_cwh = 1;
3628 }
3629#endif
3630 if (p_ut < 0)
3631 {
3632 errmsg = e_positive;
3633 p_ut = 2000;
3634 }
3635 if (p_ss < 0)
3636 {
3637 errmsg = e_positive;
3638 p_ss = 0;
3639 }
3640
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003641 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3643 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3644
3645 options[opt_idx].flags |= P_WAS_SET;
3646
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003647#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003648 // Don't do this while starting up, failure or recursively.
3649 if (!starting && errmsg == NULL && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02003650 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02003651 char_u buf_old[11], buf_old_global[11], buf_new[11], buf_type[7];
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003652 vim_snprintf((char *)buf_old, 10, "%ld", old_value);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003653 vim_snprintf((char *)buf_old_global, 10, "%ld", old_global_value);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003654 vim_snprintf((char *)buf_new, 10, "%ld", value);
3655 vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02003656 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3657 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3658 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003659 if (opt_flags & OPT_LOCAL)
3660 {
3661 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3662 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3663 }
3664 if (opt_flags & OPT_GLOBAL)
3665 {
3666 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3667 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3668 }
3669 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3670 {
3671 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3672 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3673 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3674 }
3675 if (opt_flags & OPT_MODELINE)
3676 {
3677 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3678 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3679 }
3680 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3681 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003682 reset_v_option_vars();
3683 }
3684#endif
3685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003686 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003687 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003688 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003689 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 check_redraw(options[opt_idx].flags);
3691
3692 return errmsg;
3693}
3694
3695/*
3696 * Called after an option changed: check if something needs to be redrawn.
3697 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003698 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003699check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003701 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003702 int doclear = (flags & P_RCLR) == P_RCLR;
3703 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003705 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3709 changed_window_setting();
3710 if (flags & P_RBUF)
3711 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003712 if (flags & P_RWINONLY)
3713 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003714 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 redraw_all_later(CLEAR);
3716 else if (all)
3717 redraw_all_later(NOT_VALID);
3718}
3719
3720/*
3721 * Find index for option 'arg'.
3722 * Return -1 if not found.
3723 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003724 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003725findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 int opt_idx;
3728 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 int is_term_opt;
3731
3732 /*
3733 * For first call: Initialize the quick-access table.
3734 * It contains the index for the first option that starts with a certain
3735 * letter. There are 26 letters, plus the first "t_" option.
3736 */
3737 if (quick_tab[1] == 0)
3738 {
3739 p = options[0].fullname;
3740 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3741 {
3742 if (s[0] != p[0])
3743 {
3744 if (s[0] == 't' && s[1] == '_')
3745 quick_tab[26] = opt_idx;
3746 else
3747 quick_tab[CharOrdLow(s[0])] = opt_idx;
3748 }
3749 p = s;
3750 }
3751 }
3752
3753 /*
3754 * Check for name starting with an illegal character.
3755 */
3756#ifdef EBCDIC
3757 if (!islower(arg[0]))
3758#else
3759 if (arg[0] < 'a' || arg[0] > 'z')
3760#endif
3761 return -1;
3762
3763 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3764 if (is_term_opt)
3765 opt_idx = quick_tab[26];
3766 else
3767 opt_idx = quick_tab[CharOrdLow(arg[0])];
3768 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3769 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003770 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 break;
3772 }
3773 if (s == NULL && !is_term_opt)
3774 {
3775 opt_idx = quick_tab[CharOrdLow(arg[0])];
3776 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3777 {
3778 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003779 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 break;
3781 s = NULL;
3782 }
3783 }
3784 if (s == NULL)
3785 opt_idx = -1;
3786 return opt_idx;
3787}
3788
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003789#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790/*
3791 * Get the value for an option.
3792 *
3793 * Returns:
3794 * Number or Toggle option: 1, *numval gets value.
3795 * String option: 0, *stringval gets allocated string.
3796 * Hidden Number or Toggle option: -1.
3797 * hidden String option: -2.
3798 * unknown option: -3.
3799 */
3800 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003801get_option_value(
3802 char_u *name,
3803 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003804 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003805 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
3807 int opt_idx;
3808 char_u *varp;
3809
3810 opt_idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003811 if (opt_idx < 0) // unknown option
Bram Moolenaare353c402017-02-04 19:49:16 +01003812 {
3813 int key;
3814
3815 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02003816 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003817 {
3818 char_u key_name[2];
3819 char_u *p;
3820
3821 if (key < 0)
3822 {
3823 key_name[0] = KEY2TERMCAP0(key);
3824 key_name[1] = KEY2TERMCAP1(key);
3825 }
3826 else
3827 {
3828 key_name[0] = KS_KEY;
3829 key_name[1] = (key & 0xff);
3830 }
3831 p = find_termcode(key_name);
3832 if (p != NULL)
3833 {
3834 if (stringval != NULL)
3835 *stringval = vim_strsave(p);
3836 return 0;
3837 }
3838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return -3;
Bram Moolenaare353c402017-02-04 19:49:16 +01003840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
3842 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3843
3844 if (options[opt_idx].flags & P_STRING)
3845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003846 if (varp == NULL) // hidden option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 return -2;
3848 if (stringval != NULL)
3849 {
3850#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003851 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003852 if ((char_u **)varp == &curbuf->b_p_key
3853 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 *stringval = vim_strsave((char_u *)"*****");
3855 else
3856#endif
3857 *stringval = vim_strsave(*(char_u **)(varp));
3858 }
3859 return 0;
3860 }
3861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003862 if (varp == NULL) // hidden option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 return -1;
3864 if (options[opt_idx].flags & P_NUM)
3865 *numval = *(long *)varp;
3866 else
3867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003868 // Special case: 'modified' is b_changed, but we also want to consider
3869 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if ((int *)varp == &curbuf->b_changed)
3871 *numval = curbufIsChanged();
3872 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003873 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875 return 1;
3876}
3877#endif
3878
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003879#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003880/*
3881 * Returns the option attributes and its value. Unlike the above function it
3882 * will return either global value or local value of the option depending on
3883 * what was requested, but it will never return global value if it was
3884 * requested to return local one and vice versa. Neither it will return
3885 * buffer-local value if it was requested to return window-local one.
3886 *
3887 * Pretends that option is absent if it is not present in the requested scope
3888 * (i.e. has no global, window-local or buffer-local value depending on
3889 * opt_type). Uses
3890 *
3891 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003892 * 0 hidden or unknown option, also option that does not have requested
3893 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003894 * see SOPT_* in vim.h for other flags
3895 *
3896 * Possible opt_type values: see SREQ_* in vim.h
3897 */
3898 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003899get_option_value_strict(
3900 char_u *name,
3901 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003902 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003903 int opt_type,
3904 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003905{
3906 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003907 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003908 struct vimoption *p;
3909 int r = 0;
3910
3911 opt_idx = findoption(name);
3912 if (opt_idx < 0)
3913 return 0;
3914
3915 p = &(options[opt_idx]);
3916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003917 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003918 if (p->var == NULL)
3919 return 0;
3920
3921 if (p->flags & P_BOOL)
3922 r |= SOPT_BOOL;
3923 else if (p->flags & P_NUM)
3924 r |= SOPT_NUM;
3925 else if (p->flags & P_STRING)
3926 r |= SOPT_STRING;
3927
3928 if (p->indir == PV_NONE)
3929 {
3930 if (opt_type == SREQ_GLOBAL)
3931 r |= SOPT_GLOBAL;
3932 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003933 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003934 }
3935 else
3936 {
3937 if (p->indir & PV_BOTH)
3938 r |= SOPT_GLOBAL;
3939 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003940 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003941
3942 if (p->indir & PV_WIN)
3943 {
3944 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003945 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003946 else
3947 r |= SOPT_WIN;
3948 }
3949 else if (p->indir & PV_BUF)
3950 {
3951 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003952 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003953 else
3954 r |= SOPT_BUF;
3955 }
3956 }
3957
3958 if (stringval == NULL)
3959 return r;
3960
3961 if (opt_type == SREQ_GLOBAL)
3962 varp = p->var;
3963 else
3964 {
3965 if (opt_type == SREQ_BUF)
3966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003967 // Special case: 'modified' is b_changed, but we also want to
3968 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003969 if (p->indir == PV_MOD)
3970 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02003971 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003972 varp = NULL;
3973 }
3974#ifdef FEAT_CRYPT
3975 else if (p->indir == PV_KEY)
3976 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003977 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003978 *stringval = NULL;
3979 varp = NULL;
3980 }
3981#endif
3982 else
3983 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02003984 buf_T *save_curbuf = curbuf;
3985
3986 // only getting a pointer, no need to use aucmd_prepbuf()
3987 curbuf = (buf_T *)from;
3988 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003989 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02003990 curbuf = save_curbuf;
3991 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003992 }
3993 }
3994 else if (opt_type == SREQ_WIN)
3995 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02003996 win_T *save_curwin = curwin;
3997
3998 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003999 curbuf = curwin->w_buffer;
4000 varp = get_varp(p);
4001 curwin = save_curwin;
4002 curbuf = curwin->w_buffer;
4003 }
4004 if (varp == p->var)
4005 return (r | SOPT_UNSET);
4006 }
4007
4008 if (varp != NULL)
4009 {
4010 if (p->flags & P_STRING)
4011 *stringval = vim_strsave(*(char_u **)(varp));
4012 else if (p->flags & P_NUM)
4013 *numval = *(long *) varp;
4014 else
4015 *numval = *(int *)varp;
4016 }
4017
4018 return r;
4019}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004020
4021/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004022 * Iterate over options. First argument is a pointer to a pointer to a
4023 * structure inside options[] array, second is option type like in the above
4024 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004025 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004026 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004027 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004028 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004029 * first argument to NULL.
4030 *
4031 * Returns full option name for current option on each call.
4032 */
4033 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004034option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004035{
4036 struct vimoption *ret = NULL;
4037 do
4038 {
4039 if (*option == NULL)
4040 *option = (void *) options;
4041 else if (((struct vimoption *) (*option))->fullname == NULL)
4042 {
4043 *option = NULL;
4044 return NULL;
4045 }
4046 else
4047 *option = (void *) (((struct vimoption *) (*option)) + 1);
4048
4049 ret = ((struct vimoption *) (*option));
4050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004051 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004052 if (ret->var == NULL)
4053 {
4054 ret = NULL;
4055 continue;
4056 }
4057
4058 switch (opt_type)
4059 {
4060 case SREQ_GLOBAL:
4061 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4062 ret = NULL;
4063 break;
4064 case SREQ_BUF:
4065 if (!(ret->indir & PV_BUF))
4066 ret = NULL;
4067 break;
4068 case SREQ_WIN:
4069 if (!(ret->indir & PV_WIN))
4070 ret = NULL;
4071 break;
4072 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004073 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004074 return NULL;
4075 }
4076 }
4077 while (ret == NULL);
4078
4079 return (char_u *)ret->fullname;
4080}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004081#endif
4082
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004084 * Return the flags for the option at 'opt_idx'.
4085 */
4086 long_u
4087get_option_flags(int opt_idx)
4088{
4089 return options[opt_idx].flags;
4090}
4091
4092/*
4093 * Set a flag for the option at 'opt_idx'.
4094 */
4095 void
4096set_option_flag(int opt_idx, long_u flag)
4097{
4098 options[opt_idx].flags |= flag;
4099}
4100
4101/*
4102 * Clear a flag for the option at 'opt_idx'.
4103 */
4104 void
4105clear_option_flag(int opt_idx, long_u flag)
4106{
4107 options[opt_idx].flags &= ~flag;
4108}
4109
4110/*
4111 * Returns TRUE if the option at 'opt_idx' is a global option
4112 */
4113 int
4114is_global_option(int opt_idx)
4115{
4116 return options[opt_idx].indir == PV_NONE;
4117}
4118
4119/*
4120 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4121 * local value.
4122 */
4123 int
4124is_global_local_option(int opt_idx)
4125{
4126 return options[opt_idx].indir & PV_BOTH;
4127}
4128
4129/*
4130 * Returns TRUE if the option at 'opt_idx' is a window-local option
4131 */
4132 int
4133is_window_local_option(int opt_idx)
4134{
4135 return options[opt_idx].var == VAR_WIN;
4136}
4137
4138/*
4139 * Returns TRUE if the option at 'opt_idx' is a hidden option
4140 */
4141 int
4142is_hidden_option(int opt_idx)
4143{
4144 return options[opt_idx].var == NULL;
4145}
4146
4147#if defined(FEAT_CRYPT) || defined(PROTO)
4148/*
4149 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4150 */
4151 int
4152is_crypt_key_option(int opt_idx)
4153{
4154 return options[opt_idx].indir == PV_KEY;
4155}
4156#endif
4157
4158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 * Set the value of option "name".
4160 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004161 *
4162 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004164 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004165set_option_value(
4166 char_u *name,
4167 long number,
4168 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004169 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 int opt_idx;
4172 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004173 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174
4175 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004176 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004177 {
4178 int key;
4179
4180 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004181 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004182 {
4183 char_u key_name[2];
4184
4185 if (key < 0)
4186 {
4187 key_name[0] = KEY2TERMCAP0(key);
4188 key_name[1] = KEY2TERMCAP1(key);
4189 }
4190 else
4191 {
4192 key_name[0] = KS_KEY;
4193 key_name[1] = (key & 0xff);
4194 }
4195 add_termcode(key_name, string, FALSE);
4196 if (full_screen)
4197 ttest(FALSE);
4198 redraw_all_later(CLEAR);
4199 return NULL;
4200 }
4201
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004202 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 else
4205 {
4206 flags = options[opt_idx].flags;
4207#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004208 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004211 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004212 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004215 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004216 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 else
4218 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004219 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004220 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004222 if (number == 0 && string != NULL)
4223 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004224 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 // Either we are given a string or we are setting option
4227 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004228 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004229 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004230 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004232 // There's another character after zeros or the string
4233 // is empty. In both cases, we are trying to set a
4234 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004235 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004236 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004237 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004238
4239 }
4240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004242 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004243 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004245 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004246 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 }
4249 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004250 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251}
4252
4253/*
4254 * Get the terminal code for a terminal option.
4255 * Returns NULL when not found.
4256 */
4257 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004258get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 int opt_idx;
4261 char_u *varp;
4262
4263 if (tname[0] != 't' || tname[1] != '_' ||
4264 tname[2] == NUL || tname[3] == NUL)
4265 return NULL;
4266 if ((opt_idx = findoption(tname)) >= 0)
4267 {
4268 varp = get_varp(&(options[opt_idx]));
4269 if (varp != NULL)
4270 varp = *(char_u **)(varp);
4271 return varp;
4272 }
4273 return find_termcode(tname + 2);
4274}
4275
4276 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004277get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
4279 int i;
4280
4281 i = findoption((char_u *)"hl");
4282 if (i >= 0)
4283 return options[i].def_val[VI_DEFAULT];
4284 return (char_u *)NULL;
4285}
4286
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004287 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004288get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004289{
4290 int i;
4291
4292 i = findoption((char_u *)"enc");
4293 if (i >= 0)
4294 return options[i].def_val[VI_DEFAULT];
4295 return (char_u *)NULL;
4296}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004297
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298/*
4299 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004300 * When "has_lt" is true there is a '<' before "*arg_arg".
4301 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 */
4303 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004304find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004306 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004308 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /*
4311 * Don't use get_special_key_code() for t_xx, we don't want it to call
4312 * add_termcap_entry().
4313 */
4314 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4315 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004316 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004318 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 modifiers = 0;
Bram Moolenaar459fd782019-10-13 16:43:39 +02004320 key = find_special_key(&arg, &modifiers, TRUE, TRUE, FALSE, TRUE, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004321 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 key = 0;
4323 }
4324 return key;
4325}
4326
4327/*
4328 * if 'all' == 0: show changed options
4329 * if 'all' == 1: show all normal options
4330 * if 'all' == 2: show all terminal options
4331 */
4332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004333showoptions(
4334 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004335 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 struct vimoption *p;
4338 int col;
4339 int isterm;
4340 char_u *varp;
4341 struct vimoption **items;
4342 int item_count;
4343 int run;
4344 int row, rows;
4345 int cols;
4346 int i;
4347 int len;
4348
4349#define INC 20
4350#define GAP 3
4351
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004352 items = ALLOC_MULT(struct vimoption *, PARAM_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (items == NULL)
4354 return;
4355
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004356 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004358 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004360 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004362 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004364 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
4366 /*
4367 * do the loop two times:
4368 * 1. display the short items
4369 * 2. display the long items (only strings and numbers)
4370 */
4371 for (run = 1; run <= 2 && !got_int; ++run)
4372 {
4373 /*
4374 * collect the items in items[]
4375 */
4376 item_count = 0;
4377 for (p = &options[0]; p->fullname != NULL; p++)
4378 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004379 // apply :filter /pat/
4380 if (message_filtered((char_u *) p->fullname))
4381 continue;
4382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 varp = NULL;
4384 isterm = istermoption(p);
4385 if (opt_flags != 0)
4386 {
4387 if (p->indir != PV_NONE && !isterm)
4388 varp = get_varp_scope(p, opt_flags);
4389 }
4390 else
4391 varp = get_varp(p);
4392 if (varp != NULL
4393 && ((all == 2 && isterm)
4394 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004395 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
4397 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004398 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 else
4400 {
4401 option_value2string(p, opt_flags);
4402 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4403 }
4404 if ((len <= INC - GAP && run == 1) ||
4405 (len > INC - GAP && run == 2))
4406 items[item_count++] = p;
4407 }
4408 }
4409
4410 /*
4411 * display the items
4412 */
4413 if (run == 1)
4414 {
4415 cols = (Columns + GAP - 3) / INC;
4416 if (cols == 0)
4417 cols = 1;
4418 rows = (item_count + cols - 1) / cols;
4419 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004420 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 rows = item_count;
4422 for (row = 0; row < rows && !got_int; ++row)
4423 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004424 msg_putchar('\n'); // go to next line
4425 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 break;
4427 col = 0;
4428 for (i = row; i < item_count; i += rows)
4429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004430 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 showoneopt(items[i], opt_flags);
4432 col += INC;
4433 }
4434 out_flush();
4435 ui_breakcheck();
4436 }
4437 }
4438 vim_free(items);
4439}
4440
4441/*
4442 * Return TRUE if option "p" has its default value.
4443 */
4444 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004445optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
4447 int dvi;
4448
4449 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004450 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004451 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004453 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004455 // the cast to long is required for Manx C, long_i is
4456 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004457 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004458 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4460}
4461
4462/*
4463 * showoneopt: show the value of one option
4464 * must not be called with a hidden option!
4465 */
4466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004467showoneopt(
4468 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004469 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004471 char_u *varp;
4472 int save_silent = silent_mode;
4473
4474 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004475 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477 varp = get_varp_scope(p, opt_flags);
4478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004479 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4481 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004482 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004484 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004486 msg_puts(" ");
4487 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (!(p->flags & P_BOOL))
4489 {
4490 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004491 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 option_value2string(p, opt_flags);
4493 msg_outtrans(NameBuff);
4494 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004495
4496 silent_mode = save_silent;
4497 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498}
4499
4500/*
4501 * Write modified options as ":set" commands to a file.
4502 *
4503 * There are three values for "opt_flags":
4504 * OPT_GLOBAL: Write global option values and fresh values of
4505 * buffer-local options (used for start of a session
4506 * file).
4507 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4508 * curwin (used for a vimrc file).
4509 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4510 * and local values for window-local options of
4511 * curwin. Local values are also written when at the
4512 * default value, because a modeline or autocommand
4513 * may have set them when doing ":edit file" and the
4514 * user has set them back at the default or fresh
4515 * value.
4516 * When "local_only" is TRUE, don't write fresh
4517 * values, only local values (for ":mkview").
4518 * (fresh value = value used for a new buffer or window for a local option).
4519 *
4520 * Return FAIL on error, OK otherwise.
4521 */
4522 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004523makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004526 char_u *varp; // currently used value
4527 char_u *varp_fresh; // local value
4528 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 char *cmd;
4530 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004531 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 /*
4534 * The options that don't have a default (terminal name, columns, lines)
4535 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004536 * Do the loop over "options[]" twice: once for options with the
4537 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004539 for (pri = 1; pri >= 0; --pri)
4540 {
4541 for (p = &options[0]; !istermoption(p); p++)
4542 if (!(p->flags & P_NO_MKRC)
4543 && !istermoption(p)
4544 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004546 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4548 continue;
4549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004550 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4551 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4553 continue;
4554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004555 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004557 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 continue;
4559
4560 round = 2;
4561 if (p->indir != PV_NONE)
4562 {
4563 if (p->var == VAR_WIN)
4564 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004565 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (!(opt_flags & OPT_LOCAL))
4567 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004568 // When fresh value of window-local option is not at the
4569 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4571 {
4572 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004573 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
4575 round = 1;
4576 varp_local = varp;
4577 varp = varp_fresh;
4578 }
4579 }
4580 }
4581 }
4582
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004583 // Round 1: fresh value for window-local options.
4584 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 for ( ; round <= 2; varp = varp_local, ++round)
4586 {
4587 if (round == 1 || (opt_flags & OPT_GLOBAL))
4588 cmd = "set";
4589 else
4590 cmd = "setlocal";
4591
4592 if (p->flags & P_BOOL)
4593 {
4594 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4595 return FAIL;
4596 }
4597 else if (p->flags & P_NUM)
4598 {
4599 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4600 return FAIL;
4601 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004604 int do_endif = FALSE;
4605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004606 // Don't set 'syntax' and 'filetype' again if the value is
4607 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004608 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004609#if defined(FEAT_SYN_HL)
4610 p->indir == PV_SYN ||
4611#endif
4612 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4615 *(char_u **)(varp)) < 0
4616 || put_eol(fd) < 0)
4617 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004618 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
4620 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004621 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004623 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
4625 if (put_line(fd, "endif") == FAIL)
4626 return FAIL;
4627 }
4628 }
4629 }
4630 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 return OK;
4633}
4634
4635#if defined(FEAT_FOLDING) || defined(PROTO)
4636/*
4637 * Generate set commands for the local fold options only. Used when
4638 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4639 */
4640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004641makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004643 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004645 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 == FAIL
4647# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004648 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004650 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 == FAIL
4652 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4653 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4654 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4655 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4656 )
4657 return FAIL;
4658
4659 return OK;
4660}
4661#endif
4662
4663 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004664put_setstring(
4665 FILE *fd,
4666 char *cmd,
4667 char *name,
4668 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004669 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004672 char_u *buf = NULL;
4673 char_u *part = NULL;
4674 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4677 return FAIL;
4678 if (*valuep != NULL)
4679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004680 // Output 'pastetoggle' as key names. For other
4681 // options some characters have to be escaped with
4682 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (valuep == &p_pt)
4684 {
4685 s = *valuep;
4686 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004687 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 return FAIL;
4689 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004690 // expand the option value, replace $HOME by ~
4691 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004693 int size = (int)STRLEN(*valuep) + 1;
4694
4695 // replace home directory in the whole option value into "buf"
4696 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004697 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004698 goto fail;
4699 home_replace(NULL, *valuep, buf, size, FALSE);
4700
4701 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004702 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004703 // can be expanded when read back.
4704 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4705 && vim_strchr(*valuep, ',') != NULL)
4706 {
4707 part = alloc(size);
4708 if (part == NULL)
4709 goto fail;
4710
4711 // write line break to clear the option, e.g. ':set rtp='
4712 if (put_eol(fd) == FAIL)
4713 goto fail;
4714
4715 p = buf;
4716 while (*p != NUL)
4717 {
4718 // for each comma separated option part, append value to
4719 // the option, :set rtp+=value
4720 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4721 goto fail;
4722 (void)copy_option_part(&p, part, size, ",");
4723 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4724 goto fail;
4725 }
4726 vim_free(buf);
4727 vim_free(part);
4728 return OK;
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004731 {
4732 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004734 }
4735 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737 else if (put_escstr(fd, *valuep, 2) == FAIL)
4738 return FAIL;
4739 }
4740 if (put_eol(fd) < 0)
4741 return FAIL;
4742 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004743fail:
4744 vim_free(buf);
4745 vim_free(part);
4746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
4749 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004750put_setnum(
4751 FILE *fd,
4752 char *cmd,
4753 char *name,
4754 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 long wc;
4757
4758 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4759 return FAIL;
4760 if (wc_use_keyname((char_u *)valuep, &wc))
4761 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004762 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4764 return FAIL;
4765 }
4766 else if (fprintf(fd, "%ld", *valuep) < 0)
4767 return FAIL;
4768 if (put_eol(fd) < 0)
4769 return FAIL;
4770 return OK;
4771}
4772
4773 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004774put_setbool(
4775 FILE *fd,
4776 char *cmd,
4777 char *name,
4778 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004781 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4783 || put_eol(fd) < 0)
4784 return FAIL;
4785 return OK;
4786}
4787
4788/*
4789 * Clear all the terminal options.
4790 * If the option has been allocated, free the memory.
4791 * Terminal options are never hidden or indirect.
4792 */
4793 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004794clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 /*
4797 * Reset a few things before clearing the old options. This may cause
4798 * outputting a few things that the terminal doesn't understand, but the
4799 * screen will be cleared later, so this is OK.
4800 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004801 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#endif
4805#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004806 // When starting the GUI close the display opened for the clipboard.
4807 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (gui.starting)
4809 clear_xterm_clip();
4810#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004811 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004813 free_termoptions();
4814}
4815
4816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004817free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004818{
4819 struct vimoption *p;
4820
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004821 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (istermoption(p))
4823 {
4824 if (p->flags & P_ALLOCED)
4825 free_string_option(*(char_u **)(p->var));
4826 if (p->flags & P_DEF_ALLOCED)
4827 free_string_option(p->def_val[VI_DEFAULT]);
4828 *(char_u **)(p->var) = empty_option;
4829 p->def_val[VI_DEFAULT] = empty_option;
4830 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004831#ifdef FEAT_EVAL
4832 // remember where the option was cleared
4833 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 clear_termcodes();
4837}
4838
4839/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004840 * Free the string for one term option, if it was allocated.
4841 * Set the string to empty_option and clear allocated flag.
4842 * "var" points to the option value.
4843 */
4844 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004845free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004846{
4847 struct vimoption *p;
4848
4849 for (p = &options[0]; p->fullname != NULL; p++)
4850 if (p->var == var)
4851 {
4852 if (p->flags & P_ALLOCED)
4853 free_string_option(*(char_u **)(p->var));
4854 *(char_u **)(p->var) = empty_option;
4855 p->flags &= ~P_ALLOCED;
4856 break;
4857 }
4858}
4859
4860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 * Set the terminal option defaults to the current value.
4862 * Used after setting the terminal name.
4863 */
4864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004865set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
4867 struct vimoption *p;
4868
4869 for (p = &options[0]; p->fullname != NULL; p++)
4870 {
4871 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4872 {
4873 if (p->flags & P_DEF_ALLOCED)
4874 {
4875 free_string_option(p->def_val[VI_DEFAULT]);
4876 p->flags &= ~P_DEF_ALLOCED;
4877 }
4878 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4879 if (p->flags & P_ALLOCED)
4880 {
4881 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004882 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 }
4885 }
4886}
4887
4888/*
4889 * return TRUE if 'p' starts with 't_'
4890 */
4891 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004892istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
4894 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4895}
4896
Bram Moolenaardac13472019-09-16 21:06:21 +02004897/*
4898 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4899 */
4900 int
4901istermoption_idx(int opt_idx)
4902{
4903 return istermoption(&options[opt_idx]);
4904}
4905
Bram Moolenaar113e1072019-01-20 15:30:40 +01004906#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004908 * Unset local option value, similar to ":set opt<".
4909 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004911unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004912{
4913 struct vimoption *p;
4914 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004915 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004916
4917 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004918 if (opt_idx < 0)
4919 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004920 p = &(options[opt_idx]);
4921
4922 switch ((int)p->indir)
4923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004924 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004925 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004926 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004927 break;
4928 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004929 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004930 break;
4931 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004932 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004933 break;
4934 case PV_AR:
4935 buf->b_p_ar = -1;
4936 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004937 case PV_BKC:
4938 clear_string_option(&buf->b_p_bkc);
4939 buf->b_bkc_flags = 0;
4940 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004941 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004942 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004943 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01004944 case PV_TC:
4945 clear_string_option(&buf->b_p_tc);
4946 buf->b_tc_flags = 0;
4947 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01004948 case PV_SISO:
4949 curwin->w_p_siso = -1;
4950 break;
4951 case PV_SO:
4952 curwin->w_p_so = -1;
4953 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004954#ifdef FEAT_FIND_ID
4955 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004956 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004957 break;
4958 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004959 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004960 break;
4961#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004962 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004963 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004964 break;
4965 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004966 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004967 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01004968 case PV_FP:
4969 clear_string_option(&buf->b_p_fp);
4970 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004971#ifdef FEAT_QUICKFIX
4972 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004973 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004974 break;
4975 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004976 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004977 break;
4978 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004979 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004980 break;
4981#endif
4982#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
4983 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004984 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004985 break;
4986#endif
4987#if defined(FEAT_CRYPT)
4988 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004989 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004990 break;
4991#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01004992#ifdef FEAT_LINEBREAK
4993 case PV_SBR:
4994 clear_string_option(&((win_T *)from)->w_p_sbr);
4995 break;
4996#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004997#ifdef FEAT_STL_OPT
4998 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004999 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005000 break;
5001#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005002 case PV_UL:
5003 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5004 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005005#ifdef FEAT_LISP
5006 case PV_LW:
5007 clear_string_option(&buf->b_p_lw);
5008 break;
5009#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005010 case PV_MENC:
5011 clear_string_option(&buf->b_p_menc);
5012 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005013 }
5014}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005015#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005016
5017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 * Get pointer to option variable, depending on local or global scope.
5019 */
5020 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005021get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
5023 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5024 {
5025 if (p->var == VAR_WIN)
5026 return (char_u *)GLOBAL_WO(get_varp(p));
5027 return p->var;
5028 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005029 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 switch ((int)p->indir)
5032 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005033 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005035 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5036 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5037 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005039 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5040 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5041 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005042 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005043 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005044 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005045 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5046 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005048 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5049 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005051 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5052 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005053#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5054 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5055#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005056#if defined(FEAT_CRYPT)
5057 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5058#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005059#ifdef FEAT_LINEBREAK
5060 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5061#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005062#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005063 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005064#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005065 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005066#ifdef FEAT_LISP
5067 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5068#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005069 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005070 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005072 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 return get_varp(p);
5075}
5076
5077/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005078 * Get pointer to option variable at 'opt_idx', depending on local or global
5079 * scope.
5080 */
5081 char_u *
5082get_option_varp_scope(int opt_idx, int opt_flags)
5083{
5084 return get_varp_scope(&(options[opt_idx]), opt_flags);
5085}
5086
5087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 * Get pointer to option variable.
5089 */
5090 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005091get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005093 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (p->var == NULL)
5095 return NULL;
5096
5097 switch ((int)p->indir)
5098 {
5099 case PV_NONE: return p->var;
5100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005101 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005102 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005104 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005106 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005108 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005110 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005112 case PV_TC: return *curbuf->b_p_tc != NUL
5113 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005114 case PV_BKC: return *curbuf->b_p_bkc != NUL
5115 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005116 case PV_SISO: return curwin->w_p_siso >= 0
5117 ? (char_u *)&(curwin->w_p_siso) : p->var;
5118 case PV_SO: return curwin->w_p_so >= 0
5119 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005121 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005123 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5125#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005126 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005128 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005130 case PV_FP: return *curbuf->b_p_fp != NUL
5131 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005133 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005135 case PV_GP: return *curbuf->b_p_gp != NUL
5136 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5137 case PV_MP: return *curbuf->b_p_mp != NUL
5138 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005140#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5141 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5142 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5143#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005144#if defined(FEAT_CRYPT)
5145 case PV_CM: return *curbuf->b_p_cm != NUL
5146 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5147#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005148#ifdef FEAT_LINEBREAK
5149 case PV_SBR: return *curwin->w_p_sbr != NUL
5150 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5151#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005152#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005153 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005154 ? (char_u *)&(curwin->w_p_stl) : p->var;
5155#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005156 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5157 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005158#ifdef FEAT_LISP
5159 case PV_LW: return *curbuf->b_p_lw != NUL
5160 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5161#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005162 case PV_MENC: return *curbuf->b_p_menc != NUL
5163 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#ifdef FEAT_ARABIC
5165 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5166#endif
5167 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005168#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005169 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5170#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005171#ifdef FEAT_SYN_HL
5172 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5173 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005174 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005175 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef FEAT_DIFF
5178 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5179#endif
5180#ifdef FEAT_FOLDING
5181 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5182 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5183 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5184 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5185 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5186 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5187 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5188# ifdef FEAT_EVAL
5189 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5190 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5191# endif
5192 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5193#endif
5194 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005195 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005196#ifdef FEAT_LINEBREAK
5197 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005200 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005201#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5203#endif
5204#ifdef FEAT_RIGHTLEFT
5205 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5206 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5207#endif
5208 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5209 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5210#ifdef FEAT_LINEBREAK
5211 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005212 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5213 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005215 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005217 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005218#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005219 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5220 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5221#endif
5222#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005223 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5224 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5225 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
5228 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5229 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5232 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5234 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5235#ifdef FEAT_CINDENT
5236 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5237 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5238 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5239#endif
5240#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5241 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#ifdef FEAT_FOLDING
5245 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005248#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005249 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005251#ifdef FEAT_COMPL_FUNC
5252 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005253 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005254#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005255#ifdef FEAT_EVAL
5256 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005259 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005265 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5267 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5268 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5269 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5270#ifdef FEAT_FIND_ID
5271# ifdef FEAT_EVAL
5272 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5273# endif
5274#endif
5275#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5276 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5277 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5278#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005279#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005280 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#ifdef FEAT_CRYPT
5283 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5284#endif
5285#ifdef FEAT_LISP
5286 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5287#endif
5288 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5289 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5290 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5291 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5292 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005294#ifdef FEAT_TEXTOBJ
5295 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5298#ifdef FEAT_SMARTINDENT
5299 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5303#ifdef FEAT_SEARCHPATH
5304 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5305#endif
5306 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5307#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005308 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005309 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5310#endif
5311#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005312 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5313 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5314 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#endif
5316 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5317 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5318 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5319 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005320#ifdef FEAT_PERSISTENT_UNDO
5321 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5324#ifdef FEAT_KEYMAP
5325 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5326#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005327#ifdef FEAT_SIGNS
5328 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5329#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005330#ifdef FEAT_VARTABS
5331 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5332 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5333#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005334 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005336 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 return (char_u *)&(curbuf->b_p_wm);
5338}
5339
5340/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005341 * Return a pointer to the variable for option at 'opt_idx'
5342 */
5343 char_u *
5344get_option_var(int opt_idx)
5345{
5346 return options[opt_idx].var;
5347}
5348
5349/*
5350 * Return the full name of the option at 'opt_idx'
5351 */
5352 char_u *
5353get_option_fullname(int opt_idx)
5354{
5355 return (char_u *)options[opt_idx].fullname;
5356}
5357
5358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 * Get the value of 'equalprg', either the buffer-local one or the global one.
5360 */
5361 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005362get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
5364 if (*curbuf->b_p_ep == NUL)
5365 return p_ep;
5366 return curbuf->b_p_ep;
5367}
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369/*
5370 * Copy options from one window to another.
5371 * Used when splitting a window.
5372 */
5373 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005374win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5377 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005378 after_copy_winopt(wp_to);
5379}
5380
5381/*
5382 * After copying window options: update variables depending on options.
5383 */
5384 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005385after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005386{
5387#ifdef FEAT_LINEBREAK
5388 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005389#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005390#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005391 fill_culopt_flags(NULL, wp);
5392 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395
5396/*
5397 * Copy the options from one winopt_T to another.
5398 * Doesn't free the old option values in "to", use clear_winopt() for that.
5399 * The 'scroll' option is not copied, because it depends on the window height.
5400 * The 'previewwindow' option is reset, there can be only one preview window.
5401 */
5402 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005403copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405#ifdef FEAT_ARABIC
5406 to->wo_arab = from->wo_arab;
5407#endif
5408 to->wo_list = from->wo_list;
5409 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005410 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005411#ifdef FEAT_LINEBREAK
5412 to->wo_nuw = from->wo_nuw;
5413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414#ifdef FEAT_RIGHTLEFT
5415 to->wo_rl = from->wo_rl;
5416 to->wo_rlc = vim_strsave(from->wo_rlc);
5417#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005418#ifdef FEAT_LINEBREAK
5419 to->wo_sbr = vim_strsave(from->wo_sbr);
5420#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005421#ifdef FEAT_STL_OPT
5422 to->wo_stl = vim_strsave(from->wo_stl);
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005425#ifdef FEAT_DIFF
5426 to->wo_wrap_save = from->wo_wrap_save;
5427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#ifdef FEAT_LINEBREAK
5429 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005430 to->wo_bri = from->wo_bri;
5431 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005433 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005435 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005436 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005437 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005438#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005439 to->wo_spell = from->wo_spell;
5440#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005441#ifdef FEAT_SYN_HL
5442 to->wo_cuc = from->wo_cuc;
5443 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005444 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005445 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447#ifdef FEAT_DIFF
5448 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005449 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005451#ifdef FEAT_CONCEAL
5452 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005453 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005454#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005455#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005456 to->wo_twk = vim_strsave(from->wo_twk);
5457 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459#ifdef FEAT_FOLDING
5460 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005461 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005463 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 to->wo_fdi = vim_strsave(from->wo_fdi);
5465 to->wo_fml = from->wo_fml;
5466 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005467 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005469 to->wo_fdm_save = from->wo_diff_saved
5470 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 to->wo_fdn = from->wo_fdn;
5472# ifdef FEAT_EVAL
5473 to->wo_fde = vim_strsave(from->wo_fde);
5474 to->wo_fdt = vim_strsave(from->wo_fdt);
5475# endif
5476 to->wo_fmr = vim_strsave(from->wo_fmr);
5477#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005478#ifdef FEAT_SIGNS
5479 to->wo_scl = vim_strsave(from->wo_scl);
5480#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005481
5482#ifdef FEAT_EVAL
5483 // Copy the script context so that we know where the value was last set.
5484 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5485 sizeof(to->wo_script_ctx));
5486#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005487 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488}
5489
5490/*
5491 * Check string options in a window for a NULL value.
5492 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005494check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 check_winopt(&win->w_onebuf_opt);
5497 check_winopt(&win->w_allbuf_opt);
5498}
5499
5500/*
5501 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5502 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506#ifdef FEAT_FOLDING
5507 check_string_option(&wop->wo_fdi);
5508 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005509 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510# ifdef FEAT_EVAL
5511 check_string_option(&wop->wo_fde);
5512 check_string_option(&wop->wo_fdt);
5513# endif
5514 check_string_option(&wop->wo_fmr);
5515#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005516#ifdef FEAT_SIGNS
5517 check_string_option(&wop->wo_scl);
5518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519#ifdef FEAT_RIGHTLEFT
5520 check_string_option(&wop->wo_rlc);
5521#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005522#ifdef FEAT_LINEBREAK
5523 check_string_option(&wop->wo_sbr);
5524#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005525#ifdef FEAT_STL_OPT
5526 check_string_option(&wop->wo_stl);
5527#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005528#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005529 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530 check_string_option(&wop->wo_cc);
5531#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005532#ifdef FEAT_CONCEAL
5533 check_string_option(&wop->wo_cocu);
5534#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005535#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005536 check_string_option(&wop->wo_twk);
5537 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005538#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005539#ifdef FEAT_LINEBREAK
5540 check_string_option(&wop->wo_briopt);
5541#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005542 check_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543}
5544
5545/*
5546 * Free the allocated memory inside a winopt_T.
5547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005549clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551#ifdef FEAT_FOLDING
5552 clear_string_option(&wop->wo_fdi);
5553 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005554 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555# ifdef FEAT_EVAL
5556 clear_string_option(&wop->wo_fde);
5557 clear_string_option(&wop->wo_fdt);
5558# endif
5559 clear_string_option(&wop->wo_fmr);
5560#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005561#ifdef FEAT_SIGNS
5562 clear_string_option(&wop->wo_scl);
5563#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005564#ifdef FEAT_LINEBREAK
5565 clear_string_option(&wop->wo_briopt);
5566#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005567 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568#ifdef FEAT_RIGHTLEFT
5569 clear_string_option(&wop->wo_rlc);
5570#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005571#ifdef FEAT_LINEBREAK
5572 clear_string_option(&wop->wo_sbr);
5573#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005574#ifdef FEAT_STL_OPT
5575 clear_string_option(&wop->wo_stl);
5576#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005577#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005578 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005579 clear_string_option(&wop->wo_cc);
5580#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005581#ifdef FEAT_CONCEAL
5582 clear_string_option(&wop->wo_cocu);
5583#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005584#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005585 clear_string_option(&wop->wo_twk);
5586 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588}
5589
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005590#ifdef FEAT_EVAL
5591// Index into the options table for a buffer-local option enum.
5592static int buf_opt_idx[BV_COUNT];
5593# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5594
5595/*
5596 * Initialize buf_opt_idx[] if not done already.
5597 */
5598 static void
5599init_buf_opt_idx(void)
5600{
5601 static int did_init_buf_opt_idx = FALSE;
5602 int i;
5603
5604 if (did_init_buf_opt_idx)
5605 return;
5606 did_init_buf_opt_idx = TRUE;
5607 for (i = 0; !istermoption_idx(i); i++)
5608 if (options[i].indir & PV_BUF)
5609 buf_opt_idx[options[i].indir & PV_MASK] = i;
5610}
5611#else
5612# define COPY_OPT_SCTX(buf, bv)
5613#endif
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615/*
5616 * Copy global option values to local options for one buffer.
5617 * Used when creating a new buffer and sometimes when entering a buffer.
5618 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005619 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5621 * appropriate.
5622 * BCO_NOHELP Don't copy the values to a help buffer.
5623 */
5624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005625buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int dont_do_help;
5630 int did_isk = FALSE;
5631
5632 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 * Skip this when the option defaults have not been set yet. Happens when
5634 * main() allocates the first buffer.
5635 */
5636 if (p_cpo != NULL)
5637 {
5638 /*
5639 * Always copy when entering and 'cpo' contains 'S'.
5640 * Don't copy when already initialized.
5641 * Don't copy when 'cpo' contains 's' and not entering.
5642 * 'S' BCO_ENTER initialized 's' should_copy
5643 * yes yes X X TRUE
5644 * yes no yes X FALSE
5645 * no X yes X FALSE
5646 * X no no yes FALSE
5647 * X no no no TRUE
5648 * no yes no X TRUE
5649 */
5650 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5651 && (buf->b_p_initialized
5652 || (!(flags & BCO_ENTER)
5653 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5654 should_copy = FALSE;
5655
5656 if (should_copy || (flags & BCO_ALWAYS))
5657 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005658#ifdef FEAT_EVAL
Bram Moolenaar7eed9642019-10-19 20:57:28 +02005659 vim_memset(buf->b_p_script_ctx, 0, sizeof(buf->b_p_script_ctx));
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005660 init_buf_opt_idx();
5661#endif
5662 // Don't copy the options specific to a help buffer when
5663 // BCO_NOHELP is given or the options were initialized already
5664 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5666 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005667 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 save_p_isk = buf->b_p_isk;
5670 buf->b_p_isk = NULL;
5671 }
5672 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005673 * Always free the allocated strings. If not already initialized,
5674 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 */
5676 if (!buf->b_p_initialized)
5677 {
5678 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005679 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005682 switch (*p_ffs)
5683 {
5684 case 'm':
5685 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5686 case 'd':
5687 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5688 case 'u':
5689 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5690 default:
5691 buf->b_p_ff = vim_strsave(p_ff);
5692 }
5693 if (buf->b_p_ff != NULL)
5694 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 buf->b_p_bh = empty_option;
5696 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698 else
5699 free_buf_options(buf, FALSE);
5700
5701 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005702 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 buf->b_p_ai_nopaste = p_ai_nopaste;
5704 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005705 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005707 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 buf->b_p_tw_nopaste = p_tw_nopaste;
5709 buf->b_p_tw_nobin = p_tw_nobin;
5710 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005711 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 buf->b_p_wm_nopaste = p_wm_nopaste;
5713 buf->b_p_wm_nobin = p_wm_nobin;
5714 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005715 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005716 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005717 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005718 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005719 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005721 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005723 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005725 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 buf->b_p_ml_nobin = p_ml_nobin;
5727 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005728 COPY_OPT_SCTX(buf, BV_INF);
5729 if (cmdmod.noswapfile)
5730 buf->b_p_swf = FALSE;
5731 else
5732 {
5733 buf->b_p_swf = p_swf;
5734 COPY_OPT_SCTX(buf, BV_INF);
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005737 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005738#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005739 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005740 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005742#ifdef FEAT_COMPL_FUNC
5743 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005744 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005745 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005746 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005747#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005748#ifdef FEAT_EVAL
5749 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005750 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005753 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005755#ifdef FEAT_VARTABS
5756 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005757 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005758 if (p_vsts && p_vsts != empty_option)
5759 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5760 else
5761 buf->b_p_vsts_array = 0;
5762 buf->b_p_vsts_nopaste = p_vsts_nopaste
5763 ? vim_strsave(p_vsts_nopaste) : NULL;
5764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005766 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005768 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_FOLDING
5770 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005771 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#endif
5773 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005774 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005775 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005776 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005777 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5778 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005780 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005782 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#ifdef FEAT_SMARTINDENT
5784 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005785 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#endif
5787 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005788 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#ifdef FEAT_CINDENT
5790 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005791 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005793 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005795 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005797 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005800 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5802 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005803 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804#endif
5805#ifdef FEAT_LISP
5806 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005807 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808#endif
5809#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005810 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005812 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005813 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005814 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005815#endif
5816#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005817 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005818 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005819 (void)compile_cap_prog(&buf->b_s);
5820 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005821 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005822 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005823 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#endif
5825#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5826 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005827 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005829 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005831 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005832#if defined(FEAT_EVAL)
5833 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005834 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836#ifdef FEAT_CRYPT
5837 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005838 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839#endif
5840#ifdef FEAT_SEARCHPATH
5841 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005842 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#endif
5844#ifdef FEAT_KEYMAP
5845 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005846 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 buf->b_kmap_state |= KEYMAP_INIT;
5848#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005849#ifdef FEAT_TERMINAL
5850 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005851 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005852#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005853 // This isn't really an option, but copying the langmap and IME
5854 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005856 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005858 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005860 // options that are normally global but also have a local value
5861 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005863 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005864 buf->b_p_bkc = empty_option;
5865 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866#ifdef FEAT_QUICKFIX
5867 buf->b_p_gp = empty_option;
5868 buf->b_p_mp = empty_option;
5869 buf->b_p_efm = empty_option;
5870#endif
5871 buf->b_p_ep = empty_option;
5872 buf->b_p_kp = empty_option;
5873 buf->b_p_path = empty_option;
5874 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005875 buf->b_p_tc = empty_option;
5876 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#ifdef FEAT_FIND_ID
5878 buf->b_p_def = empty_option;
5879 buf->b_p_inc = empty_option;
5880# ifdef FEAT_EVAL
5881 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005882 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883# endif
5884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 buf->b_p_dict = empty_option;
5886 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005887#ifdef FEAT_TEXTOBJ
5888 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005889 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005890#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005891#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5892 buf->b_p_bexpr = empty_option;
5893#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005894#if defined(FEAT_CRYPT)
5895 buf->b_p_cm = empty_option;
5896#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005897#ifdef FEAT_PERSISTENT_UNDO
5898 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005899 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005900#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005901#ifdef FEAT_LISP
5902 buf->b_p_lw = empty_option;
5903#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005904 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
5906 /*
5907 * Don't copy the options set by ex_help(), use the saved values,
5908 * when going from a help buffer to a non-help buffer.
5909 * Don't touch these at all when BCO_NOHELP is used and going from
5910 * or to a help buffer.
5911 */
5912 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005915#ifdef FEAT_VARTABS
5916 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5917 tabstop_set(p_vts, &buf->b_p_vts_array);
5918 else
5919 buf->b_p_vts_array = NULL;
5920#endif
5921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 else
5923 {
5924 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005925 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 did_isk = TRUE;
5927 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005928#ifdef FEAT_VARTABS
5929 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005930 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005931 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5932 tabstop_set(p_vts, &buf->b_p_vts_array);
5933 else
5934 buf->b_p_vts_array = NULL;
5935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 if (buf->b_p_bt[0] == 'h')
5938 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 }
5942 }
5943
5944 /*
5945 * When the options should be copied (ignoring BCO_ALWAYS), set the
5946 * flag that indicates that the options have been initialized.
5947 */
5948 if (should_copy)
5949 buf->b_p_initialized = TRUE;
5950 }
5951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005952 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (did_isk)
5954 (void)buf_init_chartab(buf, FALSE);
5955}
5956
5957/*
5958 * Reset the 'modifiable' option and its default value.
5959 */
5960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005961reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 int opt_idx;
5964
5965 curbuf->b_p_ma = FALSE;
5966 p_ma = FALSE;
5967 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005968 if (opt_idx >= 0)
5969 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970}
5971
5972/*
5973 * Set the global value for 'iminsert' to the local value.
5974 */
5975 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005976set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 p_iminsert = curbuf->b_p_iminsert;
5979}
5980
5981/*
5982 * Set the global value for 'imsearch' to the local value.
5983 */
5984 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005985set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 p_imsearch = curbuf->b_p_imsearch;
5988}
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990static int expand_option_idx = -1;
5991static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
5992static int expand_option_flags = 0;
5993
5994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005995set_context_in_set_cmd(
5996 expand_T *xp,
5997 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005998 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006001 long_u flags = 0; // init for GCC
6002 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 char_u *p;
6004 char_u *s;
6005 int is_term_option = FALSE;
6006 int key;
6007
6008 expand_option_flags = opt_flags;
6009
6010 xp->xp_context = EXPAND_SETTINGS;
6011 if (*arg == NUL)
6012 {
6013 xp->xp_pattern = arg;
6014 return;
6015 }
6016 p = arg + STRLEN(arg) - 1;
6017 if (*p == ' ' && *(p - 1) != '\\')
6018 {
6019 xp->xp_pattern = p + 1;
6020 return;
6021 }
6022 while (p > arg)
6023 {
6024 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006025 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (*p == ' ' || *p == ',')
6027 {
6028 while (s > arg && *(s - 1) == '\\')
6029 --s;
6030 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006031 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (*p == ' ' && ((p - s) & 1) == 0)
6033 {
6034 ++p;
6035 break;
6036 }
6037 --p;
6038 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006039 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 xp->xp_context = EXPAND_BOOL_SETTINGS;
6042 p += 2;
6043 }
6044 if (STRNCMP(p, "inv", 3) == 0)
6045 {
6046 xp->xp_context = EXPAND_BOOL_SETTINGS;
6047 p += 3;
6048 }
6049 xp->xp_pattern = arg = p;
6050 if (*arg == '<')
6051 {
6052 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006053 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 return;
6055 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006056 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 {
6058 xp->xp_context = EXPAND_NOTHING;
6059 return;
6060 }
6061 nextchar = *++p;
6062 is_term_option = TRUE;
6063 expand_option_name[2] = KEY2TERMCAP0(key);
6064 expand_option_name[3] = KEY2TERMCAP1(key);
6065 }
6066 else
6067 {
6068 if (p[0] == 't' && p[1] == '_')
6069 {
6070 p += 2;
6071 if (*p != NUL)
6072 ++p;
6073 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006074 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 nextchar = *++p;
6076 is_term_option = TRUE;
6077 expand_option_name[2] = p[-2];
6078 expand_option_name[3] = p[-1];
6079 }
6080 else
6081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006082 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6084 p++;
6085 if (*p == NUL)
6086 return;
6087 nextchar = *p;
6088 *p = NUL;
6089 opt_idx = findoption(arg);
6090 *p = nextchar;
6091 if (opt_idx == -1 || options[opt_idx].var == NULL)
6092 {
6093 xp->xp_context = EXPAND_NOTHING;
6094 return;
6095 }
6096 flags = options[opt_idx].flags;
6097 if (flags & P_BOOL)
6098 {
6099 xp->xp_context = EXPAND_NOTHING;
6100 return;
6101 }
6102 }
6103 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006104 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6106 {
6107 ++p;
6108 nextchar = '=';
6109 }
6110 if ((nextchar != '=' && nextchar != ':')
6111 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6112 {
6113 xp->xp_context = EXPAND_UNSUCCESSFUL;
6114 return;
6115 }
6116 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6117 {
6118 xp->xp_context = EXPAND_OLD_SETTING;
6119 if (is_term_option)
6120 expand_option_idx = -1;
6121 else
6122 expand_option_idx = opt_idx;
6123 xp->xp_pattern = p + 1;
6124 return;
6125 }
6126 xp->xp_context = EXPAND_NOTHING;
6127 if (is_term_option || (flags & P_NUM))
6128 return;
6129
6130 xp->xp_pattern = p + 1;
6131
6132 if (flags & P_EXPAND)
6133 {
6134 p = options[opt_idx].var;
6135 if (p == (char_u *)&p_bdir
6136 || p == (char_u *)&p_dir
6137 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006138 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 || p == (char_u *)&p_rtp
6140#ifdef FEAT_SEARCHPATH
6141 || p == (char_u *)&p_cdpath
6142#endif
6143#ifdef FEAT_SESSION
6144 || p == (char_u *)&p_vdir
6145#endif
6146 )
6147 {
6148 xp->xp_context = EXPAND_DIRECTORIES;
6149 if (p == (char_u *)&p_path
6150#ifdef FEAT_SEARCHPATH
6151 || p == (char_u *)&p_cdpath
6152#endif
6153 )
6154 xp->xp_backslash = XP_BS_THREE;
6155 else
6156 xp->xp_backslash = XP_BS_ONE;
6157 }
6158 else
6159 {
6160 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006161 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (p == (char_u *)&p_tags)
6163 xp->xp_backslash = XP_BS_THREE;
6164 else
6165 xp->xp_backslash = XP_BS_ONE;
6166 }
6167 }
6168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006169 // For an option that is a list of file names, find the start of the
6170 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006173 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (*p == ' ' || *p == ',')
6175 {
6176 s = p;
6177 while (s > xp->xp_pattern && *(s - 1) == '\\')
6178 --s;
6179 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6180 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6181 {
6182 xp->xp_pattern = p + 1;
6183 break;
6184 }
6185 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006186
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006187#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006189 if (options[opt_idx].var == (char_u *)&p_sps
6190 && STRNCMP(p, "file:", 5) == 0)
6191 {
6192 xp->xp_pattern = p + 5;
6193 break;
6194 }
6195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197
6198 return;
6199}
6200
6201 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006202ExpandSettings(
6203 expand_T *xp,
6204 regmatch_T *regmatch,
6205 int *num_file,
6206 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006208 int num_normal = 0; // Nr of matching non-term-code settings
6209 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 int opt_idx;
6211 int match;
6212 int count = 0;
6213 char_u *str;
6214 int loop;
6215 int is_term_opt;
6216 char_u name_buf[MAX_KEY_NAME_LEN];
6217 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006218 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006220 // do this loop twice:
6221 // loop == 0: count the number of matching options
6222 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 for (loop = 0; loop <= 1; ++loop)
6224 {
6225 regmatch->rm_ic = ic;
6226 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6227 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006228 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6229 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6231 {
6232 if (loop == 0)
6233 num_normal++;
6234 else
6235 (*file)[count++] = vim_strsave((char_u *)names[match]);
6236 }
6237 }
6238 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6239 opt_idx++)
6240 {
6241 if (options[opt_idx].var == NULL)
6242 continue;
6243 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6244 && !(options[opt_idx].flags & P_BOOL))
6245 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006246 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (is_term_opt && num_normal > 0)
6248 continue;
6249 match = FALSE;
6250 if (vim_regexec(regmatch, str, (colnr_T)0)
6251 || (options[opt_idx].shortname != NULL
6252 && vim_regexec(regmatch,
6253 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6254 match = TRUE;
6255 else if (is_term_opt)
6256 {
6257 name_buf[0] = '<';
6258 name_buf[1] = 't';
6259 name_buf[2] = '_';
6260 name_buf[3] = str[2];
6261 name_buf[4] = str[3];
6262 name_buf[5] = '>';
6263 name_buf[6] = NUL;
6264 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6265 {
6266 match = TRUE;
6267 str = name_buf;
6268 }
6269 }
6270 if (match)
6271 {
6272 if (loop == 0)
6273 {
6274 if (is_term_opt)
6275 num_term++;
6276 else
6277 num_normal++;
6278 }
6279 else
6280 (*file)[count++] = vim_strsave(str);
6281 }
6282 }
6283 /*
6284 * Check terminal key codes, these are not in the option table
6285 */
6286 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6287 {
6288 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6289 {
6290 if (!isprint(str[0]) || !isprint(str[1]))
6291 continue;
6292
6293 name_buf[0] = 't';
6294 name_buf[1] = '_';
6295 name_buf[2] = str[0];
6296 name_buf[3] = str[1];
6297 name_buf[4] = NUL;
6298
6299 match = FALSE;
6300 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6301 match = TRUE;
6302 else
6303 {
6304 name_buf[0] = '<';
6305 name_buf[1] = 't';
6306 name_buf[2] = '_';
6307 name_buf[3] = str[0];
6308 name_buf[4] = str[1];
6309 name_buf[5] = '>';
6310 name_buf[6] = NUL;
6311
6312 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6313 match = TRUE;
6314 }
6315 if (match)
6316 {
6317 if (loop == 0)
6318 num_term++;
6319 else
6320 (*file)[count++] = vim_strsave(name_buf);
6321 }
6322 }
6323
6324 /*
6325 * Check special key names.
6326 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006327 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6329 {
6330 name_buf[0] = '<';
6331 STRCPY(name_buf + 1, str);
6332 STRCAT(name_buf, ">");
6333
6334 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6335 {
6336 if (loop == 0)
6337 num_term++;
6338 else
6339 (*file)[count++] = vim_strsave(name_buf);
6340 }
6341 }
6342 }
6343 if (loop == 0)
6344 {
6345 if (num_normal > 0)
6346 *num_file = num_normal;
6347 else if (num_term > 0)
6348 *num_file = num_term;
6349 else
6350 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006351 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if (*file == NULL)
6353 {
6354 *file = (char_u **)"";
6355 return FAIL;
6356 }
6357 }
6358 }
6359 return OK;
6360}
6361
6362 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006363ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006365 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 char_u *buf;
6367
6368 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006369 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (*file == NULL)
6371 return FAIL;
6372
6373 /*
6374 * For a terminal key code expand_option_idx is < 0.
6375 */
6376 if (expand_option_idx < 0)
6377 {
6378 var = find_termcode(expand_option_name + 2);
6379 if (var == NULL)
6380 expand_option_idx = findoption(expand_option_name);
6381 }
6382
6383 if (expand_option_idx >= 0)
6384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006385 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 option_value2string(&options[expand_option_idx], expand_option_flags);
6387 var = NameBuff;
6388 }
6389 else if (var == NULL)
6390 var = (char_u *)"";
6391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006392 // A backslash is required before some characters. This is the reverse of
6393 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 buf = vim_strsave_escaped(var, escape_chars);
6395
6396 if (buf == NULL)
6397 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006398 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 return FAIL;
6400 }
6401
6402#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006403 // For MS-Windows et al. we don't double backslashes at the start and
6404 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006405 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (var[0] == '\\' && var[1] == '\\'
6407 && expand_option_idx >= 0
6408 && (options[expand_option_idx].flags & P_EXPAND)
6409 && vim_isfilec(var[2])
6410 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006411 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412#endif
6413
6414 *file[0] = buf;
6415 *num_file = 1;
6416 return OK;
6417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
6419/*
6420 * Get the value for the numeric or string option *opp in a nice format into
6421 * NameBuff[]. Must not be called with a hidden option!
6422 */
6423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006424option_value2string(
6425 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006426 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 char_u *varp;
6429
6430 varp = get_varp_scope(opp, opt_flags);
6431
6432 if (opp->flags & P_NUM)
6433 {
6434 long wc = 0;
6435
6436 if (wc_use_keyname(varp, &wc))
6437 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6438 else if (wc != 0)
6439 STRCPY(NameBuff, transchar((int)wc));
6440 else
6441 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6442 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006443 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
6445 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006446 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 NameBuff[0] = NUL;
6448#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006449 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006450 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 STRCPY(NameBuff, "*****");
6452#endif
6453 else if (opp->flags & P_EXPAND)
6454 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006455 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 else if ((char_u **)opp->var == &p_pt)
6457 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6458 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006459 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 }
6461}
6462
6463/*
6464 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6465 * printed as a keyname.
6466 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6467 */
6468 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006469wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6472 {
6473 *wcp = *(long *)varp;
6474 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6475 return TRUE;
6476 }
6477 return FALSE;
6478}
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480/*
6481 * Return TRUE if format option 'x' is in effect.
6482 * Take care of no formatting when 'paste' is set.
6483 */
6484 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006485has_format_option(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 if (p_paste)
6488 return FALSE;
6489 return (vim_strchr(curbuf->b_p_fo, x) != NULL);
6490}
6491
6492/*
6493 * Return TRUE if "x" is present in 'shortmess' option, or
6494 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6495 */
6496 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006497shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006499 return p_shm != NULL &&
6500 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 || (vim_strchr(p_shm, 'a') != NULL
6502 && vim_strchr((char_u *)SHM_A, x) != NULL));
6503}
6504
6505/*
6506 * paste_option_changed() - Called after p_paste was set or reset.
6507 */
6508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 static int old_p_paste = FALSE;
6512 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006513 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514#ifdef FEAT_CMDL_INFO
6515 static int save_ru = 0;
6516#endif
6517#ifdef FEAT_RIGHTLEFT
6518 static int save_ri = 0;
6519 static int save_hkmap = 0;
6520#endif
6521 buf_T *buf;
6522
6523 if (p_paste)
6524 {
6525 /*
6526 * Paste switched from off to on.
6527 * Save the current values, so they can be restored later.
6528 */
6529 if (!old_p_paste)
6530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006531 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006532 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 buf->b_p_tw_nopaste = buf->b_p_tw;
6535 buf->b_p_wm_nopaste = buf->b_p_wm;
6536 buf->b_p_sts_nopaste = buf->b_p_sts;
6537 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006538 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006539#ifdef FEAT_VARTABS
6540 if (buf->b_p_vsts_nopaste)
6541 vim_free(buf->b_p_vsts_nopaste);
6542 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6543 ? vim_strsave(buf->b_p_vsts) : NULL;
6544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006547 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006549 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550#ifdef FEAT_CMDL_INFO
6551 save_ru = p_ru;
6552#endif
6553#ifdef FEAT_RIGHTLEFT
6554 save_ri = p_ri;
6555 save_hkmap = p_hkmap;
6556#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006557 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006558 p_ai_nopaste = p_ai;
6559 p_et_nopaste = p_et;
6560 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 p_tw_nopaste = p_tw;
6562 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006563#ifdef FEAT_VARTABS
6564 if (p_vsts_nopaste)
6565 vim_free(p_vsts_nopaste);
6566 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
6569
6570 /*
6571 * Always set the option values, also when 'paste' is set when it is
6572 * already on.
6573 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006574 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006575 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006577 buf->b_p_tw = 0; // textwidth is 0
6578 buf->b_p_wm = 0; // wrapmargin is 0
6579 buf->b_p_sts = 0; // softtabstop is 0
6580 buf->b_p_ai = 0; // no auto-indent
6581 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006582#ifdef FEAT_VARTABS
6583 if (buf->b_p_vsts)
6584 free_string_option(buf->b_p_vsts);
6585 buf->b_p_vsts = empty_option;
6586 if (buf->b_p_vsts_array)
6587 vim_free(buf->b_p_vsts_array);
6588 buf->b_p_vsts_array = 0;
6589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 }
6591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006592 // set global options
6593 p_sm = 0; // no showmatch
6594 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006597 status_redraw_all(); // redraw to remove the ruler
6598 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599#endif
6600#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 p_ri = 0; // no reverse insert
6602 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006604 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 p_tw = 0;
6606 p_wm = 0;
6607 p_sts = 0;
6608 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006609#ifdef FEAT_VARTABS
6610 if (p_vsts)
6611 free_string_option(p_vsts);
6612 p_vsts = empty_option;
6613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615
6616 /*
6617 * Paste switched from on to off: Restore saved values.
6618 */
6619 else if (old_p_paste)
6620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006621 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006622 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {
6624 buf->b_p_tw = buf->b_p_tw_nopaste;
6625 buf->b_p_wm = buf->b_p_wm_nopaste;
6626 buf->b_p_sts = buf->b_p_sts_nopaste;
6627 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006628 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006629#ifdef FEAT_VARTABS
6630 if (buf->b_p_vsts)
6631 free_string_option(buf->b_p_vsts);
6632 buf->b_p_vsts = buf->b_p_vsts_nopaste
6633 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6634 if (buf->b_p_vsts_array)
6635 vim_free(buf->b_p_vsts_array);
6636 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6637 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6638 else
6639 buf->b_p_vsts_array = 0;
6640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 }
6642
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006643 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006645 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006648 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 p_ru = save_ru;
6650#endif
6651#ifdef FEAT_RIGHTLEFT
6652 p_ri = save_ri;
6653 p_hkmap = save_hkmap;
6654#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006655 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006656 p_ai = p_ai_nopaste;
6657 p_et = p_et_nopaste;
6658 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 p_tw = p_tw_nopaste;
6660 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006661#ifdef FEAT_VARTABS
6662 if (p_vsts)
6663 free_string_option(p_vsts);
6664 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
6667
6668 old_p_paste = p_paste;
6669}
6670
6671/*
6672 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6673 *
6674 * Reset 'compatible' and set the values for options that didn't get set yet
6675 * to the Vim defaults.
6676 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006677 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 */
6679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006680vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006682 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006683 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006684 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
6686 if (!option_was_set((char_u *)"cp"))
6687 {
6688 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006689 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6691 set_option_default(opt_idx, OPT_FREE, FALSE);
6692 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006693 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006695
6696 if (fname != NULL)
6697 {
6698 p = vim_getenv(envname, &dofree);
6699 if (p == NULL)
6700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006701 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006702 p = FullName_save(fname, FALSE);
6703 if (p != NULL)
6704 {
6705 vim_setenv(envname, p);
6706 vim_free(p);
6707 }
6708 }
6709 else if (dofree)
6710 vim_free(p);
6711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712}
6713
6714/*
6715 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6716 */
6717 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006718change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006720 int opt_idx;
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (p_cp != on)
6723 {
6724 p_cp = on;
6725 compatible_set();
6726 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006727 opt_idx = findoption((char_u *)"cp");
6728 if (opt_idx >= 0)
6729 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730}
6731
6732/*
6733 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006734 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 */
6736 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006737option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 int idx;
6740
6741 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006742 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 return FALSE;
6744 if (options[idx].flags & P_WAS_SET)
6745 return TRUE;
6746 return FALSE;
6747}
6748
6749/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006750 * Reset the flag indicating option "name" was set.
6751 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006752 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006753reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006754{
6755 int idx = findoption(name);
6756
6757 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006758 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006759 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006760 return OK;
6761 }
6762 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006763}
6764
6765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 * compatible_set() - Called when 'compatible' has been set or unset.
6767 *
6768 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6769 * flag) to a Vi compatible value.
6770 * When 'compatible' is unset: Set all options that have a different default
6771 * for Vim (without the P_VI_DEF flag) to that default.
6772 */
6773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006774compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
6776 int opt_idx;
6777
Bram Moolenaardac13472019-09-16 21:06:21 +02006778 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6780 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6781 set_option_default(opt_idx, OPT_FREE, p_cp);
6782 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006783 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784}
6785
Bram Moolenaardac13472019-09-16 21:06:21 +02006786#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788/*
6789 * fill_breakat_flags() -- called when 'breakat' changes value.
6790 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006792fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006794 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 int i;
6796
6797 for (i = 0; i < 256; i++)
6798 breakat_flags[i] = FALSE;
6799
6800 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006801 for (p = p_breakat; *p; p++)
6802 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804#endif
6805
6806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 * Check if backspacing over something is allowed.
6808 */
6809 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006810can_bs(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006811 int what) // BS_INDENT, BS_EOL or BS_START
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006813#ifdef FEAT_JOB_CHANNEL
6814 if (what == BS_START && bt_prompt(curbuf))
6815 return FALSE;
6816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 switch (*p_bs)
6818 {
6819 case '2': return TRUE;
6820 case '1': return (what != BS_START);
6821 case '0': return FALSE;
6822 }
6823 return vim_strchr(p_bs, what) != NULL;
6824}
6825
6826/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006827 * Return the effective 'scrolloff' value for the current window, using the
6828 * global value when appropriate.
6829 */
6830 long
6831get_scrolloff_value(void)
6832{
6833 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6834}
6835
6836/*
6837 * Return the effective 'sidescrolloff' value for the current window, using the
6838 * global value when appropriate.
6839 */
6840 long
6841get_sidescrolloff_value(void)
6842{
6843 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6844}
6845
6846/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006847 * Get the local or global value of 'backupcopy'.
6848 */
6849 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006850get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006851{
6852 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6853}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006854
Bram Moolenaaree857022019-11-09 23:26:40 +01006855#if defined(FEAT_LINEBREAK) || defined(PROTO)
6856/*
6857 * Get the local or global value of 'showbreak'.
6858 */
6859 char_u *
6860get_showbreak_value(win_T *win)
6861{
6862 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6863 return p_sbr;
6864 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6865 return empty_option;
6866 return win->w_p_sbr;
6867}
6868#endif
6869
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006870#if defined(FEAT_EVAL) || defined(PROTO)
6871/*
6872 * Get window or buffer local options.
6873 */
6874 dict_T *
6875get_winbuf_options(int bufopt)
6876{
6877 dict_T *d;
6878 int opt_idx;
6879
6880 d = dict_alloc();
6881 if (d == NULL)
6882 return NULL;
6883
Bram Moolenaardac13472019-09-16 21:06:21 +02006884 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006885 {
6886 struct vimoption *opt = &options[opt_idx];
6887
6888 if ((bufopt && (opt->indir & PV_BUF))
6889 || (!bufopt && (opt->indir & PV_WIN)))
6890 {
6891 char_u *varp = get_varp(opt);
6892
6893 if (varp != NULL)
6894 {
6895 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006896 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006897 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006898 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006899 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006900 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006901 }
6902 }
6903 }
6904
6905 return d;
6906}
6907#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006908
Bram Moolenaardac13472019-09-16 21:06:21 +02006909#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006910/*
6911 * This is called when 'culopt' is changed
6912 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006913 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006914fill_culopt_flags(char_u *val, win_T *wp)
6915{
6916 char_u *p;
6917 char_u culopt_flags_new = 0;
6918
6919 if (val == NULL)
6920 p = wp->w_p_culopt;
6921 else
6922 p = val;
6923 while (*p != NUL)
6924 {
6925 if (STRNCMP(p, "line", 4) == 0)
6926 {
6927 p += 4;
6928 culopt_flags_new |= CULOPT_LINE;
6929 }
6930 else if (STRNCMP(p, "both", 4) == 0)
6931 {
6932 p += 4;
6933 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
6934 }
6935 else if (STRNCMP(p, "number", 6) == 0)
6936 {
6937 p += 6;
6938 culopt_flags_new |= CULOPT_NBR;
6939 }
6940 else if (STRNCMP(p, "screenline", 10) == 0)
6941 {
6942 p += 10;
6943 culopt_flags_new |= CULOPT_SCRLINE;
6944 }
6945
6946 if (*p != ',' && *p != NUL)
6947 return FAIL;
6948 if (*p == ',')
6949 ++p;
6950 }
6951
6952 // Can't have both "line" and "screenline".
6953 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
6954 return FAIL;
6955 wp->w_p_culopt_flags = culopt_flags_new;
6956
6957 return OK;
6958}
6959#endif