blob: 9db9c2633f15db9a9128fb166805fc4937647518 [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
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100913 // Always use bourne 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
Bram Moolenaar4f974752019-02-17 17:44:42 +0100922# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 || fnamecmp(p, "cmd") == 0
924 || fnamecmp(p, "sh.exe") == 0
925 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200926 || fnamecmp(p, "mksh.exe") == 0
927 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000929 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 || fnamecmp(p, "bash.exe") == 0
931 || fnamecmp(p, "cmd.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100933 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100935# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (do_sp)
937 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100938# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100940# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
944 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (do_srr)
947 {
948 p_srr = (char_u *)">%s 2>&1";
949 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
950 }
951 }
952 vim_free(p);
953 }
954#endif
955
Bram Moolenaar4f974752019-02-17 17:44:42 +0100956#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100958 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
959 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * This is done after other initializations, where 'shell' might have been
961 * set, but only if they have not been set before. Default for p_shcf is
962 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100963 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000965 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 int idx3;
968
969 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000970 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 p_shcf = (char_u *)"-c";
973 options[idx3].def_val[VI_DEFAULT] = p_shcf;
974 }
975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000978 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 p_sxq = (char_u *)"\"";
981 options[idx3].def_val[VI_DEFAULT] = p_sxq;
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100984 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
985 {
986 int idx3;
987
988 /*
989 * cmd.exe on Windows will strip the first and last double quote given
990 * on the command line, e.g. most of the time things like:
991 * cmd /c "my path/to/echo" "my args to echo"
992 * become:
993 * my path/to/echo" "my args to echo
994 * when executed.
995 *
Bram Moolenaar034b1152012-02-19 18:19:30 +0100996 * To avoid this, set shellxquote to surround the command in
997 * parenthesis. This appears to make most commands work, without
998 * breaking commands that worked previously, such as
999 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001000 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001001 idx3 = findoption((char_u *)"sxq");
1002 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1003 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001004 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001005 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1006 }
1007
Bram Moolenaara64ba222012-02-12 23:23:31 +01001008 idx3 = findoption((char_u *)"shcf");
1009 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1010 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001011 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001012 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1013 }
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001017 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001018 {
1019 int idx_ffs = findoption((char_u *)"ffs");
1020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001021 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001022 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1023 set_fileformat(default_fileformat(), OPT_LOCAL);
1024 }
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#ifdef FEAT_TITLE
1027 set_title_defaults();
1028#endif
1029}
1030
1031#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1032/*
1033 * When 'helplang' is still at its default value, set it to "lang".
1034 * Only the first two characters of "lang" are used.
1035 */
1036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001037set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 int idx;
1040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001041 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 return;
1043 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001044 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 if (options[idx].flags & P_ALLOCED)
1047 free_string_option(p_hlg);
1048 p_hlg = vim_strsave(lang);
1049 if (p_hlg == NULL)
1050 p_hlg = empty_option;
1051 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001052 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001053 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001054 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1055 {
1056 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1057 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1058 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001059 // any C like setting, such as C.UTF-8, becomes "en"
1060 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1061 {
1062 p_hlg[0] = 'e';
1063 p_hlg[1] = 'n';
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 options[idx].flags |= P_ALLOCED;
1068 }
1069}
1070#endif
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_TITLE
1073/*
1074 * 'title' and 'icon' only default to true if they have not been set or reset
1075 * in .vimrc and we can read the old value.
1076 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1077 * they can be reset. This reduces startup time when using X on a remote
1078 * machine.
1079 */
1080 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001081set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 int idx1;
1084 long val;
1085
1086 /*
1087 * If GUI is (going to be) used, we can always set the window title and
1088 * icon name. Saves a bit of time, because the X11 display server does
1089 * not need to be contacted.
1090 */
1091 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001092 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
1094#ifdef FEAT_GUI
1095 if (gui.starting || gui.in_use)
1096 val = TRUE;
1097 else
1098#endif
1099 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001100 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 p_title = val;
1102 }
1103 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001104 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106#ifdef FEAT_GUI
1107 if (gui.starting || gui.in_use)
1108 val = TRUE;
1109 else
1110#endif
1111 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001112 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 p_icon = val;
1114 }
1115}
1116#endif
1117
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001118 void
1119ex_set(exarg_T *eap)
1120{
1121 int flags = 0;
1122
1123 if (eap->cmdidx == CMD_setlocal)
1124 flags = OPT_LOCAL;
1125 else if (eap->cmdidx == CMD_setglobal)
1126 flags = OPT_GLOBAL;
1127#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001128 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001129 ex_options(eap);
1130 else
1131#endif
1132 {
1133 if (eap->forceit)
1134 flags |= OPT_ONECOLUMN;
1135 (void)do_set(eap->arg, flags);
1136 }
1137}
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139/*
1140 * Parse 'arg' for option settings.
1141 *
1142 * 'arg' may be IObuff, but only when no errors can be present and option
1143 * does not need to be expanded with option_expand().
1144 * "opt_flags":
1145 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001146 * OPT_GLOBAL for ":setglobal"
1147 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001149 * OPT_WINONLY to only set window-local options
1150 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *
1152 * returns FAIL if an error is detected, OK otherwise
1153 */
1154 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001157 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001160 char *errmsg;
1161 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001163 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1164 int nextchar; // next non-white char after option name
1165 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 int len;
1167 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001168 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 long_u flags; // flags for current option
1171 char_u *varp = NULL; // pointer to variable for current option
1172 int did_show = FALSE; // already showed one value
1173 int adding; // "opt+=arg"
1174 int prepending; // "opt^=arg"
1175 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int cp_val = 0;
1177 char_u key_name[2];
1178
1179 if (*arg == NUL)
1180 {
1181 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001182 did_show = TRUE;
1183 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001186 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
1188 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001191 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1192 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 /*
1195 * ":set all" show all options.
1196 * ":set all&" set all options to their default value.
1197 */
1198 arg += 3;
1199 if (*arg == '&')
1200 {
1201 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001202 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001204 didset_options();
1205 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001206 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001211 did_show = TRUE;
1212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001214 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
1216 showoptions(2, opt_flags);
1217 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001218 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 arg += 7;
1220 }
1221 else
1222 {
1223 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001224 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 prefix = 0;
1227 arg += 2;
1228 }
1229 else if (STRNCMP(arg, "inv", 3) == 0)
1230 {
1231 prefix = 2;
1232 arg += 3;
1233 }
1234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 key = 0;
1237 if (*arg == '<')
1238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1242 len = 5;
1243 else
1244 {
1245 len = 1;
1246 while (arg[len] != NUL && arg[len] != '>')
1247 ++len;
1248 }
1249 if (arg[len] != '>')
1250 {
1251 errmsg = e_invarg;
1252 goto skip;
1253 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 arg[len] = NUL; // put NUL after name
1255 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001259 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 else
1262 {
1263 len = 0;
1264 /*
1265 * The two characters after "t_" may not be alphanumeric.
1266 */
1267 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1268 len = 4;
1269 else
1270 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1271 ++len;
1272 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001273 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001277 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001280 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 afterchar = arg[len];
1282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001284 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 ++len;
1286
1287 adding = FALSE;
1288 prepending = FALSE;
1289 removing = FALSE;
1290 if (arg[len] != NUL && arg[len + 1] == '=')
1291 {
1292 if (arg[len] == '+')
1293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001294 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ++len;
1296 }
1297 else if (arg[len] == '^')
1298 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001299 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 ++len;
1301 }
1302 else if (arg[len] == '-')
1303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001304 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ++len;
1306 }
1307 }
1308 nextchar = arg[len];
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001312 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 goto skip;
1314 }
1315
1316 if (opt_idx >= 0)
1317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001318 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 // Only give an error message when requesting the value of
1321 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1323 && (!(options[opt_idx].flags & P_BOOL)
1324 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001325 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 goto skip;
1327 }
1328
1329 flags = options[opt_idx].flags;
1330 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1331 }
1332 else
1333 {
1334 flags = P_STRING;
1335 if (key < 0)
1336 {
1337 key_name[0] = KEY2TERMCAP0(key);
1338 key_name[1] = KEY2TERMCAP1(key);
1339 }
1340 else
1341 {
1342 key_name[0] = KS_KEY;
1343 key_name[1] = (key & 0xff);
1344 }
1345 }
1346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001347 // Skip all options that are not window-local (used when showing
1348 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001349 if ((opt_flags & OPT_WINONLY)
1350 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1351 goto skip;
1352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001353 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001354 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1355 && options[opt_idx].var == VAR_WIN)
1356 goto skip;
1357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001358 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001359 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001361 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001362 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001363 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001364 goto skip;
1365 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001366 if ((flags & P_MLE) && !p_mle)
1367 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001368 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001369 goto skip;
1370 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001371#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001372 // In diff mode some options are overruled. This avoids that
1373 // 'foldmethod' becomes "marker" instead of "diff" and that
1374 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001375 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001376 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001377 && (
1378#ifdef FEAT_FOLDING
1379 options[opt_idx].indir == PV_FDM ||
1380#endif
1381 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001382 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
1386#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001387 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001390 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 goto skip;
1392 }
1393#endif
1394
1395 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1396 {
1397 arg += len;
1398 cp_val = p_cp;
1399 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1400 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
1403 cp_val = FALSE;
1404 arg += 3;
1405 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
1408 cp_val = TRUE;
1409 arg += 2;
1410 }
1411 }
1412 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001413 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
1415 errmsg = e_trailing;
1416 goto skip;
1417 }
1418 }
1419
1420 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001421 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001422 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
1424 if (nextchar == '?'
1425 || (prefix == 1
1426 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1427 && !(flags & P_BOOL)))
1428 {
1429 /*
1430 * print value
1431 */
1432 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001433 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else
1435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 gotocmdline(TRUE); // cursor at status line
1437 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 if (opt_idx >= 0)
1440 {
1441 showoneopt(&options[opt_idx], opt_flags);
1442#ifdef FEAT_EVAL
1443 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001445 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001446 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001447 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001448 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001449 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001450 (int)options[opt_idx].indir & PV_MASK]);
1451 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001452 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001453 (int)options[opt_idx].indir & PV_MASK]);
1454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
1456 }
1457 else
1458 {
1459 char_u *p;
1460
1461 p = find_termcode(key_name);
1462 if (p == NULL)
1463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001464 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 goto skip;
1466 }
1467 else
1468 (void)show_one_termcode(key_name, p, TRUE);
1469 }
1470 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001471 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 errmsg = e_trailing;
1473 }
1474 else
1475 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001476 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001477 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001479 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 if (nextchar == '=' || nextchar == ':')
1482 {
1483 errmsg = e_invarg;
1484 goto skip;
1485 }
1486
1487 /*
1488 * ":set opt!": invert
1489 * ":set opt&": reset to default value
1490 * ":set opt<": reset to global value
1491 */
1492 if (nextchar == '!')
1493 value = *(int *)(varp) ^ 1;
1494 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001495 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ((flags & P_VI_DEF) || cp_val)
1497 ? VI_DEFAULT : VIM_DEFAULT];
1498 else if (nextchar == '<')
1499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001500 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if ((int *)varp == &curbuf->b_p_ar
1502 && opt_flags == OPT_LOCAL)
1503 value = -1;
1504 else
1505 value = *(int *)get_varp_scope(&(options[opt_idx]),
1506 OPT_GLOBAL);
1507 }
1508 else
1509 {
1510 /*
1511 * ":set invopt": invert
1512 * ":set opt" or ":set noopt": set or reset
1513 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001514 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
1516 errmsg = e_trailing;
1517 goto skip;
1518 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001519 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 value = *(int *)(varp) ^ 1;
1521 else
1522 value = prefix;
1523 }
1524
1525 errmsg = set_bool_option(opt_idx, varp, (int)value,
1526 opt_flags);
1527 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1531 || prefix != 1)
1532 {
1533 errmsg = e_invarg;
1534 goto skip;
1535 }
1536
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001537 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
1539 /*
1540 * Different ways to set a number option:
1541 * & set to default value
1542 * < set to global value
1543 * <xx> accept special key codes for 'wildchar'
1544 * c accept any non-digit for 'wildchar'
1545 * [-]0-9 set number
1546 * other error
1547 */
1548 ++arg;
1549 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001550 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 ((flags & P_VI_DEF) || cp_val)
1552 ? VI_DEFAULT : VIM_DEFAULT];
1553 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001554 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001555 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1556 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001557 if ((long *)varp == &curbuf->b_p_ul
1558 && opt_flags == OPT_LOCAL)
1559 value = NO_LOCAL_UNDOLEVEL;
1560 else
1561 value = *(long *)get_varp_scope(
1562 &(options[opt_idx]), OPT_GLOBAL);
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 else if (((long *)varp == &p_wc
1565 || (long *)varp == &p_wcm)
1566 && (*arg == '<'
1567 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001568 || (*arg != NUL
1569 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 && !VIM_ISDIGIT(*arg))))
1571 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001572 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (value == 0 && (long *)varp != &p_wcm)
1574 {
1575 errmsg = e_invarg;
1576 goto skip;
1577 }
1578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1580 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581 // Allow negative (for 'undolevels'), octal and
1582 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001583 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001584 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001585 if (i == 0 || (arg[i] != NUL
1586 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001588 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 goto skip;
1590 }
1591 }
1592 else
1593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 goto skip;
1596 }
1597
1598 if (adding)
1599 value = *(long *)varp + value;
1600 if (prepending)
1601 value = *(long *)varp * value;
1602 if (removing)
1603 value = *(long *)varp - value;
1604 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001605 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001609 char_u *save_arg = NULL;
1610 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001611 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001612 char_u *newval;
1613 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001614 char_u *origval_l = NULL;
1615 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001616#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001617 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 char_u *saved_origval_l = NULL;
1619 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001620 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001621#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001622 unsigned newlen;
1623 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001624 int new_value_alloced; // new string option
1625 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 // When using ":set opt=val" for a global option
1628 // with a local value the local value will be
1629 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001631 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 varp = options[opt_idx].var;
1633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 // The old value is kept until we are sure that the
1635 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001637
Bram Moolenaard7c96872019-06-15 17:12:48 +02001638 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1639 {
1640 origval_l = *(char_u **)get_varp_scope(
1641 &(options[opt_idx]), OPT_LOCAL);
1642 origval_g = *(char_u **)get_varp_scope(
1643 &(options[opt_idx]), OPT_GLOBAL);
1644
1645 // A global-local string option might have an empty
1646 // option as value to indicate that the global
1647 // value should be used.
1648 if (((int)options[opt_idx].indir & PV_BOTH)
1649 && origval_l == empty_option)
1650 origval_l = origval_g;
1651 }
1652
1653 // When setting the local value of a global
1654 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001655 if (((int)options[opt_idx].indir & PV_BOTH)
1656 && (opt_flags & OPT_LOCAL))
1657 origval = *(char_u **)get_varp(
1658 &options[opt_idx]);
1659 else
1660 origval = oldval;
1661
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001662 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 newval = options[opt_idx].def_val[
1665 ((flags & P_VI_DEF) || cp_val)
1666 ? VI_DEFAULT : VIM_DEFAULT];
1667 if ((char_u **)varp == &p_bg)
1668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#ifdef FEAT_GUI
1671 if (gui.in_use)
1672 newval = gui_bg_default();
1673 else
1674#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001675 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001678 // expand environment variables and ~ (since the
1679 // default value was already expanded, only
1680 // required when an environment variable was set
1681 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (newval == NULL)
1683 newval = empty_option;
1684 else
1685 {
1686 s = option_expand(opt_idx, newval);
1687 if (s == NULL)
1688 s = newval;
1689 newval = vim_strsave(s);
1690 }
1691 new_value_alloced = TRUE;
1692 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001693 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 newval = vim_strsave(*(char_u **)get_varp_scope(
1696 &(options[opt_idx]), OPT_GLOBAL));
1697 new_value_alloced = TRUE;
1698 }
1699 else
1700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001701 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 /*
1704 * Set 'keywordprg' to ":help" if an empty
1705 * value was passed to :set by the user.
1706 * Misuse errbuf[] for the resulting string.
1707 */
1708 if (varp == (char_u *)&p_kp
1709 && (*arg == NUL || *arg == ' '))
1710 {
1711 STRCPY(errbuf, ":help");
1712 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001713 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001716 * Convert 'backspace' number to string, for
1717 * adding, prepending and removing string.
1718 */
1719 else if (varp == (char_u *)&p_bs
1720 && VIM_ISDIGIT(**(char_u **)varp))
1721 {
1722 i = getdigits((char_u **)varp);
1723 switch (i)
1724 {
1725 case 0:
1726 *(char_u **)varp = empty_option;
1727 break;
1728 case 1:
1729 *(char_u **)varp = vim_strsave(
1730 (char_u *)"indent,eol");
1731 break;
1732 case 2:
1733 *(char_u **)varp = vim_strsave(
1734 (char_u *)"indent,eol,start");
1735 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001736 case 3:
1737 *(char_u **)varp = vim_strsave(
1738 (char_u *)"indent,eol,nostop");
1739 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001740 }
1741 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001742 if (origval == oldval)
1743 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001744 if (origval_l == oldval)
1745 origval_l = *(char_u **)varp;
1746 if (origval_g == oldval)
1747 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001748 oldval = *(char_u **)varp;
1749 }
1750 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * Convert 'whichwrap' number to string, for
1752 * backwards compatibility with Vim 3.0.
1753 * Misuse errbuf[] for the resulting string.
1754 */
1755 else if (varp == (char_u *)&p_ww
1756 && VIM_ISDIGIT(*arg))
1757 {
1758 *errbuf = NUL;
1759 i = getdigits(&arg);
1760 if (i & 1)
1761 STRCAT(errbuf, "b,");
1762 if (i & 2)
1763 STRCAT(errbuf, "s,");
1764 if (i & 4)
1765 STRCAT(errbuf, "h,l,");
1766 if (i & 8)
1767 STRCAT(errbuf, "<,>,");
1768 if (i & 16)
1769 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001770 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 errbuf[STRLEN(errbuf) - 1] = NUL;
1772 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001773 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775 /*
1776 * Remove '>' before 'dir' and 'bdir', for
1777 * backwards compatibility with version 3.0
1778 */
1779 else if ( *arg == '>'
1780 && (varp == (char_u *)&p_dir
1781 || varp == (char_u *)&p_bdir))
1782 {
1783 ++arg;
1784 }
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
1787 * Copy the new string into allocated memory.
1788 * Can't use set_string_option_direct(), because
1789 * we need to remove the backslashes.
1790 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001791 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 newlen = (unsigned)STRLEN(arg) + 1;
1793 if (adding || prepending || removing)
1794 newlen += (unsigned)STRLEN(origval) + 1;
1795 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001796 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 break;
1798 s = newval;
1799
1800 /*
1801 * Copy the string, skip over escaped chars.
1802 * For MS-DOS and WIN32 backslashes before normal
1803 * file name characters are not removed, and keep
1804 * backslash at start, for "\\machine\path", but
1805 * do remove it for "\\\\machine\\path".
1806 * The reverse is found in ExpandOldSetting().
1807 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001808 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 if (*arg == '\\' && arg[1] != NUL
1811#ifdef BACKSLASH_IN_FILENAME
1812 && !((flags & P_EXPAND)
1813 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001814 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 && (arg[1] != '\\'
1816 || (s == newval
1817 && arg[2] != '\\')))
1818#endif
1819 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001822 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001824 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 mch_memmove(s, arg, (size_t)i);
1826 arg += i;
1827 s += i;
1828 }
1829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 *s++ = *arg++;
1831 }
1832 *s = NUL;
1833
1834 /*
1835 * Expand environment variables and ~.
1836 * Don't do it when adding without inserting a
1837 * comma.
1838 */
1839 if (!(adding || prepending || removing)
1840 || (flags & P_COMMA))
1841 {
1842 s = option_expand(opt_idx, newval);
1843 if (s != NULL)
1844 {
1845 vim_free(newval);
1846 newlen = (unsigned)STRLEN(s) + 1;
1847 if (adding || prepending || removing)
1848 newlen += (unsigned)STRLEN(origval) + 1;
1849 newval = alloc(newlen);
1850 if (newval == NULL)
1851 break;
1852 STRCPY(newval, s);
1853 }
1854 }
1855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001856 // locate newval[] in origval[] when removing it
1857 // and when adding to avoid duplicates
1858 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (removing || (flags & P_NODUP))
1860 {
1861 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001862 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001865 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
1867 prepending = FALSE;
1868 adding = FALSE;
1869 STRCPY(newval, origval);
1870 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001871
1872 // if no duplicate, move pointer to end of
1873 // original value
1874 if (s == NULL)
1875 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001878 // concatenate the two strings; add a ',' if
1879 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (adding || prepending)
1881 {
1882 comma = ((flags & P_COMMA) && *origval != NUL
1883 && *newval != NUL);
1884 if (adding)
1885 {
1886 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001887 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001888 if (comma && i > 1
1889 && (flags & P_ONECOMMA) == P_ONECOMMA
1890 && origval[i - 1] == ','
1891 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001892 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 mch_memmove(newval + i + comma, newval,
1894 STRLEN(newval) + 1);
1895 mch_memmove(newval, origval, (size_t)i);
1896 }
1897 else
1898 {
1899 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001900 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 if (comma)
1903 newval[i] = ',';
1904 }
1905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001906 // Remove newval[] from origval[]. (Note: "i" has
1907 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (removing)
1909 {
1910 STRCPY(newval, origval);
1911 if (*s)
1912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001913 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (flags & P_COMMA)
1915 {
1916 if (s == origval)
1917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (s[i] == ',')
1920 ++i;
1921 }
1922 else
1923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001924 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 --s;
1926 ++i;
1927 }
1928 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001929 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 }
1932
1933 if (flags & P_FLAGLIST)
1934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001935 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001936 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001938 // if options have P_FLAGLIST and
1939 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001940 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001942 if (*s != ',' && *(s + 1) == ','
1943 && vim_strchr(s + 2, *s) != NULL)
1944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 // Remove the duplicated value and
1946 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001947 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001948 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001951 else
1952 {
1953 if ((!(flags & P_COMMA) || *s != ',')
1954 && vim_strchr(s + 1, *s) != NULL)
1955 {
1956 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001957 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001958 }
1959 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001960 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 arg = save_arg;
1966 new_value_alloced = TRUE;
1967 }
1968
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001969 /*
1970 * Set the new value.
1971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 *(char_u **)(varp) = newval;
1973
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001974#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001975 if (!starting
1976# ifdef FEAT_CRYPT
1977 && options[opt_idx].indir != PV_KEY
1978# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001979 && origval != NULL && newval != NULL)
1980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001981 // origval may be freed by
1982 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02001983 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // newval (and varp) may become invalid if the
1985 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001986 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001987 if (origval_l != NULL)
1988 saved_origval_l = vim_strsave(origval_l);
1989 if (origval_g != NULL)
1990 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001991 }
Bram Moolenaar53744302015-07-17 17:38:22 +02001992#endif
1993
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001994 {
1995 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001996 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001997
1998 // When an option is set in the sandbox, from a
1999 // modeline or in secure mode, then deal with side
2000 // effects in secure mode. Also when the value was
2001 // set with the P_INSECURE flag and is not
2002 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002003 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002004#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002005 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002006#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002007 || (!value_is_replaced && (*p & P_INSECURE)))
2008 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002009
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002010 // Handle side effects, and set the global value
2011 // for ":set" on local options. Note: when setting
2012 // 'syntax' or 'filetype' autocommands may be
2013 // triggered that can cause havoc.
2014 errmsg = did_set_string_option(
2015 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002016 new_value_alloced, oldval, errbuf,
2017 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002018
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002019 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002022#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002023 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002024 trigger_optionsset_string(
2025 opt_idx, opt_flags, saved_origval,
2026 saved_origval_l, saved_origval_g,
2027 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002028 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002029 vim_free(saved_origval_l);
2030 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002031 vim_free(saved_newval);
2032#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002033 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (errmsg != NULL)
2035 goto skip;
2036 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 char_u *p;
2040
2041 if (nextchar == '&')
2042 {
2043 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002044 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 else
2047 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002048 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002049 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (*p == '\\' && p[1] != NUL)
2051 ++p;
2052 nextchar = *p;
2053 *p = NUL;
2054 add_termcode(key_name, arg, FALSE);
2055 *p = nextchar;
2056 }
2057 if (full_screen)
2058 ttest(FALSE);
2059 redraw_all_later(CLEAR);
2060 }
2061 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002064 did_set_option(
2065 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067
2068skip:
2069 /*
2070 * Advance to next argument.
2071 * - skip until a blank found, taking care of backslashes
2072 * - skip blanks
2073 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2074 */
2075 for (i = 0; i < 2 ; ++i)
2076 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002077 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (*arg++ == '\\' && *arg != NUL)
2079 ++arg;
2080 arg = skipwhite(arg);
2081 if (*arg != '=')
2082 break;
2083 }
2084 }
2085
2086 if (errmsg != NULL)
2087 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002088 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002089 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (i + (arg - startarg) < IOSIZE)
2091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 STRCAT(IObuff, ": ");
2094 mch_memmove(IObuff + i, startarg, (arg - startarg));
2095 IObuff[i + (arg - startarg)] = NUL;
2096 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002097 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 trans_characters(IObuff, IOSIZE);
2099
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 ++no_wait_return; // wait_return done later
2101 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 --no_wait_return;
2103
2104 return FAIL;
2105 }
2106
2107 arg = skipwhite(arg);
2108 }
2109
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110theend:
2111 if (silent_mode && did_show)
2112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002113 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002114 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002115 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002116 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002117 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002118 out_flush();
2119 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002120 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002121 }
2122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 return OK;
2124}
2125
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002126/*
2127 * Call this when an option has been given a new value through a user command.
2128 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2129 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131did_set_option(
2132 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002133 int opt_flags, // possibly with OPT_MODELINE
2134 int new_value, // value was replaced completely
2135 int value_checked) // value was checked to be safe, no need to set the
2136 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002137{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002138 long_u *p;
2139
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002140 options[opt_idx].flags |= P_WAS_SET;
2141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002142 // When an option is set in the sandbox, from a modeline or in secure mode
2143 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2144 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002145 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002146 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002147#ifdef HAVE_SANDBOX
2148 || sandbox != 0
2149#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002150 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002151 *p = *p | P_INSECURE;
2152 else if (new_value)
2153 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002154}
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156/*
2157 * Convert a key name or string into a key value.
2158 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002159 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002161 int
2162string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
2164 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002165 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (*arg == '^')
2167 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002168 if (multi_byte)
2169 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 return *arg;
2171}
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#ifdef FEAT_TITLE
2174/*
2175 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2176 * maketitle() to create and display it.
2177 * When switching the title or icon off, call mch_restore_title() to get
2178 * the old value back.
2179 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002180 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002181did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 if (starting != NO_SCREEN
2184#ifdef FEAT_GUI
2185 && !gui.starting
2186#endif
2187 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189}
2190#endif
2191
2192/*
2193 * set_options_bin - called when 'bin' changes value.
2194 */
2195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002196set_options_bin(
2197 int oldval,
2198 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 /*
2202 * The option values that are changed when 'bin' changes are
2203 * copied when 'bin is set and restored when 'bin' is reset.
2204 */
2205 if (newval)
2206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
2209 if (!(opt_flags & OPT_GLOBAL))
2210 {
2211 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2212 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2213 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2214 curbuf->b_p_et_nobin = curbuf->b_p_et;
2215 }
2216 if (!(opt_flags & OPT_LOCAL))
2217 {
2218 p_tw_nobin = p_tw;
2219 p_wm_nobin = p_wm;
2220 p_ml_nobin = p_ml;
2221 p_et_nobin = p_et;
2222 }
2223 }
2224
2225 if (!(opt_flags & OPT_GLOBAL))
2226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002227 curbuf->b_p_tw = 0; // no automatic line wrap
2228 curbuf->b_p_wm = 0; // no automatic line wrap
2229 curbuf->b_p_ml = 0; // no modelines
2230 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232 if (!(opt_flags & OPT_LOCAL))
2233 {
2234 p_tw = 0;
2235 p_wm = 0;
2236 p_ml = FALSE;
2237 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
2240 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 if (!(opt_flags & OPT_GLOBAL))
2244 {
2245 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2246 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2247 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2248 curbuf->b_p_et = curbuf->b_p_et_nobin;
2249 }
2250 if (!(opt_flags & OPT_LOCAL))
2251 {
2252 p_tw = p_tw_nobin;
2253 p_wm = p_wm_nobin;
2254 p_ml = p_ml_nobin;
2255 p_et = p_et_nobin;
2256 }
2257 }
2258}
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260/*
2261 * Expand environment variables for some string options.
2262 * These string options cannot be indirect!
2263 * If "val" is NULL expand the current value of the option.
2264 * Return pointer to NameBuff, or NULL when not expanded.
2265 */
2266 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002267option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2271 return NULL;
2272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 // If val is longer than MAXPATHL no meaningful expansion can be done,
2274 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (val != NULL && STRLEN(val) > MAXPATHL)
2276 return NULL;
2277
2278 if (val == NULL)
2279 val = *(char_u **)options[opt_idx].var;
2280
2281 /*
2282 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2283 * Escape spaces when expanding 'tags', they are used to separate file
2284 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002285 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 */
2287 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002288 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002289#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002290 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2291#endif
2292 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return NULL;
2295
2296 return NameBuff;
2297}
2298
2299/*
2300 * After setting various option values: recompute variables that depend on
2301 * option values.
2302 */
2303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 (void)init_chartab();
2308
Bram Moolenaardac13472019-09-16 21:06:21 +02002309 didset_string_options();
2310
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002311#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002312 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002313 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002314 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002315 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002318 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 (void)check_cedit();
2320#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002321#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002323 fill_breakat_flags();
2324#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002325 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002326}
2327
2328/*
2329 * More side effects of setting options.
2330 */
2331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002332didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002333{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002334 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002335 (void)highlight_changed();
2336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002338 check_opt_wim();
2339
Bram Moolenaareed9d462021-02-15 20:38:25 +01002340 // Parse default for 'listchars'.
2341 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002344 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002345
2346#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002348 (void)check_clipboard_option();
2349#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002350#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002351 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002352 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002353 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002354 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356}
2357
2358/*
2359 * Check for string options that are NULL (normally only termcap options).
2360 */
2361 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002362check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 int opt_idx;
2365
2366 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2367 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2368 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2369}
2370
2371/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002372 * Return the option index found by a pointer into term_strings[].
2373 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002375 int
2376get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002378 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2381 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002382 return opt_idx;
2383 return -1; // cannot happen: didn't find it!
2384}
2385
2386/*
2387 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2388 * Return the option index or -1 if not found.
2389 */
2390 int
2391set_term_option_alloced(char_u **p)
2392{
2393 int opt_idx = get_term_opt_idx(p);
2394
2395 if (opt_idx >= 0)
2396 options[opt_idx].flags |= P_ALLOCED;
2397 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398}
2399
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002400#if defined(FEAT_EVAL) || defined(PROTO)
2401/*
2402 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2403 * Return FALSE when it wasn't.
2404 * Return -1 for an unknown option.
2405 */
2406 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002407was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002408{
2409 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002410 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002411
2412 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002413 {
2414 flagp = insecure_flag(idx, opt_flags);
2415 return (*flagp & P_INSECURE) != 0;
2416 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002417 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002418 return -1;
2419}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002420
2421/*
2422 * Get a pointer to the flags used for the P_INSECURE flag of option
2423 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002424 * NOTE: Caller must make sure that "curwin" is set to the window from which
2425 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002426 */
2427 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002428insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002429{
2430 if (opt_flags & OPT_LOCAL)
2431 switch ((int)options[opt_idx].indir)
2432 {
2433#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002434 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002435#endif
2436#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002437# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002438 case PV_FDE: return &curwin->w_p_fde_flags;
2439 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002440# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002441# ifdef FEAT_BEVAL
2442 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2443# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002444# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002445 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002446# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002447 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002448# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002449 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002450# endif
2451#endif
2452 }
2453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002454 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002455 return &options[opt_idx].flags;
2456}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002457#endif
2458
Bram Moolenaardac13472019-09-16 21:06:21 +02002459#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002460/*
2461 * Redraw the window title and/or tab page text later.
2462 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002463void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002464{
2465 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002466 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002467}
2468#endif
2469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002471 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2472 * characters or characters in "allowed".
2473 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002474 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002475valid_name(char_u *val, char *allowed)
2476{
2477 char_u *s;
2478
2479 for (s = val; *s != NUL; ++s)
2480 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2481 return FALSE;
2482 return TRUE;
2483}
2484
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002485#if defined(FEAT_EVAL) || defined(PROTO)
2486/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002487 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002488 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002489 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002490 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002491set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002492{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002493 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2494 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002495 sctx_T new_script_ctx = script_ctx;
2496
Bram Moolenaar51258742020-05-03 17:19:33 +02002497 // Modeline already has the line number set.
2498 if (!(opt_flags & OPT_MODELINE))
2499 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002501 // Remember where the option was set. For local options need to do that
2502 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002503 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002504 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002505 if (both || (opt_flags & OPT_LOCAL))
2506 {
2507 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002508 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002509 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002510 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002511 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002512}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002513
2514/*
2515 * Set the script_ctx for a termcap option.
2516 * "name" must be the two character code, e.g. "RV".
2517 * When "name" is NULL use "opt_idx".
2518 */
2519 void
2520set_term_option_sctx_idx(char *name, int opt_idx)
2521{
2522 char_u buf[5];
2523 int idx;
2524
2525 if (name == NULL)
2526 idx = opt_idx;
2527 else
2528 {
2529 buf[0] = 't';
2530 buf[1] = '_';
2531 buf[2] = name[0];
2532 buf[3] = name[1];
2533 buf[4] = 0;
2534 idx = findoption(buf);
2535 }
2536 if (idx >= 0)
2537 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2538}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002539#endif
2540
Bram Moolenaarf4140482020-02-15 23:06:45 +01002541#if defined(FEAT_EVAL)
2542/*
2543 * Apply the OptionSet autocommand.
2544 */
2545 static void
2546apply_optionset_autocmd(
2547 int opt_idx,
2548 long opt_flags,
2549 long oldval,
2550 long oldval_g,
2551 long newval,
2552 char *errmsg)
2553{
2554 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2555
2556 // Don't do this while starting up, failure or recursively.
2557 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2558 return;
2559
2560 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2561 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2562 oldval_g);
2563 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2564 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2565 (opt_flags & OPT_LOCAL) ? "local" : "global");
2566 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2567 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2568 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2569 if (opt_flags & OPT_LOCAL)
2570 {
2571 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2572 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2573 }
2574 if (opt_flags & OPT_GLOBAL)
2575 {
2576 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2577 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2578 }
2579 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2580 {
2581 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2582 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2583 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2584 }
2585 if (opt_flags & OPT_MODELINE)
2586 {
2587 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2588 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2589 }
2590 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2591 NULL, FALSE, NULL);
2592 reset_v_option_vars();
2593}
2594#endif
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596/*
2597 * Set the value of a boolean option, and take care of side effects.
2598 * Returns NULL for success, or an error message for an error.
2599 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002600 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002601set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002602 int opt_idx, // index in options[] table
2603 char_u *varp, // pointer to the option variable
2604 int value, // new value
2605 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002608#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002609 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002612 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if ((secure
2614#ifdef HAVE_SANDBOX
2615 || sandbox != 0
2616#endif
2617 ) && (options[opt_idx].flags & P_SECURE))
2618 return e_secure;
2619
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002620#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002621 // Save the global value before changing anything. This is needed as for
2622 // a global-only option setting the "local value" in fact sets the global
2623 // value (since there is only one value).
2624 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2625 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2626 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002627#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002628
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002629 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002631 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002632 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002635#ifdef FEAT_GUI
2636 need_mouse_correct = TRUE;
2637#endif
2638
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2641 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2642
2643 /*
2644 * Handle side effects of changing a bool option.
2645 */
2646
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002647 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaar920694c2016-08-21 17:45:02 +02002651#ifdef FEAT_LANGMAP
2652 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002653 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002654 p_lnr = !p_lrm;
2655 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002656 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002657 p_lrm = !p_lnr;
2658#endif
2659
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002660#ifdef FEAT_SYN_HL
2661 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2662 reset_cursorline();
2663#endif
2664
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002665#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002667 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002669 // Only take action when the option was set. When reset we do not
2670 // delete the undo file, the option may be set again without making
2671 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002672 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002673 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002674 char_u hash[UNDO_HASH_SIZE];
2675 buf_T *save_curbuf = curbuf;
2676
Bram Moolenaar29323592016-07-24 22:04:11 +02002677 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002679 // When 'undofile' is set globally: for every buffer, otherwise
2680 // only for the current buffer: Try to read in the undofile,
2681 // if one exists, the buffer wasn't changed and the buffer was
2682 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002683 if ((curbuf == save_curbuf
2684 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2685 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2686 {
2687 u_compute_hash(hash);
2688 u_read_undo(NULL, hash, curbuf->b_fname);
2689 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002690 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002691 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002692 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002693 }
2694#endif
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 else if ((int *)varp == &curbuf->b_p_ro)
2697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2700 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002701
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002702 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002703 if (curbuf->b_p_ro)
2704 curbuf->b_did_warn = FALSE;
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002707 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#endif
2709 }
2710
Bram Moolenaar9c449722010-07-20 18:44:27 +02002711#ifdef FEAT_GUI
2712 else if ((int *)varp == &p_mh)
2713 {
2714 if (!p_mh)
2715 gui_mch_mousehide(FALSE);
2716 }
2717#endif
2718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002721 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002722# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002724 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2725 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002726 {
2727 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002728 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002729 }
2730# endif
2731# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002732 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002733# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002734 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002735#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002738 {
2739 redraw_titles();
2740 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002742 else if ((int *)varp == &curbuf->b_p_fixeol)
2743 {
2744 redraw_titles();
2745 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002747 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002748 {
2749 redraw_titles();
2750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 else if ((int *)varp == &curbuf->b_p_bin)
2755 {
2756 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2757#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002758 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#endif
2760 }
2761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002762 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2764 {
2765 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2766 NULL, NULL, TRUE, curbuf);
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else if ((int *)varp == &curbuf->b_p_swf)
2771 {
2772 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2776 // buf->b_p_swf
2777 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else if ((int *)varp == &p_terse)
2782 {
2783 char_u *p;
2784
2785 p = vim_strchr(p_shm, SHM_SEARCH);
2786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002787 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (p_terse && p == NULL)
2789 {
2790 STRCPY(IObuff, p_shm);
2791 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002792 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002794 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002796 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else if ((int *)varp == &p_paste)
2801 {
2802 paste_option_changed();
2803 }
2804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002805 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else if ((int *)varp == &p_im)
2807 {
2808 if (p_im)
2809 {
2810 if ((State & INSERT) == 0)
2811 need_start_insertmode = TRUE;
2812 stop_insert_mode = FALSE;
2813 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002815 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
2817 need_start_insertmode = FALSE;
2818 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002819 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 restart_edit = 0;
2822 }
2823 }
2824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 else if ((int *)varp == &p_ic && p_hls)
2827 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002828 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830
2831#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else if ((int *)varp == &p_hls)
2834 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002835 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837#endif
2838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2840 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else if ((int *)varp == &curwin->w_p_scb)
2842 {
2843 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002846 curwin->w_scbind_pos = curwin->w_topline;
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaar4033c552017-09-16 20:54:51 +02002850#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else if ((int *)varp == &curwin->w_p_pvw)
2853 {
2854 if (curwin->w_p_pvw)
2855 {
2856 win_T *win;
2857
Bram Moolenaar29323592016-07-24 22:04:11 +02002858 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (win->w_p_pvw && win != curwin)
2860 {
2861 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002862 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864 }
2865 }
2866#endif
2867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 else if ((int *)varp == &curbuf->b_p_tx)
2870 {
2871 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2872 }
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 set_string_option_direct((char_u *)"ffs", -1,
2878 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002879 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
2882 /*
2883 * When 'lisp' option changes include/exclude '-' in
2884 * keyword characters.
2885 */
2886#ifdef FEAT_LISP
2887 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891#endif
2892
2893#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002895 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002897 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899#endif
2900
2901 else if ((int *)varp == &curbuf->b_changed)
2902 {
2903 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002906 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910
2911#ifdef BACKSLASH_IN_FILENAME
2912 else if ((int *)varp == &p_ssl)
2913 {
2914 if (p_ssl)
2915 {
2916 psepc = '/';
2917 psepcN = '\\';
2918 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 else
2921 {
2922 psepc = '\\';
2923 psepcN = '/';
2924 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 buflist_slash_adjust();
2929 alist_slash_adjust();
2930# ifdef FEAT_EVAL
2931 scriptnames_slash_adjust();
2932# endif
2933 }
2934#endif
2935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 else if ((int *)varp == &curwin->w_p_wrap)
2938 {
2939 if (curwin->w_p_wrap)
2940 curwin->w_leftcol = 0;
2941 }
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 else if ((int *)varp == &p_ea)
2944 {
2945 if (p_ea && !old_value)
2946 win_equal(curwin, FALSE, 0);
2947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 else if ((int *)varp == &p_wiv)
2950 {
2951 /*
2952 * When 'weirdinvert' changed, set/reset 't_xs'.
2953 * Then set 'weirdinvert' according to value of 't_xs'.
2954 */
2955 if (p_wiv && !old_value)
2956 T_XS = (char_u *)"y";
2957 else if (!p_wiv && old_value)
2958 T_XS = empty_option;
2959 p_wiv = (*T_XS != NUL);
2960 }
2961
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002962#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 else if ((int *)varp == &p_beval)
2964 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002965 if (!balloonEvalForTerm)
2966 {
2967 if (p_beval && !old_value)
2968 gui_mch_enable_beval_area(balloonEval);
2969 else if (!p_beval && old_value)
2970 gui_mch_disable_beval_area(balloonEval);
2971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002973#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002974#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002975 else if ((int *)varp == &p_bevalterm)
2976 {
2977 mch_bevalterm_changed();
2978 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002981#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 else if ((int *)varp == &p_acd)
2983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002985 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 }
2987#endif
2988
2989#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else if ((int *)varp == &curwin->w_p_diff)
2992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002994 diff_buf_adjust(curwin);
2995# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (foldmethodIsDiff(curwin))
2997 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
3000#endif
3001
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003002#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 else if ((int *)varp == &p_imdisable)
3005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (p_imdisable)
3008 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003009 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 // When the option is set from an autocommand, it may need to take
3011 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003012 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014#endif
3015
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003016#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003018 else if ((int *)varp == &curwin->w_p_spell)
3019 {
3020 if (curwin->w_p_spell)
3021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 char *errmsg = did_set_spelllang(curwin);
3023
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003024 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003026 }
3027 }
3028#endif
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#ifdef FEAT_ARABIC
3031 if ((int *)varp == &curwin->w_p_arab)
3032 {
3033 if (curwin->w_p_arab)
3034 {
3035 /*
3036 * 'arabic' is set, handle various sub-settings.
3037 */
3038 if (!p_tbidi)
3039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (!curwin->w_p_rl)
3042 {
3043 curwin->w_p_rl = TRUE;
3044 changed_window_setting();
3045 }
3046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (!p_arshape)
3049 {
3050 p_arshape = TRUE;
3051 redraw_later_clear();
3052 }
3053 }
3054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // Arabic requires a utf-8 encoding, inform the user if its not
3056 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003058 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003059 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3060
Bram Moolenaar8820b482017-03-16 17:23:31 +01003061 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003062 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003063#ifdef FEAT_EVAL
3064 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3065#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3074 OPT_LOCAL);
3075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077 else
3078 {
3079 /*
3080 * 'arabic' is reset, handle various sub-settings.
3081 */
3082 if (!p_tbidi)
3083 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (curwin->w_p_rl)
3086 {
3087 curwin->w_p_rl = FALSE;
3088 changed_window_setting();
3089 }
3090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // 'arabicshape' isn't reset, it is a global option and
3092 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // 'delcombine' isn't reset, it is a global option and another
3096 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 curbuf->b_p_iminsert = B_IMODE_NONE;
3101 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3102# endif
3103 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003104 }
3105
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003106#endif
3107
Bram Moolenaar44826212019-08-22 21:23:20 +02003108#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3109 else if (((int *)varp == &curwin->w_p_nu
3110 || (int *)varp == &curwin->w_p_rnu)
3111 && gui.in_use
3112 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3113 && curbuf->b_signlist != NULL)
3114 {
3115 // If the 'number' or 'relativenumber' options are modified and
3116 // 'signcolumn' is set to 'number', then clear the screen for a full
3117 // refresh. Otherwise the sign icons are not displayed properly in the
3118 // number column. If the 'number' option is set and only the
3119 // 'relativenumber' option is toggled, then don't refresh the screen
3120 // (optimization).
3121 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3122 redraw_all_later(CLEAR);
3123 }
3124#endif
3125
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003126#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003128 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003129 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003130# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003131 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003132 if (
3133# ifdef VIMDLL
3134 !gui.in_use && !gui.starting &&
3135# endif
3136 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003137 {
3138 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003139 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003140 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003141 if (is_term_win32())
3142 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003143# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003144# ifdef FEAT_GUI
3145 if (!gui.in_use && !gui.starting)
3146# endif
3147 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003148# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003150 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003151 {
3152 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003153 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003154 init_highlight(TRUE, FALSE);
3155 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003156# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003157 }
3158#endif
3159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 /*
3161 * End of handling side effects for bool options.
3162 */
3163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003164 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 options[opt_idx].flags |= P_WAS_SET;
3167
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003168#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003169 apply_optionset_autocmd(opt_idx, opt_flags,
3170 (long)(old_value ? TRUE : FALSE),
3171 (long)(old_global_value ? TRUE : FALSE),
3172 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003173#endif
3174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003176 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003177 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003178 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 check_redraw(options[opt_idx].flags);
3180
3181 return NULL;
3182}
3183
3184/*
3185 * Set the value of a number option, and take care of side effects.
3186 * Returns NULL for success, or an error message for an error.
3187 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003188 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003189set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 int opt_idx, // index in options[] table
3191 char_u *varp, // pointer to the option variable
3192 long value, // new value
3193 char *errbuf, // buffer for error messages
3194 size_t errbuflen, // length of "errbuf"
3195 int opt_flags) // OPT_LOCAL, OPT_GLOBAL and
3196 // OPT_MODELINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003198 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003200#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003201 long old_global_value = 0; // only used when setting a local and
3202 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003203#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003204 long old_Rows = Rows; // remember old Rows
3205 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 long *pp = (long *)varp;
3207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003208 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003209 if ((secure
3210#ifdef HAVE_SANDBOX
3211 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003213 ) && (options[opt_idx].flags & P_SECURE))
3214 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003216#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003217 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003218 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003219 // value (since there is only one value).
3220 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003221 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3222 OPT_GLOBAL);
3223#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 *pp = value;
3226#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003227 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003228 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003230#ifdef FEAT_GUI
3231 need_mouse_correct = TRUE;
3232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
Bram Moolenaar14f24742012-08-08 18:01:05 +02003234 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003237#ifdef FEAT_VARTABS
3238 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3239 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3240 ? tabstop_first(curbuf->b_p_vts_array)
3241 : curbuf->b_p_ts;
3242#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246
3247 /*
3248 * Number options that need some action when changed
3249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (pp == &p_wh || pp == &p_hh)
3251 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003252 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (p_wh < 1)
3254 {
3255 errmsg = e_positive;
3256 p_wh = 1;
3257 }
3258 if (p_wmh > p_wh)
3259 {
3260 errmsg = e_winheight;
3261 p_wh = p_wmh;
3262 }
3263 if (p_hh < 0)
3264 {
3265 errmsg = e_positive;
3266 p_hh = 0;
3267 }
3268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003269 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003270 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 if (pp == &p_wh && curwin->w_height < p_wh)
3273 win_setheight((int)p_wh);
3274 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3275 win_setheight((int)p_hh);
3276 }
3277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else if (pp == &p_wmh)
3279 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003280 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (p_wmh < 0)
3282 {
3283 errmsg = e_positive;
3284 p_wmh = 0;
3285 }
3286 if (p_wmh > p_wh)
3287 {
3288 errmsg = e_winheight;
3289 p_wmh = p_wh;
3290 }
3291 win_setminheight();
3292 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003293 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003295 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (p_wiw < 1)
3297 {
3298 errmsg = e_positive;
3299 p_wiw = 1;
3300 }
3301 if (p_wmw > p_wiw)
3302 {
3303 errmsg = e_winwidth;
3304 p_wiw = p_wmw;
3305 }
3306
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003307 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003308 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 win_setwidth((int)p_wiw);
3310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 else if (pp == &p_wmw)
3312 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003313 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (p_wmw < 0)
3315 {
3316 errmsg = e_positive;
3317 p_wmw = 0;
3318 }
3319 if (p_wmw > p_wiw)
3320 {
3321 errmsg = e_winwidth;
3322 p_wmw = p_wiw;
3323 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003324 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003327 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 else if (pp == &p_ls)
3329 {
3330 last_status(FALSE);
3331 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003332
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003333 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003334 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003336 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
3339#ifdef FEAT_GUI
3340 else if (pp == &p_linespace)
3341 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003342 // Recompute gui.char_height and resize the Vim window to keep the
3343 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003344 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003345 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
3347#endif
3348
3349#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003350 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 else if (pp == &curwin->w_p_fdl)
3352 {
3353 if (curwin->w_p_fdl < 0)
3354 curwin->w_p_fdl = 0;
3355 newFoldLevel();
3356 }
3357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003358 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else if (pp == &curwin->w_p_fml)
3360 {
3361 foldUpdateAll(curwin);
3362 }
3363
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003364 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 else if (pp == &curwin->w_p_fdn)
3366 {
3367 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3368 foldUpdateAll(curwin);
3369 }
3370
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003371 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else if (pp == &curwin->w_p_fdc)
3373 {
3374 if (curwin->w_p_fdc < 0)
3375 {
3376 errmsg = e_positive;
3377 curwin->w_p_fdc = 0;
3378 }
3379 else if (curwin->w_p_fdc > 12)
3380 {
3381 errmsg = e_invarg;
3382 curwin->w_p_fdc = 12;
3383 }
3384 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003385#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003387#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003388 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3390 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003391# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (foldmethodIsIndent(curwin))
3393 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003394# endif
3395# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003396 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3397 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003398 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3399 parse_cino(curbuf);
3400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003404 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003405 else if (pp == &p_mco)
3406 {
3407 if (p_mco > MAX_MCO)
3408 p_mco = MAX_MCO;
3409 else if (p_mco < 0)
3410 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003411 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003412 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else if (pp == &curbuf->b_p_iminsert)
3415 {
3416 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3417 {
3418 errmsg = e_invarg;
3419 curbuf->b_p_iminsert = B_IMODE_NONE;
3420 }
3421 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003422 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003424#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003425 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 status_redraw_curbuf();
3427#endif
3428 }
3429
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003430#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003431 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003432 else if (pp == &p_imst)
3433 {
3434 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3435 errmsg = e_invarg;
3436 }
3437#endif
3438
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003439 else if (pp == &p_window)
3440 {
3441 if (p_window < 1)
3442 p_window = 1;
3443 else if (p_window >= Rows)
3444 p_window = Rows - 1;
3445 }
3446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 else if (pp == &curbuf->b_p_imsearch)
3448 {
3449 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3450 {
3451 errmsg = e_invarg;
3452 curbuf->b_p_imsearch = B_IMODE_NONE;
3453 }
3454 p_imsearch = curbuf->b_p_imsearch;
3455 }
3456
3457#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else if (pp == &p_titlelen)
3460 {
3461 if (p_titlelen < 0)
3462 {
3463 errmsg = e_positive;
3464 p_titlelen = 85;
3465 }
3466 if (starting != NO_SCREEN && old_value != p_titlelen)
3467 need_maketitle = TRUE;
3468 }
3469#endif
3470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003471 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 else if (pp == &p_ch)
3473 {
3474 if (p_ch < 1)
3475 {
3476 errmsg = e_positive;
3477 p_ch = 1;
3478 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003479 if (p_ch > Rows - min_rows() + 1)
3480 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // Only compute the new window layout when startup has been
3483 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (p_ch != old_value && full_screen
3485#ifdef FEAT_GUI
3486 && !gui.starting
3487#endif
3488 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003489 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003492 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 else if (pp == &p_uc)
3494 {
3495 if (p_uc < 0)
3496 {
3497 errmsg = e_positive;
3498 p_uc = 100;
3499 }
3500 if (p_uc && !old_value)
3501 ml_open_files();
3502 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003503#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003504 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003505 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003506 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003507 {
3508 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003509 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003510 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003511 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003512 {
3513 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003514 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003515 }
3516 }
3517#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003518#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003519 else if (pp == &p_mzq)
3520 mzvim_reset_timer();
3521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003523#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003524 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003525 else if (pp == &p_pyx)
3526 {
3527 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3528 errmsg = e_invarg;
3529 }
3530#endif
3531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003532 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 else if (pp == &p_ul)
3534 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003535 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003537 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 p_ul = value;
3539 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003540 else if (pp == &curbuf->b_p_ul)
3541 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003542 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003543 curbuf->b_p_ul = old_value;
3544 u_sync(TRUE);
3545 curbuf->b_p_ul = value;
3546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003548#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003549 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003550 else if (pp == &curwin->w_p_nuw)
3551 {
3552 if (curwin->w_p_nuw < 1)
3553 {
3554 errmsg = e_positive;
3555 curwin->w_p_nuw = 1;
3556 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003557 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003558 {
3559 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003560 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003561 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003562 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003563 }
3564#endif
3565
Bram Moolenaar1a384422010-07-14 19:53:30 +02003566 else if (pp == &curbuf->b_p_tw)
3567 {
3568 if (curbuf->b_p_tw < 0)
3569 {
3570 errmsg = e_positive;
3571 curbuf->b_p_tw = 0;
3572 }
3573#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003574 {
3575 win_T *wp;
3576 tabpage_T *tp;
3577
3578 FOR_ALL_TAB_WINDOWS(tp, wp)
3579 check_colorcolumn(wp);
3580 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003581#endif
3582 }
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 /*
3585 * Check the bounds for numeric options here
3586 */
3587 if (Rows < min_rows() && full_screen)
3588 {
3589 if (errbuf != NULL)
3590 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003591 vim_snprintf((char *)errbuf, errbuflen,
3592 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 errmsg = errbuf;
3594 }
3595 Rows = min_rows();
3596 }
3597 if (Columns < MIN_COLUMNS && full_screen)
3598 {
3599 if (errbuf != NULL)
3600 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003601 vim_snprintf((char *)errbuf, errbuflen,
3602 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 errmsg = errbuf;
3604 }
3605 Columns = MIN_COLUMNS;
3606 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003607 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 /*
3610 * If the screen (shell) height has been changed, assume it is the
3611 * physical screenheight.
3612 */
3613 if (old_Rows != Rows || old_Columns != Columns)
3614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003615 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 if (updating_screen)
3617 *pp = old_value;
3618 else if (full_screen
3619#ifdef FEAT_GUI
3620 && !gui.starting
3621#endif
3622 )
3623 set_shellsize((int)Columns, (int)Rows, TRUE);
3624 else
3625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 // Postpone the resizing; check the size and cmdline position for
3627 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 check_shellsize();
3629 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3630 cmdline_row = Rows - p_ch;
3631 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003632 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003633 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (curbuf->b_p_ts <= 0)
3637 {
3638 errmsg = e_positive;
3639 curbuf->b_p_ts = 8;
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (p_tm < 0)
3642 {
3643 errmsg = e_positive;
3644 p_tm = 0;
3645 }
3646 if ((curwin->w_p_scr <= 0
3647 || (curwin->w_p_scr > curwin->w_height
3648 && curwin->w_height > 0))
3649 && full_screen)
3650 {
3651 if (pp == &(curwin->w_p_scr))
3652 {
3653 if (curwin->w_p_scr != 0)
3654 errmsg = e_scroll;
3655 win_comp_scroll(curwin);
3656 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003657 // If 'scroll' became invalid because of a side effect silently adjust
3658 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 else if (curwin->w_p_scr <= 0)
3660 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003661 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 curwin->w_p_scr = curwin->w_height;
3663 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003664 if (p_hi < 0)
3665 {
3666 errmsg = e_positive;
3667 p_hi = 0;
3668 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003669 else if (p_hi > 10000)
3670 {
3671 errmsg = e_invarg;
3672 p_hi = 10000;
3673 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003674 if (p_re < 0 || p_re > 2)
3675 {
3676 errmsg = e_invarg;
3677 p_re = 0;
3678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (p_report < 0)
3680 {
3681 errmsg = e_positive;
3682 p_report = 1;
3683 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003684 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003686 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 p_sj = Rows / 2;
3688 else
3689 {
3690 errmsg = e_scroll;
3691 p_sj = 1;
3692 }
3693 }
3694 if (p_so < 0 && full_screen)
3695 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003696 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 p_so = 0;
3698 }
3699 if (p_siso < 0 && full_screen)
3700 {
3701 errmsg = e_positive;
3702 p_siso = 0;
3703 }
3704#ifdef FEAT_CMDWIN
3705 if (p_cwh < 1)
3706 {
3707 errmsg = e_positive;
3708 p_cwh = 1;
3709 }
3710#endif
3711 if (p_ut < 0)
3712 {
3713 errmsg = e_positive;
3714 p_ut = 2000;
3715 }
3716 if (p_ss < 0)
3717 {
3718 errmsg = e_positive;
3719 p_ss = 0;
3720 }
3721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003722 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3724 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3725
3726 options[opt_idx].flags |= P_WAS_SET;
3727
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003728#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003729 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3730 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003731#endif
3732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003733 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003734 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003735 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003736 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 check_redraw(options[opt_idx].flags);
3738
3739 return errmsg;
3740}
3741
3742/*
3743 * Called after an option changed: check if something needs to be redrawn.
3744 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003745 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003746check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003748 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003749 int doclear = (flags & P_RCLR) == P_RCLR;
3750 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003752 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3756 changed_window_setting();
3757 if (flags & P_RBUF)
3758 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003759 if (flags & P_RWINONLY)
3760 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003761 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 redraw_all_later(CLEAR);
3763 else if (all)
3764 redraw_all_later(NOT_VALID);
3765}
3766
3767/*
3768 * Find index for option 'arg'.
3769 * Return -1 if not found.
3770 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003771 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003772findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
3774 int opt_idx;
3775 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003776 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 int is_term_opt;
3778
3779 /*
3780 * For first call: Initialize the quick-access table.
3781 * It contains the index for the first option that starts with a certain
3782 * letter. There are 26 letters, plus the first "t_" option.
3783 */
3784 if (quick_tab[1] == 0)
3785 {
3786 p = options[0].fullname;
3787 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3788 {
3789 if (s[0] != p[0])
3790 {
3791 if (s[0] == 't' && s[1] == '_')
3792 quick_tab[26] = opt_idx;
3793 else
3794 quick_tab[CharOrdLow(s[0])] = opt_idx;
3795 }
3796 p = s;
3797 }
3798 }
3799
3800 /*
3801 * Check for name starting with an illegal character.
3802 */
3803#ifdef EBCDIC
3804 if (!islower(arg[0]))
3805#else
3806 if (arg[0] < 'a' || arg[0] > 'z')
3807#endif
3808 return -1;
3809
3810 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3811 if (is_term_opt)
3812 opt_idx = quick_tab[26];
3813 else
3814 opt_idx = quick_tab[CharOrdLow(arg[0])];
3815 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003817 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 break;
3819 }
3820 if (s == NULL && !is_term_opt)
3821 {
3822 opt_idx = quick_tab[CharOrdLow(arg[0])];
3823 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3824 {
3825 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003826 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 break;
3828 s = NULL;
3829 }
3830 }
3831 if (s == NULL)
3832 opt_idx = -1;
3833 return opt_idx;
3834}
3835
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003836#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837/*
3838 * Get the value for an option.
3839 *
3840 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003841 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003842 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003843 * String option: gov_string, *stringval gets allocated string.
3844 * Hidden Number option: gov_hidden_number.
3845 * Hidden Toggle option: gov_hidden_bool.
3846 * Hidden String option: gov_hidden_string.
3847 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003849 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003850get_option_value(
3851 char_u *name,
3852 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003853 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003854 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 int opt_idx;
3857 char_u *varp;
3858
3859 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003860 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003861 {
3862 int key;
3863
3864 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003865 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003866 {
3867 char_u key_name[2];
3868 char_u *p;
3869
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003870 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003871 if (key < 0)
3872 {
3873 key_name[0] = KEY2TERMCAP0(key);
3874 key_name[1] = KEY2TERMCAP1(key);
3875 }
3876 else
3877 {
3878 key_name[0] = KS_KEY;
3879 key_name[1] = (key & 0xff);
3880 }
3881 p = find_termcode(key_name);
3882 if (p != NULL)
3883 {
3884 if (stringval != NULL)
3885 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003886 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003887 }
3888 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003889 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
3892 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3893
3894 if (options[opt_idx].flags & P_STRING)
3895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003896 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003897 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (stringval != NULL)
3899 {
3900#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003901 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003902 if ((char_u **)varp == &curbuf->b_p_key
3903 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 *stringval = vim_strsave((char_u *)"*****");
3905 else
3906#endif
3907 *stringval = vim_strsave(*(char_u **)(varp));
3908 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003909 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003912 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003913 return (options[opt_idx].flags & P_NUM)
3914 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (options[opt_idx].flags & P_NUM)
3916 *numval = *(long *)varp;
3917 else
3918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003919 // Special case: 'modified' is b_changed, but we also want to consider
3920 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if ((int *)varp == &curbuf->b_changed)
3922 *numval = curbufIsChanged();
3923 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003924 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003926 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927}
3928#endif
3929
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003930#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003931/*
3932 * Returns the option attributes and its value. Unlike the above function it
3933 * will return either global value or local value of the option depending on
3934 * what was requested, but it will never return global value if it was
3935 * requested to return local one and vice versa. Neither it will return
3936 * buffer-local value if it was requested to return window-local one.
3937 *
3938 * Pretends that option is absent if it is not present in the requested scope
3939 * (i.e. has no global, window-local or buffer-local value depending on
3940 * opt_type). Uses
3941 *
3942 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003943 * 0 hidden or unknown option, also option that does not have requested
3944 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003945 * see SOPT_* in vim.h for other flags
3946 *
3947 * Possible opt_type values: see SREQ_* in vim.h
3948 */
3949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003950get_option_value_strict(
3951 char_u *name,
3952 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003953 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003954 int opt_type,
3955 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003956{
3957 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003958 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003959 struct vimoption *p;
3960 int r = 0;
3961
3962 opt_idx = findoption(name);
3963 if (opt_idx < 0)
3964 return 0;
3965
3966 p = &(options[opt_idx]);
3967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003968 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003969 if (p->var == NULL)
3970 return 0;
3971
3972 if (p->flags & P_BOOL)
3973 r |= SOPT_BOOL;
3974 else if (p->flags & P_NUM)
3975 r |= SOPT_NUM;
3976 else if (p->flags & P_STRING)
3977 r |= SOPT_STRING;
3978
3979 if (p->indir == PV_NONE)
3980 {
3981 if (opt_type == SREQ_GLOBAL)
3982 r |= SOPT_GLOBAL;
3983 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003984 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003985 }
3986 else
3987 {
3988 if (p->indir & PV_BOTH)
3989 r |= SOPT_GLOBAL;
3990 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003991 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003992
3993 if (p->indir & PV_WIN)
3994 {
3995 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003996 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003997 else
3998 r |= SOPT_WIN;
3999 }
4000 else if (p->indir & PV_BUF)
4001 {
4002 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004004 else
4005 r |= SOPT_BUF;
4006 }
4007 }
4008
4009 if (stringval == NULL)
4010 return r;
4011
4012 if (opt_type == SREQ_GLOBAL)
4013 varp = p->var;
4014 else
4015 {
4016 if (opt_type == SREQ_BUF)
4017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 // Special case: 'modified' is b_changed, but we also want to
4019 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004020 if (p->indir == PV_MOD)
4021 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004022 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004023 varp = NULL;
4024 }
4025#ifdef FEAT_CRYPT
4026 else if (p->indir == PV_KEY)
4027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004028 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004029 *stringval = NULL;
4030 varp = NULL;
4031 }
4032#endif
4033 else
4034 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004035 buf_T *save_curbuf = curbuf;
4036
4037 // only getting a pointer, no need to use aucmd_prepbuf()
4038 curbuf = (buf_T *)from;
4039 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004040 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004041 curbuf = save_curbuf;
4042 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004043 }
4044 }
4045 else if (opt_type == SREQ_WIN)
4046 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004047 win_T *save_curwin = curwin;
4048
4049 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004050 curbuf = curwin->w_buffer;
4051 varp = get_varp(p);
4052 curwin = save_curwin;
4053 curbuf = curwin->w_buffer;
4054 }
4055 if (varp == p->var)
4056 return (r | SOPT_UNSET);
4057 }
4058
4059 if (varp != NULL)
4060 {
4061 if (p->flags & P_STRING)
4062 *stringval = vim_strsave(*(char_u **)(varp));
4063 else if (p->flags & P_NUM)
4064 *numval = *(long *) varp;
4065 else
4066 *numval = *(int *)varp;
4067 }
4068
4069 return r;
4070}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004071
4072/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004073 * Iterate over options. First argument is a pointer to a pointer to a
4074 * structure inside options[] array, second is option type like in the above
4075 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004076 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004077 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004078 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004079 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004080 * first argument to NULL.
4081 *
4082 * Returns full option name for current option on each call.
4083 */
4084 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004085option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004086{
4087 struct vimoption *ret = NULL;
4088 do
4089 {
4090 if (*option == NULL)
4091 *option = (void *) options;
4092 else if (((struct vimoption *) (*option))->fullname == NULL)
4093 {
4094 *option = NULL;
4095 return NULL;
4096 }
4097 else
4098 *option = (void *) (((struct vimoption *) (*option)) + 1);
4099
4100 ret = ((struct vimoption *) (*option));
4101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004102 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004103 if (ret->var == NULL)
4104 {
4105 ret = NULL;
4106 continue;
4107 }
4108
4109 switch (opt_type)
4110 {
4111 case SREQ_GLOBAL:
4112 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4113 ret = NULL;
4114 break;
4115 case SREQ_BUF:
4116 if (!(ret->indir & PV_BUF))
4117 ret = NULL;
4118 break;
4119 case SREQ_WIN:
4120 if (!(ret->indir & PV_WIN))
4121 ret = NULL;
4122 break;
4123 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004124 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004125 return NULL;
4126 }
4127 }
4128 while (ret == NULL);
4129
4130 return (char_u *)ret->fullname;
4131}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004132#endif
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004135 * Return the flags for the option at 'opt_idx'.
4136 */
4137 long_u
4138get_option_flags(int opt_idx)
4139{
4140 return options[opt_idx].flags;
4141}
4142
4143/*
4144 * Set a flag for the option at 'opt_idx'.
4145 */
4146 void
4147set_option_flag(int opt_idx, long_u flag)
4148{
4149 options[opt_idx].flags |= flag;
4150}
4151
4152/*
4153 * Clear a flag for the option at 'opt_idx'.
4154 */
4155 void
4156clear_option_flag(int opt_idx, long_u flag)
4157{
4158 options[opt_idx].flags &= ~flag;
4159}
4160
4161/*
4162 * Returns TRUE if the option at 'opt_idx' is a global option
4163 */
4164 int
4165is_global_option(int opt_idx)
4166{
4167 return options[opt_idx].indir == PV_NONE;
4168}
4169
4170/*
4171 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4172 * local value.
4173 */
4174 int
4175is_global_local_option(int opt_idx)
4176{
4177 return options[opt_idx].indir & PV_BOTH;
4178}
4179
4180/*
4181 * Returns TRUE if the option at 'opt_idx' is a window-local option
4182 */
4183 int
4184is_window_local_option(int opt_idx)
4185{
4186 return options[opt_idx].var == VAR_WIN;
4187}
4188
4189/*
4190 * Returns TRUE if the option at 'opt_idx' is a hidden option
4191 */
4192 int
4193is_hidden_option(int opt_idx)
4194{
4195 return options[opt_idx].var == NULL;
4196}
4197
4198#if defined(FEAT_CRYPT) || defined(PROTO)
4199/*
4200 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4201 */
4202 int
4203is_crypt_key_option(int opt_idx)
4204{
4205 return options[opt_idx].indir == PV_KEY;
4206}
4207#endif
4208
4209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 * Set the value of option "name".
4211 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004212 *
4213 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004215 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004216set_option_value(
4217 char_u *name,
4218 long number,
4219 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004220 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 int opt_idx;
4223 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004224 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004227 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004228 {
4229 int key;
4230
4231 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004232 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004233 {
4234 char_u key_name[2];
4235
4236 if (key < 0)
4237 {
4238 key_name[0] = KEY2TERMCAP0(key);
4239 key_name[1] = KEY2TERMCAP1(key);
4240 }
4241 else
4242 {
4243 key_name[0] = KS_KEY;
4244 key_name[1] = (key & 0xff);
4245 }
4246 add_termcode(key_name, string, FALSE);
4247 if (full_screen)
4248 ttest(FALSE);
4249 redraw_all_later(CLEAR);
4250 return NULL;
4251 }
4252
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004253 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
4256 {
4257 flags = options[opt_idx].flags;
4258#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004259 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004262 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004263 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004266 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004267 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 else
4269 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004270 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004271 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004273 if (number == 0 && string != NULL)
4274 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004275 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004277 // Either we are given a string or we are setting option
4278 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004279 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004280 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004281 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004282 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004283 // There's another character after zeros or the string
4284 // is empty. In both cases, we are trying to set a
4285 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004286 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004287 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004288 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004289
4290 }
4291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004293 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004294 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004296 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004297 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 }
4300 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004301 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302}
4303
4304/*
4305 * Get the terminal code for a terminal option.
4306 * Returns NULL when not found.
4307 */
4308 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004309get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
4311 int opt_idx;
4312 char_u *varp;
4313
4314 if (tname[0] != 't' || tname[1] != '_' ||
4315 tname[2] == NUL || tname[3] == NUL)
4316 return NULL;
4317 if ((opt_idx = findoption(tname)) >= 0)
4318 {
4319 varp = get_varp(&(options[opt_idx]));
4320 if (varp != NULL)
4321 varp = *(char_u **)(varp);
4322 return varp;
4323 }
4324 return find_termcode(tname + 2);
4325}
4326
4327 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004328get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
4330 int i;
4331
4332 i = findoption((char_u *)"hl");
4333 if (i >= 0)
4334 return options[i].def_val[VI_DEFAULT];
4335 return (char_u *)NULL;
4336}
4337
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004338 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004339get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004340{
4341 int i;
4342
4343 i = findoption((char_u *)"enc");
4344 if (i >= 0)
4345 return options[i].def_val[VI_DEFAULT];
4346 return (char_u *)NULL;
4347}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004348
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349/*
4350 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004351 * When "has_lt" is true there is a '<' before "*arg_arg".
4352 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 */
4354 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004355find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004357 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004359 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 /*
4362 * Don't use get_special_key_code() for t_xx, we don't want it to call
4363 * add_termcap_entry().
4364 */
4365 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4366 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004367 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004369 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004371 key = find_special_key(&arg, &modifiers,
4372 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004373 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 key = 0;
4375 }
4376 return key;
4377}
4378
4379/*
4380 * if 'all' == 0: show changed options
4381 * if 'all' == 1: show all normal options
4382 * if 'all' == 2: show all terminal options
4383 */
4384 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004385showoptions(
4386 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004387 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 struct vimoption *p;
4390 int col;
4391 int isterm;
4392 char_u *varp;
4393 struct vimoption **items;
4394 int item_count;
4395 int run;
4396 int row, rows;
4397 int cols;
4398 int i;
4399 int len;
4400
4401#define INC 20
4402#define GAP 3
4403
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004404 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (items == NULL)
4406 return;
4407
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004408 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004410 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004412 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004414 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004416 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004419 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 * 1. display the short items
4421 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004422 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 */
4424 for (run = 1; run <= 2 && !got_int; ++run)
4425 {
4426 /*
4427 * collect the items in items[]
4428 */
4429 item_count = 0;
4430 for (p = &options[0]; p->fullname != NULL; p++)
4431 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004432 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004433 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004434 continue;
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 varp = NULL;
4437 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004438 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 if (p->indir != PV_NONE && !isterm)
4441 varp = get_varp_scope(p, opt_flags);
4442 }
4443 else
4444 varp = get_varp(p);
4445 if (varp != NULL
4446 && ((all == 2 && isterm)
4447 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004448 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004450 if (opt_flags & OPT_ONECOLUMN)
4451 len = Columns;
4452 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004453 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 else
4455 {
4456 option_value2string(p, opt_flags);
4457 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4458 }
4459 if ((len <= INC - GAP && run == 1) ||
4460 (len > INC - GAP && run == 2))
4461 items[item_count++] = p;
4462 }
4463 }
4464
4465 /*
4466 * display the items
4467 */
4468 if (run == 1)
4469 {
4470 cols = (Columns + GAP - 3) / INC;
4471 if (cols == 0)
4472 cols = 1;
4473 rows = (item_count + cols - 1) / cols;
4474 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004475 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 rows = item_count;
4477 for (row = 0; row < rows && !got_int; ++row)
4478 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004479 msg_putchar('\n'); // go to next line
4480 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 break;
4482 col = 0;
4483 for (i = row; i < item_count; i += rows)
4484 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004485 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 showoneopt(items[i], opt_flags);
4487 col += INC;
4488 }
4489 out_flush();
4490 ui_breakcheck();
4491 }
4492 }
4493 vim_free(items);
4494}
4495
4496/*
4497 * Return TRUE if option "p" has its default value.
4498 */
4499 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004500optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
4502 int dvi;
4503
4504 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004506 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004508 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004510 // the cast to long is required for Manx C, long_i is
4511 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004512 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004513 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4515}
4516
4517/*
4518 * showoneopt: show the value of one option
4519 * must not be called with a hidden option!
4520 */
4521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004522showoneopt(
4523 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004524 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004526 char_u *varp;
4527 int save_silent = silent_mode;
4528
4529 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004530 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532 varp = get_varp_scope(p, opt_flags);
4533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004534 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4536 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004537 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004539 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004541 msg_puts(" ");
4542 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (!(p->flags & P_BOOL))
4544 {
4545 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004546 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 option_value2string(p, opt_flags);
4548 msg_outtrans(NameBuff);
4549 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004550
4551 silent_mode = save_silent;
4552 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553}
4554
4555/*
4556 * Write modified options as ":set" commands to a file.
4557 *
4558 * There are three values for "opt_flags":
4559 * OPT_GLOBAL: Write global option values and fresh values of
4560 * buffer-local options (used for start of a session
4561 * file).
4562 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4563 * curwin (used for a vimrc file).
4564 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4565 * and local values for window-local options of
4566 * curwin. Local values are also written when at the
4567 * default value, because a modeline or autocommand
4568 * may have set them when doing ":edit file" and the
4569 * user has set them back at the default or fresh
4570 * value.
4571 * When "local_only" is TRUE, don't write fresh
4572 * values, only local values (for ":mkview").
4573 * (fresh value = value used for a new buffer or window for a local option).
4574 *
4575 * Return FAIL on error, OK otherwise.
4576 */
4577 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004578makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579{
4580 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004581 char_u *varp; // currently used value
4582 char_u *varp_fresh; // local value
4583 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 char *cmd;
4585 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004586 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 /*
4589 * The options that don't have a default (terminal name, columns, lines)
4590 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004591 * Do the loop over "options[]" twice: once for options with the
4592 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004594 for (pri = 1; pri >= 0; --pri)
4595 {
4596 for (p = &options[0]; !istermoption(p); p++)
4597 if (!(p->flags & P_NO_MKRC)
4598 && !istermoption(p)
4599 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004601 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4603 continue;
4604
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004605 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4606 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4608 continue;
4609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004610 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004612 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 continue;
4614
4615 round = 2;
4616 if (p->indir != PV_NONE)
4617 {
4618 if (p->var == VAR_WIN)
4619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004620 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (!(opt_flags & OPT_LOCAL))
4622 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004623 // When fresh value of window-local option is not at the
4624 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4626 {
4627 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004628 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
4630 round = 1;
4631 varp_local = varp;
4632 varp = varp_fresh;
4633 }
4634 }
4635 }
4636 }
4637
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004638 // Round 1: fresh value for window-local options.
4639 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 for ( ; round <= 2; varp = varp_local, ++round)
4641 {
4642 if (round == 1 || (opt_flags & OPT_GLOBAL))
4643 cmd = "set";
4644 else
4645 cmd = "setlocal";
4646
4647 if (p->flags & P_BOOL)
4648 {
4649 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4650 return FAIL;
4651 }
4652 else if (p->flags & P_NUM)
4653 {
4654 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4655 return FAIL;
4656 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004657 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004659 int do_endif = FALSE;
4660
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004661 // Don't set 'syntax' and 'filetype' again if the value is
4662 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004663 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004664#if defined(FEAT_SYN_HL)
4665 p->indir == PV_SYN ||
4666#endif
4667 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
4669 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4670 *(char_u **)(varp)) < 0
4671 || put_eol(fd) < 0)
4672 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004673 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004676 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004678 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
4680 if (put_line(fd, "endif") == FAIL)
4681 return FAIL;
4682 }
4683 }
4684 }
4685 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 return OK;
4688}
4689
4690#if defined(FEAT_FOLDING) || defined(PROTO)
4691/*
4692 * Generate set commands for the local fold options only. Used when
4693 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4694 */
4695 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004696makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004698 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004700 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 == FAIL
4702# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004703 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004705 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 == FAIL
4707 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4708 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4709 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4710 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4711 )
4712 return FAIL;
4713
4714 return OK;
4715}
4716#endif
4717
4718 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004719put_setstring(
4720 FILE *fd,
4721 char *cmd,
4722 char *name,
4723 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004724 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
4726 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004727 char_u *buf = NULL;
4728 char_u *part = NULL;
4729 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4732 return FAIL;
4733 if (*valuep != NULL)
4734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 // Output 'pastetoggle' as key names. For other
4736 // options some characters have to be escaped with
4737 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (valuep == &p_pt)
4739 {
4740 s = *valuep;
4741 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004742 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return FAIL;
4744 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004745 // expand the option value, replace $HOME by ~
4746 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004748 int size = (int)STRLEN(*valuep) + 1;
4749
4750 // replace home directory in the whole option value into "buf"
4751 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004752 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004753 goto fail;
4754 home_replace(NULL, *valuep, buf, size, FALSE);
4755
4756 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004757 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004758 // can be expanded when read back.
4759 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4760 && vim_strchr(*valuep, ',') != NULL)
4761 {
4762 part = alloc(size);
4763 if (part == NULL)
4764 goto fail;
4765
4766 // write line break to clear the option, e.g. ':set rtp='
4767 if (put_eol(fd) == FAIL)
4768 goto fail;
4769
4770 p = buf;
4771 while (*p != NUL)
4772 {
4773 // for each comma separated option part, append value to
4774 // the option, :set rtp+=value
4775 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4776 goto fail;
4777 (void)copy_option_part(&p, part, size, ",");
4778 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4779 goto fail;
4780 }
4781 vim_free(buf);
4782 vim_free(part);
4783 return OK;
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004786 {
4787 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004789 }
4790 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 else if (put_escstr(fd, *valuep, 2) == FAIL)
4793 return FAIL;
4794 }
4795 if (put_eol(fd) < 0)
4796 return FAIL;
4797 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004798fail:
4799 vim_free(buf);
4800 vim_free(part);
4801 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802}
4803
4804 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004805put_setnum(
4806 FILE *fd,
4807 char *cmd,
4808 char *name,
4809 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 long wc;
4812
4813 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4814 return FAIL;
4815 if (wc_use_keyname((char_u *)valuep, &wc))
4816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004817 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4819 return FAIL;
4820 }
4821 else if (fprintf(fd, "%ld", *valuep) < 0)
4822 return FAIL;
4823 if (put_eol(fd) < 0)
4824 return FAIL;
4825 return OK;
4826}
4827
4828 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004829put_setbool(
4830 FILE *fd,
4831 char *cmd,
4832 char *name,
4833 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004836 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4838 || put_eol(fd) < 0)
4839 return FAIL;
4840 return OK;
4841}
4842
4843/*
4844 * Clear all the terminal options.
4845 * If the option has been allocated, free the memory.
4846 * Terminal options are never hidden or indirect.
4847 */
4848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004849clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 /*
4852 * Reset a few things before clearing the old options. This may cause
4853 * outputting a few things that the terminal doesn't understand, but the
4854 * screen will be cleared later, so this is OK.
4855 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004856 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004858 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#endif
4860#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 // When starting the GUI close the display opened for the clipboard.
4862 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (gui.starting)
4864 clear_xterm_clip();
4865#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004866 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004868 free_termoptions();
4869}
4870
4871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004872free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004873{
4874 struct vimoption *p;
4875
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004876 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 if (istermoption(p))
4878 {
4879 if (p->flags & P_ALLOCED)
4880 free_string_option(*(char_u **)(p->var));
4881 if (p->flags & P_DEF_ALLOCED)
4882 free_string_option(p->def_val[VI_DEFAULT]);
4883 *(char_u **)(p->var) = empty_option;
4884 p->def_val[VI_DEFAULT] = empty_option;
4885 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004886#ifdef FEAT_EVAL
4887 // remember where the option was cleared
4888 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891 clear_termcodes();
4892}
4893
4894/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004895 * Free the string for one term option, if it was allocated.
4896 * Set the string to empty_option and clear allocated flag.
4897 * "var" points to the option value.
4898 */
4899 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004900free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004901{
4902 struct vimoption *p;
4903
4904 for (p = &options[0]; p->fullname != NULL; p++)
4905 if (p->var == var)
4906 {
4907 if (p->flags & P_ALLOCED)
4908 free_string_option(*(char_u **)(p->var));
4909 *(char_u **)(p->var) = empty_option;
4910 p->flags &= ~P_ALLOCED;
4911 break;
4912 }
4913}
4914
4915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 * Set the terminal option defaults to the current value.
4917 * Used after setting the terminal name.
4918 */
4919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004920set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 struct vimoption *p;
4923
4924 for (p = &options[0]; p->fullname != NULL; p++)
4925 {
4926 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4927 {
4928 if (p->flags & P_DEF_ALLOCED)
4929 {
4930 free_string_option(p->def_val[VI_DEFAULT]);
4931 p->flags &= ~P_DEF_ALLOCED;
4932 }
4933 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4934 if (p->flags & P_ALLOCED)
4935 {
4936 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004937 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939 }
4940 }
4941}
4942
4943/*
4944 * return TRUE if 'p' starts with 't_'
4945 */
4946 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004947istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
4949 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4950}
4951
Bram Moolenaardac13472019-09-16 21:06:21 +02004952/*
4953 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4954 */
4955 int
4956istermoption_idx(int opt_idx)
4957{
4958 return istermoption(&options[opt_idx]);
4959}
4960
Bram Moolenaar113e1072019-01-20 15:30:40 +01004961#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004963 * Unset local option value, similar to ":set opt<".
4964 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004965 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004966unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004967{
4968 struct vimoption *p;
4969 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004970 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004971
4972 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004973 if (opt_idx < 0)
4974 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004975 p = &(options[opt_idx]);
4976
4977 switch ((int)p->indir)
4978 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004979 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004980 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004981 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004982 break;
4983 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004984 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004985 break;
4986 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004987 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004988 break;
4989 case PV_AR:
4990 buf->b_p_ar = -1;
4991 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004992 case PV_BKC:
4993 clear_string_option(&buf->b_p_bkc);
4994 buf->b_bkc_flags = 0;
4995 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004996 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004997 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004998 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01004999 case PV_TC:
5000 clear_string_option(&buf->b_p_tc);
5001 buf->b_tc_flags = 0;
5002 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005003 case PV_SISO:
5004 curwin->w_p_siso = -1;
5005 break;
5006 case PV_SO:
5007 curwin->w_p_so = -1;
5008 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005009#ifdef FEAT_FIND_ID
5010 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005011 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005012 break;
5013 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005014 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005015 break;
5016#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005017 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005018 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005019 break;
5020 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005021 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005022 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005023 case PV_FP:
5024 clear_string_option(&buf->b_p_fp);
5025 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005026#ifdef FEAT_QUICKFIX
5027 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005028 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005029 break;
5030 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005031 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005032 break;
5033 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005034 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005035 break;
5036#endif
5037#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5038 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005039 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005040 break;
5041#endif
5042#if defined(FEAT_CRYPT)
5043 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005044 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005045 break;
5046#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005047#ifdef FEAT_LINEBREAK
5048 case PV_SBR:
5049 clear_string_option(&((win_T *)from)->w_p_sbr);
5050 break;
5051#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052#ifdef FEAT_STL_OPT
5053 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005054 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005055 break;
5056#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005057 case PV_UL:
5058 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5059 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005060#ifdef FEAT_LISP
5061 case PV_LW:
5062 clear_string_option(&buf->b_p_lw);
5063 break;
5064#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005065 case PV_MENC:
5066 clear_string_option(&buf->b_p_menc);
5067 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005068 case PV_LCS:
5069 clear_string_option(&((win_T *)from)->w_p_lcs);
5070 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5071 redraw_later(NOT_VALID);
5072 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073 }
5074}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005075#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005076
5077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 * Get pointer to option variable, depending on local or global scope.
5079 */
5080 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5084 {
5085 if (p->var == VAR_WIN)
5086 return (char_u *)GLOBAL_WO(get_varp(p));
5087 return p->var;
5088 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005089 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 switch ((int)p->indir)
5092 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005093 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005095 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5096 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5097 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005099 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5100 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5101 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005102 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005103 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005104 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005105 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5106 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005108 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5109 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005111 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5112 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005113#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5114 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5115#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005116#if defined(FEAT_CRYPT)
5117 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5118#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005119#ifdef FEAT_LINEBREAK
5120 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5121#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005122#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005123 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005124#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005125 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005126#ifdef FEAT_LISP
5127 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5128#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005129 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005130 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005131 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5132
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005134 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 return get_varp(p);
5137}
5138
5139/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005140 * Get pointer to option variable at 'opt_idx', depending on local or global
5141 * scope.
5142 */
5143 char_u *
5144get_option_varp_scope(int opt_idx, int opt_flags)
5145{
5146 return get_varp_scope(&(options[opt_idx]), opt_flags);
5147}
5148
5149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 * Get pointer to option variable.
5151 */
5152 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005153get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005155 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (p->var == NULL)
5157 return NULL;
5158
5159 switch ((int)p->indir)
5160 {
5161 case PV_NONE: return p->var;
5162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005163 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005164 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005166 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005168 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005170 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005172 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005174 case PV_TC: return *curbuf->b_p_tc != NUL
5175 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005176 case PV_BKC: return *curbuf->b_p_bkc != NUL
5177 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005178 case PV_SISO: return curwin->w_p_siso >= 0
5179 ? (char_u *)&(curwin->w_p_siso) : p->var;
5180 case PV_SO: return curwin->w_p_so >= 0
5181 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005183 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005185 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5187#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005188 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005190 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005192 case PV_FP: return *curbuf->b_p_fp != NUL
5193 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005195 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005197 case PV_GP: return *curbuf->b_p_gp != NUL
5198 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5199 case PV_MP: return *curbuf->b_p_mp != NUL
5200 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005202#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5203 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5204 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5205#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005206#if defined(FEAT_CRYPT)
5207 case PV_CM: return *curbuf->b_p_cm != NUL
5208 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5209#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005210#ifdef FEAT_LINEBREAK
5211 case PV_SBR: return *curwin->w_p_sbr != NUL
5212 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5213#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005214#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005215 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005216 ? (char_u *)&(curwin->w_p_stl) : p->var;
5217#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005218 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5219 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005220#ifdef FEAT_LISP
5221 case PV_LW: return *curbuf->b_p_lw != NUL
5222 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5223#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005224 case PV_MENC: return *curbuf->b_p_menc != NUL
5225 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226#ifdef FEAT_ARABIC
5227 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5228#endif
5229 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005230 case PV_LCS: return *curwin->w_p_lcs != NUL
5231 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005232#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005233 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5234#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005235#ifdef FEAT_SYN_HL
5236 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5237 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005238 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005239 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_DIFF
5242 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5243#endif
5244#ifdef FEAT_FOLDING
5245 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5246 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5247 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5248 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5249 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5250 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5251 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5252# ifdef FEAT_EVAL
5253 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5254 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5255# endif
5256 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5257#endif
5258 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005259 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005260#ifdef FEAT_LINEBREAK
5261 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005264 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005265#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5267#endif
5268#ifdef FEAT_RIGHTLEFT
5269 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5270 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5271#endif
5272 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5273 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5274#ifdef FEAT_LINEBREAK
5275 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005276 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5277 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005279 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005281 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005282#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005283 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5284 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5285#endif
5286#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005287 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5288 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5289 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5293 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5296 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5298 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5299#ifdef FEAT_CINDENT
5300 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5301 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5302 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5303#endif
5304#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5305 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#ifdef FEAT_FOLDING
5309 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005312#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005313 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005315#ifdef FEAT_COMPL_FUNC
5316 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005317 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005318#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005319#ifdef FEAT_EVAL
5320 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005323 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005329 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5331 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5332 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5333 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5334#ifdef FEAT_FIND_ID
5335# ifdef FEAT_EVAL
5336 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5337# endif
5338#endif
5339#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5340 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5341 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5342#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005343#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005344 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#ifdef FEAT_CRYPT
5347 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5348#endif
5349#ifdef FEAT_LISP
5350 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5351#endif
5352 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5353 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5354 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5355 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5356 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005358#ifdef FEAT_TEXTOBJ
5359 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5362#ifdef FEAT_SMARTINDENT
5363 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5367#ifdef FEAT_SEARCHPATH
5368 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5369#endif
5370 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5371#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005372 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005373 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5374#endif
5375#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005376 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5377 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5378 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005379 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380#endif
5381 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5382 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5383 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5384 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005385#ifdef FEAT_PERSISTENT_UNDO
5386 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5389#ifdef FEAT_KEYMAP
5390 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5391#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005392#ifdef FEAT_SIGNS
5393 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5394#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005395#ifdef FEAT_VARTABS
5396 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5397 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5398#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005399 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005401 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 return (char_u *)&(curbuf->b_p_wm);
5403}
5404
5405/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005406 * Return a pointer to the variable for option at 'opt_idx'
5407 */
5408 char_u *
5409get_option_var(int opt_idx)
5410{
5411 return options[opt_idx].var;
5412}
5413
5414/*
5415 * Return the full name of the option at 'opt_idx'
5416 */
5417 char_u *
5418get_option_fullname(int opt_idx)
5419{
5420 return (char_u *)options[opt_idx].fullname;
5421}
5422
5423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 * Get the value of 'equalprg', either the buffer-local one or the global one.
5425 */
5426 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005427get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 if (*curbuf->b_p_ep == NUL)
5430 return p_ep;
5431 return curbuf->b_p_ep;
5432}
5433
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434/*
5435 * Copy options from one window to another.
5436 * Used when splitting a window.
5437 */
5438 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005439win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5442 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005443 after_copy_winopt(wp_to);
5444}
5445
5446/*
5447 * After copying window options: update variables depending on options.
5448 */
5449 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005450after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005451{
5452#ifdef FEAT_LINEBREAK
5453 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005454#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005455#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005456 fill_culopt_flags(NULL, wp);
5457 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005458#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005459 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462/*
5463 * Copy the options from one winopt_T to another.
5464 * Doesn't free the old option values in "to", use clear_winopt() for that.
5465 * The 'scroll' option is not copied, because it depends on the window height.
5466 * The 'previewwindow' option is reset, there can be only one preview window.
5467 */
5468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005469copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471#ifdef FEAT_ARABIC
5472 to->wo_arab = from->wo_arab;
5473#endif
5474 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005475 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005477 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005478#ifdef FEAT_LINEBREAK
5479 to->wo_nuw = from->wo_nuw;
5480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481#ifdef FEAT_RIGHTLEFT
5482 to->wo_rl = from->wo_rl;
5483 to->wo_rlc = vim_strsave(from->wo_rlc);
5484#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005485#ifdef FEAT_LINEBREAK
5486 to->wo_sbr = vim_strsave(from->wo_sbr);
5487#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005488#ifdef FEAT_STL_OPT
5489 to->wo_stl = vim_strsave(from->wo_stl);
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005492#ifdef FEAT_DIFF
5493 to->wo_wrap_save = from->wo_wrap_save;
5494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495#ifdef FEAT_LINEBREAK
5496 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005497 to->wo_bri = from->wo_bri;
5498 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005500 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005502 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005503 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005504 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005505#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005506 to->wo_spell = from->wo_spell;
5507#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005508#ifdef FEAT_SYN_HL
5509 to->wo_cuc = from->wo_cuc;
5510 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005511 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005512 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#ifdef FEAT_DIFF
5515 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005516 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005518#ifdef FEAT_CONCEAL
5519 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005520 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005521#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005522#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005523 to->wo_twk = vim_strsave(from->wo_twk);
5524 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526#ifdef FEAT_FOLDING
5527 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005528 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005530 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 to->wo_fdi = vim_strsave(from->wo_fdi);
5532 to->wo_fml = from->wo_fml;
5533 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005534 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005536 to->wo_fdm_save = from->wo_diff_saved
5537 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 to->wo_fdn = from->wo_fdn;
5539# ifdef FEAT_EVAL
5540 to->wo_fde = vim_strsave(from->wo_fde);
5541 to->wo_fdt = vim_strsave(from->wo_fdt);
5542# endif
5543 to->wo_fmr = vim_strsave(from->wo_fmr);
5544#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005545#ifdef FEAT_SIGNS
5546 to->wo_scl = vim_strsave(from->wo_scl);
5547#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005548
5549#ifdef FEAT_EVAL
5550 // Copy the script context so that we know where the value was last set.
5551 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5552 sizeof(to->wo_script_ctx));
5553#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005554 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555}
5556
5557/*
5558 * Check string options in a window for a NULL value.
5559 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005561check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 check_winopt(&win->w_onebuf_opt);
5564 check_winopt(&win->w_allbuf_opt);
5565}
5566
5567/*
5568 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5569 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005571check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
5573#ifdef FEAT_FOLDING
5574 check_string_option(&wop->wo_fdi);
5575 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005576 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577# ifdef FEAT_EVAL
5578 check_string_option(&wop->wo_fde);
5579 check_string_option(&wop->wo_fdt);
5580# endif
5581 check_string_option(&wop->wo_fmr);
5582#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005583#ifdef FEAT_SIGNS
5584 check_string_option(&wop->wo_scl);
5585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#ifdef FEAT_RIGHTLEFT
5587 check_string_option(&wop->wo_rlc);
5588#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005589#ifdef FEAT_LINEBREAK
5590 check_string_option(&wop->wo_sbr);
5591#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005592#ifdef FEAT_STL_OPT
5593 check_string_option(&wop->wo_stl);
5594#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005595#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005596 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005597 check_string_option(&wop->wo_cc);
5598#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005599#ifdef FEAT_CONCEAL
5600 check_string_option(&wop->wo_cocu);
5601#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005602#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005603 check_string_option(&wop->wo_twk);
5604 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005605#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005606#ifdef FEAT_LINEBREAK
5607 check_string_option(&wop->wo_briopt);
5608#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005609 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005610 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611}
5612
5613/*
5614 * Free the allocated memory inside a winopt_T.
5615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005617clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619#ifdef FEAT_FOLDING
5620 clear_string_option(&wop->wo_fdi);
5621 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005622 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623# ifdef FEAT_EVAL
5624 clear_string_option(&wop->wo_fde);
5625 clear_string_option(&wop->wo_fdt);
5626# endif
5627 clear_string_option(&wop->wo_fmr);
5628#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005629#ifdef FEAT_SIGNS
5630 clear_string_option(&wop->wo_scl);
5631#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005632#ifdef FEAT_LINEBREAK
5633 clear_string_option(&wop->wo_briopt);
5634#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005635 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636#ifdef FEAT_RIGHTLEFT
5637 clear_string_option(&wop->wo_rlc);
5638#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005639#ifdef FEAT_LINEBREAK
5640 clear_string_option(&wop->wo_sbr);
5641#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005642#ifdef FEAT_STL_OPT
5643 clear_string_option(&wop->wo_stl);
5644#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005646 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647 clear_string_option(&wop->wo_cc);
5648#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005649#ifdef FEAT_CONCEAL
5650 clear_string_option(&wop->wo_cocu);
5651#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005652#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005653 clear_string_option(&wop->wo_twk);
5654 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005655#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005656 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657}
5658
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005659#ifdef FEAT_EVAL
5660// Index into the options table for a buffer-local option enum.
5661static int buf_opt_idx[BV_COUNT];
5662# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5663
5664/*
5665 * Initialize buf_opt_idx[] if not done already.
5666 */
5667 static void
5668init_buf_opt_idx(void)
5669{
5670 static int did_init_buf_opt_idx = FALSE;
5671 int i;
5672
5673 if (did_init_buf_opt_idx)
5674 return;
5675 did_init_buf_opt_idx = TRUE;
5676 for (i = 0; !istermoption_idx(i); i++)
5677 if (options[i].indir & PV_BUF)
5678 buf_opt_idx[options[i].indir & PV_MASK] = i;
5679}
5680#else
5681# define COPY_OPT_SCTX(buf, bv)
5682#endif
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684/*
5685 * Copy global option values to local options for one buffer.
5686 * Used when creating a new buffer and sometimes when entering a buffer.
5687 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005688 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5690 * appropriate.
5691 * BCO_NOHELP Don't copy the values to a help buffer.
5692 */
5693 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005694buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
5696 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005697 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 int dont_do_help;
5699 int did_isk = FALSE;
5700
5701 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * Skip this when the option defaults have not been set yet. Happens when
5703 * main() allocates the first buffer.
5704 */
5705 if (p_cpo != NULL)
5706 {
5707 /*
5708 * Always copy when entering and 'cpo' contains 'S'.
5709 * Don't copy when already initialized.
5710 * Don't copy when 'cpo' contains 's' and not entering.
5711 * 'S' BCO_ENTER initialized 's' should_copy
5712 * yes yes X X TRUE
5713 * yes no yes X FALSE
5714 * no X yes X FALSE
5715 * X no no yes FALSE
5716 * X no no no TRUE
5717 * no yes no X TRUE
5718 */
5719 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5720 && (buf->b_p_initialized
5721 || (!(flags & BCO_ENTER)
5722 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5723 should_copy = FALSE;
5724
5725 if (should_copy || (flags & BCO_ALWAYS))
5726 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005727#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005728 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005729 init_buf_opt_idx();
5730#endif
5731 // Don't copy the options specific to a help buffer when
5732 // BCO_NOHELP is given or the options were initialized already
5733 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5735 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005736 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
5738 save_p_isk = buf->b_p_isk;
5739 buf->b_p_isk = NULL;
5740 }
5741 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005742 * Always free the allocated strings. If not already initialized,
5743 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 */
5745 if (!buf->b_p_initialized)
5746 {
5747 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005748 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005751 switch (*p_ffs)
5752 {
5753 case 'm':
5754 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5755 case 'd':
5756 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5757 case 'u':
5758 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5759 default:
5760 buf->b_p_ff = vim_strsave(p_ff);
5761 }
5762 if (buf->b_p_ff != NULL)
5763 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 buf->b_p_bh = empty_option;
5765 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 else
5768 free_buf_options(buf, FALSE);
5769
5770 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005771 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 buf->b_p_ai_nopaste = p_ai_nopaste;
5773 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005774 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005776 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 buf->b_p_tw_nopaste = p_tw_nopaste;
5778 buf->b_p_tw_nobin = p_tw_nobin;
5779 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005780 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 buf->b_p_wm_nopaste = p_wm_nopaste;
5782 buf->b_p_wm_nobin = p_wm_nobin;
5783 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005784 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005785 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005786 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005787 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005788 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005790 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005792 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005794 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 buf->b_p_ml_nobin = p_ml_nobin;
5796 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005797 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005798 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005799 buf->b_p_swf = FALSE;
5800 else
5801 {
5802 buf->b_p_swf = p_swf;
5803 COPY_OPT_SCTX(buf, BV_INF);
5804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005806 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005807#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005808 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005809 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005811#ifdef FEAT_COMPL_FUNC
5812 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005813 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005814 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005815 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005816#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005817#ifdef FEAT_EVAL
5818 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005819 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005822 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005824#ifdef FEAT_VARTABS
5825 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005826 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005827 if (p_vsts && p_vsts != empty_option)
5828 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5829 else
5830 buf->b_p_vsts_array = 0;
5831 buf->b_p_vsts_nopaste = p_vsts_nopaste
5832 ? vim_strsave(p_vsts_nopaste) : NULL;
5833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005835 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005837 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838#ifdef FEAT_FOLDING
5839 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005840 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#endif
5842 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005843 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005844 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005845 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005846 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5847 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005849 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005851 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#ifdef FEAT_SMARTINDENT
5853 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005854 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#endif
5856 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005857 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858#ifdef FEAT_CINDENT
5859 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005860 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005862 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005864 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005866 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005869 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5871 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005872 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873#endif
5874#ifdef FEAT_LISP
5875 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005876 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#endif
5878#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005879 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005881 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005882 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005883 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005884#endif
5885#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005886 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005887 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005888 (void)compile_cap_prog(&buf->b_s);
5889 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005890 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005891 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005892 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005893 buf->b_s.b_p_spo = vim_strsave(p_spo);
5894 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#endif
5896#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5897 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005898 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005902 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005903#if defined(FEAT_EVAL)
5904 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005905 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#ifdef FEAT_CRYPT
5908 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005909 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#endif
5911#ifdef FEAT_SEARCHPATH
5912 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005913 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914#endif
5915#ifdef FEAT_KEYMAP
5916 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005917 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 buf->b_kmap_state |= KEYMAP_INIT;
5919#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005920#ifdef FEAT_TERMINAL
5921 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005922 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005923#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005924 // This isn't really an option, but copying the langmap and IME
5925 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005927 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005929 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005931 // options that are normally global but also have a local value
5932 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005934 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005935 buf->b_p_bkc = empty_option;
5936 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937#ifdef FEAT_QUICKFIX
5938 buf->b_p_gp = empty_option;
5939 buf->b_p_mp = empty_option;
5940 buf->b_p_efm = empty_option;
5941#endif
5942 buf->b_p_ep = empty_option;
5943 buf->b_p_kp = empty_option;
5944 buf->b_p_path = empty_option;
5945 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005946 buf->b_p_tc = empty_option;
5947 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948#ifdef FEAT_FIND_ID
5949 buf->b_p_def = empty_option;
5950 buf->b_p_inc = empty_option;
5951# ifdef FEAT_EVAL
5952 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954# endif
5955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 buf->b_p_dict = empty_option;
5957 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005958#ifdef FEAT_TEXTOBJ
5959 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005960 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005961#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005962#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5963 buf->b_p_bexpr = empty_option;
5964#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005965#if defined(FEAT_CRYPT)
5966 buf->b_p_cm = empty_option;
5967#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005968#ifdef FEAT_PERSISTENT_UNDO
5969 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005971#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005972#ifdef FEAT_LISP
5973 buf->b_p_lw = empty_option;
5974#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005975 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
5977 /*
5978 * Don't copy the options set by ex_help(), use the saved values,
5979 * when going from a help buffer to a non-help buffer.
5980 * Don't touch these at all when BCO_NOHELP is used and going from
5981 * or to a help buffer.
5982 */
5983 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005986#ifdef FEAT_VARTABS
5987 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5988 tabstop_set(p_vts, &buf->b_p_vts_array);
5989 else
5990 buf->b_p_vts_array = NULL;
5991#endif
5992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 else
5994 {
5995 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005996 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 did_isk = TRUE;
5998 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005999#ifdef FEAT_VARTABS
6000 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006002 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6003 tabstop_set(p_vts, &buf->b_p_vts_array);
6004 else
6005 buf->b_p_vts_array = NULL;
6006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (buf->b_p_bt[0] == 'h')
6009 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006011 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 }
6013 }
6014
6015 /*
6016 * When the options should be copied (ignoring BCO_ALWAYS), set the
6017 * flag that indicates that the options have been initialized.
6018 */
6019 if (should_copy)
6020 buf->b_p_initialized = TRUE;
6021 }
6022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006023 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 if (did_isk)
6025 (void)buf_init_chartab(buf, FALSE);
6026}
6027
6028/*
6029 * Reset the 'modifiable' option and its default value.
6030 */
6031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006032reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 int opt_idx;
6035
6036 curbuf->b_p_ma = FALSE;
6037 p_ma = FALSE;
6038 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006039 if (opt_idx >= 0)
6040 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041}
6042
6043/*
6044 * Set the global value for 'iminsert' to the local value.
6045 */
6046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006047set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 p_iminsert = curbuf->b_p_iminsert;
6050}
6051
6052/*
6053 * Set the global value for 'imsearch' to the local value.
6054 */
6055 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006056set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 p_imsearch = curbuf->b_p_imsearch;
6059}
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061static int expand_option_idx = -1;
6062static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6063static int expand_option_flags = 0;
6064
6065 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006066set_context_in_set_cmd(
6067 expand_T *xp,
6068 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006069 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006072 long_u flags = 0; // init for GCC
6073 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 char_u *p;
6075 char_u *s;
6076 int is_term_option = FALSE;
6077 int key;
6078
6079 expand_option_flags = opt_flags;
6080
6081 xp->xp_context = EXPAND_SETTINGS;
6082 if (*arg == NUL)
6083 {
6084 xp->xp_pattern = arg;
6085 return;
6086 }
6087 p = arg + STRLEN(arg) - 1;
6088 if (*p == ' ' && *(p - 1) != '\\')
6089 {
6090 xp->xp_pattern = p + 1;
6091 return;
6092 }
6093 while (p > arg)
6094 {
6095 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006096 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 if (*p == ' ' || *p == ',')
6098 {
6099 while (s > arg && *(s - 1) == '\\')
6100 --s;
6101 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006102 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (*p == ' ' && ((p - s) & 1) == 0)
6104 {
6105 ++p;
6106 break;
6107 }
6108 --p;
6109 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006110 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {
6112 xp->xp_context = EXPAND_BOOL_SETTINGS;
6113 p += 2;
6114 }
6115 if (STRNCMP(p, "inv", 3) == 0)
6116 {
6117 xp->xp_context = EXPAND_BOOL_SETTINGS;
6118 p += 3;
6119 }
6120 xp->xp_pattern = arg = p;
6121 if (*arg == '<')
6122 {
6123 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006124 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 return;
6126 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006127 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 xp->xp_context = EXPAND_NOTHING;
6130 return;
6131 }
6132 nextchar = *++p;
6133 is_term_option = TRUE;
6134 expand_option_name[2] = KEY2TERMCAP0(key);
6135 expand_option_name[3] = KEY2TERMCAP1(key);
6136 }
6137 else
6138 {
6139 if (p[0] == 't' && p[1] == '_')
6140 {
6141 p += 2;
6142 if (*p != NUL)
6143 ++p;
6144 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006145 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 nextchar = *++p;
6147 is_term_option = TRUE;
6148 expand_option_name[2] = p[-2];
6149 expand_option_name[3] = p[-1];
6150 }
6151 else
6152 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006153 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6155 p++;
6156 if (*p == NUL)
6157 return;
6158 nextchar = *p;
6159 *p = NUL;
6160 opt_idx = findoption(arg);
6161 *p = nextchar;
6162 if (opt_idx == -1 || options[opt_idx].var == NULL)
6163 {
6164 xp->xp_context = EXPAND_NOTHING;
6165 return;
6166 }
6167 flags = options[opt_idx].flags;
6168 if (flags & P_BOOL)
6169 {
6170 xp->xp_context = EXPAND_NOTHING;
6171 return;
6172 }
6173 }
6174 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006175 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6177 {
6178 ++p;
6179 nextchar = '=';
6180 }
6181 if ((nextchar != '=' && nextchar != ':')
6182 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6183 {
6184 xp->xp_context = EXPAND_UNSUCCESSFUL;
6185 return;
6186 }
6187 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6188 {
6189 xp->xp_context = EXPAND_OLD_SETTING;
6190 if (is_term_option)
6191 expand_option_idx = -1;
6192 else
6193 expand_option_idx = opt_idx;
6194 xp->xp_pattern = p + 1;
6195 return;
6196 }
6197 xp->xp_context = EXPAND_NOTHING;
6198 if (is_term_option || (flags & P_NUM))
6199 return;
6200
6201 xp->xp_pattern = p + 1;
6202
6203 if (flags & P_EXPAND)
6204 {
6205 p = options[opt_idx].var;
6206 if (p == (char_u *)&p_bdir
6207 || p == (char_u *)&p_dir
6208 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006209 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 || p == (char_u *)&p_rtp
6211#ifdef FEAT_SEARCHPATH
6212 || p == (char_u *)&p_cdpath
6213#endif
6214#ifdef FEAT_SESSION
6215 || p == (char_u *)&p_vdir
6216#endif
6217 )
6218 {
6219 xp->xp_context = EXPAND_DIRECTORIES;
6220 if (p == (char_u *)&p_path
6221#ifdef FEAT_SEARCHPATH
6222 || p == (char_u *)&p_cdpath
6223#endif
6224 )
6225 xp->xp_backslash = XP_BS_THREE;
6226 else
6227 xp->xp_backslash = XP_BS_ONE;
6228 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006229 else if (p == (char_u *)&p_ft)
6230 {
6231 xp->xp_context = EXPAND_FILETYPE;
6232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 else
6234 {
6235 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006236 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (p == (char_u *)&p_tags)
6238 xp->xp_backslash = XP_BS_THREE;
6239 else
6240 xp->xp_backslash = XP_BS_ONE;
6241 }
6242 }
6243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006244 // For an option that is a list of file names, find the start of the
6245 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6247 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006248 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (*p == ' ' || *p == ',')
6250 {
6251 s = p;
6252 while (s > xp->xp_pattern && *(s - 1) == '\\')
6253 --s;
6254 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6255 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6256 {
6257 xp->xp_pattern = p + 1;
6258 break;
6259 }
6260 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006261
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006262#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006263 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006264 if (options[opt_idx].var == (char_u *)&p_sps
6265 && STRNCMP(p, "file:", 5) == 0)
6266 {
6267 xp->xp_pattern = p + 5;
6268 break;
6269 }
6270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
6272
6273 return;
6274}
6275
6276 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006277ExpandSettings(
6278 expand_T *xp,
6279 regmatch_T *regmatch,
6280 int *num_file,
6281 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006283 int num_normal = 0; // Nr of matching non-term-code settings
6284 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 int opt_idx;
6286 int match;
6287 int count = 0;
6288 char_u *str;
6289 int loop;
6290 int is_term_opt;
6291 char_u name_buf[MAX_KEY_NAME_LEN];
6292 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006293 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006295 // do this loop twice:
6296 // loop == 0: count the number of matching options
6297 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 for (loop = 0; loop <= 1; ++loop)
6299 {
6300 regmatch->rm_ic = ic;
6301 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6302 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006303 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6304 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6306 {
6307 if (loop == 0)
6308 num_normal++;
6309 else
6310 (*file)[count++] = vim_strsave((char_u *)names[match]);
6311 }
6312 }
6313 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6314 opt_idx++)
6315 {
6316 if (options[opt_idx].var == NULL)
6317 continue;
6318 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6319 && !(options[opt_idx].flags & P_BOOL))
6320 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006321 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (is_term_opt && num_normal > 0)
6323 continue;
6324 match = FALSE;
6325 if (vim_regexec(regmatch, str, (colnr_T)0)
6326 || (options[opt_idx].shortname != NULL
6327 && vim_regexec(regmatch,
6328 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6329 match = TRUE;
6330 else if (is_term_opt)
6331 {
6332 name_buf[0] = '<';
6333 name_buf[1] = 't';
6334 name_buf[2] = '_';
6335 name_buf[3] = str[2];
6336 name_buf[4] = str[3];
6337 name_buf[5] = '>';
6338 name_buf[6] = NUL;
6339 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6340 {
6341 match = TRUE;
6342 str = name_buf;
6343 }
6344 }
6345 if (match)
6346 {
6347 if (loop == 0)
6348 {
6349 if (is_term_opt)
6350 num_term++;
6351 else
6352 num_normal++;
6353 }
6354 else
6355 (*file)[count++] = vim_strsave(str);
6356 }
6357 }
6358 /*
6359 * Check terminal key codes, these are not in the option table
6360 */
6361 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6362 {
6363 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6364 {
6365 if (!isprint(str[0]) || !isprint(str[1]))
6366 continue;
6367
6368 name_buf[0] = 't';
6369 name_buf[1] = '_';
6370 name_buf[2] = str[0];
6371 name_buf[3] = str[1];
6372 name_buf[4] = NUL;
6373
6374 match = FALSE;
6375 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6376 match = TRUE;
6377 else
6378 {
6379 name_buf[0] = '<';
6380 name_buf[1] = 't';
6381 name_buf[2] = '_';
6382 name_buf[3] = str[0];
6383 name_buf[4] = str[1];
6384 name_buf[5] = '>';
6385 name_buf[6] = NUL;
6386
6387 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6388 match = TRUE;
6389 }
6390 if (match)
6391 {
6392 if (loop == 0)
6393 num_term++;
6394 else
6395 (*file)[count++] = vim_strsave(name_buf);
6396 }
6397 }
6398
6399 /*
6400 * Check special key names.
6401 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006402 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6404 {
6405 name_buf[0] = '<';
6406 STRCPY(name_buf + 1, str);
6407 STRCAT(name_buf, ">");
6408
6409 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6410 {
6411 if (loop == 0)
6412 num_term++;
6413 else
6414 (*file)[count++] = vim_strsave(name_buf);
6415 }
6416 }
6417 }
6418 if (loop == 0)
6419 {
6420 if (num_normal > 0)
6421 *num_file = num_normal;
6422 else if (num_term > 0)
6423 *num_file = num_term;
6424 else
6425 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006426 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 if (*file == NULL)
6428 {
6429 *file = (char_u **)"";
6430 return FAIL;
6431 }
6432 }
6433 }
6434 return OK;
6435}
6436
6437 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006438ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006440 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 char_u *buf;
6442
6443 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006444 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (*file == NULL)
6446 return FAIL;
6447
6448 /*
6449 * For a terminal key code expand_option_idx is < 0.
6450 */
6451 if (expand_option_idx < 0)
6452 {
6453 var = find_termcode(expand_option_name + 2);
6454 if (var == NULL)
6455 expand_option_idx = findoption(expand_option_name);
6456 }
6457
6458 if (expand_option_idx >= 0)
6459 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006460 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 option_value2string(&options[expand_option_idx], expand_option_flags);
6462 var = NameBuff;
6463 }
6464 else if (var == NULL)
6465 var = (char_u *)"";
6466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 // A backslash is required before some characters. This is the reverse of
6468 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 buf = vim_strsave_escaped(var, escape_chars);
6470
6471 if (buf == NULL)
6472 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006473 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 return FAIL;
6475 }
6476
6477#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006478 // For MS-Windows et al. we don't double backslashes at the start and
6479 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006480 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 if (var[0] == '\\' && var[1] == '\\'
6482 && expand_option_idx >= 0
6483 && (options[expand_option_idx].flags & P_EXPAND)
6484 && vim_isfilec(var[2])
6485 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006486 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487#endif
6488
6489 *file[0] = buf;
6490 *num_file = 1;
6491 return OK;
6492}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494/*
6495 * Get the value for the numeric or string option *opp in a nice format into
6496 * NameBuff[]. Must not be called with a hidden option!
6497 */
6498 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006499option_value2string(
6500 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006501 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 char_u *varp;
6504
6505 varp = get_varp_scope(opp, opt_flags);
6506
6507 if (opp->flags & P_NUM)
6508 {
6509 long wc = 0;
6510
6511 if (wc_use_keyname(varp, &wc))
6512 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6513 else if (wc != 0)
6514 STRCPY(NameBuff, transchar((int)wc));
6515 else
6516 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6517 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006518 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
6520 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006521 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 NameBuff[0] = NUL;
6523#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006524 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006525 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 STRCPY(NameBuff, "*****");
6527#endif
6528 else if (opp->flags & P_EXPAND)
6529 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006530 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 else if ((char_u **)opp->var == &p_pt)
6532 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6533 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006534 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536}
6537
6538/*
6539 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6540 * printed as a keyname.
6541 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6542 */
6543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006544wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6547 {
6548 *wcp = *(long *)varp;
6549 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6550 return TRUE;
6551 }
6552 return FALSE;
6553}
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 * Return TRUE if "x" is present in 'shortmess' option, or
6557 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6558 */
6559 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006560shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006562 return p_shm != NULL &&
6563 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 || (vim_strchr(p_shm, 'a') != NULL
6565 && vim_strchr((char_u *)SHM_A, x) != NULL));
6566}
6567
6568/*
6569 * paste_option_changed() - Called after p_paste was set or reset.
6570 */
6571 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006572paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 static int old_p_paste = FALSE;
6575 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006576 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577#ifdef FEAT_CMDL_INFO
6578 static int save_ru = 0;
6579#endif
6580#ifdef FEAT_RIGHTLEFT
6581 static int save_ri = 0;
6582 static int save_hkmap = 0;
6583#endif
6584 buf_T *buf;
6585
6586 if (p_paste)
6587 {
6588 /*
6589 * Paste switched from off to on.
6590 * Save the current values, so they can be restored later.
6591 */
6592 if (!old_p_paste)
6593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006594 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006595 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 {
6597 buf->b_p_tw_nopaste = buf->b_p_tw;
6598 buf->b_p_wm_nopaste = buf->b_p_wm;
6599 buf->b_p_sts_nopaste = buf->b_p_sts;
6600 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006601 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006602#ifdef FEAT_VARTABS
6603 if (buf->b_p_vsts_nopaste)
6604 vim_free(buf->b_p_vsts_nopaste);
6605 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6606 ? vim_strsave(buf->b_p_vsts) : NULL;
6607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006610 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006612 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613#ifdef FEAT_CMDL_INFO
6614 save_ru = p_ru;
6615#endif
6616#ifdef FEAT_RIGHTLEFT
6617 save_ri = p_ri;
6618 save_hkmap = p_hkmap;
6619#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006620 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006621 p_ai_nopaste = p_ai;
6622 p_et_nopaste = p_et;
6623 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 p_tw_nopaste = p_tw;
6625 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006626#ifdef FEAT_VARTABS
6627 if (p_vsts_nopaste)
6628 vim_free(p_vsts_nopaste);
6629 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 }
6632
6633 /*
6634 * Always set the option values, also when 'paste' is set when it is
6635 * already on.
6636 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006637 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006638 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006640 buf->b_p_tw = 0; // textwidth is 0
6641 buf->b_p_wm = 0; // wrapmargin is 0
6642 buf->b_p_sts = 0; // softtabstop is 0
6643 buf->b_p_ai = 0; // no auto-indent
6644 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006645#ifdef FEAT_VARTABS
6646 if (buf->b_p_vsts)
6647 free_string_option(buf->b_p_vsts);
6648 buf->b_p_vsts = empty_option;
6649 if (buf->b_p_vsts_array)
6650 vim_free(buf->b_p_vsts_array);
6651 buf->b_p_vsts_array = 0;
6652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
6654
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006655 // set global options
6656 p_sm = 0; // no showmatch
6657 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006660 status_redraw_all(); // redraw to remove the ruler
6661 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662#endif
6663#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006664 p_ri = 0; // no reverse insert
6665 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 p_tw = 0;
6669 p_wm = 0;
6670 p_sts = 0;
6671 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006672#ifdef FEAT_VARTABS
6673 if (p_vsts)
6674 free_string_option(p_vsts);
6675 p_vsts = empty_option;
6676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
6678
6679 /*
6680 * Paste switched from on to off: Restore saved values.
6681 */
6682 else if (old_p_paste)
6683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006684 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006685 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 buf->b_p_tw = buf->b_p_tw_nopaste;
6688 buf->b_p_wm = buf->b_p_wm_nopaste;
6689 buf->b_p_sts = buf->b_p_sts_nopaste;
6690 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006691 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006692#ifdef FEAT_VARTABS
6693 if (buf->b_p_vsts)
6694 free_string_option(buf->b_p_vsts);
6695 buf->b_p_vsts = buf->b_p_vsts_nopaste
6696 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6697 if (buf->b_p_vsts_array)
6698 vim_free(buf->b_p_vsts_array);
6699 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6700 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6701 else
6702 buf->b_p_vsts_array = 0;
6703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006706 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006708 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006711 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 p_ru = save_ru;
6713#endif
6714#ifdef FEAT_RIGHTLEFT
6715 p_ri = save_ri;
6716 p_hkmap = save_hkmap;
6717#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006718 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006719 p_ai = p_ai_nopaste;
6720 p_et = p_et_nopaste;
6721 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 p_tw = p_tw_nopaste;
6723 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006724#ifdef FEAT_VARTABS
6725 if (p_vsts)
6726 free_string_option(p_vsts);
6727 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 }
6730
6731 old_p_paste = p_paste;
6732}
6733
6734/*
6735 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6736 *
6737 * Reset 'compatible' and set the values for options that didn't get set yet
6738 * to the Vim defaults.
6739 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006740 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 */
6742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006743vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006745 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006746 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006747 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 if (!option_was_set((char_u *)"cp"))
6750 {
6751 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006752 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6754 set_option_default(opt_idx, OPT_FREE, FALSE);
6755 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006756 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006758
6759 if (fname != NULL)
6760 {
6761 p = vim_getenv(envname, &dofree);
6762 if (p == NULL)
6763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006764 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006765 p = FullName_save(fname, FALSE);
6766 if (p != NULL)
6767 {
6768 vim_setenv(envname, p);
6769 vim_free(p);
6770 }
6771 }
6772 else if (dofree)
6773 vim_free(p);
6774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
6776
6777/*
6778 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6779 */
6780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006781change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006783 int opt_idx;
6784
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (p_cp != on)
6786 {
6787 p_cp = on;
6788 compatible_set();
6789 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006790 opt_idx = findoption((char_u *)"cp");
6791 if (opt_idx >= 0)
6792 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793}
6794
6795/*
6796 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006797 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 */
6799 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006800option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 int idx;
6803
6804 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 return FALSE;
6807 if (options[idx].flags & P_WAS_SET)
6808 return TRUE;
6809 return FALSE;
6810}
6811
6812/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006813 * Reset the flag indicating option "name" was set.
6814 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006815 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006816reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006817{
6818 int idx = findoption(name);
6819
6820 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006821 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006822 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006823 return OK;
6824 }
6825 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006826}
6827
6828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 * compatible_set() - Called when 'compatible' has been set or unset.
6830 *
6831 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6832 * flag) to a Vi compatible value.
6833 * When 'compatible' is unset: Set all options that have a different default
6834 * for Vim (without the P_VI_DEF flag) to that default.
6835 */
6836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006837compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 int opt_idx;
6840
Bram Moolenaardac13472019-09-16 21:06:21 +02006841 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6843 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6844 set_option_default(opt_idx, OPT_FREE, p_cp);
6845 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006846 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847}
6848
Bram Moolenaardac13472019-09-16 21:06:21 +02006849#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851/*
6852 * fill_breakat_flags() -- called when 'breakat' changes value.
6853 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006855fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006857 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 int i;
6859
6860 for (i = 0; i < 256; i++)
6861 breakat_flags[i] = FALSE;
6862
6863 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006864 for (p = p_breakat; *p; p++)
6865 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867#endif
6868
6869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * Check if backspacing over something is allowed.
6871 */
6872 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006873can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006874 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006876#ifdef FEAT_JOB_CHANNEL
6877 if (what == BS_START && bt_prompt(curbuf))
6878 return FALSE;
6879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 switch (*p_bs)
6881 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006882 case '3': return TRUE;
6883 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 case '1': return (what != BS_START);
6885 case '0': return FALSE;
6886 }
6887 return vim_strchr(p_bs, what) != NULL;
6888}
6889
6890/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006891 * Return the effective 'scrolloff' value for the current window, using the
6892 * global value when appropriate.
6893 */
6894 long
6895get_scrolloff_value(void)
6896{
6897 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6898}
6899
6900/*
6901 * Return the effective 'sidescrolloff' value for the current window, using the
6902 * global value when appropriate.
6903 */
6904 long
6905get_sidescrolloff_value(void)
6906{
6907 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6908}
6909
6910/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006911 * Get the local or global value of 'backupcopy'.
6912 */
6913 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006914get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006915{
6916 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6917}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006918
Bram Moolenaaree857022019-11-09 23:26:40 +01006919#if defined(FEAT_LINEBREAK) || defined(PROTO)
6920/*
6921 * Get the local or global value of 'showbreak'.
6922 */
6923 char_u *
6924get_showbreak_value(win_T *win)
6925{
6926 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6927 return p_sbr;
6928 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6929 return empty_option;
6930 return win->w_p_sbr;
6931}
6932#endif
6933
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006934#if defined(FEAT_EVAL) || defined(PROTO)
6935/*
6936 * Get window or buffer local options.
6937 */
6938 dict_T *
6939get_winbuf_options(int bufopt)
6940{
6941 dict_T *d;
6942 int opt_idx;
6943
6944 d = dict_alloc();
6945 if (d == NULL)
6946 return NULL;
6947
Bram Moolenaardac13472019-09-16 21:06:21 +02006948 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006949 {
6950 struct vimoption *opt = &options[opt_idx];
6951
6952 if ((bufopt && (opt->indir & PV_BUF))
6953 || (!bufopt && (opt->indir & PV_WIN)))
6954 {
6955 char_u *varp = get_varp(opt);
6956
6957 if (varp != NULL)
6958 {
6959 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006960 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006961 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006962 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006963 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006964 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006965 }
6966 }
6967 }
6968
6969 return d;
6970}
6971#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006972
Bram Moolenaardac13472019-09-16 21:06:21 +02006973#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006974/*
6975 * This is called when 'culopt' is changed
6976 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006977 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006978fill_culopt_flags(char_u *val, win_T *wp)
6979{
6980 char_u *p;
6981 char_u culopt_flags_new = 0;
6982
6983 if (val == NULL)
6984 p = wp->w_p_culopt;
6985 else
6986 p = val;
6987 while (*p != NUL)
6988 {
6989 if (STRNCMP(p, "line", 4) == 0)
6990 {
6991 p += 4;
6992 culopt_flags_new |= CULOPT_LINE;
6993 }
6994 else if (STRNCMP(p, "both", 4) == 0)
6995 {
6996 p += 4;
6997 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
6998 }
6999 else if (STRNCMP(p, "number", 6) == 0)
7000 {
7001 p += 6;
7002 culopt_flags_new |= CULOPT_NBR;
7003 }
7004 else if (STRNCMP(p, "screenline", 10) == 0)
7005 {
7006 p += 10;
7007 culopt_flags_new |= CULOPT_SCRLINE;
7008 }
7009
7010 if (*p != ',' && *p != NUL)
7011 return FAIL;
7012 if (*p == ',')
7013 ++p;
7014 }
7015
7016 // Can't have both "line" and "screenline".
7017 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7018 return FAIL;
7019 wp->w_p_culopt_flags = culopt_flags_new;
7020
7021 return OK;
7022}
7023#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007024
7025/*
7026 * Get the value of 'magic' adjusted for Vim9 script.
7027 */
7028 int
7029magic_isset(void)
7030{
7031 switch (magic_overruled)
7032 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007033 case OPTION_MAGIC_ON: return TRUE;
7034 case OPTION_MAGIC_OFF: return FALSE;
7035 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007036 }
7037#ifdef FEAT_EVAL
7038 if (in_vim9script())
7039 return TRUE;
7040#endif
7041 return p_magic;
7042}