blob: c279acf13e83f8b13baa57a42115617c118c5e28 [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 *term_bg_default(void);
Bram Moolenaar916a8182018-11-25 02:18:29 +010041static void did_set_option(int opt_idx, int opt_flags, int new_value, int value_checked);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010042static char_u *option_expand(int opt_idx, char_u *val);
43static void didset_options(void);
44static void didset_options2(void);
45static void check_string_option(char_u **pp);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000048#else
49# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
50#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010051static void set_string_option_global(int opt_idx, char_u **varp);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010052static char *did_set_string_option(int opt_idx, char_u **varp, int new_value_alloced, char_u *oldval, char *errbuf, int opt_flags, int *value_checked);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020053#ifdef FEAT_STL_OPT
54static char *check_stl_option(char_u *s);
55#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#ifdef FEAT_CLIPBOARD
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010057static char *check_clipboard_option(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000059#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020060static void set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000061#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010062static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
63static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void check_redraw(long_u flags);
65static int findoption(char_u *);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020066static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010067static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020068static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010070static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010071static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
72static int put_setbool(FILE *fd, char *cmd, char *name, int value);
73static int istermoption(struct vimoption *);
74static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
75static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020076static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static void option_value2string(struct vimoption *, int opt_flags);
78static void check_winopt(winopt_T *wop);
79static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static void paste_option_changed(void);
81static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#ifdef FEAT_LINEBREAK
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010083static void fill_breakat_flags(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010085static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
86static int check_opt_strings(char_u *val, char **values, int);
87static int check_opt_wim(void);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +020088#ifdef FEAT_LINEBREAK
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010089static int briopt_check(win_T *wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +020090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92/*
93 * Initialize the options, first part.
94 *
95 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010096 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
98 void
Bram Moolenaar07268702018-03-01 21:57:32 +010099set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100{
101 char_u *p;
102 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000103 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105#ifdef FEAT_LANGMAP
106 langmap_init();
107#endif
108
109 /* Be Vi compatible by default */
110 p_cp = TRUE;
111
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000112 /* Use POSIX compatibility when $VIM_POSIX is set. */
113 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000114 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000115 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +0200116 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000117 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 /*
120 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000121 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 */
Bram Moolenaar7c626922005-02-07 22:01:03 +0000123 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100124#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000125 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100126 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000128 )
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100129 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131#ifdef FEAT_WILDIGN
132 /*
133 * Set the default for 'backupskip' to include environment variables for
134 * temp files.
135 */
136 {
137# ifdef UNIX
138 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
139# else
140 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
141# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000142 int len;
143 garray_T ga;
144 int mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146 ga_init2(&ga, 1, 100);
147 for (n = 0; n < (long)(sizeof(names) / sizeof(char *)); ++n)
148 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000149 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# ifdef UNIX
151 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200152# ifdef MACOS_X
153 p = (char_u *)"/private/tmp";
154# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 else
158# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000159 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 if (p != NULL && *p != NUL)
161 {
162 /* First time count the NUL, otherwise count the ','. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000163 len = (int)STRLEN(p) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 if (ga_grow(&ga, len) == OK)
165 {
166 if (ga.ga_len > 0)
167 STRCAT(ga.ga_data, ",");
168 STRCAT(ga.ga_data, p);
169 add_pathsep(ga.ga_data);
170 STRCAT(ga.ga_data, "*");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 ga.ga_len += len;
172 }
173 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174 if (mustfree)
175 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 }
177 if (ga.ga_data != NULL)
178 {
179 set_string_default("bsk", ga.ga_data);
180 vim_free(ga.ga_data);
181 }
182 }
183#endif
184
185 /*
186 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
187 */
188 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000189 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000191#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
192 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
193#endif
194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#ifdef HAVE_AVAIL_MEM
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000196 /* Use amount of memory available at this moment. */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200197 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#else
199# ifdef HAVE_TOTAL_MEM
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000200 /* Use amount of memory available to Vim. */
Bram Moolenaar914572a2007-05-01 11:37:47 +0000201 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000203 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# endif
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000207 opt_idx = findoption((char_u *)"maxmem");
208 if (opt_idx >= 0)
209 {
210#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100211 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
212 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213#endif
214 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
215 }
216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 }
218
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#ifdef FEAT_SEARCHPATH
220 {
221 char_u *cdpath;
222 char_u *buf;
223 int i;
224 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000225 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
227 /* Initialize the 'cdpath' option's default value. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000228 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 if (cdpath != NULL)
230 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200231 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 if (buf != NULL)
233 {
234 buf[0] = ','; /* start with ",", current dir first */
235 j = 1;
236 for (i = 0; cdpath[i] != NUL; ++i)
237 {
238 if (vim_ispathlistsep(cdpath[i]))
239 buf[j++] = ',';
240 else
241 {
242 if (cdpath[i] == ' ' || cdpath[i] == ',')
243 buf[j++] = '\\';
244 buf[j++] = cdpath[i];
245 }
246 }
247 buf[j] = NUL;
248 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000249 if (opt_idx >= 0)
250 {
251 options[opt_idx].def_val[VI_DEFAULT] = buf;
252 options[opt_idx].flags |= P_DEF_ALLOCED;
253 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200254 else
255 vim_free(buf); /* cannot happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257 if (mustfree)
258 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 }
260 }
261#endif
262
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100263#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 /* Set print encoding on platforms that don't default to latin1 */
265 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100266# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 (char_u *)"cp1252"
268# else
269# ifdef VMS
270 (char_u *)"dec-mcs"
271# else
272# ifdef EBCDIC
273 (char_u *)"ebcdic-uk"
274# else
275# ifdef MAC
276 (char_u *)"mac-roman"
277# else /* HPUX */
278 (char_u *)"hp-roman8"
279# endif
280# endif
281# endif
282# endif
283 );
284#endif
285
286#ifdef FEAT_POSTSCRIPT
287 /* 'printexpr' must be allocated to be able to evaluate it. */
288 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100289# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000290 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291# else
292# ifdef VMS
293 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
294
295# else
296 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
297# endif
298# endif
299 );
300#endif
301
302 /*
303 * Set all the options (except the terminal options) to their default
304 * value. Also set the global value for local options.
305 */
306 set_options_default(0);
307
Bram Moolenaar07268702018-03-01 21:57:32 +0100308#ifdef CLEAN_RUNTIMEPATH
309 if (clean_arg)
310 {
311 opt_idx = findoption((char_u *)"runtimepath");
312 if (opt_idx >= 0)
313 {
314 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
315 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
316 }
317 opt_idx = findoption((char_u *)"packpath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 }
324#endif
325
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_GUI
327 if (found_reverse_arg)
328 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
329#endif
330
331 curbuf->b_p_initialized = TRUE;
332 curbuf->b_p_ar = -1; /* no local 'autoread' value */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100333 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 check_buf_options(curbuf);
335 check_win_options(curwin);
336 check_options();
337
338 /* Must be before option_expand(), because that one needs vim_isIDc() */
339 didset_options();
340
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000341#ifdef FEAT_SPELL
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200342 /* Use the current chartab for the generic chartab. This is not in
343 * didset_options() because it only depends on 'encoding'. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000344 init_spell_chartab();
345#endif
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 /*
348 * Expand environment variables and things like "~" for the defaults.
349 * If option_expand() returns non-NULL the variable is expanded. This can
350 * only happen for non-indirect options.
351 * Also set the default to the expanded value, so ":set" does not list
352 * them.
353 * Don't set the P_ALLOCED flag, because we don't want to free the
354 * default.
355 */
356 for (opt_idx = 0; !istermoption(&options[opt_idx]); opt_idx++)
357 {
358 if ((options[opt_idx].flags & P_GETTEXT)
359 && options[opt_idx].var != NULL)
360 p = (char_u *)_(*(char **)options[opt_idx].var);
361 else
362 p = option_expand(opt_idx, NULL);
363 if (p != NULL && (p = vim_strsave(p)) != NULL)
364 {
365 *(char_u **)options[opt_idx].var = p;
366 /* VIMEXP
367 * Defaults for all expanded options are currently the same for Vi
368 * and Vim. When this changes, add some code here! Also need to
369 * split P_DEF_ALLOCED in two.
370 */
371 if (options[opt_idx].flags & P_DEF_ALLOCED)
372 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
373 options[opt_idx].def_val[VI_DEFAULT] = p;
374 options[opt_idx].flags |= P_DEF_ALLOCED;
375 }
376 }
377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 save_file_ff(curbuf); /* Buffer is unchanged */
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#if defined(FEAT_ARABIC)
381 /* Detect use of mlterm.
382 * Mlterm is a terminal emulator akin to xterm that has some special
383 * abilities (bidi namely).
384 * NOTE: mlterm's author is being asked to 'set' a variable
385 * instead of an environment variable due to inheritance.
386 */
387 if (mch_getenv((char_u *)"MLTERM") != NULL)
388 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
389#endif
390
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200391 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Bram Moolenaar4f974752019-02-17 17:44:42 +0100393# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 /*
395 * If $LANG isn't set, try to get a good value for it. This makes the
396 * right language be used automatically. Don't do this for English.
397 */
398 if (mch_getenv((char_u *)"LANG") == NULL)
399 {
400 char buf[20];
401
402 /* Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
403 * LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
404 * only the first two. */
405 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
406 (LPTSTR)buf, 20);
407 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
408 {
409 /* There are a few exceptions (probably more) */
410 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
411 STRCPY(buf, "zh_TW");
412 else if (STRNICMP(buf, "chs", 3) == 0
413 || STRNICMP(buf, "zhc", 3) == 0)
414 STRCPY(buf, "zh_CN");
415 else if (STRNICMP(buf, "jp", 2) == 0)
416 STRCPY(buf, "ja");
417 else
418 buf[2] = NUL; /* truncate to two-letter code */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100419 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000422# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000423# ifdef MACOS_CONVERT
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000424 /* Moved to os_mac_conv.c to avoid dependency problems. */
425 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427# endif
428
429 /* enc_locale() will try to find the encoding of the current locale. */
430 p = enc_locale();
431 if (p != NULL)
432 {
433 char_u *save_enc;
434
435 /* Try setting 'encoding' and check if the value is valid.
436 * If not, go back to the default "latin1". */
437 save_enc = p_enc;
438 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000439 if (STRCMP(p_enc, "gb18030") == 0)
440 {
441 /* We don't support "gb18030", but "cp936" is a good substitute
442 * for practical purposes, thus use that. It's not an alias to
443 * still support conversion between gb18030 and utf-8. */
444 p_enc = vim_strsave((char_u *)"cp936");
445 vim_free(p);
446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 if (mb_init() == NULL)
448 {
449 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000450 if (opt_idx >= 0)
451 {
452 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
453 options[opt_idx].flags |= P_DEF_ALLOCED;
454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
Bram Moolenaard0573012017-10-28 21:11:06 +0200456#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100457 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000458 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000459 /* Adjust the default for 'isprint' and 'iskeyword' to match
460 * latin1. Also set the defaults for when 'nocompatible' is
461 * set. */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000462 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000463 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000464 set_string_option_direct((char_u *)"isk", -1,
465 ISK_LATIN1, OPT_FREE, SID_NONE);
466 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000467 if (opt_idx >= 0)
468 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000469 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000470 if (opt_idx >= 0)
471 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000472 (void)init_chartab();
473 }
474#endif
475
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200476#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 /* Win32 console: When GetACP() returns a different value from
478 * GetConsoleCP() set 'termencoding'. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200479 if (
480# ifdef VIMDLL
481 (!gui.in_use && !gui.starting) &&
482# endif
483 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {
485 char buf[50];
486
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100487 /* Win32 console: In ConPTY, GetConsoleCP() returns zero.
488 * Use an alternative value. */
489 if (GetConsoleCP() == 0)
490 sprintf(buf, "cp%ld", (long)GetACP());
491 else
492 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 p_tenc = vim_strsave((char_u *)buf);
494 if (p_tenc != NULL)
495 {
496 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000497 if (opt_idx >= 0)
498 {
499 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
500 options[opt_idx].flags |= P_DEF_ALLOCED;
501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 convert_setup(&input_conv, p_tenc, p_enc);
503 convert_setup(&output_conv, p_enc, p_tenc);
504 }
505 else
506 p_tenc = empty_option;
507 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100508#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100509#if defined(MSWIN)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000510 /* $HOME may have characters in active code page. */
511 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 }
514 else
515 {
516 vim_free(p_enc);
517 p_enc = save_enc;
518 }
519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
521#ifdef FEAT_MULTI_LANG
522 /* Set the default for 'helplang'. */
523 set_helplang_default(get_mess_lang());
524#endif
525}
526
527/*
528 * Set an option to its default value.
529 * This does not take care of side effects!
530 */
531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100532set_option_default(
533 int opt_idx,
534 int opt_flags, /* OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL */
535 int compatible) /* use Vi default value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 char_u *varp; /* pointer to variable for current option */
538 int dvi; /* index in def_val[] */
539 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000540 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
542
543 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
544 flags = options[opt_idx].flags;
Bram Moolenaar3638c682005-06-08 22:05:14 +0000545 if (varp != NULL) /* skip hidden option, nothing to do for it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
547 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
548 if (flags & P_STRING)
549 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200550 /* Use set_string_option_direct() for local options to handle
551 * freeing and allocating the value. */
552 if (options[opt_idx].indir != PV_NONE)
553 set_string_option_direct(NULL, opt_idx,
554 options[opt_idx].def_val[dvi], opt_flags, 0);
555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200557 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
558 free_string_option(*(char_u **)(varp));
559 *(char_u **)varp = options[opt_idx].def_val[dvi];
560 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
562 }
563 else if (flags & P_NUM)
564 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000565 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 win_comp_scroll(curwin);
567 else
568 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100569 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
570
571 if ((long *)varp == &curwin->w_p_so
572 || (long *)varp == &curwin->w_p_siso)
573 // 'scrolloff' and 'sidescrolloff' local values have a
574 // different default value than the global default.
575 *(long *)varp = -1;
576 else
577 *(long *)varp = def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 /* May also set global value for local option. */
579 if (both)
580 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100581 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583 }
584 else /* P_BOOL */
585 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000586 /* the cast to long is required for Manx C, long_i is needed for
587 * MSVC */
588 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000589#ifdef UNIX
590 /* 'modeline' defaults to off for root */
591 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
592 *(int *)varp = FALSE;
593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 /* May also set global value for local option. */
595 if (both)
596 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
597 *(int *)varp;
598 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000599
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000600 /* The default value is not insecure. */
601 flagsp = insecure_flag(opt_idx, opt_flags);
602 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
604
605#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200606 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#endif
608}
609
610/*
611 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200612 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 */
614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100615set_options_default(
616 int opt_flags) /* OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000620 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 for (i = 0; !istermoption(&options[i]); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200623 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200624 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100625 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200626# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200627 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200628 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200629# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100630 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 set_option_default(i, opt_flags, p_cp);
632
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 /* The 'scroll' option must be computed for all windows. */
Bram Moolenaarf740b292006-02-16 22:11:02 +0000634 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200636#ifdef FEAT_CINDENT
637 parse_cino(curbuf);
638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639}
640
641/*
642 * Set the Vi-default value of a string option.
643 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100644 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100646 static void
647set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 char_u *p;
650 int opt_idx;
651
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100652 if (escape && vim_strchr(val, ' ') != NULL)
653 p = vim_strsave_escaped(val, (char_u *)" ");
654 else
655 p = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (p != NULL) /* we don't want a NULL */
657 {
658 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000659 if (opt_idx >= 0)
660 {
661 if (options[opt_idx].flags & P_DEF_ALLOCED)
662 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
663 options[opt_idx].def_val[VI_DEFAULT] = p;
664 options[opt_idx].flags |= P_DEF_ALLOCED;
665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667}
668
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100669 void
670set_string_default(char *name, char_u *val)
671{
672 set_string_default_esc(name, val, FALSE);
673}
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675/*
676 * Set the Vi-default value of a number option.
677 * Used for 'lines' and 'columns'.
678 */
679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000682 int opt_idx;
683
684 opt_idx = findoption((char_u *)name);
685 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000686 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687}
688
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200689/*
690 * Set all window-local and buffer-local options to the Vim default.
691 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200692 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200693 */
694 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200695set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200696{
697 win_T *save_curwin = curwin;
698 int i;
699
700 curwin = wp;
701 curbuf = curwin->w_buffer;
702 block_autocmds();
703
704 for (i = 0; !istermoption(&options[i]); i++)
705 {
706 struct vimoption *p = &(options[i]);
707 char_u *varp = get_varp_scope(p, OPT_LOCAL);
708
709 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200710 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200711 && !(options[i].flags & P_NODEFAULT)
712 && !optval_default(p, varp, FALSE))
713 set_option_default(i, OPT_LOCAL, FALSE);
714 }
715
716 unblock_autocmds();
717 curwin = save_curwin;
718 curbuf = curwin->w_buffer;
719}
720
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000721#if defined(EXITFREE) || defined(PROTO)
722/*
723 * Free all options.
724 */
725 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100726free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000727{
728 int i;
729
730 for (i = 0; !istermoption(&options[i]); i++)
731 {
732 if (options[i].indir == PV_NONE)
733 {
734 /* global option: free value and default value. */
Bram Moolenaar67391142017-02-19 21:07:04 +0100735 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000736 free_string_option(*(char_u **)options[i].var);
737 if (options[i].flags & P_DEF_ALLOCED)
738 free_string_option(options[i].def_val[VI_DEFAULT]);
739 }
740 else if (options[i].var != VAR_WIN
741 && (options[i].flags & P_STRING))
742 /* buffer-local option: free global value */
743 free_string_option(*(char_u **)options[i].var);
744 }
745}
746#endif
747
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
750 * Initialize the options, part two: After getting Rows and Columns and
751 * setting 'term'.
752 */
753 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100754set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000756 int idx;
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100759 * 'scroll' defaults to half the window height. The stored default is zero,
760 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000762 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000763 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000764 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 comp_col();
766
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000767 /*
768 * 'window' is only for backwards compatibility with Vi.
769 * Default is Rows - 1.
770 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000771 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000772 p_window = Rows - 1;
773 set_number_default("window", Rows - 1);
774
Bram Moolenaarf740b292006-02-16 22:11:02 +0000775 /* For DOS console the default is always black. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100776#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000777 /*
778 * If 'background' wasn't set by the user, try guessing the value,
779 * depending on the terminal name. Only need to check for terminals
780 * with a dark background, that can handle color.
781 */
782 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000783 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
784 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000786 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaarf740b292006-02-16 22:11:02 +0000787 /* don't mark it as set, when starting the GUI it may be
788 * changed again */
789 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000792
793#ifdef CURSOR_SHAPE
794 parse_shape_opt(SHAPE_CURSOR); /* set cursor shapes from 'guicursor' */
795#endif
796#ifdef FEAT_MOUSESHAPE
797 parse_shape_opt(SHAPE_MOUSE); /* set mouse shapes from 'mouseshape' */
798#endif
799#ifdef FEAT_PRINTER
800 (void)parse_printoptions(); /* parse 'printoptions' default value */
801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802}
803
804/*
Bram Moolenaarf740b292006-02-16 22:11:02 +0000805 * Return "dark" or "light" depending on the kind of terminal.
806 * This is just guessing! Recognized are:
807 * "linux" Linux console
808 * "screen.linux" Linux console with screen
Bram Moolenaarc6da01a2017-09-07 22:37:36 +0200809 * "cygwin.*" Cygwin shell
810 * "putty.*" Putty program
Bram Moolenaarf740b292006-02-16 22:11:02 +0000811 * We also check the COLORFGBG environment variable, which is set by
812 * rxvt and derivatives. This variable contains either two or three
813 * values separated by semicolons; we want the last value in either
814 * case. If this value is 0-6 or 8, our background is dark.
815 */
816 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100817term_bg_default(void)
Bram Moolenaarf740b292006-02-16 22:11:02 +0000818{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100819#if defined(MSWIN)
Bram Moolenaarc6da01a2017-09-07 22:37:36 +0200820 /* DOS console is nearly always black */
Bram Moolenaarf740b292006-02-16 22:11:02 +0000821 return (char_u *)"dark";
822#else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000823 char_u *p;
824
Bram Moolenaarf740b292006-02-16 22:11:02 +0000825 if (STRCMP(T_NAME, "linux") == 0
826 || STRCMP(T_NAME, "screen.linux") == 0
Bram Moolenaarc6da01a2017-09-07 22:37:36 +0200827 || STRNCMP(T_NAME, "cygwin", 6) == 0
828 || STRNCMP(T_NAME, "putty", 5) == 0
Bram Moolenaarf740b292006-02-16 22:11:02 +0000829 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
830 && (p = vim_strrchr(p, ';')) != NULL
831 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
832 && p[2] == NUL))
833 return (char_u *)"dark";
834 return (char_u *)"light";
835#endif
836}
837
838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * Initialize the options, part three: After reading the .vimrc
840 */
841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100842set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100844#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845/*
846 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
847 * This is done after other initializations, where 'shell' might have been
848 * set, but only if they have not been set before.
849 */
850 char_u *p;
851 int idx_srr;
852 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100853# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 int idx_sp;
855 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000859 if (idx_srr < 0)
860 do_srr = FALSE;
861 else
862 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100863# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000865 if (idx_sp < 0)
866 do_sp = FALSE;
867 else
868 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100869# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200870 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (p != NULL)
872 {
873 /*
874 * Default for p_sp is "| tee", for p_srr is ">".
875 * For known shells it is changed here to include stderr.
876 */
877 if ( fnamecmp(p, "csh") == 0
878 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100879# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 || fnamecmp(p, "csh.exe") == 0
881 || fnamecmp(p, "tcsh.exe") == 0
882# endif
883 )
884 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100885# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (do_sp)
887 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100888# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100890# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100892# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
894 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (do_srr)
897 {
898 p_srr = (char_u *)">&";
899 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
900 }
901 }
902 else
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100903 /* Always use bourne shell style redirection if we reach this */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if ( fnamecmp(p, "sh") == 0
905 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200906 || fnamecmp(p, "mksh") == 0
907 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000909 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200911 || fnamecmp(p, "fish") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "cmd") == 0
914 || fnamecmp(p, "sh.exe") == 0
915 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200916 || fnamecmp(p, "mksh.exe") == 0
917 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000919 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 || fnamecmp(p, "bash.exe") == 0
921 || fnamecmp(p, "cmd.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (do_sp)
927 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100928# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100930# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
934 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (do_srr)
937 {
938 p_srr = (char_u *)">%s 2>&1";
939 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
940 }
941 }
942 vim_free(p);
943 }
944#endif
945
Bram Moolenaar4f974752019-02-17 17:44:42 +0100946#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100948 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
949 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * This is done after other initializations, where 'shell' might have been
951 * set, but only if they have not been set before. Default for p_shcf is
952 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100953 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000955 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
957 int idx3;
958
959 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000960 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 {
962 p_shcf = (char_u *)"-c";
963 options[idx3].def_val[VI_DEFAULT] = p_shcf;
964 }
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /* Somehow Win32 requires the quotes around the redirection too */
967 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000968 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
970 p_sxq = (char_u *)"\"";
971 options[idx3].def_val[VI_DEFAULT] = p_sxq;
972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100974 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
975 {
976 int idx3;
977
978 /*
979 * cmd.exe on Windows will strip the first and last double quote given
980 * on the command line, e.g. most of the time things like:
981 * cmd /c "my path/to/echo" "my args to echo"
982 * become:
983 * my path/to/echo" "my args to echo
984 * when executed.
985 *
Bram Moolenaar034b1152012-02-19 18:19:30 +0100986 * To avoid this, set shellxquote to surround the command in
987 * parenthesis. This appears to make most commands work, without
988 * breaking commands that worked previously, such as
989 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +0100990 */
Bram Moolenaara64ba222012-02-12 23:23:31 +0100991 idx3 = findoption((char_u *)"sxq");
992 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
993 {
Bram Moolenaar034b1152012-02-19 18:19:30 +0100994 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +0100995 options[idx3].def_val[VI_DEFAULT] = p_sxq;
996 }
997
Bram Moolenaara64ba222012-02-12 23:23:31 +0100998 idx3 = findoption((char_u *)"shcf");
999 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1000 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001001 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001002 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1003 }
1004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#endif
1006
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001007 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001008 {
1009 int idx_ffs = findoption((char_u *)"ffs");
1010
1011 /* Apply the first entry of 'fileformats' to the initial buffer. */
1012 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1013 set_fileformat(default_fileformat(), OPT_LOCAL);
1014 }
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_TITLE
1017 set_title_defaults();
1018#endif
1019}
1020
1021#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1022/*
1023 * When 'helplang' is still at its default value, set it to "lang".
1024 * Only the first two characters of "lang" are used.
1025 */
1026 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001027set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 int idx;
1030
1031 if (lang == NULL || STRLEN(lang) < 2) /* safety check */
1032 return;
1033 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001034 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (options[idx].flags & P_ALLOCED)
1037 free_string_option(p_hlg);
1038 p_hlg = vim_strsave(lang);
1039 if (p_hlg == NULL)
1040 p_hlg = empty_option;
1041 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001042 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001043 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001044 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1045 {
1046 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1047 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1048 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001049 // any C like setting, such as C.UTF-8, becomes "en"
1050 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1051 {
1052 p_hlg[0] = 'e';
1053 p_hlg[1] = 'n';
1054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 options[idx].flags |= P_ALLOCED;
1058 }
1059}
1060#endif
1061
1062#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001064gui_bg_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 if (gui_get_lightness(gui.back_pixel) < 127)
1067 return (char_u *)"dark";
1068 return (char_u *)"light";
1069}
1070
1071/*
1072 * Option initializations that can only be done after opening the GUI window.
1073 */
1074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075init_gui_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 /* Set the 'background' option according to the lightness of the
1078 * background color, unless the user has set it already. */
1079 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
1080 {
1081 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
1082 highlight_changed();
1083 }
1084}
1085#endif
1086
1087#ifdef FEAT_TITLE
1088/*
1089 * 'title' and 'icon' only default to true if they have not been set or reset
1090 * in .vimrc and we can read the old value.
1091 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1092 * they can be reset. This reduces startup time when using X on a remote
1093 * machine.
1094 */
1095 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001096set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098 int idx1;
1099 long val;
1100
1101 /*
1102 * If GUI is (going to be) used, we can always set the window title and
1103 * icon name. Saves a bit of time, because the X11 display server does
1104 * not need to be contacted.
1105 */
1106 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001107 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109#ifdef FEAT_GUI
1110 if (gui.starting || gui.in_use)
1111 val = TRUE;
1112 else
1113#endif
1114 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001115 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 p_title = val;
1117 }
1118 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001119 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121#ifdef FEAT_GUI
1122 if (gui.starting || gui.in_use)
1123 val = TRUE;
1124 else
1125#endif
1126 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001127 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 p_icon = val;
1129 }
1130}
1131#endif
1132
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001133#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001134/*
1135 * Trigger the OptionSet autocommand.
1136 * "opt_idx" is the index of the option being set.
1137 * "opt_flags" can be OPT_LOCAL etc.
1138 * "oldval" the old value
1139 * "oldval_l" the old local value (only non-NULL if global and local value
1140 * are set)
1141 * "oldval_g" the old global value (only non-NULL if global and local value
1142 * are set)
1143 * "newval" the new value
1144 */
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001145 static void
1146trigger_optionsset_string(
1147 int opt_idx,
1148 int opt_flags,
Bram Moolenaard7c96872019-06-15 17:12:48 +02001149 char_u *oldval,
1150 char_u *oldval_l,
1151 char_u *oldval_g,
1152 char_u *newval)
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001153{
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02001154 // Don't do this recursively.
1155 if (oldval != NULL && newval != NULL
1156 && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001157 {
1158 char_u buf_type[7];
1159
1160 sprintf((char *)buf_type, "%s",
1161 (opt_flags & OPT_LOCAL) ? "local" : "global");
1162 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
1163 set_vim_var_string(VV_OPTION_NEW, newval, -1);
1164 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001165 if (opt_flags & OPT_LOCAL)
1166 {
1167 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
1168 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
1169 }
1170 if (opt_flags & OPT_GLOBAL)
1171 {
1172 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
1173 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
1174 }
1175 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1176 {
1177 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
1178 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
1179 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
1180 }
1181 if (opt_flags & OPT_MODELINE)
1182 {
1183 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
1184 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
1185 }
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001186 apply_autocmds(EVENT_OPTIONSET,
1187 (char_u *)options[opt_idx].fullname, NULL, FALSE, NULL);
1188 reset_v_option_vars();
1189 }
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001190}
1191#endif
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193/*
1194 * Parse 'arg' for option settings.
1195 *
1196 * 'arg' may be IObuff, but only when no errors can be present and option
1197 * does not need to be expanded with option_expand().
1198 * "opt_flags":
1199 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001200 * OPT_GLOBAL for ":setglobal"
1201 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001203 * OPT_WINONLY to only set window-local options
1204 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 *
1206 * returns FAIL if an error is detected, OK otherwise
1207 */
1208 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001209do_set(
1210 char_u *arg, /* option string (may be written to!) */
1211 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212{
1213 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 char *errmsg;
1215 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *startarg;
1217 int prefix; /* 1: nothing, 0: "no", 2: "inv" in front of name */
1218 int nextchar; /* next non-white char after option name */
1219 int afterchar; /* character just after option name */
1220 int len;
1221 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001222 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 int key;
1224 long_u flags; /* flags for current option */
1225 char_u *varp = NULL; /* pointer to variable for current option */
1226 int did_show = FALSE; /* already showed one value */
1227 int adding; /* "opt+=arg" */
1228 int prepending; /* "opt^=arg" */
1229 int removing; /* "opt-=arg" */
1230 int cp_val = 0;
1231 char_u key_name[2];
1232
1233 if (*arg == NUL)
1234 {
1235 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001236 did_show = TRUE;
1237 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239
1240 while (*arg != NUL) /* loop to process all options */
1241 {
1242 errmsg = NULL;
1243 startarg = arg; /* remember for error message */
1244
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001245 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1246 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {
1248 /*
1249 * ":set all" show all options.
1250 * ":set all&" set all options to their default value.
1251 */
1252 arg += 3;
1253 if (*arg == '&')
1254 {
1255 ++arg;
1256 /* Only for :set command set global value of local options. */
1257 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001258 didset_options();
1259 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001260 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001265 did_show = TRUE;
1266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001268 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
1270 showoptions(2, opt_flags);
1271 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001272 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 arg += 7;
1274 }
1275 else
1276 {
1277 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001278 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
1280 prefix = 0;
1281 arg += 2;
1282 }
1283 else if (STRNCMP(arg, "inv", 3) == 0)
1284 {
1285 prefix = 2;
1286 arg += 3;
1287 }
1288
1289 /* find end of name */
1290 key = 0;
1291 if (*arg == '<')
1292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 opt_idx = -1;
1294 /* look out for <t_>;> */
1295 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1296 len = 5;
1297 else
1298 {
1299 len = 1;
1300 while (arg[len] != NUL && arg[len] != '>')
1301 ++len;
1302 }
1303 if (arg[len] != '>')
1304 {
1305 errmsg = e_invarg;
1306 goto skip;
1307 }
1308 arg[len] = NUL; /* put NUL after name */
1309 if (arg[1] == 't' && arg[2] == '_') /* could be term code */
1310 opt_idx = findoption(arg + 1);
1311 arg[len++] = '>'; /* restore '>' */
1312 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001313 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 else
1316 {
1317 len = 0;
1318 /*
1319 * The two characters after "t_" may not be alphanumeric.
1320 */
1321 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1322 len = 4;
1323 else
1324 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1325 ++len;
1326 nextchar = arg[len];
1327 arg[len] = NUL; /* put NUL after name */
1328 opt_idx = findoption(arg);
1329 arg[len] = nextchar; /* restore nextchar */
1330 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001331 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333
1334 /* remember character after option name */
1335 afterchar = arg[len];
1336
1337 /* skip white space, allow ":set ai ?" */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001338 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 ++len;
1340
1341 adding = FALSE;
1342 prepending = FALSE;
1343 removing = FALSE;
1344 if (arg[len] != NUL && arg[len + 1] == '=')
1345 {
1346 if (arg[len] == '+')
1347 {
1348 adding = TRUE; /* "+=" */
1349 ++len;
1350 }
1351 else if (arg[len] == '^')
1352 {
1353 prepending = TRUE; /* "^=" */
1354 ++len;
1355 }
1356 else if (arg[len] == '-')
1357 {
1358 removing = TRUE; /* "-=" */
1359 ++len;
1360 }
1361 }
1362 nextchar = arg[len];
1363
1364 if (opt_idx == -1 && key == 0) /* found a mismatch: skip */
1365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001366 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 goto skip;
1368 }
1369
1370 if (opt_idx >= 0)
1371 {
1372 if (options[opt_idx].var == NULL) /* hidden option: skip */
1373 {
1374 /* Only give an error message when requesting the value of
1375 * a hidden option, ignore setting it. */
1376 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1377 && (!(options[opt_idx].flags & P_BOOL)
1378 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001379 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 goto skip;
1381 }
1382
1383 flags = options[opt_idx].flags;
1384 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1385 }
1386 else
1387 {
1388 flags = P_STRING;
1389 if (key < 0)
1390 {
1391 key_name[0] = KEY2TERMCAP0(key);
1392 key_name[1] = KEY2TERMCAP1(key);
1393 }
1394 else
1395 {
1396 key_name[0] = KS_KEY;
1397 key_name[1] = (key & 0xff);
1398 }
1399 }
1400
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 /* Skip all options that are not window-local (used when showing
1402 * an already loaded buffer in a window). */
1403 if ((opt_flags & OPT_WINONLY)
1404 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1405 goto skip;
1406
Bram Moolenaara3227e22006-03-08 21:32:40 +00001407 /* Skip all options that are window-local (used for :vimgrep). */
1408 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1409 && options[opt_idx].var == VAR_WIN)
1410 goto skip;
1411
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001412 /* Disallow changing some options from modelines. */
1413 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001415 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001417 errmsg = _("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001418 goto skip;
1419 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001420 if ((flags & P_MLE) && !p_mle)
1421 {
1422 errmsg = _("E992: Not allowed in a modeline when 'modelineexpr' is off");
1423 goto skip;
1424 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001425#ifdef FEAT_DIFF
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001426 /* In diff mode some options are overruled. This avoids that
1427 * 'foldmethod' becomes "marker" instead of "diff" and that
1428 * "wrap" gets set. */
1429 if (curwin->w_p_diff
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001430 && opt_idx >= 0 /* shut up coverity warning */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001431 && (
1432#ifdef FEAT_FOLDING
1433 options[opt_idx].indir == PV_FDM ||
1434#endif
1435 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001436 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439
1440#ifdef HAVE_SANDBOX
1441 /* Disallow changing some options in the sandbox */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001442 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001444 errmsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 goto skip;
1446 }
1447#endif
1448
1449 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1450 {
1451 arg += len;
1452 cp_val = p_cp;
1453 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1454 {
1455 if (arg[3] == 'm') /* "opt&vim": set to Vim default */
1456 {
1457 cp_val = FALSE;
1458 arg += 3;
1459 }
1460 else /* "opt&vi": set to Vi default */
1461 {
1462 cp_val = TRUE;
1463 arg += 2;
1464 }
1465 }
1466 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001467 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
1469 errmsg = e_trailing;
1470 goto skip;
1471 }
1472 }
1473
1474 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001475 * allow '=' and ':' for hystorical reasons (MSDOS command.com
1476 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 */
1478 if (nextchar == '?'
1479 || (prefix == 1
1480 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1481 && !(flags & P_BOOL)))
1482 {
1483 /*
1484 * print value
1485 */
1486 if (did_show)
1487 msg_putchar('\n'); /* cursor below last one */
1488 else
1489 {
1490 gotocmdline(TRUE); /* cursor at status line */
1491 did_show = TRUE; /* remember that we did a line */
1492 }
1493 if (opt_idx >= 0)
1494 {
1495 showoneopt(&options[opt_idx], opt_flags);
1496#ifdef FEAT_EVAL
1497 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001498 {
1499 /* Mention where the option was last set. */
1500 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001501 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001502 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001503 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001504 (int)options[opt_idx].indir & PV_MASK]);
1505 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001506 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001507 (int)options[opt_idx].indir & PV_MASK]);
1508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#endif
1510 }
1511 else
1512 {
1513 char_u *p;
1514
1515 p = find_termcode(key_name);
1516 if (p == NULL)
1517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001518 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 goto skip;
1520 }
1521 else
1522 (void)show_one_termcode(key_name, p, TRUE);
1523 }
1524 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001525 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 errmsg = e_trailing;
1527 }
1528 else
1529 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001530 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001531 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (flags & P_BOOL) /* boolean */
1534 {
1535 if (nextchar == '=' || nextchar == ':')
1536 {
1537 errmsg = e_invarg;
1538 goto skip;
1539 }
1540
1541 /*
1542 * ":set opt!": invert
1543 * ":set opt&": reset to default value
1544 * ":set opt<": reset to global value
1545 */
1546 if (nextchar == '!')
1547 value = *(int *)(varp) ^ 1;
1548 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001549 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 ((flags & P_VI_DEF) || cp_val)
1551 ? VI_DEFAULT : VIM_DEFAULT];
1552 else if (nextchar == '<')
1553 {
1554 /* For 'autoread' -1 means to use global value. */
1555 if ((int *)varp == &curbuf->b_p_ar
1556 && opt_flags == OPT_LOCAL)
1557 value = -1;
1558 else
1559 value = *(int *)get_varp_scope(&(options[opt_idx]),
1560 OPT_GLOBAL);
1561 }
1562 else
1563 {
1564 /*
1565 * ":set invopt": invert
1566 * ":set opt" or ":set noopt": set or reset
1567 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001568 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 errmsg = e_trailing;
1571 goto skip;
1572 }
1573 if (prefix == 2) /* inv */
1574 value = *(int *)(varp) ^ 1;
1575 else
1576 value = prefix;
1577 }
1578
1579 errmsg = set_bool_option(opt_idx, varp, (int)value,
1580 opt_flags);
1581 }
1582 else /* numeric or string */
1583 {
1584 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1585 || prefix != 1)
1586 {
1587 errmsg = e_invarg;
1588 goto skip;
1589 }
1590
1591 if (flags & P_NUM) /* numeric */
1592 {
1593 /*
1594 * Different ways to set a number option:
1595 * & set to default value
1596 * < set to global value
1597 * <xx> accept special key codes for 'wildchar'
1598 * c accept any non-digit for 'wildchar'
1599 * [-]0-9 set number
1600 * other error
1601 */
1602 ++arg;
1603 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001604 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 ((flags & P_VI_DEF) || cp_val)
1606 ? VI_DEFAULT : VIM_DEFAULT];
1607 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001608 {
1609 /* For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1610 * use the global value. */
1611 if ((long *)varp == &curbuf->b_p_ul
1612 && opt_flags == OPT_LOCAL)
1613 value = NO_LOCAL_UNDOLEVEL;
1614 else
1615 value = *(long *)get_varp_scope(
1616 &(options[opt_idx]), OPT_GLOBAL);
1617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 else if (((long *)varp == &p_wc
1619 || (long *)varp == &p_wcm)
1620 && (*arg == '<'
1621 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001622 || (*arg != NUL
1623 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 && !VIM_ISDIGIT(*arg))))
1625 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001626 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (value == 0 && (long *)varp != &p_wcm)
1628 {
1629 errmsg = e_invarg;
1630 goto skip;
1631 }
1632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1634 {
Bram Moolenaar18400e62015-01-27 15:58:40 +01001635 /* Allow negative (for 'undolevels'), octal and
1636 * hex numbers. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001637 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001638 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001639 if (i == 0 || (arg[i] != NUL
1640 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001642 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 goto skip;
1644 }
1645 }
1646 else
1647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001648 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 goto skip;
1650 }
1651
1652 if (adding)
1653 value = *(long *)varp + value;
1654 if (prepending)
1655 value = *(long *)varp * value;
1656 if (removing)
1657 value = *(long *)varp - value;
1658 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001659 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 else if (opt_idx >= 0) /* string */
1662 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001663 char_u *save_arg = NULL;
1664 char_u *s = NULL;
1665 char_u *oldval = NULL; /* previous value if *varp */
1666 char_u *newval;
1667 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001668 char_u *origval_l = NULL;
1669 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001670#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001671 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001672 char_u *saved_origval_l = NULL;
1673 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001674 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001675#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001676 unsigned newlen;
1677 int comma;
1678 int bs;
1679 int new_value_alloced; /* new string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 was allocated */
1681
1682 /* When using ":set opt=val" for a global option
1683 * with a local value the local value will be
1684 * reset, use the global value here. */
1685 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001686 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 varp = options[opt_idx].var;
1688
1689 /* The old value is kept until we are sure that the
1690 * new value is valid. */
1691 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001692
Bram Moolenaard7c96872019-06-15 17:12:48 +02001693 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1694 {
1695 origval_l = *(char_u **)get_varp_scope(
1696 &(options[opt_idx]), OPT_LOCAL);
1697 origval_g = *(char_u **)get_varp_scope(
1698 &(options[opt_idx]), OPT_GLOBAL);
1699
1700 // A global-local string option might have an empty
1701 // option as value to indicate that the global
1702 // value should be used.
1703 if (((int)options[opt_idx].indir & PV_BOTH)
1704 && origval_l == empty_option)
1705 origval_l = origval_g;
1706 }
1707
1708 // When setting the local value of a global
1709 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001710 if (((int)options[opt_idx].indir & PV_BOTH)
1711 && (opt_flags & OPT_LOCAL))
1712 origval = *(char_u **)get_varp(
1713 &options[opt_idx]);
1714 else
1715 origval = oldval;
1716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (nextchar == '&') /* set to default val */
1718 {
1719 newval = options[opt_idx].def_val[
1720 ((flags & P_VI_DEF) || cp_val)
1721 ? VI_DEFAULT : VIM_DEFAULT];
1722 if ((char_u **)varp == &p_bg)
1723 {
1724 /* guess the value of 'background' */
1725#ifdef FEAT_GUI
1726 if (gui.in_use)
1727 newval = gui_bg_default();
1728 else
1729#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001730 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
1732
1733 /* expand environment variables and ~ (since the
1734 * default value was already expanded, only
1735 * required when an environment variable was set
1736 * later */
1737 if (newval == NULL)
1738 newval = empty_option;
1739 else
1740 {
1741 s = option_expand(opt_idx, newval);
1742 if (s == NULL)
1743 s = newval;
1744 newval = vim_strsave(s);
1745 }
1746 new_value_alloced = TRUE;
1747 }
1748 else if (nextchar == '<') /* set to global val */
1749 {
1750 newval = vim_strsave(*(char_u **)get_varp_scope(
1751 &(options[opt_idx]), OPT_GLOBAL));
1752 new_value_alloced = TRUE;
1753 }
1754 else
1755 {
1756 ++arg; /* jump to after the '=' or ':' */
1757
1758 /*
1759 * Set 'keywordprg' to ":help" if an empty
1760 * value was passed to :set by the user.
1761 * Misuse errbuf[] for the resulting string.
1762 */
1763 if (varp == (char_u *)&p_kp
1764 && (*arg == NUL || *arg == ' '))
1765 {
1766 STRCPY(errbuf, ":help");
1767 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001768 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001771 * Convert 'backspace' number to string, for
1772 * adding, prepending and removing string.
1773 */
1774 else if (varp == (char_u *)&p_bs
1775 && VIM_ISDIGIT(**(char_u **)varp))
1776 {
1777 i = getdigits((char_u **)varp);
1778 switch (i)
1779 {
1780 case 0:
1781 *(char_u **)varp = empty_option;
1782 break;
1783 case 1:
1784 *(char_u **)varp = vim_strsave(
1785 (char_u *)"indent,eol");
1786 break;
1787 case 2:
1788 *(char_u **)varp = vim_strsave(
1789 (char_u *)"indent,eol,start");
1790 break;
1791 }
1792 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001793 if (origval == oldval)
1794 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001795 if (origval_l == oldval)
1796 origval_l = *(char_u **)varp;
1797 if (origval_g == oldval)
1798 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001799 oldval = *(char_u **)varp;
1800 }
1801 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 * Convert 'whichwrap' number to string, for
1803 * backwards compatibility with Vim 3.0.
1804 * Misuse errbuf[] for the resulting string.
1805 */
1806 else if (varp == (char_u *)&p_ww
1807 && VIM_ISDIGIT(*arg))
1808 {
1809 *errbuf = NUL;
1810 i = getdigits(&arg);
1811 if (i & 1)
1812 STRCAT(errbuf, "b,");
1813 if (i & 2)
1814 STRCAT(errbuf, "s,");
1815 if (i & 4)
1816 STRCAT(errbuf, "h,l,");
1817 if (i & 8)
1818 STRCAT(errbuf, "<,>,");
1819 if (i & 16)
1820 STRCAT(errbuf, "[,],");
1821 if (*errbuf != NUL) /* remove trailing , */
1822 errbuf[STRLEN(errbuf) - 1] = NUL;
1823 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001824 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826 /*
1827 * Remove '>' before 'dir' and 'bdir', for
1828 * backwards compatibility with version 3.0
1829 */
1830 else if ( *arg == '>'
1831 && (varp == (char_u *)&p_dir
1832 || varp == (char_u *)&p_bdir))
1833 {
1834 ++arg;
1835 }
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * Copy the new string into allocated memory.
1839 * Can't use set_string_option_direct(), because
1840 * we need to remove the backslashes.
1841 */
1842 /* get a bit too much */
1843 newlen = (unsigned)STRLEN(arg) + 1;
1844 if (adding || prepending || removing)
1845 newlen += (unsigned)STRLEN(origval) + 1;
1846 newval = alloc(newlen);
1847 if (newval == NULL) /* out of mem, don't change */
1848 break;
1849 s = newval;
1850
1851 /*
1852 * Copy the string, skip over escaped chars.
1853 * For MS-DOS and WIN32 backslashes before normal
1854 * file name characters are not removed, and keep
1855 * backslash at start, for "\\machine\path", but
1856 * do remove it for "\\\\machine\\path".
1857 * The reverse is found in ExpandOldSetting().
1858 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001859 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 if (*arg == '\\' && arg[1] != NUL
1862#ifdef BACKSLASH_IN_FILENAME
1863 && !((flags & P_EXPAND)
1864 && vim_isfilec(arg[1])
1865 && (arg[1] != '\\'
1866 || (s == newval
1867 && arg[2] != '\\')))
1868#endif
1869 )
1870 ++arg; /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001872 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
1874 /* copy multibyte char */
1875 mch_memmove(s, arg, (size_t)i);
1876 arg += i;
1877 s += i;
1878 }
1879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 *s++ = *arg++;
1881 }
1882 *s = NUL;
1883
1884 /*
1885 * Expand environment variables and ~.
1886 * Don't do it when adding without inserting a
1887 * comma.
1888 */
1889 if (!(adding || prepending || removing)
1890 || (flags & P_COMMA))
1891 {
1892 s = option_expand(opt_idx, newval);
1893 if (s != NULL)
1894 {
1895 vim_free(newval);
1896 newlen = (unsigned)STRLEN(s) + 1;
1897 if (adding || prepending || removing)
1898 newlen += (unsigned)STRLEN(origval) + 1;
1899 newval = alloc(newlen);
1900 if (newval == NULL)
1901 break;
1902 STRCPY(newval, s);
1903 }
1904 }
1905
1906 /* locate newval[] in origval[] when removing it
1907 * and when adding to avoid duplicates */
1908 i = 0; /* init for GCC */
1909 if (removing || (flags & P_NODUP))
1910 {
1911 i = (int)STRLEN(newval);
1912 bs = 0;
1913 for (s = origval; *s; ++s)
1914 {
1915 if ((!(flags & P_COMMA)
1916 || s == origval
1917 || (s[-1] == ',' && !(bs & 1)))
1918 && STRNCMP(s, newval, i) == 0
1919 && (!(flags & P_COMMA)
1920 || s[i] == ','
1921 || s[i] == NUL))
1922 break;
Bram Moolenaar0b2f94d2011-03-22 14:35:05 +01001923 /* Count backslashes. Only a comma with an
Bram Moolenaar8f79acd2016-01-01 14:48:20 +01001924 * even number of backslashes or a single
1925 * backslash preceded by a comma before it
1926 * is recognized as a separator */
1927 if ((s > origval + 1
1928 && s[-1] == '\\'
1929 && s[-2] != ',')
1930 || (s == origval + 1
1931 && s[-1] == '\\'))
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 ++bs;
1934 else
1935 bs = 0;
1936 }
1937
1938 /* do not add if already there */
1939 if ((adding || prepending) && *s)
1940 {
1941 prepending = FALSE;
1942 adding = FALSE;
1943 STRCPY(newval, origval);
1944 }
1945 }
1946
1947 /* concatenate the two strings; add a ',' if
1948 * needed */
1949 if (adding || prepending)
1950 {
1951 comma = ((flags & P_COMMA) && *origval != NUL
1952 && *newval != NUL);
1953 if (adding)
1954 {
1955 i = (int)STRLEN(origval);
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001956 /* strip a trailing comma, would get 2 */
Bram Moolenaar17467472015-11-10 17:50:24 +01001957 if (comma && i > 1
1958 && (flags & P_ONECOMMA) == P_ONECOMMA
1959 && origval[i - 1] == ','
1960 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001961 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 mch_memmove(newval + i + comma, newval,
1963 STRLEN(newval) + 1);
1964 mch_memmove(newval, origval, (size_t)i);
1965 }
1966 else
1967 {
1968 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001969 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 if (comma)
1972 newval[i] = ',';
1973 }
1974
1975 /* Remove newval[] from origval[]. (Note: "i" has
1976 * been set above and is used here). */
1977 if (removing)
1978 {
1979 STRCPY(newval, origval);
1980 if (*s)
1981 {
1982 /* may need to remove a comma */
1983 if (flags & P_COMMA)
1984 {
1985 if (s == origval)
1986 {
1987 /* include comma after string */
1988 if (s[i] == ',')
1989 ++i;
1990 }
1991 else
1992 {
1993 /* include comma before string */
1994 --s;
1995 ++i;
1996 }
1997 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001998 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
2001
2002 if (flags & P_FLAGLIST)
2003 {
2004 /* Remove flags that appear twice. */
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002005 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002006 {
2007 /* if options have P_FLAGLIST and
2008 * P_ONECOMMA such as 'whichwrap' */
2009 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002011 if (*s != ',' && *(s + 1) == ','
2012 && vim_strchr(s + 2, *s) != NULL)
2013 {
2014 /* Remove the duplicated value and
2015 * the next comma. */
2016 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002017 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002020 else
2021 {
2022 if ((!(flags & P_COMMA) || *s != ',')
2023 && vim_strchr(s + 1, *s) != NULL)
2024 {
2025 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002026 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002027 }
2028 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002029 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032
2033 if (save_arg != NULL) /* number for 'whichwrap' */
2034 arg = save_arg;
2035 new_value_alloced = TRUE;
2036 }
2037
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002038 /*
2039 * Set the new value.
2040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 *(char_u **)(varp) = newval;
2042
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002043#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002044 if (!starting
2045# ifdef FEAT_CRYPT
2046 && options[opt_idx].indir != PV_KEY
2047# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002048 && origval != NULL && newval != NULL)
2049 {
Bram Moolenaar53744302015-07-17 17:38:22 +02002050 /* origval may be freed by
2051 * did_set_string_option(), make a copy. */
2052 saved_origval = vim_strsave(origval);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002053 /* newval (and varp) may become invalid if the
2054 * buffer is closed by autocommands. */
2055 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002056 if (origval_l != NULL)
2057 saved_origval_l = vim_strsave(origval_l);
2058 if (origval_g != NULL)
2059 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002060 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002061#endif
2062
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002063 {
2064 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002065 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002066
2067 // When an option is set in the sandbox, from a
2068 // modeline or in secure mode, then deal with side
2069 // effects in secure mode. Also when the value was
2070 // set with the P_INSECURE flag and is not
2071 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002072 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002073#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002074 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002075#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002076 || (!value_is_replaced && (*p & P_INSECURE)))
2077 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002078
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002079 // Handle side effects, and set the global value
2080 // for ":set" on local options. Note: when setting
2081 // 'syntax' or 'filetype' autocommands may be
2082 // triggered that can cause havoc.
2083 errmsg = did_set_string_option(
2084 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002085 new_value_alloced, oldval, errbuf,
2086 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002087
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002088 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002091#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002092 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002093 trigger_optionsset_string(
2094 opt_idx, opt_flags, saved_origval,
2095 saved_origval_l, saved_origval_g,
2096 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002097 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002098 vim_free(saved_origval_l);
2099 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002100 vim_free(saved_newval);
2101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 /* If error detected, print the error message. */
2103 if (errmsg != NULL)
2104 goto skip;
2105 }
2106 else /* key code option */
2107 {
2108 char_u *p;
2109
2110 if (nextchar == '&')
2111 {
2112 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002113 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
2115 else
2116 {
2117 ++arg; /* jump to after the '=' or ':' */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002118 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (*p == '\\' && p[1] != NUL)
2120 ++p;
2121 nextchar = *p;
2122 *p = NUL;
2123 add_termcode(key_name, arg, FALSE);
2124 *p = nextchar;
2125 }
2126 if (full_screen)
2127 ttest(FALSE);
2128 redraw_all_later(CLEAR);
2129 }
2130 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002133 did_set_option(
2134 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136
2137skip:
2138 /*
2139 * Advance to next argument.
2140 * - skip until a blank found, taking care of backslashes
2141 * - skip blanks
2142 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2143 */
2144 for (i = 0; i < 2 ; ++i)
2145 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002146 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (*arg++ == '\\' && *arg != NUL)
2148 ++arg;
2149 arg = skipwhite(arg);
2150 if (*arg != '=')
2151 break;
2152 }
2153 }
2154
2155 if (errmsg != NULL)
2156 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002157 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002158 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if (i + (arg - startarg) < IOSIZE)
2160 {
2161 /* append the argument with the error */
2162 STRCAT(IObuff, ": ");
2163 mch_memmove(IObuff + i, startarg, (arg - startarg));
2164 IObuff[i + (arg - startarg)] = NUL;
2165 }
2166 /* make sure all characters are printable */
2167 trans_characters(IObuff, IOSIZE);
2168
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002169 ++no_wait_return; // wait_return done later
2170 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 --no_wait_return;
2172
2173 return FAIL;
2174 }
2175
2176 arg = skipwhite(arg);
2177 }
2178
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002179theend:
2180 if (silent_mode && did_show)
2181 {
2182 /* After displaying option values in silent mode. */
2183 silent_mode = FALSE;
2184 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2185 msg_putchar('\n');
2186 cursor_on(); /* msg_start() switches it off */
2187 out_flush();
2188 silent_mode = TRUE;
2189 info_message = FALSE; /* use mch_msg(), not mch_errmsg() */
2190 }
2191
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return OK;
2193}
2194
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002195/*
2196 * Call this when an option has been given a new value through a user command.
2197 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2198 */
2199 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002200did_set_option(
2201 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002202 int opt_flags, // possibly with OPT_MODELINE
2203 int new_value, // value was replaced completely
2204 int value_checked) // value was checked to be safe, no need to set the
2205 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002206{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002207 long_u *p;
2208
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002209 options[opt_idx].flags |= P_WAS_SET;
2210
2211 /* When an option is set in the sandbox, from a modeline or in secure mode
2212 * set the P_INSECURE flag. Otherwise, if a new value is stored reset the
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002213 * flag. */
2214 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002215 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002216#ifdef HAVE_SANDBOX
2217 || sandbox != 0
2218#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002219 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002220 *p = *p | P_INSECURE;
2221 else if (new_value)
2222 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002223}
2224
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002225 static char *
2226illegal_char(char *errbuf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228 if (errbuf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002229 return "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 sprintf((char *)errbuf, _("E539: Illegal character <%s>"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002231 (char *)transchar(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return errbuf;
2233}
2234
2235/*
2236 * Convert a key name or string into a key value.
2237 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002238 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002240 int
2241string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
2243 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002244 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (*arg == '^')
2246 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002247 if (multi_byte)
2248 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 return *arg;
2250}
2251
2252#ifdef FEAT_CMDWIN
2253/*
2254 * Check value of 'cedit' and set cedit_key.
2255 * Returns NULL if value is OK, error message otherwise.
2256 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002257 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002258check_cedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 int n;
2261
2262 if (*p_cedit == NUL)
2263 cedit_key = -1;
2264 else
2265 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002266 n = string_to_key(p_cedit, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (vim_isprintc(n))
2268 return e_invarg;
2269 cedit_key = n;
2270 }
2271 return NULL;
2272}
2273#endif
2274
2275#ifdef FEAT_TITLE
2276/*
2277 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2278 * maketitle() to create and display it.
2279 * When switching the title or icon off, call mch_restore_title() to get
2280 * the old value back.
2281 */
2282 static void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002283did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 if (starting != NO_SCREEN
2286#ifdef FEAT_GUI
2287 && !gui.starting
2288#endif
2289 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291}
2292#endif
2293
2294/*
2295 * set_options_bin - called when 'bin' changes value.
2296 */
2297 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002298set_options_bin(
2299 int oldval,
2300 int newval,
2301 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 /*
2304 * The option values that are changed when 'bin' changes are
2305 * copied when 'bin is set and restored when 'bin' is reset.
2306 */
2307 if (newval)
2308 {
2309 if (!oldval) /* switched on */
2310 {
2311 if (!(opt_flags & OPT_GLOBAL))
2312 {
2313 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2314 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2315 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2316 curbuf->b_p_et_nobin = curbuf->b_p_et;
2317 }
2318 if (!(opt_flags & OPT_LOCAL))
2319 {
2320 p_tw_nobin = p_tw;
2321 p_wm_nobin = p_wm;
2322 p_ml_nobin = p_ml;
2323 p_et_nobin = p_et;
2324 }
2325 }
2326
2327 if (!(opt_flags & OPT_GLOBAL))
2328 {
2329 curbuf->b_p_tw = 0; /* no automatic line wrap */
2330 curbuf->b_p_wm = 0; /* no automatic line wrap */
2331 curbuf->b_p_ml = 0; /* no modelines */
2332 curbuf->b_p_et = 0; /* no expandtab */
2333 }
2334 if (!(opt_flags & OPT_LOCAL))
2335 {
2336 p_tw = 0;
2337 p_wm = 0;
2338 p_ml = FALSE;
2339 p_et = FALSE;
2340 p_bin = TRUE; /* needed when called for the "-b" argument */
2341 }
2342 }
2343 else if (oldval) /* switched off */
2344 {
2345 if (!(opt_flags & OPT_GLOBAL))
2346 {
2347 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2348 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2349 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2350 curbuf->b_p_et = curbuf->b_p_et_nobin;
2351 }
2352 if (!(opt_flags & OPT_LOCAL))
2353 {
2354 p_tw = p_tw_nobin;
2355 p_wm = p_wm_nobin;
2356 p_ml = p_ml_nobin;
2357 p_et = p_et_nobin;
2358 }
2359 }
2360}
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362/*
2363 * Expand environment variables for some string options.
2364 * These string options cannot be indirect!
2365 * If "val" is NULL expand the current value of the option.
2366 * Return pointer to NameBuff, or NULL when not expanded.
2367 */
2368 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002369option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 /* if option doesn't need expansion nothing to do */
2372 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2373 return NULL;
2374
2375 /* If val is longer than MAXPATHL no meaningful expansion can be done,
2376 * expand_env() would truncate the string. */
2377 if (val != NULL && STRLEN(val) > MAXPATHL)
2378 return NULL;
2379
2380 if (val == NULL)
2381 val = *(char_u **)options[opt_idx].var;
2382
2383 /*
2384 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2385 * Escape spaces when expanding 'tags', they are used to separate file
2386 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002387 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 */
2389 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002390 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002391#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002392 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2393#endif
2394 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (STRCMP(NameBuff, val) == 0) /* they are the same */
2396 return NULL;
2397
2398 return NameBuff;
2399}
2400
2401/*
2402 * After setting various option values: recompute variables that depend on
2403 * option values.
2404 */
2405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002406didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 /* initialize the table for 'iskeyword' et.al. */
2409 (void)init_chartab();
2410
2411 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
2412 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02002413 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_SESSION
2415 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
2416 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
2417#endif
2418#ifdef FEAT_FOLDING
2419 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2420#endif
2421 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002422 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#if defined(FEAT_MOUSE) && (defined(UNIX) || defined(VMS))
2425 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
2426#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002427#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002428 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002429 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002430 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002431 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002432#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002433#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
2435#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002436#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
2438#endif
2439#ifdef FEAT_CMDWIN
2440 /* set cedit_key */
2441 (void)check_cedit();
2442#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002443#ifdef FEAT_LINEBREAK
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02002444 briopt_check(curwin);
Bram Moolenaar597a4222014-06-25 14:39:50 +02002445#endif
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002446#ifdef FEAT_LINEBREAK
2447 /* initialize the table for 'breakat'. */
2448 fill_breakat_flags();
2449#endif
2450
2451}
2452
2453/*
2454 * More side effects of setting options.
2455 */
2456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002457didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002458{
2459 /* Initialize the highlight_attr[] table. */
2460 (void)highlight_changed();
2461
2462 /* Parse default for 'wildmode' */
2463 check_opt_wim();
2464
2465 (void)set_chars_option(&p_lcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002466 /* Parse default for 'fillchars'. */
2467 (void)set_chars_option(&p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002468
2469#ifdef FEAT_CLIPBOARD
2470 /* Parse default for 'clipboard' */
2471 (void)check_clipboard_option();
2472#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002473#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002474 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002475 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002476 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002477 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479}
2480
2481/*
2482 * Check for string options that are NULL (normally only termcap options).
2483 */
2484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002485check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 int opt_idx;
2488
2489 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2490 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2491 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2492}
2493
2494/*
2495 * Check string options in a buffer for NULL value.
2496 */
2497 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002498check_buf_options(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 check_string_option(&buf->b_p_bh);
2501 check_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 check_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 check_string_option(&buf->b_p_ff);
2504#ifdef FEAT_FIND_ID
2505 check_string_option(&buf->b_p_def);
2506 check_string_option(&buf->b_p_inc);
2507# ifdef FEAT_EVAL
2508 check_string_option(&buf->b_p_inex);
2509# endif
2510#endif
2511#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2512 check_string_option(&buf->b_p_inde);
2513 check_string_option(&buf->b_p_indk);
2514#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002515#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2516 check_string_option(&buf->b_p_bexpr);
2517#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002518#if defined(FEAT_CRYPT)
2519 check_string_option(&buf->b_p_cm);
2520#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002521 check_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002522#if defined(FEAT_EVAL)
2523 check_string_option(&buf->b_p_fex);
2524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525#ifdef FEAT_CRYPT
2526 check_string_option(&buf->b_p_key);
2527#endif
2528 check_string_option(&buf->b_p_kp);
2529 check_string_option(&buf->b_p_mps);
2530 check_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002531 check_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 check_string_option(&buf->b_p_isk);
2533#ifdef FEAT_COMMENTS
2534 check_string_option(&buf->b_p_com);
2535#endif
2536#ifdef FEAT_FOLDING
2537 check_string_option(&buf->b_p_cms);
2538#endif
2539 check_string_option(&buf->b_p_nf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002540#ifdef FEAT_TEXTOBJ
2541 check_string_option(&buf->b_p_qe);
2542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#ifdef FEAT_SYN_HL
2544 check_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002545 check_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002546#endif
2547#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002548 check_string_option(&buf->b_s.b_p_spc);
2549 check_string_option(&buf->b_s.b_p_spf);
2550 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#endif
2552#ifdef FEAT_SEARCHPATH
2553 check_string_option(&buf->b_p_sua);
2554#endif
2555#ifdef FEAT_CINDENT
2556 check_string_option(&buf->b_p_cink);
2557 check_string_option(&buf->b_p_cino);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01002558 parse_cino(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 check_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
2562 check_string_option(&buf->b_p_cinw);
2563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 check_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002565#ifdef FEAT_COMPL_FUNC
2566 check_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002567 check_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002568#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002569#ifdef FEAT_EVAL
2570 check_string_option(&buf->b_p_tfu);
2571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572#ifdef FEAT_KEYMAP
2573 check_string_option(&buf->b_p_keymap);
2574#endif
2575#ifdef FEAT_QUICKFIX
2576 check_string_option(&buf->b_p_gp);
2577 check_string_option(&buf->b_p_mp);
2578 check_string_option(&buf->b_p_efm);
2579#endif
2580 check_string_option(&buf->b_p_ep);
2581 check_string_option(&buf->b_p_path);
2582 check_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002583 check_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 check_string_option(&buf->b_p_dict);
2585 check_string_option(&buf->b_p_tsr);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002586#ifdef FEAT_LISP
2587 check_string_option(&buf->b_p_lw);
2588#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002589 check_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002590 check_string_option(&buf->b_p_menc);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002591#ifdef FEAT_VARTABS
2592 check_string_option(&buf->b_p_vsts);
2593 check_string_option(&buf->b_p_vts);
2594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595}
2596
2597/*
2598 * Free the string allocated for an option.
2599 * Checks for the string being empty_option. This may happen if we're out of
2600 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
2601 * check_options().
2602 * Does NOT check for P_ALLOCED flag!
2603 */
2604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002605free_string_option(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 if (p != empty_option)
2608 vim_free(p);
2609}
2610
2611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002612clear_string_option(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
2614 if (*pp != empty_option)
2615 vim_free(*pp);
2616 *pp = empty_option;
2617}
2618
2619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002620check_string_option(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
2622 if (*pp == NULL)
2623 *pp = empty_option;
2624}
2625
2626/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002627 * Return the option index found by a pointer into term_strings[].
2628 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002630 int
2631get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002633 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2636 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002637 return opt_idx;
2638 return -1; // cannot happen: didn't find it!
2639}
2640
2641/*
2642 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2643 * Return the option index or -1 if not found.
2644 */
2645 int
2646set_term_option_alloced(char_u **p)
2647{
2648 int opt_idx = get_term_opt_idx(p);
2649
2650 if (opt_idx >= 0)
2651 options[opt_idx].flags |= P_ALLOCED;
2652 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653}
2654
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002655#if defined(FEAT_EVAL) || defined(PROTO)
2656/*
2657 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2658 * Return FALSE when it wasn't.
2659 * Return -1 for an unknown option.
2660 */
2661 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002662was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002663{
2664 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002665 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002666
2667 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002668 {
2669 flagp = insecure_flag(idx, opt_flags);
2670 return (*flagp & P_INSECURE) != 0;
2671 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002672 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002673 return -1;
2674}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675
2676/*
2677 * Get a pointer to the flags used for the P_INSECURE flag of option
2678 * "opt_idx". For some local options a local flags field is used.
2679 */
2680 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002681insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002682{
2683 if (opt_flags & OPT_LOCAL)
2684 switch ((int)options[opt_idx].indir)
2685 {
2686#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002687 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002688#endif
2689#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002690# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002691 case PV_FDE: return &curwin->w_p_fde_flags;
2692 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002693# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002694# ifdef FEAT_BEVAL
2695 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2696# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002697# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002698 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002699# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002700 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002701# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002702 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703# endif
2704#endif
2705 }
2706
2707 /* Nothing special, return global flags field. */
2708 return &options[opt_idx].flags;
2709}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002710#endif
2711
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002712#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002713/*
2714 * Redraw the window title and/or tab page text later.
2715 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01002716static void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002717{
2718 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002719 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002720}
2721#endif
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723/*
2724 * Set a string option to a new value (without checking the effect).
2725 * The string is copied into allocated memory.
2726 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002727 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
2728 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
2729 * "set_sid".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 */
2731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002732set_string_option_direct(
2733 char_u *name,
2734 int opt_idx,
2735 char_u *val,
2736 int opt_flags, /* OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL */
2737 int set_sid UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738{
2739 char_u *s;
2740 char_u **varp;
2741 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002742 int idx = opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
Bram Moolenaar89d40322006-08-29 15:30:07 +00002744 if (idx == -1) /* use name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002746 idx = findoption(name);
2747 if (idx < 0) /* not found (should not happen) */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002749 semsg(_(e_intern2), "set_string_option_direct()");
2750 siemsg(_("For option %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 return;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754
Bram Moolenaar89d40322006-08-29 15:30:07 +00002755 if (options[idx].var == NULL) /* can't set hidden option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 return;
2757
2758 s = vim_strsave(val);
2759 if (s != NULL)
2760 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002761 varp = (char_u **)get_varp_scope(&(options[idx]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 both ? OPT_LOCAL : opt_flags);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002763 if ((opt_flags & OPT_FREE) && (options[idx].flags & P_ALLOCED))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 free_string_option(*varp);
2765 *varp = s;
2766
2767 /* For buffer/window local option may also set the global value. */
2768 if (both)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002769 set_string_option_global(idx, varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
Bram Moolenaar89d40322006-08-29 15:30:07 +00002771 options[idx].flags |= P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773 /* When setting both values of a global option with a local value,
2774 * make the local value empty, so that the global value is used. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002775 if (((int)options[idx].indir & PV_BOTH) && both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
2777 free_string_option(*varp);
2778 *varp = empty_option;
2779 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002780# ifdef FEAT_EVAL
2781 if (set_sid != SID_NONE)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002782 {
2783 sctx_T script_ctx;
2784
2785 if (set_sid == 0)
2786 script_ctx = current_sctx;
2787 else
2788 {
2789 script_ctx.sc_sid = set_sid;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002790 script_ctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002791 script_ctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002792 script_ctx.sc_version = 1;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002793 }
2794 set_option_sctx_idx(idx, opt_flags, script_ctx);
2795 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798}
2799
2800/*
Bram Moolenaar20c023a2019-05-26 21:03:24 +02002801 * Like set_string_option_direct(), but for a window-local option in "wp".
2802 * Blocks autocommands to avoid the old curwin becoming invalid.
2803 */
2804 void
2805set_string_option_direct_in_win(
2806 win_T *wp,
2807 char_u *name,
2808 int opt_idx,
2809 char_u *val,
2810 int opt_flags,
2811 int set_sid)
2812{
2813 win_T *save_curwin = curwin;
2814
2815 block_autocmds();
2816 curwin = wp;
2817 curbuf = curwin->w_buffer;
2818 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
2819 curwin = save_curwin;
2820 curbuf = curwin->w_buffer;
2821 unblock_autocmds();
2822}
2823
2824/*
2825 * Like set_string_option_direct(), but for a buffer-local option in "buf".
2826 * Blocks autocommands to avoid the old curbuf becoming invalid.
2827 */
2828 void
2829set_string_option_direct_in_buf(
2830 buf_T *buf,
2831 char_u *name,
2832 int opt_idx,
2833 char_u *val,
2834 int opt_flags,
2835 int set_sid)
2836{
2837 buf_T *save_curbuf = curbuf;
2838
2839 block_autocmds();
2840 curbuf = buf;
2841 curwin->w_buffer = curbuf;
2842 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
2843 curbuf = save_curbuf;
2844 curwin->w_buffer = curbuf;
2845 unblock_autocmds();
2846}
2847
2848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 * Set global value for string option when it's a local option.
2850 */
2851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002852set_string_option_global(
2853 int opt_idx, /* option index */
2854 char_u **varp) /* pointer to option variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 char_u **p, *s;
2857
2858 /* the global value is always allocated */
2859 if (options[opt_idx].var == VAR_WIN)
2860 p = (char_u **)GLOBAL_WO(varp);
2861 else
2862 p = (char_u **)options[opt_idx].var;
2863 if (options[opt_idx].indir != PV_NONE
2864 && p != varp
2865 && (s = vim_strsave(*varp)) != NULL)
2866 {
2867 free_string_option(*p);
2868 *p = s;
2869 }
2870}
2871
2872/*
2873 * Set a string option to a new value, and handle the effects.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002874 *
2875 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002877 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878set_string_option(
2879 int opt_idx,
2880 char_u *value,
2881 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 char_u *s;
2884 char_u **varp;
2885 char_u *oldval;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002886#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002887 char_u *oldval_l = NULL;
2888 char_u *oldval_g = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02002889 char_u *saved_oldval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02002890 char_u *saved_oldval_l = NULL;
2891 char_u *saved_oldval_g = NULL;
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002892 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02002893#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002894 char *r = NULL;
Bram Moolenaar916a8182018-11-25 02:18:29 +01002895 int value_checked = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896
2897 if (options[opt_idx].var == NULL) /* don't set hidden option */
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002898 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900 s = vim_strsave(value);
2901 if (s != NULL)
2902 {
2903 varp = (char_u **)get_varp_scope(&(options[opt_idx]),
2904 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002905 ? (((int)options[opt_idx].indir & PV_BOTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 ? OPT_GLOBAL : OPT_LOCAL)
2907 : opt_flags);
2908 oldval = *varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002909#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002910 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2911 {
2912 oldval_l = *(char_u **)get_varp_scope(&(options[opt_idx]),
2913 OPT_LOCAL);
2914 oldval_g = *(char_u **)get_varp_scope(&(options[opt_idx]),
2915 OPT_GLOBAL);
2916 }
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 *varp = s;
Bram Moolenaar53744302015-07-17 17:38:22 +02002919
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002920#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002921 if (!starting
2922# ifdef FEAT_CRYPT
2923 && options[opt_idx].indir != PV_KEY
2924# endif
2925 )
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002926 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02002927 if (oldval_l != NULL)
2928 saved_oldval_l = vim_strsave(oldval_l);
2929 if (oldval_g != NULL)
2930 saved_oldval_g = vim_strsave(oldval_g);
Bram Moolenaar53744302015-07-17 17:38:22 +02002931 saved_oldval = vim_strsave(oldval);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002932 saved_newval = vim_strsave(s);
2933 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002934#endif
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002935 if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002936 opt_flags, &value_checked)) == NULL)
2937 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
Bram Moolenaar53744302015-07-17 17:38:22 +02002938
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002939#if defined(FEAT_EVAL)
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002940 /* call autocommand after handling side effects */
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002941 if (r == NULL)
2942 trigger_optionsset_string(opt_idx, opt_flags,
Bram Moolenaard7c96872019-06-15 17:12:48 +02002943 saved_oldval, saved_oldval_l,
2944 saved_oldval_g, saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002945 vim_free(saved_oldval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002946 vim_free(saved_oldval_l);
2947 vim_free(saved_oldval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002948 vim_free(saved_newval);
Bram Moolenaar53744302015-07-17 17:38:22 +02002949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02002951 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952}
2953
2954/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002955 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2956 * characters or characters in "allowed".
2957 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002958 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002959valid_name(char_u *val, char *allowed)
2960{
2961 char_u *s;
2962
2963 for (s = val; *s != NUL; ++s)
2964 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2965 return FALSE;
2966 return TRUE;
2967}
2968
2969/*
Bram Moolenaard0b51382016-11-04 15:23:45 +01002970 * Return TRUE if "val" is a valid 'filetype' name.
2971 * Also used for 'syntax' and 'keymap'.
2972 */
2973 static int
2974valid_filetype(char_u *val)
2975{
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002976 return valid_name(val, ".-_");
2977}
Bram Moolenaard0b51382016-11-04 15:23:45 +01002978
Bram Moolenaar862f1e12019-04-10 22:33:41 +02002979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 * Handle string options that need some action to perform when changed.
2981 * Returns NULL for success, or an error message for an error.
2982 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002983 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002984did_set_string_option(
Bram Moolenaar916a8182018-11-25 02:18:29 +01002985 int opt_idx, // index in options[] table
2986 char_u **varp, // pointer to the option variable
2987 int new_value_alloced, // new value was allocated
2988 char_u *oldval, // previous value of the option
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002989 char *errbuf, // buffer for errors, or NULL
Bram Moolenaar916a8182018-11-25 02:18:29 +01002990 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
2991 int *value_checked) // value was checked to be save, no
2992 // need to set P_INSECURE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 char_u *s, *p;
2996 int did_chartab = FALSE;
2997 char_u **gvarp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002998 long_u free_oldval = (options[opt_idx].flags & P_ALLOCED);
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00002999#ifdef FEAT_GUI
3000 /* set when changing an option that only requires a redraw in the GUI */
3001 int redraw_gui_only = FALSE;
3002#endif
Bram Moolenaarc3ffc9b2018-06-17 17:32:58 +02003003 int value_changed = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003004#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3005 int did_swaptcap = FALSE;
3006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 /* Get the global option to compare with, otherwise we would have to check
3009 * two values for all local options. */
3010 gvarp = (char_u **)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL);
3011
3012 /* Disallow changing some options from secure mode */
3013 if ((secure
3014#ifdef HAVE_SANDBOX
3015 || sandbox != 0
3016#endif
3017 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 errmsg = e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
Bram Moolenaar916a8182018-11-25 02:18:29 +01003020 // Check for a "normal" directory or file name in some options. Disallow a
3021 // path separator (slash and/or backslash), wildcards and characters that
3022 // are often illegal in a file name. Be more permissive if "secure" is off.
Bram Moolenaar7554da42016-11-25 22:04:13 +01003023 else if (((options[opt_idx].flags & P_NFNAME)
Bram Moolenaar0945eaf2016-11-29 22:10:48 +01003024 && vim_strpbrk(*varp, (char_u *)(secure
3025 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
Bram Moolenaar7554da42016-11-25 22:04:13 +01003026 || ((options[opt_idx].flags & P_NDNAME)
3027 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003028 errmsg = e_invarg;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 /* 'term' */
3031 else if (varp == &T_NAME)
3032 {
3033 if (T_NAME[0] == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003034 errmsg = N_("E529: Cannot set 'term' to empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#ifdef FEAT_GUI
3036 if (gui.in_use)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003037 errmsg = N_("E530: Cannot change term in GUI");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else if (term_is_gui(T_NAME))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003039 errmsg = N_("E531: Use \":gui\" to start the GUI");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#endif
3041 else if (set_termname(T_NAME) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003042 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else
Bram Moolenaar67391142017-02-19 21:07:04 +01003044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 /* Screen colors may have changed. */
3046 redraw_later_clear();
Bram Moolenaar67391142017-02-19 21:07:04 +01003047
3048 /* Both 'term' and 'ttytype' point to T_NAME, only set the
3049 * P_ALLOCED flag on 'term'. */
3050 opt_idx = findoption((char_u *)"term");
Bram Moolenaar354796c2017-02-23 17:18:37 +01003051 free_oldval = (options[opt_idx].flags & P_ALLOCED);
Bram Moolenaar67391142017-02-19 21:07:04 +01003052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054
3055 /* 'backupcopy' */
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02003056 else if (gvarp == &p_bkc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 {
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02003058 char_u *bkc = p_bkc;
3059 unsigned int *flags = &bkc_flags;
3060
3061 if (opt_flags & OPT_LOCAL)
3062 {
3063 bkc = curbuf->b_p_bkc;
3064 flags = &curbuf->b_bkc_flags;
3065 }
3066
Bram Moolenaar84d17a62014-09-29 17:15:18 +02003067 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
3068 /* make the local value empty: use the global value */
3069 *flags = 0;
3070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
Bram Moolenaar84d17a62014-09-29 17:15:18 +02003072 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
3073 errmsg = e_invarg;
3074 if ((((int)*flags & BKC_AUTO) != 0)
3075 + (((int)*flags & BKC_YES) != 0)
3076 + (((int)*flags & BKC_NO) != 0) != 1)
3077 {
3078 /* Must have exactly one of "auto", "yes" and "no". */
3079 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
3080 errmsg = e_invarg;
3081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
3083 }
3084
3085 /* 'backupext' and 'patchmode' */
3086 else if (varp == &p_bex || varp == &p_pm)
3087 {
3088 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
3089 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003090 errmsg = N_("E589: 'backupext' and 'patchmode' are equal");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003092#ifdef FEAT_LINEBREAK
3093 /* 'breakindentopt' */
3094 else if (varp == &curwin->w_p_briopt)
3095 {
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02003096 if (briopt_check(curwin) == FAIL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003097 errmsg = e_invarg;
3098 }
3099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 /*
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01003102 * 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * If the new option is invalid, use old value. 'lisp' option: refill
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01003104 * g_chartab[] for '-' char
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 */
3106 else if ( varp == &p_isi
3107 || varp == &(curbuf->b_p_isk)
3108 || varp == &p_isp
3109 || varp == &p_isf)
3110 {
3111 if (init_chartab() == FAIL)
3112 {
3113 did_chartab = TRUE; /* need to restore it below */
3114 errmsg = e_invarg; /* error in value */
3115 }
3116 }
3117
3118 /* 'helpfile' */
3119 else if (varp == &p_hf)
3120 {
3121 /* May compute new values for $VIM and $VIMRUNTIME */
3122 if (didset_vim)
3123 {
3124 vim_setenv((char_u *)"VIM", (char_u *)"");
3125 didset_vim = FALSE;
3126 }
3127 if (didset_vimruntime)
3128 {
3129 vim_setenv((char_u *)"VIMRUNTIME", (char_u *)"");
3130 didset_vimruntime = FALSE;
3131 }
3132 }
3133
Bram Moolenaar1a384422010-07-14 19:53:30 +02003134#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02003135 /* 'cursorlineopt' */
3136 else if (varp == &curwin->w_p_culopt
3137 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
3138 {
3139 if (**varp == NUL
3140 || check_opt_strings(*varp, p_culopt_values, FALSE) != OK)
3141 errmsg = e_invarg;
3142 }
3143
Bram Moolenaar1a384422010-07-14 19:53:30 +02003144 /* 'colorcolumn' */
3145 else if (varp == &curwin->w_p_cc)
3146 errmsg = check_colorcolumn(curwin);
3147#endif
3148
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149#ifdef FEAT_MULTI_LANG
3150 /* 'helplang' */
3151 else if (varp == &p_hlg)
3152 {
3153 /* Check for "", "ab", "ab,cd", etc. */
3154 for (s = p_hlg; *s != NUL; s += 3)
3155 {
3156 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
3157 {
3158 errmsg = e_invarg;
3159 break;
3160 }
3161 if (s[2] == NUL)
3162 break;
3163 }
3164 }
3165#endif
3166
3167 /* 'highlight' */
3168 else if (varp == &p_hl)
3169 {
3170 if (highlight_changed() == FAIL)
3171 errmsg = e_invarg; /* invalid flags */
3172 }
3173
3174 /* 'nrformats' */
3175 else if (gvarp == &p_nf)
3176 {
3177 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
3178 errmsg = e_invarg;
3179 }
3180
3181#ifdef FEAT_SESSION
3182 /* 'sessionoptions' */
3183 else if (varp == &p_ssop)
3184 {
3185 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3186 errmsg = e_invarg;
3187 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3188 {
3189 /* Don't allow both "sesdir" and "curdir". */
3190 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
3191 errmsg = e_invarg;
3192 }
3193 }
3194 /* 'viewoptions' */
3195 else if (varp == &p_vop)
3196 {
3197 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
3198 errmsg = e_invarg;
3199 }
3200#endif
3201
3202 /* 'scrollopt' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else if (varp == &p_sbo)
3204 {
3205 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
3206 errmsg = e_invarg;
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /* 'ambiwidth' */
Bram Moolenaar3848e002016-03-19 18:42:29 +01003210 else if (varp == &p_ambw || varp == &p_emoji)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
3213 errmsg = e_invarg;
Bram Moolenaar5c3bd0a2010-08-04 20:55:44 +02003214 else if (set_chars_option(&p_lcs) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003215 errmsg = _("E834: Conflicts with value of 'listchars'");
Bram Moolenaar5c3bd0a2010-08-04 20:55:44 +02003216 else if (set_chars_option(&p_fcs) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003217 errmsg = _("E835: Conflicts with value of 'fillchars'");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 /* 'background' */
3221 else if (varp == &p_bg)
3222 {
3223 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
3224 {
3225#ifdef FEAT_EVAL
3226 int dark = (*p_bg == 'd');
3227#endif
3228
3229 init_highlight(FALSE, FALSE);
3230
3231#ifdef FEAT_EVAL
3232 if (dark != (*p_bg == 'd')
3233 && get_var_value((char_u *)"g:colors_name") != NULL)
3234 {
3235 /* The color scheme must have set 'background' back to another
3236 * value, that's not what we want here. Disable the color
3237 * scheme and set the colors again. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00003238 do_unlet((char_u *)"g:colors_name", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 free_string_option(p_bg);
3240 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
3241 check_string_option(&p_bg);
3242 init_highlight(FALSE, FALSE);
3243 }
3244#endif
3245 }
3246 else
3247 errmsg = e_invarg;
3248 }
3249
3250 /* 'wildmode' */
3251 else if (varp == &p_wim)
3252 {
3253 if (check_opt_wim() == FAIL)
3254 errmsg = e_invarg;
3255 }
3256
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003257 /* 'wildoptions' */
3258 else if (varp == &p_wop)
3259 {
3260 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
3261 errmsg = e_invarg;
3262 }
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#ifdef FEAT_WAK
3265 /* 'winaltkeys' */
3266 else if (varp == &p_wak)
3267 {
3268 if (*p_wak == NUL
3269 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
3270 errmsg = e_invarg;
3271# ifdef FEAT_MENU
3272# ifdef FEAT_GUI_MOTIF
3273 else if (gui.in_use)
3274 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
3275# else
3276# ifdef FEAT_GUI_GTK
3277 else if (gui.in_use)
3278 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
3279# endif
3280# endif
3281# endif
3282 }
3283#endif
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /* 'eventignore' */
3286 else if (varp == &p_ei)
3287 {
3288 if (check_ei() == FAIL)
3289 errmsg = e_invarg;
3290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01003292 /* 'encoding', 'fileencoding', 'termencoding' and 'makeencoding' */
3293 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
3294 || gvarp == &p_menc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
3296 if (gvarp == &p_fenc)
3297 {
Bram Moolenaarf2f70252008-02-13 17:36:06 +00003298 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 errmsg = e_modifiable;
3300 else if (vim_strchr(*varp, ',') != NULL)
3301 /* No comma allowed in 'fileencoding'; catches confusing it
3302 * with 'fileencodings'. */
3303 errmsg = e_invarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003305 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003306#ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 /* May show a "+" in the title now. */
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003308 redraw_titles();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003309#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003310 /* Add 'fileencoding' to the swap file. */
3311 ml_setflags(curbuf);
3312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 }
3314 if (errmsg == NULL)
3315 {
3316 /* canonize the value, so that STRCMP() can be used on it */
3317 p = enc_canonize(*varp);
3318 if (p != NULL)
3319 {
3320 vim_free(*varp);
3321 *varp = p;
3322 }
3323 if (varp == &p_enc)
3324 {
3325 errmsg = mb_init();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003326#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003327 redraw_titles();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330 }
3331
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003332#if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
3334 {
3335 /* GTK+ 2 uses only a single encoding, and that is UTF-8. */
3336 if (STRCMP(p_tenc, "utf-8") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003337 errmsg = N_("E617: Cannot be changed in the GTK+ 2 GUI");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
3341 if (errmsg == NULL)
3342 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003343#ifdef FEAT_KEYMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 /* When 'keymap' is used and 'encoding' changes, reload the keymap
3345 * (with another encoding). */
3346 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
3347 (void)keymap_init();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
3350 /* When 'termencoding' is not empty and 'encoding' changes or when
3351 * 'termencoding' changes, need to setup for keyboard input and
3352 * display output conversion. */
3353 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
3354 {
Bram Moolenaar17471e82017-11-26 23:47:18 +01003355 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
3356 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
3357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003358 semsg(_("E950: Cannot convert between %s and %s"),
Bram Moolenaar17471e82017-11-26 23:47:18 +01003359 p_tenc, p_enc);
3360 errmsg = e_invarg;
3361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003363
Bram Moolenaar4f974752019-02-17 17:44:42 +01003364#if defined(MSWIN)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003365 /* $HOME may have characters in active code page. */
3366 if (varp == &p_enc)
3367 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372#if defined(FEAT_POSTSCRIPT)
3373 else if (varp == &p_penc)
3374 {
3375 /* Canonize printencoding if VIM standard one */
3376 p = enc_canonize(p_penc);
3377 if (p != NULL)
3378 {
3379 vim_free(p_penc);
3380 p_penc = p;
3381 }
3382 else
3383 {
3384 /* Ensure lower case and '-' for '_' */
3385 for (s = p_penc; *s != NUL; s++)
3386 {
3387 if (*s == '_')
3388 *s = '-';
3389 else
3390 *s = TOLOWER_ASC(*s);
3391 }
3392 }
3393 }
3394#endif
3395
Bram Moolenaar9372a112005-12-06 19:59:18 +00003396#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else if (varp == &p_imak)
3398 {
Bram Moolenaar86e57922017-04-23 18:44:26 +02003399 if (!im_xim_isvalid_imactivate())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 errmsg = e_invarg;
3401 }
3402#endif
3403
3404#ifdef FEAT_KEYMAP
3405 else if (varp == &curbuf->b_p_keymap)
3406 {
Bram Moolenaard0b51382016-11-04 15:23:45 +01003407 if (!valid_filetype(*varp))
3408 errmsg = e_invarg;
3409 else
Bram Moolenaar916a8182018-11-25 02:18:29 +01003410 {
3411 int secure_save = secure;
3412
3413 // Reset the secure flag, since the value of 'keymap' has
3414 // been checked to be safe.
3415 secure = 0;
3416
3417 // load or unload key mapping tables
Bram Moolenaard0b51382016-11-04 15:23:45 +01003418 errmsg = keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar916a8182018-11-25 02:18:29 +01003420 secure = secure_save;
3421
3422 // Since we check the value, there is no need to set P_INSECURE,
3423 // even when the value comes from a modeline.
3424 *value_checked = TRUE;
3425 }
3426
Bram Moolenaarfab06232009-03-04 03:13:35 +00003427 if (errmsg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
Bram Moolenaarfab06232009-03-04 03:13:35 +00003429 if (*curbuf->b_p_keymap != NUL)
3430 {
3431 /* Installed a new keymap, switch on using it. */
3432 curbuf->b_p_iminsert = B_IMODE_LMAP;
3433 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
3434 curbuf->b_p_imsearch = B_IMODE_LMAP;
3435 }
3436 else
3437 {
3438 /* Cleared the keymap, may reset 'iminsert' and 'imsearch'. */
3439 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3440 curbuf->b_p_iminsert = B_IMODE_NONE;
3441 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
3442 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3443 }
3444 if ((opt_flags & OPT_LOCAL) == 0)
3445 {
3446 set_iminsert_global();
3447 set_imsearch_global();
3448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 status_redraw_curbuf();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451 }
3452#endif
3453
3454 /* 'fileformat' */
3455 else if (gvarp == &p_ff)
3456 {
3457 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
3458 errmsg = e_modifiable;
3459 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
3460 errmsg = e_invarg;
3461 else
3462 {
3463 /* may also change 'textmode' */
3464 if (get_fileformat(curbuf) == EOL_DOS)
3465 curbuf->b_p_tx = TRUE;
3466 else
3467 curbuf->b_p_tx = FALSE;
3468#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003469 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003471 /* update flag in swap file */
3472 ml_setflags(curbuf);
Bram Moolenaar0413d482010-02-11 17:02:11 +01003473 /* Redraw needed when switching to/from "mac": a CR in the text
3474 * will be displayed differently. */
3475 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
3476 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
3478 }
3479
3480 /* 'fileformats' */
3481 else if (varp == &p_ffs)
3482 {
3483 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
3484 errmsg = e_invarg;
3485 else
3486 {
3487 /* also change 'textauto' */
3488 if (*p_ffs == NUL)
3489 p_ta = FALSE;
3490 else
3491 p_ta = TRUE;
3492 }
3493 }
3494
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02003495#if defined(FEAT_CRYPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 /* 'cryptkey' */
3497 else if (gvarp == &p_key)
3498 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02003499 // Make sure the ":set" command doesn't show the new value in the
3500 // history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 remove_key_from_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02003502
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02003503 if (STRCMP(curbuf->b_p_key, oldval) != 0)
3504 /* Need to update the swapfile. */
Bram Moolenaarbc563362015-06-09 18:35:25 +02003505 ml_set_crypt_key(curbuf, oldval,
3506 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar49771f42010-07-20 17:32:38 +02003507 }
3508
3509 else if (gvarp == &p_cm)
3510 {
3511 if (opt_flags & OPT_LOCAL)
3512 p = curbuf->b_p_cm;
3513 else
3514 p = p_cm;
3515 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
3516 errmsg = e_invarg;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02003517 else if (crypt_self_test() == FAIL)
Bram Moolenaar49771f42010-07-20 17:32:38 +02003518 errmsg = e_invarg;
3519 else
3520 {
3521 /* When setting the global value to empty, make it "zip". */
3522 if (*p_cm == NUL)
3523 {
3524 if (new_value_alloced)
3525 free_string_option(p_cm);
3526 p_cm = vim_strsave((char_u *)"zip");
3527 new_value_alloced = TRUE;
3528 }
Bram Moolenaar2be79502014-08-13 21:58:28 +02003529 /* When using ":set cm=name" the local value is going to be empty.
3530 * Do that here, otherwise the crypt functions will still use the
3531 * local value. */
3532 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3533 {
3534 free_string_option(curbuf->b_p_cm);
3535 curbuf->b_p_cm = empty_option;
3536 }
Bram Moolenaar49771f42010-07-20 17:32:38 +02003537
3538 /* Need to update the swapfile when the effective method changed.
3539 * Set "s" to the effective old value, "p" to the effective new
3540 * method and compare. */
3541 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
3542 s = p_cm; /* was previously using the global value */
3543 else
3544 s = oldval;
3545 if (*curbuf->b_p_cm == NUL)
3546 p = p_cm; /* is now using the global value */
3547 else
3548 p = curbuf->b_p_cm;
3549 if (STRCMP(s, p) != 0)
Bram Moolenaarbc563362015-06-09 18:35:25 +02003550 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
Bram Moolenaar49771f42010-07-20 17:32:38 +02003551
3552 /* If the global value changes need to update the swapfile for all
3553 * buffers using that value. */
3554 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
3555 {
3556 buf_T *buf;
3557
Bram Moolenaar29323592016-07-24 22:04:11 +02003558 FOR_ALL_BUFFERS(buf)
Bram Moolenaar49771f42010-07-20 17:32:38 +02003559 if (buf != curbuf && *buf->b_p_cm == NUL)
Bram Moolenaarbc563362015-06-09 18:35:25 +02003560 ml_set_crypt_key(buf, buf->b_p_key, oldval);
Bram Moolenaar49771f42010-07-20 17:32:38 +02003561 }
3562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564#endif
3565
3566 /* 'matchpairs' */
3567 else if (gvarp == &p_mps)
3568 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01003569 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01003571 for (p = *varp; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar09365022013-01-17 17:37:35 +01003573 int x2 = -1;
3574 int x3 = -1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01003575
3576 if (*p != NUL)
3577 p += mb_ptr2len(p);
3578 if (*p != NUL)
3579 x2 = *p++;
3580 if (*p != NUL)
3581 {
3582 x3 = mb_ptr2char(p);
3583 p += mb_ptr2len(p);
3584 }
Bram Moolenaar09365022013-01-17 17:37:35 +01003585 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01003586 {
3587 errmsg = e_invarg;
3588 break;
3589 }
3590 if (*p == NUL)
3591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01003593 }
3594 else
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01003595 {
3596 /* Check for "x:y,x:y" */
3597 for (p = *varp; *p != NUL; p += 4)
3598 {
3599 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3600 {
3601 errmsg = e_invarg;
3602 break;
3603 }
3604 if (p[3] == NUL)
3605 break;
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 }
3609
3610#ifdef FEAT_COMMENTS
3611 /* 'comments' */
3612 else if (gvarp == &p_com)
3613 {
3614 for (s = *varp; *s; )
3615 {
3616 while (*s && *s != ':')
3617 {
3618 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
3619 && !VIM_ISDIGIT(*s) && *s != '-')
3620 {
3621 errmsg = illegal_char(errbuf, *s);
3622 break;
3623 }
3624 ++s;
3625 }
3626 if (*s++ == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003627 errmsg = N_("E524: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 else if (*s == ',' || *s == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003629 errmsg = N_("E525: Zero length string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (errmsg != NULL)
3631 break;
3632 while (*s && *s != ',')
3633 {
3634 if (*s == '\\' && s[1] != NUL)
3635 ++s;
3636 ++s;
3637 }
3638 s = skip_to_option_part(s);
3639 }
3640 }
3641#endif
3642
3643 /* 'listchars' */
3644 else if (varp == &p_lcs)
3645 {
3646 errmsg = set_chars_option(varp);
3647 }
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 /* 'fillchars' */
3650 else if (varp == &p_fcs)
3651 {
3652 errmsg = set_chars_option(varp);
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655#ifdef FEAT_CMDWIN
3656 /* 'cedit' */
3657 else if (varp == &p_cedit)
3658 {
3659 errmsg = check_cedit();
3660 }
3661#endif
3662
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003663 /* 'verbosefile' */
3664 else if (varp == &p_vfile)
3665 {
3666 verbose_stop();
3667 if (*p_vfile != NUL && verbose_open() == FAIL)
3668 errmsg = e_invarg;
3669 }
3670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671#ifdef FEAT_VIMINFO
3672 /* 'viminfo' */
3673 else if (varp == &p_viminfo)
3674 {
3675 for (s = p_viminfo; *s;)
3676 {
3677 /* Check it's a valid character */
3678 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
3679 {
3680 errmsg = illegal_char(errbuf, *s);
3681 break;
3682 }
3683 if (*s == 'n') /* name is always last one */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 else if (*s == 'r') /* skip until next ',' */
3686 {
3687 while (*++s && *s != ',')
3688 ;
3689 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00003690 else if (*s == '%')
3691 {
3692 /* optional number */
3693 while (vim_isdigit(*++s))
3694 ;
3695 }
3696 else if (*s == '!' || *s == 'h' || *s == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 ++s; /* no extra chars */
3698 else /* must have a number */
3699 {
3700 while (vim_isdigit(*++s))
3701 ;
3702
3703 if (!VIM_ISDIGIT(*(s - 1)))
3704 {
3705 if (errbuf != NULL)
3706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003707 sprintf(errbuf, _("E526: Missing number after <%s>"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 transchar_byte(*(s - 1)));
3709 errmsg = errbuf;
3710 }
3711 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003712 errmsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 break;
3714 }
3715 }
3716 if (*s == ',')
3717 ++s;
3718 else if (*s)
3719 {
3720 if (errbuf != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003721 errmsg = N_("E527: Missing comma");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003723 errmsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 break;
3725 }
3726 }
3727 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003728 errmsg = N_("E528: Must specify a ' value");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 }
3730#endif /* FEAT_VIMINFO */
3731
3732 /* terminal options */
3733 else if (istermoption(&options[opt_idx]) && full_screen)
3734 {
3735 /* ":set t_Co=0" and ":set t_Co=1" do ":set t_Co=" */
3736 if (varp == &T_CCO)
3737 {
Bram Moolenaarc84e8952009-03-18 13:21:18 +00003738 int colors = atoi((char *)T_CCO);
3739
3740 /* Only reinitialize colors if t_Co value has really changed to
3741 * avoid expensive reload of colorscheme if t_Co is set to the
3742 * same value multiple times. */
3743 if (colors != t_colors)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaarc84e8952009-03-18 13:21:18 +00003745 t_colors = colors;
3746 if (t_colors <= 1)
3747 {
3748 if (new_value_alloced)
3749 vim_free(T_CCO);
3750 T_CCO = empty_option;
3751 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003752#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3753 if (is_term_win32())
3754 {
3755 swap_tcap();
3756 did_swaptcap = TRUE;
3757 }
3758#endif
Bram Moolenaarc84e8952009-03-18 13:21:18 +00003759 /* We now have a different color setup, initialize it again. */
3760 init_highlight(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 ttest(FALSE);
3764 if (varp == &T_ME)
3765 {
3766 out_str(T_ME);
3767 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003768#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 /* Since t_me has been set, this probably means that the user
3770 * wants to use this as default colors. Need to reset default
3771 * background/foreground colors. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003772# ifdef VIMDLL
3773 if (!gui.in_use && !gui.starting)
3774# endif
3775 mch_set_normal_colors();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776#endif
3777 }
Bram Moolenaard9c60642017-01-27 20:03:18 +01003778 if (varp == &T_BE && termcap_active)
3779 {
3780 if (*T_BE == NUL)
3781 /* When clearing t_BE we assume the user no longer wants
3782 * bracketed paste, thus disable it by writing t_BD. */
3783 out_str(T_BD);
3784 else
3785 out_str(T_BE);
3786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788
3789#ifdef FEAT_LINEBREAK
3790 /* 'showbreak' */
3791 else if (varp == &p_sbr)
3792 {
3793 for (s = p_sbr; *s; )
3794 {
3795 if (ptr2cells(s) != 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003796 errmsg = N_("E595: contains unprintable or wide character");
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003797 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799 }
3800#endif
3801
3802#ifdef FEAT_GUI
3803 /* 'guifont' */
3804 else if (varp == &p_guifont)
3805 {
3806 if (gui.in_use)
3807 {
3808 p = p_guifont;
Bram Moolenaar9372a112005-12-06 19:59:18 +00003809# if defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 /*
3811 * Put up a font dialog and let the user select a new value.
3812 * If this is cancelled go back to the old value but don't
3813 * give an error message.
3814 */
3815 if (STRCMP(p, "*") == 0)
3816 {
3817 p = gui_mch_font_dialog(oldval);
3818
3819 if (new_value_alloced)
3820 free_string_option(p_guifont);
3821
3822 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
3823 new_value_alloced = TRUE;
3824 }
3825# endif
3826 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
3827 {
3828# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
3829 if (STRCMP(p_guifont, "*") == 0)
3830 {
3831 /* Dialog was cancelled: Keep the old value without giving
3832 * an error message. */
3833 if (new_value_alloced)
3834 free_string_option(p_guifont);
3835 p_guifont = vim_strsave(oldval);
3836 new_value_alloced = TRUE;
3837 }
3838 else
3839# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003840 errmsg = N_("E596: Invalid font(s)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 }
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00003843 redraw_gui_only = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845# ifdef FEAT_XFONTSET
3846 else if (varp == &p_guifontset)
3847 {
3848 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003849 errmsg = N_("E597: can't select fontset");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaarb1443b42019-01-13 23:51:14 +01003851 errmsg = N_("E598: Invalid fontset");
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00003852 redraw_gui_only = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 else if (varp == &p_guifontwide)
3856 {
3857 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003858 errmsg = N_("E533: can't select wide font");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003860 errmsg = N_("E534: Invalid wide font");
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00003861 redraw_gui_only = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863#endif
3864
3865#ifdef CURSOR_SHAPE
3866 /* 'guicursor' */
3867 else if (varp == &p_guicursor)
3868 errmsg = parse_shape_opt(SHAPE_CURSOR);
3869#endif
3870
3871#ifdef FEAT_MOUSESHAPE
3872 /* 'mouseshape' */
3873 else if (varp == &p_mouseshape)
3874 {
3875 errmsg = parse_shape_opt(SHAPE_MOUSE);
3876 update_mouseshape(-1);
3877 }
3878#endif
3879
3880#ifdef FEAT_PRINTER
3881 else if (varp == &p_popt)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003882 errmsg = parse_printoptions();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003883# if defined(FEAT_POSTSCRIPT)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003884 else if (varp == &p_pmfn)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003885 errmsg = parse_printmbfont();
Bram Moolenaar8299df92004-07-10 09:47:34 +00003886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#endif
3888
3889#ifdef FEAT_LANGMAP
3890 /* 'langmap' */
3891 else if (varp == &p_langmap)
3892 langmap_set();
3893#endif
3894
3895#ifdef FEAT_LINEBREAK
3896 /* 'breakat' */
3897 else if (varp == &p_breakat)
3898 fill_breakat_flags();
3899#endif
3900
3901#ifdef FEAT_TITLE
3902 /* 'titlestring' and 'iconstring' */
3903 else if (varp == &p_titlestring || varp == &p_iconstring)
3904 {
3905# ifdef FEAT_STL_OPT
3906 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
3907
3908 /* NULL => statusline syntax */
3909 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
3910 stl_syntax |= flagval;
3911 else
3912 stl_syntax &= ~flagval;
3913# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003914 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916#endif
3917
3918#ifdef FEAT_GUI
3919 /* 'guioptions' */
3920 else if (varp == &p_go)
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00003921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 gui_init_which_components(oldval);
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00003923 redraw_gui_only = TRUE;
3924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#endif
3926
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003927#if defined(FEAT_GUI_TABLINE)
3928 /* 'guitablabel' */
3929 else if (varp == &p_gtl)
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00003930 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003931 redraw_tabline = TRUE;
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00003932 redraw_gui_only = TRUE;
3933 }
3934 /* 'guitabtooltip' */
3935 else if (varp == &p_gtt)
3936 {
3937 redraw_gui_only = TRUE;
3938 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003939#endif
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#if defined(FEAT_MOUSE_TTY) && (defined(UNIX) || defined(VMS))
3942 /* 'ttymouse' */
3943 else if (varp == &p_ttym)
3944 {
Bram Moolenaarf1f8bc52005-03-07 23:20:08 +00003945 /* Switch the mouse off before changing the escape sequences used for
3946 * that. */
3947 mch_setmouse(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3949 errmsg = e_invarg;
3950 else
3951 check_mouse_termcode();
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003952 if (termcap_active)
3953 setmouse(); /* may switch it on again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 }
3955#endif
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 /* 'selection' */
3958 else if (varp == &p_sel)
3959 {
3960 if (*p_sel == NUL
3961 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
3962 errmsg = e_invarg;
3963 }
3964
3965 /* 'selectmode' */
3966 else if (varp == &p_slm)
3967 {
3968 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
3969 errmsg = e_invarg;
3970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
3972#ifdef FEAT_BROWSE
3973 /* 'browsedir' */
3974 else if (varp == &p_bsdir)
3975 {
3976 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
3977 && !mch_isdir(p_bsdir))
3978 errmsg = e_invarg;
3979 }
3980#endif
3981
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 /* 'keymodel' */
3983 else if (varp == &p_km)
3984 {
3985 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
3986 errmsg = e_invarg;
3987 else
3988 {
3989 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
3990 km_startsel = (vim_strchr(p_km, 'a') != NULL);
3991 }
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 /* 'mousemodel' */
3995 else if (varp == &p_mousem)
3996 {
3997 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3998 errmsg = e_invarg;
3999#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
4000 else if (*p_mousem != *oldval)
4001 /* Changed from "extend" to "popup" or "popup_setpos" or vv: need
4002 * to create or delete the popup menus. */
4003 gui_motif_update_mousemodel(root_menu);
4004#endif
4005 }
4006
4007 /* 'switchbuf' */
4008 else if (varp == &p_swb)
4009 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004010 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 errmsg = e_invarg;
4012 }
4013
4014 /* 'debug' */
4015 else if (varp == &p_debug)
4016 {
Bram Moolenaar57657d82006-04-21 22:12:41 +00004017 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 errmsg = e_invarg;
4019 }
4020
4021 /* 'display' */
4022 else if (varp == &p_dy)
4023 {
4024 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
4025 errmsg = e_invarg;
4026 else
4027 (void)init_chartab();
4028
4029 }
4030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 /* 'eadirection' */
4032 else if (varp == &p_ead)
4033 {
4034 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
4035 errmsg = e_invarg;
4036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
4038#ifdef FEAT_CLIPBOARD
4039 /* 'clipboard' */
4040 else if (varp == &p_cb)
4041 errmsg = check_clipboard_option();
4042#endif
4043
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004044#ifdef FEAT_SPELL
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004045 /* When 'spelllang' or 'spellfile' is set and there is a window for this
4046 * buffer in which 'spell' is set load the wordlists. */
Bram Moolenaar2683c8e2014-11-19 19:33:16 +01004047 else if (varp == &(curwin->w_s->b_p_spl)
4048 || varp == &(curwin->w_s->b_p_spf))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004049 {
Bram Moolenaar862f1e12019-04-10 22:33:41 +02004050 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
4051
4052 if ((is_spellfile && !valid_spellfile(*varp))
4053 || (!is_spellfile && !valid_spellang(*varp)))
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02004054 errmsg = e_invarg;
4055 else
Bram Moolenaar862f1e12019-04-10 22:33:41 +02004056 errmsg = did_set_spell_option(is_spellfile);
Bram Moolenaar217ad922005-03-20 22:37:15 +00004057 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004058 /* When 'spellcapcheck' is set compile the regexp program. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004059 else if (varp == &(curwin->w_s->b_p_spc))
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004060 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004061 errmsg = compile_cap_prog(curwin->w_s);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004062 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004063 /* 'spellsuggest' */
4064 else if (varp == &p_sps)
4065 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004066 if (spell_check_sps() != OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004067 errmsg = e_invarg;
4068 }
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00004069 /* 'mkspellmem' */
4070 else if (varp == &p_msm)
4071 {
4072 if (spell_check_msm() != OK)
4073 errmsg = e_invarg;
4074 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004075#endif
4076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 /* When 'bufhidden' is set, check for valid value. */
4078 else if (gvarp == &p_bh)
4079 {
4080 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
4081 errmsg = e_invarg;
4082 }
4083
4084 /* When 'buftype' is set, check for valid value. */
4085 else if (gvarp == &p_bt)
4086 {
4087 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
4088 errmsg = e_invarg;
4089 else
4090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (curwin->w_status_height)
4092 {
4093 curwin->w_redr_status = TRUE;
4094 redraw_later(VALID);
4095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004097#ifdef FEAT_TITLE
Bram Moolenaar5075aad2010-01-27 15:58:13 +01004098 redraw_titles();
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103#ifdef FEAT_STL_OPT
4104 /* 'statusline' or 'rulerformat' */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004105 else if (gvarp == &p_stl || varp == &p_ruf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107 int wid;
4108
4109 if (varp == &p_ruf) /* reset ru_wid first */
4110 ru_wid = 0;
4111 s = *varp;
4112 if (varp == &p_ruf && *s == '%')
4113 {
4114 /* set ru_wid if 'ruf' starts with "%99(" */
4115 if (*++s == '-') /* ignore a '-' */
4116 s++;
4117 wid = getdigits(&s);
4118 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
4119 ru_wid = wid;
4120 else
4121 errmsg = check_stl_option(p_ruf);
4122 }
Bram Moolenaar3709e7c2006-08-08 14:29:16 +00004123 /* check 'statusline' only if it doesn't start with "%!" */
Bram Moolenaar177d8c62007-09-06 11:33:37 +00004124 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 errmsg = check_stl_option(s);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004126 if (varp == &p_ruf && errmsg == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 comp_col();
4128 }
4129#endif
4130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /* check if it is a valid value for 'complete' -- Acevedo */
4132 else if (gvarp == &p_cpt)
4133 {
4134 for (s = *varp; *s;)
4135 {
Bram Moolenaar11b73d62012-06-29 15:51:30 +02004136 while (*s == ',' || *s == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 s++;
4138 if (!*s)
4139 break;
4140 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
4141 {
4142 errmsg = illegal_char(errbuf, *s);
4143 break;
4144 }
4145 if (*++s != NUL && *s != ',' && *s != ' ')
4146 {
4147 if (s[-1] == 'k' || s[-1] == 's')
4148 {
4149 /* skip optional filename after 'k' and 's' */
4150 while (*s && *s != ',' && *s != ' ')
4151 {
Bram Moolenaar226c5342017-02-17 14:53:15 +01004152 if (*s == '\\' && s[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 ++s;
4154 ++s;
4155 }
4156 }
4157 else
4158 {
4159 if (errbuf != NULL)
4160 {
4161 sprintf((char *)errbuf,
4162 _("E535: Illegal character after <%c>"),
4163 *--s);
4164 errmsg = errbuf;
4165 }
4166 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167 errmsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 break;
4169 }
4170 }
4171 }
4172 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004173
Bram Moolenaarac3150d2019-07-28 16:36:39 +02004174 // 'completeopt'
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004175 else if (varp == &p_cot)
4176 {
4177 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
4178 errmsg = e_invarg;
Bram Moolenaarc0200422016-04-20 12:02:02 +02004179 else
4180 completeopt_was_set();
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004181 }
Bram Moolenaarac3150d2019-07-28 16:36:39 +02004182
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004183#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02004184 // 'completeslash'
Bram Moolenaarb78564d2019-07-28 19:24:36 +02004185 else if (gvarp == &p_csl)
Bram Moolenaarac3150d2019-07-28 16:36:39 +02004186 {
Bram Moolenaarb78564d2019-07-28 19:24:36 +02004187 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
4188 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaarac3150d2019-07-28 16:36:39 +02004189 errmsg = e_invarg;
4190 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004193#ifdef FEAT_SIGNS
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004194 // 'signcolumn'
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004195 else if (varp == &curwin->w_p_scl)
4196 {
4197 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
4198 errmsg = e_invarg;
Bram Moolenaare4b407f2019-07-04 11:59:28 +02004199 // When changing the 'signcolumn' to or from 'number', recompute the
4200 // width of the number column if 'number' or 'relativenumber' is set.
4201 if (((*oldval == 'n' && *(oldval + 1) == 'u')
4202 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
4203 && (curwin->w_p_nu || curwin->w_p_rnu))
4204 curwin->w_nrwidth_line_count = 0;
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004205 }
4206#endif
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
Bram Moolenaar4f974752019-02-17 17:44:42 +01004209#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar031cb742016-11-24 21:46:19 +01004210 /* 'toolbar' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 else if (varp == &p_toolbar)
4212 {
4213 if (opt_strings_flags(p_toolbar, p_toolbar_values,
4214 &toolbar_flags, TRUE) != OK)
4215 errmsg = e_invarg;
4216 else
4217 {
4218 out_flush();
4219 gui_mch_show_toolbar((toolbar_flags &
4220 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4221 }
4222 }
4223#endif
4224
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004225#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 /* 'toolbariconsize': GTK+ 2 only */
4227 else if (varp == &p_tbis)
4228 {
4229 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4230 errmsg = e_invarg;
4231 else
4232 {
4233 out_flush();
4234 gui_mch_show_toolbar((toolbar_flags &
4235 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4236 }
4237 }
4238#endif
4239
4240 /* 'pastetoggle': translate key codes like in a mapping */
4241 else if (varp == &p_pt)
4242 {
4243 if (*p_pt)
4244 {
Bram Moolenaar9c102382006-05-03 21:26:49 +00004245 (void)replace_termcodes(p_pt, &p, TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (p != NULL)
4247 {
4248 if (new_value_alloced)
4249 free_string_option(p_pt);
4250 p_pt = p;
4251 new_value_alloced = TRUE;
4252 }
4253 }
4254 }
4255
4256 /* 'backspace' */
4257 else if (varp == &p_bs)
4258 {
4259 if (VIM_ISDIGIT(*p_bs))
4260 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004261 if (*p_bs > '2' || p_bs[1] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 errmsg = e_invarg;
4263 }
4264 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
4265 errmsg = e_invarg;
4266 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02004267 else if (varp == &p_bo)
4268 {
4269 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
4270 errmsg = e_invarg;
4271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01004273 /* 'tagcase' */
4274 else if (gvarp == &p_tc)
4275 {
4276 unsigned int *flags;
4277
4278 if (opt_flags & OPT_LOCAL)
4279 {
4280 p = curbuf->b_p_tc;
4281 flags = &curbuf->b_tc_flags;
4282 }
4283 else
4284 {
4285 p = p_tc;
4286 flags = &tc_flags;
4287 }
4288
4289 if ((opt_flags & OPT_LOCAL) && *p == NUL)
4290 /* make the local value empty: use the global value */
4291 *flags = 0;
4292 else if (*p == NUL
4293 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
4294 errmsg = e_invarg;
4295 }
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 /* 'casemap' */
4298 else if (varp == &p_cmp)
4299 {
4300 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
4301 errmsg = e_invarg;
4302 }
4303
4304#ifdef FEAT_DIFF
4305 /* 'diffopt' */
4306 else if (varp == &p_dip)
4307 {
4308 if (diffopt_changed() == FAIL)
4309 errmsg = e_invarg;
4310 }
4311#endif
4312
4313#ifdef FEAT_FOLDING
4314 /* 'foldmethod' */
4315 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
4316 {
4317 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
4318 || *curwin->w_p_fdm == NUL)
4319 errmsg = e_invarg;
4320 else
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01004321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 foldUpdateAll(curwin);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01004323 if (foldmethodIsDiff(curwin))
4324 newFoldLevel();
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327# ifdef FEAT_EVAL
4328 /* 'foldexpr' */
4329 else if (varp == &curwin->w_p_fde)
4330 {
4331 if (foldmethodIsExpr(curwin))
4332 foldUpdateAll(curwin);
4333 }
4334# endif
4335 /* 'foldmarker' */
4336 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
4337 {
4338 p = vim_strchr(*varp, ',');
4339 if (p == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004340 errmsg = N_("E536: comma required");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 else if (p == *varp || p[1] == NUL)
4342 errmsg = e_invarg;
4343 else if (foldmethodIsMarker(curwin))
4344 foldUpdateAll(curwin);
4345 }
4346 /* 'commentstring' */
4347 else if (gvarp == &p_cms)
4348 {
4349 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004350 errmsg = N_("E537: 'commentstring' must be empty or contain %s");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 /* 'foldopen' */
4353 else if (varp == &p_fdo)
4354 {
4355 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
4356 errmsg = e_invarg;
4357 }
4358 /* 'foldclose' */
4359 else if (varp == &p_fcl)
4360 {
4361 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
4362 errmsg = e_invarg;
4363 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004364 /* 'foldignore' */
4365 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
4366 {
4367 if (foldmethodIsIndent(curwin))
4368 foldUpdateAll(curwin);
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#endif
4371
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 /* 'virtualedit' */
4373 else if (varp == &p_ve)
4374 {
4375 if (opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE) != OK)
4376 errmsg = e_invarg;
4377 else if (STRCMP(p_ve, oldval) != 0)
4378 {
4379 /* Recompute cursor position in case the new 've' setting
4380 * changes something. */
4381 validate_virtcol();
4382 coladvance(curwin->w_virtcol);
4383 }
4384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
4387 else if (varp == &p_csqf)
4388 {
4389 if (p_csqf != NULL)
4390 {
4391 p = p_csqf;
4392 while (*p != NUL)
4393 {
4394 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
4395 || p[1] == NUL
4396 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
4397 || (p[2] != NUL && p[2] != ','))
4398 {
4399 errmsg = e_invarg;
4400 break;
4401 }
4402 else if (p[2] == NUL)
4403 break;
4404 else
4405 p += 3;
4406 }
4407 }
4408 }
4409#endif
4410
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004411#ifdef FEAT_CINDENT
4412 /* 'cinoptions' */
4413 else if (gvarp == &p_cino)
4414 {
4415 /* TODO: recognize errors */
4416 parse_cino(curbuf);
4417 }
4418#endif
4419
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02004420#if defined(FEAT_RENDER_OPTIONS)
Bram Moolenaar031cb742016-11-24 21:46:19 +01004421 /* 'renderoptions' */
Bram Moolenaar3767c6e2017-12-05 16:57:56 +01004422 else if (varp == &p_rop)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02004423 {
4424 if (!gui_mch_set_rendering_options(p_rop))
4425 errmsg = e_invarg;
4426 }
4427#endif
4428
Bram Moolenaard0b51382016-11-04 15:23:45 +01004429 else if (gvarp == &p_ft)
4430 {
4431 if (!valid_filetype(*varp))
4432 errmsg = e_invarg;
Bram Moolenaar90492982017-06-22 14:16:31 +02004433 else
Bram Moolenaar916a8182018-11-25 02:18:29 +01004434 {
Bram Moolenaarc3ffc9b2018-06-17 17:32:58 +02004435 value_changed = STRCMP(oldval, *varp) != 0;
Bram Moolenaar916a8182018-11-25 02:18:29 +01004436
4437 // Since we check the value, there is no need to set P_INSECURE,
4438 // even when the value comes from a modeline.
4439 *value_checked = TRUE;
4440 }
Bram Moolenaard0b51382016-11-04 15:23:45 +01004441 }
Bram Moolenaard0b51382016-11-04 15:23:45 +01004442
4443#ifdef FEAT_SYN_HL
4444 else if (gvarp == &p_syn)
4445 {
4446 if (!valid_filetype(*varp))
4447 errmsg = e_invarg;
Bram Moolenaarc3ffc9b2018-06-17 17:32:58 +02004448 else
Bram Moolenaar916a8182018-11-25 02:18:29 +01004449 {
Bram Moolenaarc3ffc9b2018-06-17 17:32:58 +02004450 value_changed = STRCMP(oldval, *varp) != 0;
Bram Moolenaar916a8182018-11-25 02:18:29 +01004451
4452 // Since we check the value, there is no need to set P_INSECURE,
4453 // even when the value comes from a modeline.
4454 *value_checked = TRUE;
4455 }
Bram Moolenaard0b51382016-11-04 15:23:45 +01004456 }
4457#endif
4458
Bram Moolenaar825680f2017-07-22 17:04:02 +02004459#ifdef FEAT_TERMINAL
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004460 // 'termwinkey'
Bram Moolenaar6d150f72018-04-21 20:03:20 +02004461 else if (varp == &curwin->w_p_twk)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02004462 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02004463 if (*curwin->w_p_twk != NUL
4464 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02004465 errmsg = e_invarg;
4466 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004467 // 'termwinsize'
Bram Moolenaar6d150f72018-04-21 20:03:20 +02004468 else if (varp == &curwin->w_p_tws)
Bram Moolenaar825680f2017-07-22 17:04:02 +02004469 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02004470 if (*curwin->w_p_tws != NUL)
Bram Moolenaar825680f2017-07-22 17:04:02 +02004471 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02004472 p = skipdigits(curwin->w_p_tws);
4473 if (p == curwin->w_p_tws
Bram Moolenaar498c2562018-04-15 23:45:15 +02004474 || (*p != 'x' && *p != '*')
4475 || *skipdigits(p + 1) != NUL)
Bram Moolenaar825680f2017-07-22 17:04:02 +02004476 errmsg = e_invarg;
4477 }
4478 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004479# if defined(MSWIN)
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004480 // 'termwintype'
4481 else if (varp == &p_twt)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004482 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004483 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004484 errmsg = e_invarg;
4485 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004486# endif
Bram Moolenaar825680f2017-07-22 17:04:02 +02004487#endif
4488
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004489#ifdef FEAT_VARTABS
4490 /* 'varsofttabstop' */
4491 else if (varp == &(curbuf->b_p_vsts))
4492 {
4493 char_u *cp;
4494
4495 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
4496 {
4497 if (curbuf->b_p_vsts_array)
4498 {
4499 vim_free(curbuf->b_p_vsts_array);
4500 curbuf->b_p_vsts_array = 0;
4501 }
4502 }
4503 else
4504 {
4505 for (cp = *varp; *cp; ++cp)
4506 {
4507 if (vim_isdigit(*cp))
4508 continue;
4509 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
4510 continue;
4511 errmsg = e_invarg;
4512 break;
4513 }
4514 if (errmsg == NULL)
4515 {
4516 int *oldarray = curbuf->b_p_vsts_array;
4517 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)))
4518 {
4519 if (oldarray)
4520 vim_free(oldarray);
4521 }
4522 else
4523 errmsg = e_invarg;
4524 }
4525 }
4526 }
4527
4528 /* 'vartabstop' */
4529 else if (varp == &(curbuf->b_p_vts))
4530 {
4531 char_u *cp;
4532
4533 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
4534 {
4535 if (curbuf->b_p_vts_array)
4536 {
4537 vim_free(curbuf->b_p_vts_array);
4538 curbuf->b_p_vts_array = NULL;
4539 }
4540 }
4541 else
4542 {
4543 for (cp = *varp; *cp; ++cp)
4544 {
4545 if (vim_isdigit(*cp))
4546 continue;
4547 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
4548 continue;
4549 errmsg = e_invarg;
4550 break;
4551 }
4552 if (errmsg == NULL)
4553 {
4554 int *oldarray = curbuf->b_p_vts_array;
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01004555
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004556 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)))
4557 {
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01004558 vim_free(oldarray);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004559#ifdef FEAT_FOLDING
4560 if (foldmethodIsIndent(curwin))
4561 foldUpdateAll(curwin);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01004562#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004563 }
4564 else
4565 errmsg = e_invarg;
4566 }
4567 }
4568 }
4569#endif
4570
Bram Moolenaar79648732019-07-18 21:43:07 +02004571#ifdef FEAT_TEXT_PROP
4572 // 'previewpopup'
4573 else if (varp == &p_pvp)
4574 {
4575 if (parse_previewpopup(NULL) == FAIL)
4576 errmsg = e_invarg;
4577 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004578# ifdef FEAT_QUICKFIX
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02004579 // 'completepopup'
4580 else if (varp == &p_cpp)
4581 {
4582 if (parse_completepopup(NULL) == FAIL)
4583 errmsg = e_invarg;
4584 }
Bram Moolenaar36e4d982019-08-20 21:12:16 +02004585# endif
Bram Moolenaar79648732019-07-18 21:43:07 +02004586#endif
4587
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 /* Options that are a list of flags. */
4589 else
4590 {
4591 p = NULL;
Bram Moolenaar031cb742016-11-24 21:46:19 +01004592 if (varp == &p_ww) /* 'whichwrap' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 p = (char_u *)WW_ALL;
Bram Moolenaar031cb742016-11-24 21:46:19 +01004594 if (varp == &p_shm) /* 'shortmess' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 p = (char_u *)SHM_ALL;
Bram Moolenaar031cb742016-11-24 21:46:19 +01004596 else if (varp == &(p_cpo)) /* 'cpoptions' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 p = (char_u *)CPO_ALL;
Bram Moolenaar031cb742016-11-24 21:46:19 +01004598 else if (varp == &(curbuf->b_p_fo)) /* 'formatoptions' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 p = (char_u *)FO_ALL;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004600#ifdef FEAT_CONCEAL
Bram Moolenaar031cb742016-11-24 21:46:19 +01004601 else if (varp == &curwin->w_p_cocu) /* 'concealcursor' */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004602 p = (char_u *)COCU_ALL;
4603#endif
Bram Moolenaar031cb742016-11-24 21:46:19 +01004604 else if (varp == &p_mouse) /* 'mouse' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606#ifdef FEAT_MOUSE
4607 p = (char_u *)MOUSE_ALL;
4608#else
4609 if (*p_mouse != NUL)
Bram Moolenaarb1443b42019-01-13 23:51:14 +01004610 errmsg = N_("E538: No mouse support");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#endif
4612 }
4613#if defined(FEAT_GUI)
Bram Moolenaar031cb742016-11-24 21:46:19 +01004614 else if (varp == &p_go) /* 'guioptions' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 p = (char_u *)GO_ALL;
4616#endif
4617 if (p != NULL)
4618 {
4619 for (s = *varp; *s; ++s)
4620 if (vim_strchr(p, *s) == NULL)
4621 {
4622 errmsg = illegal_char(errbuf, *s);
4623 break;
4624 }
4625 }
4626 }
4627
4628 /*
4629 * If error detected, restore the previous value.
4630 */
4631 if (errmsg != NULL)
4632 {
4633 if (new_value_alloced)
4634 free_string_option(*varp);
4635 *varp = oldval;
4636 /*
4637 * When resetting some values, need to act on it.
4638 */
4639 if (did_chartab)
4640 (void)init_chartab();
4641 if (varp == &p_hl)
4642 (void)highlight_changed();
4643 }
4644 else
4645 {
4646#ifdef FEAT_EVAL
4647 /* Remember where the option was set. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004648 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#endif
4650 /*
4651 * Free string options that are in allocated memory.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004652 * Use "free_oldval", because recursiveness may change the flags under
4653 * our fingers (esp. init_highlight()).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004655 if (free_oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 free_string_option(oldval);
4657 if (new_value_alloced)
4658 options[opt_idx].flags |= P_ALLOCED;
4659 else
4660 options[opt_idx].flags &= ~P_ALLOCED;
4661
4662 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004663 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
4665 /* global option with local value set to use global value; free
4666 * the local value and make it empty */
4667 p = get_varp_scope(&(options[opt_idx]), OPT_LOCAL);
4668 free_string_option(*(char_u **)p);
4669 *(char_u **)p = empty_option;
4670 }
4671
4672 /* May set global value for local option. */
4673 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4674 set_string_option_global(opt_idx, varp);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004675
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004676 /*
4677 * Trigger the autocommand only after setting the flags.
4678 */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004679#ifdef FEAT_SYN_HL
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004680 /* When 'syntax' is set, load the syntax of that name */
4681 if (varp == &(curbuf->b_p_syn))
4682 {
Bram Moolenaara5616b02018-06-17 19:08:30 +02004683 static int syn_recursive = 0;
4684
4685 ++syn_recursive;
4686 // Only pass TRUE for "force" when the value changed or not used
4687 // recursively, to avoid endless recurrence.
4688 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4689 value_changed || syn_recursive == 1, curbuf);
Bram Moolenaarc7f1e402019-08-03 13:29:46 +02004690 curbuf->b_flags |= BF_SYN_SET;
Bram Moolenaara5616b02018-06-17 19:08:30 +02004691 --syn_recursive;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004692 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004693#endif
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004694 else if (varp == &(curbuf->b_p_ft))
4695 {
Bram Moolenaar90492982017-06-22 14:16:31 +02004696 /* 'filetype' is set, trigger the FileType autocommand.
4697 * Skip this when called from a modeline and the filetype was
Bram Moolenaara5616b02018-06-17 19:08:30 +02004698 * already set to this value. */
Bram Moolenaarc3ffc9b2018-06-17 17:32:58 +02004699 if (!(opt_flags & OPT_MODELINE) || value_changed)
Bram Moolenaar90492982017-06-22 14:16:31 +02004700 {
Bram Moolenaar916a8182018-11-25 02:18:29 +01004701 static int ft_recursive = 0;
4702 int secure_save = secure;
4703
4704 // Reset the secure flag, since the value of 'filetype' has
4705 // been checked to be safe.
4706 secure = 0;
Bram Moolenaara5616b02018-06-17 19:08:30 +02004707
4708 ++ft_recursive;
Bram Moolenaar90492982017-06-22 14:16:31 +02004709 did_filetype = TRUE;
Bram Moolenaara5616b02018-06-17 19:08:30 +02004710 // Only pass TRUE for "force" when the value changed or not
4711 // used recursively, to avoid endless recurrence.
4712 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4713 value_changed || ft_recursive == 1, curbuf);
4714 --ft_recursive;
Bram Moolenaar163095f2017-07-09 15:41:53 +02004715 /* Just in case the old "curbuf" is now invalid. */
4716 if (varp != &(curbuf->b_p_ft))
4717 varp = NULL;
Bram Moolenaar916a8182018-11-25 02:18:29 +01004718
4719 secure = secure_save;
Bram Moolenaar90492982017-06-22 14:16:31 +02004720 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004721 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004722#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02004723 if (varp == &(curwin->w_s->b_p_spl))
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004724 {
4725 char_u fname[200];
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004726 char_u *q = curwin->w_s->b_p_spl;
4727
4728 /* Skip the first name if it is "cjk". */
4729 if (STRNCMP(q, "cjk,", 4) == 0)
4730 q += 4;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004731
4732 /*
4733 * Source the spell/LANG.vim in 'runtimepath'.
4734 * They could set 'spellcapcheck' depending on the language.
4735 * Use the first name in 'spelllang' up to '_region' or
4736 * '.encoding'.
4737 */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004738 for (p = q; *p != NUL; ++p)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01004739 if (!ASCII_ISALNUM(*p) && *p != '-')
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004740 break;
Bram Moolenaar82e8c922018-11-20 13:32:36 +01004741 if (p > q)
4742 {
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02004743 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4744 (int)(p - q), q);
Bram Moolenaar82e8c922018-11-20 13:32:36 +01004745 source_runtime(fname, DIP_ALL);
4746 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004747 }
4748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750
4751#ifdef FEAT_MOUSE
4752 if (varp == &p_mouse)
4753 {
4754# ifdef FEAT_MOUSE_TTY
4755 if (*p_mouse == NUL)
4756 mch_setmouse(FALSE); /* switch mouse off */
4757 else
4758# endif
4759 setmouse(); /* in case 'mouse' changed */
4760 }
4761#endif
4762
Bram Moolenaar913077c2012-03-28 19:59:04 +02004763 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01004764 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02004765 curwin->w_set_curswant = TRUE;
4766
Bram Moolenaarfaff14a2009-02-04 16:29:07 +00004767#ifdef FEAT_GUI
4768 /* check redraw when it's not a GUI option or the GUI is active. */
4769 if (!redraw_gui_only || gui.in_use)
4770#endif
4771 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004773#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
4774 if (did_swaptcap)
4775 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004776 set_termname((char_u *)"win32");
4777 init_highlight(TRUE, FALSE);
4778 }
4779#endif
4780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return errmsg;
4782}
4783
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#ifdef FEAT_STL_OPT
4785/*
4786 * Check validity of options with the 'statusline' format.
4787 * Return error message or NULL.
4788 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004789 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004790check_stl_option(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
4792 int itemcnt = 0;
4793 int groupdepth = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004794 static char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 while (*s && itemcnt < STL_MAX_ITEM)
4797 {
4798 /* Check for valid keys after % sequences */
4799 while (*s && *s != '%')
4800 s++;
4801 if (!*s)
4802 break;
4803 s++;
4804 if (*s != '%' && *s != ')')
4805 ++itemcnt;
4806 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
4807 {
4808 s++;
4809 continue;
4810 }
4811 if (*s == ')')
4812 {
4813 s++;
4814 if (--groupdepth < 0)
4815 break;
4816 continue;
4817 }
4818 if (*s == '-')
4819 s++;
4820 while (VIM_ISDIGIT(*s))
4821 s++;
Bram Moolenaar238a5642006-02-21 22:12:05 +00004822 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 continue;
4824 if (*s == '.')
4825 {
4826 s++;
4827 while (*s && VIM_ISDIGIT(*s))
4828 s++;
4829 }
4830 if (*s == '(')
4831 {
4832 groupdepth++;
4833 continue;
4834 }
4835 if (vim_strchr(STL_ALL, *s) == NULL)
4836 {
4837 return illegal_char(errbuf, *s);
4838 }
4839 if (*s == '{')
4840 {
4841 s++;
4842 while (*s != '}' && *s)
4843 s++;
4844 if (*s != '}')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004845 return N_("E540: Unclosed expression sequence");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 }
4848 if (itemcnt >= STL_MAX_ITEM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004849 return N_("E541: too many items");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (groupdepth != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004851 return N_("E542: unbalanced groups");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return NULL;
4853}
4854#endif
4855
4856#ifdef FEAT_CLIPBOARD
4857/*
4858 * Extract the items in the 'clipboard' option and set global values.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 * Return an error message or NULL for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004861 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004862check_clipboard_option(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01004864 int new_unnamed = 0;
Bram Moolenaar89af4392012-07-10 18:31:54 +02004865 int new_autoselect_star = FALSE;
4866 int new_autoselect_plus = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 int new_autoselectml = FALSE;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004868 int new_html = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 regprog_T *new_exclude_prog = NULL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004870 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 char_u *p;
4872
4873 for (p = p_cb; *p != NUL; )
4874 {
4875 if (STRNCMP(p, "unnamed", 7) == 0 && (p[7] == ',' || p[7] == NUL))
4876 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01004877 new_unnamed |= CLIP_UNNAMED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 p += 7;
4879 }
Bram Moolenaar11b73d62012-06-29 15:51:30 +02004880 else if (STRNCMP(p, "unnamedplus", 11) == 0
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01004881 && (p[11] == ',' || p[11] == NUL))
4882 {
4883 new_unnamed |= CLIP_UNNAMED_PLUS;
4884 p += 11;
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 else if (STRNCMP(p, "autoselect", 10) == 0
Bram Moolenaar89af4392012-07-10 18:31:54 +02004887 && (p[10] == ',' || p[10] == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaar89af4392012-07-10 18:31:54 +02004889 new_autoselect_star = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 p += 10;
4891 }
Bram Moolenaar89af4392012-07-10 18:31:54 +02004892 else if (STRNCMP(p, "autoselectplus", 14) == 0
4893 && (p[14] == ',' || p[14] == NUL))
4894 {
4895 new_autoselect_plus = TRUE;
4896 p += 14;
4897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else if (STRNCMP(p, "autoselectml", 12) == 0
Bram Moolenaar89af4392012-07-10 18:31:54 +02004899 && (p[12] == ',' || p[12] == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
4901 new_autoselectml = TRUE;
4902 p += 12;
4903 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004904 else if (STRNCMP(p, "html", 4) == 0 && (p[4] == ',' || p[4] == NUL))
4905 {
4906 new_html = TRUE;
4907 p += 4;
4908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 else if (STRNCMP(p, "exclude:", 8) == 0 && new_exclude_prog == NULL)
4910 {
4911 p += 8;
4912 new_exclude_prog = vim_regcomp(p, RE_MAGIC);
4913 if (new_exclude_prog == NULL)
4914 errmsg = e_invarg;
4915 break;
4916 }
4917 else
4918 {
4919 errmsg = e_invarg;
4920 break;
4921 }
4922 if (*p == ',')
4923 ++p;
4924 }
4925 if (errmsg == NULL)
4926 {
4927 clip_unnamed = new_unnamed;
Bram Moolenaar89af4392012-07-10 18:31:54 +02004928 clip_autoselect_star = new_autoselect_star;
4929 clip_autoselect_plus = new_autoselect_plus;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 clip_autoselectml = new_autoselectml;
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00004931 clip_html = new_html;
Bram Moolenaar473de612013-06-08 18:19:48 +02004932 vim_regfree(clip_exclude_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 clip_exclude_prog = new_exclude_prog;
Bram Moolenaara76638f2010-06-05 12:49:46 +02004934#ifdef FEAT_GUI_GTK
4935 if (gui.in_use)
4936 {
4937 gui_gtk_set_selection_targets();
4938 gui_gtk_set_dnd_targets();
4939 }
4940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 else
Bram Moolenaar473de612013-06-08 18:19:48 +02004943 vim_regfree(new_exclude_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 return errmsg;
4946}
4947#endif
4948
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004949#if defined(FEAT_EVAL) || defined(PROTO)
4950/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004951 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004952 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004953 */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004954 static void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004955set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004956{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004957 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
4958 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004959 sctx_T new_script_ctx = script_ctx;
4960
4961 new_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004962
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004963 /* Remember where the option was set. For local options need to do that
4964 * in the buffer or window structure. */
4965 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004966 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004967 if (both || (opt_flags & OPT_LOCAL))
4968 {
4969 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004970 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004971 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004972 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004973 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004974}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004975
4976/*
4977 * Set the script_ctx for a termcap option.
4978 * "name" must be the two character code, e.g. "RV".
4979 * When "name" is NULL use "opt_idx".
4980 */
4981 void
4982set_term_option_sctx_idx(char *name, int opt_idx)
4983{
4984 char_u buf[5];
4985 int idx;
4986
4987 if (name == NULL)
4988 idx = opt_idx;
4989 else
4990 {
4991 buf[0] = 't';
4992 buf[1] = '_';
4993 buf[2] = name[0];
4994 buf[3] = name[1];
4995 buf[4] = 0;
4996 idx = findoption(buf);
4997 }
4998 if (idx >= 0)
4999 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
5000}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005001#endif
5002
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003/*
5004 * Set the value of a boolean option, and take care of side effects.
5005 * Returns NULL for success, or an error message for an error.
5006 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005007 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005008set_bool_option(
5009 int opt_idx, /* index in options[] table */
5010 char_u *varp, /* pointer to the option variable */
5011 int value, /* new value */
5012 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005015#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02005016 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 /* Disallow changing some options from secure mode */
5020 if ((secure
5021#ifdef HAVE_SANDBOX
5022 || sandbox != 0
5023#endif
5024 ) && (options[opt_idx].flags & P_SECURE))
5025 return e_secure;
5026
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005027#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02005028 // Save the global value before changing anything. This is needed as for
5029 // a global-only option setting the "local value" in fact sets the global
5030 // value (since there is only one value).
5031 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
5032 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
5033 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005034#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02005035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 *(int *)varp = value; /* set the new value */
5037#ifdef FEAT_EVAL
5038 /* Remember where the option was set. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005039 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040#endif
5041
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005042#ifdef FEAT_GUI
5043 need_mouse_correct = TRUE;
5044#endif
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 /* May set global value for local option. */
5047 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
5048 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
5049
5050 /*
5051 * Handle side effects of changing a bool option.
5052 */
5053
5054 /* 'compatible' */
5055 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
Bram Moolenaar920694c2016-08-21 17:45:02 +02005058#ifdef FEAT_LANGMAP
5059 if ((int *)varp == &p_lrm)
5060 /* 'langremap' -> !'langnoremap' */
5061 p_lnr = !p_lrm;
5062 else if ((int *)varp == &p_lnr)
5063 /* 'langnoremap' -> !'langremap' */
5064 p_lrm = !p_lnr;
5065#endif
5066
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02005067#ifdef FEAT_SYN_HL
5068 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
5069 reset_cursorline();
5070#endif
5071
Bram Moolenaar374d32d2012-01-04 19:34:37 +01005072#ifdef FEAT_PERSISTENT_UNDO
5073 /* 'undofile' */
5074 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
5075 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02005076 /* Only take action when the option was set. When reset we do not
5077 * delete the undo file, the option may be set again without making
5078 * any changes in between. */
5079 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01005080 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02005081 char_u hash[UNDO_HASH_SIZE];
5082 buf_T *save_curbuf = curbuf;
5083
Bram Moolenaar29323592016-07-24 22:04:11 +02005084 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01005085 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02005086 /* When 'undofile' is set globally: for every buffer, otherwise
5087 * only for the current buffer: Try to read in the undofile,
5088 * if one exists, the buffer wasn't changed and the buffer was
5089 * loaded */
5090 if ((curbuf == save_curbuf
5091 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
5092 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
5093 {
5094 u_compute_hash(hash);
5095 u_read_undo(NULL, hash, curbuf->b_fname);
5096 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01005097 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02005098 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01005099 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01005100 }
5101#endif
5102
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 else if ((int *)varp == &curbuf->b_p_ro)
5104 {
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00005105 /* when 'readonly' is reset globally, also reset readonlymode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
5107 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00005108
5109 /* when 'readonly' is set may give W10 again */
5110 if (curbuf->b_p_ro)
5111 curbuf->b_did_warn = FALSE;
5112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005114 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#endif
5116 }
5117
Bram Moolenaar9c449722010-07-20 18:44:27 +02005118#ifdef FEAT_GUI
5119 else if ((int *)varp == &p_mh)
5120 {
5121 if (!p_mh)
5122 gui_mch_mousehide(FALSE);
5123 }
5124#endif
5125
Bram Moolenaara539df02010-08-01 14:35:05 +02005126 /* when 'modifiable' is changed, redraw the window title */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005128 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02005129# ifdef FEAT_TERMINAL
5130 /* Cannot set 'modifiable' when in Terminal mode. */
Bram Moolenaard7db27b2018-03-07 23:02:33 +01005131 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
5132 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02005133 {
5134 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005135 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02005136 }
5137# endif
5138# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005139 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02005140# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005141 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02005142#ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 /* when 'endofline' is changed, redraw the window title */
5144 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005145 {
5146 redraw_titles();
5147 }
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005148 /* when 'fixeol' is changed, redraw the window title */
5149 else if ((int *)varp == &curbuf->b_p_fixeol)
5150 {
5151 redraw_titles();
5152 }
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005153 /* when 'bomb' is changed, redraw the window title and tab page text */
Bram Moolenaar83eb8852007-08-12 13:51:26 +00005154 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005155 {
5156 redraw_titles();
5157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158#endif
5159
5160 /* when 'bin' is set also set some other options */
5161 else if ((int *)varp == &curbuf->b_p_bin)
5162 {
5163 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
5164#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005165 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166#endif
5167 }
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 /* when 'buflisted' changes, trigger autocommands */
5170 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
5171 {
5172 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
5173 NULL, NULL, TRUE, curbuf);
5174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
5176 /* when 'swf' is set, create swapfile, when reset remove swapfile */
5177 else if ((int *)varp == &curbuf->b_p_swf)
5178 {
5179 if (curbuf->b_p_swf && p_uc)
5180 ml_open_file(curbuf); /* create the swap file */
5181 else
Bram Moolenaard55de222007-05-06 13:38:48 +00005182 /* no need to reset curbuf->b_may_swap, ml_open_file() will check
5183 * buf->b_p_swf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 mf_close_file(curbuf, TRUE); /* remove the swap file */
5185 }
5186
5187 /* when 'terse' is set change 'shortmess' */
5188 else if ((int *)varp == &p_terse)
5189 {
5190 char_u *p;
5191
5192 p = vim_strchr(p_shm, SHM_SEARCH);
5193
5194 /* insert 's' in p_shm */
5195 if (p_terse && p == NULL)
5196 {
5197 STRCPY(IObuff, p_shm);
5198 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005199 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 /* remove 's' from p_shm */
5202 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00005203 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205
5206 /* when 'paste' is set or reset also change other options */
5207 else if ((int *)varp == &p_paste)
5208 {
5209 paste_option_changed();
5210 }
5211
5212 /* when 'insertmode' is set from an autocommand need to do work here */
5213 else if ((int *)varp == &p_im)
5214 {
5215 if (p_im)
5216 {
5217 if ((State & INSERT) == 0)
5218 need_start_insertmode = TRUE;
5219 stop_insert_mode = FALSE;
5220 }
Bram Moolenaar00672e12016-06-26 18:38:13 +02005221 /* only reset if it was set previously */
5222 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 need_start_insertmode = FALSE;
5225 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005226 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 clear_cmdline = TRUE; /* remove "(insert)" */
5228 restart_edit = 0;
5229 }
5230 }
5231
5232 /* when 'ignorecase' is set or reset and 'hlsearch' is set, redraw */
5233 else if ((int *)varp == &p_ic && p_hls)
5234 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005235 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237
5238#ifdef FEAT_SEARCH_EXTRA
5239 /* when 'hlsearch' is set or reset: reset no_hlsearch */
5240 else if ((int *)varp == &p_hls)
5241 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02005242 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244#endif
5245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 /* when 'scrollbind' is set: snapshot the current position to avoid a jump
5247 * at the end of normal_cmd() */
5248 else if ((int *)varp == &curwin->w_p_scb)
5249 {
5250 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02005251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02005253 curwin->w_scbind_pos = curwin->w_topline;
5254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
Bram Moolenaar4033c552017-09-16 20:54:51 +02005257#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 /* There can be only one window with 'previewwindow' set. */
5259 else if ((int *)varp == &curwin->w_p_pvw)
5260 {
5261 if (curwin->w_p_pvw)
5262 {
5263 win_T *win;
5264
Bram Moolenaar29323592016-07-24 22:04:11 +02005265 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (win->w_p_pvw && win != curwin)
5267 {
5268 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005269 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
5271 }
5272 }
5273#endif
5274
5275 /* when 'textmode' is set or reset also change 'fileformat' */
5276 else if ((int *)varp == &curbuf->b_p_tx)
5277 {
5278 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
5279 }
5280
5281 /* when 'textauto' is set or reset also change 'fileformats' */
5282 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01005283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 set_string_option_direct((char_u *)"ffs", -1,
5285 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005286 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01005287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * When 'lisp' option changes include/exclude '-' in
5291 * keyword characters.
5292 */
5293#ifdef FEAT_LISP
5294 else if (varp == (char_u *)&(curbuf->b_p_lisp))
5295 {
5296 (void)buf_init_chartab(curbuf, FALSE); /* ignore errors */
5297 }
5298#endif
5299
5300#ifdef FEAT_TITLE
5301 /* when 'title' changed, may need to change the title; same for 'icon' */
Bram Moolenaar84a93082018-06-16 22:58:15 +02005302 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02005304 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306#endif
5307
5308 else if ((int *)varp == &curbuf->b_changed)
5309 {
5310 if (!value)
5311 save_file_ff(curbuf); /* Buffer is unchanged */
5312#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005313 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 }
5317
5318#ifdef BACKSLASH_IN_FILENAME
5319 else if ((int *)varp == &p_ssl)
5320 {
5321 if (p_ssl)
5322 {
5323 psepc = '/';
5324 psepcN = '\\';
5325 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 else
5328 {
5329 psepc = '\\';
5330 psepcN = '/';
5331 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333
5334 /* need to adjust the file name arguments and buffer names. */
5335 buflist_slash_adjust();
5336 alist_slash_adjust();
5337# ifdef FEAT_EVAL
5338 scriptnames_slash_adjust();
5339# endif
5340 }
5341#endif
5342
5343 /* If 'wrap' is set, set w_leftcol to zero. */
5344 else if ((int *)varp == &curwin->w_p_wrap)
5345 {
5346 if (curwin->w_p_wrap)
5347 curwin->w_leftcol = 0;
5348 }
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 else if ((int *)varp == &p_ea)
5351 {
5352 if (p_ea && !old_value)
5353 win_equal(curwin, FALSE, 0);
5354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 else if ((int *)varp == &p_wiv)
5357 {
5358 /*
5359 * When 'weirdinvert' changed, set/reset 't_xs'.
5360 * Then set 'weirdinvert' according to value of 't_xs'.
5361 */
5362 if (p_wiv && !old_value)
5363 T_XS = (char_u *)"y";
5364 else if (!p_wiv && old_value)
5365 T_XS = empty_option;
5366 p_wiv = (*T_XS != NUL);
5367 }
5368
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005369#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 else if ((int *)varp == &p_beval)
5371 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005372 if (!balloonEvalForTerm)
5373 {
5374 if (p_beval && !old_value)
5375 gui_mch_enable_beval_area(balloonEval);
5376 else if (!p_beval && old_value)
5377 gui_mch_disable_beval_area(balloonEval);
5378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005380#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005381#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005382 else if ((int *)varp == &p_bevalterm)
5383 {
5384 mch_bevalterm_changed();
5385 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005388#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 else if ((int *)varp == &p_acd)
5390 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00005391 /* Change directories when the 'acd' option is set now. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02005392 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394#endif
5395
5396#ifdef FEAT_DIFF
5397 /* 'diff' */
5398 else if ((int *)varp == &curwin->w_p_diff)
5399 {
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00005400 /* May add or remove the buffer from the list of diff buffers. */
5401 diff_buf_adjust(curwin);
5402# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 if (foldmethodIsDiff(curwin))
5404 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00005405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407#endif
5408
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005409#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 /* 'imdisable' */
5411 else if ((int *)varp == &p_imdisable)
5412 {
5413 /* Only de-activate it here, it will be enabled when changing mode. */
5414 if (p_imdisable)
5415 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02005416 else if (State & INSERT)
5417 /* When the option is set from an autocommand, it may need to take
5418 * effect right away. */
5419 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421#endif
5422
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005423#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00005424 /* 'spell' */
5425 else if ((int *)varp == &curwin->w_p_spell)
5426 {
5427 if (curwin->w_p_spell)
5428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005429 char *errmsg = did_set_spelllang(curwin);
5430
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00005431 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005432 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00005433 }
5434 }
5435#endif
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#ifdef FEAT_ARABIC
5438 if ((int *)varp == &curwin->w_p_arab)
5439 {
5440 if (curwin->w_p_arab)
5441 {
5442 /*
5443 * 'arabic' is set, handle various sub-settings.
5444 */
5445 if (!p_tbidi)
5446 {
5447 /* set rightleft mode */
5448 if (!curwin->w_p_rl)
5449 {
5450 curwin->w_p_rl = TRUE;
5451 changed_window_setting();
5452 }
5453
5454 /* Enable Arabic shaping (major part of what Arabic requires) */
5455 if (!p_arshape)
5456 {
5457 p_arshape = TRUE;
5458 redraw_later_clear();
5459 }
5460 }
5461
5462 /* Arabic requires a utf-8 encoding, inform the user if its not
5463 * set. */
5464 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00005465 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00005466 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
5467
Bram Moolenaar8820b482017-03-16 17:23:31 +01005468 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01005469 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00005470#ifdef FEAT_EVAL
5471 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
5472#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00005473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 /* set 'delcombine' */
5476 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478# ifdef FEAT_KEYMAP
5479 /* Force-set the necessary keymap for arabic */
5480 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
5481 OPT_LOCAL);
5482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else
5485 {
5486 /*
5487 * 'arabic' is reset, handle various sub-settings.
5488 */
5489 if (!p_tbidi)
5490 {
5491 /* reset rightleft mode */
5492 if (curwin->w_p_rl)
5493 {
5494 curwin->w_p_rl = FALSE;
5495 changed_window_setting();
5496 }
5497
5498 /* 'arabicshape' isn't reset, it is a global option and
5499 * another window may still need it "on". */
5500 }
5501
5502 /* 'delcombine' isn't reset, it is a global option and another
5503 * window may still want it "on". */
5504
5505# ifdef FEAT_KEYMAP
5506 /* Revert to the default keymap */
5507 curbuf->b_p_iminsert = B_IMODE_NONE;
5508 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
5509# endif
5510 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00005511 }
5512
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00005513#endif
5514
Bram Moolenaar44826212019-08-22 21:23:20 +02005515#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
5516 else if (((int *)varp == &curwin->w_p_nu
5517 || (int *)varp == &curwin->w_p_rnu)
5518 && gui.in_use
5519 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
5520 && curbuf->b_signlist != NULL)
5521 {
5522 // If the 'number' or 'relativenumber' options are modified and
5523 // 'signcolumn' is set to 'number', then clear the screen for a full
5524 // refresh. Otherwise the sign icons are not displayed properly in the
5525 // number column. If the 'number' option is set and only the
5526 // 'relativenumber' option is toggled, then don't refresh the screen
5527 // (optimization).
5528 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
5529 redraw_all_later(CLEAR);
5530 }
5531#endif
5532
Bram Moolenaar61be73b2016-04-29 22:59:22 +02005533#ifdef FEAT_TERMGUICOLORS
5534 /* 'termguicolors' */
5535 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02005536 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005537# ifdef FEAT_VTP
5538 /* Do not turn on 'tgc' when 24-bit colors are not supported. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005539 if (
5540# ifdef VIMDLL
5541 !gui.in_use && !gui.starting &&
5542# endif
5543 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005544 {
5545 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005546 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005547 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005548 if (is_term_win32())
5549 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005550# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02005551# ifdef FEAT_GUI
5552 if (!gui.in_use && !gui.starting)
5553# endif
5554 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005555# ifdef FEAT_VTP
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005556 /* reset t_Co */
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005557 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02005558 {
5559 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005560 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02005561 init_highlight(TRUE, FALSE);
5562 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005563# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02005564 }
5565#endif
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 /*
5568 * End of handling side effects for bool options.
5569 */
5570
Bram Moolenaar53744302015-07-17 17:38:22 +02005571 /* after handling side effects, call autocommand */
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 options[opt_idx].flags |= P_WAS_SET;
5574
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005575#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02005576 // Don't do this while starting up or recursively.
5577 if (!starting && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02005578 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02005579 char_u buf_old[2], buf_old_global[2], buf_new[2], buf_type[7];
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02005580
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02005581 vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02005582 vim_snprintf((char *)buf_old_global, 2, "%d",
5583 old_global_value ? TRUE: FALSE);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02005584 vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02005585 vim_snprintf((char *)buf_type, 7, "%s",
5586 (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02005587 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
5588 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
5589 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02005590 if (opt_flags & OPT_LOCAL)
5591 {
5592 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
5593 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
5594 }
5595 if (opt_flags & OPT_GLOBAL)
5596 {
5597 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
5598 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
5599 }
5600 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
5601 {
5602 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
5603 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
5604 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
5605 }
5606 if (opt_flags & OPT_MODELINE)
5607 {
5608 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
5609 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
5610 }
5611 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
5612 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02005613 reset_v_option_vars();
5614 }
5615#endif
5616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 comp_col(); /* in case 'ruler' or 'showcmd' changed */
Bram Moolenaar913077c2012-03-28 19:59:04 +02005618 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01005619 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02005620 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 check_redraw(options[opt_idx].flags);
5622
5623 return NULL;
5624}
5625
5626/*
5627 * Set the value of a number option, and take care of side effects.
5628 * Returns NULL for success, or an error message for an error.
5629 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005630 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005631set_num_option(
5632 int opt_idx, /* index in options[] table */
5633 char_u *varp, /* pointer to the option variable */
5634 long value, /* new value */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005635 char *errbuf, /* buffer for error messages */
Bram Moolenaar9b578142016-01-30 19:39:49 +01005636 size_t errbuflen, /* length of "errbuf" */
5637 int opt_flags) /* OPT_LOCAL, OPT_GLOBAL and
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 OPT_MODELINE */
5639{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005640 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005642#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02005643 long old_global_value = 0; // only used when setting a local and
5644 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005645#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02005646 long old_Rows = Rows; // remember old Rows
5647 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 long *pp = (long *)varp;
5649
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005650 /* Disallow changing some options from secure mode. */
5651 if ((secure
5652#ifdef HAVE_SANDBOX
5653 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005655 ) && (options[opt_idx].flags & P_SECURE))
5656 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005658#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02005659 // Save the global value before changing anything. This is needed as for
5660 // a global-only option setting the "local value" infact sets the global
5661 // value (since there is only one value).
5662 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02005663 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
5664 OPT_GLOBAL);
5665#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02005666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 *pp = value;
5668#ifdef FEAT_EVAL
5669 /* Remember where the option was set. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005670 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005672#ifdef FEAT_GUI
5673 need_mouse_correct = TRUE;
5674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Bram Moolenaar14f24742012-08-08 18:01:05 +02005676 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
5678 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005679#ifdef FEAT_VARTABS
5680 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
5681 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
5682 ? tabstop_first(curbuf->b_p_vts_array)
5683 : curbuf->b_p_ts;
5684#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688
5689 /*
5690 * Number options that need some action when changed
5691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (pp == &p_wh || pp == &p_hh)
5693 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005694 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (p_wh < 1)
5696 {
5697 errmsg = e_positive;
5698 p_wh = 1;
5699 }
5700 if (p_wmh > p_wh)
5701 {
5702 errmsg = e_winheight;
5703 p_wh = p_wmh;
5704 }
5705 if (p_hh < 0)
5706 {
5707 errmsg = e_positive;
5708 p_hh = 0;
5709 }
5710
5711 /* Change window height NOW */
Bram Moolenaar459ca562016-11-10 18:16:33 +01005712 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 if (pp == &p_wh && curwin->w_height < p_wh)
5715 win_setheight((int)p_wh);
5716 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
5717 win_setheight((int)p_hh);
5718 }
5719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 else if (pp == &p_wmh)
5721 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005722 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (p_wmh < 0)
5724 {
5725 errmsg = e_positive;
5726 p_wmh = 0;
5727 }
5728 if (p_wmh > p_wh)
5729 {
5730 errmsg = e_winheight;
5731 p_wmh = p_wh;
5732 }
5733 win_setminheight();
5734 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005735 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005737 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (p_wiw < 1)
5739 {
5740 errmsg = e_positive;
5741 p_wiw = 1;
5742 }
5743 if (p_wmw > p_wiw)
5744 {
5745 errmsg = e_winwidth;
5746 p_wiw = p_wmw;
5747 }
5748
5749 /* Change window width NOW */
Bram Moolenaar459ca562016-11-10 18:16:33 +01005750 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 win_setwidth((int)p_wiw);
5752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 else if (pp == &p_wmw)
5754 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005755 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (p_wmw < 0)
5757 {
5758 errmsg = e_positive;
5759 p_wmw = 0;
5760 }
5761 if (p_wmw > p_wiw)
5762 {
5763 errmsg = e_winwidth;
5764 p_wmw = p_wiw;
5765 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005766 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 /* (re)set last window status line */
5770 else if (pp == &p_ls)
5771 {
5772 last_status(FALSE);
5773 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00005774
5775 /* (re)set tab page line */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005776 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00005777 {
5778 shell_new_rows(); /* recompute window positions and heights */
5779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781#ifdef FEAT_GUI
5782 else if (pp == &p_linespace)
5783 {
Bram Moolenaar02743632005-07-25 20:42:36 +00005784 /* Recompute gui.char_height and resize the Vim window to keep the
5785 * same number of lines. */
5786 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00005787 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 }
5789#endif
5790
5791#ifdef FEAT_FOLDING
5792 /* 'foldlevel' */
5793 else if (pp == &curwin->w_p_fdl)
5794 {
5795 if (curwin->w_p_fdl < 0)
5796 curwin->w_p_fdl = 0;
5797 newFoldLevel();
5798 }
5799
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00005800 /* 'foldminlines' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 else if (pp == &curwin->w_p_fml)
5802 {
5803 foldUpdateAll(curwin);
5804 }
5805
5806 /* 'foldnestmax' */
5807 else if (pp == &curwin->w_p_fdn)
5808 {
5809 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
5810 foldUpdateAll(curwin);
5811 }
5812
5813 /* 'foldcolumn' */
5814 else if (pp == &curwin->w_p_fdc)
5815 {
5816 if (curwin->w_p_fdc < 0)
5817 {
5818 errmsg = e_positive;
5819 curwin->w_p_fdc = 0;
5820 }
5821 else if (curwin->w_p_fdc > 12)
5822 {
5823 errmsg = e_invarg;
5824 curwin->w_p_fdc = 12;
5825 }
5826 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005827#endif /* FEAT_FOLDING */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005829#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 /* 'shiftwidth' or 'tabstop' */
5831 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
5832 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005833# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (foldmethodIsIndent(curwin))
5835 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005836# endif
5837# ifdef FEAT_CINDENT
5838 /* When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
5839 * parse 'cinoptions'. */
5840 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
5841 parse_cino(curbuf);
5842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005846 /* 'maxcombine' */
5847 else if (pp == &p_mco)
5848 {
5849 if (p_mco > MAX_MCO)
5850 p_mco = MAX_MCO;
5851 else if (p_mco < 0)
5852 p_mco = 0;
5853 screenclear(); /* will re-allocate the screen */
5854 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 else if (pp == &curbuf->b_p_iminsert)
5857 {
5858 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
5859 {
5860 errmsg = e_invarg;
5861 curbuf->b_p_iminsert = B_IMODE_NONE;
5862 }
5863 p_iminsert = curbuf->b_p_iminsert;
5864 if (termcap_active) /* don't do this in the alternate screen */
5865 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005866#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 /* Show/unshow value of 'keymap' in status lines. */
5868 status_redraw_curbuf();
5869#endif
5870 }
5871
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005872#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
5873 /* 'imstyle' */
5874 else if (pp == &p_imst)
5875 {
5876 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
5877 errmsg = e_invarg;
5878 }
5879#endif
5880
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005881 else if (pp == &p_window)
5882 {
5883 if (p_window < 1)
5884 p_window = 1;
5885 else if (p_window >= Rows)
5886 p_window = Rows - 1;
5887 }
5888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 else if (pp == &curbuf->b_p_imsearch)
5890 {
5891 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
5892 {
5893 errmsg = e_invarg;
5894 curbuf->b_p_imsearch = B_IMODE_NONE;
5895 }
5896 p_imsearch = curbuf->b_p_imsearch;
5897 }
5898
5899#ifdef FEAT_TITLE
5900 /* if 'titlelen' has changed, redraw the title */
5901 else if (pp == &p_titlelen)
5902 {
5903 if (p_titlelen < 0)
5904 {
5905 errmsg = e_positive;
5906 p_titlelen = 85;
5907 }
5908 if (starting != NO_SCREEN && old_value != p_titlelen)
5909 need_maketitle = TRUE;
5910 }
5911#endif
5912
5913 /* if p_ch changed value, change the command line height */
5914 else if (pp == &p_ch)
5915 {
5916 if (p_ch < 1)
5917 {
5918 errmsg = e_positive;
5919 p_ch = 1;
5920 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00005921 if (p_ch > Rows - min_rows() + 1)
5922 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
5924 /* Only compute the new window layout when startup has been
5925 * completed. Otherwise the frame sizes may be wrong. */
5926 if (p_ch != old_value && full_screen
5927#ifdef FEAT_GUI
5928 && !gui.starting
5929#endif
5930 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005931 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
5933
5934 /* when 'updatecount' changes from zero to non-zero, open swap files */
5935 else if (pp == &p_uc)
5936 {
5937 if (p_uc < 0)
5938 {
5939 errmsg = e_positive;
5940 p_uc = 100;
5941 }
5942 if (p_uc && !old_value)
5943 ml_open_files();
5944 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005945#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005946 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005947 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005948 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005949 {
5950 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005951 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005952 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005953 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005954 {
5955 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005956 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005957 }
5958 }
5959#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00005960#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00005961 else if (pp == &p_mzq)
5962 mzvim_reset_timer();
5963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005965#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
5966 /* 'pyxversion' */
5967 else if (pp == &p_pyx)
5968 {
5969 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
5970 errmsg = e_invarg;
5971 }
5972#endif
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 /* sync undo before 'undolevels' changes */
5975 else if (pp == &p_ul)
5976 {
5977 /* use the old value, otherwise u_sync() may not work properly */
5978 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005979 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 p_ul = value;
5981 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005982 else if (pp == &curbuf->b_p_ul)
5983 {
5984 /* use the old value, otherwise u_sync() may not work properly */
5985 curbuf->b_p_ul = old_value;
5986 u_sync(TRUE);
5987 curbuf->b_p_ul = value;
5988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005990#ifdef FEAT_LINEBREAK
5991 /* 'numberwidth' must be positive */
5992 else if (pp == &curwin->w_p_nuw)
5993 {
5994 if (curwin->w_p_nuw < 1)
5995 {
5996 errmsg = e_positive;
5997 curwin->w_p_nuw = 1;
5998 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02005999 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006000 {
6001 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02006002 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006003 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02006004 curwin->w_nrwidth_line_count = 0; /* trigger a redraw */
Bram Moolenaar592e0a22004-07-03 16:05:59 +00006005 }
6006#endif
6007
Bram Moolenaar1a384422010-07-14 19:53:30 +02006008 else if (pp == &curbuf->b_p_tw)
6009 {
6010 if (curbuf->b_p_tw < 0)
6011 {
6012 errmsg = e_positive;
6013 curbuf->b_p_tw = 0;
6014 }
6015#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02006016 {
6017 win_T *wp;
6018 tabpage_T *tp;
6019
6020 FOR_ALL_TAB_WINDOWS(tp, wp)
6021 check_colorcolumn(wp);
6022 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02006023#endif
6024 }
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 /*
6027 * Check the bounds for numeric options here
6028 */
6029 if (Rows < min_rows() && full_screen)
6030 {
6031 if (errbuf != NULL)
6032 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006033 vim_snprintf((char *)errbuf, errbuflen,
6034 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 errmsg = errbuf;
6036 }
6037 Rows = min_rows();
6038 }
6039 if (Columns < MIN_COLUMNS && full_screen)
6040 {
6041 if (errbuf != NULL)
6042 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00006043 vim_snprintf((char *)errbuf, errbuflen,
6044 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 errmsg = errbuf;
6046 }
6047 Columns = MIN_COLUMNS;
6048 }
Bram Moolenaare057d402013-06-30 17:51:51 +02006049 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 /*
6052 * If the screen (shell) height has been changed, assume it is the
6053 * physical screenheight.
6054 */
6055 if (old_Rows != Rows || old_Columns != Columns)
6056 {
6057 /* Changing the screen size is not allowed while updating the screen. */
6058 if (updating_screen)
6059 *pp = old_value;
6060 else if (full_screen
6061#ifdef FEAT_GUI
6062 && !gui.starting
6063#endif
6064 )
6065 set_shellsize((int)Columns, (int)Rows, TRUE);
6066 else
6067 {
6068 /* Postpone the resizing; check the size and cmdline position for
6069 * messages. */
6070 check_shellsize();
6071 if (cmdline_row > Rows - p_ch && Rows > p_ch)
6072 cmdline_row = Rows - p_ch;
6073 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00006074 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006075 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (curbuf->b_p_ts <= 0)
6079 {
6080 errmsg = e_positive;
6081 curbuf->b_p_ts = 8;
6082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (p_tm < 0)
6084 {
6085 errmsg = e_positive;
6086 p_tm = 0;
6087 }
6088 if ((curwin->w_p_scr <= 0
6089 || (curwin->w_p_scr > curwin->w_height
6090 && curwin->w_height > 0))
6091 && full_screen)
6092 {
6093 if (pp == &(curwin->w_p_scr))
6094 {
6095 if (curwin->w_p_scr != 0)
6096 errmsg = e_scroll;
6097 win_comp_scroll(curwin);
6098 }
6099 /* If 'scroll' became invalid because of a side effect silently adjust
6100 * it. */
6101 else if (curwin->w_p_scr <= 0)
6102 curwin->w_p_scr = 1;
6103 else /* curwin->w_p_scr > curwin->w_height */
6104 curwin->w_p_scr = curwin->w_height;
6105 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00006106 if (p_hi < 0)
6107 {
6108 errmsg = e_positive;
6109 p_hi = 0;
6110 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02006111 else if (p_hi > 10000)
6112 {
6113 errmsg = e_invarg;
6114 p_hi = 10000;
6115 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006116 if (p_re < 0 || p_re > 2)
6117 {
6118 errmsg = e_invarg;
6119 p_re = 0;
6120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 if (p_report < 0)
6122 {
6123 errmsg = e_positive;
6124 p_report = 1;
6125 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00006126 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
6128 if (Rows != old_Rows) /* Rows changed, just adjust p_sj */
6129 p_sj = Rows / 2;
6130 else
6131 {
6132 errmsg = e_scroll;
6133 p_sj = 1;
6134 }
6135 }
6136 if (p_so < 0 && full_screen)
6137 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006138 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 p_so = 0;
6140 }
6141 if (p_siso < 0 && full_screen)
6142 {
6143 errmsg = e_positive;
6144 p_siso = 0;
6145 }
6146#ifdef FEAT_CMDWIN
6147 if (p_cwh < 1)
6148 {
6149 errmsg = e_positive;
6150 p_cwh = 1;
6151 }
6152#endif
6153 if (p_ut < 0)
6154 {
6155 errmsg = e_positive;
6156 p_ut = 2000;
6157 }
6158 if (p_ss < 0)
6159 {
6160 errmsg = e_positive;
6161 p_ss = 0;
6162 }
6163
6164 /* May set global value for local option. */
6165 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
6166 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
6167
6168 options[opt_idx].flags |= P_WAS_SET;
6169
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006170#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02006171 // Don't do this while starting up, failure or recursively.
6172 if (!starting && errmsg == NULL && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02006173 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02006174 char_u buf_old[11], buf_old_global[11], buf_new[11], buf_type[7];
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02006175 vim_snprintf((char *)buf_old, 10, "%ld", old_value);
Bram Moolenaard7c96872019-06-15 17:12:48 +02006176 vim_snprintf((char *)buf_old_global, 10, "%ld", old_global_value);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02006177 vim_snprintf((char *)buf_new, 10, "%ld", value);
6178 vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02006179 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
6180 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
6181 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02006182 if (opt_flags & OPT_LOCAL)
6183 {
6184 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
6185 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
6186 }
6187 if (opt_flags & OPT_GLOBAL)
6188 {
6189 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
6190 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
6191 }
6192 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
6193 {
6194 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
6195 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
6196 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
6197 }
6198 if (opt_flags & OPT_MODELINE)
6199 {
6200 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
6201 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
6202 }
6203 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
6204 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02006205 reset_v_option_vars();
6206 }
6207#endif
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 comp_col(); /* in case 'columns' or 'ls' changed */
Bram Moolenaar913077c2012-03-28 19:59:04 +02006210 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01006211 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02006212 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 check_redraw(options[opt_idx].flags);
6214
6215 return errmsg;
6216}
6217
6218/*
6219 * Called after an option changed: check if something needs to be redrawn.
6220 */
6221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006222check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 /* Careful: P_RCLR and P_RALL are a combination of other P_ flags */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006225 int doclear = (flags & P_RCLR) == P_RCLR;
6226 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if ((flags & P_RSTAT) || all) /* mark all status lines dirty */
6229 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
6232 changed_window_setting();
6233 if (flags & P_RBUF)
6234 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01006235 if (flags & P_RWINONLY)
6236 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006237 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 redraw_all_later(CLEAR);
6239 else if (all)
6240 redraw_all_later(NOT_VALID);
6241}
6242
6243/*
6244 * Find index for option 'arg'.
6245 * Return -1 if not found.
6246 */
6247 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006248findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 int opt_idx;
6251 char *s, *p;
6252 static short quick_tab[27] = {0, 0}; /* quick access table */
6253 int is_term_opt;
6254
6255 /*
6256 * For first call: Initialize the quick-access table.
6257 * It contains the index for the first option that starts with a certain
6258 * letter. There are 26 letters, plus the first "t_" option.
6259 */
6260 if (quick_tab[1] == 0)
6261 {
6262 p = options[0].fullname;
6263 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
6264 {
6265 if (s[0] != p[0])
6266 {
6267 if (s[0] == 't' && s[1] == '_')
6268 quick_tab[26] = opt_idx;
6269 else
6270 quick_tab[CharOrdLow(s[0])] = opt_idx;
6271 }
6272 p = s;
6273 }
6274 }
6275
6276 /*
6277 * Check for name starting with an illegal character.
6278 */
6279#ifdef EBCDIC
6280 if (!islower(arg[0]))
6281#else
6282 if (arg[0] < 'a' || arg[0] > 'z')
6283#endif
6284 return -1;
6285
6286 is_term_opt = (arg[0] == 't' && arg[1] == '_');
6287 if (is_term_opt)
6288 opt_idx = quick_tab[26];
6289 else
6290 opt_idx = quick_tab[CharOrdLow(arg[0])];
6291 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
6292 {
6293 if (STRCMP(arg, s) == 0) /* match full name */
6294 break;
6295 }
6296 if (s == NULL && !is_term_opt)
6297 {
6298 opt_idx = quick_tab[CharOrdLow(arg[0])];
6299 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
6300 {
6301 s = options[opt_idx].shortname;
6302 if (s != NULL && STRCMP(arg, s) == 0) /* match short name */
6303 break;
6304 s = NULL;
6305 }
6306 }
6307 if (s == NULL)
6308 opt_idx = -1;
6309 return opt_idx;
6310}
6311
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006312#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313/*
6314 * Get the value for an option.
6315 *
6316 * Returns:
6317 * Number or Toggle option: 1, *numval gets value.
6318 * String option: 0, *stringval gets allocated string.
6319 * Hidden Number or Toggle option: -1.
6320 * hidden String option: -2.
6321 * unknown option: -3.
6322 */
6323 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006324get_option_value(
6325 char_u *name,
6326 long *numval,
6327 char_u **stringval, /* NULL when only checking existence */
6328 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 int opt_idx;
6331 char_u *varp;
6332
6333 opt_idx = findoption(name);
6334 if (opt_idx < 0) /* unknown option */
Bram Moolenaare353c402017-02-04 19:49:16 +01006335 {
6336 int key;
6337
6338 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02006339 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01006340 {
6341 char_u key_name[2];
6342 char_u *p;
6343
6344 if (key < 0)
6345 {
6346 key_name[0] = KEY2TERMCAP0(key);
6347 key_name[1] = KEY2TERMCAP1(key);
6348 }
6349 else
6350 {
6351 key_name[0] = KS_KEY;
6352 key_name[1] = (key & 0xff);
6353 }
6354 p = find_termcode(key_name);
6355 if (p != NULL)
6356 {
6357 if (stringval != NULL)
6358 *stringval = vim_strsave(p);
6359 return 0;
6360 }
6361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 return -3;
Bram Moolenaare353c402017-02-04 19:49:16 +01006363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
6366
6367 if (options[opt_idx].flags & P_STRING)
6368 {
6369 if (varp == NULL) /* hidden option */
6370 return -2;
6371 if (stringval != NULL)
6372 {
6373#ifdef FEAT_CRYPT
6374 /* never return the value of the crypt key */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006375 if ((char_u **)varp == &curbuf->b_p_key
6376 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 *stringval = vim_strsave((char_u *)"*****");
6378 else
6379#endif
6380 *stringval = vim_strsave(*(char_u **)(varp));
6381 }
6382 return 0;
6383 }
6384
6385 if (varp == NULL) /* hidden option */
6386 return -1;
6387 if (options[opt_idx].flags & P_NUM)
6388 *numval = *(long *)varp;
6389 else
6390 {
6391 /* Special case: 'modified' is b_changed, but we also want to consider
6392 * it set when 'ff' or 'fenc' changed. */
6393 if ((int *)varp == &curbuf->b_changed)
6394 *numval = curbufIsChanged();
6395 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02006396 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 }
6398 return 1;
6399}
6400#endif
6401
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006402#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006403/*
6404 * Returns the option attributes and its value. Unlike the above function it
6405 * will return either global value or local value of the option depending on
6406 * what was requested, but it will never return global value if it was
6407 * requested to return local one and vice versa. Neither it will return
6408 * buffer-local value if it was requested to return window-local one.
6409 *
6410 * Pretends that option is absent if it is not present in the requested scope
6411 * (i.e. has no global, window-local or buffer-local value depending on
6412 * opt_type). Uses
6413 *
6414 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02006415 * 0 hidden or unknown option, also option that does not have requested
6416 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006417 * see SOPT_* in vim.h for other flags
6418 *
6419 * Possible opt_type values: see SREQ_* in vim.h
6420 */
6421 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006422get_option_value_strict(
6423 char_u *name,
6424 long *numval,
6425 char_u **stringval, /* NULL when only obtaining attributes */
6426 int opt_type,
6427 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006428{
6429 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02006430 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006431 struct vimoption *p;
6432 int r = 0;
6433
6434 opt_idx = findoption(name);
6435 if (opt_idx < 0)
6436 return 0;
6437
6438 p = &(options[opt_idx]);
6439
6440 /* Hidden option */
6441 if (p->var == NULL)
6442 return 0;
6443
6444 if (p->flags & P_BOOL)
6445 r |= SOPT_BOOL;
6446 else if (p->flags & P_NUM)
6447 r |= SOPT_NUM;
6448 else if (p->flags & P_STRING)
6449 r |= SOPT_STRING;
6450
6451 if (p->indir == PV_NONE)
6452 {
6453 if (opt_type == SREQ_GLOBAL)
6454 r |= SOPT_GLOBAL;
6455 else
6456 return 0; /* Did not request global-only option */
6457 }
6458 else
6459 {
6460 if (p->indir & PV_BOTH)
6461 r |= SOPT_GLOBAL;
6462 else if (opt_type == SREQ_GLOBAL)
6463 return 0; /* Requested global option */
6464
6465 if (p->indir & PV_WIN)
6466 {
6467 if (opt_type == SREQ_BUF)
6468 return 0; /* Did not request window-local option */
6469 else
6470 r |= SOPT_WIN;
6471 }
6472 else if (p->indir & PV_BUF)
6473 {
6474 if (opt_type == SREQ_WIN)
6475 return 0; /* Did not request buffer-local option */
6476 else
6477 r |= SOPT_BUF;
6478 }
6479 }
6480
6481 if (stringval == NULL)
6482 return r;
6483
6484 if (opt_type == SREQ_GLOBAL)
6485 varp = p->var;
6486 else
6487 {
6488 if (opt_type == SREQ_BUF)
6489 {
6490 /* Special case: 'modified' is b_changed, but we also want to
6491 * consider it set when 'ff' or 'fenc' changed. */
6492 if (p->indir == PV_MOD)
6493 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02006494 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006495 varp = NULL;
6496 }
6497#ifdef FEAT_CRYPT
6498 else if (p->indir == PV_KEY)
6499 {
6500 /* never return the value of the crypt key */
6501 *stringval = NULL;
6502 varp = NULL;
6503 }
6504#endif
6505 else
6506 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02006507 buf_T *save_curbuf = curbuf;
6508
6509 // only getting a pointer, no need to use aucmd_prepbuf()
6510 curbuf = (buf_T *)from;
6511 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006512 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02006513 curbuf = save_curbuf;
6514 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006515 }
6516 }
6517 else if (opt_type == SREQ_WIN)
6518 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02006519 win_T *save_curwin = curwin;
6520
6521 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006522 curbuf = curwin->w_buffer;
6523 varp = get_varp(p);
6524 curwin = save_curwin;
6525 curbuf = curwin->w_buffer;
6526 }
6527 if (varp == p->var)
6528 return (r | SOPT_UNSET);
6529 }
6530
6531 if (varp != NULL)
6532 {
6533 if (p->flags & P_STRING)
6534 *stringval = vim_strsave(*(char_u **)(varp));
6535 else if (p->flags & P_NUM)
6536 *numval = *(long *) varp;
6537 else
6538 *numval = *(int *)varp;
6539 }
6540
6541 return r;
6542}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006543
6544/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006545 * Iterate over options. First argument is a pointer to a pointer to a
6546 * structure inside options[] array, second is option type like in the above
6547 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006548 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006549 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006550 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006551 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006552 * first argument to NULL.
6553 *
6554 * Returns full option name for current option on each call.
6555 */
6556 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006557option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006558{
6559 struct vimoption *ret = NULL;
6560 do
6561 {
6562 if (*option == NULL)
6563 *option = (void *) options;
6564 else if (((struct vimoption *) (*option))->fullname == NULL)
6565 {
6566 *option = NULL;
6567 return NULL;
6568 }
6569 else
6570 *option = (void *) (((struct vimoption *) (*option)) + 1);
6571
6572 ret = ((struct vimoption *) (*option));
6573
6574 /* Hidden option */
6575 if (ret->var == NULL)
6576 {
6577 ret = NULL;
6578 continue;
6579 }
6580
6581 switch (opt_type)
6582 {
6583 case SREQ_GLOBAL:
6584 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
6585 ret = NULL;
6586 break;
6587 case SREQ_BUF:
6588 if (!(ret->indir & PV_BUF))
6589 ret = NULL;
6590 break;
6591 case SREQ_WIN:
6592 if (!(ret->indir & PV_WIN))
6593 ret = NULL;
6594 break;
6595 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01006596 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01006597 return NULL;
6598 }
6599 }
6600 while (ret == NULL);
6601
6602 return (char_u *)ret->fullname;
6603}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02006604#endif
6605
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606/*
6607 * Set the value of option "name".
6608 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02006609 *
6610 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006612 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006613set_option_value(
6614 char_u *name,
6615 long number,
6616 char_u *string,
6617 int opt_flags) /* OPT_LOCAL or 0 (both) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 int opt_idx;
6620 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006621 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
6623 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006624 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01006625 {
6626 int key;
6627
6628 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02006629 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01006630 {
6631 char_u key_name[2];
6632
6633 if (key < 0)
6634 {
6635 key_name[0] = KEY2TERMCAP0(key);
6636 key_name[1] = KEY2TERMCAP1(key);
6637 }
6638 else
6639 {
6640 key_name[0] = KS_KEY;
6641 key_name[1] = (key & 0xff);
6642 }
6643 add_termcode(key_name, string, FALSE);
6644 if (full_screen)
6645 ttest(FALSE);
6646 redraw_all_later(CLEAR);
6647 return NULL;
6648 }
6649
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006650 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01006651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 else
6653 {
6654 flags = options[opt_idx].flags;
6655#ifdef HAVE_SANDBOX
6656 /* Disallow changing some options in the sandbox */
6657 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006659 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02006660 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006663 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02006664 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 else
6666 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00006667 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (varp != NULL) /* hidden option is not changed */
6669 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00006670 if (number == 0 && string != NULL)
6671 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00006672 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00006673
6674 /* Either we are given a string or we are setting option
6675 * to zero. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00006676 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00006677 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00006678 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00006679 {
6680 /* There's another character after zeros or the string
6681 * is empty. In both cases, we are trying to set a
6682 * num option using a string. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006683 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00006684 name, string);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02006685 return NULL; /* do nothing as we hit an error */
Bram Moolenaar96bb6212007-06-19 18:52:53 +00006686
6687 }
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02006690 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00006691 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02006693 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006694 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 }
6696 }
6697 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02006698 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699}
6700
6701/*
6702 * Get the terminal code for a terminal option.
6703 * Returns NULL when not found.
6704 */
6705 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006706get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 int opt_idx;
6709 char_u *varp;
6710
6711 if (tname[0] != 't' || tname[1] != '_' ||
6712 tname[2] == NUL || tname[3] == NUL)
6713 return NULL;
6714 if ((opt_idx = findoption(tname)) >= 0)
6715 {
6716 varp = get_varp(&(options[opt_idx]));
6717 if (varp != NULL)
6718 varp = *(char_u **)(varp);
6719 return varp;
6720 }
6721 return find_termcode(tname + 2);
6722}
6723
6724 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006725get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 int i;
6728
6729 i = findoption((char_u *)"hl");
6730 if (i >= 0)
6731 return options[i].def_val[VI_DEFAULT];
6732 return (char_u *)NULL;
6733}
6734
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006735 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006736get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006737{
6738 int i;
6739
6740 i = findoption((char_u *)"enc");
6741 if (i >= 0)
6742 return options[i].def_val[VI_DEFAULT];
6743 return (char_u *)NULL;
6744}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746/*
6747 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02006748 * When "has_lt" is true there is a '<' before "*arg_arg".
6749 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 */
6751 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02006752find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02006754 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02006756 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758 /*
6759 * Don't use get_special_key_code() for t_xx, we don't want it to call
6760 * add_termcap_entry().
6761 */
6762 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
6763 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02006764 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 {
6766 --arg; /* put arg at the '<' */
6767 modifiers = 0;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02006768 key = find_special_key(&arg, &modifiers, TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 if (modifiers) /* can't handle modifiers here */
6770 key = 0;
6771 }
6772 return key;
6773}
6774
6775/*
6776 * if 'all' == 0: show changed options
6777 * if 'all' == 1: show all normal options
6778 * if 'all' == 2: show all terminal options
6779 */
6780 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006781showoptions(
6782 int all,
6783 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 struct vimoption *p;
6786 int col;
6787 int isterm;
6788 char_u *varp;
6789 struct vimoption **items;
6790 int item_count;
6791 int run;
6792 int row, rows;
6793 int cols;
6794 int i;
6795 int len;
6796
6797#define INC 20
6798#define GAP 3
6799
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006800 items = ALLOC_MULT(struct vimoption *, PARAM_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (items == NULL)
6802 return;
6803
6804 /* Highlight title */
6805 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006806 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006808 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006810 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006812 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 /*
6815 * do the loop two times:
6816 * 1. display the short items
6817 * 2. display the long items (only strings and numbers)
6818 */
6819 for (run = 1; run <= 2 && !got_int; ++run)
6820 {
6821 /*
6822 * collect the items in items[]
6823 */
6824 item_count = 0;
6825 for (p = &options[0]; p->fullname != NULL; p++)
6826 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02006827 // apply :filter /pat/
6828 if (message_filtered((char_u *) p->fullname))
6829 continue;
6830
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 varp = NULL;
6832 isterm = istermoption(p);
6833 if (opt_flags != 0)
6834 {
6835 if (p->indir != PV_NONE && !isterm)
6836 varp = get_varp_scope(p, opt_flags);
6837 }
6838 else
6839 varp = get_varp(p);
6840 if (varp != NULL
6841 && ((all == 2 && isterm)
6842 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02006843 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
6845 if (p->flags & P_BOOL)
6846 len = 1; /* a toggle option fits always */
6847 else
6848 {
6849 option_value2string(p, opt_flags);
6850 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
6851 }
6852 if ((len <= INC - GAP && run == 1) ||
6853 (len > INC - GAP && run == 2))
6854 items[item_count++] = p;
6855 }
6856 }
6857
6858 /*
6859 * display the items
6860 */
6861 if (run == 1)
6862 {
6863 cols = (Columns + GAP - 3) / INC;
6864 if (cols == 0)
6865 cols = 1;
6866 rows = (item_count + cols - 1) / cols;
6867 }
6868 else /* run == 2 */
6869 rows = item_count;
6870 for (row = 0; row < rows && !got_int; ++row)
6871 {
6872 msg_putchar('\n'); /* go to next line */
6873 if (got_int) /* 'q' typed in more */
6874 break;
6875 col = 0;
6876 for (i = row; i < item_count; i += rows)
6877 {
6878 msg_col = col; /* make columns */
6879 showoneopt(items[i], opt_flags);
6880 col += INC;
6881 }
6882 out_flush();
6883 ui_breakcheck();
6884 }
6885 }
6886 vim_free(items);
6887}
6888
6889/*
6890 * Return TRUE if option "p" has its default value.
6891 */
6892 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02006893optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 int dvi;
6896
6897 if (varp == NULL)
6898 return TRUE; /* hidden option is always at default */
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02006899 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006901 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (p->flags & P_BOOL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006903 /* the cast to long is required for Manx C, long_i is
6904 * needed for MSVC */
6905 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 /* P_STRING */
6907 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
6908}
6909
6910/*
6911 * showoneopt: show the value of one option
6912 * must not be called with a hidden option!
6913 */
6914 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006915showoneopt(
6916 struct vimoption *p,
6917 int opt_flags) /* OPT_LOCAL or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006919 char_u *varp;
6920 int save_silent = silent_mode;
6921
6922 silent_mode = FALSE;
6923 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
6925 varp = get_varp_scope(p, opt_flags);
6926
6927 /* for 'modified' we also need to check if 'ff' or 'fenc' changed. */
6928 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
6929 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01006930 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006932 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006934 msg_puts(" ");
6935 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (!(p->flags & P_BOOL))
6937 {
6938 msg_putchar('=');
6939 /* put value string in NameBuff */
6940 option_value2string(p, opt_flags);
6941 msg_outtrans(NameBuff);
6942 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006943
6944 silent_mode = save_silent;
6945 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946}
6947
6948/*
6949 * Write modified options as ":set" commands to a file.
6950 *
6951 * There are three values for "opt_flags":
6952 * OPT_GLOBAL: Write global option values and fresh values of
6953 * buffer-local options (used for start of a session
6954 * file).
6955 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
6956 * curwin (used for a vimrc file).
6957 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
6958 * and local values for window-local options of
6959 * curwin. Local values are also written when at the
6960 * default value, because a modeline or autocommand
6961 * may have set them when doing ":edit file" and the
6962 * user has set them back at the default or fresh
6963 * value.
6964 * When "local_only" is TRUE, don't write fresh
6965 * values, only local values (for ":mkview").
6966 * (fresh value = value used for a new buffer or window for a local option).
6967 *
6968 * Return FAIL on error, OK otherwise.
6969 */
6970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006971makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 struct vimoption *p;
6974 char_u *varp; /* currently used value */
6975 char_u *varp_fresh; /* local value */
6976 char_u *varp_local = NULL; /* fresh value */
6977 char *cmd;
6978 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00006979 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981 /*
6982 * The options that don't have a default (terminal name, columns, lines)
6983 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00006984 * Do the loop over "options[]" twice: once for options with the
6985 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00006987 for (pri = 1; pri >= 0; --pri)
6988 {
6989 for (p = &options[0]; !istermoption(p); p++)
6990 if (!(p->flags & P_NO_MKRC)
6991 && !istermoption(p)
6992 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {
6994 /* skip global option when only doing locals */
6995 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
6996 continue;
6997
6998 /* Do not store options like 'bufhidden' and 'syntax' in a vimrc
6999 * file, they are always buffer-specific. */
7000 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
7001 continue;
7002
7003 /* Global values are only written when not at the default value. */
7004 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02007005 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 continue;
7007
7008 round = 2;
7009 if (p->indir != PV_NONE)
7010 {
7011 if (p->var == VAR_WIN)
7012 {
7013 /* skip window-local option when only doing globals */
7014 if (!(opt_flags & OPT_LOCAL))
7015 continue;
7016 /* When fresh value of window-local option is not at the
7017 * default, need to write it too. */
7018 if (!(opt_flags & OPT_GLOBAL) && !local_only)
7019 {
7020 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02007021 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
7023 round = 1;
7024 varp_local = varp;
7025 varp = varp_fresh;
7026 }
7027 }
7028 }
7029 }
7030
7031 /* Round 1: fresh value for window-local options.
7032 * Round 2: other values */
7033 for ( ; round <= 2; varp = varp_local, ++round)
7034 {
7035 if (round == 1 || (opt_flags & OPT_GLOBAL))
7036 cmd = "set";
7037 else
7038 cmd = "setlocal";
7039
7040 if (p->flags & P_BOOL)
7041 {
7042 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
7043 return FAIL;
7044 }
7045 else if (p->flags & P_NUM)
7046 {
7047 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
7048 return FAIL;
7049 }
7050 else /* P_STRING */
7051 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007052 int do_endif = FALSE;
7053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 /* Don't set 'syntax' and 'filetype' again if the value is
7055 * already right, avoids reloading the syntax file. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007056 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007057#if defined(FEAT_SYN_HL)
7058 p->indir == PV_SYN ||
7059#endif
7060 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {
7062 if (fprintf(fd, "if &%s != '%s'", p->fullname,
7063 *(char_u **)(varp)) < 0
7064 || put_eol(fd) < 0)
7065 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007066 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007069 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007071 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {
7073 if (put_line(fd, "endif") == FAIL)
7074 return FAIL;
7075 }
7076 }
7077 }
7078 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00007079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 return OK;
7081}
7082
7083#if defined(FEAT_FOLDING) || defined(PROTO)
7084/*
7085 * Generate set commands for the local fold options only. Used when
7086 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
7087 */
7088 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007089makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007091 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007093 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 == FAIL
7095# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007096 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007098 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 == FAIL
7100 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
7101 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
7102 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
7103 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
7104 )
7105 return FAIL;
7106
7107 return OK;
7108}
7109#endif
7110
7111 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007112put_setstring(
7113 FILE *fd,
7114 char *cmd,
7115 char *name,
7116 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007117 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
7119 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007120 char_u *buf = NULL;
7121 char_u *part = NULL;
7122 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123
7124 if (fprintf(fd, "%s %s=", cmd, name) < 0)
7125 return FAIL;
7126 if (*valuep != NULL)
7127 {
7128 /* Output 'pastetoggle' as key names. For other
7129 * options some characters have to be escaped with
7130 * CTRL-V or backslash */
7131 if (valuep == &p_pt)
7132 {
7133 s = *valuep;
7134 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00007135 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 return FAIL;
7137 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007138 // expand the option value, replace $HOME by ~
7139 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007141 int size = (int)STRLEN(*valuep) + 1;
7142
7143 // replace home directory in the whole option value into "buf"
7144 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02007145 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007146 goto fail;
7147 home_replace(NULL, *valuep, buf, size, FALSE);
7148
7149 // If the option value is longer than MAXPATHL, we need to append
7150 // earch comma separated part of the option separately, so that it
7151 // can be expanded when read back.
7152 if (size >= MAXPATHL && (flags & P_COMMA) != 0
7153 && vim_strchr(*valuep, ',') != NULL)
7154 {
7155 part = alloc(size);
7156 if (part == NULL)
7157 goto fail;
7158
7159 // write line break to clear the option, e.g. ':set rtp='
7160 if (put_eol(fd) == FAIL)
7161 goto fail;
7162
7163 p = buf;
7164 while (*p != NUL)
7165 {
7166 // for each comma separated option part, append value to
7167 // the option, :set rtp+=value
7168 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
7169 goto fail;
7170 (void)copy_option_part(&p, part, size, ",");
7171 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
7172 goto fail;
7173 }
7174 vim_free(buf);
7175 vim_free(part);
7176 return OK;
7177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02007179 {
7180 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02007182 }
7183 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
7185 else if (put_escstr(fd, *valuep, 2) == FAIL)
7186 return FAIL;
7187 }
7188 if (put_eol(fd) < 0)
7189 return FAIL;
7190 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01007191fail:
7192 vim_free(buf);
7193 vim_free(part);
7194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195}
7196
7197 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007198put_setnum(
7199 FILE *fd,
7200 char *cmd,
7201 char *name,
7202 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 long wc;
7205
7206 if (fprintf(fd, "%s %s=", cmd, name) < 0)
7207 return FAIL;
7208 if (wc_use_keyname((char_u *)valuep, &wc))
7209 {
7210 /* print 'wildchar' and 'wildcharm' as a key name */
7211 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
7212 return FAIL;
7213 }
7214 else if (fprintf(fd, "%ld", *valuep) < 0)
7215 return FAIL;
7216 if (put_eol(fd) < 0)
7217 return FAIL;
7218 return OK;
7219}
7220
7221 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007222put_setbool(
7223 FILE *fd,
7224 char *cmd,
7225 char *name,
7226 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
Bram Moolenaar893de922007-10-02 18:40:57 +00007228 if (value < 0) /* global/local option using global value */
7229 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
7231 || put_eol(fd) < 0)
7232 return FAIL;
7233 return OK;
7234}
7235
7236/*
7237 * Clear all the terminal options.
7238 * If the option has been allocated, free the memory.
7239 * Terminal options are never hidden or indirect.
7240 */
7241 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007242clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 /*
7245 * Reset a few things before clearing the old options. This may cause
7246 * outputting a few things that the terminal doesn't understand, but the
7247 * screen will be cleared later, so this is OK.
7248 */
7249#ifdef FEAT_MOUSE_TTY
7250 mch_setmouse(FALSE); /* switch mouse off */
7251#endif
7252#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02007253 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254#endif
7255#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
7256 /* When starting the GUI close the display opened for the clipboard.
7257 * After restoring the title, because that will need the display. */
7258 if (gui.starting)
7259 clear_xterm_clip();
7260#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007261 stoptermcap(); /* stop termcap mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007263 free_termoptions();
7264}
7265
7266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007267free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007268{
7269 struct vimoption *p;
7270
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02007271 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 if (istermoption(p))
7273 {
7274 if (p->flags & P_ALLOCED)
7275 free_string_option(*(char_u **)(p->var));
7276 if (p->flags & P_DEF_ALLOCED)
7277 free_string_option(p->def_val[VI_DEFAULT]);
7278 *(char_u **)(p->var) = empty_option;
7279 p->def_val[VI_DEFAULT] = empty_option;
7280 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02007281#ifdef FEAT_EVAL
7282 // remember where the option was cleared
7283 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
7284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286 clear_termcodes();
7287}
7288
7289/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00007290 * Free the string for one term option, if it was allocated.
7291 * Set the string to empty_option and clear allocated flag.
7292 * "var" points to the option value.
7293 */
7294 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007295free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00007296{
7297 struct vimoption *p;
7298
7299 for (p = &options[0]; p->fullname != NULL; p++)
7300 if (p->var == var)
7301 {
7302 if (p->flags & P_ALLOCED)
7303 free_string_option(*(char_u **)(p->var));
7304 *(char_u **)(p->var) = empty_option;
7305 p->flags &= ~P_ALLOCED;
7306 break;
7307 }
7308}
7309
7310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 * Set the terminal option defaults to the current value.
7312 * Used after setting the terminal name.
7313 */
7314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007315set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 struct vimoption *p;
7318
7319 for (p = &options[0]; p->fullname != NULL; p++)
7320 {
7321 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
7322 {
7323 if (p->flags & P_DEF_ALLOCED)
7324 {
7325 free_string_option(p->def_val[VI_DEFAULT]);
7326 p->flags &= ~P_DEF_ALLOCED;
7327 }
7328 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
7329 if (p->flags & P_ALLOCED)
7330 {
7331 p->flags |= P_DEF_ALLOCED;
7332 p->flags &= ~P_ALLOCED; /* don't free the value now */
7333 }
7334 }
7335 }
7336}
7337
7338/*
7339 * return TRUE if 'p' starts with 't_'
7340 */
7341 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007342istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 return (p->fullname[0] == 't' && p->fullname[1] == '_');
7345}
7346
Bram Moolenaar113e1072019-01-20 15:30:40 +01007347#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007349 * Unset local option value, similar to ":set opt<".
7350 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007351 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007352unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007353{
7354 struct vimoption *p;
7355 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007356 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007357
7358 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007359 if (opt_idx < 0)
7360 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007361 p = &(options[opt_idx]);
7362
7363 switch ((int)p->indir)
7364 {
7365 /* global option with local value: use local value if it's been set */
7366 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007367 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007368 break;
7369 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007370 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007371 break;
7372 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007373 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007374 break;
7375 case PV_AR:
7376 buf->b_p_ar = -1;
7377 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007378 case PV_BKC:
7379 clear_string_option(&buf->b_p_bkc);
7380 buf->b_bkc_flags = 0;
7381 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007382 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007383 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007384 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007385 case PV_TC:
7386 clear_string_option(&buf->b_p_tc);
7387 buf->b_tc_flags = 0;
7388 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01007389 case PV_SISO:
7390 curwin->w_p_siso = -1;
7391 break;
7392 case PV_SO:
7393 curwin->w_p_so = -1;
7394 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007395#ifdef FEAT_FIND_ID
7396 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007397 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007398 break;
7399 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007400 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007401 break;
7402#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007403 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007404 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007405 break;
7406 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007407 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007408 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007409 case PV_FP:
7410 clear_string_option(&buf->b_p_fp);
7411 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007412#ifdef FEAT_QUICKFIX
7413 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007414 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007415 break;
7416 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007417 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007418 break;
7419 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007420 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007421 break;
7422#endif
7423#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7424 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007425 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007426 break;
7427#endif
7428#if defined(FEAT_CRYPT)
7429 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007430 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007431 break;
7432#endif
7433#ifdef FEAT_STL_OPT
7434 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02007435 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007436 break;
7437#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007438 case PV_UL:
7439 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
7440 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007441#ifdef FEAT_LISP
7442 case PV_LW:
7443 clear_string_option(&buf->b_p_lw);
7444 break;
7445#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007446 case PV_MENC:
7447 clear_string_option(&buf->b_p_menc);
7448 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007449 }
7450}
Bram Moolenaar113e1072019-01-20 15:30:40 +01007451#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02007452
7453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 * Get pointer to option variable, depending on local or global scope.
7455 */
7456 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007457get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
7460 {
7461 if (p->var == VAR_WIN)
7462 return (char_u *)GLOBAL_WO(get_varp(p));
7463 return p->var;
7464 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00007465 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
7467 switch ((int)p->indir)
7468 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007469 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007471 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
7472 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
7473 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007475 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
7476 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
7477 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00007478 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007479 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007480 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01007481 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
7482 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007484 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
7485 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007487 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
7488 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007489#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7490 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
7491#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007492#if defined(FEAT_CRYPT)
7493 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
7494#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007495#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007496 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007497#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007498 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007499#ifdef FEAT_LISP
7500 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
7501#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007502 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007503 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505 return NULL; /* "cannot happen" */
7506 }
7507 return get_varp(p);
7508}
7509
7510/*
7511 * Get pointer to option variable.
7512 */
7513 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007514get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515{
7516 /* hidden option, always return NULL */
7517 if (p->var == NULL)
7518 return NULL;
7519
7520 switch ((int)p->indir)
7521 {
7522 case PV_NONE: return p->var;
7523
7524 /* global option with local value: use local value if it's been set */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007525 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007527 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007529 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00007531 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007533 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01007535 case PV_TC: return *curbuf->b_p_tc != NUL
7536 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007537 case PV_BKC: return *curbuf->b_p_bkc != NUL
7538 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01007539 case PV_SISO: return curwin->w_p_siso >= 0
7540 ? (char_u *)&(curwin->w_p_siso) : p->var;
7541 case PV_SO: return curwin->w_p_so >= 0
7542 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007544 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007546 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 ? (char_u *)&(curbuf->b_p_inc) : p->var;
7548#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007549 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007551 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01007553 case PV_FP: return *curbuf->b_p_fp != NUL
7554 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007556 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007558 case PV_GP: return *curbuf->b_p_gp != NUL
7559 ? (char_u *)&(curbuf->b_p_gp) : p->var;
7560 case PV_MP: return *curbuf->b_p_mp != NUL
7561 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00007563#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
7564 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
7565 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
7566#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02007567#if defined(FEAT_CRYPT)
7568 case PV_CM: return *curbuf->b_p_cm != NUL
7569 ? (char_u *)&(curbuf->b_p_cm) : p->var;
7570#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007571#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007572 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007573 ? (char_u *)&(curwin->w_p_stl) : p->var;
7574#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01007575 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
7576 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01007577#ifdef FEAT_LISP
7578 case PV_LW: return *curbuf->b_p_lw != NUL
7579 ? (char_u *)&(curbuf->b_p_lw) : p->var;
7580#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01007581 case PV_MENC: return *curbuf->b_p_menc != NUL
7582 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583#ifdef FEAT_ARABIC
7584 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
7585#endif
7586 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007587#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007588 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
7589#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007590#ifdef FEAT_SYN_HL
7591 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
7592 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02007593 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02007594 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596#ifdef FEAT_DIFF
7597 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
7598#endif
7599#ifdef FEAT_FOLDING
7600 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
7601 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
7602 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
7603 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
7604 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
7605 case PV_FML: return (char_u *)&(curwin->w_p_fml);
7606 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
7607# ifdef FEAT_EVAL
7608 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
7609 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
7610# endif
7611 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
7612#endif
7613 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02007614 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00007615#ifdef FEAT_LINEBREAK
7616 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
7617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007619 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007620#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
7622#endif
7623#ifdef FEAT_RIGHTLEFT
7624 case PV_RL: return (char_u *)&(curwin->w_p_rl);
7625 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
7626#endif
7627 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
7628 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
7629#ifdef FEAT_LINEBREAK
7630 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007631 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
7632 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007634 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007636 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007637#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007638 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
7639 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
7640#endif
7641#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007642 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
7643 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
7644 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
7647 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
7648 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
7651 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
7653 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
7654#ifdef FEAT_CINDENT
7655 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
7656 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
7657 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
7658#endif
7659#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
7660 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
7661#endif
7662#ifdef FEAT_COMMENTS
7663 case PV_COM: return (char_u *)&(curbuf->b_p_com);
7664#endif
7665#ifdef FEAT_FOLDING
7666 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
7667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02007669#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02007670 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007672#ifdef FEAT_COMPL_FUNC
7673 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00007674 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007675#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02007676#ifdef FEAT_EVAL
7677 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
7678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007680 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00007686 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
7688 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
7689 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
7690 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
7691#ifdef FEAT_FIND_ID
7692# ifdef FEAT_EVAL
7693 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
7694# endif
7695#endif
7696#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
7697 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
7698 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
7699#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007700#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007701 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
7702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703#ifdef FEAT_CRYPT
7704 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
7705#endif
7706#ifdef FEAT_LISP
7707 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
7708#endif
7709 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
7710 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
7711 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
7712 case PV_MOD: return (char_u *)&(curbuf->b_changed);
7713 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007715#ifdef FEAT_TEXTOBJ
7716 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
7717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
7719#ifdef FEAT_SMARTINDENT
7720 case PV_SI: return (char_u *)&(curbuf->b_p_si);
7721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
7724#ifdef FEAT_SEARCHPATH
7725 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
7726#endif
7727 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
7728#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00007729 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007730 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
7731#endif
7732#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02007733 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
7734 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
7735 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736#endif
7737 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
7738 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
7739 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
7740 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007741#ifdef FEAT_PERSISTENT_UNDO
7742 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
7743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
7745#ifdef FEAT_KEYMAP
7746 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
7747#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007748#ifdef FEAT_SIGNS
7749 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
7750#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02007751#ifdef FEAT_VARTABS
7752 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
7753 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
7754#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007755 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757 /* always return a valid pointer to avoid a crash! */
7758 return (char_u *)&(curbuf->b_p_wm);
7759}
7760
7761/*
7762 * Get the value of 'equalprg', either the buffer-local one or the global one.
7763 */
7764 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007765get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 if (*curbuf->b_p_ep == NUL)
7768 return p_ep;
7769 return curbuf->b_p_ep;
7770}
7771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772/*
7773 * Copy options from one window to another.
7774 * Used when splitting a window.
7775 */
7776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007777win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
7780 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02007781#if defined(FEAT_LINEBREAK)
7782 briopt_check(wp_to);
7783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785
7786/*
7787 * Copy the options from one winopt_T to another.
7788 * Doesn't free the old option values in "to", use clear_winopt() for that.
7789 * The 'scroll' option is not copied, because it depends on the window height.
7790 * The 'previewwindow' option is reset, there can be only one preview window.
7791 */
7792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007793copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795#ifdef FEAT_ARABIC
7796 to->wo_arab = from->wo_arab;
7797#endif
7798 to->wo_list = from->wo_list;
7799 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02007800 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00007801#ifdef FEAT_LINEBREAK
7802 to->wo_nuw = from->wo_nuw;
7803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804#ifdef FEAT_RIGHTLEFT
7805 to->wo_rl = from->wo_rl;
7806 to->wo_rlc = vim_strsave(from->wo_rlc);
7807#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007808#ifdef FEAT_STL_OPT
7809 to->wo_stl = vim_strsave(from->wo_stl);
7810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007812#ifdef FEAT_DIFF
7813 to->wo_wrap_save = from->wo_wrap_save;
7814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815#ifdef FEAT_LINEBREAK
7816 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02007817 to->wo_bri = from->wo_bri;
7818 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007820 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007822 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01007823 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007824 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007825#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00007826 to->wo_spell = from->wo_spell;
7827#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007828#ifdef FEAT_SYN_HL
7829 to->wo_cuc = from->wo_cuc;
7830 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02007831 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02007832 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834#ifdef FEAT_DIFF
7835 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007836 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007838#ifdef FEAT_CONCEAL
7839 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02007840 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007841#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007842#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007843 to->wo_twk = vim_strsave(from->wo_twk);
7844 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846#ifdef FEAT_FOLDING
7847 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007848 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007850 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 to->wo_fdi = vim_strsave(from->wo_fdi);
7852 to->wo_fml = from->wo_fml;
7853 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02007854 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02007856 to->wo_fdm_save = from->wo_diff_saved
7857 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 to->wo_fdn = from->wo_fdn;
7859# ifdef FEAT_EVAL
7860 to->wo_fde = vim_strsave(from->wo_fde);
7861 to->wo_fdt = vim_strsave(from->wo_fdt);
7862# endif
7863 to->wo_fmr = vim_strsave(from->wo_fmr);
7864#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007865#ifdef FEAT_SIGNS
7866 to->wo_scl = vim_strsave(from->wo_scl);
7867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 check_winopt(to); /* don't want NULL pointers */
7869}
7870
7871/*
7872 * Check string options in a window for a NULL value.
7873 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02007874 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007875check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
7877 check_winopt(&win->w_onebuf_opt);
7878 check_winopt(&win->w_allbuf_opt);
7879}
7880
7881/*
7882 * Check for NULL pointers in a winopt_T and replace them with empty_option.
7883 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02007884 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007885check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887#ifdef FEAT_FOLDING
7888 check_string_option(&wop->wo_fdi);
7889 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02007890 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891# ifdef FEAT_EVAL
7892 check_string_option(&wop->wo_fde);
7893 check_string_option(&wop->wo_fdt);
7894# endif
7895 check_string_option(&wop->wo_fmr);
7896#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007897#ifdef FEAT_SIGNS
7898 check_string_option(&wop->wo_scl);
7899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900#ifdef FEAT_RIGHTLEFT
7901 check_string_option(&wop->wo_rlc);
7902#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007903#ifdef FEAT_STL_OPT
7904 check_string_option(&wop->wo_stl);
7905#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02007906#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02007907 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02007908 check_string_option(&wop->wo_cc);
7909#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007910#ifdef FEAT_CONCEAL
7911 check_string_option(&wop->wo_cocu);
7912#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007913#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007914 check_string_option(&wop->wo_twk);
7915 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007916#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02007917#ifdef FEAT_LINEBREAK
7918 check_string_option(&wop->wo_briopt);
7919#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007920 check_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
7923/*
7924 * Free the allocated memory inside a winopt_T.
7925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007927clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929#ifdef FEAT_FOLDING
7930 clear_string_option(&wop->wo_fdi);
7931 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02007932 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933# ifdef FEAT_EVAL
7934 clear_string_option(&wop->wo_fde);
7935 clear_string_option(&wop->wo_fdt);
7936# endif
7937 clear_string_option(&wop->wo_fmr);
7938#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007939#ifdef FEAT_SIGNS
7940 clear_string_option(&wop->wo_scl);
7941#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02007942#ifdef FEAT_LINEBREAK
7943 clear_string_option(&wop->wo_briopt);
7944#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02007945 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946#ifdef FEAT_RIGHTLEFT
7947 clear_string_option(&wop->wo_rlc);
7948#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007949#ifdef FEAT_STL_OPT
7950 clear_string_option(&wop->wo_stl);
7951#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02007952#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02007953 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02007954 clear_string_option(&wop->wo_cc);
7955#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007956#ifdef FEAT_CONCEAL
7957 clear_string_option(&wop->wo_cocu);
7958#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007959#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02007960 clear_string_option(&wop->wo_twk);
7961 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02007962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963}
7964
7965/*
7966 * Copy global option values to local options for one buffer.
7967 * Used when creating a new buffer and sometimes when entering a buffer.
7968 * flags:
7969 * BCO_ENTER We will enter the buf buffer.
7970 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
7971 * appropriate.
7972 * BCO_NOHELP Don't copy the values to a help buffer.
7973 */
7974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007975buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 int should_copy = TRUE;
7978 char_u *save_p_isk = NULL; /* init for GCC */
7979 int dont_do_help;
7980 int did_isk = FALSE;
7981
7982 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 * Skip this when the option defaults have not been set yet. Happens when
7984 * main() allocates the first buffer.
7985 */
7986 if (p_cpo != NULL)
7987 {
7988 /*
7989 * Always copy when entering and 'cpo' contains 'S'.
7990 * Don't copy when already initialized.
7991 * Don't copy when 'cpo' contains 's' and not entering.
7992 * 'S' BCO_ENTER initialized 's' should_copy
7993 * yes yes X X TRUE
7994 * yes no yes X FALSE
7995 * no X yes X FALSE
7996 * X no no yes FALSE
7997 * X no no no TRUE
7998 * no yes no X TRUE
7999 */
8000 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
8001 && (buf->b_p_initialized
8002 || (!(flags & BCO_ENTER)
8003 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
8004 should_copy = FALSE;
8005
8006 if (should_copy || (flags & BCO_ALWAYS))
8007 {
8008 /* Don't copy the options specific to a help buffer when
8009 * BCO_NOHELP is given or the options were initialized already
8010 * (jumping back to a help file with CTRL-T or CTRL-O) */
8011 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
8012 || buf->b_p_initialized;
8013 if (dont_do_help) /* don't free b_p_isk */
8014 {
8015 save_p_isk = buf->b_p_isk;
8016 buf->b_p_isk = NULL;
8017 }
8018 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02008019 * Always free the allocated strings. If not already initialized,
8020 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 */
8022 if (!buf->b_p_initialized)
8023 {
8024 free_buf_options(buf, TRUE);
8025 buf->b_p_ro = FALSE; /* don't copy readonly */
8026 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02008028 switch (*p_ffs)
8029 {
8030 case 'm':
8031 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
8032 case 'd':
8033 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
8034 case 'u':
8035 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
8036 default:
8037 buf->b_p_ff = vim_strsave(p_ff);
8038 }
8039 if (buf->b_p_ff != NULL)
8040 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 buf->b_p_bh = empty_option;
8042 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
8044 else
8045 free_buf_options(buf, FALSE);
8046
8047 buf->b_p_ai = p_ai;
8048 buf->b_p_ai_nopaste = p_ai_nopaste;
8049 buf->b_p_sw = p_sw;
8050 buf->b_p_tw = p_tw;
8051 buf->b_p_tw_nopaste = p_tw_nopaste;
8052 buf->b_p_tw_nobin = p_tw_nobin;
8053 buf->b_p_wm = p_wm;
8054 buf->b_p_wm_nopaste = p_wm_nopaste;
8055 buf->b_p_wm_nobin = p_wm_nobin;
8056 buf->b_p_bin = p_bin;
Bram Moolenaare8bb2552005-07-08 22:26:47 +00008057 buf->b_p_bomb = p_bomb;
Bram Moolenaarb388be02015-07-22 22:19:38 +02008058 buf->b_p_fixeol = p_fixeol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 buf->b_p_et = p_et;
8060 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008061 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 buf->b_p_ml = p_ml;
8063 buf->b_p_ml_nobin = p_ml_nobin;
8064 buf->b_p_inf = p_inf;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02008065 buf->b_p_swf = cmdmod.noswapfile ? FALSE : p_swf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02008067#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02008068 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008070#ifdef FEAT_COMPL_FUNC
8071 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00008072 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008073#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02008074#ifdef FEAT_EVAL
8075 buf->b_p_tfu = vim_strsave(p_tfu);
8076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 buf->b_p_sts = p_sts;
8078 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008079#ifdef FEAT_VARTABS
8080 buf->b_p_vsts = vim_strsave(p_vsts);
8081 if (p_vsts && p_vsts != empty_option)
8082 tabstop_set(p_vsts, &buf->b_p_vsts_array);
8083 else
8084 buf->b_p_vsts_array = 0;
8085 buf->b_p_vsts_nopaste = p_vsts_nopaste
8086 ? vim_strsave(p_vsts_nopaste) : NULL;
8087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 buf->b_p_sn = p_sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#ifdef FEAT_COMMENTS
8090 buf->b_p_com = vim_strsave(p_com);
8091#endif
8092#ifdef FEAT_FOLDING
8093 buf->b_p_cms = vim_strsave(p_cms);
8094#endif
8095 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00008096 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 buf->b_p_nf = vim_strsave(p_nf);
8098 buf->b_p_mps = vim_strsave(p_mps);
8099#ifdef FEAT_SMARTINDENT
8100 buf->b_p_si = p_si;
8101#endif
8102 buf->b_p_ci = p_ci;
8103#ifdef FEAT_CINDENT
8104 buf->b_p_cin = p_cin;
8105 buf->b_p_cink = vim_strsave(p_cink);
8106 buf->b_p_cino = vim_strsave(p_cino);
8107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 /* Don't copy 'filetype', it must be detected */
8109 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 buf->b_p_pi = p_pi;
8111#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
8112 buf->b_p_cinw = vim_strsave(p_cinw);
8113#endif
8114#ifdef FEAT_LISP
8115 buf->b_p_lisp = p_lisp;
8116#endif
8117#ifdef FEAT_SYN_HL
8118 /* Don't copy 'syntax', it must be set */
8119 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00008120 buf->b_p_smc = p_smc;
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01008121 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008122#endif
8123#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02008124 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008125 (void)compile_cap_prog(&buf->b_s);
8126 buf->b_s.b_p_spf = vim_strsave(p_spf);
8127 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128#endif
8129#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
8130 buf->b_p_inde = vim_strsave(p_inde);
8131 buf->b_p_indk = vim_strsave(p_indk);
8132#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01008133 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008134#if defined(FEAT_EVAL)
8135 buf->b_p_fex = vim_strsave(p_fex);
8136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137#ifdef FEAT_CRYPT
8138 buf->b_p_key = vim_strsave(p_key);
8139#endif
8140#ifdef FEAT_SEARCHPATH
8141 buf->b_p_sua = vim_strsave(p_sua);
8142#endif
8143#ifdef FEAT_KEYMAP
8144 buf->b_p_keymap = vim_strsave(p_keymap);
8145 buf->b_kmap_state |= KEYMAP_INIT;
8146#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02008147#ifdef FEAT_TERMINAL
8148 buf->b_p_twsl = p_twsl;
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 /* This isn't really an option, but copying the langmap and IME
8151 * state from the current buffer is better than resetting it. */
8152 buf->b_p_iminsert = p_iminsert;
8153 buf->b_p_imsearch = p_imsearch;
8154
8155 /* options that are normally global but also have a local value
8156 * are not copied, start using the global value */
8157 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01008158 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02008159 buf->b_p_bkc = empty_option;
8160 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161#ifdef FEAT_QUICKFIX
8162 buf->b_p_gp = empty_option;
8163 buf->b_p_mp = empty_option;
8164 buf->b_p_efm = empty_option;
8165#endif
8166 buf->b_p_ep = empty_option;
8167 buf->b_p_kp = empty_option;
8168 buf->b_p_path = empty_option;
8169 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01008170 buf->b_p_tc = empty_option;
8171 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172#ifdef FEAT_FIND_ID
8173 buf->b_p_def = empty_option;
8174 buf->b_p_inc = empty_option;
8175# ifdef FEAT_EVAL
8176 buf->b_p_inex = vim_strsave(p_inex);
8177# endif
8178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 buf->b_p_dict = empty_option;
8180 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008181#ifdef FEAT_TEXTOBJ
8182 buf->b_p_qe = vim_strsave(p_qe);
8183#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00008184#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
8185 buf->b_p_bexpr = empty_option;
8186#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02008187#if defined(FEAT_CRYPT)
8188 buf->b_p_cm = empty_option;
8189#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008190#ifdef FEAT_PERSISTENT_UNDO
8191 buf->b_p_udf = p_udf;
8192#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01008193#ifdef FEAT_LISP
8194 buf->b_p_lw = empty_option;
8195#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01008196 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197
8198 /*
8199 * Don't copy the options set by ex_help(), use the saved values,
8200 * when going from a help buffer to a non-help buffer.
8201 * Don't touch these at all when BCO_NOHELP is used and going from
8202 * or to a help buffer.
8203 */
8204 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008207#ifdef FEAT_VARTABS
8208 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
8209 tabstop_set(p_vts, &buf->b_p_vts_array);
8210 else
8211 buf->b_p_vts_array = NULL;
8212#endif
8213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 else
8215 {
8216 buf->b_p_isk = vim_strsave(p_isk);
8217 did_isk = TRUE;
8218 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008219#ifdef FEAT_VARTABS
8220 buf->b_p_vts = vim_strsave(p_vts);
8221 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
8222 tabstop_set(p_vts, &buf->b_p_vts_array);
8223 else
8224 buf->b_p_vts_array = NULL;
8225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 if (buf->b_p_bt[0] == 'h')
8228 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 buf->b_p_ma = p_ma;
8230 }
8231 }
8232
8233 /*
8234 * When the options should be copied (ignoring BCO_ALWAYS), set the
8235 * flag that indicates that the options have been initialized.
8236 */
8237 if (should_copy)
8238 buf->b_p_initialized = TRUE;
8239 }
8240
8241 check_buf_options(buf); /* make sure we don't have NULLs */
8242 if (did_isk)
8243 (void)buf_init_chartab(buf, FALSE);
8244}
8245
8246/*
8247 * Reset the 'modifiable' option and its default value.
8248 */
8249 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008250reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
8252 int opt_idx;
8253
8254 curbuf->b_p_ma = FALSE;
8255 p_ma = FALSE;
8256 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00008257 if (opt_idx >= 0)
8258 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259}
8260
8261/*
8262 * Set the global value for 'iminsert' to the local value.
8263 */
8264 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008265set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266{
8267 p_iminsert = curbuf->b_p_iminsert;
8268}
8269
8270/*
8271 * Set the global value for 'imsearch' to the local value.
8272 */
8273 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008274set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275{
8276 p_imsearch = curbuf->b_p_imsearch;
8277}
8278
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279static int expand_option_idx = -1;
8280static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
8281static int expand_option_flags = 0;
8282
8283 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008284set_context_in_set_cmd(
8285 expand_T *xp,
8286 char_u *arg,
8287 int opt_flags) /* OPT_GLOBAL and/or OPT_LOCAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 int nextchar;
8290 long_u flags = 0; /* init for GCC */
8291 int opt_idx = 0; /* init for GCC */
8292 char_u *p;
8293 char_u *s;
8294 int is_term_option = FALSE;
8295 int key;
8296
8297 expand_option_flags = opt_flags;
8298
8299 xp->xp_context = EXPAND_SETTINGS;
8300 if (*arg == NUL)
8301 {
8302 xp->xp_pattern = arg;
8303 return;
8304 }
8305 p = arg + STRLEN(arg) - 1;
8306 if (*p == ' ' && *(p - 1) != '\\')
8307 {
8308 xp->xp_pattern = p + 1;
8309 return;
8310 }
8311 while (p > arg)
8312 {
8313 s = p;
8314 /* count number of backslashes before ' ' or ',' */
8315 if (*p == ' ' || *p == ',')
8316 {
8317 while (s > arg && *(s - 1) == '\\')
8318 --s;
8319 }
8320 /* break at a space with an even number of backslashes */
8321 if (*p == ' ' && ((p - s) & 1) == 0)
8322 {
8323 ++p;
8324 break;
8325 }
8326 --p;
8327 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00008328 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
8330 xp->xp_context = EXPAND_BOOL_SETTINGS;
8331 p += 2;
8332 }
8333 if (STRNCMP(p, "inv", 3) == 0)
8334 {
8335 xp->xp_context = EXPAND_BOOL_SETTINGS;
8336 p += 3;
8337 }
8338 xp->xp_pattern = arg = p;
8339 if (*arg == '<')
8340 {
8341 while (*p != '>')
8342 if (*p++ == NUL) /* expand terminal option name */
8343 return;
8344 key = get_special_key_code(arg + 1);
8345 if (key == 0) /* unknown name */
8346 {
8347 xp->xp_context = EXPAND_NOTHING;
8348 return;
8349 }
8350 nextchar = *++p;
8351 is_term_option = TRUE;
8352 expand_option_name[2] = KEY2TERMCAP0(key);
8353 expand_option_name[3] = KEY2TERMCAP1(key);
8354 }
8355 else
8356 {
8357 if (p[0] == 't' && p[1] == '_')
8358 {
8359 p += 2;
8360 if (*p != NUL)
8361 ++p;
8362 if (*p == NUL)
8363 return; /* expand option name */
8364 nextchar = *++p;
8365 is_term_option = TRUE;
8366 expand_option_name[2] = p[-2];
8367 expand_option_name[3] = p[-1];
8368 }
8369 else
8370 {
8371 /* Allow * wildcard */
8372 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
8373 p++;
8374 if (*p == NUL)
8375 return;
8376 nextchar = *p;
8377 *p = NUL;
8378 opt_idx = findoption(arg);
8379 *p = nextchar;
8380 if (opt_idx == -1 || options[opt_idx].var == NULL)
8381 {
8382 xp->xp_context = EXPAND_NOTHING;
8383 return;
8384 }
8385 flags = options[opt_idx].flags;
8386 if (flags & P_BOOL)
8387 {
8388 xp->xp_context = EXPAND_NOTHING;
8389 return;
8390 }
8391 }
8392 }
8393 /* handle "-=" and "+=" */
8394 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
8395 {
8396 ++p;
8397 nextchar = '=';
8398 }
8399 if ((nextchar != '=' && nextchar != ':')
8400 || xp->xp_context == EXPAND_BOOL_SETTINGS)
8401 {
8402 xp->xp_context = EXPAND_UNSUCCESSFUL;
8403 return;
8404 }
8405 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
8406 {
8407 xp->xp_context = EXPAND_OLD_SETTING;
8408 if (is_term_option)
8409 expand_option_idx = -1;
8410 else
8411 expand_option_idx = opt_idx;
8412 xp->xp_pattern = p + 1;
8413 return;
8414 }
8415 xp->xp_context = EXPAND_NOTHING;
8416 if (is_term_option || (flags & P_NUM))
8417 return;
8418
8419 xp->xp_pattern = p + 1;
8420
8421 if (flags & P_EXPAND)
8422 {
8423 p = options[opt_idx].var;
8424 if (p == (char_u *)&p_bdir
8425 || p == (char_u *)&p_dir
8426 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01008427 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 || p == (char_u *)&p_rtp
8429#ifdef FEAT_SEARCHPATH
8430 || p == (char_u *)&p_cdpath
8431#endif
8432#ifdef FEAT_SESSION
8433 || p == (char_u *)&p_vdir
8434#endif
8435 )
8436 {
8437 xp->xp_context = EXPAND_DIRECTORIES;
8438 if (p == (char_u *)&p_path
8439#ifdef FEAT_SEARCHPATH
8440 || p == (char_u *)&p_cdpath
8441#endif
8442 )
8443 xp->xp_backslash = XP_BS_THREE;
8444 else
8445 xp->xp_backslash = XP_BS_ONE;
8446 }
8447 else
8448 {
8449 xp->xp_context = EXPAND_FILES;
8450 /* for 'tags' need three backslashes for a space */
8451 if (p == (char_u *)&p_tags)
8452 xp->xp_backslash = XP_BS_THREE;
8453 else
8454 xp->xp_backslash = XP_BS_ONE;
8455 }
8456 }
8457
8458 /* For an option that is a list of file names, find the start of the
8459 * last file name. */
8460 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
8461 {
8462 /* count number of backslashes before ' ' or ',' */
8463 if (*p == ' ' || *p == ',')
8464 {
8465 s = p;
8466 while (s > xp->xp_pattern && *(s - 1) == '\\')
8467 --s;
8468 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
8469 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
8470 {
8471 xp->xp_pattern = p + 1;
8472 break;
8473 }
8474 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008475
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008476#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008477 /* for 'spellsuggest' start at "file:" */
8478 if (options[opt_idx].var == (char_u *)&p_sps
8479 && STRNCMP(p, "file:", 5) == 0)
8480 {
8481 xp->xp_pattern = p + 5;
8482 break;
8483 }
8484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 }
8486
8487 return;
8488}
8489
8490 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008491ExpandSettings(
8492 expand_T *xp,
8493 regmatch_T *regmatch,
8494 int *num_file,
8495 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497 int num_normal = 0; /* Nr of matching non-term-code settings */
8498 int num_term = 0; /* Nr of matching terminal code settings */
8499 int opt_idx;
8500 int match;
8501 int count = 0;
8502 char_u *str;
8503 int loop;
8504 int is_term_opt;
8505 char_u name_buf[MAX_KEY_NAME_LEN];
8506 static char *(names[]) = {"all", "termcap"};
8507 int ic = regmatch->rm_ic; /* remember the ignore-case flag */
8508
8509 /* do this loop twice:
8510 * loop == 0: count the number of matching options
8511 * loop == 1: copy the matching options into allocated memory
8512 */
8513 for (loop = 0; loop <= 1; ++loop)
8514 {
8515 regmatch->rm_ic = ic;
8516 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
8517 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00008518 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
8519 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
8521 {
8522 if (loop == 0)
8523 num_normal++;
8524 else
8525 (*file)[count++] = vim_strsave((char_u *)names[match]);
8526 }
8527 }
8528 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
8529 opt_idx++)
8530 {
8531 if (options[opt_idx].var == NULL)
8532 continue;
8533 if (xp->xp_context == EXPAND_BOOL_SETTINGS
8534 && !(options[opt_idx].flags & P_BOOL))
8535 continue;
8536 is_term_opt = istermoption(&options[opt_idx]);
8537 if (is_term_opt && num_normal > 0)
8538 continue;
8539 match = FALSE;
8540 if (vim_regexec(regmatch, str, (colnr_T)0)
8541 || (options[opt_idx].shortname != NULL
8542 && vim_regexec(regmatch,
8543 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
8544 match = TRUE;
8545 else if (is_term_opt)
8546 {
8547 name_buf[0] = '<';
8548 name_buf[1] = 't';
8549 name_buf[2] = '_';
8550 name_buf[3] = str[2];
8551 name_buf[4] = str[3];
8552 name_buf[5] = '>';
8553 name_buf[6] = NUL;
8554 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
8555 {
8556 match = TRUE;
8557 str = name_buf;
8558 }
8559 }
8560 if (match)
8561 {
8562 if (loop == 0)
8563 {
8564 if (is_term_opt)
8565 num_term++;
8566 else
8567 num_normal++;
8568 }
8569 else
8570 (*file)[count++] = vim_strsave(str);
8571 }
8572 }
8573 /*
8574 * Check terminal key codes, these are not in the option table
8575 */
8576 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
8577 {
8578 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
8579 {
8580 if (!isprint(str[0]) || !isprint(str[1]))
8581 continue;
8582
8583 name_buf[0] = 't';
8584 name_buf[1] = '_';
8585 name_buf[2] = str[0];
8586 name_buf[3] = str[1];
8587 name_buf[4] = NUL;
8588
8589 match = FALSE;
8590 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
8591 match = TRUE;
8592 else
8593 {
8594 name_buf[0] = '<';
8595 name_buf[1] = 't';
8596 name_buf[2] = '_';
8597 name_buf[3] = str[0];
8598 name_buf[4] = str[1];
8599 name_buf[5] = '>';
8600 name_buf[6] = NUL;
8601
8602 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
8603 match = TRUE;
8604 }
8605 if (match)
8606 {
8607 if (loop == 0)
8608 num_term++;
8609 else
8610 (*file)[count++] = vim_strsave(name_buf);
8611 }
8612 }
8613
8614 /*
8615 * Check special key names.
8616 */
8617 regmatch->rm_ic = TRUE; /* ignore case here */
8618 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
8619 {
8620 name_buf[0] = '<';
8621 STRCPY(name_buf + 1, str);
8622 STRCAT(name_buf, ">");
8623
8624 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
8625 {
8626 if (loop == 0)
8627 num_term++;
8628 else
8629 (*file)[count++] = vim_strsave(name_buf);
8630 }
8631 }
8632 }
8633 if (loop == 0)
8634 {
8635 if (num_normal > 0)
8636 *num_file = num_normal;
8637 else if (num_term > 0)
8638 *num_file = num_term;
8639 else
8640 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008641 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 if (*file == NULL)
8643 {
8644 *file = (char_u **)"";
8645 return FAIL;
8646 }
8647 }
8648 }
8649 return OK;
8650}
8651
8652 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008653ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
8655 char_u *var = NULL; /* init for GCC */
8656 char_u *buf;
8657
8658 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008659 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 if (*file == NULL)
8661 return FAIL;
8662
8663 /*
8664 * For a terminal key code expand_option_idx is < 0.
8665 */
8666 if (expand_option_idx < 0)
8667 {
8668 var = find_termcode(expand_option_name + 2);
8669 if (var == NULL)
8670 expand_option_idx = findoption(expand_option_name);
8671 }
8672
8673 if (expand_option_idx >= 0)
8674 {
8675 /* put string of option value in NameBuff */
8676 option_value2string(&options[expand_option_idx], expand_option_flags);
8677 var = NameBuff;
8678 }
8679 else if (var == NULL)
8680 var = (char_u *)"";
8681
8682 /* A backslash is required before some characters. This is the reverse of
8683 * what happens in do_set(). */
8684 buf = vim_strsave_escaped(var, escape_chars);
8685
8686 if (buf == NULL)
8687 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01008688 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 return FAIL;
8690 }
8691
8692#ifdef BACKSLASH_IN_FILENAME
8693 /* For MS-Windows et al. we don't double backslashes at the start and
8694 * before a file name character. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008695 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 if (var[0] == '\\' && var[1] == '\\'
8697 && expand_option_idx >= 0
8698 && (options[expand_option_idx].flags & P_EXPAND)
8699 && vim_isfilec(var[2])
8700 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00008701 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702#endif
8703
8704 *file[0] = buf;
8705 *num_file = 1;
8706 return OK;
8707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
8709/*
8710 * Get the value for the numeric or string option *opp in a nice format into
8711 * NameBuff[]. Must not be called with a hidden option!
8712 */
8713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008714option_value2string(
8715 struct vimoption *opp,
8716 int opt_flags) /* OPT_GLOBAL and/or OPT_LOCAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 char_u *varp;
8719
8720 varp = get_varp_scope(opp, opt_flags);
8721
8722 if (opp->flags & P_NUM)
8723 {
8724 long wc = 0;
8725
8726 if (wc_use_keyname(varp, &wc))
8727 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
8728 else if (wc != 0)
8729 STRCPY(NameBuff, transchar((int)wc));
8730 else
8731 sprintf((char *)NameBuff, "%ld", *(long *)varp);
8732 }
8733 else /* P_STRING */
8734 {
8735 varp = *(char_u **)(varp);
8736 if (varp == NULL) /* just in case */
8737 NameBuff[0] = NUL;
8738#ifdef FEAT_CRYPT
8739 /* don't show the actual value of 'key', only that it's set */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008740 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 STRCPY(NameBuff, "*****");
8742#endif
8743 else if (opp->flags & P_EXPAND)
8744 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
8745 /* Translate 'pastetoggle' into special key names */
8746 else if ((char_u **)opp->var == &p_pt)
8747 str2specialbuf(p_pt, NameBuff, MAXPATHL);
8748 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008749 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 }
8751}
8752
8753/*
8754 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
8755 * printed as a keyname.
8756 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
8757 */
8758 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008759wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
8762 {
8763 *wcp = *(long *)varp;
8764 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
8765 return TRUE;
8766 }
8767 return FALSE;
8768}
8769
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770/*
8771 * Return TRUE if format option 'x' is in effect.
8772 * Take care of no formatting when 'paste' is set.
8773 */
8774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008775has_format_option(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
8777 if (p_paste)
8778 return FALSE;
8779 return (vim_strchr(curbuf->b_p_fo, x) != NULL);
8780}
8781
8782/*
8783 * Return TRUE if "x" is present in 'shortmess' option, or
8784 * 'shortmess' contains 'a' and "x" is present in SHM_A.
8785 */
8786 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008787shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01008789 return p_shm != NULL &&
8790 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 || (vim_strchr(p_shm, 'a') != NULL
8792 && vim_strchr((char_u *)SHM_A, x) != NULL));
8793}
8794
8795/*
8796 * paste_option_changed() - Called after p_paste was set or reset.
8797 */
8798 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008799paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 static int old_p_paste = FALSE;
8802 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008803 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804#ifdef FEAT_CMDL_INFO
8805 static int save_ru = 0;
8806#endif
8807#ifdef FEAT_RIGHTLEFT
8808 static int save_ri = 0;
8809 static int save_hkmap = 0;
8810#endif
8811 buf_T *buf;
8812
8813 if (p_paste)
8814 {
8815 /*
8816 * Paste switched from off to on.
8817 * Save the current values, so they can be restored later.
8818 */
8819 if (!old_p_paste)
8820 {
8821 /* save options for each buffer */
Bram Moolenaar29323592016-07-24 22:04:11 +02008822 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 {
8824 buf->b_p_tw_nopaste = buf->b_p_tw;
8825 buf->b_p_wm_nopaste = buf->b_p_wm;
8826 buf->b_p_sts_nopaste = buf->b_p_sts;
8827 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008828 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008829#ifdef FEAT_VARTABS
8830 if (buf->b_p_vsts_nopaste)
8831 vim_free(buf->b_p_vsts_nopaste);
8832 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
8833 ? vim_strsave(buf->b_p_vsts) : NULL;
8834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 }
8836
8837 /* save global options */
8838 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008839 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840#ifdef FEAT_CMDL_INFO
8841 save_ru = p_ru;
8842#endif
8843#ifdef FEAT_RIGHTLEFT
8844 save_ri = p_ri;
8845 save_hkmap = p_hkmap;
8846#endif
8847 /* save global values for local buffer options */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008848 p_ai_nopaste = p_ai;
8849 p_et_nopaste = p_et;
8850 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 p_tw_nopaste = p_tw;
8852 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008853#ifdef FEAT_VARTABS
8854 if (p_vsts_nopaste)
8855 vim_free(p_vsts_nopaste);
8856 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
8857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
8859
8860 /*
8861 * Always set the option values, also when 'paste' is set when it is
8862 * already on.
8863 */
8864 /* set options for each buffer */
Bram Moolenaar29323592016-07-24 22:04:11 +02008865 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
8867 buf->b_p_tw = 0; /* textwidth is 0 */
8868 buf->b_p_wm = 0; /* wrapmargin is 0 */
8869 buf->b_p_sts = 0; /* softtabstop is 0 */
8870 buf->b_p_ai = 0; /* no auto-indent */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008871 buf->b_p_et = 0; /* no expandtab */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008872#ifdef FEAT_VARTABS
8873 if (buf->b_p_vsts)
8874 free_string_option(buf->b_p_vsts);
8875 buf->b_p_vsts = empty_option;
8876 if (buf->b_p_vsts_array)
8877 vim_free(buf->b_p_vsts_array);
8878 buf->b_p_vsts_array = 0;
8879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 }
8881
8882 /* set global options */
8883 p_sm = 0; /* no showmatch */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008884 p_sta = 0; /* no smarttab */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 if (p_ru)
8887 status_redraw_all(); /* redraw to remove the ruler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 p_ru = 0; /* no ruler */
8889#endif
8890#ifdef FEAT_RIGHTLEFT
8891 p_ri = 0; /* no reverse insert */
8892 p_hkmap = 0; /* no Hebrew keyboard */
8893#endif
8894 /* set global values for local buffer options */
8895 p_tw = 0;
8896 p_wm = 0;
8897 p_sts = 0;
8898 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008899#ifdef FEAT_VARTABS
8900 if (p_vsts)
8901 free_string_option(p_vsts);
8902 p_vsts = empty_option;
8903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905
8906 /*
8907 * Paste switched from on to off: Restore saved values.
8908 */
8909 else if (old_p_paste)
8910 {
8911 /* restore options for each buffer */
Bram Moolenaar29323592016-07-24 22:04:11 +02008912 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
8914 buf->b_p_tw = buf->b_p_tw_nopaste;
8915 buf->b_p_wm = buf->b_p_wm_nopaste;
8916 buf->b_p_sts = buf->b_p_sts_nopaste;
8917 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008918 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008919#ifdef FEAT_VARTABS
8920 if (buf->b_p_vsts)
8921 free_string_option(buf->b_p_vsts);
8922 buf->b_p_vsts = buf->b_p_vsts_nopaste
8923 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
8924 if (buf->b_p_vsts_array)
8925 vim_free(buf->b_p_vsts_array);
8926 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
8927 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
8928 else
8929 buf->b_p_vsts_array = 0;
8930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 }
8932
8933 /* restore global options */
8934 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008935 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (p_ru != save_ru)
8938 status_redraw_all(); /* redraw to draw the ruler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 p_ru = save_ru;
8940#endif
8941#ifdef FEAT_RIGHTLEFT
8942 p_ri = save_ri;
8943 p_hkmap = save_hkmap;
8944#endif
8945 /* set global values for local buffer options */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02008946 p_ai = p_ai_nopaste;
8947 p_et = p_et_nopaste;
8948 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 p_tw = p_tw_nopaste;
8950 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008951#ifdef FEAT_VARTABS
8952 if (p_vsts)
8953 free_string_option(p_vsts);
8954 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
8955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 }
8957
8958 old_p_paste = p_paste;
8959}
8960
8961/*
8962 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
8963 *
8964 * Reset 'compatible' and set the values for options that didn't get set yet
8965 * to the Vim defaults.
8966 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008967 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 */
8969 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008970vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008972 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00008973 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008974 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
8976 if (!option_was_set((char_u *)"cp"))
8977 {
8978 p_cp = FALSE;
8979 for (opt_idx = 0; !istermoption(&options[opt_idx]); opt_idx++)
8980 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
8981 set_option_default(opt_idx, OPT_FREE, FALSE);
8982 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02008983 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008985
8986 if (fname != NULL)
8987 {
8988 p = vim_getenv(envname, &dofree);
8989 if (p == NULL)
8990 {
8991 /* Set $MYVIMRC to the first vimrc file found. */
8992 p = FullName_save(fname, FALSE);
8993 if (p != NULL)
8994 {
8995 vim_setenv(envname, p);
8996 vim_free(p);
8997 }
8998 }
8999 else if (dofree)
9000 vim_free(p);
9001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002}
9003
9004/*
9005 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
9006 */
9007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009008change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00009010 int opt_idx;
9011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 if (p_cp != on)
9013 {
9014 p_cp = on;
9015 compatible_set();
9016 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00009017 opt_idx = findoption((char_u *)"cp");
9018 if (opt_idx >= 0)
9019 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020}
9021
9022/*
9023 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02009024 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 */
9026 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009027option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029 int idx;
9030
9031 idx = findoption(name);
9032 if (idx < 0) /* unknown option */
9033 return FALSE;
9034 if (options[idx].flags & P_WAS_SET)
9035 return TRUE;
9036 return FALSE;
9037}
9038
9039/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01009040 * Reset the flag indicating option "name" was set.
9041 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02009042 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009043reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01009044{
9045 int idx = findoption(name);
9046
9047 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02009048 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01009049 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02009050 return OK;
9051 }
9052 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01009053}
9054
9055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 * compatible_set() - Called when 'compatible' has been set or unset.
9057 *
9058 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
9059 * flag) to a Vi compatible value.
9060 * When 'compatible' is unset: Set all options that have a different default
9061 * for Vim (without the P_VI_DEF flag) to that default.
9062 */
9063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009064compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066 int opt_idx;
9067
9068 for (opt_idx = 0; !istermoption(&options[opt_idx]); opt_idx++)
9069 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
9070 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
9071 set_option_default(opt_idx, OPT_FREE, p_cp);
9072 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02009073 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074}
9075
9076#ifdef FEAT_LINEBREAK
9077
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078/*
9079 * fill_breakat_flags() -- called when 'breakat' changes value.
9080 */
9081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009082fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009084 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 int i;
9086
9087 for (i = 0; i < 256; i++)
9088 breakat_flags[i] = FALSE;
9089
9090 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009091 for (p = p_breakat; *p; p++)
9092 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094#endif
9095
9096/*
9097 * Check an option that can be a range of string values.
9098 *
9099 * Return OK for correct value, FAIL otherwise.
9100 * Empty is always OK.
9101 */
9102 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009103check_opt_strings(
9104 char_u *val,
9105 char **values,
9106 int list) /* when TRUE: accept a list of values */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 return opt_strings_flags(val, values, NULL, list);
9109}
9110
9111/*
9112 * Handle an option that can be a range of string values.
9113 * Set a flag in "*flagp" for each string present.
9114 *
9115 * Return OK for correct value, FAIL otherwise.
9116 * Empty is always OK.
9117 */
9118 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009119opt_strings_flags(
9120 char_u *val, /* new value */
9121 char **values, /* array of valid string values */
9122 unsigned *flagp,
9123 int list) /* when TRUE: accept a list of values */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 int i;
9126 int len;
9127 unsigned new_flags = 0;
9128
9129 while (*val)
9130 {
9131 for (i = 0; ; ++i)
9132 {
9133 if (values[i] == NULL) /* val not found in values[] */
9134 return FAIL;
9135
9136 len = (int)STRLEN(values[i]);
9137 if (STRNCMP(values[i], val, len) == 0
9138 && ((list && val[len] == ',') || val[len] == NUL))
9139 {
9140 val += len + (val[len] == ',');
9141 new_flags |= (1 << i);
9142 break; /* check next item in val list */
9143 }
9144 }
9145 }
9146 if (flagp != NULL)
9147 *flagp = new_flags;
9148
9149 return OK;
9150}
9151
9152/*
9153 * Read the 'wildmode' option, fill wim_flags[].
9154 */
9155 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009156check_opt_wim(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 char_u new_wim_flags[4];
9159 char_u *p;
9160 int i;
9161 int idx = 0;
9162
9163 for (i = 0; i < 4; ++i)
9164 new_wim_flags[i] = 0;
9165
9166 for (p = p_wim; *p; ++p)
9167 {
9168 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
9169 ;
9170 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
9171 return FAIL;
9172 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
9173 new_wim_flags[idx] |= WIM_LONGEST;
9174 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
9175 new_wim_flags[idx] |= WIM_FULL;
9176 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
9177 new_wim_flags[idx] |= WIM_LIST;
9178 else
9179 return FAIL;
9180 p += i;
9181 if (*p == NUL)
9182 break;
9183 if (*p == ',')
9184 {
9185 if (idx == 3)
9186 return FAIL;
9187 ++idx;
9188 }
9189 }
9190
9191 /* fill remaining entries with last flag */
9192 while (idx < 3)
9193 {
9194 new_wim_flags[idx + 1] = new_wim_flags[idx];
9195 ++idx;
9196 }
9197
9198 /* only when there are no errors, wim_flags[] is changed */
9199 for (i = 0; i < 4; ++i)
9200 wim_flags[i] = new_wim_flags[i];
9201 return OK;
9202}
9203
9204/*
9205 * Check if backspacing over something is allowed.
9206 */
9207 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009208can_bs(
9209 int what) /* BS_INDENT, BS_EOL or BS_START */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02009211#ifdef FEAT_JOB_CHANNEL
9212 if (what == BS_START && bt_prompt(curbuf))
9213 return FALSE;
9214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 switch (*p_bs)
9216 {
9217 case '2': return TRUE;
9218 case '1': return (what != BS_START);
9219 case '0': return FALSE;
9220 }
9221 return vim_strchr(p_bs, what) != NULL;
9222}
9223
9224/*
9225 * Save the current values of 'fileformat' and 'fileencoding', so that we know
9226 * the file must be considered changed when the value is different.
9227 */
9228 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009229save_file_ff(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230{
9231 buf->b_start_ffc = *buf->b_p_ff;
9232 buf->b_start_eol = buf->b_p_eol;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00009233 buf->b_start_bomb = buf->b_p_bomb;
9234
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 /* Only use free/alloc when necessary, they take time. */
9236 if (buf->b_start_fenc == NULL
9237 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
9238 {
9239 vim_free(buf->b_start_fenc);
9240 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
9241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242}
9243
9244/*
9245 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
9246 * from when editing started (save_file_ff() called).
Bram Moolenaar83eb8852007-08-12 13:51:26 +00009247 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
9248 * changed and 'binary' is not set.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02009249 * Also when 'endofline' was changed and 'fixeol' is not set.
Bram Moolenaar164c60f2011-01-22 00:11:50 +01009250 * When "ignore_empty" is true don't consider a new, empty buffer to be
9251 * changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 */
9253 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009254file_ff_differs(buf_T *buf, int ignore_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
Bram Moolenaar9cffde92007-07-24 07:51:18 +00009256 /* In a buffer that was never loaded the options are not valid. */
9257 if (buf->b_flags & BF_NEVERLOADED)
9258 return FALSE;
Bram Moolenaar164c60f2011-01-22 00:11:50 +01009259 if (ignore_empty
9260 && (buf->b_flags & BF_NEW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 && buf->b_ml.ml_line_count == 1
9262 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
9263 return FALSE;
9264 if (buf->b_start_ffc != *buf->b_p_ff)
9265 return TRUE;
Bram Moolenaar34d72d42015-07-17 14:18:08 +02009266 if ((buf->b_p_bin || !buf->b_p_fixeol) && buf->b_start_eol != buf->b_p_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 return TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00009268 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
9269 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 if (buf->b_start_fenc == NULL)
9271 return (*buf->b_p_fenc != NUL);
9272 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273}
9274
9275/*
9276 * return OK if "p" is a valid fileformat name, FAIL otherwise.
9277 */
9278 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009279check_ff_value(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
9281 return check_opt_strings(p, p_ff_values, FALSE);
9282}
Bram Moolenaar14f24742012-08-08 18:01:05 +02009283
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01009284/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01009285 * Return the effective 'scrolloff' value for the current window, using the
9286 * global value when appropriate.
9287 */
9288 long
9289get_scrolloff_value(void)
9290{
9291 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
9292}
9293
9294/*
9295 * Return the effective 'sidescrolloff' value for the current window, using the
9296 * global value when appropriate.
9297 */
9298 long
9299get_sidescrolloff_value(void)
9300{
9301 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
9302}
9303
9304/*
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01009305 * Check matchpairs option for "*initc".
9306 * If there is a match set "*initc" to the matching character and "*findc" to
9307 * the opposite character. Set "*backwards" to the direction.
9308 * When "switchit" is TRUE swap the direction.
9309 */
9310 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009311find_mps_values(
9312 int *initc,
9313 int *findc,
9314 int *backwards,
9315 int switchit)
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01009316{
9317 char_u *ptr;
9318
9319 ptr = curbuf->b_p_mps;
9320 while (*ptr != NUL)
9321 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01009322 if (has_mbyte)
9323 {
9324 char_u *prev;
9325
9326 if (mb_ptr2char(ptr) == *initc)
9327 {
9328 if (switchit)
9329 {
9330 *findc = *initc;
9331 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
9332 *backwards = TRUE;
9333 }
9334 else
9335 {
9336 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
9337 *backwards = FALSE;
9338 }
9339 return;
9340 }
9341 prev = ptr;
9342 ptr += mb_ptr2len(ptr) + 1;
9343 if (mb_ptr2char(ptr) == *initc)
9344 {
9345 if (switchit)
9346 {
9347 *findc = *initc;
9348 *initc = mb_ptr2char(prev);
9349 *backwards = FALSE;
9350 }
9351 else
9352 {
9353 *findc = mb_ptr2char(prev);
9354 *backwards = TRUE;
9355 }
9356 return;
9357 }
9358 ptr += mb_ptr2len(ptr);
9359 }
9360 else
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01009361 {
9362 if (*ptr == *initc)
9363 {
9364 if (switchit)
9365 {
9366 *backwards = TRUE;
9367 *findc = *initc;
9368 *initc = ptr[2];
9369 }
9370 else
9371 {
9372 *backwards = FALSE;
9373 *findc = ptr[2];
9374 }
9375 return;
9376 }
9377 ptr += 2;
9378 if (*ptr == *initc)
9379 {
9380 if (switchit)
9381 {
9382 *backwards = FALSE;
9383 *findc = *initc;
9384 *initc = ptr[-2];
9385 }
9386 else
9387 {
9388 *backwards = TRUE;
9389 *findc = ptr[-2];
9390 }
9391 return;
9392 }
9393 ++ptr;
9394 }
9395 if (*ptr == ',')
9396 ++ptr;
9397 }
9398}
Bram Moolenaar597a4222014-06-25 14:39:50 +02009399
9400#if defined(FEAT_LINEBREAK) || defined(PROTO)
9401/*
9402 * This is called when 'breakindentopt' is changed and when a window is
9403 * initialized.
9404 */
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02009405 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009406briopt_check(win_T *wp)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009407{
9408 char_u *p;
9409 int bri_shift = 0;
9410 long bri_min = 20;
9411 int bri_sbr = FALSE;
9412
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02009413 p = wp->w_p_briopt;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009414 while (*p != NUL)
9415 {
9416 if (STRNCMP(p, "shift:", 6) == 0
9417 && ((p[6] == '-' && VIM_ISDIGIT(p[7])) || VIM_ISDIGIT(p[6])))
9418 {
9419 p += 6;
9420 bri_shift = getdigits(&p);
9421 }
9422 else if (STRNCMP(p, "min:", 4) == 0 && VIM_ISDIGIT(p[4]))
9423 {
9424 p += 4;
9425 bri_min = getdigits(&p);
9426 }
9427 else if (STRNCMP(p, "sbr", 3) == 0)
9428 {
9429 p += 3;
9430 bri_sbr = TRUE;
9431 }
9432 if (*p != ',' && *p != NUL)
9433 return FAIL;
9434 if (*p == ',')
9435 ++p;
9436 }
9437
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02009438 wp->w_p_brishift = bri_shift;
9439 wp->w_p_brimin = bri_min;
9440 wp->w_p_brisbr = bri_sbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009441
9442 return OK;
9443}
9444#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02009445
9446/*
9447 * Get the local or global value of 'backupcopy'.
9448 */
9449 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009450get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02009451{
9452 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
9453}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02009454
9455#if defined(FEAT_SIGNS) || defined(PROTO)
9456/*
9457 * Return TRUE when window "wp" has a column to draw signs in.
9458 */
9459 int
9460signcolumn_on(win_T *wp)
9461{
Bram Moolenaar394c5d82019-06-17 21:48:05 +02009462 // If 'signcolumn' is set to 'number', signs are displayed in the 'number'
9463 // column (if present). Otherwise signs are to be displayed in the sign
9464 // column.
9465 if (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
9466 return wp->w_buffer->b_signlist != NULL && !wp->w_p_nu && !wp->w_p_rnu;
9467
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02009468 if (*wp->w_p_scl == 'n')
9469 return FALSE;
9470 if (*wp->w_p_scl == 'y')
9471 return TRUE;
9472 return (wp->w_buffer->b_signlist != NULL
9473# ifdef FEAT_NETBEANS_INTG
9474 || wp->w_buffer->b_has_sign_column
9475# endif
9476 );
9477}
9478#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02009479
9480#if defined(FEAT_EVAL) || defined(PROTO)
9481/*
9482 * Get window or buffer local options.
9483 */
9484 dict_T *
9485get_winbuf_options(int bufopt)
9486{
9487 dict_T *d;
9488 int opt_idx;
9489
9490 d = dict_alloc();
9491 if (d == NULL)
9492 return NULL;
9493
9494 for (opt_idx = 0; !istermoption(&options[opt_idx]); opt_idx++)
9495 {
9496 struct vimoption *opt = &options[opt_idx];
9497
9498 if ((bufopt && (opt->indir & PV_BUF))
9499 || (!bufopt && (opt->indir & PV_WIN)))
9500 {
9501 char_u *varp = get_varp(opt);
9502
9503 if (varp != NULL)
9504 {
9505 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02009506 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02009507 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02009508 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02009509 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02009510 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02009511 }
9512 }
9513 }
9514
9515 return d;
9516}
9517#endif