blob: 7c783757b2d4a1d05b138d4c5d97d47d0980c521 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
60static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void option_value2string(struct vimoption *, int opt_flags);
63static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
148 for (n = 0; n < (long)(sizeof(names) / sizeof(char *)); ++n)
149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef HAVE_AVAIL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#else
205# ifdef HAVE_TOTAL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100206 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000207 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_SEARCHPATH
226 {
227 char_u *cdpath;
228 char_u *buf;
229 int i;
230 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000231 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100233 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000234 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (cdpath != NULL)
236 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200237 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (buf != NULL)
239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100240 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 j = 1;
242 for (i = 0; cdpath[i] != NUL; ++i)
243 {
244 if (vim_ispathlistsep(cdpath[i]))
245 buf[j++] = ',';
246 else
247 {
248 if (cdpath[i] == ' ' || cdpath[i] == ',')
249 buf[j++] = '\\';
250 buf[j++] = cdpath[i];
251 }
252 }
253 buf[j] = NUL;
254 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000255 if (opt_idx >= 0)
256 {
257 options[opt_idx].def_val[VI_DEFAULT] = buf;
258 options[opt_idx].flags |= P_DEF_ALLOCED;
259 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200260 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100261 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000263 if (mustfree)
264 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
267#endif
268
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100270 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100272# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 (char_u *)"cp1252"
274# else
275# ifdef VMS
276 (char_u *)"dec-mcs"
277# else
278# ifdef EBCDIC
279 (char_u *)"ebcdic-uk"
280# else
281# ifdef MAC
282 (char_u *)"mac-roman"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100283# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 (char_u *)"hp-roman8"
285# endif
286# endif
287# endif
288# endif
289 );
290#endif
291
292#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100293 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100295# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000296 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297# else
298# ifdef VMS
299 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
300
301# else
302 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
303# endif
304# endif
305 );
306#endif
307
308 /*
309 * Set all the options (except the terminal options) to their default
310 * value. Also set the global value for local options.
311 */
312 set_options_default(0);
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
334 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
392 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000427# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100428 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000429 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# endif
432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100433 // enc_locale() will try to find the encoding of the current locale.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 p = enc_locale();
435 if (p != NULL)
436 {
437 char_u *save_enc;
438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100439 // Try setting 'encoding' and check if the value is valid.
440 // If not, go back to the default "latin1".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 save_enc = p_enc;
442 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000443 if (STRCMP(p_enc, "gb18030") == 0)
444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100445 // We don't support "gb18030", but "cp936" is a good substitute
446 // for practical purposes, thus use that. It's not an alias to
447 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000448 p_enc = vim_strsave((char_u *)"cp936");
449 vim_free(p);
450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if (mb_init() == NULL)
452 {
453 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000454 if (opt_idx >= 0)
455 {
456 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
457 options[opt_idx].flags |= P_DEF_ALLOCED;
458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
Bram Moolenaard0573012017-10-28 21:11:06 +0200460#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100463 // Adjust the default for 'isprint' and 'iskeyword' to match
464 // latin1. Also set the defaults for when 'nocompatible' is
465 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000466 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000467 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000468 set_string_option_direct((char_u *)"isk", -1,
469 ISK_LATIN1, OPT_FREE, SID_NONE);
470 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000471 if (opt_idx >= 0)
472 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000473 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000474 if (opt_idx >= 0)
475 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000476 (void)init_chartab();
477 }
478#endif
479
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200480#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100481 // Win32 console: When GetACP() returns a different value from
482 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200483 if (
484# ifdef VIMDLL
485 (!gui.in_use && !gui.starting) &&
486# endif
487 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
489 char buf[50];
490
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
492 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100493 if (GetConsoleCP() == 0)
494 sprintf(buf, "cp%ld", (long)GetACP());
495 else
496 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 p_tenc = vim_strsave((char_u *)buf);
498 if (p_tenc != NULL)
499 {
500 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000501 if (opt_idx >= 0)
502 {
503 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
504 options[opt_idx].flags |= P_DEF_ALLOCED;
505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 convert_setup(&input_conv, p_tenc, p_enc);
507 convert_setup(&output_conv, p_enc, p_tenc);
508 }
509 else
510 p_tenc = empty_option;
511 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100512#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100513#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100514 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000515 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 else
519 {
520 vim_free(p_enc);
521 p_enc = save_enc;
522 }
523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
525#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100526 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 set_helplang_default(get_mess_lang());
528#endif
529}
530
531/*
532 * Set an option to its default value.
533 * This does not take care of side effects!
534 */
535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536set_option_default(
537 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100538 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
539 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 char_u *varp; // pointer to variable for current option
542 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000544 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
546
547 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
548 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
551 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
552 if (flags & P_STRING)
553 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100554 // Use set_string_option_direct() for local options to handle
555 // freeing and allocating the value.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200556 if (options[opt_idx].indir != PV_NONE)
557 set_string_option_direct(NULL, opt_idx,
558 options[opt_idx].def_val[dvi], opt_flags, 0);
559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200561 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
562 free_string_option(*(char_u **)(varp));
563 *(char_u **)varp = options[opt_idx].def_val[dvi];
564 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 }
567 else if (flags & P_NUM)
568 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000569 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 win_comp_scroll(curwin);
571 else
572 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100573 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
574
575 if ((long *)varp == &curwin->w_p_so
576 || (long *)varp == &curwin->w_p_siso)
577 // 'scrolloff' and 'sidescrolloff' local values have a
578 // different default value than the global default.
579 *(long *)varp = -1;
580 else
581 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100582 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (both)
584 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100585 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100590 // the cast to long is required for Manx C, long_i is needed for
591 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000592 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000593#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000595 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
596 *(int *)varp = FALSE;
597#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (both)
600 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
601 *(int *)varp;
602 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000605 flagsp = insecure_flag(opt_idx, opt_flags);
606 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608
609#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200610 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#endif
612}
613
614/*
615 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200616 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100620 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000624 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaardac13472019-09-16 21:06:21 +0200626 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200627 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200628 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100629 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200630# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200631 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200632 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200633# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100634 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 set_option_default(i, opt_flags, p_cp);
636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100637 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000638 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200640#ifdef FEAT_CINDENT
641 parse_cino(curbuf);
642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643}
644
645/*
646 * Set the Vi-default value of a string option.
647 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100648 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100650 static void
651set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 char_u *p;
654 int opt_idx;
655
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100656 if (escape && vim_strchr(val, ' ') != NULL)
657 p = vim_strsave_escaped(val, (char_u *)" ");
658 else
659 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000663 if (opt_idx >= 0)
664 {
665 if (options[opt_idx].flags & P_DEF_ALLOCED)
666 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
667 options[opt_idx].def_val[VI_DEFAULT] = p;
668 options[opt_idx].flags |= P_DEF_ALLOCED;
669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671}
672
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 void
674set_string_default(char *name, char_u *val)
675{
676 set_string_default_esc(name, val, FALSE);
677}
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200680 * For an option value that contains comma separated items, find "newval" in
681 * "origval". Return NULL if not found.
682 */
683 static char_u *
684find_dup_item(char_u *origval, char_u *newval, long_u flags)
685{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200686 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200687 size_t newlen;
688 char_u *s;
689
690 if (origval == NULL)
691 return NULL;
692
693 newlen = STRLEN(newval);
694 for (s = origval; *s != NUL; ++s)
695 {
696 if ((!(flags & P_COMMA)
697 || s == origval
698 || (s[-1] == ',' && !(bs & 1)))
699 && STRNCMP(s, newval, newlen) == 0
700 && (!(flags & P_COMMA)
701 || s[newlen] == ','
702 || s[newlen] == NUL))
703 return s;
704 // Count backslashes. Only a comma with an even number of backslashes
705 // or a single backslash preceded by a comma before it is recognized as
706 // a separator.
707 if ((s > origval + 1
708 && s[-1] == '\\'
709 && s[-2] != ',')
710 || (s == origval + 1
711 && s[-1] == '\\'))
712 ++bs;
713 else
714 bs = 0;
715 }
716 return NULL;
717}
718
719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 * Set the Vi-default value of a number option.
721 * Used for 'lines' and 'columns'.
722 */
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000726 int opt_idx;
727
728 opt_idx = findoption((char_u *)name);
729 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000730 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731}
732
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200733/*
734 * Set all window-local and buffer-local options to the Vim default.
735 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200736 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200737 */
738 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200739set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200740{
741 win_T *save_curwin = curwin;
742 int i;
743
744 curwin = wp;
745 curbuf = curwin->w_buffer;
746 block_autocmds();
747
Bram Moolenaardac13472019-09-16 21:06:21 +0200748 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200749 {
750 struct vimoption *p = &(options[i]);
751 char_u *varp = get_varp_scope(p, OPT_LOCAL);
752
753 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200754 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200755 && !(options[i].flags & P_NODEFAULT)
756 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200757 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200758 }
759
760 unblock_autocmds();
761 curwin = save_curwin;
762 curbuf = curwin->w_buffer;
763}
764
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000765#if defined(EXITFREE) || defined(PROTO)
766/*
767 * Free all options.
768 */
769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100770free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000771{
772 int i;
773
Bram Moolenaardac13472019-09-16 21:06:21 +0200774 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000775 {
776 if (options[i].indir == PV_NONE)
777 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100778 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100779 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000780 free_string_option(*(char_u **)options[i].var);
781 if (options[i].flags & P_DEF_ALLOCED)
782 free_string_option(options[i].def_val[VI_DEFAULT]);
783 }
784 else if (options[i].var != VAR_WIN
785 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100786 // buffer-local option: free global value
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000787 free_string_option(*(char_u **)options[i].var);
788 }
789}
790#endif
791
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
794 * Initialize the options, part two: After getting Rows and Columns and
795 * setting 'term'.
796 */
797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100798set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000800 int idx;
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100803 * 'scroll' defaults to half the window height. The stored default is zero,
804 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000806 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000807 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000808 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 comp_col();
810
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000811 /*
812 * 'window' is only for backwards compatibility with Vi.
813 * Default is Rows - 1.
814 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000815 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000816 p_window = Rows - 1;
817 set_number_default("window", Rows - 1);
818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100820#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000821 /*
822 * If 'background' wasn't set by the user, try guessing the value,
823 * depending on the terminal name. Only need to check for terminals
824 * with a dark background, that can handle color.
825 */
826 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000827 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
828 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000830 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 // don't mark it as set, when starting the GUI it may be
832 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000833 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000836
837#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000839#endif
840#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000842#endif
843#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846}
847
848/*
849 * Initialize the options, part three: After reading the .vimrc
850 */
851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100852set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100854#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855/*
856 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
857 * This is done after other initializations, where 'shell' might have been
858 * set, but only if they have not been set before.
859 */
860 char_u *p;
861 int idx_srr;
862 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100863# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 int idx_sp;
865 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000869 if (idx_srr < 0)
870 do_srr = FALSE;
871 else
872 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100873# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000875 if (idx_sp < 0)
876 do_sp = FALSE;
877 else
878 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100879# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200880 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (p != NULL)
882 {
883 /*
884 * Default for p_sp is "| tee", for p_srr is ">".
885 * For known shells it is changed here to include stderr.
886 */
887 if ( fnamecmp(p, "csh") == 0
888 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100889# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 || fnamecmp(p, "csh.exe") == 0
891 || fnamecmp(p, "tcsh.exe") == 0
892# endif
893 )
894 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100895# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (do_sp)
897 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100898# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100900# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
904 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (do_srr)
907 {
908 p_srr = (char_u *)">&";
909 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
910 }
911 }
912 else
Natanael Copa56318362021-05-06 18:46:35 +0200913 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if ( fnamecmp(p, "sh") == 0
915 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200916 || fnamecmp(p, "mksh") == 0
917 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000919 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200921 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200922 || fnamecmp(p, "ash") == 0
923 || fnamecmp(p, "dash") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100924# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 || fnamecmp(p, "cmd") == 0
926 || fnamecmp(p, "sh.exe") == 0
927 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200928 || fnamecmp(p, "mksh.exe") == 0
929 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000931 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 || fnamecmp(p, "bash.exe") == 0
933 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200934 || fnamecmp(p, "dash.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100936 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100938# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (do_sp)
940 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100941# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100943# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (do_srr)
950 {
951 p_srr = (char_u *)">%s 2>&1";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955 vim_free(p);
956 }
957#endif
958
Bram Moolenaar4f974752019-02-17 17:44:42 +0100959#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100961 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
962 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 * This is done after other initializations, where 'shell' might have been
964 * set, but only if they have not been set before. Default for p_shcf is
965 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100966 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000968 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
970 int idx3;
971
972 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000973 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
975 p_shcf = (char_u *)"-c";
976 options[idx3].def_val[VI_DEFAULT] = p_shcf;
977 }
978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100979 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000981 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {
983 p_sxq = (char_u *)"\"";
984 options[idx3].def_val[VI_DEFAULT] = p_sxq;
985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100987 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
988 {
989 int idx3;
990
991 /*
992 * cmd.exe on Windows will strip the first and last double quote given
993 * on the command line, e.g. most of the time things like:
994 * cmd /c "my path/to/echo" "my args to echo"
995 * become:
996 * my path/to/echo" "my args to echo
997 * when executed.
998 *
Bram Moolenaar034b1152012-02-19 18:19:30 +0100999 * To avoid this, set shellxquote to surround the command in
1000 * parenthesis. This appears to make most commands work, without
1001 * breaking commands that worked previously, such as
1002 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001003 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001004 idx3 = findoption((char_u *)"sxq");
1005 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1006 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001007 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001008 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1009 }
1010
Bram Moolenaara64ba222012-02-12 23:23:31 +01001011 idx3 = findoption((char_u *)"shcf");
1012 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1013 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001014 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001015 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1016 }
1017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#endif
1019
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001020 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001021 {
1022 int idx_ffs = findoption((char_u *)"ffs");
1023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001024 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001025 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1026 set_fileformat(default_fileformat(), OPT_LOCAL);
1027 }
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef FEAT_TITLE
1030 set_title_defaults();
1031#endif
1032}
1033
1034#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1035/*
1036 * When 'helplang' is still at its default value, set it to "lang".
1037 * Only the first two characters of "lang" are used.
1038 */
1039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001040set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 int idx;
1043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001044 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 return;
1046 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001047 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 if (options[idx].flags & P_ALLOCED)
1050 free_string_option(p_hlg);
1051 p_hlg = vim_strsave(lang);
1052 if (p_hlg == NULL)
1053 p_hlg = empty_option;
1054 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001055 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001056 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001057 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1058 {
1059 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1060 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1061 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001062 // any C like setting, such as C.UTF-8, becomes "en"
1063 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1064 {
1065 p_hlg[0] = 'e';
1066 p_hlg[1] = 'n';
1067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 options[idx].flags |= P_ALLOCED;
1071 }
1072}
1073#endif
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#ifdef FEAT_TITLE
1076/*
1077 * 'title' and 'icon' only default to true if they have not been set or reset
1078 * in .vimrc and we can read the old value.
1079 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1080 * they can be reset. This reduces startup time when using X on a remote
1081 * machine.
1082 */
1083 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001084set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 int idx1;
1087 long val;
1088
1089 /*
1090 * If GUI is (going to be) used, we can always set the window title and
1091 * icon name. Saves a bit of time, because the X11 display server does
1092 * not need to be contacted.
1093 */
1094 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001095 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
1097#ifdef FEAT_GUI
1098 if (gui.starting || gui.in_use)
1099 val = TRUE;
1100 else
1101#endif
1102 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001103 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 p_title = val;
1105 }
1106 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001107 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109#ifdef FEAT_GUI
1110 if (gui.starting || gui.in_use)
1111 val = TRUE;
1112 else
1113#endif
1114 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001115 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 p_icon = val;
1117 }
1118}
1119#endif
1120
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001121 void
1122ex_set(exarg_T *eap)
1123{
1124 int flags = 0;
1125
1126 if (eap->cmdidx == CMD_setlocal)
1127 flags = OPT_LOCAL;
1128 else if (eap->cmdidx == CMD_setglobal)
1129 flags = OPT_GLOBAL;
1130#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001131 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001132 ex_options(eap);
1133 else
1134#endif
1135 {
1136 if (eap->forceit)
1137 flags |= OPT_ONECOLUMN;
1138 (void)do_set(eap->arg, flags);
1139 }
1140}
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142/*
1143 * Parse 'arg' for option settings.
1144 *
1145 * 'arg' may be IObuff, but only when no errors can be present and option
1146 * does not need to be expanded with option_expand().
1147 * "opt_flags":
1148 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001149 * OPT_GLOBAL for ":setglobal"
1150 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001152 * OPT_WINONLY to only set window-local options
1153 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 *
1155 * returns FAIL if an error is detected, OK otherwise
1156 */
1157 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001158do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001159 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001160 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161{
1162 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001163 char *errmsg;
1164 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1167 int nextchar; // next non-white char after option name
1168 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int len;
1170 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001171 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001173 long_u flags; // flags for current option
1174 char_u *varp = NULL; // pointer to variable for current option
1175 int did_show = FALSE; // already showed one value
1176 int adding; // "opt+=arg"
1177 int prepending; // "opt^=arg"
1178 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 int cp_val = 0;
1180 char_u key_name[2];
1181
1182 if (*arg == NUL)
1183 {
1184 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001185 did_show = TRUE;
1186 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 }
1188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
1191 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001192 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001194 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1195 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
1197 /*
1198 * ":set all" show all options.
1199 * ":set all&" set all options to their default value.
1200 */
1201 arg += 3;
1202 if (*arg == '&')
1203 {
1204 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001205 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001207 didset_options();
1208 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001209 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001214 did_show = TRUE;
1215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001217 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 showoptions(2, opt_flags);
1220 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001221 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 arg += 7;
1223 }
1224 else
1225 {
1226 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001227 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
1229 prefix = 0;
1230 arg += 2;
1231 }
1232 else if (STRNCMP(arg, "inv", 3) == 0)
1233 {
1234 prefix = 2;
1235 arg += 3;
1236 }
1237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001238 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 key = 0;
1240 if (*arg == '<')
1241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001243 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1245 len = 5;
1246 else
1247 {
1248 len = 1;
1249 while (arg[len] != NUL && arg[len] != '>')
1250 ++len;
1251 }
1252 if (arg[len] != '>')
1253 {
1254 errmsg = e_invarg;
1255 goto skip;
1256 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 arg[len] = NUL; // put NUL after name
1258 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001260 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001262 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264 else
1265 {
1266 len = 0;
1267 /*
1268 * The two characters after "t_" may not be alphanumeric.
1269 */
1270 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1271 len = 4;
1272 else
1273 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1274 ++len;
1275 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001276 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001278 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001280 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 afterchar = arg[len];
1285
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001286 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001287 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 ++len;
1289
1290 adding = FALSE;
1291 prepending = FALSE;
1292 removing = FALSE;
1293 if (arg[len] != NUL && arg[len + 1] == '=')
1294 {
1295 if (arg[len] == '+')
1296 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001297 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 ++len;
1299 }
1300 else if (arg[len] == '^')
1301 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001302 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 ++len;
1304 }
1305 else if (arg[len] == '-')
1306 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001307 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ++len;
1309 }
1310 }
1311 nextchar = arg[len];
1312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001313 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001315 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 goto skip;
1317 }
1318
1319 if (opt_idx >= 0)
1320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001321 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001323 // Only give an error message when requesting the value of
1324 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1326 && (!(options[opt_idx].flags & P_BOOL)
1327 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001328 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 goto skip;
1330 }
1331
1332 flags = options[opt_idx].flags;
1333 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1334 }
1335 else
1336 {
1337 flags = P_STRING;
1338 if (key < 0)
1339 {
1340 key_name[0] = KEY2TERMCAP0(key);
1341 key_name[1] = KEY2TERMCAP1(key);
1342 }
1343 else
1344 {
1345 key_name[0] = KS_KEY;
1346 key_name[1] = (key & 0xff);
1347 }
1348 }
1349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 // Skip all options that are not window-local (used when showing
1351 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001352 if ((opt_flags & OPT_WINONLY)
1353 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1354 goto skip;
1355
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001356 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001357 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1358 && options[opt_idx].var == VAR_WIN)
1359 goto skip;
1360
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001361 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001362 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001364 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001365 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001366 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001367 goto skip;
1368 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001369 if ((flags & P_MLE) && !p_mle)
1370 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001371 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001372 goto skip;
1373 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001374#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001375 // In diff mode some options are overruled. This avoids that
1376 // 'foldmethod' becomes "marker" instead of "diff" and that
1377 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001378 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001379 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001380 && (
1381#ifdef FEAT_FOLDING
1382 options[opt_idx].indir == PV_FDM ||
1383#endif
1384 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001385 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001393 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 goto skip;
1395 }
1396#endif
1397
1398 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1399 {
1400 arg += len;
1401 cp_val = p_cp;
1402 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1403 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001404 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 {
1406 cp_val = FALSE;
1407 arg += 3;
1408 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001409 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
1411 cp_val = TRUE;
1412 arg += 2;
1413 }
1414 }
1415 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001416 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
1418 errmsg = e_trailing;
1419 goto skip;
1420 }
1421 }
1422
1423 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001424 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001425 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 */
1427 if (nextchar == '?'
1428 || (prefix == 1
1429 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1430 && !(flags & P_BOOL)))
1431 {
1432 /*
1433 * print value
1434 */
1435 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else
1438 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001439 gotocmdline(TRUE); // cursor at status line
1440 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442 if (opt_idx >= 0)
1443 {
1444 showoneopt(&options[opt_idx], opt_flags);
1445#ifdef FEAT_EVAL
1446 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001448 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001449 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001450 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001451 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001452 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001453 (int)options[opt_idx].indir & PV_MASK]);
1454 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001455 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001456 (int)options[opt_idx].indir & PV_MASK]);
1457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#endif
1459 }
1460 else
1461 {
1462 char_u *p;
1463
1464 p = find_termcode(key_name);
1465 if (p == NULL)
1466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001467 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 goto skip;
1469 }
1470 else
1471 (void)show_one_termcode(key_name, p, TRUE);
1472 }
1473 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001474 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 errmsg = e_trailing;
1476 }
1477 else
1478 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001479 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001480 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001482 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 if (nextchar == '=' || nextchar == ':')
1485 {
1486 errmsg = e_invarg;
1487 goto skip;
1488 }
1489
1490 /*
1491 * ":set opt!": invert
1492 * ":set opt&": reset to default value
1493 * ":set opt<": reset to global value
1494 */
1495 if (nextchar == '!')
1496 value = *(int *)(varp) ^ 1;
1497 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001498 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 ((flags & P_VI_DEF) || cp_val)
1500 ? VI_DEFAULT : VIM_DEFAULT];
1501 else if (nextchar == '<')
1502 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001503 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if ((int *)varp == &curbuf->b_p_ar
1505 && opt_flags == OPT_LOCAL)
1506 value = -1;
1507 else
1508 value = *(int *)get_varp_scope(&(options[opt_idx]),
1509 OPT_GLOBAL);
1510 }
1511 else
1512 {
1513 /*
1514 * ":set invopt": invert
1515 * ":set opt" or ":set noopt": set or reset
1516 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001517 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 errmsg = e_trailing;
1520 goto skip;
1521 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001522 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 value = *(int *)(varp) ^ 1;
1524 else
1525 value = prefix;
1526 }
1527
1528 errmsg = set_bool_option(opt_idx, varp, (int)value,
1529 opt_flags);
1530 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1534 || prefix != 1)
1535 {
1536 errmsg = e_invarg;
1537 goto skip;
1538 }
1539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001540 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 {
1542 /*
1543 * Different ways to set a number option:
1544 * & set to default value
1545 * < set to global value
1546 * <xx> accept special key codes for 'wildchar'
1547 * c accept any non-digit for 'wildchar'
1548 * [-]0-9 set number
1549 * other error
1550 */
1551 ++arg;
1552 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001553 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 ((flags & P_VI_DEF) || cp_val)
1555 ? VI_DEFAULT : VIM_DEFAULT];
1556 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001557 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001558 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1559 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001560 if ((long *)varp == &curbuf->b_p_ul
1561 && opt_flags == OPT_LOCAL)
1562 value = NO_LOCAL_UNDOLEVEL;
1563 else
1564 value = *(long *)get_varp_scope(
1565 &(options[opt_idx]), OPT_GLOBAL);
1566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 else if (((long *)varp == &p_wc
1568 || (long *)varp == &p_wcm)
1569 && (*arg == '<'
1570 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001571 || (*arg != NUL
1572 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 && !VIM_ISDIGIT(*arg))))
1574 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001575 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (value == 0 && (long *)varp != &p_wcm)
1577 {
1578 errmsg = e_invarg;
1579 goto skip;
1580 }
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1583 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001584 // Allow negative (for 'undolevels'), octal and
1585 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001586 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001587 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001588 if (i == 0 || (arg[i] != NUL
1589 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001591 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 goto skip;
1593 }
1594 }
1595 else
1596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001597 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 goto skip;
1599 }
1600
1601 if (adding)
1602 value = *(long *)varp + value;
1603 if (prepending)
1604 value = *(long *)varp * value;
1605 if (removing)
1606 value = *(long *)varp - value;
1607 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001608 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001612 char_u *save_arg = NULL;
1613 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001615 char_u *newval;
1616 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001617 char_u *origval_l = NULL;
1618 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001619#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001620 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001621 char_u *saved_origval_l = NULL;
1622 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001623 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001624#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001625 unsigned newlen;
1626 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 int new_value_alloced; // new string option
1628 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001630 // When using ":set opt=val" for a global option
1631 // with a local value the local value will be
1632 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001634 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 varp = options[opt_idx].var;
1636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001637 // The old value is kept until we are sure that the
1638 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001640
Bram Moolenaard7c96872019-06-15 17:12:48 +02001641 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1642 {
1643 origval_l = *(char_u **)get_varp_scope(
1644 &(options[opt_idx]), OPT_LOCAL);
1645 origval_g = *(char_u **)get_varp_scope(
1646 &(options[opt_idx]), OPT_GLOBAL);
1647
1648 // A global-local string option might have an empty
1649 // option as value to indicate that the global
1650 // value should be used.
1651 if (((int)options[opt_idx].indir & PV_BOTH)
1652 && origval_l == empty_option)
1653 origval_l = origval_g;
1654 }
1655
1656 // When setting the local value of a global
1657 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001658 if (((int)options[opt_idx].indir & PV_BOTH)
1659 && (opt_flags & OPT_LOCAL))
1660 origval = *(char_u **)get_varp(
1661 &options[opt_idx]);
1662 else
1663 origval = oldval;
1664
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001665 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
1667 newval = options[opt_idx].def_val[
1668 ((flags & P_VI_DEF) || cp_val)
1669 ? VI_DEFAULT : VIM_DEFAULT];
1670 if ((char_u **)varp == &p_bg)
1671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001672 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673#ifdef FEAT_GUI
1674 if (gui.in_use)
1675 newval = gui_bg_default();
1676 else
1677#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001678 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
1680
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001681 // expand environment variables and ~ (since the
1682 // default value was already expanded, only
1683 // required when an environment variable was set
1684 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 if (newval == NULL)
1686 newval = empty_option;
1687 else
1688 {
1689 s = option_expand(opt_idx, newval);
1690 if (s == NULL)
1691 s = newval;
1692 newval = vim_strsave(s);
1693 }
1694 new_value_alloced = TRUE;
1695 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001696 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
1698 newval = vim_strsave(*(char_u **)get_varp_scope(
1699 &(options[opt_idx]), OPT_GLOBAL));
1700 new_value_alloced = TRUE;
1701 }
1702 else
1703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001704 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706 /*
1707 * Set 'keywordprg' to ":help" if an empty
1708 * value was passed to :set by the user.
1709 * Misuse errbuf[] for the resulting string.
1710 */
1711 if (varp == (char_u *)&p_kp
1712 && (*arg == NUL || *arg == ' '))
1713 {
1714 STRCPY(errbuf, ":help");
1715 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001716 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
1718 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001719 * Convert 'backspace' number to string, for
1720 * adding, prepending and removing string.
1721 */
1722 else if (varp == (char_u *)&p_bs
1723 && VIM_ISDIGIT(**(char_u **)varp))
1724 {
1725 i = getdigits((char_u **)varp);
1726 switch (i)
1727 {
1728 case 0:
1729 *(char_u **)varp = empty_option;
1730 break;
1731 case 1:
1732 *(char_u **)varp = vim_strsave(
1733 (char_u *)"indent,eol");
1734 break;
1735 case 2:
1736 *(char_u **)varp = vim_strsave(
1737 (char_u *)"indent,eol,start");
1738 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001739 case 3:
1740 *(char_u **)varp = vim_strsave(
1741 (char_u *)"indent,eol,nostop");
1742 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001743 }
1744 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001745 if (origval == oldval)
1746 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001747 if (origval_l == oldval)
1748 origval_l = *(char_u **)varp;
1749 if (origval_g == oldval)
1750 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001751 oldval = *(char_u **)varp;
1752 }
1753 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 * Convert 'whichwrap' number to string, for
1755 * backwards compatibility with Vim 3.0.
1756 * Misuse errbuf[] for the resulting string.
1757 */
1758 else if (varp == (char_u *)&p_ww
1759 && VIM_ISDIGIT(*arg))
1760 {
1761 *errbuf = NUL;
1762 i = getdigits(&arg);
1763 if (i & 1)
1764 STRCAT(errbuf, "b,");
1765 if (i & 2)
1766 STRCAT(errbuf, "s,");
1767 if (i & 4)
1768 STRCAT(errbuf, "h,l,");
1769 if (i & 8)
1770 STRCAT(errbuf, "<,>,");
1771 if (i & 16)
1772 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001773 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 errbuf[STRLEN(errbuf) - 1] = NUL;
1775 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001776 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 }
1778 /*
1779 * Remove '>' before 'dir' and 'bdir', for
1780 * backwards compatibility with version 3.0
1781 */
1782 else if ( *arg == '>'
1783 && (varp == (char_u *)&p_dir
1784 || varp == (char_u *)&p_bdir))
1785 {
1786 ++arg;
1787 }
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 /*
1790 * Copy the new string into allocated memory.
1791 * Can't use set_string_option_direct(), because
1792 * we need to remove the backslashes.
1793 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001794 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 newlen = (unsigned)STRLEN(arg) + 1;
1796 if (adding || prepending || removing)
1797 newlen += (unsigned)STRLEN(origval) + 1;
1798 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 break;
1801 s = newval;
1802
1803 /*
1804 * Copy the string, skip over escaped chars.
1805 * For MS-DOS and WIN32 backslashes before normal
1806 * file name characters are not removed, and keep
1807 * backslash at start, for "\\machine\path", but
1808 * do remove it for "\\\\machine\\path".
1809 * The reverse is found in ExpandOldSetting().
1810 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001811 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813 if (*arg == '\\' && arg[1] != NUL
1814#ifdef BACKSLASH_IN_FILENAME
1815 && !((flags & P_EXPAND)
1816 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001817 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 && (arg[1] != '\\'
1819 || (s == newval
1820 && arg[2] != '\\')))
1821#endif
1822 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001823 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001825 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001827 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 mch_memmove(s, arg, (size_t)i);
1829 arg += i;
1830 s += i;
1831 }
1832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 *s++ = *arg++;
1834 }
1835 *s = NUL;
1836
1837 /*
1838 * Expand environment variables and ~.
1839 * Don't do it when adding without inserting a
1840 * comma.
1841 */
1842 if (!(adding || prepending || removing)
1843 || (flags & P_COMMA))
1844 {
1845 s = option_expand(opt_idx, newval);
1846 if (s != NULL)
1847 {
1848 vim_free(newval);
1849 newlen = (unsigned)STRLEN(s) + 1;
1850 if (adding || prepending || removing)
1851 newlen += (unsigned)STRLEN(origval) + 1;
1852 newval = alloc(newlen);
1853 if (newval == NULL)
1854 break;
1855 STRCPY(newval, s);
1856 }
1857 }
1858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001859 // locate newval[] in origval[] when removing it
1860 // and when adding to avoid duplicates
1861 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (removing || (flags & P_NODUP))
1863 {
1864 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001865 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001867 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001868 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 prepending = FALSE;
1871 adding = FALSE;
1872 STRCPY(newval, origval);
1873 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001874
1875 // if no duplicate, move pointer to end of
1876 // original value
1877 if (s == NULL)
1878 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001881 // concatenate the two strings; add a ',' if
1882 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (adding || prepending)
1884 {
1885 comma = ((flags & P_COMMA) && *origval != NUL
1886 && *newval != NUL);
1887 if (adding)
1888 {
1889 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001890 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001891 if (comma && i > 1
1892 && (flags & P_ONECOMMA) == P_ONECOMMA
1893 && origval[i - 1] == ','
1894 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001895 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 mch_memmove(newval + i + comma, newval,
1897 STRLEN(newval) + 1);
1898 mch_memmove(newval, origval, (size_t)i);
1899 }
1900 else
1901 {
1902 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001903 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 if (comma)
1906 newval[i] = ',';
1907 }
1908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001909 // Remove newval[] from origval[]. (Note: "i" has
1910 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (removing)
1912 {
1913 STRCPY(newval, origval);
1914 if (*s)
1915 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001916 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (flags & P_COMMA)
1918 {
1919 if (s == origval)
1920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001921 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (s[i] == ',')
1923 ++i;
1924 }
1925 else
1926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001927 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 --s;
1929 ++i;
1930 }
1931 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001932 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 }
1935
1936 if (flags & P_FLAGLIST)
1937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001938 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001939 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001940 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001941 // if options have P_FLAGLIST and
1942 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001943 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001945 if (*s != ',' && *(s + 1) == ','
1946 && vim_strchr(s + 2, *s) != NULL)
1947 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001948 // Remove the duplicated value and
1949 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001950 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001951 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001954 else
1955 {
1956 if ((!(flags & P_COMMA) || *s != ',')
1957 && vim_strchr(s + 1, *s) != NULL)
1958 {
1959 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001960 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001961 }
1962 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001963 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001967 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 arg = save_arg;
1969 new_value_alloced = TRUE;
1970 }
1971
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001972 /*
1973 * Set the new value.
1974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 *(char_u **)(varp) = newval;
1976
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001977#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001978 if (!starting
1979# ifdef FEAT_CRYPT
1980 && options[opt_idx].indir != PV_KEY
1981# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001982 && origval != NULL && newval != NULL)
1983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // origval may be freed by
1985 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02001986 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001987 // newval (and varp) may become invalid if the
1988 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001989 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001990 if (origval_l != NULL)
1991 saved_origval_l = vim_strsave(origval_l);
1992 if (origval_g != NULL)
1993 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001994 }
Bram Moolenaar53744302015-07-17 17:38:22 +02001995#endif
1996
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001997 {
1998 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001999 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002000
2001 // When an option is set in the sandbox, from a
2002 // modeline or in secure mode, then deal with side
2003 // effects in secure mode. Also when the value was
2004 // set with the P_INSECURE flag and is not
2005 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002006 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002007#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002008 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002009#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002010 || (!value_is_replaced && (*p & P_INSECURE)))
2011 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002012
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002013 // Handle side effects, and set the global value
2014 // for ":set" on local options. Note: when setting
2015 // 'syntax' or 'filetype' autocommands may be
2016 // triggered that can cause havoc.
2017 errmsg = did_set_string_option(
2018 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002019 new_value_alloced, oldval, errbuf,
2020 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002021
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002022 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002025#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002026 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002027 trigger_optionsset_string(
2028 opt_idx, opt_flags, saved_origval,
2029 saved_origval_l, saved_origval_g,
2030 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002031 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002032 vim_free(saved_origval_l);
2033 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002034 vim_free(saved_newval);
2035#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (errmsg != NULL)
2038 goto skip;
2039 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002040 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 char_u *p;
2043
2044 if (nextchar == '&')
2045 {
2046 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002047 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 else
2050 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002051 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002052 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 if (*p == '\\' && p[1] != NUL)
2054 ++p;
2055 nextchar = *p;
2056 *p = NUL;
2057 add_termcode(key_name, arg, FALSE);
2058 *p = nextchar;
2059 }
2060 if (full_screen)
2061 ttest(FALSE);
2062 redraw_all_later(CLEAR);
2063 }
2064 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002067 did_set_option(
2068 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070
2071skip:
2072 /*
2073 * Advance to next argument.
2074 * - skip until a blank found, taking care of backslashes
2075 * - skip blanks
2076 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2077 */
2078 for (i = 0; i < 2 ; ++i)
2079 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002080 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (*arg++ == '\\' && *arg != NUL)
2082 ++arg;
2083 arg = skipwhite(arg);
2084 if (*arg != '=')
2085 break;
2086 }
2087 }
2088
2089 if (errmsg != NULL)
2090 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002091 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002092 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if (i + (arg - startarg) < IOSIZE)
2094 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002095 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 STRCAT(IObuff, ": ");
2097 mch_memmove(IObuff + i, startarg, (arg - startarg));
2098 IObuff[i + (arg - startarg)] = NUL;
2099 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002100 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 trans_characters(IObuff, IOSIZE);
2102
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 ++no_wait_return; // wait_return done later
2104 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 --no_wait_return;
2106
2107 return FAIL;
2108 }
2109
2110 arg = skipwhite(arg);
2111 }
2112
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002113theend:
2114 if (silent_mode && did_show)
2115 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002116 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002117 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002118 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002119 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002120 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002121 out_flush();
2122 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002123 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002124 }
2125
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 return OK;
2127}
2128
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002129/*
2130 * Call this when an option has been given a new value through a user command.
2131 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2132 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134did_set_option(
2135 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002136 int opt_flags, // possibly with OPT_MODELINE
2137 int new_value, // value was replaced completely
2138 int value_checked) // value was checked to be safe, no need to set the
2139 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002140{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002141 long_u *p;
2142
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002143 options[opt_idx].flags |= P_WAS_SET;
2144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 // When an option is set in the sandbox, from a modeline or in secure mode
2146 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2147 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002148 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002149 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002150#ifdef HAVE_SANDBOX
2151 || sandbox != 0
2152#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002153 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002154 *p = *p | P_INSECURE;
2155 else if (new_value)
2156 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002157}
2158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159/*
2160 * Convert a key name or string into a key value.
2161 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002162 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002164 int
2165string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002168 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (*arg == '^')
2170 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002171 if (multi_byte)
2172 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 return *arg;
2174}
2175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176#ifdef FEAT_TITLE
2177/*
2178 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2179 * maketitle() to create and display it.
2180 * When switching the title or icon off, call mch_restore_title() to get
2181 * the old value back.
2182 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002183 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002184did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 if (starting != NO_SCREEN
2187#ifdef FEAT_GUI
2188 && !gui.starting
2189#endif
2190 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192}
2193#endif
2194
2195/*
2196 * set_options_bin - called when 'bin' changes value.
2197 */
2198 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002199set_options_bin(
2200 int oldval,
2201 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 /*
2205 * The option values that are changed when 'bin' changes are
2206 * copied when 'bin is set and restored when 'bin' is reset.
2207 */
2208 if (newval)
2209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002210 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 if (!(opt_flags & OPT_GLOBAL))
2213 {
2214 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2215 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2216 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2217 curbuf->b_p_et_nobin = curbuf->b_p_et;
2218 }
2219 if (!(opt_flags & OPT_LOCAL))
2220 {
2221 p_tw_nobin = p_tw;
2222 p_wm_nobin = p_wm;
2223 p_ml_nobin = p_ml;
2224 p_et_nobin = p_et;
2225 }
2226 }
2227
2228 if (!(opt_flags & OPT_GLOBAL))
2229 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002230 curbuf->b_p_tw = 0; // no automatic line wrap
2231 curbuf->b_p_wm = 0; // no automatic line wrap
2232 curbuf->b_p_ml = 0; // no modelines
2233 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235 if (!(opt_flags & OPT_LOCAL))
2236 {
2237 p_tw = 0;
2238 p_wm = 0;
2239 p_ml = FALSE;
2240 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 if (!(opt_flags & OPT_GLOBAL))
2247 {
2248 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2249 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2250 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2251 curbuf->b_p_et = curbuf->b_p_et_nobin;
2252 }
2253 if (!(opt_flags & OPT_LOCAL))
2254 {
2255 p_tw = p_tw_nobin;
2256 p_wm = p_wm_nobin;
2257 p_ml = p_ml_nobin;
2258 p_et = p_et_nobin;
2259 }
2260 }
2261}
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263/*
2264 * Expand environment variables for some string options.
2265 * These string options cannot be indirect!
2266 * If "val" is NULL expand the current value of the option.
2267 * Return pointer to NameBuff, or NULL when not expanded.
2268 */
2269 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002270option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002272 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2274 return NULL;
2275
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002276 // If val is longer than MAXPATHL no meaningful expansion can be done,
2277 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (val != NULL && STRLEN(val) > MAXPATHL)
2279 return NULL;
2280
2281 if (val == NULL)
2282 val = *(char_u **)options[opt_idx].var;
2283
2284 /*
2285 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2286 * Escape spaces when expanding 'tags', they are used to separate file
2287 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002288 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 */
2290 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002291 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002292#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002293 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2294#endif
2295 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002296 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 return NULL;
2298
2299 return NameBuff;
2300}
2301
2302/*
2303 * After setting various option values: recompute variables that depend on
2304 * option values.
2305 */
2306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002307didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002309 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 (void)init_chartab();
2311
Bram Moolenaardac13472019-09-16 21:06:21 +02002312 didset_string_options();
2313
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002314#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002315 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002316 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002317 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002318 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002321 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 (void)check_cedit();
2323#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002324#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002325 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002326 fill_breakat_flags();
2327#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002328 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002329}
2330
2331/*
2332 * More side effects of setting options.
2333 */
2334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002335didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002336{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002338 (void)highlight_changed();
2339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002341 check_opt_wim();
2342
Bram Moolenaareed9d462021-02-15 20:38:25 +01002343 // Parse default for 'listchars'.
2344 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2345
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002346 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002347 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002348
2349#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002350 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002351 (void)check_clipboard_option();
2352#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002353#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002354 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002355 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002356 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002357 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359}
2360
2361/*
2362 * Check for string options that are NULL (normally only termcap options).
2363 */
2364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002365check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 int opt_idx;
2368
2369 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2370 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2371 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2372}
2373
2374/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002375 * Return the option index found by a pointer into term_strings[].
2376 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002378 int
2379get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002381 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2384 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002385 return opt_idx;
2386 return -1; // cannot happen: didn't find it!
2387}
2388
2389/*
2390 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2391 * Return the option index or -1 if not found.
2392 */
2393 int
2394set_term_option_alloced(char_u **p)
2395{
2396 int opt_idx = get_term_opt_idx(p);
2397
2398 if (opt_idx >= 0)
2399 options[opt_idx].flags |= P_ALLOCED;
2400 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401}
2402
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002403#if defined(FEAT_EVAL) || defined(PROTO)
2404/*
2405 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2406 * Return FALSE when it wasn't.
2407 * Return -1 for an unknown option.
2408 */
2409 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002410was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002411{
2412 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002413 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002414
2415 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002416 {
2417 flagp = insecure_flag(idx, opt_flags);
2418 return (*flagp & P_INSECURE) != 0;
2419 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002420 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002421 return -1;
2422}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002423
2424/*
2425 * Get a pointer to the flags used for the P_INSECURE flag of option
2426 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002427 * NOTE: Caller must make sure that "curwin" is set to the window from which
2428 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002429 */
2430 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002431insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002432{
2433 if (opt_flags & OPT_LOCAL)
2434 switch ((int)options[opt_idx].indir)
2435 {
2436#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002437 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002438#endif
2439#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002440# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002441 case PV_FDE: return &curwin->w_p_fde_flags;
2442 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002443# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002444# ifdef FEAT_BEVAL
2445 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2446# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002447# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002448 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002449# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002450 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002451# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002452 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002453# endif
2454#endif
2455 }
2456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002457 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002458 return &options[opt_idx].flags;
2459}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002460#endif
2461
Bram Moolenaardac13472019-09-16 21:06:21 +02002462#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002463/*
2464 * Redraw the window title and/or tab page text later.
2465 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002466void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002467{
2468 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002469 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002470}
2471#endif
2472
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002474 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2475 * characters or characters in "allowed".
2476 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002477 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002478valid_name(char_u *val, char *allowed)
2479{
2480 char_u *s;
2481
2482 for (s = val; *s != NUL; ++s)
2483 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2484 return FALSE;
2485 return TRUE;
2486}
2487
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002488#if defined(FEAT_EVAL) || defined(PROTO)
2489/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002490 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002491 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002492 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002493 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002494set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002495{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002496 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2497 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002498 sctx_T new_script_ctx = script_ctx;
2499
Bram Moolenaar51258742020-05-03 17:19:33 +02002500 // Modeline already has the line number set.
2501 if (!(opt_flags & OPT_MODELINE))
2502 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002503
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002504 // Remember where the option was set. For local options need to do that
2505 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002506 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002507 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002508 if (both || (opt_flags & OPT_LOCAL))
2509 {
2510 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002511 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002512 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002513 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002514 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002516
2517/*
2518 * Set the script_ctx for a termcap option.
2519 * "name" must be the two character code, e.g. "RV".
2520 * When "name" is NULL use "opt_idx".
2521 */
2522 void
2523set_term_option_sctx_idx(char *name, int opt_idx)
2524{
2525 char_u buf[5];
2526 int idx;
2527
2528 if (name == NULL)
2529 idx = opt_idx;
2530 else
2531 {
2532 buf[0] = 't';
2533 buf[1] = '_';
2534 buf[2] = name[0];
2535 buf[3] = name[1];
2536 buf[4] = 0;
2537 idx = findoption(buf);
2538 }
2539 if (idx >= 0)
2540 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2541}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542#endif
2543
Bram Moolenaarf4140482020-02-15 23:06:45 +01002544#if defined(FEAT_EVAL)
2545/*
2546 * Apply the OptionSet autocommand.
2547 */
2548 static void
2549apply_optionset_autocmd(
2550 int opt_idx,
2551 long opt_flags,
2552 long oldval,
2553 long oldval_g,
2554 long newval,
2555 char *errmsg)
2556{
2557 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2558
2559 // Don't do this while starting up, failure or recursively.
2560 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2561 return;
2562
2563 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2564 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2565 oldval_g);
2566 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2567 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2568 (opt_flags & OPT_LOCAL) ? "local" : "global");
2569 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2570 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2571 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2572 if (opt_flags & OPT_LOCAL)
2573 {
2574 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2575 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2576 }
2577 if (opt_flags & OPT_GLOBAL)
2578 {
2579 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2580 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2581 }
2582 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2583 {
2584 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2585 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2586 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2587 }
2588 if (opt_flags & OPT_MODELINE)
2589 {
2590 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2591 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2592 }
2593 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2594 NULL, FALSE, NULL);
2595 reset_v_option_vars();
2596}
2597#endif
2598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599/*
2600 * Set the value of a boolean option, and take care of side effects.
2601 * Returns NULL for success, or an error message for an error.
2602 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002603 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002604set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002605 int opt_idx, // index in options[] table
2606 char_u *varp, // pointer to the option variable
2607 int value, // new value
2608 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002611#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002612 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002615 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 if ((secure
2617#ifdef HAVE_SANDBOX
2618 || sandbox != 0
2619#endif
2620 ) && (options[opt_idx].flags & P_SECURE))
2621 return e_secure;
2622
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002623#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002624 // Save the global value before changing anything. This is needed as for
2625 // a global-only option setting the "local value" in fact sets the global
2626 // value (since there is only one value).
2627 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2628 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2629 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002630#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002632 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002634 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002635 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#endif
2637
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002638#ifdef FEAT_GUI
2639 need_mouse_correct = TRUE;
2640#endif
2641
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002642 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2644 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2645
2646 /*
2647 * Handle side effects of changing a bool option.
2648 */
2649
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002650 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaar920694c2016-08-21 17:45:02 +02002654#ifdef FEAT_LANGMAP
2655 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002656 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002657 p_lnr = !p_lrm;
2658 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002660 p_lrm = !p_lnr;
2661#endif
2662
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002663#ifdef FEAT_SYN_HL
2664 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2665 reset_cursorline();
2666#endif
2667
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002668#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002669 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002670 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002672 // Only take action when the option was set. When reset we do not
2673 // delete the undo file, the option may be set again without making
2674 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002675 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002676 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002677 char_u hash[UNDO_HASH_SIZE];
2678 buf_T *save_curbuf = curbuf;
2679
Bram Moolenaar29323592016-07-24 22:04:11 +02002680 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002681 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002682 // When 'undofile' is set globally: for every buffer, otherwise
2683 // only for the current buffer: Try to read in the undofile,
2684 // if one exists, the buffer wasn't changed and the buffer was
2685 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002686 if ((curbuf == save_curbuf
2687 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2688 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2689 {
2690 u_compute_hash(hash);
2691 u_read_undo(NULL, hash, curbuf->b_fname);
2692 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002693 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002694 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002695 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002696 }
2697#endif
2698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 else if ((int *)varp == &curbuf->b_p_ro)
2700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2703 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002704
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002706 if (curbuf->b_p_ro)
2707 curbuf->b_did_warn = FALSE;
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002710 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#endif
2712 }
2713
Bram Moolenaar9c449722010-07-20 18:44:27 +02002714#ifdef FEAT_GUI
2715 else if ((int *)varp == &p_mh)
2716 {
2717 if (!p_mh)
2718 gui_mch_mousehide(FALSE);
2719 }
2720#endif
2721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002724 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002725# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002727 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2728 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002729 {
2730 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002731 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002732 }
2733# endif
2734# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002735 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002736# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002737 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002738#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002741 {
2742 redraw_titles();
2743 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002745 else if ((int *)varp == &curbuf->b_p_fixeol)
2746 {
2747 redraw_titles();
2748 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002750 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002751 {
2752 redraw_titles();
2753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#endif
2755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 else if ((int *)varp == &curbuf->b_p_bin)
2758 {
2759 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2760#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002761 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762#endif
2763 }
2764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2767 {
2768 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2769 NULL, NULL, TRUE, curbuf);
2770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else if ((int *)varp == &curbuf->b_p_swf)
2774 {
2775 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002776 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2779 // buf->b_p_swf
2780 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
2782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else if ((int *)varp == &p_terse)
2785 {
2786 char_u *p;
2787
2788 p = vim_strchr(p_shm, SHM_SEARCH);
2789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (p_terse && p == NULL)
2792 {
2793 STRCPY(IObuff, p_shm);
2794 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002795 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002799 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002802 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else if ((int *)varp == &p_paste)
2804 {
2805 paste_option_changed();
2806 }
2807
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002808 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 else if ((int *)varp == &p_im)
2810 {
2811 if (p_im)
2812 {
2813 if ((State & INSERT) == 0)
2814 need_start_insertmode = TRUE;
2815 stop_insert_mode = FALSE;
2816 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002817 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002818 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
2820 need_start_insertmode = FALSE;
2821 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002822 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 restart_edit = 0;
2825 }
2826 }
2827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 else if ((int *)varp == &p_ic && p_hls)
2830 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002831 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833
2834#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 else if ((int *)varp == &p_hls)
2837 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002838 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840#endif
2841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2843 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 else if ((int *)varp == &curwin->w_p_scb)
2845 {
2846 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002849 curwin->w_scbind_pos = curwin->w_topline;
2850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar4033c552017-09-16 20:54:51 +02002853#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 else if ((int *)varp == &curwin->w_p_pvw)
2856 {
2857 if (curwin->w_p_pvw)
2858 {
2859 win_T *win;
2860
Bram Moolenaar29323592016-07-24 22:04:11 +02002861 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 if (win->w_p_pvw && win != curwin)
2863 {
2864 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002865 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 }
2868 }
2869#endif
2870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 else if ((int *)varp == &curbuf->b_p_tx)
2873 {
2874 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2875 }
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 set_string_option_direct((char_u *)"ffs", -1,
2881 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002882 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
2885 /*
2886 * When 'lisp' option changes include/exclude '-' in
2887 * keyword characters.
2888 */
2889#ifdef FEAT_LISP
2890 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894#endif
2895
2896#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002898 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002900 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902#endif
2903
2904 else if ((int *)varp == &curbuf->b_changed)
2905 {
2906 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002909 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913
2914#ifdef BACKSLASH_IN_FILENAME
2915 else if ((int *)varp == &p_ssl)
2916 {
2917 if (p_ssl)
2918 {
2919 psepc = '/';
2920 psepcN = '\\';
2921 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
2923 else
2924 {
2925 psepc = '\\';
2926 psepcN = '/';
2927 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 buflist_slash_adjust();
2932 alist_slash_adjust();
2933# ifdef FEAT_EVAL
2934 scriptnames_slash_adjust();
2935# endif
2936 }
2937#endif
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 else if ((int *)varp == &curwin->w_p_wrap)
2941 {
2942 if (curwin->w_p_wrap)
2943 curwin->w_leftcol = 0;
2944 }
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 else if ((int *)varp == &p_ea)
2947 {
2948 if (p_ea && !old_value)
2949 win_equal(curwin, FALSE, 0);
2950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952 else if ((int *)varp == &p_wiv)
2953 {
2954 /*
2955 * When 'weirdinvert' changed, set/reset 't_xs'.
2956 * Then set 'weirdinvert' according to value of 't_xs'.
2957 */
2958 if (p_wiv && !old_value)
2959 T_XS = (char_u *)"y";
2960 else if (!p_wiv && old_value)
2961 T_XS = empty_option;
2962 p_wiv = (*T_XS != NUL);
2963 }
2964
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002965#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else if ((int *)varp == &p_beval)
2967 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002968 if (!balloonEvalForTerm)
2969 {
2970 if (p_beval && !old_value)
2971 gui_mch_enable_beval_area(balloonEval);
2972 else if (!p_beval && old_value)
2973 gui_mch_disable_beval_area(balloonEval);
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002976#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002977#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002978 else if ((int *)varp == &p_bevalterm)
2979 {
2980 mch_bevalterm_changed();
2981 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002984#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else if ((int *)varp == &p_acd)
2986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002988 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
2990#endif
2991
2992#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 else if ((int *)varp == &curwin->w_p_diff)
2995 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002997 diff_buf_adjust(curwin);
2998# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (foldmethodIsDiff(curwin))
3000 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003#endif
3004
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003005#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 else if ((int *)varp == &p_imdisable)
3008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003009 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (p_imdisable)
3011 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003012 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 // When the option is set from an autocommand, it may need to take
3014 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003015 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017#endif
3018
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003019#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003021 else if ((int *)varp == &curwin->w_p_spell)
3022 {
3023 if (curwin->w_p_spell)
3024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 char *errmsg = did_set_spelllang(curwin);
3026
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003027 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003028 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003029 }
3030 }
3031#endif
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033#ifdef FEAT_ARABIC
3034 if ((int *)varp == &curwin->w_p_arab)
3035 {
3036 if (curwin->w_p_arab)
3037 {
3038 /*
3039 * 'arabic' is set, handle various sub-settings.
3040 */
3041 if (!p_tbidi)
3042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003043 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (!curwin->w_p_rl)
3045 {
3046 curwin->w_p_rl = TRUE;
3047 changed_window_setting();
3048 }
3049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 if (!p_arshape)
3052 {
3053 p_arshape = TRUE;
3054 redraw_later_clear();
3055 }
3056 }
3057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // Arabic requires a utf-8 encoding, inform the user if its not
3059 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003061 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003062 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3063
Bram Moolenaar8820b482017-03-16 17:23:31 +01003064 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003065 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003066#ifdef FEAT_EVAL
3067 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3068#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003071 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073
3074# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003075 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3077 OPT_LOCAL);
3078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 }
3080 else
3081 {
3082 /*
3083 * 'arabic' is reset, handle various sub-settings.
3084 */
3085 if (!p_tbidi)
3086 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003087 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (curwin->w_p_rl)
3089 {
3090 curwin->w_p_rl = FALSE;
3091 changed_window_setting();
3092 }
3093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // 'arabicshape' isn't reset, it is a global option and
3095 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
3097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // 'delcombine' isn't reset, it is a global option and another
3099 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 curbuf->b_p_iminsert = B_IMODE_NONE;
3104 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3105# endif
3106 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003107 }
3108
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003109#endif
3110
Bram Moolenaar44826212019-08-22 21:23:20 +02003111#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3112 else if (((int *)varp == &curwin->w_p_nu
3113 || (int *)varp == &curwin->w_p_rnu)
3114 && gui.in_use
3115 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3116 && curbuf->b_signlist != NULL)
3117 {
3118 // If the 'number' or 'relativenumber' options are modified and
3119 // 'signcolumn' is set to 'number', then clear the screen for a full
3120 // refresh. Otherwise the sign icons are not displayed properly in the
3121 // number column. If the 'number' option is set and only the
3122 // 'relativenumber' option is toggled, then don't refresh the screen
3123 // (optimization).
3124 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3125 redraw_all_later(CLEAR);
3126 }
3127#endif
3128
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003129#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003131 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003132 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003133# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003135 if (
3136# ifdef VIMDLL
3137 !gui.in_use && !gui.starting &&
3138# endif
3139 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003140 {
3141 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003142 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003143 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003144 if (is_term_win32())
3145 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003146# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003147# ifdef FEAT_GUI
3148 if (!gui.in_use && !gui.starting)
3149# endif
3150 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003151# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003153 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003154 {
3155 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003156 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003157 init_highlight(TRUE, FALSE);
3158 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003159# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003160 }
3161#endif
3162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 /*
3164 * End of handling side effects for bool options.
3165 */
3166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 options[opt_idx].flags |= P_WAS_SET;
3170
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003171#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003172 apply_optionset_autocmd(opt_idx, opt_flags,
3173 (long)(old_value ? TRUE : FALSE),
3174 (long)(old_global_value ? TRUE : FALSE),
3175 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003176#endif
3177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003179 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003180 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003181 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003182
3183 if ((opt_flags & OPT_NO_REDRAW) == 0)
3184 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 return NULL;
3187}
3188
3189/*
3190 * Set the value of a number option, and take care of side effects.
3191 * Returns NULL for success, or an error message for an error.
3192 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003193 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003194set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003195 int opt_idx, // index in options[] table
3196 char_u *varp, // pointer to the option variable
3197 long value, // new value
3198 char *errbuf, // buffer for error messages
3199 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003200 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3201 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003203 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003205#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003206 long old_global_value = 0; // only used when setting a local and
3207 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003208#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003209 long old_Rows = Rows; // remember old Rows
3210 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 long *pp = (long *)varp;
3212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003214 if ((secure
3215#ifdef HAVE_SANDBOX
3216 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003218 ) && (options[opt_idx].flags & P_SECURE))
3219 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003221#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003222 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003223 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003224 // value (since there is only one value).
3225 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003226 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3227 OPT_GLOBAL);
3228#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 *pp = value;
3231#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003233 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003235#ifdef FEAT_GUI
3236 need_mouse_correct = TRUE;
3237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaar14f24742012-08-08 18:01:05 +02003239 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
3241 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003242#ifdef FEAT_VARTABS
3243 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3244 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3245 ? tabstop_first(curbuf->b_p_vts_array)
3246 : curbuf->b_p_ts;
3247#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251
3252 /*
3253 * Number options that need some action when changed
3254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (pp == &p_wh || pp == &p_hh)
3256 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003257 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (p_wh < 1)
3259 {
3260 errmsg = e_positive;
3261 p_wh = 1;
3262 }
3263 if (p_wmh > p_wh)
3264 {
3265 errmsg = e_winheight;
3266 p_wh = p_wmh;
3267 }
3268 if (p_hh < 0)
3269 {
3270 errmsg = e_positive;
3271 p_hh = 0;
3272 }
3273
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003274 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003275 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
3277 if (pp == &p_wh && curwin->w_height < p_wh)
3278 win_setheight((int)p_wh);
3279 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3280 win_setheight((int)p_hh);
3281 }
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 else if (pp == &p_wmh)
3284 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003285 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (p_wmh < 0)
3287 {
3288 errmsg = e_positive;
3289 p_wmh = 0;
3290 }
3291 if (p_wmh > p_wh)
3292 {
3293 errmsg = e_winheight;
3294 p_wmh = p_wh;
3295 }
3296 win_setminheight();
3297 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003298 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003300 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 if (p_wiw < 1)
3302 {
3303 errmsg = e_positive;
3304 p_wiw = 1;
3305 }
3306 if (p_wmw > p_wiw)
3307 {
3308 errmsg = e_winwidth;
3309 p_wiw = p_wmw;
3310 }
3311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003312 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003313 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 win_setwidth((int)p_wiw);
3315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 else if (pp == &p_wmw)
3317 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003318 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (p_wmw < 0)
3320 {
3321 errmsg = e_positive;
3322 p_wmw = 0;
3323 }
3324 if (p_wmw > p_wiw)
3325 {
3326 errmsg = e_winwidth;
3327 p_wmw = p_wiw;
3328 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003329 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003332 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 else if (pp == &p_ls)
3334 {
3335 last_status(FALSE);
3336 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003337
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003339 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003341 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
3344#ifdef FEAT_GUI
3345 else if (pp == &p_linespace)
3346 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003347 // Recompute gui.char_height and resize the Vim window to keep the
3348 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003349 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003350 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
3352#endif
3353
3354#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003355 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 else if (pp == &curwin->w_p_fdl)
3357 {
3358 if (curwin->w_p_fdl < 0)
3359 curwin->w_p_fdl = 0;
3360 newFoldLevel();
3361 }
3362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003363 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else if (pp == &curwin->w_p_fml)
3365 {
3366 foldUpdateAll(curwin);
3367 }
3368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003369 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else if (pp == &curwin->w_p_fdn)
3371 {
3372 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3373 foldUpdateAll(curwin);
3374 }
3375
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003376 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 else if (pp == &curwin->w_p_fdc)
3378 {
3379 if (curwin->w_p_fdc < 0)
3380 {
3381 errmsg = e_positive;
3382 curwin->w_p_fdc = 0;
3383 }
3384 else if (curwin->w_p_fdc > 12)
3385 {
3386 errmsg = e_invarg;
3387 curwin->w_p_fdc = 12;
3388 }
3389 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003390#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003392#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003393 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3395 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003396# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (foldmethodIsIndent(curwin))
3398 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003399# endif
3400# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003401 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3402 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003403 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3404 parse_cino(curbuf);
3405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003409 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003410 else if (pp == &p_mco)
3411 {
3412 if (p_mco > MAX_MCO)
3413 p_mco = MAX_MCO;
3414 else if (p_mco < 0)
3415 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003416 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003417 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 else if (pp == &curbuf->b_p_iminsert)
3420 {
3421 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3422 {
3423 errmsg = e_invarg;
3424 curbuf->b_p_iminsert = B_IMODE_NONE;
3425 }
3426 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003429#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003430 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 status_redraw_curbuf();
3432#endif
3433 }
3434
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003435#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003436 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003437 else if (pp == &p_imst)
3438 {
3439 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3440 errmsg = e_invarg;
3441 }
3442#endif
3443
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003444 else if (pp == &p_window)
3445 {
3446 if (p_window < 1)
3447 p_window = 1;
3448 else if (p_window >= Rows)
3449 p_window = Rows - 1;
3450 }
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 else if (pp == &curbuf->b_p_imsearch)
3453 {
3454 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3455 {
3456 errmsg = e_invarg;
3457 curbuf->b_p_imsearch = B_IMODE_NONE;
3458 }
3459 p_imsearch = curbuf->b_p_imsearch;
3460 }
3461
3462#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 else if (pp == &p_titlelen)
3465 {
3466 if (p_titlelen < 0)
3467 {
3468 errmsg = e_positive;
3469 p_titlelen = 85;
3470 }
3471 if (starting != NO_SCREEN && old_value != p_titlelen)
3472 need_maketitle = TRUE;
3473 }
3474#endif
3475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003476 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else if (pp == &p_ch)
3478 {
3479 if (p_ch < 1)
3480 {
3481 errmsg = e_positive;
3482 p_ch = 1;
3483 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003484 if (p_ch > Rows - min_rows() + 1)
3485 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 // Only compute the new window layout when startup has been
3488 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (p_ch != old_value && full_screen
3490#ifdef FEAT_GUI
3491 && !gui.starting
3492#endif
3493 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003494 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003497 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 else if (pp == &p_uc)
3499 {
3500 if (p_uc < 0)
3501 {
3502 errmsg = e_positive;
3503 p_uc = 100;
3504 }
3505 if (p_uc && !old_value)
3506 ml_open_files();
3507 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003508#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003509 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003510 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003511 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003512 {
3513 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003514 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003515 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003516 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003517 {
3518 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003519 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003520 }
3521 }
3522#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003523#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003524 else if (pp == &p_mzq)
3525 mzvim_reset_timer();
3526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003528#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003529 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003530 else if (pp == &p_pyx)
3531 {
3532 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3533 errmsg = e_invarg;
3534 }
3535#endif
3536
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003537 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 else if (pp == &p_ul)
3539 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003540 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003542 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 p_ul = value;
3544 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003545 else if (pp == &curbuf->b_p_ul)
3546 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003547 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003548 curbuf->b_p_ul = old_value;
3549 u_sync(TRUE);
3550 curbuf->b_p_ul = value;
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003553#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003554 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003555 else if (pp == &curwin->w_p_nuw)
3556 {
3557 if (curwin->w_p_nuw < 1)
3558 {
3559 errmsg = e_positive;
3560 curwin->w_p_nuw = 1;
3561 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003562 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003563 {
3564 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003565 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003566 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003568 }
3569#endif
3570
Bram Moolenaar1a384422010-07-14 19:53:30 +02003571 else if (pp == &curbuf->b_p_tw)
3572 {
3573 if (curbuf->b_p_tw < 0)
3574 {
3575 errmsg = e_positive;
3576 curbuf->b_p_tw = 0;
3577 }
3578#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003579 {
3580 win_T *wp;
3581 tabpage_T *tp;
3582
3583 FOR_ALL_TAB_WINDOWS(tp, wp)
3584 check_colorcolumn(wp);
3585 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003586#endif
3587 }
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 /*
3590 * Check the bounds for numeric options here
3591 */
3592 if (Rows < min_rows() && full_screen)
3593 {
3594 if (errbuf != NULL)
3595 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003596 vim_snprintf((char *)errbuf, errbuflen,
3597 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 errmsg = errbuf;
3599 }
3600 Rows = min_rows();
3601 }
3602 if (Columns < MIN_COLUMNS && full_screen)
3603 {
3604 if (errbuf != NULL)
3605 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003606 vim_snprintf((char *)errbuf, errbuflen,
3607 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 errmsg = errbuf;
3609 }
3610 Columns = MIN_COLUMNS;
3611 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003612 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /*
3615 * If the screen (shell) height has been changed, assume it is the
3616 * physical screenheight.
3617 */
3618 if (old_Rows != Rows || old_Columns != Columns)
3619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003620 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (updating_screen)
3622 *pp = old_value;
3623 else if (full_screen
3624#ifdef FEAT_GUI
3625 && !gui.starting
3626#endif
3627 )
3628 set_shellsize((int)Columns, (int)Rows, TRUE);
3629 else
3630 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003631 // Postpone the resizing; check the size and cmdline position for
3632 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 check_shellsize();
3634 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3635 cmdline_row = Rows - p_ch;
3636 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003637 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003638 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (curbuf->b_p_ts <= 0)
3642 {
3643 errmsg = e_positive;
3644 curbuf->b_p_ts = 8;
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (p_tm < 0)
3647 {
3648 errmsg = e_positive;
3649 p_tm = 0;
3650 }
3651 if ((curwin->w_p_scr <= 0
3652 || (curwin->w_p_scr > curwin->w_height
3653 && curwin->w_height > 0))
3654 && full_screen)
3655 {
3656 if (pp == &(curwin->w_p_scr))
3657 {
3658 if (curwin->w_p_scr != 0)
3659 errmsg = e_scroll;
3660 win_comp_scroll(curwin);
3661 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003662 // If 'scroll' became invalid because of a side effect silently adjust
3663 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else if (curwin->w_p_scr <= 0)
3665 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003666 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 curwin->w_p_scr = curwin->w_height;
3668 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003669 if (p_hi < 0)
3670 {
3671 errmsg = e_positive;
3672 p_hi = 0;
3673 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003674 else if (p_hi > 10000)
3675 {
3676 errmsg = e_invarg;
3677 p_hi = 10000;
3678 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003679 if (p_re < 0 || p_re > 2)
3680 {
3681 errmsg = e_invarg;
3682 p_re = 0;
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 if (p_report < 0)
3685 {
3686 errmsg = e_positive;
3687 p_report = 1;
3688 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003689 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003691 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 p_sj = Rows / 2;
3693 else
3694 {
3695 errmsg = e_scroll;
3696 p_sj = 1;
3697 }
3698 }
3699 if (p_so < 0 && full_screen)
3700 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003701 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 p_so = 0;
3703 }
3704 if (p_siso < 0 && full_screen)
3705 {
3706 errmsg = e_positive;
3707 p_siso = 0;
3708 }
3709#ifdef FEAT_CMDWIN
3710 if (p_cwh < 1)
3711 {
3712 errmsg = e_positive;
3713 p_cwh = 1;
3714 }
3715#endif
3716 if (p_ut < 0)
3717 {
3718 errmsg = e_positive;
3719 p_ut = 2000;
3720 }
3721 if (p_ss < 0)
3722 {
3723 errmsg = e_positive;
3724 p_ss = 0;
3725 }
3726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003727 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3729 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3730
3731 options[opt_idx].flags |= P_WAS_SET;
3732
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003733#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003734 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3735 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003736#endif
3737
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003739 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003740 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003741 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003742 if ((opt_flags & OPT_NO_REDRAW) == 0)
3743 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 return errmsg;
3746}
3747
3748/*
3749 * Called after an option changed: check if something needs to be redrawn.
3750 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003751 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003752check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003755 int doclear = (flags & P_RCLR) == P_RCLR;
3756 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
3761 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3762 changed_window_setting();
3763 if (flags & P_RBUF)
3764 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003765 if (flags & P_RWINONLY)
3766 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003767 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 redraw_all_later(CLEAR);
3769 else if (all)
3770 redraw_all_later(NOT_VALID);
3771}
3772
3773/*
3774 * Find index for option 'arg'.
3775 * Return -1 if not found.
3776 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003777 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003778findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 int opt_idx;
3781 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003782 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 int is_term_opt;
3784
3785 /*
3786 * For first call: Initialize the quick-access table.
3787 * It contains the index for the first option that starts with a certain
3788 * letter. There are 26 letters, plus the first "t_" option.
3789 */
3790 if (quick_tab[1] == 0)
3791 {
3792 p = options[0].fullname;
3793 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3794 {
3795 if (s[0] != p[0])
3796 {
3797 if (s[0] == 't' && s[1] == '_')
3798 quick_tab[26] = opt_idx;
3799 else
3800 quick_tab[CharOrdLow(s[0])] = opt_idx;
3801 }
3802 p = s;
3803 }
3804 }
3805
3806 /*
3807 * Check for name starting with an illegal character.
3808 */
3809#ifdef EBCDIC
3810 if (!islower(arg[0]))
3811#else
3812 if (arg[0] < 'a' || arg[0] > 'z')
3813#endif
3814 return -1;
3815
3816 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3817 if (is_term_opt)
3818 opt_idx = quick_tab[26];
3819 else
3820 opt_idx = quick_tab[CharOrdLow(arg[0])];
3821 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3822 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003823 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 break;
3825 }
3826 if (s == NULL && !is_term_opt)
3827 {
3828 opt_idx = quick_tab[CharOrdLow(arg[0])];
3829 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3830 {
3831 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003832 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 break;
3834 s = NULL;
3835 }
3836 }
3837 if (s == NULL)
3838 opt_idx = -1;
3839 return opt_idx;
3840}
3841
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003842#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843/*
3844 * Get the value for an option.
3845 *
3846 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003847 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003848 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003849 * String option: gov_string, *stringval gets allocated string.
3850 * Hidden Number option: gov_hidden_number.
3851 * Hidden Toggle option: gov_hidden_bool.
3852 * Hidden String option: gov_hidden_string.
3853 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003855 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003856get_option_value(
3857 char_u *name,
3858 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003859 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003860 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 int opt_idx;
3863 char_u *varp;
3864
3865 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003866 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003867 {
3868 int key;
3869
3870 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003871 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003872 {
3873 char_u key_name[2];
3874 char_u *p;
3875
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003876 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003877 if (key < 0)
3878 {
3879 key_name[0] = KEY2TERMCAP0(key);
3880 key_name[1] = KEY2TERMCAP1(key);
3881 }
3882 else
3883 {
3884 key_name[0] = KS_KEY;
3885 key_name[1] = (key & 0xff);
3886 }
3887 p = find_termcode(key_name);
3888 if (p != NULL)
3889 {
3890 if (stringval != NULL)
3891 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003892 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003893 }
3894 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003895 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
3898 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3899
3900 if (options[opt_idx].flags & P_STRING)
3901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003902 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003903 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (stringval != NULL)
3905 {
3906#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003907 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003908 if ((char_u **)varp == &curbuf->b_p_key
3909 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 *stringval = vim_strsave((char_u *)"*****");
3911 else
3912#endif
3913 *stringval = vim_strsave(*(char_u **)(varp));
3914 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003915 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003918 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003919 return (options[opt_idx].flags & P_NUM)
3920 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if (options[opt_idx].flags & P_NUM)
3922 *numval = *(long *)varp;
3923 else
3924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003925 // Special case: 'modified' is b_changed, but we also want to consider
3926 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if ((int *)varp == &curbuf->b_changed)
3928 *numval = curbufIsChanged();
3929 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003930 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003932 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933}
3934#endif
3935
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003936#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003937/*
3938 * Returns the option attributes and its value. Unlike the above function it
3939 * will return either global value or local value of the option depending on
3940 * what was requested, but it will never return global value if it was
3941 * requested to return local one and vice versa. Neither it will return
3942 * buffer-local value if it was requested to return window-local one.
3943 *
3944 * Pretends that option is absent if it is not present in the requested scope
3945 * (i.e. has no global, window-local or buffer-local value depending on
3946 * opt_type). Uses
3947 *
3948 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003949 * 0 hidden or unknown option, also option that does not have requested
3950 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003951 * see SOPT_* in vim.h for other flags
3952 *
3953 * Possible opt_type values: see SREQ_* in vim.h
3954 */
3955 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003956get_option_value_strict(
3957 char_u *name,
3958 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003959 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003960 int opt_type,
3961 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003962{
3963 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003964 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003965 struct vimoption *p;
3966 int r = 0;
3967
3968 opt_idx = findoption(name);
3969 if (opt_idx < 0)
3970 return 0;
3971
3972 p = &(options[opt_idx]);
3973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003974 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003975 if (p->var == NULL)
3976 return 0;
3977
3978 if (p->flags & P_BOOL)
3979 r |= SOPT_BOOL;
3980 else if (p->flags & P_NUM)
3981 r |= SOPT_NUM;
3982 else if (p->flags & P_STRING)
3983 r |= SOPT_STRING;
3984
3985 if (p->indir == PV_NONE)
3986 {
3987 if (opt_type == SREQ_GLOBAL)
3988 r |= SOPT_GLOBAL;
3989 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003990 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003991 }
3992 else
3993 {
3994 if (p->indir & PV_BOTH)
3995 r |= SOPT_GLOBAL;
3996 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003997 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003998
3999 if (p->indir & PV_WIN)
4000 {
4001 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004002 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004003 else
4004 r |= SOPT_WIN;
4005 }
4006 else if (p->indir & PV_BUF)
4007 {
4008 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004010 else
4011 r |= SOPT_BUF;
4012 }
4013 }
4014
4015 if (stringval == NULL)
4016 return r;
4017
4018 if (opt_type == SREQ_GLOBAL)
4019 varp = p->var;
4020 else
4021 {
4022 if (opt_type == SREQ_BUF)
4023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004024 // Special case: 'modified' is b_changed, but we also want to
4025 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004026 if (p->indir == PV_MOD)
4027 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004028 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004029 varp = NULL;
4030 }
4031#ifdef FEAT_CRYPT
4032 else if (p->indir == PV_KEY)
4033 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004034 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004035 *stringval = NULL;
4036 varp = NULL;
4037 }
4038#endif
4039 else
4040 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004041 buf_T *save_curbuf = curbuf;
4042
4043 // only getting a pointer, no need to use aucmd_prepbuf()
4044 curbuf = (buf_T *)from;
4045 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004046 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004047 curbuf = save_curbuf;
4048 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004049 }
4050 }
4051 else if (opt_type == SREQ_WIN)
4052 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004053 win_T *save_curwin = curwin;
4054
4055 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004056 curbuf = curwin->w_buffer;
4057 varp = get_varp(p);
4058 curwin = save_curwin;
4059 curbuf = curwin->w_buffer;
4060 }
4061 if (varp == p->var)
4062 return (r | SOPT_UNSET);
4063 }
4064
4065 if (varp != NULL)
4066 {
4067 if (p->flags & P_STRING)
4068 *stringval = vim_strsave(*(char_u **)(varp));
4069 else if (p->flags & P_NUM)
4070 *numval = *(long *) varp;
4071 else
4072 *numval = *(int *)varp;
4073 }
4074
4075 return r;
4076}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004077
4078/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004079 * Iterate over options. First argument is a pointer to a pointer to a
4080 * structure inside options[] array, second is option type like in the above
4081 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004082 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004083 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004084 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004085 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004086 * first argument to NULL.
4087 *
4088 * Returns full option name for current option on each call.
4089 */
4090 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004091option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004092{
4093 struct vimoption *ret = NULL;
4094 do
4095 {
4096 if (*option == NULL)
4097 *option = (void *) options;
4098 else if (((struct vimoption *) (*option))->fullname == NULL)
4099 {
4100 *option = NULL;
4101 return NULL;
4102 }
4103 else
4104 *option = (void *) (((struct vimoption *) (*option)) + 1);
4105
4106 ret = ((struct vimoption *) (*option));
4107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004108 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004109 if (ret->var == NULL)
4110 {
4111 ret = NULL;
4112 continue;
4113 }
4114
4115 switch (opt_type)
4116 {
4117 case SREQ_GLOBAL:
4118 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4119 ret = NULL;
4120 break;
4121 case SREQ_BUF:
4122 if (!(ret->indir & PV_BUF))
4123 ret = NULL;
4124 break;
4125 case SREQ_WIN:
4126 if (!(ret->indir & PV_WIN))
4127 ret = NULL;
4128 break;
4129 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004130 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004131 return NULL;
4132 }
4133 }
4134 while (ret == NULL);
4135
4136 return (char_u *)ret->fullname;
4137}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004138#endif
4139
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004141 * Return the flags for the option at 'opt_idx'.
4142 */
4143 long_u
4144get_option_flags(int opt_idx)
4145{
4146 return options[opt_idx].flags;
4147}
4148
4149/*
4150 * Set a flag for the option at 'opt_idx'.
4151 */
4152 void
4153set_option_flag(int opt_idx, long_u flag)
4154{
4155 options[opt_idx].flags |= flag;
4156}
4157
4158/*
4159 * Clear a flag for the option at 'opt_idx'.
4160 */
4161 void
4162clear_option_flag(int opt_idx, long_u flag)
4163{
4164 options[opt_idx].flags &= ~flag;
4165}
4166
4167/*
4168 * Returns TRUE if the option at 'opt_idx' is a global option
4169 */
4170 int
4171is_global_option(int opt_idx)
4172{
4173 return options[opt_idx].indir == PV_NONE;
4174}
4175
4176/*
4177 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4178 * local value.
4179 */
4180 int
4181is_global_local_option(int opt_idx)
4182{
4183 return options[opt_idx].indir & PV_BOTH;
4184}
4185
4186/*
4187 * Returns TRUE if the option at 'opt_idx' is a window-local option
4188 */
4189 int
4190is_window_local_option(int opt_idx)
4191{
4192 return options[opt_idx].var == VAR_WIN;
4193}
4194
4195/*
4196 * Returns TRUE if the option at 'opt_idx' is a hidden option
4197 */
4198 int
4199is_hidden_option(int opt_idx)
4200{
4201 return options[opt_idx].var == NULL;
4202}
4203
4204#if defined(FEAT_CRYPT) || defined(PROTO)
4205/*
4206 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4207 */
4208 int
4209is_crypt_key_option(int opt_idx)
4210{
4211 return options[opt_idx].indir == PV_KEY;
4212}
4213#endif
4214
4215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * Set the value of option "name".
4217 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004218 *
4219 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004221 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004222set_option_value(
4223 char_u *name,
4224 long number,
4225 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 int opt_idx;
4229 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004230 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
4232 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004233 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004234 {
4235 int key;
4236
4237 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004238 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004239 {
4240 char_u key_name[2];
4241
4242 if (key < 0)
4243 {
4244 key_name[0] = KEY2TERMCAP0(key);
4245 key_name[1] = KEY2TERMCAP1(key);
4246 }
4247 else
4248 {
4249 key_name[0] = KS_KEY;
4250 key_name[1] = (key & 0xff);
4251 }
4252 add_termcode(key_name, string, FALSE);
4253 if (full_screen)
4254 ttest(FALSE);
4255 redraw_all_later(CLEAR);
4256 return NULL;
4257 }
4258
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004259 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 else
4262 {
4263 flags = options[opt_idx].flags;
4264#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004265 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004268 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004269 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004272 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004273 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 else
4275 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004276 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004277 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004279 if (number == 0 && string != NULL)
4280 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004281 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004283 // Either we are given a string or we are setting option
4284 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004285 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004286 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004287 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004288 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004289 // There's another character after zeros or the string
4290 // is empty. In both cases, we are trying to set a
4291 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004292 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004293 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004294 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004295
4296 }
4297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004299 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004300 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004302 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004303 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004307 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308}
4309
4310/*
4311 * Get the terminal code for a terminal option.
4312 * Returns NULL when not found.
4313 */
4314 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004315get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
4317 int opt_idx;
4318 char_u *varp;
4319
4320 if (tname[0] != 't' || tname[1] != '_' ||
4321 tname[2] == NUL || tname[3] == NUL)
4322 return NULL;
4323 if ((opt_idx = findoption(tname)) >= 0)
4324 {
4325 varp = get_varp(&(options[opt_idx]));
4326 if (varp != NULL)
4327 varp = *(char_u **)(varp);
4328 return varp;
4329 }
4330 return find_termcode(tname + 2);
4331}
4332
4333 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004334get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 int i;
4337
4338 i = findoption((char_u *)"hl");
4339 if (i >= 0)
4340 return options[i].def_val[VI_DEFAULT];
4341 return (char_u *)NULL;
4342}
4343
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004344 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004345get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004346{
4347 int i;
4348
4349 i = findoption((char_u *)"enc");
4350 if (i >= 0)
4351 return options[i].def_val[VI_DEFAULT];
4352 return (char_u *)NULL;
4353}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004354
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355/*
4356 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004357 * When "has_lt" is true there is a '<' before "*arg_arg".
4358 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 */
4360 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004361find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004363 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004365 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 /*
4368 * Don't use get_special_key_code() for t_xx, we don't want it to call
4369 * add_termcap_entry().
4370 */
4371 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4372 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004373 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004375 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004377 key = find_special_key(&arg, &modifiers,
4378 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004379 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 key = 0;
4381 }
4382 return key;
4383}
4384
4385/*
4386 * if 'all' == 0: show changed options
4387 * if 'all' == 1: show all normal options
4388 * if 'all' == 2: show all terminal options
4389 */
4390 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004391showoptions(
4392 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004393 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
4395 struct vimoption *p;
4396 int col;
4397 int isterm;
4398 char_u *varp;
4399 struct vimoption **items;
4400 int item_count;
4401 int run;
4402 int row, rows;
4403 int cols;
4404 int i;
4405 int len;
4406
4407#define INC 20
4408#define GAP 3
4409
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004410 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 if (items == NULL)
4412 return;
4413
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004414 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004416 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004418 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004420 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004422 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004425 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 * 1. display the short items
4427 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004428 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 */
4430 for (run = 1; run <= 2 && !got_int; ++run)
4431 {
4432 /*
4433 * collect the items in items[]
4434 */
4435 item_count = 0;
4436 for (p = &options[0]; p->fullname != NULL; p++)
4437 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004438 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004439 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004440 continue;
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 varp = NULL;
4443 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004444 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
4446 if (p->indir != PV_NONE && !isterm)
4447 varp = get_varp_scope(p, opt_flags);
4448 }
4449 else
4450 varp = get_varp(p);
4451 if (varp != NULL
4452 && ((all == 2 && isterm)
4453 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004454 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004456 if (opt_flags & OPT_ONECOLUMN)
4457 len = Columns;
4458 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004459 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 else
4461 {
4462 option_value2string(p, opt_flags);
4463 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4464 }
4465 if ((len <= INC - GAP && run == 1) ||
4466 (len > INC - GAP && run == 2))
4467 items[item_count++] = p;
4468 }
4469 }
4470
4471 /*
4472 * display the items
4473 */
4474 if (run == 1)
4475 {
4476 cols = (Columns + GAP - 3) / INC;
4477 if (cols == 0)
4478 cols = 1;
4479 rows = (item_count + cols - 1) / cols;
4480 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004481 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 rows = item_count;
4483 for (row = 0; row < rows && !got_int; ++row)
4484 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004485 msg_putchar('\n'); // go to next line
4486 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 break;
4488 col = 0;
4489 for (i = row; i < item_count; i += rows)
4490 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004491 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 showoneopt(items[i], opt_flags);
4493 col += INC;
4494 }
4495 out_flush();
4496 ui_breakcheck();
4497 }
4498 }
4499 vim_free(items);
4500}
4501
4502/*
4503 * Return TRUE if option "p" has its default value.
4504 */
4505 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004506optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507{
4508 int dvi;
4509
4510 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004511 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004512 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004514 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004516 // the cast to long is required for Manx C, long_i is
4517 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004518 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004519 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4521}
4522
4523/*
4524 * showoneopt: show the value of one option
4525 * must not be called with a hidden option!
4526 */
4527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004528showoneopt(
4529 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004530 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004532 char_u *varp;
4533 int save_silent = silent_mode;
4534
4535 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004536 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 varp = get_varp_scope(p, opt_flags);
4539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004540 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4542 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004543 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004545 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004547 msg_puts(" ");
4548 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (!(p->flags & P_BOOL))
4550 {
4551 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004552 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 option_value2string(p, opt_flags);
4554 msg_outtrans(NameBuff);
4555 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004556
4557 silent_mode = save_silent;
4558 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559}
4560
4561/*
4562 * Write modified options as ":set" commands to a file.
4563 *
4564 * There are three values for "opt_flags":
4565 * OPT_GLOBAL: Write global option values and fresh values of
4566 * buffer-local options (used for start of a session
4567 * file).
4568 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4569 * curwin (used for a vimrc file).
4570 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4571 * and local values for window-local options of
4572 * curwin. Local values are also written when at the
4573 * default value, because a modeline or autocommand
4574 * may have set them when doing ":edit file" and the
4575 * user has set them back at the default or fresh
4576 * value.
4577 * When "local_only" is TRUE, don't write fresh
4578 * values, only local values (for ":mkview").
4579 * (fresh value = value used for a new buffer or window for a local option).
4580 *
4581 * Return FAIL on error, OK otherwise.
4582 */
4583 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004584makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585{
4586 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004587 char_u *varp; // currently used value
4588 char_u *varp_fresh; // local value
4589 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 char *cmd;
4591 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004592 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 /*
4595 * The options that don't have a default (terminal name, columns, lines)
4596 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004597 * Do the loop over "options[]" twice: once for options with the
4598 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004600 for (pri = 1; pri >= 0; --pri)
4601 {
4602 for (p = &options[0]; !istermoption(p); p++)
4603 if (!(p->flags & P_NO_MKRC)
4604 && !istermoption(p)
4605 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004607 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4609 continue;
4610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4612 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4614 continue;
4615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004616 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004618 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 continue;
4620
Bram Moolenaard23b7142021-04-17 21:04:34 +02004621 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4622 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004623 continue;
4624
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 round = 2;
4626 if (p->indir != PV_NONE)
4627 {
4628 if (p->var == VAR_WIN)
4629 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004630 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (!(opt_flags & OPT_LOCAL))
4632 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004633 // When fresh value of window-local option is not at the
4634 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4636 {
4637 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004638 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
4640 round = 1;
4641 varp_local = varp;
4642 varp = varp_fresh;
4643 }
4644 }
4645 }
4646 }
4647
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004648 // Round 1: fresh value for window-local options.
4649 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 for ( ; round <= 2; varp = varp_local, ++round)
4651 {
4652 if (round == 1 || (opt_flags & OPT_GLOBAL))
4653 cmd = "set";
4654 else
4655 cmd = "setlocal";
4656
4657 if (p->flags & P_BOOL)
4658 {
4659 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4660 return FAIL;
4661 }
4662 else if (p->flags & P_NUM)
4663 {
4664 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4665 return FAIL;
4666 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004667 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004669 int do_endif = FALSE;
4670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004671 // Don't set 'syntax' and 'filetype' again if the value is
4672 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004673 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004674#if defined(FEAT_SYN_HL)
4675 p->indir == PV_SYN ||
4676#endif
4677 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
4679 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4680 *(char_u **)(varp)) < 0
4681 || put_eol(fd) < 0)
4682 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004683 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
4685 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004686 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004688 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
4690 if (put_line(fd, "endif") == FAIL)
4691 return FAIL;
4692 }
4693 }
4694 }
4695 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 return OK;
4698}
4699
4700#if defined(FEAT_FOLDING) || defined(PROTO)
4701/*
4702 * Generate set commands for the local fold options only. Used when
4703 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4704 */
4705 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004706makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004708 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004710 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 == FAIL
4712# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004713 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004715 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 == FAIL
4717 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4718 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4719 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4720 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4721 )
4722 return FAIL;
4723
4724 return OK;
4725}
4726#endif
4727
4728 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004729put_setstring(
4730 FILE *fd,
4731 char *cmd,
4732 char *name,
4733 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004734 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004737 char_u *buf = NULL;
4738 char_u *part = NULL;
4739 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4742 return FAIL;
4743 if (*valuep != NULL)
4744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004745 // Output 'pastetoggle' as key names. For other
4746 // options some characters have to be escaped with
4747 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (valuep == &p_pt)
4749 {
4750 s = *valuep;
4751 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004752 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 return FAIL;
4754 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004755 // expand the option value, replace $HOME by ~
4756 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004758 int size = (int)STRLEN(*valuep) + 1;
4759
4760 // replace home directory in the whole option value into "buf"
4761 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004762 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004763 goto fail;
4764 home_replace(NULL, *valuep, buf, size, FALSE);
4765
4766 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004767 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004768 // can be expanded when read back.
4769 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4770 && vim_strchr(*valuep, ',') != NULL)
4771 {
4772 part = alloc(size);
4773 if (part == NULL)
4774 goto fail;
4775
4776 // write line break to clear the option, e.g. ':set rtp='
4777 if (put_eol(fd) == FAIL)
4778 goto fail;
4779
4780 p = buf;
4781 while (*p != NUL)
4782 {
4783 // for each comma separated option part, append value to
4784 // the option, :set rtp+=value
4785 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4786 goto fail;
4787 (void)copy_option_part(&p, part, size, ",");
4788 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4789 goto fail;
4790 }
4791 vim_free(buf);
4792 vim_free(part);
4793 return OK;
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004796 {
4797 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004799 }
4800 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802 else if (put_escstr(fd, *valuep, 2) == FAIL)
4803 return FAIL;
4804 }
4805 if (put_eol(fd) < 0)
4806 return FAIL;
4807 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004808fail:
4809 vim_free(buf);
4810 vim_free(part);
4811 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812}
4813
4814 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004815put_setnum(
4816 FILE *fd,
4817 char *cmd,
4818 char *name,
4819 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
4821 long wc;
4822
4823 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4824 return FAIL;
4825 if (wc_use_keyname((char_u *)valuep, &wc))
4826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004827 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4829 return FAIL;
4830 }
4831 else if (fprintf(fd, "%ld", *valuep) < 0)
4832 return FAIL;
4833 if (put_eol(fd) < 0)
4834 return FAIL;
4835 return OK;
4836}
4837
4838 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004839put_setbool(
4840 FILE *fd,
4841 char *cmd,
4842 char *name,
4843 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004845 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004846 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4848 || put_eol(fd) < 0)
4849 return FAIL;
4850 return OK;
4851}
4852
4853/*
4854 * Clear all the terminal options.
4855 * If the option has been allocated, free the memory.
4856 * Terminal options are never hidden or indirect.
4857 */
4858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004859clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 /*
4862 * Reset a few things before clearing the old options. This may cause
4863 * outputting a few things that the terminal doesn't understand, but the
4864 * screen will be cleared later, so this is OK.
4865 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004866 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#endif
4870#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004871 // When starting the GUI close the display opened for the clipboard.
4872 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if (gui.starting)
4874 clear_xterm_clip();
4875#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004876 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004878 free_termoptions();
4879}
4880
4881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004882free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004883{
4884 struct vimoption *p;
4885
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004886 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (istermoption(p))
4888 {
4889 if (p->flags & P_ALLOCED)
4890 free_string_option(*(char_u **)(p->var));
4891 if (p->flags & P_DEF_ALLOCED)
4892 free_string_option(p->def_val[VI_DEFAULT]);
4893 *(char_u **)(p->var) = empty_option;
4894 p->def_val[VI_DEFAULT] = empty_option;
4895 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004896#ifdef FEAT_EVAL
4897 // remember where the option was cleared
4898 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 clear_termcodes();
4902}
4903
4904/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004905 * Free the string for one term option, if it was allocated.
4906 * Set the string to empty_option and clear allocated flag.
4907 * "var" points to the option value.
4908 */
4909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004910free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004911{
4912 struct vimoption *p;
4913
4914 for (p = &options[0]; p->fullname != NULL; p++)
4915 if (p->var == var)
4916 {
4917 if (p->flags & P_ALLOCED)
4918 free_string_option(*(char_u **)(p->var));
4919 *(char_u **)(p->var) = empty_option;
4920 p->flags &= ~P_ALLOCED;
4921 break;
4922 }
4923}
4924
4925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 * Set the terminal option defaults to the current value.
4927 * Used after setting the terminal name.
4928 */
4929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004930set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
4932 struct vimoption *p;
4933
4934 for (p = &options[0]; p->fullname != NULL; p++)
4935 {
4936 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4937 {
4938 if (p->flags & P_DEF_ALLOCED)
4939 {
4940 free_string_option(p->def_val[VI_DEFAULT]);
4941 p->flags &= ~P_DEF_ALLOCED;
4942 }
4943 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4944 if (p->flags & P_ALLOCED)
4945 {
4946 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004947 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
4950 }
4951}
4952
4953/*
4954 * return TRUE if 'p' starts with 't_'
4955 */
4956 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004957istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4960}
4961
Bram Moolenaardac13472019-09-16 21:06:21 +02004962/*
4963 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4964 */
4965 int
4966istermoption_idx(int opt_idx)
4967{
4968 return istermoption(&options[opt_idx]);
4969}
4970
Bram Moolenaar113e1072019-01-20 15:30:40 +01004971#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004973 * Unset local option value, similar to ":set opt<".
4974 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004975 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004976unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004977{
4978 struct vimoption *p;
4979 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004980 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004981
4982 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004983 if (opt_idx < 0)
4984 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004985 p = &(options[opt_idx]);
4986
4987 switch ((int)p->indir)
4988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004989 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004990 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004991 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004992 break;
4993 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004994 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004995 break;
4996 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004997 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004998 break;
4999 case PV_AR:
5000 buf->b_p_ar = -1;
5001 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005002 case PV_BKC:
5003 clear_string_option(&buf->b_p_bkc);
5004 buf->b_bkc_flags = 0;
5005 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005006 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005007 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005008 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005009 case PV_TC:
5010 clear_string_option(&buf->b_p_tc);
5011 buf->b_tc_flags = 0;
5012 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005013 case PV_SISO:
5014 curwin->w_p_siso = -1;
5015 break;
5016 case PV_SO:
5017 curwin->w_p_so = -1;
5018 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005019#ifdef FEAT_FIND_ID
5020 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005021 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005022 break;
5023 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005024 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005025 break;
5026#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005027 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005028 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005029 break;
5030 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005031 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005032 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005033 case PV_FP:
5034 clear_string_option(&buf->b_p_fp);
5035 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005036#ifdef FEAT_QUICKFIX
5037 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005038 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005039 break;
5040 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005041 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005042 break;
5043 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005044 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005045 break;
5046#endif
5047#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5048 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005049 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005050 break;
5051#endif
5052#if defined(FEAT_CRYPT)
5053 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005054 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005055 break;
5056#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005057#ifdef FEAT_LINEBREAK
5058 case PV_SBR:
5059 clear_string_option(&((win_T *)from)->w_p_sbr);
5060 break;
5061#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005062#ifdef FEAT_STL_OPT
5063 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005064 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005065 break;
5066#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005067 case PV_UL:
5068 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5069 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005070#ifdef FEAT_LISP
5071 case PV_LW:
5072 clear_string_option(&buf->b_p_lw);
5073 break;
5074#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005075 case PV_MENC:
5076 clear_string_option(&buf->b_p_menc);
5077 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005078 case PV_LCS:
5079 clear_string_option(&((win_T *)from)->w_p_lcs);
5080 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5081 redraw_later(NOT_VALID);
5082 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083 }
5084}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005085#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005086
5087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 * Get pointer to option variable, depending on local or global scope.
5089 */
5090 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005091get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5094 {
5095 if (p->var == VAR_WIN)
5096 return (char_u *)GLOBAL_WO(get_varp(p));
5097 return p->var;
5098 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005099 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 switch ((int)p->indir)
5102 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005103 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005105 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5106 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5107 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005109 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5110 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5111 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005112 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005113 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005114 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005115 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5116 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005118 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5119 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005121 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5122 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005123#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5124 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5125#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005126#if defined(FEAT_CRYPT)
5127 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5128#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005129#ifdef FEAT_LINEBREAK
5130 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5131#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005132#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005133 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005134#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005135 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005136#ifdef FEAT_LISP
5137 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5138#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005139 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005140 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005141 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005144 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 return get_varp(p);
5147}
5148
5149/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005150 * Get pointer to option variable at 'opt_idx', depending on local or global
5151 * scope.
5152 */
5153 char_u *
5154get_option_varp_scope(int opt_idx, int opt_flags)
5155{
5156 return get_varp_scope(&(options[opt_idx]), opt_flags);
5157}
5158
5159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 * Get pointer to option variable.
5161 */
5162 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005163get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005165 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (p->var == NULL)
5167 return NULL;
5168
5169 switch ((int)p->indir)
5170 {
5171 case PV_NONE: return p->var;
5172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005173 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005174 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005176 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005178 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005180 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005182 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005184 case PV_TC: return *curbuf->b_p_tc != NUL
5185 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005186 case PV_BKC: return *curbuf->b_p_bkc != NUL
5187 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005188 case PV_SISO: return curwin->w_p_siso >= 0
5189 ? (char_u *)&(curwin->w_p_siso) : p->var;
5190 case PV_SO: return curwin->w_p_so >= 0
5191 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005193 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005195 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5197#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005198 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005200 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005202 case PV_FP: return *curbuf->b_p_fp != NUL
5203 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005205 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005207 case PV_GP: return *curbuf->b_p_gp != NUL
5208 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5209 case PV_MP: return *curbuf->b_p_mp != NUL
5210 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005212#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5213 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5214 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5215#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005216#if defined(FEAT_CRYPT)
5217 case PV_CM: return *curbuf->b_p_cm != NUL
5218 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5219#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005220#ifdef FEAT_LINEBREAK
5221 case PV_SBR: return *curwin->w_p_sbr != NUL
5222 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5223#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005224#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005225 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005226 ? (char_u *)&(curwin->w_p_stl) : p->var;
5227#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005228 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5229 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005230#ifdef FEAT_LISP
5231 case PV_LW: return *curbuf->b_p_lw != NUL
5232 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5233#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005234 case PV_MENC: return *curbuf->b_p_menc != NUL
5235 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#ifdef FEAT_ARABIC
5237 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5238#endif
5239 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005240 case PV_LCS: return *curwin->w_p_lcs != NUL
5241 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005242#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005243 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5244#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005245#ifdef FEAT_SYN_HL
5246 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5247 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005248 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005249 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#ifdef FEAT_DIFF
5252 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5253#endif
5254#ifdef FEAT_FOLDING
5255 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5256 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5257 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5258 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5259 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5260 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5261 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5262# ifdef FEAT_EVAL
5263 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5264 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5265# endif
5266 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5267#endif
5268 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005269 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005270#ifdef FEAT_LINEBREAK
5271 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005274 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005275#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5277#endif
5278#ifdef FEAT_RIGHTLEFT
5279 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5280 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5281#endif
5282 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5283 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5284#ifdef FEAT_LINEBREAK
5285 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005286 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5287 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005289 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005291 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005292#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005293 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5294 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5295#endif
5296#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005297 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5298 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5299 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
5302 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5303 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5306 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5308 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5309#ifdef FEAT_CINDENT
5310 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5311 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5312 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5313#endif
5314#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5315 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#ifdef FEAT_FOLDING
5319 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005322#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005323 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005325#ifdef FEAT_COMPL_FUNC
5326 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005327 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005328#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005329#ifdef FEAT_EVAL
5330 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005333 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005339 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5341 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5342 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5343 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5344#ifdef FEAT_FIND_ID
5345# ifdef FEAT_EVAL
5346 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5347# endif
5348#endif
5349#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5350 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5351 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5352#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005353#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005354 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356#ifdef FEAT_CRYPT
5357 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5358#endif
5359#ifdef FEAT_LISP
5360 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5361#endif
5362 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5363 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5364 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5365 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5366 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005368#ifdef FEAT_TEXTOBJ
5369 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5372#ifdef FEAT_SMARTINDENT
5373 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5377#ifdef FEAT_SEARCHPATH
5378 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5379#endif
5380 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5381#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005382 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005383 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5384#endif
5385#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005386 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5387 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5388 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005389 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#endif
5391 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5392 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5393 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5394 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005395#ifdef FEAT_PERSISTENT_UNDO
5396 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5399#ifdef FEAT_KEYMAP
5400 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5401#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005402#ifdef FEAT_SIGNS
5403 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5404#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005405#ifdef FEAT_VARTABS
5406 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5407 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5408#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005409 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005411 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 return (char_u *)&(curbuf->b_p_wm);
5413}
5414
5415/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005416 * Return a pointer to the variable for option at 'opt_idx'
5417 */
5418 char_u *
5419get_option_var(int opt_idx)
5420{
5421 return options[opt_idx].var;
5422}
5423
5424/*
5425 * Return the full name of the option at 'opt_idx'
5426 */
5427 char_u *
5428get_option_fullname(int opt_idx)
5429{
5430 return (char_u *)options[opt_idx].fullname;
5431}
5432
5433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 * Get the value of 'equalprg', either the buffer-local one or the global one.
5435 */
5436 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005437get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439 if (*curbuf->b_p_ep == NUL)
5440 return p_ep;
5441 return curbuf->b_p_ep;
5442}
5443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444/*
5445 * Copy options from one window to another.
5446 * Used when splitting a window.
5447 */
5448 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005449win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5452 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005453 after_copy_winopt(wp_to);
5454}
5455
5456/*
5457 * After copying window options: update variables depending on options.
5458 */
5459 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005460after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005461{
5462#ifdef FEAT_LINEBREAK
5463 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005464#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005465#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005466 fill_culopt_flags(NULL, wp);
5467 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005468#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005469 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472/*
5473 * Copy the options from one winopt_T to another.
5474 * Doesn't free the old option values in "to", use clear_winopt() for that.
5475 * The 'scroll' option is not copied, because it depends on the window height.
5476 * The 'previewwindow' option is reset, there can be only one preview window.
5477 */
5478 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005479copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480{
5481#ifdef FEAT_ARABIC
5482 to->wo_arab = from->wo_arab;
5483#endif
5484 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005485 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005487 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005488#ifdef FEAT_LINEBREAK
5489 to->wo_nuw = from->wo_nuw;
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491#ifdef FEAT_RIGHTLEFT
5492 to->wo_rl = from->wo_rl;
5493 to->wo_rlc = vim_strsave(from->wo_rlc);
5494#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005495#ifdef FEAT_LINEBREAK
5496 to->wo_sbr = vim_strsave(from->wo_sbr);
5497#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005498#ifdef FEAT_STL_OPT
5499 to->wo_stl = vim_strsave(from->wo_stl);
5500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005502#ifdef FEAT_DIFF
5503 to->wo_wrap_save = from->wo_wrap_save;
5504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505#ifdef FEAT_LINEBREAK
5506 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005507 to->wo_bri = from->wo_bri;
5508 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005510 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005512 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005513 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005514 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005515#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005516 to->wo_spell = from->wo_spell;
5517#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005518#ifdef FEAT_SYN_HL
5519 to->wo_cuc = from->wo_cuc;
5520 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005521 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005522 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#ifdef FEAT_DIFF
5525 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005526 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005528#ifdef FEAT_CONCEAL
5529 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005530 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005531#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005532#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005533 to->wo_twk = vim_strsave(from->wo_twk);
5534 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536#ifdef FEAT_FOLDING
5537 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005538 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005540 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 to->wo_fdi = vim_strsave(from->wo_fdi);
5542 to->wo_fml = from->wo_fml;
5543 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005544 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005546 to->wo_fdm_save = from->wo_diff_saved
5547 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 to->wo_fdn = from->wo_fdn;
5549# ifdef FEAT_EVAL
5550 to->wo_fde = vim_strsave(from->wo_fde);
5551 to->wo_fdt = vim_strsave(from->wo_fdt);
5552# endif
5553 to->wo_fmr = vim_strsave(from->wo_fmr);
5554#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005555#ifdef FEAT_SIGNS
5556 to->wo_scl = vim_strsave(from->wo_scl);
5557#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005558
5559#ifdef FEAT_EVAL
5560 // Copy the script context so that we know where the value was last set.
5561 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5562 sizeof(to->wo_script_ctx));
5563#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005564 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565}
5566
5567/*
5568 * Check string options in a window for a NULL value.
5569 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005571check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
5573 check_winopt(&win->w_onebuf_opt);
5574 check_winopt(&win->w_allbuf_opt);
5575}
5576
5577/*
5578 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5579 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005580 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583#ifdef FEAT_FOLDING
5584 check_string_option(&wop->wo_fdi);
5585 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005586 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587# ifdef FEAT_EVAL
5588 check_string_option(&wop->wo_fde);
5589 check_string_option(&wop->wo_fdt);
5590# endif
5591 check_string_option(&wop->wo_fmr);
5592#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005593#ifdef FEAT_SIGNS
5594 check_string_option(&wop->wo_scl);
5595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596#ifdef FEAT_RIGHTLEFT
5597 check_string_option(&wop->wo_rlc);
5598#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005599#ifdef FEAT_LINEBREAK
5600 check_string_option(&wop->wo_sbr);
5601#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005602#ifdef FEAT_STL_OPT
5603 check_string_option(&wop->wo_stl);
5604#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005605#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005606 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005607 check_string_option(&wop->wo_cc);
5608#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005609#ifdef FEAT_CONCEAL
5610 check_string_option(&wop->wo_cocu);
5611#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005612#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005613 check_string_option(&wop->wo_twk);
5614 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005615#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005616#ifdef FEAT_LINEBREAK
5617 check_string_option(&wop->wo_briopt);
5618#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005619 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005620 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
5622
5623/*
5624 * Free the allocated memory inside a winopt_T.
5625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005627clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629#ifdef FEAT_FOLDING
5630 clear_string_option(&wop->wo_fdi);
5631 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005632 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633# ifdef FEAT_EVAL
5634 clear_string_option(&wop->wo_fde);
5635 clear_string_option(&wop->wo_fdt);
5636# endif
5637 clear_string_option(&wop->wo_fmr);
5638#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005639#ifdef FEAT_SIGNS
5640 clear_string_option(&wop->wo_scl);
5641#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005642#ifdef FEAT_LINEBREAK
5643 clear_string_option(&wop->wo_briopt);
5644#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005645 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646#ifdef FEAT_RIGHTLEFT
5647 clear_string_option(&wop->wo_rlc);
5648#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005649#ifdef FEAT_LINEBREAK
5650 clear_string_option(&wop->wo_sbr);
5651#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005652#ifdef FEAT_STL_OPT
5653 clear_string_option(&wop->wo_stl);
5654#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005655#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005656 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005657 clear_string_option(&wop->wo_cc);
5658#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005659#ifdef FEAT_CONCEAL
5660 clear_string_option(&wop->wo_cocu);
5661#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005662#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005663 clear_string_option(&wop->wo_twk);
5664 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005665#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005666 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667}
5668
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005669#ifdef FEAT_EVAL
5670// Index into the options table for a buffer-local option enum.
5671static int buf_opt_idx[BV_COUNT];
5672# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5673
5674/*
5675 * Initialize buf_opt_idx[] if not done already.
5676 */
5677 static void
5678init_buf_opt_idx(void)
5679{
5680 static int did_init_buf_opt_idx = FALSE;
5681 int i;
5682
5683 if (did_init_buf_opt_idx)
5684 return;
5685 did_init_buf_opt_idx = TRUE;
5686 for (i = 0; !istermoption_idx(i); i++)
5687 if (options[i].indir & PV_BUF)
5688 buf_opt_idx[options[i].indir & PV_MASK] = i;
5689}
5690#else
5691# define COPY_OPT_SCTX(buf, bv)
5692#endif
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694/*
5695 * Copy global option values to local options for one buffer.
5696 * Used when creating a new buffer and sometimes when entering a buffer.
5697 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005698 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5700 * appropriate.
5701 * BCO_NOHELP Don't copy the values to a help buffer.
5702 */
5703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005704buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
5706 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005707 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 int dont_do_help;
5709 int did_isk = FALSE;
5710
5711 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 * Skip this when the option defaults have not been set yet. Happens when
5713 * main() allocates the first buffer.
5714 */
5715 if (p_cpo != NULL)
5716 {
5717 /*
5718 * Always copy when entering and 'cpo' contains 'S'.
5719 * Don't copy when already initialized.
5720 * Don't copy when 'cpo' contains 's' and not entering.
5721 * 'S' BCO_ENTER initialized 's' should_copy
5722 * yes yes X X TRUE
5723 * yes no yes X FALSE
5724 * no X yes X FALSE
5725 * X no no yes FALSE
5726 * X no no no TRUE
5727 * no yes no X TRUE
5728 */
5729 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5730 && (buf->b_p_initialized
5731 || (!(flags & BCO_ENTER)
5732 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5733 should_copy = FALSE;
5734
5735 if (should_copy || (flags & BCO_ALWAYS))
5736 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005737#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005738 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005739 init_buf_opt_idx();
5740#endif
5741 // Don't copy the options specific to a help buffer when
5742 // BCO_NOHELP is given or the options were initialized already
5743 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5745 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005746 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 save_p_isk = buf->b_p_isk;
5749 buf->b_p_isk = NULL;
5750 }
5751 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005752 * Always free the allocated strings. If not already initialized,
5753 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 */
5755 if (!buf->b_p_initialized)
5756 {
5757 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005758 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005761 switch (*p_ffs)
5762 {
5763 case 'm':
5764 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5765 case 'd':
5766 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5767 case 'u':
5768 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5769 default:
5770 buf->b_p_ff = vim_strsave(p_ff);
5771 }
5772 if (buf->b_p_ff != NULL)
5773 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 buf->b_p_bh = empty_option;
5775 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
5777 else
5778 free_buf_options(buf, FALSE);
5779
5780 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005781 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 buf->b_p_ai_nopaste = p_ai_nopaste;
5783 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005784 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005786 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 buf->b_p_tw_nopaste = p_tw_nopaste;
5788 buf->b_p_tw_nobin = p_tw_nobin;
5789 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005790 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 buf->b_p_wm_nopaste = p_wm_nopaste;
5792 buf->b_p_wm_nobin = p_wm_nobin;
5793 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005794 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005795 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005796 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005797 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005798 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005800 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005802 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005804 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 buf->b_p_ml_nobin = p_ml_nobin;
5806 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005807 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005808 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005809 buf->b_p_swf = FALSE;
5810 else
5811 {
5812 buf->b_p_swf = p_swf;
5813 COPY_OPT_SCTX(buf, BV_INF);
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005816 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005817#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005818 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005819 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005821#ifdef FEAT_COMPL_FUNC
5822 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005823 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005824 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005825 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005826#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005827#ifdef FEAT_EVAL
5828 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005829 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005832 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005834#ifdef FEAT_VARTABS
5835 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005836 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005837 if (p_vsts && p_vsts != empty_option)
5838 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5839 else
5840 buf->b_p_vsts_array = 0;
5841 buf->b_p_vsts_nopaste = p_vsts_nopaste
5842 ? vim_strsave(p_vsts_nopaste) : NULL;
5843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005845 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005847 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#ifdef FEAT_FOLDING
5849 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005850 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#endif
5852 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005853 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005854 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005855 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005856 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5857 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005859 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005861 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#ifdef FEAT_SMARTINDENT
5863 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005864 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#endif
5866 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005867 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868#ifdef FEAT_CINDENT
5869 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005870 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005872 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005874 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005876 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005879 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5881 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005882 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883#endif
5884#ifdef FEAT_LISP
5885 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005886 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#endif
5888#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005889 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005891 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005892 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005893 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005894#endif
5895#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005896 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005897 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005898 (void)compile_cap_prog(&buf->b_s);
5899 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005901 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005902 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005903 buf->b_s.b_p_spo = vim_strsave(p_spo);
5904 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#endif
5906#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5907 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005908 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005910 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005912 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005913#if defined(FEAT_EVAL)
5914 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005915 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917#ifdef FEAT_CRYPT
5918 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005919 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920#endif
5921#ifdef FEAT_SEARCHPATH
5922 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005923 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924#endif
5925#ifdef FEAT_KEYMAP
5926 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005927 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 buf->b_kmap_state |= KEYMAP_INIT;
5929#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005930#ifdef FEAT_TERMINAL
5931 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005933#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005934 // This isn't really an option, but copying the langmap and IME
5935 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005937 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005939 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005941 // options that are normally global but also have a local value
5942 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005944 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005945 buf->b_p_bkc = empty_option;
5946 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947#ifdef FEAT_QUICKFIX
5948 buf->b_p_gp = empty_option;
5949 buf->b_p_mp = empty_option;
5950 buf->b_p_efm = empty_option;
5951#endif
5952 buf->b_p_ep = empty_option;
5953 buf->b_p_kp = empty_option;
5954 buf->b_p_path = empty_option;
5955 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005956 buf->b_p_tc = empty_option;
5957 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#ifdef FEAT_FIND_ID
5959 buf->b_p_def = empty_option;
5960 buf->b_p_inc = empty_option;
5961# ifdef FEAT_EVAL
5962 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964# endif
5965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 buf->b_p_dict = empty_option;
5967 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005968#ifdef FEAT_TEXTOBJ
5969 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005971#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005972#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5973 buf->b_p_bexpr = empty_option;
5974#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005975#if defined(FEAT_CRYPT)
5976 buf->b_p_cm = empty_option;
5977#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005978#ifdef FEAT_PERSISTENT_UNDO
5979 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005980 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005981#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005982#ifdef FEAT_LISP
5983 buf->b_p_lw = empty_option;
5984#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005985 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 /*
5988 * Don't copy the options set by ex_help(), use the saved values,
5989 * when going from a help buffer to a non-help buffer.
5990 * Don't touch these at all when BCO_NOHELP is used and going from
5991 * or to a help buffer.
5992 */
5993 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005996#ifdef FEAT_VARTABS
5997 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5998 tabstop_set(p_vts, &buf->b_p_vts_array);
5999 else
6000 buf->b_p_vts_array = NULL;
6001#endif
6002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 else
6004 {
6005 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006006 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 did_isk = TRUE;
6008 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006009#ifdef FEAT_VARTABS
6010 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006011 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006012 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6013 tabstop_set(p_vts, &buf->b_p_vts_array);
6014 else
6015 buf->b_p_vts_array = NULL;
6016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 if (buf->b_p_bt[0] == 'h')
6019 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 }
6023 }
6024
6025 /*
6026 * When the options should be copied (ignoring BCO_ALWAYS), set the
6027 * flag that indicates that the options have been initialized.
6028 */
6029 if (should_copy)
6030 buf->b_p_initialized = TRUE;
6031 }
6032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006033 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (did_isk)
6035 (void)buf_init_chartab(buf, FALSE);
6036}
6037
6038/*
6039 * Reset the 'modifiable' option and its default value.
6040 */
6041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006042reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 int opt_idx;
6045
6046 curbuf->b_p_ma = FALSE;
6047 p_ma = FALSE;
6048 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006049 if (opt_idx >= 0)
6050 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051}
6052
6053/*
6054 * Set the global value for 'iminsert' to the local value.
6055 */
6056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006057set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 p_iminsert = curbuf->b_p_iminsert;
6060}
6061
6062/*
6063 * Set the global value for 'imsearch' to the local value.
6064 */
6065 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006066set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
6068 p_imsearch = curbuf->b_p_imsearch;
6069}
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071static int expand_option_idx = -1;
6072static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6073static int expand_option_flags = 0;
6074
6075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006076set_context_in_set_cmd(
6077 expand_T *xp,
6078 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006079 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006082 long_u flags = 0; // init for GCC
6083 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 char_u *p;
6085 char_u *s;
6086 int is_term_option = FALSE;
6087 int key;
6088
6089 expand_option_flags = opt_flags;
6090
6091 xp->xp_context = EXPAND_SETTINGS;
6092 if (*arg == NUL)
6093 {
6094 xp->xp_pattern = arg;
6095 return;
6096 }
6097 p = arg + STRLEN(arg) - 1;
6098 if (*p == ' ' && *(p - 1) != '\\')
6099 {
6100 xp->xp_pattern = p + 1;
6101 return;
6102 }
6103 while (p > arg)
6104 {
6105 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (*p == ' ' || *p == ',')
6108 {
6109 while (s > arg && *(s - 1) == '\\')
6110 --s;
6111 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006112 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (*p == ' ' && ((p - s) & 1) == 0)
6114 {
6115 ++p;
6116 break;
6117 }
6118 --p;
6119 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006120 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {
6122 xp->xp_context = EXPAND_BOOL_SETTINGS;
6123 p += 2;
6124 }
6125 if (STRNCMP(p, "inv", 3) == 0)
6126 {
6127 xp->xp_context = EXPAND_BOOL_SETTINGS;
6128 p += 3;
6129 }
6130 xp->xp_pattern = arg = p;
6131 if (*arg == '<')
6132 {
6133 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006134 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 return;
6136 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006137 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
6139 xp->xp_context = EXPAND_NOTHING;
6140 return;
6141 }
6142 nextchar = *++p;
6143 is_term_option = TRUE;
6144 expand_option_name[2] = KEY2TERMCAP0(key);
6145 expand_option_name[3] = KEY2TERMCAP1(key);
6146 }
6147 else
6148 {
6149 if (p[0] == 't' && p[1] == '_')
6150 {
6151 p += 2;
6152 if (*p != NUL)
6153 ++p;
6154 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006155 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 nextchar = *++p;
6157 is_term_option = TRUE;
6158 expand_option_name[2] = p[-2];
6159 expand_option_name[3] = p[-1];
6160 }
6161 else
6162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006163 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6165 p++;
6166 if (*p == NUL)
6167 return;
6168 nextchar = *p;
6169 *p = NUL;
6170 opt_idx = findoption(arg);
6171 *p = nextchar;
6172 if (opt_idx == -1 || options[opt_idx].var == NULL)
6173 {
6174 xp->xp_context = EXPAND_NOTHING;
6175 return;
6176 }
6177 flags = options[opt_idx].flags;
6178 if (flags & P_BOOL)
6179 {
6180 xp->xp_context = EXPAND_NOTHING;
6181 return;
6182 }
6183 }
6184 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006185 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6187 {
6188 ++p;
6189 nextchar = '=';
6190 }
6191 if ((nextchar != '=' && nextchar != ':')
6192 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6193 {
6194 xp->xp_context = EXPAND_UNSUCCESSFUL;
6195 return;
6196 }
6197 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6198 {
6199 xp->xp_context = EXPAND_OLD_SETTING;
6200 if (is_term_option)
6201 expand_option_idx = -1;
6202 else
6203 expand_option_idx = opt_idx;
6204 xp->xp_pattern = p + 1;
6205 return;
6206 }
6207 xp->xp_context = EXPAND_NOTHING;
6208 if (is_term_option || (flags & P_NUM))
6209 return;
6210
6211 xp->xp_pattern = p + 1;
6212
6213 if (flags & P_EXPAND)
6214 {
6215 p = options[opt_idx].var;
6216 if (p == (char_u *)&p_bdir
6217 || p == (char_u *)&p_dir
6218 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006219 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 || p == (char_u *)&p_rtp
6221#ifdef FEAT_SEARCHPATH
6222 || p == (char_u *)&p_cdpath
6223#endif
6224#ifdef FEAT_SESSION
6225 || p == (char_u *)&p_vdir
6226#endif
6227 )
6228 {
6229 xp->xp_context = EXPAND_DIRECTORIES;
6230 if (p == (char_u *)&p_path
6231#ifdef FEAT_SEARCHPATH
6232 || p == (char_u *)&p_cdpath
6233#endif
6234 )
6235 xp->xp_backslash = XP_BS_THREE;
6236 else
6237 xp->xp_backslash = XP_BS_ONE;
6238 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006239 else if (p == (char_u *)&p_ft)
6240 {
6241 xp->xp_context = EXPAND_FILETYPE;
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 else
6244 {
6245 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006246 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (p == (char_u *)&p_tags)
6248 xp->xp_backslash = XP_BS_THREE;
6249 else
6250 xp->xp_backslash = XP_BS_ONE;
6251 }
6252 }
6253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006254 // For an option that is a list of file names, find the start of the
6255 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6257 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006258 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (*p == ' ' || *p == ',')
6260 {
6261 s = p;
6262 while (s > xp->xp_pattern && *(s - 1) == '\\')
6263 --s;
6264 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6265 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6266 {
6267 xp->xp_pattern = p + 1;
6268 break;
6269 }
6270 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006271
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006272#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006273 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006274 if (options[opt_idx].var == (char_u *)&p_sps
6275 && STRNCMP(p, "file:", 5) == 0)
6276 {
6277 xp->xp_pattern = p + 5;
6278 break;
6279 }
6280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
6282
6283 return;
6284}
6285
6286 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006287ExpandSettings(
6288 expand_T *xp,
6289 regmatch_T *regmatch,
6290 int *num_file,
6291 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006293 int num_normal = 0; // Nr of matching non-term-code settings
6294 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 int opt_idx;
6296 int match;
6297 int count = 0;
6298 char_u *str;
6299 int loop;
6300 int is_term_opt;
6301 char_u name_buf[MAX_KEY_NAME_LEN];
6302 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006305 // do this loop twice:
6306 // loop == 0: count the number of matching options
6307 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 for (loop = 0; loop <= 1; ++loop)
6309 {
6310 regmatch->rm_ic = ic;
6311 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6312 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006313 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6314 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6316 {
6317 if (loop == 0)
6318 num_normal++;
6319 else
6320 (*file)[count++] = vim_strsave((char_u *)names[match]);
6321 }
6322 }
6323 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6324 opt_idx++)
6325 {
6326 if (options[opt_idx].var == NULL)
6327 continue;
6328 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6329 && !(options[opt_idx].flags & P_BOOL))
6330 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006331 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 if (is_term_opt && num_normal > 0)
6333 continue;
6334 match = FALSE;
6335 if (vim_regexec(regmatch, str, (colnr_T)0)
6336 || (options[opt_idx].shortname != NULL
6337 && vim_regexec(regmatch,
6338 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6339 match = TRUE;
6340 else if (is_term_opt)
6341 {
6342 name_buf[0] = '<';
6343 name_buf[1] = 't';
6344 name_buf[2] = '_';
6345 name_buf[3] = str[2];
6346 name_buf[4] = str[3];
6347 name_buf[5] = '>';
6348 name_buf[6] = NUL;
6349 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6350 {
6351 match = TRUE;
6352 str = name_buf;
6353 }
6354 }
6355 if (match)
6356 {
6357 if (loop == 0)
6358 {
6359 if (is_term_opt)
6360 num_term++;
6361 else
6362 num_normal++;
6363 }
6364 else
6365 (*file)[count++] = vim_strsave(str);
6366 }
6367 }
6368 /*
6369 * Check terminal key codes, these are not in the option table
6370 */
6371 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6372 {
6373 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6374 {
6375 if (!isprint(str[0]) || !isprint(str[1]))
6376 continue;
6377
6378 name_buf[0] = 't';
6379 name_buf[1] = '_';
6380 name_buf[2] = str[0];
6381 name_buf[3] = str[1];
6382 name_buf[4] = NUL;
6383
6384 match = FALSE;
6385 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6386 match = TRUE;
6387 else
6388 {
6389 name_buf[0] = '<';
6390 name_buf[1] = 't';
6391 name_buf[2] = '_';
6392 name_buf[3] = str[0];
6393 name_buf[4] = str[1];
6394 name_buf[5] = '>';
6395 name_buf[6] = NUL;
6396
6397 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6398 match = TRUE;
6399 }
6400 if (match)
6401 {
6402 if (loop == 0)
6403 num_term++;
6404 else
6405 (*file)[count++] = vim_strsave(name_buf);
6406 }
6407 }
6408
6409 /*
6410 * Check special key names.
6411 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006412 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6414 {
6415 name_buf[0] = '<';
6416 STRCPY(name_buf + 1, str);
6417 STRCAT(name_buf, ">");
6418
6419 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6420 {
6421 if (loop == 0)
6422 num_term++;
6423 else
6424 (*file)[count++] = vim_strsave(name_buf);
6425 }
6426 }
6427 }
6428 if (loop == 0)
6429 {
6430 if (num_normal > 0)
6431 *num_file = num_normal;
6432 else if (num_term > 0)
6433 *num_file = num_term;
6434 else
6435 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006436 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (*file == NULL)
6438 {
6439 *file = (char_u **)"";
6440 return FAIL;
6441 }
6442 }
6443 }
6444 return OK;
6445}
6446
6447 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006448ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006450 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 char_u *buf;
6452
6453 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006454 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 if (*file == NULL)
6456 return FAIL;
6457
6458 /*
6459 * For a terminal key code expand_option_idx is < 0.
6460 */
6461 if (expand_option_idx < 0)
6462 {
6463 var = find_termcode(expand_option_name + 2);
6464 if (var == NULL)
6465 expand_option_idx = findoption(expand_option_name);
6466 }
6467
6468 if (expand_option_idx >= 0)
6469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006470 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 option_value2string(&options[expand_option_idx], expand_option_flags);
6472 var = NameBuff;
6473 }
6474 else if (var == NULL)
6475 var = (char_u *)"";
6476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006477 // A backslash is required before some characters. This is the reverse of
6478 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 buf = vim_strsave_escaped(var, escape_chars);
6480
6481 if (buf == NULL)
6482 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006483 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 return FAIL;
6485 }
6486
6487#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006488 // For MS-Windows et al. we don't double backslashes at the start and
6489 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006490 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (var[0] == '\\' && var[1] == '\\'
6492 && expand_option_idx >= 0
6493 && (options[expand_option_idx].flags & P_EXPAND)
6494 && vim_isfilec(var[2])
6495 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006496 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497#endif
6498
6499 *file[0] = buf;
6500 *num_file = 1;
6501 return OK;
6502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504/*
6505 * Get the value for the numeric or string option *opp in a nice format into
6506 * NameBuff[]. Must not be called with a hidden option!
6507 */
6508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509option_value2string(
6510 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006511 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 char_u *varp;
6514
6515 varp = get_varp_scope(opp, opt_flags);
6516
6517 if (opp->flags & P_NUM)
6518 {
6519 long wc = 0;
6520
6521 if (wc_use_keyname(varp, &wc))
6522 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6523 else if (wc != 0)
6524 STRCPY(NameBuff, transchar((int)wc));
6525 else
6526 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6527 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006528 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
6530 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006531 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 NameBuff[0] = NUL;
6533#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006534 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006535 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 STRCPY(NameBuff, "*****");
6537#endif
6538 else if (opp->flags & P_EXPAND)
6539 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006540 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 else if ((char_u **)opp->var == &p_pt)
6542 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6543 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006544 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546}
6547
6548/*
6549 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6550 * printed as a keyname.
6551 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6552 */
6553 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006554wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6557 {
6558 *wcp = *(long *)varp;
6559 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6560 return TRUE;
6561 }
6562 return FALSE;
6563}
6564
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 * Return TRUE if "x" is present in 'shortmess' option, or
6567 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6568 */
6569 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006570shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006572 return p_shm != NULL &&
6573 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 || (vim_strchr(p_shm, 'a') != NULL
6575 && vim_strchr((char_u *)SHM_A, x) != NULL));
6576}
6577
6578/*
6579 * paste_option_changed() - Called after p_paste was set or reset.
6580 */
6581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006582paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 static int old_p_paste = FALSE;
6585 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006586 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587#ifdef FEAT_CMDL_INFO
6588 static int save_ru = 0;
6589#endif
6590#ifdef FEAT_RIGHTLEFT
6591 static int save_ri = 0;
6592 static int save_hkmap = 0;
6593#endif
6594 buf_T *buf;
6595
6596 if (p_paste)
6597 {
6598 /*
6599 * Paste switched from off to on.
6600 * Save the current values, so they can be restored later.
6601 */
6602 if (!old_p_paste)
6603 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006604 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006605 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 {
6607 buf->b_p_tw_nopaste = buf->b_p_tw;
6608 buf->b_p_wm_nopaste = buf->b_p_wm;
6609 buf->b_p_sts_nopaste = buf->b_p_sts;
6610 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006611 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006612#ifdef FEAT_VARTABS
6613 if (buf->b_p_vsts_nopaste)
6614 vim_free(buf->b_p_vsts_nopaste);
6615 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6616 ? vim_strsave(buf->b_p_vsts) : NULL;
6617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 }
6619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006620 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006622 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623#ifdef FEAT_CMDL_INFO
6624 save_ru = p_ru;
6625#endif
6626#ifdef FEAT_RIGHTLEFT
6627 save_ri = p_ri;
6628 save_hkmap = p_hkmap;
6629#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006630 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006631 p_ai_nopaste = p_ai;
6632 p_et_nopaste = p_et;
6633 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 p_tw_nopaste = p_tw;
6635 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006636#ifdef FEAT_VARTABS
6637 if (p_vsts_nopaste)
6638 vim_free(p_vsts_nopaste);
6639 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 }
6642
6643 /*
6644 * Always set the option values, also when 'paste' is set when it is
6645 * already on.
6646 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006647 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006648 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006650 buf->b_p_tw = 0; // textwidth is 0
6651 buf->b_p_wm = 0; // wrapmargin is 0
6652 buf->b_p_sts = 0; // softtabstop is 0
6653 buf->b_p_ai = 0; // no auto-indent
6654 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006655#ifdef FEAT_VARTABS
6656 if (buf->b_p_vsts)
6657 free_string_option(buf->b_p_vsts);
6658 buf->b_p_vsts = empty_option;
6659 if (buf->b_p_vsts_array)
6660 vim_free(buf->b_p_vsts_array);
6661 buf->b_p_vsts_array = 0;
6662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
6664
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006665 // set global options
6666 p_sm = 0; // no showmatch
6667 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006670 status_redraw_all(); // redraw to remove the ruler
6671 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672#endif
6673#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006674 p_ri = 0; // no reverse insert
6675 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006677 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 p_tw = 0;
6679 p_wm = 0;
6680 p_sts = 0;
6681 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006682#ifdef FEAT_VARTABS
6683 if (p_vsts)
6684 free_string_option(p_vsts);
6685 p_vsts = empty_option;
6686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688
6689 /*
6690 * Paste switched from on to off: Restore saved values.
6691 */
6692 else if (old_p_paste)
6693 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006694 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006695 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 {
6697 buf->b_p_tw = buf->b_p_tw_nopaste;
6698 buf->b_p_wm = buf->b_p_wm_nopaste;
6699 buf->b_p_sts = buf->b_p_sts_nopaste;
6700 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006701 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006702#ifdef FEAT_VARTABS
6703 if (buf->b_p_vsts)
6704 free_string_option(buf->b_p_vsts);
6705 buf->b_p_vsts = buf->b_p_vsts_nopaste
6706 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6707 if (buf->b_p_vsts_array)
6708 vim_free(buf->b_p_vsts_array);
6709 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6710 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6711 else
6712 buf->b_p_vsts_array = 0;
6713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
6715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006716 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006718 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006721 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 p_ru = save_ru;
6723#endif
6724#ifdef FEAT_RIGHTLEFT
6725 p_ri = save_ri;
6726 p_hkmap = save_hkmap;
6727#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006728 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006729 p_ai = p_ai_nopaste;
6730 p_et = p_et_nopaste;
6731 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 p_tw = p_tw_nopaste;
6733 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006734#ifdef FEAT_VARTABS
6735 if (p_vsts)
6736 free_string_option(p_vsts);
6737 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
6740
6741 old_p_paste = p_paste;
6742}
6743
6744/*
6745 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6746 *
6747 * Reset 'compatible' and set the values for options that didn't get set yet
6748 * to the Vim defaults.
6749 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006750 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 */
6752 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006753vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006755 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006756 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006757 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 if (!option_was_set((char_u *)"cp"))
6760 {
6761 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006762 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6764 set_option_default(opt_idx, OPT_FREE, FALSE);
6765 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006766 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006768
6769 if (fname != NULL)
6770 {
6771 p = vim_getenv(envname, &dofree);
6772 if (p == NULL)
6773 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006774 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006775 p = FullName_save(fname, FALSE);
6776 if (p != NULL)
6777 {
6778 vim_setenv(envname, p);
6779 vim_free(p);
6780 }
6781 }
6782 else if (dofree)
6783 vim_free(p);
6784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785}
6786
6787/*
6788 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6789 */
6790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006791change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006793 int opt_idx;
6794
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (p_cp != on)
6796 {
6797 p_cp = on;
6798 compatible_set();
6799 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006800 opt_idx = findoption((char_u *)"cp");
6801 if (opt_idx >= 0)
6802 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803}
6804
6805/*
6806 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006807 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 */
6809 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006810option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
6812 int idx;
6813
6814 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006815 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 return FALSE;
6817 if (options[idx].flags & P_WAS_SET)
6818 return TRUE;
6819 return FALSE;
6820}
6821
6822/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006823 * Reset the flag indicating option "name" was set.
6824 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006826reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006827{
6828 int idx = findoption(name);
6829
6830 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006831 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006832 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006833 return OK;
6834 }
6835 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006836}
6837
6838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 * compatible_set() - Called when 'compatible' has been set or unset.
6840 *
6841 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6842 * flag) to a Vi compatible value.
6843 * When 'compatible' is unset: Set all options that have a different default
6844 * for Vim (without the P_VI_DEF flag) to that default.
6845 */
6846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006847compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 int opt_idx;
6850
Bram Moolenaardac13472019-09-16 21:06:21 +02006851 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6853 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6854 set_option_default(opt_idx, OPT_FREE, p_cp);
6855 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006856 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857}
6858
Bram Moolenaardac13472019-09-16 21:06:21 +02006859#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861/*
6862 * fill_breakat_flags() -- called when 'breakat' changes value.
6863 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006865fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006867 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 int i;
6869
6870 for (i = 0; i < 256; i++)
6871 breakat_flags[i] = FALSE;
6872
6873 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006874 for (p = p_breakat; *p; p++)
6875 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877#endif
6878
6879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 * Check if backspacing over something is allowed.
6881 */
6882 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006883can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006884 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006886#ifdef FEAT_JOB_CHANNEL
6887 if (what == BS_START && bt_prompt(curbuf))
6888 return FALSE;
6889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 switch (*p_bs)
6891 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006892 case '3': return TRUE;
6893 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 case '1': return (what != BS_START);
6895 case '0': return FALSE;
6896 }
6897 return vim_strchr(p_bs, what) != NULL;
6898}
6899
6900/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006901 * Return the effective 'scrolloff' value for the current window, using the
6902 * global value when appropriate.
6903 */
6904 long
6905get_scrolloff_value(void)
6906{
6907 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6908}
6909
6910/*
6911 * Return the effective 'sidescrolloff' value for the current window, using the
6912 * global value when appropriate.
6913 */
6914 long
6915get_sidescrolloff_value(void)
6916{
6917 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6918}
6919
6920/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006921 * Get the local or global value of 'backupcopy'.
6922 */
6923 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006924get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006925{
6926 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6927}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006928
Bram Moolenaaree857022019-11-09 23:26:40 +01006929#if defined(FEAT_LINEBREAK) || defined(PROTO)
6930/*
6931 * Get the local or global value of 'showbreak'.
6932 */
6933 char_u *
6934get_showbreak_value(win_T *win)
6935{
6936 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6937 return p_sbr;
6938 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6939 return empty_option;
6940 return win->w_p_sbr;
6941}
6942#endif
6943
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006944#if defined(FEAT_EVAL) || defined(PROTO)
6945/*
6946 * Get window or buffer local options.
6947 */
6948 dict_T *
6949get_winbuf_options(int bufopt)
6950{
6951 dict_T *d;
6952 int opt_idx;
6953
6954 d = dict_alloc();
6955 if (d == NULL)
6956 return NULL;
6957
Bram Moolenaardac13472019-09-16 21:06:21 +02006958 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006959 {
6960 struct vimoption *opt = &options[opt_idx];
6961
6962 if ((bufopt && (opt->indir & PV_BUF))
6963 || (!bufopt && (opt->indir & PV_WIN)))
6964 {
6965 char_u *varp = get_varp(opt);
6966
6967 if (varp != NULL)
6968 {
6969 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006970 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006971 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006972 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006973 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006974 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006975 }
6976 }
6977 }
6978
6979 return d;
6980}
6981#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006982
Bram Moolenaardac13472019-09-16 21:06:21 +02006983#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006984/*
6985 * This is called when 'culopt' is changed
6986 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006987 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006988fill_culopt_flags(char_u *val, win_T *wp)
6989{
6990 char_u *p;
6991 char_u culopt_flags_new = 0;
6992
6993 if (val == NULL)
6994 p = wp->w_p_culopt;
6995 else
6996 p = val;
6997 while (*p != NUL)
6998 {
6999 if (STRNCMP(p, "line", 4) == 0)
7000 {
7001 p += 4;
7002 culopt_flags_new |= CULOPT_LINE;
7003 }
7004 else if (STRNCMP(p, "both", 4) == 0)
7005 {
7006 p += 4;
7007 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7008 }
7009 else if (STRNCMP(p, "number", 6) == 0)
7010 {
7011 p += 6;
7012 culopt_flags_new |= CULOPT_NBR;
7013 }
7014 else if (STRNCMP(p, "screenline", 10) == 0)
7015 {
7016 p += 10;
7017 culopt_flags_new |= CULOPT_SCRLINE;
7018 }
7019
7020 if (*p != ',' && *p != NUL)
7021 return FAIL;
7022 if (*p == ',')
7023 ++p;
7024 }
7025
7026 // Can't have both "line" and "screenline".
7027 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7028 return FAIL;
7029 wp->w_p_culopt_flags = culopt_flags_new;
7030
7031 return OK;
7032}
7033#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007034
7035/*
7036 * Get the value of 'magic' adjusted for Vim9 script.
7037 */
7038 int
7039magic_isset(void)
7040{
7041 switch (magic_overruled)
7042 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007043 case OPTION_MAGIC_ON: return TRUE;
7044 case OPTION_MAGIC_OFF: return FALSE;
7045 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007046 }
7047#ifdef FEAT_EVAL
7048 if (in_vim9script())
7049 return TRUE;
7050#endif
7051 return p_magic;
7052}