blob: 1ccb5d9ec1f6df46593be991044c21dcb4a7cd3c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010040static char_u *option_expand(int opt_idx, char_u *val);
41static void didset_options(void);
42static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000043#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010044static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000045#else
46# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
47#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010048static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
49static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020050static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010051static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020052static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010053static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010054static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
56static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020057static int istermoption(struct vimoption *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
59static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020060static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static void option_value2string(struct vimoption *, int opt_flags);
62static void check_winopt(winopt_T *wop);
63static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void paste_option_changed(void);
65static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67/*
68 * Initialize the options, first part.
69 *
70 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010071 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 */
73 void
Bram Moolenaar07268702018-03-01 21:57:32 +010074set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000075{
76 char_u *p;
77 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000078 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80#ifdef FEAT_LANGMAP
81 langmap_init();
82#endif
83
84 /* Be Vi compatible by default */
85 p_cp = TRUE;
86
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 /* Use POSIX compatibility when $VIM_POSIX is set. */
88 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000089 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000090 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020091 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000092 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000093
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 /*
95 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000096 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000098 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010099#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000100 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100101 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000103 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200104#if defined(MSWIN)
105 {
106 // For MS-Windows put the path in quotes instead of escaping spaces.
107 char_u *cmd;
108 size_t len;
109
110 if (vim_strchr(p, ' ') != NULL)
111 {
112 len = STRLEN(p) + 3; // two quotes and a trailing NUL
113 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200114 if (cmd != NULL)
115 {
116 vim_snprintf((char *)cmd, len, "\"%s\"", p);
117 set_string_default("sh", cmd);
118 vim_free(cmd);
119 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200120 }
121 else
122 set_string_default("sh", p);
123 }
124#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100125 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128#ifdef FEAT_WILDIGN
129 /*
130 * Set the default for 'backupskip' to include environment variables for
131 * temp files.
132 */
133 {
134# ifdef UNIX
135 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
136# else
137 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
138# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000139 int len;
140 garray_T ga;
141 int mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143 ga_init2(&ga, 1, 100);
144 for (n = 0; n < (long)(sizeof(names) / sizeof(char *)); ++n)
145 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000146 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# ifdef UNIX
148 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200149# ifdef MACOS_X
150 p = (char_u *)"/private/tmp";
151# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 else
155# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000156 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 if (p != NULL && *p != NUL)
158 {
159 /* First time count the NUL, otherwise count the ','. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000160 len = (int)STRLEN(p) + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (ga_grow(&ga, len) == OK)
162 {
163 if (ga.ga_len > 0)
164 STRCAT(ga.ga_data, ",");
165 STRCAT(ga.ga_data, p);
166 add_pathsep(ga.ga_data);
167 STRCAT(ga.ga_data, "*");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 ga.ga_len += len;
169 }
170 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000171 if (mustfree)
172 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 }
174 if (ga.ga_data != NULL)
175 {
176 set_string_default("bsk", ga.ga_data);
177 vim_free(ga.ga_data);
178 }
179 }
180#endif
181
182 /*
183 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
184 */
185 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000186 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000188#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
189 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
190#endif
191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#ifdef HAVE_AVAIL_MEM
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000193 /* Use amount of memory available at this moment. */
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200194 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#else
196# ifdef HAVE_TOTAL_MEM
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197 /* Use amount of memory available to Vim. */
Bram Moolenaar914572a2007-05-01 11:37:47 +0000198 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000200 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201# endif
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000204 opt_idx = findoption((char_u *)"maxmem");
205 if (opt_idx >= 0)
206 {
207#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100208 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
209 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000210#endif
211 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
212 }
213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
215
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifdef FEAT_SEARCHPATH
217 {
218 char_u *cdpath;
219 char_u *buf;
220 int i;
221 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000222 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
224 /* Initialize the 'cdpath' option's default value. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000225 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (cdpath != NULL)
227 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200228 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 if (buf != NULL)
230 {
231 buf[0] = ','; /* start with ",", current dir first */
232 j = 1;
233 for (i = 0; cdpath[i] != NUL; ++i)
234 {
235 if (vim_ispathlistsep(cdpath[i]))
236 buf[j++] = ',';
237 else
238 {
239 if (cdpath[i] == ' ' || cdpath[i] == ',')
240 buf[j++] = '\\';
241 buf[j++] = cdpath[i];
242 }
243 }
244 buf[j] = NUL;
245 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000246 if (opt_idx >= 0)
247 {
248 options[opt_idx].def_val[VI_DEFAULT] = buf;
249 options[opt_idx].flags |= P_DEF_ALLOCED;
250 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200251 else
252 vim_free(buf); /* cannot happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254 if (mustfree)
255 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 }
257 }
258#endif
259
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100260#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 /* Set print encoding on platforms that don't default to latin1 */
262 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100263# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 (char_u *)"cp1252"
265# else
266# ifdef VMS
267 (char_u *)"dec-mcs"
268# else
269# ifdef EBCDIC
270 (char_u *)"ebcdic-uk"
271# else
272# ifdef MAC
273 (char_u *)"mac-roman"
274# else /* HPUX */
275 (char_u *)"hp-roman8"
276# endif
277# endif
278# endif
279# endif
280 );
281#endif
282
283#ifdef FEAT_POSTSCRIPT
284 /* 'printexpr' must be allocated to be able to evaluate it. */
285 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100286# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000287 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288# else
289# ifdef VMS
290 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
291
292# else
293 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
294# endif
295# endif
296 );
297#endif
298
299 /*
300 * Set all the options (except the terminal options) to their default
301 * value. Also set the global value for local options.
302 */
303 set_options_default(0);
304
Bram Moolenaar07268702018-03-01 21:57:32 +0100305#ifdef CLEAN_RUNTIMEPATH
306 if (clean_arg)
307 {
308 opt_idx = findoption((char_u *)"runtimepath");
309 if (opt_idx >= 0)
310 {
311 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
312 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
313 }
314 opt_idx = findoption((char_u *)"packpath");
315 if (opt_idx >= 0)
316 {
317 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
318 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
319 }
320 }
321#endif
322
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_GUI
324 if (found_reverse_arg)
325 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
326#endif
327
328 curbuf->b_p_initialized = TRUE;
329 curbuf->b_p_ar = -1; /* no local 'autoread' value */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100330 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 check_buf_options(curbuf);
332 check_win_options(curwin);
333 check_options();
334
335 /* Must be before option_expand(), because that one needs vim_isIDc() */
336 didset_options();
337
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000338#ifdef FEAT_SPELL
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200339 /* Use the current chartab for the generic chartab. This is not in
340 * didset_options() because it only depends on 'encoding'. */
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000341 init_spell_chartab();
342#endif
343
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 /*
345 * Expand environment variables and things like "~" for the defaults.
346 * If option_expand() returns non-NULL the variable is expanded. This can
347 * only happen for non-indirect options.
348 * Also set the default to the expanded value, so ":set" does not list
349 * them.
350 * Don't set the P_ALLOCED flag, because we don't want to free the
351 * default.
352 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200353 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 if ((options[opt_idx].flags & P_GETTEXT)
356 && options[opt_idx].var != NULL)
357 p = (char_u *)_(*(char **)options[opt_idx].var);
358 else
359 p = option_expand(opt_idx, NULL);
360 if (p != NULL && (p = vim_strsave(p)) != NULL)
361 {
362 *(char_u **)options[opt_idx].var = p;
363 /* VIMEXP
364 * Defaults for all expanded options are currently the same for Vi
365 * and Vim. When this changes, add some code here! Also need to
366 * split P_DEF_ALLOCED in two.
367 */
368 if (options[opt_idx].flags & P_DEF_ALLOCED)
369 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
370 options[opt_idx].def_val[VI_DEFAULT] = p;
371 options[opt_idx].flags |= P_DEF_ALLOCED;
372 }
373 }
374
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 save_file_ff(curbuf); /* Buffer is unchanged */
376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#if defined(FEAT_ARABIC)
378 /* Detect use of mlterm.
379 * Mlterm is a terminal emulator akin to xterm that has some special
380 * abilities (bidi namely).
381 * NOTE: mlterm's author is being asked to 'set' a variable
382 * instead of an environment variable due to inheritance.
383 */
384 if (mch_getenv((char_u *)"MLTERM") != NULL)
385 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
386#endif
387
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200388 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaar4f974752019-02-17 17:44:42 +0100390# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 /*
392 * If $LANG isn't set, try to get a good value for it. This makes the
393 * right language be used automatically. Don't do this for English.
394 */
395 if (mch_getenv((char_u *)"LANG") == NULL)
396 {
397 char buf[20];
398
399 /* Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
400 * LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
401 * only the first two. */
402 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
403 (LPTSTR)buf, 20);
404 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
405 {
406 /* There are a few exceptions (probably more) */
407 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
408 STRCPY(buf, "zh_TW");
409 else if (STRNICMP(buf, "chs", 3) == 0
410 || STRNICMP(buf, "zhc", 3) == 0)
411 STRCPY(buf, "zh_CN");
412 else if (STRNICMP(buf, "jp", 2) == 0)
413 STRCPY(buf, "ja");
414 else
415 buf[2] = NUL; /* truncate to two-letter code */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100416 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 }
418 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000419# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000420# ifdef MACOS_CONVERT
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000421 /* Moved to os_mac_conv.c to avoid dependency problems. */
422 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424# endif
425
426 /* enc_locale() will try to find the encoding of the current locale. */
427 p = enc_locale();
428 if (p != NULL)
429 {
430 char_u *save_enc;
431
432 /* Try setting 'encoding' and check if the value is valid.
433 * If not, go back to the default "latin1". */
434 save_enc = p_enc;
435 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000436 if (STRCMP(p_enc, "gb18030") == 0)
437 {
438 /* We don't support "gb18030", but "cp936" is a good substitute
439 * for practical purposes, thus use that. It's not an alias to
440 * still support conversion between gb18030 and utf-8. */
441 p_enc = vim_strsave((char_u *)"cp936");
442 vim_free(p);
443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (mb_init() == NULL)
445 {
446 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000447 if (opt_idx >= 0)
448 {
449 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
450 options[opt_idx].flags |= P_DEF_ALLOCED;
451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
Bram Moolenaard0573012017-10-28 21:11:06 +0200453#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100454 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000455 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000456 /* Adjust the default for 'isprint' and 'iskeyword' to match
457 * latin1. Also set the defaults for when 'nocompatible' is
458 * set. */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000459 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000460 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000461 set_string_option_direct((char_u *)"isk", -1,
462 ISK_LATIN1, OPT_FREE, SID_NONE);
463 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000464 if (opt_idx >= 0)
465 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000466 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000467 if (opt_idx >= 0)
468 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000469 (void)init_chartab();
470 }
471#endif
472
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200473#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 /* Win32 console: When GetACP() returns a different value from
475 * GetConsoleCP() set 'termencoding'. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200476 if (
477# ifdef VIMDLL
478 (!gui.in_use && !gui.starting) &&
479# endif
480 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
482 char buf[50];
483
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100484 /* Win32 console: In ConPTY, GetConsoleCP() returns zero.
485 * Use an alternative value. */
486 if (GetConsoleCP() == 0)
487 sprintf(buf, "cp%ld", (long)GetACP());
488 else
489 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 p_tenc = vim_strsave((char_u *)buf);
491 if (p_tenc != NULL)
492 {
493 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000494 if (opt_idx >= 0)
495 {
496 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
497 options[opt_idx].flags |= P_DEF_ALLOCED;
498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 convert_setup(&input_conv, p_tenc, p_enc);
500 convert_setup(&output_conv, p_enc, p_tenc);
501 }
502 else
503 p_tenc = empty_option;
504 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100505#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100506#if defined(MSWIN)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000507 /* $HOME may have characters in active code page. */
508 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 }
511 else
512 {
513 vim_free(p_enc);
514 p_enc = save_enc;
515 }
516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518#ifdef FEAT_MULTI_LANG
519 /* Set the default for 'helplang'. */
520 set_helplang_default(get_mess_lang());
521#endif
522}
523
524/*
525 * Set an option to its default value.
526 * This does not take care of side effects!
527 */
528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100529set_option_default(
530 int opt_idx,
531 int opt_flags, /* OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL */
532 int compatible) /* use Vi default value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 char_u *varp; /* pointer to variable for current option */
535 int dvi; /* index in def_val[] */
536 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000537 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
539
540 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
541 flags = options[opt_idx].flags;
Bram Moolenaar3638c682005-06-08 22:05:14 +0000542 if (varp != NULL) /* skip hidden option, nothing to do for it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
544 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
545 if (flags & P_STRING)
546 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200547 /* Use set_string_option_direct() for local options to handle
548 * freeing and allocating the value. */
549 if (options[opt_idx].indir != PV_NONE)
550 set_string_option_direct(NULL, opt_idx,
551 options[opt_idx].def_val[dvi], opt_flags, 0);
552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200554 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
555 free_string_option(*(char_u **)(varp));
556 *(char_u **)varp = options[opt_idx].def_val[dvi];
557 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 }
559 }
560 else if (flags & P_NUM)
561 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000562 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 win_comp_scroll(curwin);
564 else
565 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100566 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
567
568 if ((long *)varp == &curwin->w_p_so
569 || (long *)varp == &curwin->w_p_siso)
570 // 'scrolloff' and 'sidescrolloff' local values have a
571 // different default value than the global default.
572 *(long *)varp = -1;
573 else
574 *(long *)varp = def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 /* May also set global value for local option. */
576 if (both)
577 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100578 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 }
581 else /* P_BOOL */
582 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000583 /* the cast to long is required for Manx C, long_i is needed for
584 * MSVC */
585 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000586#ifdef UNIX
587 /* 'modeline' defaults to off for root */
588 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
589 *(int *)varp = FALSE;
590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 /* May also set global value for local option. */
592 if (both)
593 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
594 *(int *)varp;
595 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000596
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000597 /* The default value is not insecure. */
598 flagsp = insecure_flag(opt_idx, opt_flags);
599 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601
602#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200603 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#endif
605}
606
607/*
608 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200609 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 */
611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100612set_options_default(
613 int opt_flags) /* OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000617 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
Bram Moolenaardac13472019-09-16 21:06:21 +0200619 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200620 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200621 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100622 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200623# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200624 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200625 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200626# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100627 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 set_option_default(i, opt_flags, p_cp);
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 /* The 'scroll' option must be computed for all windows. */
Bram Moolenaarf740b292006-02-16 22:11:02 +0000631 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200633#ifdef FEAT_CINDENT
634 parse_cino(curbuf);
635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636}
637
638/*
639 * Set the Vi-default value of a string option.
640 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100641 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100643 static void
644set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 char_u *p;
647 int opt_idx;
648
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100649 if (escape && vim_strchr(val, ' ') != NULL)
650 p = vim_strsave_escaped(val, (char_u *)" ");
651 else
652 p = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (p != NULL) /* we don't want a NULL */
654 {
655 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000656 if (opt_idx >= 0)
657 {
658 if (options[opt_idx].flags & P_DEF_ALLOCED)
659 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
660 options[opt_idx].def_val[VI_DEFAULT] = p;
661 options[opt_idx].flags |= P_DEF_ALLOCED;
662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664}
665
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100666 void
667set_string_default(char *name, char_u *val)
668{
669 set_string_default_esc(name, val, FALSE);
670}
671
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672/*
673 * Set the Vi-default value of a number option.
674 * Used for 'lines' and 'columns'.
675 */
676 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100677set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000679 int opt_idx;
680
681 opt_idx = findoption((char_u *)name);
682 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000683 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684}
685
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200686/*
687 * Set all window-local and buffer-local options to the Vim default.
688 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200689 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200690 */
691 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200692set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200693{
694 win_T *save_curwin = curwin;
695 int i;
696
697 curwin = wp;
698 curbuf = curwin->w_buffer;
699 block_autocmds();
700
Bram Moolenaardac13472019-09-16 21:06:21 +0200701 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200702 {
703 struct vimoption *p = &(options[i]);
704 char_u *varp = get_varp_scope(p, OPT_LOCAL);
705
706 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200707 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200708 && !(options[i].flags & P_NODEFAULT)
709 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200710 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200711 }
712
713 unblock_autocmds();
714 curwin = save_curwin;
715 curbuf = curwin->w_buffer;
716}
717
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000718#if defined(EXITFREE) || defined(PROTO)
719/*
720 * Free all options.
721 */
722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100723free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000724{
725 int i;
726
Bram Moolenaardac13472019-09-16 21:06:21 +0200727 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000728 {
729 if (options[i].indir == PV_NONE)
730 {
731 /* global option: free value and default value. */
Bram Moolenaar67391142017-02-19 21:07:04 +0100732 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000733 free_string_option(*(char_u **)options[i].var);
734 if (options[i].flags & P_DEF_ALLOCED)
735 free_string_option(options[i].def_val[VI_DEFAULT]);
736 }
737 else if (options[i].var != VAR_WIN
738 && (options[i].flags & P_STRING))
739 /* buffer-local option: free global value */
740 free_string_option(*(char_u **)options[i].var);
741 }
742}
743#endif
744
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746/*
747 * Initialize the options, part two: After getting Rows and Columns and
748 * setting 'term'.
749 */
750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100751set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000753 int idx;
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100756 * 'scroll' defaults to half the window height. The stored default is zero,
757 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000759 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000760 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000761 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 comp_col();
763
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000764 /*
765 * 'window' is only for backwards compatibility with Vi.
766 * Default is Rows - 1.
767 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000768 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000769 p_window = Rows - 1;
770 set_number_default("window", Rows - 1);
771
Bram Moolenaarf740b292006-02-16 22:11:02 +0000772 /* For DOS console the default is always black. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100773#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000774 /*
775 * If 'background' wasn't set by the user, try guessing the value,
776 * depending on the terminal name. Only need to check for terminals
777 * with a dark background, that can handle color.
778 */
779 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000780 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
781 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000783 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaarf740b292006-02-16 22:11:02 +0000784 /* don't mark it as set, when starting the GUI it may be
785 * changed again */
786 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000789
790#ifdef CURSOR_SHAPE
791 parse_shape_opt(SHAPE_CURSOR); /* set cursor shapes from 'guicursor' */
792#endif
793#ifdef FEAT_MOUSESHAPE
794 parse_shape_opt(SHAPE_MOUSE); /* set mouse shapes from 'mouseshape' */
795#endif
796#ifdef FEAT_PRINTER
797 (void)parse_printoptions(); /* parse 'printoptions' default value */
798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799}
800
801/*
802 * Initialize the options, part three: After reading the .vimrc
803 */
804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100805set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100807#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808/*
809 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
810 * This is done after other initializations, where 'shell' might have been
811 * set, but only if they have not been set before.
812 */
813 char_u *p;
814 int idx_srr;
815 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100816# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 int idx_sp;
818 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000822 if (idx_srr < 0)
823 do_srr = FALSE;
824 else
825 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100826# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000828 if (idx_sp < 0)
829 do_sp = FALSE;
830 else
831 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100832# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200833 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (p != NULL)
835 {
836 /*
837 * Default for p_sp is "| tee", for p_srr is ">".
838 * For known shells it is changed here to include stderr.
839 */
840 if ( fnamecmp(p, "csh") == 0
841 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100842# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 || fnamecmp(p, "csh.exe") == 0
844 || fnamecmp(p, "tcsh.exe") == 0
845# endif
846 )
847 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100848# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (do_sp)
850 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100851# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100853# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
857 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (do_srr)
860 {
861 p_srr = (char_u *)">&";
862 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
863 }
864 }
865 else
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100866 /* Always use bourne shell style redirection if we reach this */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if ( fnamecmp(p, "sh") == 0
868 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200869 || fnamecmp(p, "mksh") == 0
870 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000872 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200874 || fnamecmp(p, "fish") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100875# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 || fnamecmp(p, "cmd") == 0
877 || fnamecmp(p, "sh.exe") == 0
878 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200879 || fnamecmp(p, "mksh.exe") == 0
880 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000882 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 || fnamecmp(p, "bash.exe") == 0
884 || fnamecmp(p, "cmd.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100888# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (do_sp)
890 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100891# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100893# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
897 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (do_srr)
900 {
901 p_srr = (char_u *)">%s 2>&1";
902 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
903 }
904 }
905 vim_free(p);
906 }
907#endif
908
Bram Moolenaar4f974752019-02-17 17:44:42 +0100909#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100911 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
912 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * This is done after other initializations, where 'shell' might have been
914 * set, but only if they have not been set before. Default for p_shcf is
915 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100916 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000918 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 int idx3;
921
922 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000923 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
925 p_shcf = (char_u *)"-c";
926 options[idx3].def_val[VI_DEFAULT] = p_shcf;
927 }
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 /* Somehow Win32 requires the quotes around the redirection too */
930 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000931 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
933 p_sxq = (char_u *)"\"";
934 options[idx3].def_val[VI_DEFAULT] = p_sxq;
935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100937 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
938 {
939 int idx3;
940
941 /*
942 * cmd.exe on Windows will strip the first and last double quote given
943 * on the command line, e.g. most of the time things like:
944 * cmd /c "my path/to/echo" "my args to echo"
945 * become:
946 * my path/to/echo" "my args to echo
947 * when executed.
948 *
Bram Moolenaar034b1152012-02-19 18:19:30 +0100949 * To avoid this, set shellxquote to surround the command in
950 * parenthesis. This appears to make most commands work, without
951 * breaking commands that worked previously, such as
952 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +0100953 */
Bram Moolenaara64ba222012-02-12 23:23:31 +0100954 idx3 = findoption((char_u *)"sxq");
955 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
956 {
Bram Moolenaar034b1152012-02-19 18:19:30 +0100957 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +0100958 options[idx3].def_val[VI_DEFAULT] = p_sxq;
959 }
960
Bram Moolenaara64ba222012-02-12 23:23:31 +0100961 idx3 = findoption((char_u *)"shcf");
962 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
963 {
Bram Moolenaar034b1152012-02-19 18:19:30 +0100964 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +0100965 options[idx3].def_val[VI_DEFAULT] = p_shcf;
966 }
967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#endif
969
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100970 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +0100971 {
972 int idx_ffs = findoption((char_u *)"ffs");
973
974 /* Apply the first entry of 'fileformats' to the initial buffer. */
975 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
976 set_fileformat(default_fileformat(), OPT_LOCAL);
977 }
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef FEAT_TITLE
980 set_title_defaults();
981#endif
982}
983
984#if defined(FEAT_MULTI_LANG) || defined(PROTO)
985/*
986 * When 'helplang' is still at its default value, set it to "lang".
987 * Only the first two characters of "lang" are used.
988 */
989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100990set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
992 int idx;
993
994 if (lang == NULL || STRLEN(lang) < 2) /* safety check */
995 return;
996 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000997 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 if (options[idx].flags & P_ALLOCED)
1000 free_string_option(p_hlg);
1001 p_hlg = vim_strsave(lang);
1002 if (p_hlg == NULL)
1003 p_hlg = empty_option;
1004 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001005 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001006 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001007 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1008 {
1009 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1010 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1011 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001012 // any C like setting, such as C.UTF-8, becomes "en"
1013 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1014 {
1015 p_hlg[0] = 'e';
1016 p_hlg[1] = 'n';
1017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 options[idx].flags |= P_ALLOCED;
1021 }
1022}
1023#endif
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#ifdef FEAT_TITLE
1026/*
1027 * 'title' and 'icon' only default to true if they have not been set or reset
1028 * in .vimrc and we can read the old value.
1029 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1030 * they can be reset. This reduces startup time when using X on a remote
1031 * machine.
1032 */
1033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001034set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
1036 int idx1;
1037 long val;
1038
1039 /*
1040 * If GUI is (going to be) used, we can always set the window title and
1041 * icon name. Saves a bit of time, because the X11 display server does
1042 * not need to be contacted.
1043 */
1044 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001045 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
1047#ifdef FEAT_GUI
1048 if (gui.starting || gui.in_use)
1049 val = TRUE;
1050 else
1051#endif
1052 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001053 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 p_title = val;
1055 }
1056 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001057 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
1059#ifdef FEAT_GUI
1060 if (gui.starting || gui.in_use)
1061 val = TRUE;
1062 else
1063#endif
1064 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001065 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 p_icon = val;
1067 }
1068}
1069#endif
1070
1071/*
1072 * Parse 'arg' for option settings.
1073 *
1074 * 'arg' may be IObuff, but only when no errors can be present and option
1075 * does not need to be expanded with option_expand().
1076 * "opt_flags":
1077 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001078 * OPT_GLOBAL for ":setglobal"
1079 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001081 * OPT_WINONLY to only set window-local options
1082 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 *
1084 * returns FAIL if an error is detected, OK otherwise
1085 */
1086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001087do_set(
1088 char_u *arg, /* option string (may be written to!) */
1089 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001092 char *errmsg;
1093 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 char_u *startarg;
1095 int prefix; /* 1: nothing, 0: "no", 2: "inv" in front of name */
1096 int nextchar; /* next non-white char after option name */
1097 int afterchar; /* character just after option name */
1098 int len;
1099 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001100 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 int key;
1102 long_u flags; /* flags for current option */
1103 char_u *varp = NULL; /* pointer to variable for current option */
1104 int did_show = FALSE; /* already showed one value */
1105 int adding; /* "opt+=arg" */
1106 int prepending; /* "opt^=arg" */
1107 int removing; /* "opt-=arg" */
1108 int cp_val = 0;
1109 char_u key_name[2];
1110
1111 if (*arg == NUL)
1112 {
1113 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001114 did_show = TRUE;
1115 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 }
1117
1118 while (*arg != NUL) /* loop to process all options */
1119 {
1120 errmsg = NULL;
1121 startarg = arg; /* remember for error message */
1122
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001123 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1124 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
1126 /*
1127 * ":set all" show all options.
1128 * ":set all&" set all options to their default value.
1129 */
1130 arg += 3;
1131 if (*arg == '&')
1132 {
1133 ++arg;
1134 /* Only for :set command set global value of local options. */
1135 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001136 didset_options();
1137 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001138 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001143 did_show = TRUE;
1144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001146 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
1148 showoptions(2, opt_flags);
1149 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001150 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 arg += 7;
1152 }
1153 else
1154 {
1155 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001156 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 prefix = 0;
1159 arg += 2;
1160 }
1161 else if (STRNCMP(arg, "inv", 3) == 0)
1162 {
1163 prefix = 2;
1164 arg += 3;
1165 }
1166
1167 /* find end of name */
1168 key = 0;
1169 if (*arg == '<')
1170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 opt_idx = -1;
1172 /* look out for <t_>;> */
1173 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1174 len = 5;
1175 else
1176 {
1177 len = 1;
1178 while (arg[len] != NUL && arg[len] != '>')
1179 ++len;
1180 }
1181 if (arg[len] != '>')
1182 {
1183 errmsg = e_invarg;
1184 goto skip;
1185 }
1186 arg[len] = NUL; /* put NUL after name */
1187 if (arg[1] == 't' && arg[2] == '_') /* could be term code */
1188 opt_idx = findoption(arg + 1);
1189 arg[len++] = '>'; /* restore '>' */
1190 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001191 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 else
1194 {
1195 len = 0;
1196 /*
1197 * The two characters after "t_" may not be alphanumeric.
1198 */
1199 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1200 len = 4;
1201 else
1202 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1203 ++len;
1204 nextchar = arg[len];
1205 arg[len] = NUL; /* put NUL after name */
1206 opt_idx = findoption(arg);
1207 arg[len] = nextchar; /* restore nextchar */
1208 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001209 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211
1212 /* remember character after option name */
1213 afterchar = arg[len];
1214
1215 /* skip white space, allow ":set ai ?" */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001216 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 ++len;
1218
1219 adding = FALSE;
1220 prepending = FALSE;
1221 removing = FALSE;
1222 if (arg[len] != NUL && arg[len + 1] == '=')
1223 {
1224 if (arg[len] == '+')
1225 {
1226 adding = TRUE; /* "+=" */
1227 ++len;
1228 }
1229 else if (arg[len] == '^')
1230 {
1231 prepending = TRUE; /* "^=" */
1232 ++len;
1233 }
1234 else if (arg[len] == '-')
1235 {
1236 removing = TRUE; /* "-=" */
1237 ++len;
1238 }
1239 }
1240 nextchar = arg[len];
1241
1242 if (opt_idx == -1 && key == 0) /* found a mismatch: skip */
1243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001244 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 goto skip;
1246 }
1247
1248 if (opt_idx >= 0)
1249 {
1250 if (options[opt_idx].var == NULL) /* hidden option: skip */
1251 {
1252 /* Only give an error message when requesting the value of
1253 * a hidden option, ignore setting it. */
1254 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1255 && (!(options[opt_idx].flags & P_BOOL)
1256 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001257 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 goto skip;
1259 }
1260
1261 flags = options[opt_idx].flags;
1262 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1263 }
1264 else
1265 {
1266 flags = P_STRING;
1267 if (key < 0)
1268 {
1269 key_name[0] = KEY2TERMCAP0(key);
1270 key_name[1] = KEY2TERMCAP1(key);
1271 }
1272 else
1273 {
1274 key_name[0] = KS_KEY;
1275 key_name[1] = (key & 0xff);
1276 }
1277 }
1278
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001279 /* Skip all options that are not window-local (used when showing
1280 * an already loaded buffer in a window). */
1281 if ((opt_flags & OPT_WINONLY)
1282 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1283 goto skip;
1284
Bram Moolenaara3227e22006-03-08 21:32:40 +00001285 /* Skip all options that are window-local (used for :vimgrep). */
1286 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1287 && options[opt_idx].var == VAR_WIN)
1288 goto skip;
1289
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001290 /* Disallow changing some options from modelines. */
1291 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001293 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001295 errmsg = _("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001296 goto skip;
1297 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001298 if ((flags & P_MLE) && !p_mle)
1299 {
1300 errmsg = _("E992: Not allowed in a modeline when 'modelineexpr' is off");
1301 goto skip;
1302 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001303#ifdef FEAT_DIFF
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001304 /* In diff mode some options are overruled. This avoids that
1305 * 'foldmethod' becomes "marker" instead of "diff" and that
1306 * "wrap" gets set. */
1307 if (curwin->w_p_diff
Bram Moolenaara9d52e32010-07-31 16:44:19 +02001308 && opt_idx >= 0 /* shut up coverity warning */
Bram Moolenaara6c07602017-03-05 21:18:27 +01001309 && (
1310#ifdef FEAT_FOLDING
1311 options[opt_idx].indir == PV_FDM ||
1312#endif
1313 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001314 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
1317
1318#ifdef HAVE_SANDBOX
1319 /* Disallow changing some options in the sandbox */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001320 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001322 errmsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 goto skip;
1324 }
1325#endif
1326
1327 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1328 {
1329 arg += len;
1330 cp_val = p_cp;
1331 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1332 {
1333 if (arg[3] == 'm') /* "opt&vim": set to Vim default */
1334 {
1335 cp_val = FALSE;
1336 arg += 3;
1337 }
1338 else /* "opt&vi": set to Vi default */
1339 {
1340 cp_val = TRUE;
1341 arg += 2;
1342 }
1343 }
1344 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001345 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
1347 errmsg = e_trailing;
1348 goto skip;
1349 }
1350 }
1351
1352 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001353 * allow '=' and ':' for hystorical reasons (MSDOS command.com
1354 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 */
1356 if (nextchar == '?'
1357 || (prefix == 1
1358 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1359 && !(flags & P_BOOL)))
1360 {
1361 /*
1362 * print value
1363 */
1364 if (did_show)
1365 msg_putchar('\n'); /* cursor below last one */
1366 else
1367 {
1368 gotocmdline(TRUE); /* cursor at status line */
1369 did_show = TRUE; /* remember that we did a line */
1370 }
1371 if (opt_idx >= 0)
1372 {
1373 showoneopt(&options[opt_idx], opt_flags);
1374#ifdef FEAT_EVAL
1375 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001376 {
1377 /* Mention where the option was last set. */
1378 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001379 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001380 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001381 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001382 (int)options[opt_idx].indir & PV_MASK]);
1383 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001384 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001385 (int)options[opt_idx].indir & PV_MASK]);
1386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388 }
1389 else
1390 {
1391 char_u *p;
1392
1393 p = find_termcode(key_name);
1394 if (p == NULL)
1395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001396 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 goto skip;
1398 }
1399 else
1400 (void)show_one_termcode(key_name, p, TRUE);
1401 }
1402 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001403 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 errmsg = e_trailing;
1405 }
1406 else
1407 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001408 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001409 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (flags & P_BOOL) /* boolean */
1412 {
1413 if (nextchar == '=' || nextchar == ':')
1414 {
1415 errmsg = e_invarg;
1416 goto skip;
1417 }
1418
1419 /*
1420 * ":set opt!": invert
1421 * ":set opt&": reset to default value
1422 * ":set opt<": reset to global value
1423 */
1424 if (nextchar == '!')
1425 value = *(int *)(varp) ^ 1;
1426 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001427 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 ((flags & P_VI_DEF) || cp_val)
1429 ? VI_DEFAULT : VIM_DEFAULT];
1430 else if (nextchar == '<')
1431 {
1432 /* For 'autoread' -1 means to use global value. */
1433 if ((int *)varp == &curbuf->b_p_ar
1434 && opt_flags == OPT_LOCAL)
1435 value = -1;
1436 else
1437 value = *(int *)get_varp_scope(&(options[opt_idx]),
1438 OPT_GLOBAL);
1439 }
1440 else
1441 {
1442 /*
1443 * ":set invopt": invert
1444 * ":set opt" or ":set noopt": set or reset
1445 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001446 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 {
1448 errmsg = e_trailing;
1449 goto skip;
1450 }
1451 if (prefix == 2) /* inv */
1452 value = *(int *)(varp) ^ 1;
1453 else
1454 value = prefix;
1455 }
1456
1457 errmsg = set_bool_option(opt_idx, varp, (int)value,
1458 opt_flags);
1459 }
1460 else /* numeric or string */
1461 {
1462 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1463 || prefix != 1)
1464 {
1465 errmsg = e_invarg;
1466 goto skip;
1467 }
1468
1469 if (flags & P_NUM) /* numeric */
1470 {
1471 /*
1472 * Different ways to set a number option:
1473 * & set to default value
1474 * < set to global value
1475 * <xx> accept special key codes for 'wildchar'
1476 * c accept any non-digit for 'wildchar'
1477 * [-]0-9 set number
1478 * other error
1479 */
1480 ++arg;
1481 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001482 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 ((flags & P_VI_DEF) || cp_val)
1484 ? VI_DEFAULT : VIM_DEFAULT];
1485 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001486 {
1487 /* For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1488 * use the global value. */
1489 if ((long *)varp == &curbuf->b_p_ul
1490 && opt_flags == OPT_LOCAL)
1491 value = NO_LOCAL_UNDOLEVEL;
1492 else
1493 value = *(long *)get_varp_scope(
1494 &(options[opt_idx]), OPT_GLOBAL);
1495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 else if (((long *)varp == &p_wc
1497 || (long *)varp == &p_wcm)
1498 && (*arg == '<'
1499 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001500 || (*arg != NUL
1501 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 && !VIM_ISDIGIT(*arg))))
1503 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001504 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (value == 0 && (long *)varp != &p_wcm)
1506 {
1507 errmsg = e_invarg;
1508 goto skip;
1509 }
1510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1512 {
Bram Moolenaar18400e62015-01-27 15:58:40 +01001513 /* Allow negative (for 'undolevels'), octal and
1514 * hex numbers. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001515 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001516 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001517 if (i == 0 || (arg[i] != NUL
1518 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001520 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 goto skip;
1522 }
1523 }
1524 else
1525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001526 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 goto skip;
1528 }
1529
1530 if (adding)
1531 value = *(long *)varp + value;
1532 if (prepending)
1533 value = *(long *)varp * value;
1534 if (removing)
1535 value = *(long *)varp - value;
1536 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001537 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
1539 else if (opt_idx >= 0) /* string */
1540 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001541 char_u *save_arg = NULL;
1542 char_u *s = NULL;
1543 char_u *oldval = NULL; /* previous value if *varp */
1544 char_u *newval;
1545 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001546 char_u *origval_l = NULL;
1547 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001548#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001549 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001550 char_u *saved_origval_l = NULL;
1551 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001552 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001553#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001554 unsigned newlen;
1555 int comma;
1556 int bs;
1557 int new_value_alloced; /* new string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 was allocated */
1559
1560 /* When using ":set opt=val" for a global option
1561 * with a local value the local value will be
1562 * reset, use the global value here. */
1563 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001564 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 varp = options[opt_idx].var;
1566
1567 /* The old value is kept until we are sure that the
1568 * new value is valid. */
1569 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001570
Bram Moolenaard7c96872019-06-15 17:12:48 +02001571 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1572 {
1573 origval_l = *(char_u **)get_varp_scope(
1574 &(options[opt_idx]), OPT_LOCAL);
1575 origval_g = *(char_u **)get_varp_scope(
1576 &(options[opt_idx]), OPT_GLOBAL);
1577
1578 // A global-local string option might have an empty
1579 // option as value to indicate that the global
1580 // value should be used.
1581 if (((int)options[opt_idx].indir & PV_BOTH)
1582 && origval_l == empty_option)
1583 origval_l = origval_g;
1584 }
1585
1586 // When setting the local value of a global
1587 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001588 if (((int)options[opt_idx].indir & PV_BOTH)
1589 && (opt_flags & OPT_LOCAL))
1590 origval = *(char_u **)get_varp(
1591 &options[opt_idx]);
1592 else
1593 origval = oldval;
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (nextchar == '&') /* set to default val */
1596 {
1597 newval = options[opt_idx].def_val[
1598 ((flags & P_VI_DEF) || cp_val)
1599 ? VI_DEFAULT : VIM_DEFAULT];
1600 if ((char_u **)varp == &p_bg)
1601 {
1602 /* guess the value of 'background' */
1603#ifdef FEAT_GUI
1604 if (gui.in_use)
1605 newval = gui_bg_default();
1606 else
1607#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001608 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610
1611 /* expand environment variables and ~ (since the
1612 * default value was already expanded, only
1613 * required when an environment variable was set
1614 * later */
1615 if (newval == NULL)
1616 newval = empty_option;
1617 else
1618 {
1619 s = option_expand(opt_idx, newval);
1620 if (s == NULL)
1621 s = newval;
1622 newval = vim_strsave(s);
1623 }
1624 new_value_alloced = TRUE;
1625 }
1626 else if (nextchar == '<') /* set to global val */
1627 {
1628 newval = vim_strsave(*(char_u **)get_varp_scope(
1629 &(options[opt_idx]), OPT_GLOBAL));
1630 new_value_alloced = TRUE;
1631 }
1632 else
1633 {
1634 ++arg; /* jump to after the '=' or ':' */
1635
1636 /*
1637 * Set 'keywordprg' to ":help" if an empty
1638 * value was passed to :set by the user.
1639 * Misuse errbuf[] for the resulting string.
1640 */
1641 if (varp == (char_u *)&p_kp
1642 && (*arg == NUL || *arg == ' '))
1643 {
1644 STRCPY(errbuf, ":help");
1645 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 }
1648 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001649 * Convert 'backspace' number to string, for
1650 * adding, prepending and removing string.
1651 */
1652 else if (varp == (char_u *)&p_bs
1653 && VIM_ISDIGIT(**(char_u **)varp))
1654 {
1655 i = getdigits((char_u **)varp);
1656 switch (i)
1657 {
1658 case 0:
1659 *(char_u **)varp = empty_option;
1660 break;
1661 case 1:
1662 *(char_u **)varp = vim_strsave(
1663 (char_u *)"indent,eol");
1664 break;
1665 case 2:
1666 *(char_u **)varp = vim_strsave(
1667 (char_u *)"indent,eol,start");
1668 break;
1669 }
1670 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001671 if (origval == oldval)
1672 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001673 if (origval_l == oldval)
1674 origval_l = *(char_u **)varp;
1675 if (origval_g == oldval)
1676 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001677 oldval = *(char_u **)varp;
1678 }
1679 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 * Convert 'whichwrap' number to string, for
1681 * backwards compatibility with Vim 3.0.
1682 * Misuse errbuf[] for the resulting string.
1683 */
1684 else if (varp == (char_u *)&p_ww
1685 && VIM_ISDIGIT(*arg))
1686 {
1687 *errbuf = NUL;
1688 i = getdigits(&arg);
1689 if (i & 1)
1690 STRCAT(errbuf, "b,");
1691 if (i & 2)
1692 STRCAT(errbuf, "s,");
1693 if (i & 4)
1694 STRCAT(errbuf, "h,l,");
1695 if (i & 8)
1696 STRCAT(errbuf, "<,>,");
1697 if (i & 16)
1698 STRCAT(errbuf, "[,],");
1699 if (*errbuf != NUL) /* remove trailing , */
1700 errbuf[STRLEN(errbuf) - 1] = NUL;
1701 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001702 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
1704 /*
1705 * Remove '>' before 'dir' and 'bdir', for
1706 * backwards compatibility with version 3.0
1707 */
1708 else if ( *arg == '>'
1709 && (varp == (char_u *)&p_dir
1710 || varp == (char_u *)&p_bdir))
1711 {
1712 ++arg;
1713 }
1714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 /*
1716 * Copy the new string into allocated memory.
1717 * Can't use set_string_option_direct(), because
1718 * we need to remove the backslashes.
1719 */
1720 /* get a bit too much */
1721 newlen = (unsigned)STRLEN(arg) + 1;
1722 if (adding || prepending || removing)
1723 newlen += (unsigned)STRLEN(origval) + 1;
1724 newval = alloc(newlen);
1725 if (newval == NULL) /* out of mem, don't change */
1726 break;
1727 s = newval;
1728
1729 /*
1730 * Copy the string, skip over escaped chars.
1731 * For MS-DOS and WIN32 backslashes before normal
1732 * file name characters are not removed, and keep
1733 * backslash at start, for "\\machine\path", but
1734 * do remove it for "\\\\machine\\path".
1735 * The reverse is found in ExpandOldSetting().
1736 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001737 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
1739 if (*arg == '\\' && arg[1] != NUL
1740#ifdef BACKSLASH_IN_FILENAME
1741 && !((flags & P_EXPAND)
1742 && vim_isfilec(arg[1])
1743 && (arg[1] != '\\'
1744 || (s == newval
1745 && arg[2] != '\\')))
1746#endif
1747 )
1748 ++arg; /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001750 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
1752 /* copy multibyte char */
1753 mch_memmove(s, arg, (size_t)i);
1754 arg += i;
1755 s += i;
1756 }
1757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *s++ = *arg++;
1759 }
1760 *s = NUL;
1761
1762 /*
1763 * Expand environment variables and ~.
1764 * Don't do it when adding without inserting a
1765 * comma.
1766 */
1767 if (!(adding || prepending || removing)
1768 || (flags & P_COMMA))
1769 {
1770 s = option_expand(opt_idx, newval);
1771 if (s != NULL)
1772 {
1773 vim_free(newval);
1774 newlen = (unsigned)STRLEN(s) + 1;
1775 if (adding || prepending || removing)
1776 newlen += (unsigned)STRLEN(origval) + 1;
1777 newval = alloc(newlen);
1778 if (newval == NULL)
1779 break;
1780 STRCPY(newval, s);
1781 }
1782 }
1783
1784 /* locate newval[] in origval[] when removing it
1785 * and when adding to avoid duplicates */
1786 i = 0; /* init for GCC */
1787 if (removing || (flags & P_NODUP))
1788 {
1789 i = (int)STRLEN(newval);
1790 bs = 0;
1791 for (s = origval; *s; ++s)
1792 {
1793 if ((!(flags & P_COMMA)
1794 || s == origval
1795 || (s[-1] == ',' && !(bs & 1)))
1796 && STRNCMP(s, newval, i) == 0
1797 && (!(flags & P_COMMA)
1798 || s[i] == ','
1799 || s[i] == NUL))
1800 break;
Bram Moolenaar0b2f94d2011-03-22 14:35:05 +01001801 /* Count backslashes. Only a comma with an
Bram Moolenaar8f79acd2016-01-01 14:48:20 +01001802 * even number of backslashes or a single
1803 * backslash preceded by a comma before it
1804 * is recognized as a separator */
1805 if ((s > origval + 1
1806 && s[-1] == '\\'
1807 && s[-2] != ',')
1808 || (s == origval + 1
1809 && s[-1] == '\\'))
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 ++bs;
1812 else
1813 bs = 0;
1814 }
1815
1816 /* do not add if already there */
1817 if ((adding || prepending) && *s)
1818 {
1819 prepending = FALSE;
1820 adding = FALSE;
1821 STRCPY(newval, origval);
1822 }
1823 }
1824
1825 /* concatenate the two strings; add a ',' if
1826 * needed */
1827 if (adding || prepending)
1828 {
1829 comma = ((flags & P_COMMA) && *origval != NUL
1830 && *newval != NUL);
1831 if (adding)
1832 {
1833 i = (int)STRLEN(origval);
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001834 /* strip a trailing comma, would get 2 */
Bram Moolenaar17467472015-11-10 17:50:24 +01001835 if (comma && i > 1
1836 && (flags & P_ONECOMMA) == P_ONECOMMA
1837 && origval[i - 1] == ','
1838 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001839 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 mch_memmove(newval + i + comma, newval,
1841 STRLEN(newval) + 1);
1842 mch_memmove(newval, origval, (size_t)i);
1843 }
1844 else
1845 {
1846 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001847 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 if (comma)
1850 newval[i] = ',';
1851 }
1852
1853 /* Remove newval[] from origval[]. (Note: "i" has
1854 * been set above and is used here). */
1855 if (removing)
1856 {
1857 STRCPY(newval, origval);
1858 if (*s)
1859 {
1860 /* may need to remove a comma */
1861 if (flags & P_COMMA)
1862 {
1863 if (s == origval)
1864 {
1865 /* include comma after string */
1866 if (s[i] == ',')
1867 ++i;
1868 }
1869 else
1870 {
1871 /* include comma before string */
1872 --s;
1873 ++i;
1874 }
1875 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001876 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
1878 }
1879
1880 if (flags & P_FLAGLIST)
1881 {
1882 /* Remove flags that appear twice. */
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001883 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001884 {
1885 /* if options have P_FLAGLIST and
1886 * P_ONECOMMA such as 'whichwrap' */
1887 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001889 if (*s != ',' && *(s + 1) == ','
1890 && vim_strchr(s + 2, *s) != NULL)
1891 {
1892 /* Remove the duplicated value and
1893 * the next comma. */
1894 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001895 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001898 else
1899 {
1900 if ((!(flags & P_COMMA) || *s != ',')
1901 && vim_strchr(s + 1, *s) != NULL)
1902 {
1903 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001904 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001905 }
1906 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001907 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910
1911 if (save_arg != NULL) /* number for 'whichwrap' */
1912 arg = save_arg;
1913 new_value_alloced = TRUE;
1914 }
1915
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001916 /*
1917 * Set the new value.
1918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 *(char_u **)(varp) = newval;
1920
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001921#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001922 if (!starting
1923# ifdef FEAT_CRYPT
1924 && options[opt_idx].indir != PV_KEY
1925# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001926 && origval != NULL && newval != NULL)
1927 {
Bram Moolenaar53744302015-07-17 17:38:22 +02001928 /* origval may be freed by
1929 * did_set_string_option(), make a copy. */
1930 saved_origval = vim_strsave(origval);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001931 /* newval (and varp) may become invalid if the
1932 * buffer is closed by autocommands. */
1933 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001934 if (origval_l != NULL)
1935 saved_origval_l = vim_strsave(origval_l);
1936 if (origval_g != NULL)
1937 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001938 }
Bram Moolenaar53744302015-07-17 17:38:22 +02001939#endif
1940
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001941 {
1942 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001943 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001944
1945 // When an option is set in the sandbox, from a
1946 // modeline or in secure mode, then deal with side
1947 // effects in secure mode. Also when the value was
1948 // set with the P_INSECURE flag and is not
1949 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001950 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001951#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001952 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001953#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01001954 || (!value_is_replaced && (*p & P_INSECURE)))
1955 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001956
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001957 // Handle side effects, and set the global value
1958 // for ":set" on local options. Note: when setting
1959 // 'syntax' or 'filetype' autocommands may be
1960 // triggered that can cause havoc.
1961 errmsg = did_set_string_option(
1962 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01001963 new_value_alloced, oldval, errbuf,
1964 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001965
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001966 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001969#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001970 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001971 trigger_optionsset_string(
1972 opt_idx, opt_flags, saved_origval,
1973 saved_origval_l, saved_origval_g,
1974 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001975 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001976 vim_free(saved_origval_l);
1977 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001978 vim_free(saved_newval);
1979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 /* If error detected, print the error message. */
1981 if (errmsg != NULL)
1982 goto skip;
1983 }
1984 else /* key code option */
1985 {
1986 char_u *p;
1987
1988 if (nextchar == '&')
1989 {
1990 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993 else
1994 {
1995 ++arg; /* jump to after the '=' or ':' */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001996 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (*p == '\\' && p[1] != NUL)
1998 ++p;
1999 nextchar = *p;
2000 *p = NUL;
2001 add_termcode(key_name, arg, FALSE);
2002 *p = nextchar;
2003 }
2004 if (full_screen)
2005 ttest(FALSE);
2006 redraw_all_later(CLEAR);
2007 }
2008 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002009
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002011 did_set_option(
2012 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014
2015skip:
2016 /*
2017 * Advance to next argument.
2018 * - skip until a blank found, taking care of backslashes
2019 * - skip blanks
2020 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2021 */
2022 for (i = 0; i < 2 ; ++i)
2023 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002024 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (*arg++ == '\\' && *arg != NUL)
2026 ++arg;
2027 arg = skipwhite(arg);
2028 if (*arg != '=')
2029 break;
2030 }
2031 }
2032
2033 if (errmsg != NULL)
2034 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002035 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002036 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (i + (arg - startarg) < IOSIZE)
2038 {
2039 /* append the argument with the error */
2040 STRCAT(IObuff, ": ");
2041 mch_memmove(IObuff + i, startarg, (arg - startarg));
2042 IObuff[i + (arg - startarg)] = NUL;
2043 }
2044 /* make sure all characters are printable */
2045 trans_characters(IObuff, IOSIZE);
2046
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002047 ++no_wait_return; // wait_return done later
2048 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 --no_wait_return;
2050
2051 return FAIL;
2052 }
2053
2054 arg = skipwhite(arg);
2055 }
2056
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002057theend:
2058 if (silent_mode && did_show)
2059 {
2060 /* After displaying option values in silent mode. */
2061 silent_mode = FALSE;
2062 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2063 msg_putchar('\n');
2064 cursor_on(); /* msg_start() switches it off */
2065 out_flush();
2066 silent_mode = TRUE;
2067 info_message = FALSE; /* use mch_msg(), not mch_errmsg() */
2068 }
2069
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 return OK;
2071}
2072
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002073/*
2074 * Call this when an option has been given a new value through a user command.
2075 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2076 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002078did_set_option(
2079 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002080 int opt_flags, // possibly with OPT_MODELINE
2081 int new_value, // value was replaced completely
2082 int value_checked) // value was checked to be safe, no need to set the
2083 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002084{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002085 long_u *p;
2086
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002087 options[opt_idx].flags |= P_WAS_SET;
2088
2089 /* When an option is set in the sandbox, from a modeline or in secure mode
2090 * set the P_INSECURE flag. Otherwise, if a new value is stored reset the
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002091 * flag. */
2092 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002093 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002094#ifdef HAVE_SANDBOX
2095 || sandbox != 0
2096#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002097 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002098 *p = *p | P_INSECURE;
2099 else if (new_value)
2100 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002101}
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103/*
2104 * Convert a key name or string into a key value.
2105 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002106 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002108 int
2109string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002112 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 if (*arg == '^')
2114 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002115 if (multi_byte)
2116 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 return *arg;
2118}
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#ifdef FEAT_TITLE
2121/*
2122 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2123 * maketitle() to create and display it.
2124 * When switching the title or icon off, call mch_restore_title() to get
2125 * the old value back.
2126 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002127 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002128did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 if (starting != NO_SCREEN
2131#ifdef FEAT_GUI
2132 && !gui.starting
2133#endif
2134 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136}
2137#endif
2138
2139/*
2140 * set_options_bin - called when 'bin' changes value.
2141 */
2142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002143set_options_bin(
2144 int oldval,
2145 int newval,
2146 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 /*
2149 * The option values that are changed when 'bin' changes are
2150 * copied when 'bin is set and restored when 'bin' is reset.
2151 */
2152 if (newval)
2153 {
2154 if (!oldval) /* switched on */
2155 {
2156 if (!(opt_flags & OPT_GLOBAL))
2157 {
2158 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2159 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2160 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2161 curbuf->b_p_et_nobin = curbuf->b_p_et;
2162 }
2163 if (!(opt_flags & OPT_LOCAL))
2164 {
2165 p_tw_nobin = p_tw;
2166 p_wm_nobin = p_wm;
2167 p_ml_nobin = p_ml;
2168 p_et_nobin = p_et;
2169 }
2170 }
2171
2172 if (!(opt_flags & OPT_GLOBAL))
2173 {
2174 curbuf->b_p_tw = 0; /* no automatic line wrap */
2175 curbuf->b_p_wm = 0; /* no automatic line wrap */
2176 curbuf->b_p_ml = 0; /* no modelines */
2177 curbuf->b_p_et = 0; /* no expandtab */
2178 }
2179 if (!(opt_flags & OPT_LOCAL))
2180 {
2181 p_tw = 0;
2182 p_wm = 0;
2183 p_ml = FALSE;
2184 p_et = FALSE;
2185 p_bin = TRUE; /* needed when called for the "-b" argument */
2186 }
2187 }
2188 else if (oldval) /* switched off */
2189 {
2190 if (!(opt_flags & OPT_GLOBAL))
2191 {
2192 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2193 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2194 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2195 curbuf->b_p_et = curbuf->b_p_et_nobin;
2196 }
2197 if (!(opt_flags & OPT_LOCAL))
2198 {
2199 p_tw = p_tw_nobin;
2200 p_wm = p_wm_nobin;
2201 p_ml = p_ml_nobin;
2202 p_et = p_et_nobin;
2203 }
2204 }
2205}
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207/*
2208 * Expand environment variables for some string options.
2209 * These string options cannot be indirect!
2210 * If "val" is NULL expand the current value of the option.
2211 * Return pointer to NameBuff, or NULL when not expanded.
2212 */
2213 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002214option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 /* if option doesn't need expansion nothing to do */
2217 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2218 return NULL;
2219
2220 /* If val is longer than MAXPATHL no meaningful expansion can be done,
2221 * expand_env() would truncate the string. */
2222 if (val != NULL && STRLEN(val) > MAXPATHL)
2223 return NULL;
2224
2225 if (val == NULL)
2226 val = *(char_u **)options[opt_idx].var;
2227
2228 /*
2229 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2230 * Escape spaces when expanding 'tags', they are used to separate file
2231 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002232 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 */
2234 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002235 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002236#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002237 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2238#endif
2239 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (STRCMP(NameBuff, val) == 0) /* they are the same */
2241 return NULL;
2242
2243 return NameBuff;
2244}
2245
2246/*
2247 * After setting various option values: recompute variables that depend on
2248 * option values.
2249 */
2250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002251didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 /* initialize the table for 'iskeyword' et.al. */
2254 (void)init_chartab();
2255
Bram Moolenaardac13472019-09-16 21:06:21 +02002256 didset_string_options();
2257
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002258#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002259 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002260 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002261 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002262 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002265 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 (void)check_cedit();
2267#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002268#ifdef FEAT_LINEBREAK
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002269 /* initialize the table for 'breakat'. */
2270 fill_breakat_flags();
2271#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002272 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002273}
2274
2275/*
2276 * More side effects of setting options.
2277 */
2278 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002279didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002280{
2281 /* Initialize the highlight_attr[] table. */
2282 (void)highlight_changed();
2283
2284 /* Parse default for 'wildmode' */
2285 check_opt_wim();
2286
2287 (void)set_chars_option(&p_lcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002288 /* Parse default for 'fillchars'. */
2289 (void)set_chars_option(&p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002290
2291#ifdef FEAT_CLIPBOARD
2292 /* Parse default for 'clipboard' */
2293 (void)check_clipboard_option();
2294#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002295#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002296 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002297 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002298 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002299 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301}
2302
2303/*
2304 * Check for string options that are NULL (normally only termcap options).
2305 */
2306 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002307check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 int opt_idx;
2310
2311 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2312 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2313 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2314}
2315
2316/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002317 * Return the option index found by a pointer into term_strings[].
2318 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002320 int
2321get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002323 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2326 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002327 return opt_idx;
2328 return -1; // cannot happen: didn't find it!
2329}
2330
2331/*
2332 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2333 * Return the option index or -1 if not found.
2334 */
2335 int
2336set_term_option_alloced(char_u **p)
2337{
2338 int opt_idx = get_term_opt_idx(p);
2339
2340 if (opt_idx >= 0)
2341 options[opt_idx].flags |= P_ALLOCED;
2342 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343}
2344
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002345#if defined(FEAT_EVAL) || defined(PROTO)
2346/*
2347 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2348 * Return FALSE when it wasn't.
2349 * Return -1 for an unknown option.
2350 */
2351 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002352was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002353{
2354 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002355 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002356
2357 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002358 {
2359 flagp = insecure_flag(idx, opt_flags);
2360 return (*flagp & P_INSECURE) != 0;
2361 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002362 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002363 return -1;
2364}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002365
2366/*
2367 * Get a pointer to the flags used for the P_INSECURE flag of option
2368 * "opt_idx". For some local options a local flags field is used.
2369 */
2370 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002371insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002372{
2373 if (opt_flags & OPT_LOCAL)
2374 switch ((int)options[opt_idx].indir)
2375 {
2376#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002377 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002378#endif
2379#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002380# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002381 case PV_FDE: return &curwin->w_p_fde_flags;
2382 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002383# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002384# ifdef FEAT_BEVAL
2385 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2386# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002387# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002388 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002389# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002390 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002391# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002392 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002393# endif
2394#endif
2395 }
2396
2397 /* Nothing special, return global flags field. */
2398 return &options[opt_idx].flags;
2399}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002400#endif
2401
Bram Moolenaardac13472019-09-16 21:06:21 +02002402#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002403/*
2404 * Redraw the window title and/or tab page text later.
2405 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002406void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002407{
2408 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002409 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002410}
2411#endif
2412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002414 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2415 * characters or characters in "allowed".
2416 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002417 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002418valid_name(char_u *val, char *allowed)
2419{
2420 char_u *s;
2421
2422 for (s = val; *s != NUL; ++s)
2423 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2424 return FALSE;
2425 return TRUE;
2426}
2427
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002428#if defined(FEAT_EVAL) || defined(PROTO)
2429/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002430 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002431 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002432 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002433 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002434set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002435{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002436 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2437 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002438 sctx_T new_script_ctx = script_ctx;
2439
2440 new_script_ctx.sc_lnum += sourcing_lnum;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002441
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002442 /* Remember where the option was set. For local options need to do that
2443 * in the buffer or window structure. */
2444 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002445 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002446 if (both || (opt_flags & OPT_LOCAL))
2447 {
2448 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002449 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002450 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002451 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002452 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002453}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002454
2455/*
2456 * Set the script_ctx for a termcap option.
2457 * "name" must be the two character code, e.g. "RV".
2458 * When "name" is NULL use "opt_idx".
2459 */
2460 void
2461set_term_option_sctx_idx(char *name, int opt_idx)
2462{
2463 char_u buf[5];
2464 int idx;
2465
2466 if (name == NULL)
2467 idx = opt_idx;
2468 else
2469 {
2470 buf[0] = 't';
2471 buf[1] = '_';
2472 buf[2] = name[0];
2473 buf[3] = name[1];
2474 buf[4] = 0;
2475 idx = findoption(buf);
2476 }
2477 if (idx >= 0)
2478 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2479}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002480#endif
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482/*
2483 * Set the value of a boolean option, and take care of side effects.
2484 * Returns NULL for success, or an error message for an error.
2485 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002486 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002487set_bool_option(
2488 int opt_idx, /* index in options[] table */
2489 char_u *varp, /* pointer to the option variable */
2490 int value, /* new value */
2491 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002494#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002495 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 /* Disallow changing some options from secure mode */
2499 if ((secure
2500#ifdef HAVE_SANDBOX
2501 || sandbox != 0
2502#endif
2503 ) && (options[opt_idx].flags & P_SECURE))
2504 return e_secure;
2505
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002506#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002507 // Save the global value before changing anything. This is needed as for
2508 // a global-only option setting the "local value" in fact sets the global
2509 // value (since there is only one value).
2510 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2511 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2512 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002513#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002514
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 *(int *)varp = value; /* set the new value */
2516#ifdef FEAT_EVAL
2517 /* Remember where the option was set. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002518 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519#endif
2520
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002521#ifdef FEAT_GUI
2522 need_mouse_correct = TRUE;
2523#endif
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /* May set global value for local option. */
2526 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2527 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2528
2529 /*
2530 * Handle side effects of changing a bool option.
2531 */
2532
2533 /* 'compatible' */
2534 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
Bram Moolenaar920694c2016-08-21 17:45:02 +02002537#ifdef FEAT_LANGMAP
2538 if ((int *)varp == &p_lrm)
2539 /* 'langremap' -> !'langnoremap' */
2540 p_lnr = !p_lrm;
2541 else if ((int *)varp == &p_lnr)
2542 /* 'langnoremap' -> !'langremap' */
2543 p_lrm = !p_lnr;
2544#endif
2545
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002546#ifdef FEAT_SYN_HL
2547 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2548 reset_cursorline();
2549#endif
2550
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002551#ifdef FEAT_PERSISTENT_UNDO
2552 /* 'undofile' */
2553 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2554 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002555 /* Only take action when the option was set. When reset we do not
2556 * delete the undo file, the option may be set again without making
2557 * any changes in between. */
2558 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002559 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002560 char_u hash[UNDO_HASH_SIZE];
2561 buf_T *save_curbuf = curbuf;
2562
Bram Moolenaar29323592016-07-24 22:04:11 +02002563 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002564 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002565 /* When 'undofile' is set globally: for every buffer, otherwise
2566 * only for the current buffer: Try to read in the undofile,
2567 * if one exists, the buffer wasn't changed and the buffer was
2568 * loaded */
2569 if ((curbuf == save_curbuf
2570 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2571 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2572 {
2573 u_compute_hash(hash);
2574 u_read_undo(NULL, hash, curbuf->b_fname);
2575 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002576 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002577 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002578 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002579 }
2580#endif
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 else if ((int *)varp == &curbuf->b_p_ro)
2583 {
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002584 /* when 'readonly' is reset globally, also reset readonlymode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2586 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002587
2588 /* when 'readonly' is set may give W10 again */
2589 if (curbuf->b_p_ro)
2590 curbuf->b_did_warn = FALSE;
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002593 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#endif
2595 }
2596
Bram Moolenaar9c449722010-07-20 18:44:27 +02002597#ifdef FEAT_GUI
2598 else if ((int *)varp == &p_mh)
2599 {
2600 if (!p_mh)
2601 gui_mch_mousehide(FALSE);
2602 }
2603#endif
2604
Bram Moolenaara539df02010-08-01 14:35:05 +02002605 /* when 'modifiable' is changed, redraw the window title */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002607 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002608# ifdef FEAT_TERMINAL
2609 /* Cannot set 'modifiable' when in Terminal mode. */
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002610 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2611 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002612 {
2613 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002614 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002615 }
2616# endif
2617# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002618 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002619# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002620 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002621#ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 /* when 'endofline' is changed, redraw the window title */
2623 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002624 {
2625 redraw_titles();
2626 }
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002627 /* when 'fixeol' is changed, redraw the window title */
2628 else if ((int *)varp == &curbuf->b_p_fixeol)
2629 {
2630 redraw_titles();
2631 }
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002632 /* when 'bomb' is changed, redraw the window title and tab page text */
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002633 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002634 {
2635 redraw_titles();
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637#endif
2638
2639 /* when 'bin' is set also set some other options */
2640 else if ((int *)varp == &curbuf->b_p_bin)
2641 {
2642 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2643#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002644 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#endif
2646 }
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 /* when 'buflisted' changes, trigger autocommands */
2649 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2650 {
2651 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2652 NULL, NULL, TRUE, curbuf);
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
2655 /* when 'swf' is set, create swapfile, when reset remove swapfile */
2656 else if ((int *)varp == &curbuf->b_p_swf)
2657 {
2658 if (curbuf->b_p_swf && p_uc)
2659 ml_open_file(curbuf); /* create the swap file */
2660 else
Bram Moolenaard55de222007-05-06 13:38:48 +00002661 /* no need to reset curbuf->b_may_swap, ml_open_file() will check
2662 * buf->b_p_swf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 mf_close_file(curbuf, TRUE); /* remove the swap file */
2664 }
2665
2666 /* when 'terse' is set change 'shortmess' */
2667 else if ((int *)varp == &p_terse)
2668 {
2669 char_u *p;
2670
2671 p = vim_strchr(p_shm, SHM_SEARCH);
2672
2673 /* insert 's' in p_shm */
2674 if (p_terse && p == NULL)
2675 {
2676 STRCPY(IObuff, p_shm);
2677 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002678 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
2680 /* remove 's' from p_shm */
2681 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002682 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
2684
2685 /* when 'paste' is set or reset also change other options */
2686 else if ((int *)varp == &p_paste)
2687 {
2688 paste_option_changed();
2689 }
2690
2691 /* when 'insertmode' is set from an autocommand need to do work here */
2692 else if ((int *)varp == &p_im)
2693 {
2694 if (p_im)
2695 {
2696 if ((State & INSERT) == 0)
2697 need_start_insertmode = TRUE;
2698 stop_insert_mode = FALSE;
2699 }
Bram Moolenaar00672e12016-06-26 18:38:13 +02002700 /* only reset if it was set previously */
2701 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
2703 need_start_insertmode = FALSE;
2704 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002705 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 clear_cmdline = TRUE; /* remove "(insert)" */
2707 restart_edit = 0;
2708 }
2709 }
2710
2711 /* when 'ignorecase' is set or reset and 'hlsearch' is set, redraw */
2712 else if ((int *)varp == &p_ic && p_hls)
2713 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002714 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716
2717#ifdef FEAT_SEARCH_EXTRA
2718 /* when 'hlsearch' is set or reset: reset no_hlsearch */
2719 else if ((int *)varp == &p_hls)
2720 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002721 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723#endif
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 /* when 'scrollbind' is set: snapshot the current position to avoid a jump
2726 * at the end of normal_cmd() */
2727 else if ((int *)varp == &curwin->w_p_scb)
2728 {
2729 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002732 curwin->w_scbind_pos = curwin->w_topline;
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
Bram Moolenaar4033c552017-09-16 20:54:51 +02002736#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 /* There can be only one window with 'previewwindow' set. */
2738 else if ((int *)varp == &curwin->w_p_pvw)
2739 {
2740 if (curwin->w_p_pvw)
2741 {
2742 win_T *win;
2743
Bram Moolenaar29323592016-07-24 22:04:11 +02002744 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (win->w_p_pvw && win != curwin)
2746 {
2747 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002748 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 }
2751 }
2752#endif
2753
2754 /* when 'textmode' is set or reset also change 'fileformat' */
2755 else if ((int *)varp == &curbuf->b_p_tx)
2756 {
2757 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2758 }
2759
2760 /* when 'textauto' is set or reset also change 'fileformats' */
2761 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 set_string_option_direct((char_u *)"ffs", -1,
2764 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002765 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768 /*
2769 * When 'lisp' option changes include/exclude '-' in
2770 * keyword characters.
2771 */
2772#ifdef FEAT_LISP
2773 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2774 {
2775 (void)buf_init_chartab(curbuf, FALSE); /* ignore errors */
2776 }
2777#endif
2778
2779#ifdef FEAT_TITLE
2780 /* when 'title' changed, may need to change the title; same for 'icon' */
Bram Moolenaar84a93082018-06-16 22:58:15 +02002781 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002783 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785#endif
2786
2787 else if ((int *)varp == &curbuf->b_changed)
2788 {
2789 if (!value)
2790 save_file_ff(curbuf); /* Buffer is unchanged */
2791#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002792 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796
2797#ifdef BACKSLASH_IN_FILENAME
2798 else if ((int *)varp == &p_ssl)
2799 {
2800 if (p_ssl)
2801 {
2802 psepc = '/';
2803 psepcN = '\\';
2804 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 else
2807 {
2808 psepc = '\\';
2809 psepcN = '/';
2810 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812
2813 /* need to adjust the file name arguments and buffer names. */
2814 buflist_slash_adjust();
2815 alist_slash_adjust();
2816# ifdef FEAT_EVAL
2817 scriptnames_slash_adjust();
2818# endif
2819 }
2820#endif
2821
2822 /* If 'wrap' is set, set w_leftcol to zero. */
2823 else if ((int *)varp == &curwin->w_p_wrap)
2824 {
2825 if (curwin->w_p_wrap)
2826 curwin->w_leftcol = 0;
2827 }
2828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 else if ((int *)varp == &p_ea)
2830 {
2831 if (p_ea && !old_value)
2832 win_equal(curwin, FALSE, 0);
2833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 else if ((int *)varp == &p_wiv)
2836 {
2837 /*
2838 * When 'weirdinvert' changed, set/reset 't_xs'.
2839 * Then set 'weirdinvert' according to value of 't_xs'.
2840 */
2841 if (p_wiv && !old_value)
2842 T_XS = (char_u *)"y";
2843 else if (!p_wiv && old_value)
2844 T_XS = empty_option;
2845 p_wiv = (*T_XS != NUL);
2846 }
2847
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002848#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 else if ((int *)varp == &p_beval)
2850 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002851 if (!balloonEvalForTerm)
2852 {
2853 if (p_beval && !old_value)
2854 gui_mch_enable_beval_area(balloonEval);
2855 else if (!p_beval && old_value)
2856 gui_mch_disable_beval_area(balloonEval);
2857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002859#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002860#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002861 else if ((int *)varp == &p_bevalterm)
2862 {
2863 mch_bevalterm_changed();
2864 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002867#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else if ((int *)varp == &p_acd)
2869 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002870 /* Change directories when the 'acd' option is set now. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02002871 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873#endif
2874
2875#ifdef FEAT_DIFF
2876 /* 'diff' */
2877 else if ((int *)varp == &curwin->w_p_diff)
2878 {
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002879 /* May add or remove the buffer from the list of diff buffers. */
2880 diff_buf_adjust(curwin);
2881# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 if (foldmethodIsDiff(curwin))
2883 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002884# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
2886#endif
2887
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002888#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 /* 'imdisable' */
2890 else if ((int *)varp == &p_imdisable)
2891 {
2892 /* Only de-activate it here, it will be enabled when changing mode. */
2893 if (p_imdisable)
2894 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02002895 else if (State & INSERT)
2896 /* When the option is set from an autocommand, it may need to take
2897 * effect right away. */
2898 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900#endif
2901
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002902#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002903 /* 'spell' */
2904 else if ((int *)varp == &curwin->w_p_spell)
2905 {
2906 if (curwin->w_p_spell)
2907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002908 char *errmsg = did_set_spelllang(curwin);
2909
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002910 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002911 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002912 }
2913 }
2914#endif
2915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916#ifdef FEAT_ARABIC
2917 if ((int *)varp == &curwin->w_p_arab)
2918 {
2919 if (curwin->w_p_arab)
2920 {
2921 /*
2922 * 'arabic' is set, handle various sub-settings.
2923 */
2924 if (!p_tbidi)
2925 {
2926 /* set rightleft mode */
2927 if (!curwin->w_p_rl)
2928 {
2929 curwin->w_p_rl = TRUE;
2930 changed_window_setting();
2931 }
2932
2933 /* Enable Arabic shaping (major part of what Arabic requires) */
2934 if (!p_arshape)
2935 {
2936 p_arshape = TRUE;
2937 redraw_later_clear();
2938 }
2939 }
2940
2941 /* Arabic requires a utf-8 encoding, inform the user if its not
2942 * set. */
2943 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002944 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00002945 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
2946
Bram Moolenaar8820b482017-03-16 17:23:31 +01002947 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01002948 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00002949#ifdef FEAT_EVAL
2950 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
2951#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 /* set 'delcombine' */
2955 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957# ifdef FEAT_KEYMAP
2958 /* Force-set the necessary keymap for arabic */
2959 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
2960 OPT_LOCAL);
2961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
2963 else
2964 {
2965 /*
2966 * 'arabic' is reset, handle various sub-settings.
2967 */
2968 if (!p_tbidi)
2969 {
2970 /* reset rightleft mode */
2971 if (curwin->w_p_rl)
2972 {
2973 curwin->w_p_rl = FALSE;
2974 changed_window_setting();
2975 }
2976
2977 /* 'arabicshape' isn't reset, it is a global option and
2978 * another window may still need it "on". */
2979 }
2980
2981 /* 'delcombine' isn't reset, it is a global option and another
2982 * window may still want it "on". */
2983
2984# ifdef FEAT_KEYMAP
2985 /* Revert to the default keymap */
2986 curbuf->b_p_iminsert = B_IMODE_NONE;
2987 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2988# endif
2989 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00002990 }
2991
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00002992#endif
2993
Bram Moolenaar44826212019-08-22 21:23:20 +02002994#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
2995 else if (((int *)varp == &curwin->w_p_nu
2996 || (int *)varp == &curwin->w_p_rnu)
2997 && gui.in_use
2998 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
2999 && curbuf->b_signlist != NULL)
3000 {
3001 // If the 'number' or 'relativenumber' options are modified and
3002 // 'signcolumn' is set to 'number', then clear the screen for a full
3003 // refresh. Otherwise the sign icons are not displayed properly in the
3004 // number column. If the 'number' option is set and only the
3005 // 'relativenumber' option is toggled, then don't refresh the screen
3006 // (optimization).
3007 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3008 redraw_all_later(CLEAR);
3009 }
3010#endif
3011
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003012#ifdef FEAT_TERMGUICOLORS
3013 /* 'termguicolors' */
3014 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003015 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003016# ifdef FEAT_VTP
3017 /* Do not turn on 'tgc' when 24-bit colors are not supported. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003018 if (
3019# ifdef VIMDLL
3020 !gui.in_use && !gui.starting &&
3021# endif
3022 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003023 {
3024 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003025 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003026 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003027 if (is_term_win32())
3028 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003029# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003030# ifdef FEAT_GUI
3031 if (!gui.in_use && !gui.starting)
3032# endif
3033 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003034# ifdef FEAT_VTP
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003035 /* reset t_Co */
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003036 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003037 {
3038 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003039 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003040 init_highlight(TRUE, FALSE);
3041 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003042# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003043 }
3044#endif
3045
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 /*
3047 * End of handling side effects for bool options.
3048 */
3049
Bram Moolenaar53744302015-07-17 17:38:22 +02003050 /* after handling side effects, call autocommand */
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 options[opt_idx].flags |= P_WAS_SET;
3053
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003054#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003055 // Don't do this while starting up or recursively.
3056 if (!starting && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02003057 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02003058 char_u buf_old[2], buf_old_global[2], buf_new[2], buf_type[7];
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003059
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003060 vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003061 vim_snprintf((char *)buf_old_global, 2, "%d",
3062 old_global_value ? TRUE: FALSE);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003063 vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003064 vim_snprintf((char *)buf_type, 7, "%s",
3065 (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02003066 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3067 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3068 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003069 if (opt_flags & OPT_LOCAL)
3070 {
3071 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3072 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3073 }
3074 if (opt_flags & OPT_GLOBAL)
3075 {
3076 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3077 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3078 }
3079 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3080 {
3081 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3082 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3083 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3084 }
3085 if (opt_flags & OPT_MODELINE)
3086 {
3087 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3088 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3089 }
3090 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3091 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003092 reset_v_option_vars();
3093 }
3094#endif
3095
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 comp_col(); /* in case 'ruler' or 'showcmd' changed */
Bram Moolenaar913077c2012-03-28 19:59:04 +02003097 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003098 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003099 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 check_redraw(options[opt_idx].flags);
3101
3102 return NULL;
3103}
3104
3105/*
3106 * Set the value of a number option, and take care of side effects.
3107 * Returns NULL for success, or an error message for an error.
3108 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003109 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003110set_num_option(
3111 int opt_idx, /* index in options[] table */
3112 char_u *varp, /* pointer to the option variable */
3113 long value, /* new value */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003114 char *errbuf, /* buffer for error messages */
Bram Moolenaar9b578142016-01-30 19:39:49 +01003115 size_t errbuflen, /* length of "errbuf" */
3116 int opt_flags) /* OPT_LOCAL, OPT_GLOBAL and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 OPT_MODELINE */
3118{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003119 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003121#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003122 long old_global_value = 0; // only used when setting a local and
3123 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003124#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003125 long old_Rows = Rows; // remember old Rows
3126 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 long *pp = (long *)varp;
3128
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003129 /* Disallow changing some options from secure mode. */
3130 if ((secure
3131#ifdef HAVE_SANDBOX
3132 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003134 ) && (options[opt_idx].flags & P_SECURE))
3135 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003137#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003138 // Save the global value before changing anything. This is needed as for
3139 // a global-only option setting the "local value" infact sets the global
3140 // value (since there is only one value).
3141 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003142 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3143 OPT_GLOBAL);
3144#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 *pp = value;
3147#ifdef FEAT_EVAL
3148 /* Remember where the option was set. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003149 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003151#ifdef FEAT_GUI
3152 need_mouse_correct = TRUE;
3153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
Bram Moolenaar14f24742012-08-08 18:01:05 +02003155 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003158#ifdef FEAT_VARTABS
3159 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3160 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3161 ? tabstop_first(curbuf->b_p_vts_array)
3162 : curbuf->b_p_ts;
3163#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167
3168 /*
3169 * Number options that need some action when changed
3170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (pp == &p_wh || pp == &p_hh)
3172 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003173 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (p_wh < 1)
3175 {
3176 errmsg = e_positive;
3177 p_wh = 1;
3178 }
3179 if (p_wmh > p_wh)
3180 {
3181 errmsg = e_winheight;
3182 p_wh = p_wmh;
3183 }
3184 if (p_hh < 0)
3185 {
3186 errmsg = e_positive;
3187 p_hh = 0;
3188 }
3189
3190 /* Change window height NOW */
Bram Moolenaar459ca562016-11-10 18:16:33 +01003191 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 {
3193 if (pp == &p_wh && curwin->w_height < p_wh)
3194 win_setheight((int)p_wh);
3195 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3196 win_setheight((int)p_hh);
3197 }
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 else if (pp == &p_wmh)
3200 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003201 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (p_wmh < 0)
3203 {
3204 errmsg = e_positive;
3205 p_wmh = 0;
3206 }
3207 if (p_wmh > p_wh)
3208 {
3209 errmsg = e_winheight;
3210 p_wmh = p_wh;
3211 }
3212 win_setminheight();
3213 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003214 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003216 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (p_wiw < 1)
3218 {
3219 errmsg = e_positive;
3220 p_wiw = 1;
3221 }
3222 if (p_wmw > p_wiw)
3223 {
3224 errmsg = e_winwidth;
3225 p_wiw = p_wmw;
3226 }
3227
3228 /* Change window width NOW */
Bram Moolenaar459ca562016-11-10 18:16:33 +01003229 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 win_setwidth((int)p_wiw);
3231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 else if (pp == &p_wmw)
3233 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003234 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (p_wmw < 0)
3236 {
3237 errmsg = e_positive;
3238 p_wmw = 0;
3239 }
3240 if (p_wmw > p_wiw)
3241 {
3242 errmsg = e_winwidth;
3243 p_wmw = p_wiw;
3244 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003245 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 /* (re)set last window status line */
3249 else if (pp == &p_ls)
3250 {
3251 last_status(FALSE);
3252 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003253
3254 /* (re)set tab page line */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003255 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003256 {
3257 shell_new_rows(); /* recompute window positions and heights */
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260#ifdef FEAT_GUI
3261 else if (pp == &p_linespace)
3262 {
Bram Moolenaar02743632005-07-25 20:42:36 +00003263 /* Recompute gui.char_height and resize the Vim window to keep the
3264 * same number of lines. */
3265 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003266 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268#endif
3269
3270#ifdef FEAT_FOLDING
3271 /* 'foldlevel' */
3272 else if (pp == &curwin->w_p_fdl)
3273 {
3274 if (curwin->w_p_fdl < 0)
3275 curwin->w_p_fdl = 0;
3276 newFoldLevel();
3277 }
3278
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003279 /* 'foldminlines' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 else if (pp == &curwin->w_p_fml)
3281 {
3282 foldUpdateAll(curwin);
3283 }
3284
3285 /* 'foldnestmax' */
3286 else if (pp == &curwin->w_p_fdn)
3287 {
3288 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3289 foldUpdateAll(curwin);
3290 }
3291
3292 /* 'foldcolumn' */
3293 else if (pp == &curwin->w_p_fdc)
3294 {
3295 if (curwin->w_p_fdc < 0)
3296 {
3297 errmsg = e_positive;
3298 curwin->w_p_fdc = 0;
3299 }
3300 else if (curwin->w_p_fdc > 12)
3301 {
3302 errmsg = e_invarg;
3303 curwin->w_p_fdc = 12;
3304 }
3305 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003306#endif /* FEAT_FOLDING */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003308#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /* 'shiftwidth' or 'tabstop' */
3310 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3311 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003312# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 if (foldmethodIsIndent(curwin))
3314 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003315# endif
3316# ifdef FEAT_CINDENT
3317 /* When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3318 * parse 'cinoptions'. */
3319 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3320 parse_cino(curbuf);
3321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003325 /* 'maxcombine' */
3326 else if (pp == &p_mco)
3327 {
3328 if (p_mco > MAX_MCO)
3329 p_mco = MAX_MCO;
3330 else if (p_mco < 0)
3331 p_mco = 0;
3332 screenclear(); /* will re-allocate the screen */
3333 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003334
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 else if (pp == &curbuf->b_p_iminsert)
3336 {
3337 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3338 {
3339 errmsg = e_invarg;
3340 curbuf->b_p_iminsert = B_IMODE_NONE;
3341 }
3342 p_iminsert = curbuf->b_p_iminsert;
3343 if (termcap_active) /* don't do this in the alternate screen */
3344 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003345#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 /* Show/unshow value of 'keymap' in status lines. */
3347 status_redraw_curbuf();
3348#endif
3349 }
3350
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003351#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3352 /* 'imstyle' */
3353 else if (pp == &p_imst)
3354 {
3355 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3356 errmsg = e_invarg;
3357 }
3358#endif
3359
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003360 else if (pp == &p_window)
3361 {
3362 if (p_window < 1)
3363 p_window = 1;
3364 else if (p_window >= Rows)
3365 p_window = Rows - 1;
3366 }
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 else if (pp == &curbuf->b_p_imsearch)
3369 {
3370 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3371 {
3372 errmsg = e_invarg;
3373 curbuf->b_p_imsearch = B_IMODE_NONE;
3374 }
3375 p_imsearch = curbuf->b_p_imsearch;
3376 }
3377
3378#ifdef FEAT_TITLE
3379 /* if 'titlelen' has changed, redraw the title */
3380 else if (pp == &p_titlelen)
3381 {
3382 if (p_titlelen < 0)
3383 {
3384 errmsg = e_positive;
3385 p_titlelen = 85;
3386 }
3387 if (starting != NO_SCREEN && old_value != p_titlelen)
3388 need_maketitle = TRUE;
3389 }
3390#endif
3391
3392 /* if p_ch changed value, change the command line height */
3393 else if (pp == &p_ch)
3394 {
3395 if (p_ch < 1)
3396 {
3397 errmsg = e_positive;
3398 p_ch = 1;
3399 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003400 if (p_ch > Rows - min_rows() + 1)
3401 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403 /* Only compute the new window layout when startup has been
3404 * completed. Otherwise the frame sizes may be wrong. */
3405 if (p_ch != old_value && full_screen
3406#ifdef FEAT_GUI
3407 && !gui.starting
3408#endif
3409 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003410 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412
3413 /* when 'updatecount' changes from zero to non-zero, open swap files */
3414 else if (pp == &p_uc)
3415 {
3416 if (p_uc < 0)
3417 {
3418 errmsg = e_positive;
3419 p_uc = 100;
3420 }
3421 if (p_uc && !old_value)
3422 ml_open_files();
3423 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003424#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003425 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003426 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003427 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003428 {
3429 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003430 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003431 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003432 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003433 {
3434 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003435 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003436 }
3437 }
3438#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003439#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003440 else if (pp == &p_mzq)
3441 mzvim_reset_timer();
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003444#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
3445 /* 'pyxversion' */
3446 else if (pp == &p_pyx)
3447 {
3448 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3449 errmsg = e_invarg;
3450 }
3451#endif
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 /* sync undo before 'undolevels' changes */
3454 else if (pp == &p_ul)
3455 {
3456 /* use the old value, otherwise u_sync() may not work properly */
3457 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003458 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 p_ul = value;
3460 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003461 else if (pp == &curbuf->b_p_ul)
3462 {
3463 /* use the old value, otherwise u_sync() may not work properly */
3464 curbuf->b_p_ul = old_value;
3465 u_sync(TRUE);
3466 curbuf->b_p_ul = value;
3467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003469#ifdef FEAT_LINEBREAK
3470 /* 'numberwidth' must be positive */
3471 else if (pp == &curwin->w_p_nuw)
3472 {
3473 if (curwin->w_p_nuw < 1)
3474 {
3475 errmsg = e_positive;
3476 curwin->w_p_nuw = 1;
3477 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003478 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003479 {
3480 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003481 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003482 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003483 curwin->w_nrwidth_line_count = 0; /* trigger a redraw */
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003484 }
3485#endif
3486
Bram Moolenaar1a384422010-07-14 19:53:30 +02003487 else if (pp == &curbuf->b_p_tw)
3488 {
3489 if (curbuf->b_p_tw < 0)
3490 {
3491 errmsg = e_positive;
3492 curbuf->b_p_tw = 0;
3493 }
3494#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003495 {
3496 win_T *wp;
3497 tabpage_T *tp;
3498
3499 FOR_ALL_TAB_WINDOWS(tp, wp)
3500 check_colorcolumn(wp);
3501 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003502#endif
3503 }
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 /*
3506 * Check the bounds for numeric options here
3507 */
3508 if (Rows < min_rows() && full_screen)
3509 {
3510 if (errbuf != NULL)
3511 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003512 vim_snprintf((char *)errbuf, errbuflen,
3513 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 errmsg = errbuf;
3515 }
3516 Rows = min_rows();
3517 }
3518 if (Columns < MIN_COLUMNS && full_screen)
3519 {
3520 if (errbuf != NULL)
3521 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003522 vim_snprintf((char *)errbuf, errbuflen,
3523 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 errmsg = errbuf;
3525 }
3526 Columns = MIN_COLUMNS;
3527 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003528 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 /*
3531 * If the screen (shell) height has been changed, assume it is the
3532 * physical screenheight.
3533 */
3534 if (old_Rows != Rows || old_Columns != Columns)
3535 {
3536 /* Changing the screen size is not allowed while updating the screen. */
3537 if (updating_screen)
3538 *pp = old_value;
3539 else if (full_screen
3540#ifdef FEAT_GUI
3541 && !gui.starting
3542#endif
3543 )
3544 set_shellsize((int)Columns, (int)Rows, TRUE);
3545 else
3546 {
3547 /* Postpone the resizing; check the size and cmdline position for
3548 * messages. */
3549 check_shellsize();
3550 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3551 cmdline_row = Rows - p_ch;
3552 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003553 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003554 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (curbuf->b_p_ts <= 0)
3558 {
3559 errmsg = e_positive;
3560 curbuf->b_p_ts = 8;
3561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (p_tm < 0)
3563 {
3564 errmsg = e_positive;
3565 p_tm = 0;
3566 }
3567 if ((curwin->w_p_scr <= 0
3568 || (curwin->w_p_scr > curwin->w_height
3569 && curwin->w_height > 0))
3570 && full_screen)
3571 {
3572 if (pp == &(curwin->w_p_scr))
3573 {
3574 if (curwin->w_p_scr != 0)
3575 errmsg = e_scroll;
3576 win_comp_scroll(curwin);
3577 }
3578 /* If 'scroll' became invalid because of a side effect silently adjust
3579 * it. */
3580 else if (curwin->w_p_scr <= 0)
3581 curwin->w_p_scr = 1;
3582 else /* curwin->w_p_scr > curwin->w_height */
3583 curwin->w_p_scr = curwin->w_height;
3584 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003585 if (p_hi < 0)
3586 {
3587 errmsg = e_positive;
3588 p_hi = 0;
3589 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003590 else if (p_hi > 10000)
3591 {
3592 errmsg = e_invarg;
3593 p_hi = 10000;
3594 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003595 if (p_re < 0 || p_re > 2)
3596 {
3597 errmsg = e_invarg;
3598 p_re = 0;
3599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (p_report < 0)
3601 {
3602 errmsg = e_positive;
3603 p_report = 1;
3604 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003605 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 if (Rows != old_Rows) /* Rows changed, just adjust p_sj */
3608 p_sj = Rows / 2;
3609 else
3610 {
3611 errmsg = e_scroll;
3612 p_sj = 1;
3613 }
3614 }
3615 if (p_so < 0 && full_screen)
3616 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003617 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 p_so = 0;
3619 }
3620 if (p_siso < 0 && full_screen)
3621 {
3622 errmsg = e_positive;
3623 p_siso = 0;
3624 }
3625#ifdef FEAT_CMDWIN
3626 if (p_cwh < 1)
3627 {
3628 errmsg = e_positive;
3629 p_cwh = 1;
3630 }
3631#endif
3632 if (p_ut < 0)
3633 {
3634 errmsg = e_positive;
3635 p_ut = 2000;
3636 }
3637 if (p_ss < 0)
3638 {
3639 errmsg = e_positive;
3640 p_ss = 0;
3641 }
3642
3643 /* May set global value for local option. */
3644 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3645 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3646
3647 options[opt_idx].flags |= P_WAS_SET;
3648
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003649#if defined(FEAT_EVAL)
Bram Moolenaar3f3fb0b2018-09-21 11:59:32 +02003650 // Don't do this while starting up, failure or recursively.
3651 if (!starting && errmsg == NULL && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
Bram Moolenaar53744302015-07-17 17:38:22 +02003652 {
Bram Moolenaard7c96872019-06-15 17:12:48 +02003653 char_u buf_old[11], buf_old_global[11], buf_new[11], buf_type[7];
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003654 vim_snprintf((char *)buf_old, 10, "%ld", old_value);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003655 vim_snprintf((char *)buf_old_global, 10, "%ld", old_global_value);
Bram Moolenaarfb9bc482015-07-17 22:04:48 +02003656 vim_snprintf((char *)buf_new, 10, "%ld", value);
3657 vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global");
Bram Moolenaar53744302015-07-17 17:38:22 +02003658 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3659 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3660 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
Bram Moolenaard7c96872019-06-15 17:12:48 +02003661 if (opt_flags & OPT_LOCAL)
3662 {
3663 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3664 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3665 }
3666 if (opt_flags & OPT_GLOBAL)
3667 {
3668 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3669 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3670 }
3671 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3672 {
3673 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3674 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3675 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3676 }
3677 if (opt_flags & OPT_MODELINE)
3678 {
3679 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3680 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3681 }
3682 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3683 NULL, FALSE, NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003684 reset_v_option_vars();
3685 }
3686#endif
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 comp_col(); /* in case 'columns' or 'ls' changed */
Bram Moolenaar913077c2012-03-28 19:59:04 +02003689 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003690 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003691 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 check_redraw(options[opt_idx].flags);
3693
3694 return errmsg;
3695}
3696
3697/*
3698 * Called after an option changed: check if something needs to be redrawn.
3699 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003701check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703 /* Careful: P_RCLR and P_RALL are a combination of other P_ flags */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003704 int doclear = (flags & P_RCLR) == P_RCLR;
3705 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 if ((flags & P_RSTAT) || all) /* mark all status lines dirty */
3708 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3711 changed_window_setting();
3712 if (flags & P_RBUF)
3713 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003714 if (flags & P_RWINONLY)
3715 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003716 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 redraw_all_later(CLEAR);
3718 else if (all)
3719 redraw_all_later(NOT_VALID);
3720}
3721
3722/*
3723 * Find index for option 'arg'.
3724 * Return -1 if not found.
3725 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003726 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003727findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
3729 int opt_idx;
3730 char *s, *p;
3731 static short quick_tab[27] = {0, 0}; /* quick access table */
3732 int is_term_opt;
3733
3734 /*
3735 * For first call: Initialize the quick-access table.
3736 * It contains the index for the first option that starts with a certain
3737 * letter. There are 26 letters, plus the first "t_" option.
3738 */
3739 if (quick_tab[1] == 0)
3740 {
3741 p = options[0].fullname;
3742 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3743 {
3744 if (s[0] != p[0])
3745 {
3746 if (s[0] == 't' && s[1] == '_')
3747 quick_tab[26] = opt_idx;
3748 else
3749 quick_tab[CharOrdLow(s[0])] = opt_idx;
3750 }
3751 p = s;
3752 }
3753 }
3754
3755 /*
3756 * Check for name starting with an illegal character.
3757 */
3758#ifdef EBCDIC
3759 if (!islower(arg[0]))
3760#else
3761 if (arg[0] < 'a' || arg[0] > 'z')
3762#endif
3763 return -1;
3764
3765 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3766 if (is_term_opt)
3767 opt_idx = quick_tab[26];
3768 else
3769 opt_idx = quick_tab[CharOrdLow(arg[0])];
3770 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3771 {
3772 if (STRCMP(arg, s) == 0) /* match full name */
3773 break;
3774 }
3775 if (s == NULL && !is_term_opt)
3776 {
3777 opt_idx = quick_tab[CharOrdLow(arg[0])];
3778 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3779 {
3780 s = options[opt_idx].shortname;
3781 if (s != NULL && STRCMP(arg, s) == 0) /* match short name */
3782 break;
3783 s = NULL;
3784 }
3785 }
3786 if (s == NULL)
3787 opt_idx = -1;
3788 return opt_idx;
3789}
3790
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003791#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792/*
3793 * Get the value for an option.
3794 *
3795 * Returns:
3796 * Number or Toggle option: 1, *numval gets value.
3797 * String option: 0, *stringval gets allocated string.
3798 * Hidden Number or Toggle option: -1.
3799 * hidden String option: -2.
3800 * unknown option: -3.
3801 */
3802 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003803get_option_value(
3804 char_u *name,
3805 long *numval,
3806 char_u **stringval, /* NULL when only checking existence */
3807 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
3809 int opt_idx;
3810 char_u *varp;
3811
3812 opt_idx = findoption(name);
3813 if (opt_idx < 0) /* unknown option */
Bram Moolenaare353c402017-02-04 19:49:16 +01003814 {
3815 int key;
3816
3817 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02003818 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003819 {
3820 char_u key_name[2];
3821 char_u *p;
3822
3823 if (key < 0)
3824 {
3825 key_name[0] = KEY2TERMCAP0(key);
3826 key_name[1] = KEY2TERMCAP1(key);
3827 }
3828 else
3829 {
3830 key_name[0] = KS_KEY;
3831 key_name[1] = (key & 0xff);
3832 }
3833 p = find_termcode(key_name);
3834 if (p != NULL)
3835 {
3836 if (stringval != NULL)
3837 *stringval = vim_strsave(p);
3838 return 0;
3839 }
3840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 return -3;
Bram Moolenaare353c402017-02-04 19:49:16 +01003842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3845
3846 if (options[opt_idx].flags & P_STRING)
3847 {
3848 if (varp == NULL) /* hidden option */
3849 return -2;
3850 if (stringval != NULL)
3851 {
3852#ifdef FEAT_CRYPT
3853 /* never return the value of the crypt key */
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003854 if ((char_u **)varp == &curbuf->b_p_key
3855 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 *stringval = vim_strsave((char_u *)"*****");
3857 else
3858#endif
3859 *stringval = vim_strsave(*(char_u **)(varp));
3860 }
3861 return 0;
3862 }
3863
3864 if (varp == NULL) /* hidden option */
3865 return -1;
3866 if (options[opt_idx].flags & P_NUM)
3867 *numval = *(long *)varp;
3868 else
3869 {
3870 /* Special case: 'modified' is b_changed, but we also want to consider
3871 * it set when 'ff' or 'fenc' changed. */
3872 if ((int *)varp == &curbuf->b_changed)
3873 *numval = curbufIsChanged();
3874 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003875 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
3877 return 1;
3878}
3879#endif
3880
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003881#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003882/*
3883 * Returns the option attributes and its value. Unlike the above function it
3884 * will return either global value or local value of the option depending on
3885 * what was requested, but it will never return global value if it was
3886 * requested to return local one and vice versa. Neither it will return
3887 * buffer-local value if it was requested to return window-local one.
3888 *
3889 * Pretends that option is absent if it is not present in the requested scope
3890 * (i.e. has no global, window-local or buffer-local value depending on
3891 * opt_type). Uses
3892 *
3893 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003894 * 0 hidden or unknown option, also option that does not have requested
3895 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003896 * see SOPT_* in vim.h for other flags
3897 *
3898 * Possible opt_type values: see SREQ_* in vim.h
3899 */
3900 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003901get_option_value_strict(
3902 char_u *name,
3903 long *numval,
3904 char_u **stringval, /* NULL when only obtaining attributes */
3905 int opt_type,
3906 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003907{
3908 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003909 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003910 struct vimoption *p;
3911 int r = 0;
3912
3913 opt_idx = findoption(name);
3914 if (opt_idx < 0)
3915 return 0;
3916
3917 p = &(options[opt_idx]);
3918
3919 /* Hidden option */
3920 if (p->var == NULL)
3921 return 0;
3922
3923 if (p->flags & P_BOOL)
3924 r |= SOPT_BOOL;
3925 else if (p->flags & P_NUM)
3926 r |= SOPT_NUM;
3927 else if (p->flags & P_STRING)
3928 r |= SOPT_STRING;
3929
3930 if (p->indir == PV_NONE)
3931 {
3932 if (opt_type == SREQ_GLOBAL)
3933 r |= SOPT_GLOBAL;
3934 else
3935 return 0; /* Did not request global-only option */
3936 }
3937 else
3938 {
3939 if (p->indir & PV_BOTH)
3940 r |= SOPT_GLOBAL;
3941 else if (opt_type == SREQ_GLOBAL)
3942 return 0; /* Requested global option */
3943
3944 if (p->indir & PV_WIN)
3945 {
3946 if (opt_type == SREQ_BUF)
3947 return 0; /* Did not request window-local option */
3948 else
3949 r |= SOPT_WIN;
3950 }
3951 else if (p->indir & PV_BUF)
3952 {
3953 if (opt_type == SREQ_WIN)
3954 return 0; /* Did not request buffer-local option */
3955 else
3956 r |= SOPT_BUF;
3957 }
3958 }
3959
3960 if (stringval == NULL)
3961 return r;
3962
3963 if (opt_type == SREQ_GLOBAL)
3964 varp = p->var;
3965 else
3966 {
3967 if (opt_type == SREQ_BUF)
3968 {
3969 /* Special case: 'modified' is b_changed, but we also want to
3970 * consider it set when 'ff' or 'fenc' changed. */
3971 if (p->indir == PV_MOD)
3972 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02003973 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003974 varp = NULL;
3975 }
3976#ifdef FEAT_CRYPT
3977 else if (p->indir == PV_KEY)
3978 {
3979 /* never return the value of the crypt key */
3980 *stringval = NULL;
3981 varp = NULL;
3982 }
3983#endif
3984 else
3985 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02003986 buf_T *save_curbuf = curbuf;
3987
3988 // only getting a pointer, no need to use aucmd_prepbuf()
3989 curbuf = (buf_T *)from;
3990 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003991 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02003992 curbuf = save_curbuf;
3993 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003994 }
3995 }
3996 else if (opt_type == SREQ_WIN)
3997 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02003998 win_T *save_curwin = curwin;
3999
4000 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004001 curbuf = curwin->w_buffer;
4002 varp = get_varp(p);
4003 curwin = save_curwin;
4004 curbuf = curwin->w_buffer;
4005 }
4006 if (varp == p->var)
4007 return (r | SOPT_UNSET);
4008 }
4009
4010 if (varp != NULL)
4011 {
4012 if (p->flags & P_STRING)
4013 *stringval = vim_strsave(*(char_u **)(varp));
4014 else if (p->flags & P_NUM)
4015 *numval = *(long *) varp;
4016 else
4017 *numval = *(int *)varp;
4018 }
4019
4020 return r;
4021}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004022
4023/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004024 * Iterate over options. First argument is a pointer to a pointer to a
4025 * structure inside options[] array, second is option type like in the above
4026 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004027 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004028 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004029 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004030 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004031 * first argument to NULL.
4032 *
4033 * Returns full option name for current option on each call.
4034 */
4035 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004036option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004037{
4038 struct vimoption *ret = NULL;
4039 do
4040 {
4041 if (*option == NULL)
4042 *option = (void *) options;
4043 else if (((struct vimoption *) (*option))->fullname == NULL)
4044 {
4045 *option = NULL;
4046 return NULL;
4047 }
4048 else
4049 *option = (void *) (((struct vimoption *) (*option)) + 1);
4050
4051 ret = ((struct vimoption *) (*option));
4052
4053 /* Hidden option */
4054 if (ret->var == NULL)
4055 {
4056 ret = NULL;
4057 continue;
4058 }
4059
4060 switch (opt_type)
4061 {
4062 case SREQ_GLOBAL:
4063 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4064 ret = NULL;
4065 break;
4066 case SREQ_BUF:
4067 if (!(ret->indir & PV_BUF))
4068 ret = NULL;
4069 break;
4070 case SREQ_WIN:
4071 if (!(ret->indir & PV_WIN))
4072 ret = NULL;
4073 break;
4074 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004075 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004076 return NULL;
4077 }
4078 }
4079 while (ret == NULL);
4080
4081 return (char_u *)ret->fullname;
4082}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004083#endif
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004086 * Return the flags for the option at 'opt_idx'.
4087 */
4088 long_u
4089get_option_flags(int opt_idx)
4090{
4091 return options[opt_idx].flags;
4092}
4093
4094/*
4095 * Set a flag for the option at 'opt_idx'.
4096 */
4097 void
4098set_option_flag(int opt_idx, long_u flag)
4099{
4100 options[opt_idx].flags |= flag;
4101}
4102
4103/*
4104 * Clear a flag for the option at 'opt_idx'.
4105 */
4106 void
4107clear_option_flag(int opt_idx, long_u flag)
4108{
4109 options[opt_idx].flags &= ~flag;
4110}
4111
4112/*
4113 * Returns TRUE if the option at 'opt_idx' is a global option
4114 */
4115 int
4116is_global_option(int opt_idx)
4117{
4118 return options[opt_idx].indir == PV_NONE;
4119}
4120
4121/*
4122 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4123 * local value.
4124 */
4125 int
4126is_global_local_option(int opt_idx)
4127{
4128 return options[opt_idx].indir & PV_BOTH;
4129}
4130
4131/*
4132 * Returns TRUE if the option at 'opt_idx' is a window-local option
4133 */
4134 int
4135is_window_local_option(int opt_idx)
4136{
4137 return options[opt_idx].var == VAR_WIN;
4138}
4139
4140/*
4141 * Returns TRUE if the option at 'opt_idx' is a hidden option
4142 */
4143 int
4144is_hidden_option(int opt_idx)
4145{
4146 return options[opt_idx].var == NULL;
4147}
4148
4149#if defined(FEAT_CRYPT) || defined(PROTO)
4150/*
4151 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4152 */
4153 int
4154is_crypt_key_option(int opt_idx)
4155{
4156 return options[opt_idx].indir == PV_KEY;
4157}
4158#endif
4159
4160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 * Set the value of option "name".
4162 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004163 *
4164 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004166 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004167set_option_value(
4168 char_u *name,
4169 long number,
4170 char_u *string,
4171 int opt_flags) /* OPT_LOCAL or 0 (both) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 int opt_idx;
4174 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004175 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004178 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004179 {
4180 int key;
4181
4182 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004183 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004184 {
4185 char_u key_name[2];
4186
4187 if (key < 0)
4188 {
4189 key_name[0] = KEY2TERMCAP0(key);
4190 key_name[1] = KEY2TERMCAP1(key);
4191 }
4192 else
4193 {
4194 key_name[0] = KS_KEY;
4195 key_name[1] = (key & 0xff);
4196 }
4197 add_termcode(key_name, string, FALSE);
4198 if (full_screen)
4199 ttest(FALSE);
4200 redraw_all_later(CLEAR);
4201 return NULL;
4202 }
4203
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004204 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 else
4207 {
4208 flags = options[opt_idx].flags;
4209#ifdef HAVE_SANDBOX
4210 /* Disallow changing some options in the sandbox */
4211 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004213 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004214 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004217 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004218 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 else
4220 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004221 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (varp != NULL) /* hidden option is not changed */
4223 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004224 if (number == 0 && string != NULL)
4225 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004226 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004227
4228 /* Either we are given a string or we are setting option
4229 * to zero. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004230 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004231 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004232 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004233 {
4234 /* There's another character after zeros or the string
4235 * is empty. In both cases, we are trying to set a
4236 * num option using a string. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004237 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004238 name, string);
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004239 return NULL; /* do nothing as we hit an error */
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004240
4241 }
4242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004244 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004245 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004247 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004248 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 }
4251 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004252 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253}
4254
4255/*
4256 * Get the terminal code for a terminal option.
4257 * Returns NULL when not found.
4258 */
4259 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004260get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 int opt_idx;
4263 char_u *varp;
4264
4265 if (tname[0] != 't' || tname[1] != '_' ||
4266 tname[2] == NUL || tname[3] == NUL)
4267 return NULL;
4268 if ((opt_idx = findoption(tname)) >= 0)
4269 {
4270 varp = get_varp(&(options[opt_idx]));
4271 if (varp != NULL)
4272 varp = *(char_u **)(varp);
4273 return varp;
4274 }
4275 return find_termcode(tname + 2);
4276}
4277
4278 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004279get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280{
4281 int i;
4282
4283 i = findoption((char_u *)"hl");
4284 if (i >= 0)
4285 return options[i].def_val[VI_DEFAULT];
4286 return (char_u *)NULL;
4287}
4288
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004289 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004290get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004291{
4292 int i;
4293
4294 i = findoption((char_u *)"enc");
4295 if (i >= 0)
4296 return options[i].def_val[VI_DEFAULT];
4297 return (char_u *)NULL;
4298}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004299
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300/*
4301 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004302 * When "has_lt" is true there is a '<' before "*arg_arg".
4303 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 */
4305 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004306find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004308 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004310 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 /*
4313 * Don't use get_special_key_code() for t_xx, we don't want it to call
4314 * add_termcap_entry().
4315 */
4316 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4317 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004318 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 --arg; /* put arg at the '<' */
4321 modifiers = 0;
Bram Moolenaar459fd782019-10-13 16:43:39 +02004322 key = find_special_key(&arg, &modifiers, TRUE, TRUE, FALSE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (modifiers) /* can't handle modifiers here */
4324 key = 0;
4325 }
4326 return key;
4327}
4328
4329/*
4330 * if 'all' == 0: show changed options
4331 * if 'all' == 1: show all normal options
4332 * if 'all' == 2: show all terminal options
4333 */
4334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004335showoptions(
4336 int all,
4337 int opt_flags) /* OPT_LOCAL and/or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338{
4339 struct vimoption *p;
4340 int col;
4341 int isterm;
4342 char_u *varp;
4343 struct vimoption **items;
4344 int item_count;
4345 int run;
4346 int row, rows;
4347 int cols;
4348 int i;
4349 int len;
4350
4351#define INC 20
4352#define GAP 3
4353
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004354 items = ALLOC_MULT(struct vimoption *, PARAM_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 if (items == NULL)
4356 return;
4357
4358 /* Highlight title */
4359 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004360 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004362 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004364 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004366 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 /*
4369 * do the loop two times:
4370 * 1. display the short items
4371 * 2. display the long items (only strings and numbers)
4372 */
4373 for (run = 1; run <= 2 && !got_int; ++run)
4374 {
4375 /*
4376 * collect the items in items[]
4377 */
4378 item_count = 0;
4379 for (p = &options[0]; p->fullname != NULL; p++)
4380 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004381 // apply :filter /pat/
4382 if (message_filtered((char_u *) p->fullname))
4383 continue;
4384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 varp = NULL;
4386 isterm = istermoption(p);
4387 if (opt_flags != 0)
4388 {
4389 if (p->indir != PV_NONE && !isterm)
4390 varp = get_varp_scope(p, opt_flags);
4391 }
4392 else
4393 varp = get_varp(p);
4394 if (varp != NULL
4395 && ((all == 2 && isterm)
4396 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004397 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
4399 if (p->flags & P_BOOL)
4400 len = 1; /* a toggle option fits always */
4401 else
4402 {
4403 option_value2string(p, opt_flags);
4404 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4405 }
4406 if ((len <= INC - GAP && run == 1) ||
4407 (len > INC - GAP && run == 2))
4408 items[item_count++] = p;
4409 }
4410 }
4411
4412 /*
4413 * display the items
4414 */
4415 if (run == 1)
4416 {
4417 cols = (Columns + GAP - 3) / INC;
4418 if (cols == 0)
4419 cols = 1;
4420 rows = (item_count + cols - 1) / cols;
4421 }
4422 else /* run == 2 */
4423 rows = item_count;
4424 for (row = 0; row < rows && !got_int; ++row)
4425 {
4426 msg_putchar('\n'); /* go to next line */
4427 if (got_int) /* 'q' typed in more */
4428 break;
4429 col = 0;
4430 for (i = row; i < item_count; i += rows)
4431 {
4432 msg_col = col; /* make columns */
4433 showoneopt(items[i], opt_flags);
4434 col += INC;
4435 }
4436 out_flush();
4437 ui_breakcheck();
4438 }
4439 }
4440 vim_free(items);
4441}
4442
4443/*
4444 * Return TRUE if option "p" has its default value.
4445 */
4446 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004447optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
4449 int dvi;
4450
4451 if (varp == NULL)
4452 return TRUE; /* hidden option is always at default */
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004453 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004455 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 if (p->flags & P_BOOL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004457 /* the cast to long is required for Manx C, long_i is
4458 * needed for MSVC */
4459 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 /* P_STRING */
4461 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4462}
4463
4464/*
4465 * showoneopt: show the value of one option
4466 * must not be called with a hidden option!
4467 */
4468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004469showoneopt(
4470 struct vimoption *p,
4471 int opt_flags) /* OPT_LOCAL or OPT_GLOBAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004473 char_u *varp;
4474 int save_silent = silent_mode;
4475
4476 silent_mode = FALSE;
4477 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 varp = get_varp_scope(p, opt_flags);
4480
4481 /* for 'modified' we also need to check if 'ff' or 'fenc' changed. */
4482 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4483 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004484 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004486 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004488 msg_puts(" ");
4489 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if (!(p->flags & P_BOOL))
4491 {
4492 msg_putchar('=');
4493 /* put value string in NameBuff */
4494 option_value2string(p, opt_flags);
4495 msg_outtrans(NameBuff);
4496 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004497
4498 silent_mode = save_silent;
4499 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500}
4501
4502/*
4503 * Write modified options as ":set" commands to a file.
4504 *
4505 * There are three values for "opt_flags":
4506 * OPT_GLOBAL: Write global option values and fresh values of
4507 * buffer-local options (used for start of a session
4508 * file).
4509 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4510 * curwin (used for a vimrc file).
4511 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4512 * and local values for window-local options of
4513 * curwin. Local values are also written when at the
4514 * default value, because a modeline or autocommand
4515 * may have set them when doing ":edit file" and the
4516 * user has set them back at the default or fresh
4517 * value.
4518 * When "local_only" is TRUE, don't write fresh
4519 * values, only local values (for ":mkview").
4520 * (fresh value = value used for a new buffer or window for a local option).
4521 *
4522 * Return FAIL on error, OK otherwise.
4523 */
4524 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004525makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526{
4527 struct vimoption *p;
4528 char_u *varp; /* currently used value */
4529 char_u *varp_fresh; /* local value */
4530 char_u *varp_local = NULL; /* fresh value */
4531 char *cmd;
4532 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004533 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 /*
4536 * The options that don't have a default (terminal name, columns, lines)
4537 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004538 * Do the loop over "options[]" twice: once for options with the
4539 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004541 for (pri = 1; pri >= 0; --pri)
4542 {
4543 for (p = &options[0]; !istermoption(p); p++)
4544 if (!(p->flags & P_NO_MKRC)
4545 && !istermoption(p)
4546 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 {
4548 /* skip global option when only doing locals */
4549 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4550 continue;
4551
4552 /* Do not store options like 'bufhidden' and 'syntax' in a vimrc
4553 * file, they are always buffer-specific. */
4554 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4555 continue;
4556
4557 /* Global values are only written when not at the default value. */
4558 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004559 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 continue;
4561
4562 round = 2;
4563 if (p->indir != PV_NONE)
4564 {
4565 if (p->var == VAR_WIN)
4566 {
4567 /* skip window-local option when only doing globals */
4568 if (!(opt_flags & OPT_LOCAL))
4569 continue;
4570 /* When fresh value of window-local option is not at the
4571 * default, need to write it too. */
4572 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4573 {
4574 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004575 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 round = 1;
4578 varp_local = varp;
4579 varp = varp_fresh;
4580 }
4581 }
4582 }
4583 }
4584
4585 /* Round 1: fresh value for window-local options.
4586 * Round 2: other values */
4587 for ( ; round <= 2; varp = varp_local, ++round)
4588 {
4589 if (round == 1 || (opt_flags & OPT_GLOBAL))
4590 cmd = "set";
4591 else
4592 cmd = "setlocal";
4593
4594 if (p->flags & P_BOOL)
4595 {
4596 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4597 return FAIL;
4598 }
4599 else if (p->flags & P_NUM)
4600 {
4601 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4602 return FAIL;
4603 }
4604 else /* P_STRING */
4605 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004606 int do_endif = FALSE;
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /* Don't set 'syntax' and 'filetype' again if the value is
4609 * already right, avoids reloading the syntax file. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004610 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004611#if defined(FEAT_SYN_HL)
4612 p->indir == PV_SYN ||
4613#endif
4614 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4617 *(char_u **)(varp)) < 0
4618 || put_eol(fd) < 0)
4619 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004620 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004623 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004625 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
4627 if (put_line(fd, "endif") == FAIL)
4628 return FAIL;
4629 }
4630 }
4631 }
4632 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return OK;
4635}
4636
4637#if defined(FEAT_FOLDING) || defined(PROTO)
4638/*
4639 * Generate set commands for the local fold options only. Used when
4640 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4641 */
4642 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004643makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004645 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004647 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 == FAIL
4649# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004650 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004652 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 == FAIL
4654 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4655 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4656 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4657 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4658 )
4659 return FAIL;
4660
4661 return OK;
4662}
4663#endif
4664
4665 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004666put_setstring(
4667 FILE *fd,
4668 char *cmd,
4669 char *name,
4670 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004671 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004674 char_u *buf = NULL;
4675 char_u *part = NULL;
4676 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4679 return FAIL;
4680 if (*valuep != NULL)
4681 {
4682 /* Output 'pastetoggle' as key names. For other
4683 * options some characters have to be escaped with
4684 * CTRL-V or backslash */
4685 if (valuep == &p_pt)
4686 {
4687 s = *valuep;
4688 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004689 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 return FAIL;
4691 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004692 // expand the option value, replace $HOME by ~
4693 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004695 int size = (int)STRLEN(*valuep) + 1;
4696
4697 // replace home directory in the whole option value into "buf"
4698 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004699 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004700 goto fail;
4701 home_replace(NULL, *valuep, buf, size, FALSE);
4702
4703 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004704 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004705 // can be expanded when read back.
4706 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4707 && vim_strchr(*valuep, ',') != NULL)
4708 {
4709 part = alloc(size);
4710 if (part == NULL)
4711 goto fail;
4712
4713 // write line break to clear the option, e.g. ':set rtp='
4714 if (put_eol(fd) == FAIL)
4715 goto fail;
4716
4717 p = buf;
4718 while (*p != NUL)
4719 {
4720 // for each comma separated option part, append value to
4721 // the option, :set rtp+=value
4722 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4723 goto fail;
4724 (void)copy_option_part(&p, part, size, ",");
4725 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4726 goto fail;
4727 }
4728 vim_free(buf);
4729 vim_free(part);
4730 return OK;
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004733 {
4734 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004736 }
4737 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
4739 else if (put_escstr(fd, *valuep, 2) == FAIL)
4740 return FAIL;
4741 }
4742 if (put_eol(fd) < 0)
4743 return FAIL;
4744 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004745fail:
4746 vim_free(buf);
4747 vim_free(part);
4748 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749}
4750
4751 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004752put_setnum(
4753 FILE *fd,
4754 char *cmd,
4755 char *name,
4756 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 long wc;
4759
4760 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4761 return FAIL;
4762 if (wc_use_keyname((char_u *)valuep, &wc))
4763 {
4764 /* print 'wildchar' and 'wildcharm' as a key name */
4765 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4766 return FAIL;
4767 }
4768 else if (fprintf(fd, "%ld", *valuep) < 0)
4769 return FAIL;
4770 if (put_eol(fd) < 0)
4771 return FAIL;
4772 return OK;
4773}
4774
4775 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004776put_setbool(
4777 FILE *fd,
4778 char *cmd,
4779 char *name,
4780 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
Bram Moolenaar893de922007-10-02 18:40:57 +00004782 if (value < 0) /* global/local option using global value */
4783 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4785 || put_eol(fd) < 0)
4786 return FAIL;
4787 return OK;
4788}
4789
4790/*
4791 * Clear all the terminal options.
4792 * If the option has been allocated, free the memory.
4793 * Terminal options are never hidden or indirect.
4794 */
4795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004796clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 /*
4799 * Reset a few things before clearing the old options. This may cause
4800 * outputting a few things that the terminal doesn't understand, but the
4801 * screen will be cleared later, so this is OK.
4802 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004803 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02004805 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806#endif
4807#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
4808 /* When starting the GUI close the display opened for the clipboard.
4809 * After restoring the title, because that will need the display. */
4810 if (gui.starting)
4811 clear_xterm_clip();
4812#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004813 stoptermcap(); /* stop termcap mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004815 free_termoptions();
4816}
4817
4818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004819free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004820{
4821 struct vimoption *p;
4822
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004823 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (istermoption(p))
4825 {
4826 if (p->flags & P_ALLOCED)
4827 free_string_option(*(char_u **)(p->var));
4828 if (p->flags & P_DEF_ALLOCED)
4829 free_string_option(p->def_val[VI_DEFAULT]);
4830 *(char_u **)(p->var) = empty_option;
4831 p->def_val[VI_DEFAULT] = empty_option;
4832 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004833#ifdef FEAT_EVAL
4834 // remember where the option was cleared
4835 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 clear_termcodes();
4839}
4840
4841/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004842 * Free the string for one term option, if it was allocated.
4843 * Set the string to empty_option and clear allocated flag.
4844 * "var" points to the option value.
4845 */
4846 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004847free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004848{
4849 struct vimoption *p;
4850
4851 for (p = &options[0]; p->fullname != NULL; p++)
4852 if (p->var == var)
4853 {
4854 if (p->flags & P_ALLOCED)
4855 free_string_option(*(char_u **)(p->var));
4856 *(char_u **)(p->var) = empty_option;
4857 p->flags &= ~P_ALLOCED;
4858 break;
4859 }
4860}
4861
4862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 * Set the terminal option defaults to the current value.
4864 * Used after setting the terminal name.
4865 */
4866 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
4869 struct vimoption *p;
4870
4871 for (p = &options[0]; p->fullname != NULL; p++)
4872 {
4873 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4874 {
4875 if (p->flags & P_DEF_ALLOCED)
4876 {
4877 free_string_option(p->def_val[VI_DEFAULT]);
4878 p->flags &= ~P_DEF_ALLOCED;
4879 }
4880 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4881 if (p->flags & P_ALLOCED)
4882 {
4883 p->flags |= P_DEF_ALLOCED;
4884 p->flags &= ~P_ALLOCED; /* don't free the value now */
4885 }
4886 }
4887 }
4888}
4889
4890/*
4891 * return TRUE if 'p' starts with 't_'
4892 */
4893 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004894istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4897}
4898
Bram Moolenaardac13472019-09-16 21:06:21 +02004899/*
4900 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4901 */
4902 int
4903istermoption_idx(int opt_idx)
4904{
4905 return istermoption(&options[opt_idx]);
4906}
4907
Bram Moolenaar113e1072019-01-20 15:30:40 +01004908#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004910 * Unset local option value, similar to ":set opt<".
4911 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004913unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004914{
4915 struct vimoption *p;
4916 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004917 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004918
4919 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004920 if (opt_idx < 0)
4921 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004922 p = &(options[opt_idx]);
4923
4924 switch ((int)p->indir)
4925 {
4926 /* global option with local value: use local value if it's been set */
4927 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004928 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004929 break;
4930 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004931 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004932 break;
4933 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004934 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004935 break;
4936 case PV_AR:
4937 buf->b_p_ar = -1;
4938 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004939 case PV_BKC:
4940 clear_string_option(&buf->b_p_bkc);
4941 buf->b_bkc_flags = 0;
4942 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004943 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004944 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004945 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01004946 case PV_TC:
4947 clear_string_option(&buf->b_p_tc);
4948 buf->b_tc_flags = 0;
4949 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01004950 case PV_SISO:
4951 curwin->w_p_siso = -1;
4952 break;
4953 case PV_SO:
4954 curwin->w_p_so = -1;
4955 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004956#ifdef FEAT_FIND_ID
4957 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004958 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004959 break;
4960 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004961 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004962 break;
4963#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004964 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004965 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004966 break;
4967 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004968 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004969 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01004970 case PV_FP:
4971 clear_string_option(&buf->b_p_fp);
4972 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004973#ifdef FEAT_QUICKFIX
4974 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004975 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004976 break;
4977 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004978 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004979 break;
4980 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004981 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004982 break;
4983#endif
4984#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
4985 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004986 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004987 break;
4988#endif
4989#if defined(FEAT_CRYPT)
4990 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004991 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004992 break;
4993#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01004994#ifdef FEAT_LINEBREAK
4995 case PV_SBR:
4996 clear_string_option(&((win_T *)from)->w_p_sbr);
4997 break;
4998#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004999#ifdef FEAT_STL_OPT
5000 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005001 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005002 break;
5003#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005004 case PV_UL:
5005 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5006 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005007#ifdef FEAT_LISP
5008 case PV_LW:
5009 clear_string_option(&buf->b_p_lw);
5010 break;
5011#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005012 case PV_MENC:
5013 clear_string_option(&buf->b_p_menc);
5014 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005015 }
5016}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005017#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005018
5019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 * Get pointer to option variable, depending on local or global scope.
5021 */
5022 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005023get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5026 {
5027 if (p->var == VAR_WIN)
5028 return (char_u *)GLOBAL_WO(get_varp(p));
5029 return p->var;
5030 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005031 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
5033 switch ((int)p->indir)
5034 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005035 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005037 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5038 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5039 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005041 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5042 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5043 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005044 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005045 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005046 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005047 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5048 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005050 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5051 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005053 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5054 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005055#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5056 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5057#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005058#if defined(FEAT_CRYPT)
5059 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5060#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005061#ifdef FEAT_LINEBREAK
5062 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5063#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005064#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005065 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005066#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005067 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005068#ifdef FEAT_LISP
5069 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5070#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005071 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005072 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 return NULL; /* "cannot happen" */
5075 }
5076 return get_varp(p);
5077}
5078
5079/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005080 * Get pointer to option variable at 'opt_idx', depending on local or global
5081 * scope.
5082 */
5083 char_u *
5084get_option_varp_scope(int opt_idx, int opt_flags)
5085{
5086 return get_varp_scope(&(options[opt_idx]), opt_flags);
5087}
5088
5089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 * Get pointer to option variable.
5091 */
5092 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005093get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
5095 /* hidden option, always return NULL */
5096 if (p->var == NULL)
5097 return NULL;
5098
5099 switch ((int)p->indir)
5100 {
5101 case PV_NONE: return p->var;
5102
5103 /* global option with local value: use local value if it's been set */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005104 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005106 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005108 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005110 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005112 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005114 case PV_TC: return *curbuf->b_p_tc != NUL
5115 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005116 case PV_BKC: return *curbuf->b_p_bkc != NUL
5117 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005118 case PV_SISO: return curwin->w_p_siso >= 0
5119 ? (char_u *)&(curwin->w_p_siso) : p->var;
5120 case PV_SO: return curwin->w_p_so >= 0
5121 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005123 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005125 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5127#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005128 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005130 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005132 case PV_FP: return *curbuf->b_p_fp != NUL
5133 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005135 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005137 case PV_GP: return *curbuf->b_p_gp != NUL
5138 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5139 case PV_MP: return *curbuf->b_p_mp != NUL
5140 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005142#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5143 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5144 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5145#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005146#if defined(FEAT_CRYPT)
5147 case PV_CM: return *curbuf->b_p_cm != NUL
5148 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5149#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005150#ifdef FEAT_LINEBREAK
5151 case PV_SBR: return *curwin->w_p_sbr != NUL
5152 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5153#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005154#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005155 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005156 ? (char_u *)&(curwin->w_p_stl) : p->var;
5157#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005158 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5159 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005160#ifdef FEAT_LISP
5161 case PV_LW: return *curbuf->b_p_lw != NUL
5162 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5163#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005164 case PV_MENC: return *curbuf->b_p_menc != NUL
5165 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166#ifdef FEAT_ARABIC
5167 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5168#endif
5169 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005170#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005171 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5172#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005173#ifdef FEAT_SYN_HL
5174 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5175 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005176 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005177 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#ifdef FEAT_DIFF
5180 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5181#endif
5182#ifdef FEAT_FOLDING
5183 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5184 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5185 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5186 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5187 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5188 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5189 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5190# ifdef FEAT_EVAL
5191 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5192 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5193# endif
5194 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5195#endif
5196 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005197 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005198#ifdef FEAT_LINEBREAK
5199 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005202 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005203#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5205#endif
5206#ifdef FEAT_RIGHTLEFT
5207 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5208 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5209#endif
5210 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5211 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5212#ifdef FEAT_LINEBREAK
5213 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005214 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5215 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005217 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005219 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005220#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005221 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5222 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5223#endif
5224#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005225 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5226 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5227 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5231 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5234 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5236 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5237#ifdef FEAT_CINDENT
5238 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5239 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5240 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5241#endif
5242#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5243 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#ifdef FEAT_FOLDING
5247 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005250#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005251 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005253#ifdef FEAT_COMPL_FUNC
5254 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005255 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005256#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005257#ifdef FEAT_EVAL
5258 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005261 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005267 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5269 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5270 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5271 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5272#ifdef FEAT_FIND_ID
5273# ifdef FEAT_EVAL
5274 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5275# endif
5276#endif
5277#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5278 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5279 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5280#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005281#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005282 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284#ifdef FEAT_CRYPT
5285 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5286#endif
5287#ifdef FEAT_LISP
5288 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5289#endif
5290 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5291 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5292 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5293 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5294 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005296#ifdef FEAT_TEXTOBJ
5297 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5300#ifdef FEAT_SMARTINDENT
5301 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5305#ifdef FEAT_SEARCHPATH
5306 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5307#endif
5308 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5309#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005310 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005311 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5312#endif
5313#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005314 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5315 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5316 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
5318 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5319 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5320 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5321 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005322#ifdef FEAT_PERSISTENT_UNDO
5323 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5326#ifdef FEAT_KEYMAP
5327 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5328#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005329#ifdef FEAT_SIGNS
5330 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5331#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005332#ifdef FEAT_VARTABS
5333 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5334 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5335#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005336 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 /* always return a valid pointer to avoid a crash! */
5339 return (char_u *)&(curbuf->b_p_wm);
5340}
5341
5342/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005343 * Return a pointer to the variable for option at 'opt_idx'
5344 */
5345 char_u *
5346get_option_var(int opt_idx)
5347{
5348 return options[opt_idx].var;
5349}
5350
5351/*
5352 * Return the full name of the option at 'opt_idx'
5353 */
5354 char_u *
5355get_option_fullname(int opt_idx)
5356{
5357 return (char_u *)options[opt_idx].fullname;
5358}
5359
5360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 * Get the value of 'equalprg', either the buffer-local one or the global one.
5362 */
5363 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005364get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 if (*curbuf->b_p_ep == NUL)
5367 return p_ep;
5368 return curbuf->b_p_ep;
5369}
5370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371/*
5372 * Copy options from one window to another.
5373 * Used when splitting a window.
5374 */
5375 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005376win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5379 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005380 after_copy_winopt(wp_to);
5381}
5382
5383/*
5384 * After copying window options: update variables depending on options.
5385 */
5386 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005387after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005388{
5389#ifdef FEAT_LINEBREAK
5390 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005391#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005392#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005393 fill_culopt_flags(NULL, wp);
5394 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398/*
5399 * Copy the options from one winopt_T to another.
5400 * Doesn't free the old option values in "to", use clear_winopt() for that.
5401 * The 'scroll' option is not copied, because it depends on the window height.
5402 * The 'previewwindow' option is reset, there can be only one preview window.
5403 */
5404 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005405copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407#ifdef FEAT_ARABIC
5408 to->wo_arab = from->wo_arab;
5409#endif
5410 to->wo_list = from->wo_list;
5411 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005412 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005413#ifdef FEAT_LINEBREAK
5414 to->wo_nuw = from->wo_nuw;
5415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#ifdef FEAT_RIGHTLEFT
5417 to->wo_rl = from->wo_rl;
5418 to->wo_rlc = vim_strsave(from->wo_rlc);
5419#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005420#ifdef FEAT_LINEBREAK
5421 to->wo_sbr = vim_strsave(from->wo_sbr);
5422#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005423#ifdef FEAT_STL_OPT
5424 to->wo_stl = vim_strsave(from->wo_stl);
5425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005427#ifdef FEAT_DIFF
5428 to->wo_wrap_save = from->wo_wrap_save;
5429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#ifdef FEAT_LINEBREAK
5431 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005432 to->wo_bri = from->wo_bri;
5433 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005435 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005437 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005438 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005439 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005440#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005441 to->wo_spell = from->wo_spell;
5442#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005443#ifdef FEAT_SYN_HL
5444 to->wo_cuc = from->wo_cuc;
5445 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005446 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005447 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449#ifdef FEAT_DIFF
5450 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005451 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005453#ifdef FEAT_CONCEAL
5454 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005455 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005456#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005457#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005458 to->wo_twk = vim_strsave(from->wo_twk);
5459 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461#ifdef FEAT_FOLDING
5462 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005463 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005465 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 to->wo_fdi = vim_strsave(from->wo_fdi);
5467 to->wo_fml = from->wo_fml;
5468 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005469 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005471 to->wo_fdm_save = from->wo_diff_saved
5472 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 to->wo_fdn = from->wo_fdn;
5474# ifdef FEAT_EVAL
5475 to->wo_fde = vim_strsave(from->wo_fde);
5476 to->wo_fdt = vim_strsave(from->wo_fdt);
5477# endif
5478 to->wo_fmr = vim_strsave(from->wo_fmr);
5479#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005480#ifdef FEAT_SIGNS
5481 to->wo_scl = vim_strsave(from->wo_scl);
5482#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005483
5484#ifdef FEAT_EVAL
5485 // Copy the script context so that we know where the value was last set.
5486 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5487 sizeof(to->wo_script_ctx));
5488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 check_winopt(to); /* don't want NULL pointers */
5490}
5491
5492/*
5493 * Check string options in a window for a NULL value.
5494 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005496check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 check_winopt(&win->w_onebuf_opt);
5499 check_winopt(&win->w_allbuf_opt);
5500}
5501
5502/*
5503 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5504 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005505 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005506check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508#ifdef FEAT_FOLDING
5509 check_string_option(&wop->wo_fdi);
5510 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005511 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512# ifdef FEAT_EVAL
5513 check_string_option(&wop->wo_fde);
5514 check_string_option(&wop->wo_fdt);
5515# endif
5516 check_string_option(&wop->wo_fmr);
5517#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005518#ifdef FEAT_SIGNS
5519 check_string_option(&wop->wo_scl);
5520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#ifdef FEAT_RIGHTLEFT
5522 check_string_option(&wop->wo_rlc);
5523#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005524#ifdef FEAT_LINEBREAK
5525 check_string_option(&wop->wo_sbr);
5526#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005527#ifdef FEAT_STL_OPT
5528 check_string_option(&wop->wo_stl);
5529#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005531 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005532 check_string_option(&wop->wo_cc);
5533#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005534#ifdef FEAT_CONCEAL
5535 check_string_option(&wop->wo_cocu);
5536#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005537#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005538 check_string_option(&wop->wo_twk);
5539 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005540#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005541#ifdef FEAT_LINEBREAK
5542 check_string_option(&wop->wo_briopt);
5543#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005544 check_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545}
5546
5547/*
5548 * Free the allocated memory inside a winopt_T.
5549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005551clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553#ifdef FEAT_FOLDING
5554 clear_string_option(&wop->wo_fdi);
5555 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005556 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557# ifdef FEAT_EVAL
5558 clear_string_option(&wop->wo_fde);
5559 clear_string_option(&wop->wo_fdt);
5560# endif
5561 clear_string_option(&wop->wo_fmr);
5562#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005563#ifdef FEAT_SIGNS
5564 clear_string_option(&wop->wo_scl);
5565#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005566#ifdef FEAT_LINEBREAK
5567 clear_string_option(&wop->wo_briopt);
5568#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005569 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570#ifdef FEAT_RIGHTLEFT
5571 clear_string_option(&wop->wo_rlc);
5572#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005573#ifdef FEAT_LINEBREAK
5574 clear_string_option(&wop->wo_sbr);
5575#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005576#ifdef FEAT_STL_OPT
5577 clear_string_option(&wop->wo_stl);
5578#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005579#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005580 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005581 clear_string_option(&wop->wo_cc);
5582#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005583#ifdef FEAT_CONCEAL
5584 clear_string_option(&wop->wo_cocu);
5585#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005586#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005587 clear_string_option(&wop->wo_twk);
5588 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590}
5591
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005592#ifdef FEAT_EVAL
5593// Index into the options table for a buffer-local option enum.
5594static int buf_opt_idx[BV_COUNT];
5595# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5596
5597/*
5598 * Initialize buf_opt_idx[] if not done already.
5599 */
5600 static void
5601init_buf_opt_idx(void)
5602{
5603 static int did_init_buf_opt_idx = FALSE;
5604 int i;
5605
5606 if (did_init_buf_opt_idx)
5607 return;
5608 did_init_buf_opt_idx = TRUE;
5609 for (i = 0; !istermoption_idx(i); i++)
5610 if (options[i].indir & PV_BUF)
5611 buf_opt_idx[options[i].indir & PV_MASK] = i;
5612}
5613#else
5614# define COPY_OPT_SCTX(buf, bv)
5615#endif
5616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617/*
5618 * Copy global option values to local options for one buffer.
5619 * Used when creating a new buffer and sometimes when entering a buffer.
5620 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005621 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5623 * appropriate.
5624 * BCO_NOHELP Don't copy the values to a help buffer.
5625 */
5626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005627buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 int should_copy = TRUE;
5630 char_u *save_p_isk = NULL; /* init for GCC */
5631 int dont_do_help;
5632 int did_isk = FALSE;
5633
5634 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 * Skip this when the option defaults have not been set yet. Happens when
5636 * main() allocates the first buffer.
5637 */
5638 if (p_cpo != NULL)
5639 {
5640 /*
5641 * Always copy when entering and 'cpo' contains 'S'.
5642 * Don't copy when already initialized.
5643 * Don't copy when 'cpo' contains 's' and not entering.
5644 * 'S' BCO_ENTER initialized 's' should_copy
5645 * yes yes X X TRUE
5646 * yes no yes X FALSE
5647 * no X yes X FALSE
5648 * X no no yes FALSE
5649 * X no no no TRUE
5650 * no yes no X TRUE
5651 */
5652 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5653 && (buf->b_p_initialized
5654 || (!(flags & BCO_ENTER)
5655 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5656 should_copy = FALSE;
5657
5658 if (should_copy || (flags & BCO_ALWAYS))
5659 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005660#ifdef FEAT_EVAL
Bram Moolenaar7eed9642019-10-19 20:57:28 +02005661 vim_memset(buf->b_p_script_ctx, 0, sizeof(buf->b_p_script_ctx));
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005662 init_buf_opt_idx();
5663#endif
5664 // Don't copy the options specific to a help buffer when
5665 // BCO_NOHELP is given or the options were initialized already
5666 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5668 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005669 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 save_p_isk = buf->b_p_isk;
5672 buf->b_p_isk = NULL;
5673 }
5674 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005675 * Always free the allocated strings. If not already initialized,
5676 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 */
5678 if (!buf->b_p_initialized)
5679 {
5680 free_buf_options(buf, TRUE);
5681 buf->b_p_ro = FALSE; /* don't copy readonly */
5682 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005684 switch (*p_ffs)
5685 {
5686 case 'm':
5687 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5688 case 'd':
5689 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5690 case 'u':
5691 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5692 default:
5693 buf->b_p_ff = vim_strsave(p_ff);
5694 }
5695 if (buf->b_p_ff != NULL)
5696 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 buf->b_p_bh = empty_option;
5698 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 else
5701 free_buf_options(buf, FALSE);
5702
5703 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005704 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 buf->b_p_ai_nopaste = p_ai_nopaste;
5706 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005707 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005709 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 buf->b_p_tw_nopaste = p_tw_nopaste;
5711 buf->b_p_tw_nobin = p_tw_nobin;
5712 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005713 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 buf->b_p_wm_nopaste = p_wm_nopaste;
5715 buf->b_p_wm_nobin = p_wm_nobin;
5716 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005717 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005718 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005719 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005720 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005721 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005723 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005725 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005727 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 buf->b_p_ml_nobin = p_ml_nobin;
5729 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005730 COPY_OPT_SCTX(buf, BV_INF);
5731 if (cmdmod.noswapfile)
5732 buf->b_p_swf = FALSE;
5733 else
5734 {
5735 buf->b_p_swf = p_swf;
5736 COPY_OPT_SCTX(buf, BV_INF);
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005739 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005740#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005741 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005742 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005744#ifdef FEAT_COMPL_FUNC
5745 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005746 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005747 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005748 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005749#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005750#ifdef FEAT_EVAL
5751 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005752 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005755 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005757#ifdef FEAT_VARTABS
5758 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005759 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005760 if (p_vsts && p_vsts != empty_option)
5761 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5762 else
5763 buf->b_p_vsts_array = 0;
5764 buf->b_p_vsts_nopaste = p_vsts_nopaste
5765 ? vim_strsave(p_vsts_nopaste) : NULL;
5766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005768 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005770 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#ifdef FEAT_FOLDING
5772 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005773 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774#endif
5775 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005776 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005777 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005778 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005779 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5780 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005782 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005784 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef FEAT_SMARTINDENT
5786 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005787 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788#endif
5789 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005790 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#ifdef FEAT_CINDENT
5792 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005793 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005795 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005797 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005799 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005802 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5804 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005805 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806#endif
5807#ifdef FEAT_LISP
5808 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005809 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#endif
5811#ifdef FEAT_SYN_HL
5812 /* Don't copy 'syntax', it must be set */
5813 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005814 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005815 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005816 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005817#endif
5818#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005819 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005820 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005821 (void)compile_cap_prog(&buf->b_s);
5822 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005823 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005824 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005825 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
5827#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5828 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005829 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005831 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005833 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005834#if defined(FEAT_EVAL)
5835 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005836 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838#ifdef FEAT_CRYPT
5839 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005840 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#endif
5842#ifdef FEAT_SEARCHPATH
5843 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005844 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845#endif
5846#ifdef FEAT_KEYMAP
5847 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005848 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 buf->b_kmap_state |= KEYMAP_INIT;
5850#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005851#ifdef FEAT_TERMINAL
5852 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005853 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /* This isn't really an option, but copying the langmap and IME
5856 * state from the current buffer is better than resetting it. */
5857 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005858 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005860 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862 /* options that are normally global but also have a local value
5863 * are not copied, start using the global value */
5864 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005865 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005866 buf->b_p_bkc = empty_option;
5867 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868#ifdef FEAT_QUICKFIX
5869 buf->b_p_gp = empty_option;
5870 buf->b_p_mp = empty_option;
5871 buf->b_p_efm = empty_option;
5872#endif
5873 buf->b_p_ep = empty_option;
5874 buf->b_p_kp = empty_option;
5875 buf->b_p_path = empty_option;
5876 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005877 buf->b_p_tc = empty_option;
5878 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879#ifdef FEAT_FIND_ID
5880 buf->b_p_def = empty_option;
5881 buf->b_p_inc = empty_option;
5882# ifdef FEAT_EVAL
5883 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005884 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885# endif
5886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 buf->b_p_dict = empty_option;
5888 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005889#ifdef FEAT_TEXTOBJ
5890 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005891 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005892#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005893#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5894 buf->b_p_bexpr = empty_option;
5895#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005896#if defined(FEAT_CRYPT)
5897 buf->b_p_cm = empty_option;
5898#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005899#ifdef FEAT_PERSISTENT_UNDO
5900 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005901 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005902#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005903#ifdef FEAT_LISP
5904 buf->b_p_lw = empty_option;
5905#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005906 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
5908 /*
5909 * Don't copy the options set by ex_help(), use the saved values,
5910 * when going from a help buffer to a non-help buffer.
5911 * Don't touch these at all when BCO_NOHELP is used and going from
5912 * or to a help buffer.
5913 */
5914 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005917#ifdef FEAT_VARTABS
5918 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5919 tabstop_set(p_vts, &buf->b_p_vts_array);
5920 else
5921 buf->b_p_vts_array = NULL;
5922#endif
5923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 else
5925 {
5926 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005927 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 did_isk = TRUE;
5929 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005930#ifdef FEAT_VARTABS
5931 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005933 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5934 tabstop_set(p_vts, &buf->b_p_vts_array);
5935 else
5936 buf->b_p_vts_array = NULL;
5937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 if (buf->b_p_bt[0] == 'h')
5940 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005942 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 }
5944 }
5945
5946 /*
5947 * When the options should be copied (ignoring BCO_ALWAYS), set the
5948 * flag that indicates that the options have been initialized.
5949 */
5950 if (should_copy)
5951 buf->b_p_initialized = TRUE;
5952 }
5953
5954 check_buf_options(buf); /* make sure we don't have NULLs */
5955 if (did_isk)
5956 (void)buf_init_chartab(buf, FALSE);
5957}
5958
5959/*
5960 * Reset the 'modifiable' option and its default value.
5961 */
5962 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005963reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964{
5965 int opt_idx;
5966
5967 curbuf->b_p_ma = FALSE;
5968 p_ma = FALSE;
5969 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00005970 if (opt_idx >= 0)
5971 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972}
5973
5974/*
5975 * Set the global value for 'iminsert' to the local value.
5976 */
5977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005978set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979{
5980 p_iminsert = curbuf->b_p_iminsert;
5981}
5982
5983/*
5984 * Set the global value for 'imsearch' to the local value.
5985 */
5986 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005987set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 p_imsearch = curbuf->b_p_imsearch;
5990}
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992static int expand_option_idx = -1;
5993static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
5994static int expand_option_flags = 0;
5995
5996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005997set_context_in_set_cmd(
5998 expand_T *xp,
5999 char_u *arg,
6000 int opt_flags) /* OPT_GLOBAL and/or OPT_LOCAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 int nextchar;
6003 long_u flags = 0; /* init for GCC */
6004 int opt_idx = 0; /* init for GCC */
6005 char_u *p;
6006 char_u *s;
6007 int is_term_option = FALSE;
6008 int key;
6009
6010 expand_option_flags = opt_flags;
6011
6012 xp->xp_context = EXPAND_SETTINGS;
6013 if (*arg == NUL)
6014 {
6015 xp->xp_pattern = arg;
6016 return;
6017 }
6018 p = arg + STRLEN(arg) - 1;
6019 if (*p == ' ' && *(p - 1) != '\\')
6020 {
6021 xp->xp_pattern = p + 1;
6022 return;
6023 }
6024 while (p > arg)
6025 {
6026 s = p;
6027 /* count number of backslashes before ' ' or ',' */
6028 if (*p == ' ' || *p == ',')
6029 {
6030 while (s > arg && *(s - 1) == '\\')
6031 --s;
6032 }
6033 /* break at a space with an even number of backslashes */
6034 if (*p == ' ' && ((p - s) & 1) == 0)
6035 {
6036 ++p;
6037 break;
6038 }
6039 --p;
6040 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006041 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {
6043 xp->xp_context = EXPAND_BOOL_SETTINGS;
6044 p += 2;
6045 }
6046 if (STRNCMP(p, "inv", 3) == 0)
6047 {
6048 xp->xp_context = EXPAND_BOOL_SETTINGS;
6049 p += 3;
6050 }
6051 xp->xp_pattern = arg = p;
6052 if (*arg == '<')
6053 {
6054 while (*p != '>')
6055 if (*p++ == NUL) /* expand terminal option name */
6056 return;
6057 key = get_special_key_code(arg + 1);
6058 if (key == 0) /* unknown name */
6059 {
6060 xp->xp_context = EXPAND_NOTHING;
6061 return;
6062 }
6063 nextchar = *++p;
6064 is_term_option = TRUE;
6065 expand_option_name[2] = KEY2TERMCAP0(key);
6066 expand_option_name[3] = KEY2TERMCAP1(key);
6067 }
6068 else
6069 {
6070 if (p[0] == 't' && p[1] == '_')
6071 {
6072 p += 2;
6073 if (*p != NUL)
6074 ++p;
6075 if (*p == NUL)
6076 return; /* expand option name */
6077 nextchar = *++p;
6078 is_term_option = TRUE;
6079 expand_option_name[2] = p[-2];
6080 expand_option_name[3] = p[-1];
6081 }
6082 else
6083 {
6084 /* Allow * wildcard */
6085 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6086 p++;
6087 if (*p == NUL)
6088 return;
6089 nextchar = *p;
6090 *p = NUL;
6091 opt_idx = findoption(arg);
6092 *p = nextchar;
6093 if (opt_idx == -1 || options[opt_idx].var == NULL)
6094 {
6095 xp->xp_context = EXPAND_NOTHING;
6096 return;
6097 }
6098 flags = options[opt_idx].flags;
6099 if (flags & P_BOOL)
6100 {
6101 xp->xp_context = EXPAND_NOTHING;
6102 return;
6103 }
6104 }
6105 }
6106 /* handle "-=" and "+=" */
6107 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6108 {
6109 ++p;
6110 nextchar = '=';
6111 }
6112 if ((nextchar != '=' && nextchar != ':')
6113 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6114 {
6115 xp->xp_context = EXPAND_UNSUCCESSFUL;
6116 return;
6117 }
6118 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6119 {
6120 xp->xp_context = EXPAND_OLD_SETTING;
6121 if (is_term_option)
6122 expand_option_idx = -1;
6123 else
6124 expand_option_idx = opt_idx;
6125 xp->xp_pattern = p + 1;
6126 return;
6127 }
6128 xp->xp_context = EXPAND_NOTHING;
6129 if (is_term_option || (flags & P_NUM))
6130 return;
6131
6132 xp->xp_pattern = p + 1;
6133
6134 if (flags & P_EXPAND)
6135 {
6136 p = options[opt_idx].var;
6137 if (p == (char_u *)&p_bdir
6138 || p == (char_u *)&p_dir
6139 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006140 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 || p == (char_u *)&p_rtp
6142#ifdef FEAT_SEARCHPATH
6143 || p == (char_u *)&p_cdpath
6144#endif
6145#ifdef FEAT_SESSION
6146 || p == (char_u *)&p_vdir
6147#endif
6148 )
6149 {
6150 xp->xp_context = EXPAND_DIRECTORIES;
6151 if (p == (char_u *)&p_path
6152#ifdef FEAT_SEARCHPATH
6153 || p == (char_u *)&p_cdpath
6154#endif
6155 )
6156 xp->xp_backslash = XP_BS_THREE;
6157 else
6158 xp->xp_backslash = XP_BS_ONE;
6159 }
6160 else
6161 {
6162 xp->xp_context = EXPAND_FILES;
6163 /* for 'tags' need three backslashes for a space */
6164 if (p == (char_u *)&p_tags)
6165 xp->xp_backslash = XP_BS_THREE;
6166 else
6167 xp->xp_backslash = XP_BS_ONE;
6168 }
6169 }
6170
6171 /* For an option that is a list of file names, find the start of the
6172 * last file name. */
6173 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6174 {
6175 /* count number of backslashes before ' ' or ',' */
6176 if (*p == ' ' || *p == ',')
6177 {
6178 s = p;
6179 while (s > xp->xp_pattern && *(s - 1) == '\\')
6180 --s;
6181 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6182 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6183 {
6184 xp->xp_pattern = p + 1;
6185 break;
6186 }
6187 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006188
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006189#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006190 /* for 'spellsuggest' start at "file:" */
6191 if (options[opt_idx].var == (char_u *)&p_sps
6192 && STRNCMP(p, "file:", 5) == 0)
6193 {
6194 xp->xp_pattern = p + 5;
6195 break;
6196 }
6197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
6199
6200 return;
6201}
6202
6203 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006204ExpandSettings(
6205 expand_T *xp,
6206 regmatch_T *regmatch,
6207 int *num_file,
6208 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 int num_normal = 0; /* Nr of matching non-term-code settings */
6211 int num_term = 0; /* Nr of matching terminal code settings */
6212 int opt_idx;
6213 int match;
6214 int count = 0;
6215 char_u *str;
6216 int loop;
6217 int is_term_opt;
6218 char_u name_buf[MAX_KEY_NAME_LEN];
6219 static char *(names[]) = {"all", "termcap"};
6220 int ic = regmatch->rm_ic; /* remember the ignore-case flag */
6221
6222 /* do this loop twice:
6223 * loop == 0: count the number of matching options
6224 * loop == 1: copy the matching options into allocated memory
6225 */
6226 for (loop = 0; loop <= 1; ++loop)
6227 {
6228 regmatch->rm_ic = ic;
6229 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6230 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006231 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6232 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6234 {
6235 if (loop == 0)
6236 num_normal++;
6237 else
6238 (*file)[count++] = vim_strsave((char_u *)names[match]);
6239 }
6240 }
6241 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6242 opt_idx++)
6243 {
6244 if (options[opt_idx].var == NULL)
6245 continue;
6246 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6247 && !(options[opt_idx].flags & P_BOOL))
6248 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006249 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 if (is_term_opt && num_normal > 0)
6251 continue;
6252 match = FALSE;
6253 if (vim_regexec(regmatch, str, (colnr_T)0)
6254 || (options[opt_idx].shortname != NULL
6255 && vim_regexec(regmatch,
6256 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6257 match = TRUE;
6258 else if (is_term_opt)
6259 {
6260 name_buf[0] = '<';
6261 name_buf[1] = 't';
6262 name_buf[2] = '_';
6263 name_buf[3] = str[2];
6264 name_buf[4] = str[3];
6265 name_buf[5] = '>';
6266 name_buf[6] = NUL;
6267 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6268 {
6269 match = TRUE;
6270 str = name_buf;
6271 }
6272 }
6273 if (match)
6274 {
6275 if (loop == 0)
6276 {
6277 if (is_term_opt)
6278 num_term++;
6279 else
6280 num_normal++;
6281 }
6282 else
6283 (*file)[count++] = vim_strsave(str);
6284 }
6285 }
6286 /*
6287 * Check terminal key codes, these are not in the option table
6288 */
6289 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6290 {
6291 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6292 {
6293 if (!isprint(str[0]) || !isprint(str[1]))
6294 continue;
6295
6296 name_buf[0] = 't';
6297 name_buf[1] = '_';
6298 name_buf[2] = str[0];
6299 name_buf[3] = str[1];
6300 name_buf[4] = NUL;
6301
6302 match = FALSE;
6303 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6304 match = TRUE;
6305 else
6306 {
6307 name_buf[0] = '<';
6308 name_buf[1] = 't';
6309 name_buf[2] = '_';
6310 name_buf[3] = str[0];
6311 name_buf[4] = str[1];
6312 name_buf[5] = '>';
6313 name_buf[6] = NUL;
6314
6315 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6316 match = TRUE;
6317 }
6318 if (match)
6319 {
6320 if (loop == 0)
6321 num_term++;
6322 else
6323 (*file)[count++] = vim_strsave(name_buf);
6324 }
6325 }
6326
6327 /*
6328 * Check special key names.
6329 */
6330 regmatch->rm_ic = TRUE; /* ignore case here */
6331 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6332 {
6333 name_buf[0] = '<';
6334 STRCPY(name_buf + 1, str);
6335 STRCAT(name_buf, ">");
6336
6337 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6338 {
6339 if (loop == 0)
6340 num_term++;
6341 else
6342 (*file)[count++] = vim_strsave(name_buf);
6343 }
6344 }
6345 }
6346 if (loop == 0)
6347 {
6348 if (num_normal > 0)
6349 *num_file = num_normal;
6350 else if (num_term > 0)
6351 *num_file = num_term;
6352 else
6353 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006354 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 if (*file == NULL)
6356 {
6357 *file = (char_u **)"";
6358 return FAIL;
6359 }
6360 }
6361 }
6362 return OK;
6363}
6364
6365 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 char_u *var = NULL; /* init for GCC */
6369 char_u *buf;
6370
6371 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006372 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (*file == NULL)
6374 return FAIL;
6375
6376 /*
6377 * For a terminal key code expand_option_idx is < 0.
6378 */
6379 if (expand_option_idx < 0)
6380 {
6381 var = find_termcode(expand_option_name + 2);
6382 if (var == NULL)
6383 expand_option_idx = findoption(expand_option_name);
6384 }
6385
6386 if (expand_option_idx >= 0)
6387 {
6388 /* put string of option value in NameBuff */
6389 option_value2string(&options[expand_option_idx], expand_option_flags);
6390 var = NameBuff;
6391 }
6392 else if (var == NULL)
6393 var = (char_u *)"";
6394
6395 /* A backslash is required before some characters. This is the reverse of
6396 * what happens in do_set(). */
6397 buf = vim_strsave_escaped(var, escape_chars);
6398
6399 if (buf == NULL)
6400 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006401 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 return FAIL;
6403 }
6404
6405#ifdef BACKSLASH_IN_FILENAME
6406 /* For MS-Windows et al. we don't double backslashes at the start and
6407 * before a file name character. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006408 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (var[0] == '\\' && var[1] == '\\'
6410 && expand_option_idx >= 0
6411 && (options[expand_option_idx].flags & P_EXPAND)
6412 && vim_isfilec(var[2])
6413 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006414 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415#endif
6416
6417 *file[0] = buf;
6418 *num_file = 1;
6419 return OK;
6420}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422/*
6423 * Get the value for the numeric or string option *opp in a nice format into
6424 * NameBuff[]. Must not be called with a hidden option!
6425 */
6426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006427option_value2string(
6428 struct vimoption *opp,
6429 int opt_flags) /* OPT_GLOBAL and/or OPT_LOCAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 char_u *varp;
6432
6433 varp = get_varp_scope(opp, opt_flags);
6434
6435 if (opp->flags & P_NUM)
6436 {
6437 long wc = 0;
6438
6439 if (wc_use_keyname(varp, &wc))
6440 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6441 else if (wc != 0)
6442 STRCPY(NameBuff, transchar((int)wc));
6443 else
6444 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6445 }
6446 else /* P_STRING */
6447 {
6448 varp = *(char_u **)(varp);
6449 if (varp == NULL) /* just in case */
6450 NameBuff[0] = NUL;
6451#ifdef FEAT_CRYPT
6452 /* don't show the actual value of 'key', only that it's set */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006453 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 STRCPY(NameBuff, "*****");
6455#endif
6456 else if (opp->flags & P_EXPAND)
6457 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
6458 /* Translate 'pastetoggle' into special key names */
6459 else if ((char_u **)opp->var == &p_pt)
6460 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6461 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006462 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 }
6464}
6465
6466/*
6467 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6468 * printed as a keyname.
6469 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6470 */
6471 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6475 {
6476 *wcp = *(long *)varp;
6477 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6478 return TRUE;
6479 }
6480 return FALSE;
6481}
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483/*
6484 * Return TRUE if format option 'x' is in effect.
6485 * Take care of no formatting when 'paste' is set.
6486 */
6487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006488has_format_option(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 if (p_paste)
6491 return FALSE;
6492 return (vim_strchr(curbuf->b_p_fo, x) != NULL);
6493}
6494
6495/*
6496 * Return TRUE if "x" is present in 'shortmess' option, or
6497 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6498 */
6499 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006500shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006502 return p_shm != NULL &&
6503 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 || (vim_strchr(p_shm, 'a') != NULL
6505 && vim_strchr((char_u *)SHM_A, x) != NULL));
6506}
6507
6508/*
6509 * paste_option_changed() - Called after p_paste was set or reset.
6510 */
6511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006512paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 static int old_p_paste = FALSE;
6515 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006516 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517#ifdef FEAT_CMDL_INFO
6518 static int save_ru = 0;
6519#endif
6520#ifdef FEAT_RIGHTLEFT
6521 static int save_ri = 0;
6522 static int save_hkmap = 0;
6523#endif
6524 buf_T *buf;
6525
6526 if (p_paste)
6527 {
6528 /*
6529 * Paste switched from off to on.
6530 * Save the current values, so they can be restored later.
6531 */
6532 if (!old_p_paste)
6533 {
6534 /* save options for each buffer */
Bram Moolenaar29323592016-07-24 22:04:11 +02006535 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
6537 buf->b_p_tw_nopaste = buf->b_p_tw;
6538 buf->b_p_wm_nopaste = buf->b_p_wm;
6539 buf->b_p_sts_nopaste = buf->b_p_sts;
6540 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006541 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006542#ifdef FEAT_VARTABS
6543 if (buf->b_p_vsts_nopaste)
6544 vim_free(buf->b_p_vsts_nopaste);
6545 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6546 ? vim_strsave(buf->b_p_vsts) : NULL;
6547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549
6550 /* save global options */
6551 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006552 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553#ifdef FEAT_CMDL_INFO
6554 save_ru = p_ru;
6555#endif
6556#ifdef FEAT_RIGHTLEFT
6557 save_ri = p_ri;
6558 save_hkmap = p_hkmap;
6559#endif
6560 /* save global values for local buffer options */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006561 p_ai_nopaste = p_ai;
6562 p_et_nopaste = p_et;
6563 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 p_tw_nopaste = p_tw;
6565 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006566#ifdef FEAT_VARTABS
6567 if (p_vsts_nopaste)
6568 vim_free(p_vsts_nopaste);
6569 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572
6573 /*
6574 * Always set the option values, also when 'paste' is set when it is
6575 * already on.
6576 */
6577 /* set options for each buffer */
Bram Moolenaar29323592016-07-24 22:04:11 +02006578 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
6580 buf->b_p_tw = 0; /* textwidth is 0 */
6581 buf->b_p_wm = 0; /* wrapmargin is 0 */
6582 buf->b_p_sts = 0; /* softtabstop is 0 */
6583 buf->b_p_ai = 0; /* no auto-indent */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006584 buf->b_p_et = 0; /* no expandtab */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006585#ifdef FEAT_VARTABS
6586 if (buf->b_p_vsts)
6587 free_string_option(buf->b_p_vsts);
6588 buf->b_p_vsts = empty_option;
6589 if (buf->b_p_vsts_array)
6590 vim_free(buf->b_p_vsts_array);
6591 buf->b_p_vsts_array = 0;
6592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594
6595 /* set global options */
6596 p_sm = 0; /* no showmatch */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006597 p_sta = 0; /* no smarttab */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 if (p_ru)
6600 status_redraw_all(); /* redraw to remove the ruler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 p_ru = 0; /* no ruler */
6602#endif
6603#ifdef FEAT_RIGHTLEFT
6604 p_ri = 0; /* no reverse insert */
6605 p_hkmap = 0; /* no Hebrew keyboard */
6606#endif
6607 /* set global values for local buffer options */
6608 p_tw = 0;
6609 p_wm = 0;
6610 p_sts = 0;
6611 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006612#ifdef FEAT_VARTABS
6613 if (p_vsts)
6614 free_string_option(p_vsts);
6615 p_vsts = empty_option;
6616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 }
6618
6619 /*
6620 * Paste switched from on to off: Restore saved values.
6621 */
6622 else if (old_p_paste)
6623 {
6624 /* restore options for each buffer */
Bram Moolenaar29323592016-07-24 22:04:11 +02006625 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
6627 buf->b_p_tw = buf->b_p_tw_nopaste;
6628 buf->b_p_wm = buf->b_p_wm_nopaste;
6629 buf->b_p_sts = buf->b_p_sts_nopaste;
6630 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006631 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006632#ifdef FEAT_VARTABS
6633 if (buf->b_p_vsts)
6634 free_string_option(buf->b_p_vsts);
6635 buf->b_p_vsts = buf->b_p_vsts_nopaste
6636 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6637 if (buf->b_p_vsts_array)
6638 vim_free(buf->b_p_vsts_array);
6639 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6640 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6641 else
6642 buf->b_p_vsts_array = 0;
6643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645
6646 /* restore global options */
6647 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006648 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (p_ru != save_ru)
6651 status_redraw_all(); /* redraw to draw the ruler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 p_ru = save_ru;
6653#endif
6654#ifdef FEAT_RIGHTLEFT
6655 p_ri = save_ri;
6656 p_hkmap = save_hkmap;
6657#endif
6658 /* set global values for local buffer options */
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006659 p_ai = p_ai_nopaste;
6660 p_et = p_et_nopaste;
6661 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 p_tw = p_tw_nopaste;
6663 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006664#ifdef FEAT_VARTABS
6665 if (p_vsts)
6666 free_string_option(p_vsts);
6667 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
6670
6671 old_p_paste = p_paste;
6672}
6673
6674/*
6675 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6676 *
6677 * Reset 'compatible' and set the values for options that didn't get set yet
6678 * to the Vim defaults.
6679 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006680 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 */
6682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006683vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006685 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006686 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006687 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 if (!option_was_set((char_u *)"cp"))
6690 {
6691 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006692 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6694 set_option_default(opt_idx, OPT_FREE, FALSE);
6695 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006696 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006698
6699 if (fname != NULL)
6700 {
6701 p = vim_getenv(envname, &dofree);
6702 if (p == NULL)
6703 {
6704 /* Set $MYVIMRC to the first vimrc file found. */
6705 p = FullName_save(fname, FALSE);
6706 if (p != NULL)
6707 {
6708 vim_setenv(envname, p);
6709 vim_free(p);
6710 }
6711 }
6712 else if (dofree)
6713 vim_free(p);
6714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715}
6716
6717/*
6718 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6719 */
6720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006721change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006723 int opt_idx;
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 if (p_cp != on)
6726 {
6727 p_cp = on;
6728 compatible_set();
6729 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006730 opt_idx = findoption((char_u *)"cp");
6731 if (opt_idx >= 0)
6732 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
6734
6735/*
6736 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006737 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 */
6739 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006740option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 int idx;
6743
6744 idx = findoption(name);
6745 if (idx < 0) /* unknown option */
6746 return FALSE;
6747 if (options[idx].flags & P_WAS_SET)
6748 return TRUE;
6749 return FALSE;
6750}
6751
6752/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006753 * Reset the flag indicating option "name" was set.
6754 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006755 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006756reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006757{
6758 int idx = findoption(name);
6759
6760 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006761 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006762 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006763 return OK;
6764 }
6765 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006766}
6767
6768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 * compatible_set() - Called when 'compatible' has been set or unset.
6770 *
6771 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6772 * flag) to a Vi compatible value.
6773 * When 'compatible' is unset: Set all options that have a different default
6774 * for Vim (without the P_VI_DEF flag) to that default.
6775 */
6776 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006777compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 int opt_idx;
6780
Bram Moolenaardac13472019-09-16 21:06:21 +02006781 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6783 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6784 set_option_default(opt_idx, OPT_FREE, p_cp);
6785 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006786 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787}
6788
Bram Moolenaardac13472019-09-16 21:06:21 +02006789#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791/*
6792 * fill_breakat_flags() -- called when 'breakat' changes value.
6793 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006795fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006797 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 int i;
6799
6800 for (i = 0; i < 256; i++)
6801 breakat_flags[i] = FALSE;
6802
6803 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006804 for (p = p_breakat; *p; p++)
6805 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807#endif
6808
6809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 * Check if backspacing over something is allowed.
6811 */
6812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006813can_bs(
6814 int what) /* BS_INDENT, BS_EOL or BS_START */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006816#ifdef FEAT_JOB_CHANNEL
6817 if (what == BS_START && bt_prompt(curbuf))
6818 return FALSE;
6819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 switch (*p_bs)
6821 {
6822 case '2': return TRUE;
6823 case '1': return (what != BS_START);
6824 case '0': return FALSE;
6825 }
6826 return vim_strchr(p_bs, what) != NULL;
6827}
6828
6829/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006830 * Return the effective 'scrolloff' value for the current window, using the
6831 * global value when appropriate.
6832 */
6833 long
6834get_scrolloff_value(void)
6835{
6836 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6837}
6838
6839/*
6840 * Return the effective 'sidescrolloff' value for the current window, using the
6841 * global value when appropriate.
6842 */
6843 long
6844get_sidescrolloff_value(void)
6845{
6846 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6847}
6848
6849/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006850 * Get the local or global value of 'backupcopy'.
6851 */
6852 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006853get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006854{
6855 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6856}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006857
Bram Moolenaaree857022019-11-09 23:26:40 +01006858#if defined(FEAT_LINEBREAK) || defined(PROTO)
6859/*
6860 * Get the local or global value of 'showbreak'.
6861 */
6862 char_u *
6863get_showbreak_value(win_T *win)
6864{
6865 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6866 return p_sbr;
6867 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6868 return empty_option;
6869 return win->w_p_sbr;
6870}
6871#endif
6872
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006873#if defined(FEAT_EVAL) || defined(PROTO)
6874/*
6875 * Get window or buffer local options.
6876 */
6877 dict_T *
6878get_winbuf_options(int bufopt)
6879{
6880 dict_T *d;
6881 int opt_idx;
6882
6883 d = dict_alloc();
6884 if (d == NULL)
6885 return NULL;
6886
Bram Moolenaardac13472019-09-16 21:06:21 +02006887 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006888 {
6889 struct vimoption *opt = &options[opt_idx];
6890
6891 if ((bufopt && (opt->indir & PV_BUF))
6892 || (!bufopt && (opt->indir & PV_WIN)))
6893 {
6894 char_u *varp = get_varp(opt);
6895
6896 if (varp != NULL)
6897 {
6898 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006899 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006900 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006901 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006902 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006903 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006904 }
6905 }
6906 }
6907
6908 return d;
6909}
6910#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006911
Bram Moolenaardac13472019-09-16 21:06:21 +02006912#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006913/*
6914 * This is called when 'culopt' is changed
6915 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006916 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006917fill_culopt_flags(char_u *val, win_T *wp)
6918{
6919 char_u *p;
6920 char_u culopt_flags_new = 0;
6921
6922 if (val == NULL)
6923 p = wp->w_p_culopt;
6924 else
6925 p = val;
6926 while (*p != NUL)
6927 {
6928 if (STRNCMP(p, "line", 4) == 0)
6929 {
6930 p += 4;
6931 culopt_flags_new |= CULOPT_LINE;
6932 }
6933 else if (STRNCMP(p, "both", 4) == 0)
6934 {
6935 p += 4;
6936 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
6937 }
6938 else if (STRNCMP(p, "number", 6) == 0)
6939 {
6940 p += 6;
6941 culopt_flags_new |= CULOPT_NBR;
6942 }
6943 else if (STRNCMP(p, "screenline", 10) == 0)
6944 {
6945 p += 10;
6946 culopt_flags_new |= CULOPT_SCRLINE;
6947 }
6948
6949 if (*p != ',' && *p != NUL)
6950 return FAIL;
6951 if (*p == ',')
6952 ++p;
6953 }
6954
6955 // Can't have both "line" and "screenline".
6956 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
6957 return FAIL;
6958 wp->w_p_culopt_flags = culopt_flags_new;
6959
6960 return OK;
6961}
6962#endif