blob: bba467e5e97f7e23f1e05b3f77cd0adb79760a04 [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
2340 (void)set_chars_option(&p_lcs);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002341 // Parse default for 'fillchars'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002342 (void)set_chars_option(&p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002343
2344#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002345 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002346 (void)check_clipboard_option();
2347#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002348#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002349 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002350 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002351 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002352 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354}
2355
2356/*
2357 * Check for string options that are NULL (normally only termcap options).
2358 */
2359 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002360check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 int opt_idx;
2363
2364 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2365 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2366 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2367}
2368
2369/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002370 * Return the option index found by a pointer into term_strings[].
2371 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002373 int
2374get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002376 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2379 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002380 return opt_idx;
2381 return -1; // cannot happen: didn't find it!
2382}
2383
2384/*
2385 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2386 * Return the option index or -1 if not found.
2387 */
2388 int
2389set_term_option_alloced(char_u **p)
2390{
2391 int opt_idx = get_term_opt_idx(p);
2392
2393 if (opt_idx >= 0)
2394 options[opt_idx].flags |= P_ALLOCED;
2395 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396}
2397
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002398#if defined(FEAT_EVAL) || defined(PROTO)
2399/*
2400 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2401 * Return FALSE when it wasn't.
2402 * Return -1 for an unknown option.
2403 */
2404 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002405was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002406{
2407 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002408 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002409
2410 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002411 {
2412 flagp = insecure_flag(idx, opt_flags);
2413 return (*flagp & P_INSECURE) != 0;
2414 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002415 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002416 return -1;
2417}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002418
2419/*
2420 * Get a pointer to the flags used for the P_INSECURE flag of option
2421 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002422 * NOTE: Caller must make sure that "curwin" is set to the window from which
2423 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002424 */
2425 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002426insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002427{
2428 if (opt_flags & OPT_LOCAL)
2429 switch ((int)options[opt_idx].indir)
2430 {
2431#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002432 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002433#endif
2434#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002435# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002436 case PV_FDE: return &curwin->w_p_fde_flags;
2437 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002438# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002439# ifdef FEAT_BEVAL
2440 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2441# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002442# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002443 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002444# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002445 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002446# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002447 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002448# endif
2449#endif
2450 }
2451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002452 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002453 return &options[opt_idx].flags;
2454}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002455#endif
2456
Bram Moolenaardac13472019-09-16 21:06:21 +02002457#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002458/*
2459 * Redraw the window title and/or tab page text later.
2460 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002461void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002462{
2463 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002464 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002465}
2466#endif
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002469 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2470 * characters or characters in "allowed".
2471 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002472 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002473valid_name(char_u *val, char *allowed)
2474{
2475 char_u *s;
2476
2477 for (s = val; *s != NUL; ++s)
2478 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2479 return FALSE;
2480 return TRUE;
2481}
2482
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002483#if defined(FEAT_EVAL) || defined(PROTO)
2484/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002485 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002486 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002487 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002488 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002489set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002490{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002491 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2492 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002493 sctx_T new_script_ctx = script_ctx;
2494
Bram Moolenaar51258742020-05-03 17:19:33 +02002495 // Modeline already has the line number set.
2496 if (!(opt_flags & OPT_MODELINE))
2497 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002498
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002499 // Remember where the option was set. For local options need to do that
2500 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002501 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002502 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002503 if (both || (opt_flags & OPT_LOCAL))
2504 {
2505 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002506 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002507 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002508 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002509 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002510}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002511
2512/*
2513 * Set the script_ctx for a termcap option.
2514 * "name" must be the two character code, e.g. "RV".
2515 * When "name" is NULL use "opt_idx".
2516 */
2517 void
2518set_term_option_sctx_idx(char *name, int opt_idx)
2519{
2520 char_u buf[5];
2521 int idx;
2522
2523 if (name == NULL)
2524 idx = opt_idx;
2525 else
2526 {
2527 buf[0] = 't';
2528 buf[1] = '_';
2529 buf[2] = name[0];
2530 buf[3] = name[1];
2531 buf[4] = 0;
2532 idx = findoption(buf);
2533 }
2534 if (idx >= 0)
2535 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2536}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002537#endif
2538
Bram Moolenaarf4140482020-02-15 23:06:45 +01002539#if defined(FEAT_EVAL)
2540/*
2541 * Apply the OptionSet autocommand.
2542 */
2543 static void
2544apply_optionset_autocmd(
2545 int opt_idx,
2546 long opt_flags,
2547 long oldval,
2548 long oldval_g,
2549 long newval,
2550 char *errmsg)
2551{
2552 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2553
2554 // Don't do this while starting up, failure or recursively.
2555 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2556 return;
2557
2558 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2559 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2560 oldval_g);
2561 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2562 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2563 (opt_flags & OPT_LOCAL) ? "local" : "global");
2564 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2565 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2566 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2567 if (opt_flags & OPT_LOCAL)
2568 {
2569 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2570 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2571 }
2572 if (opt_flags & OPT_GLOBAL)
2573 {
2574 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2575 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2576 }
2577 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2578 {
2579 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2580 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2581 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2582 }
2583 if (opt_flags & OPT_MODELINE)
2584 {
2585 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2586 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2587 }
2588 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2589 NULL, FALSE, NULL);
2590 reset_v_option_vars();
2591}
2592#endif
2593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594/*
2595 * Set the value of a boolean option, and take care of side effects.
2596 * Returns NULL for success, or an error message for an error.
2597 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002598 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002599set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002600 int opt_idx, // index in options[] table
2601 char_u *varp, // pointer to the option variable
2602 int value, // new value
2603 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002606#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002607 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002610 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if ((secure
2612#ifdef HAVE_SANDBOX
2613 || sandbox != 0
2614#endif
2615 ) && (options[opt_idx].flags & P_SECURE))
2616 return e_secure;
2617
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002618#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002619 // Save the global value before changing anything. This is needed as for
2620 // a global-only option setting the "local value" in fact sets the global
2621 // value (since there is only one value).
2622 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2623 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2624 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002625#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002627 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002629 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002630 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631#endif
2632
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002633#ifdef FEAT_GUI
2634 need_mouse_correct = TRUE;
2635#endif
2636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2639 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2640
2641 /*
2642 * Handle side effects of changing a bool option.
2643 */
2644
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002645 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar920694c2016-08-21 17:45:02 +02002649#ifdef FEAT_LANGMAP
2650 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002651 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002652 p_lnr = !p_lrm;
2653 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002654 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002655 p_lrm = !p_lnr;
2656#endif
2657
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002658#ifdef FEAT_SYN_HL
2659 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2660 reset_cursorline();
2661#endif
2662
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002663#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002665 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2666 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002667 // Only take action when the option was set. When reset we do not
2668 // delete the undo file, the option may be set again without making
2669 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002670 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002671 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002672 char_u hash[UNDO_HASH_SIZE];
2673 buf_T *save_curbuf = curbuf;
2674
Bram Moolenaar29323592016-07-24 22:04:11 +02002675 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002676 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 // When 'undofile' is set globally: for every buffer, otherwise
2678 // only for the current buffer: Try to read in the undofile,
2679 // if one exists, the buffer wasn't changed and the buffer was
2680 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002681 if ((curbuf == save_curbuf
2682 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2683 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2684 {
2685 u_compute_hash(hash);
2686 u_read_undo(NULL, hash, curbuf->b_fname);
2687 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002688 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002689 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002690 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002691 }
2692#endif
2693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 else if ((int *)varp == &curbuf->b_p_ro)
2695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2698 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002699
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002700 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002701 if (curbuf->b_p_ro)
2702 curbuf->b_did_warn = FALSE;
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002705 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706#endif
2707 }
2708
Bram Moolenaar9c449722010-07-20 18:44:27 +02002709#ifdef FEAT_GUI
2710 else if ((int *)varp == &p_mh)
2711 {
2712 if (!p_mh)
2713 gui_mch_mousehide(FALSE);
2714 }
2715#endif
2716
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002719 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002720# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002722 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2723 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002724 {
2725 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002726 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002727 }
2728# endif
2729# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002730 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002731# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002732 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002733#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002736 {
2737 redraw_titles();
2738 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002740 else if ((int *)varp == &curbuf->b_p_fixeol)
2741 {
2742 redraw_titles();
2743 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002744 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002745 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002746 {
2747 redraw_titles();
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#endif
2750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else if ((int *)varp == &curbuf->b_p_bin)
2753 {
2754 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2755#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002756 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757#endif
2758 }
2759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2762 {
2763 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2764 NULL, NULL, TRUE, curbuf);
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 else if ((int *)varp == &curbuf->b_p_swf)
2769 {
2770 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2774 // buf->b_p_swf
2775 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else if ((int *)varp == &p_terse)
2780 {
2781 char_u *p;
2782
2783 p = vim_strchr(p_shm, SHM_SEARCH);
2784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002785 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (p_terse && p == NULL)
2787 {
2788 STRCPY(IObuff, p_shm);
2789 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002790 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002792 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002794 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else if ((int *)varp == &p_paste)
2799 {
2800 paste_option_changed();
2801 }
2802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 else if ((int *)varp == &p_im)
2805 {
2806 if (p_im)
2807 {
2808 if ((State & INSERT) == 0)
2809 need_start_insertmode = TRUE;
2810 stop_insert_mode = FALSE;
2811 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002812 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002813 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
2815 need_start_insertmode = FALSE;
2816 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002817 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002818 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 restart_edit = 0;
2820 }
2821 }
2822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 else if ((int *)varp == &p_ic && p_hls)
2825 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002826 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828
2829#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002830 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 else if ((int *)varp == &p_hls)
2832 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002833 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
2835#endif
2836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2838 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 else if ((int *)varp == &curwin->w_p_scb)
2840 {
2841 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002844 curwin->w_scbind_pos = curwin->w_topline;
2845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
Bram Moolenaar4033c552017-09-16 20:54:51 +02002848#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 else if ((int *)varp == &curwin->w_p_pvw)
2851 {
2852 if (curwin->w_p_pvw)
2853 {
2854 win_T *win;
2855
Bram Moolenaar29323592016-07-24 22:04:11 +02002856 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (win->w_p_pvw && win != curwin)
2858 {
2859 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002860 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 }
2863 }
2864#endif
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else if ((int *)varp == &curbuf->b_p_tx)
2868 {
2869 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2870 }
2871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 set_string_option_direct((char_u *)"ffs", -1,
2876 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002877 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 /*
2881 * When 'lisp' option changes include/exclude '-' in
2882 * keyword characters.
2883 */
2884#ifdef FEAT_LISP
2885 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002887 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889#endif
2890
2891#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002893 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002895 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897#endif
2898
2899 else if ((int *)varp == &curbuf->b_changed)
2900 {
2901 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002904 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908
2909#ifdef BACKSLASH_IN_FILENAME
2910 else if ((int *)varp == &p_ssl)
2911 {
2912 if (p_ssl)
2913 {
2914 psepc = '/';
2915 psepcN = '\\';
2916 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 else
2919 {
2920 psepc = '\\';
2921 psepcN = '/';
2922 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 buflist_slash_adjust();
2927 alist_slash_adjust();
2928# ifdef FEAT_EVAL
2929 scriptnames_slash_adjust();
2930# endif
2931 }
2932#endif
2933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 else if ((int *)varp == &curwin->w_p_wrap)
2936 {
2937 if (curwin->w_p_wrap)
2938 curwin->w_leftcol = 0;
2939 }
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 else if ((int *)varp == &p_ea)
2942 {
2943 if (p_ea && !old_value)
2944 win_equal(curwin, FALSE, 0);
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 else if ((int *)varp == &p_wiv)
2948 {
2949 /*
2950 * When 'weirdinvert' changed, set/reset 't_xs'.
2951 * Then set 'weirdinvert' according to value of 't_xs'.
2952 */
2953 if (p_wiv && !old_value)
2954 T_XS = (char_u *)"y";
2955 else if (!p_wiv && old_value)
2956 T_XS = empty_option;
2957 p_wiv = (*T_XS != NUL);
2958 }
2959
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002960#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 else if ((int *)varp == &p_beval)
2962 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002963 if (!balloonEvalForTerm)
2964 {
2965 if (p_beval && !old_value)
2966 gui_mch_enable_beval_area(balloonEval);
2967 else if (!p_beval && old_value)
2968 gui_mch_disable_beval_area(balloonEval);
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002971#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002972#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002973 else if ((int *)varp == &p_bevalterm)
2974 {
2975 mch_bevalterm_changed();
2976 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002979#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 else if ((int *)varp == &p_acd)
2981 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002983 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
2985#endif
2986
2987#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 else if ((int *)varp == &curwin->w_p_diff)
2990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002992 diff_buf_adjust(curwin);
2993# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 if (foldmethodIsDiff(curwin))
2995 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998#endif
2999
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003000#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 else if ((int *)varp == &p_imdisable)
3003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (p_imdisable)
3006 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003007 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 // When the option is set from an autocommand, it may need to take
3009 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003010 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012#endif
3013
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003014#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003016 else if ((int *)varp == &curwin->w_p_spell)
3017 {
3018 if (curwin->w_p_spell)
3019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003020 char *errmsg = did_set_spelllang(curwin);
3021
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003022 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003024 }
3025 }
3026#endif
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#ifdef FEAT_ARABIC
3029 if ((int *)varp == &curwin->w_p_arab)
3030 {
3031 if (curwin->w_p_arab)
3032 {
3033 /*
3034 * 'arabic' is set, handle various sub-settings.
3035 */
3036 if (!p_tbidi)
3037 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003038 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (!curwin->w_p_rl)
3040 {
3041 curwin->w_p_rl = TRUE;
3042 changed_window_setting();
3043 }
3044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (!p_arshape)
3047 {
3048 p_arshape = TRUE;
3049 redraw_later_clear();
3050 }
3051 }
3052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003053 // Arabic requires a utf-8 encoding, inform the user if its not
3054 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003056 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003057 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3058
Bram Moolenaar8820b482017-03-16 17:23:31 +01003059 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003060 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003061#ifdef FEAT_EVAL
3062 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3063#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003066 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3072 OPT_LOCAL);
3073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075 else
3076 {
3077 /*
3078 * 'arabic' is reset, handle various sub-settings.
3079 */
3080 if (!p_tbidi)
3081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 if (curwin->w_p_rl)
3084 {
3085 curwin->w_p_rl = FALSE;
3086 changed_window_setting();
3087 }
3088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089 // 'arabicshape' isn't reset, it is a global option and
3090 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003093 // 'delcombine' isn't reset, it is a global option and another
3094 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 curbuf->b_p_iminsert = B_IMODE_NONE;
3099 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3100# endif
3101 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003102 }
3103
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003104#endif
3105
Bram Moolenaar44826212019-08-22 21:23:20 +02003106#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3107 else if (((int *)varp == &curwin->w_p_nu
3108 || (int *)varp == &curwin->w_p_rnu)
3109 && gui.in_use
3110 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3111 && curbuf->b_signlist != NULL)
3112 {
3113 // If the 'number' or 'relativenumber' options are modified and
3114 // 'signcolumn' is set to 'number', then clear the screen for a full
3115 // refresh. Otherwise the sign icons are not displayed properly in the
3116 // number column. If the 'number' option is set and only the
3117 // 'relativenumber' option is toggled, then don't refresh the screen
3118 // (optimization).
3119 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3120 redraw_all_later(CLEAR);
3121 }
3122#endif
3123
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003124#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003125 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003126 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003127 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003128# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003130 if (
3131# ifdef VIMDLL
3132 !gui.in_use && !gui.starting &&
3133# endif
3134 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003135 {
3136 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003137 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003138 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003139 if (is_term_win32())
3140 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003141# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003142# ifdef FEAT_GUI
3143 if (!gui.in_use && !gui.starting)
3144# endif
3145 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003146# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003147 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003148 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003149 {
3150 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003151 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003152 init_highlight(TRUE, FALSE);
3153 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003154# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003155 }
3156#endif
3157
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 /*
3159 * End of handling side effects for bool options.
3160 */
3161
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003162 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 options[opt_idx].flags |= P_WAS_SET;
3165
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003166#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003167 apply_optionset_autocmd(opt_idx, opt_flags,
3168 (long)(old_value ? TRUE : FALSE),
3169 (long)(old_global_value ? TRUE : FALSE),
3170 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003171#endif
3172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003174 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003175 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003176 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 check_redraw(options[opt_idx].flags);
3178
3179 return NULL;
3180}
3181
3182/*
3183 * Set the value of a number option, and take care of side effects.
3184 * Returns NULL for success, or an error message for an error.
3185 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003186 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003187set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003188 int opt_idx, // index in options[] table
3189 char_u *varp, // pointer to the option variable
3190 long value, // new value
3191 char *errbuf, // buffer for error messages
3192 size_t errbuflen, // length of "errbuf"
3193 int opt_flags) // OPT_LOCAL, OPT_GLOBAL and
3194 // OPT_MODELINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003196 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003198#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003199 long old_global_value = 0; // only used when setting a local and
3200 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003201#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003202 long old_Rows = Rows; // remember old Rows
3203 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 long *pp = (long *)varp;
3205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003206 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003207 if ((secure
3208#ifdef HAVE_SANDBOX
3209 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003211 ) && (options[opt_idx].flags & P_SECURE))
3212 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003214#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003215 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003216 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003217 // value (since there is only one value).
3218 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003219 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3220 OPT_GLOBAL);
3221#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 *pp = value;
3224#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003226 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003228#ifdef FEAT_GUI
3229 need_mouse_correct = TRUE;
3230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
Bram Moolenaar14f24742012-08-08 18:01:05 +02003232 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003235#ifdef FEAT_VARTABS
3236 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3237 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3238 ? tabstop_first(curbuf->b_p_vts_array)
3239 : curbuf->b_p_ts;
3240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244
3245 /*
3246 * Number options that need some action when changed
3247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (pp == &p_wh || pp == &p_hh)
3249 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003250 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (p_wh < 1)
3252 {
3253 errmsg = e_positive;
3254 p_wh = 1;
3255 }
3256 if (p_wmh > p_wh)
3257 {
3258 errmsg = e_winheight;
3259 p_wh = p_wmh;
3260 }
3261 if (p_hh < 0)
3262 {
3263 errmsg = e_positive;
3264 p_hh = 0;
3265 }
3266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003268 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
3270 if (pp == &p_wh && curwin->w_height < p_wh)
3271 win_setheight((int)p_wh);
3272 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3273 win_setheight((int)p_hh);
3274 }
3275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 else if (pp == &p_wmh)
3277 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003278 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (p_wmh < 0)
3280 {
3281 errmsg = e_positive;
3282 p_wmh = 0;
3283 }
3284 if (p_wmh > p_wh)
3285 {
3286 errmsg = e_winheight;
3287 p_wmh = p_wh;
3288 }
3289 win_setminheight();
3290 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003291 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003293 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 if (p_wiw < 1)
3295 {
3296 errmsg = e_positive;
3297 p_wiw = 1;
3298 }
3299 if (p_wmw > p_wiw)
3300 {
3301 errmsg = e_winwidth;
3302 p_wiw = p_wmw;
3303 }
3304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003305 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003306 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 win_setwidth((int)p_wiw);
3308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else if (pp == &p_wmw)
3310 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003311 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (p_wmw < 0)
3313 {
3314 errmsg = e_positive;
3315 p_wmw = 0;
3316 }
3317 if (p_wmw > p_wiw)
3318 {
3319 errmsg = e_winwidth;
3320 p_wmw = p_wiw;
3321 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003322 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else if (pp == &p_ls)
3327 {
3328 last_status(FALSE);
3329 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003330
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003331 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003332 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003333 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003334 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
3337#ifdef FEAT_GUI
3338 else if (pp == &p_linespace)
3339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003340 // Recompute gui.char_height and resize the Vim window to keep the
3341 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003342 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003343 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
3345#endif
3346
3347#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003348 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 else if (pp == &curwin->w_p_fdl)
3350 {
3351 if (curwin->w_p_fdl < 0)
3352 curwin->w_p_fdl = 0;
3353 newFoldLevel();
3354 }
3355
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003356 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else if (pp == &curwin->w_p_fml)
3358 {
3359 foldUpdateAll(curwin);
3360 }
3361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003362 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else if (pp == &curwin->w_p_fdn)
3364 {
3365 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3366 foldUpdateAll(curwin);
3367 }
3368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003369 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else if (pp == &curwin->w_p_fdc)
3371 {
3372 if (curwin->w_p_fdc < 0)
3373 {
3374 errmsg = e_positive;
3375 curwin->w_p_fdc = 0;
3376 }
3377 else if (curwin->w_p_fdc > 12)
3378 {
3379 errmsg = e_invarg;
3380 curwin->w_p_fdc = 12;
3381 }
3382 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003383#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003385#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003386 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3388 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003389# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (foldmethodIsIndent(curwin))
3391 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003392# endif
3393# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003394 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3395 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003396 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3397 parse_cino(curbuf);
3398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003402 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003403 else if (pp == &p_mco)
3404 {
3405 if (p_mco > MAX_MCO)
3406 p_mco = MAX_MCO;
3407 else if (p_mco < 0)
3408 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003409 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003410 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 else if (pp == &curbuf->b_p_iminsert)
3413 {
3414 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3415 {
3416 errmsg = e_invarg;
3417 curbuf->b_p_iminsert = B_IMODE_NONE;
3418 }
3419 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003420 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003422#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 status_redraw_curbuf();
3425#endif
3426 }
3427
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003428#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003429 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003430 else if (pp == &p_imst)
3431 {
3432 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3433 errmsg = e_invarg;
3434 }
3435#endif
3436
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003437 else if (pp == &p_window)
3438 {
3439 if (p_window < 1)
3440 p_window = 1;
3441 else if (p_window >= Rows)
3442 p_window = Rows - 1;
3443 }
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else if (pp == &curbuf->b_p_imsearch)
3446 {
3447 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3448 {
3449 errmsg = e_invarg;
3450 curbuf->b_p_imsearch = B_IMODE_NONE;
3451 }
3452 p_imsearch = curbuf->b_p_imsearch;
3453 }
3454
3455#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 else if (pp == &p_titlelen)
3458 {
3459 if (p_titlelen < 0)
3460 {
3461 errmsg = e_positive;
3462 p_titlelen = 85;
3463 }
3464 if (starting != NO_SCREEN && old_value != p_titlelen)
3465 need_maketitle = TRUE;
3466 }
3467#endif
3468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003469 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 else if (pp == &p_ch)
3471 {
3472 if (p_ch < 1)
3473 {
3474 errmsg = e_positive;
3475 p_ch = 1;
3476 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003477 if (p_ch > Rows - min_rows() + 1)
3478 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003480 // Only compute the new window layout when startup has been
3481 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (p_ch != old_value && full_screen
3483#ifdef FEAT_GUI
3484 && !gui.starting
3485#endif
3486 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003487 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
3489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 else if (pp == &p_uc)
3492 {
3493 if (p_uc < 0)
3494 {
3495 errmsg = e_positive;
3496 p_uc = 100;
3497 }
3498 if (p_uc && !old_value)
3499 ml_open_files();
3500 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003501#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003502 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003503 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003504 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003505 {
3506 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003507 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003508 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003509 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003510 {
3511 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003512 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003513 }
3514 }
3515#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003516#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003517 else if (pp == &p_mzq)
3518 mzvim_reset_timer();
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003521#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003522 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003523 else if (pp == &p_pyx)
3524 {
3525 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3526 errmsg = e_invarg;
3527 }
3528#endif
3529
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003530 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 else if (pp == &p_ul)
3532 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003533 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003535 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 p_ul = value;
3537 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003538 else if (pp == &curbuf->b_p_ul)
3539 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003540 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003541 curbuf->b_p_ul = old_value;
3542 u_sync(TRUE);
3543 curbuf->b_p_ul = value;
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003546#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003547 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003548 else if (pp == &curwin->w_p_nuw)
3549 {
3550 if (curwin->w_p_nuw < 1)
3551 {
3552 errmsg = e_positive;
3553 curwin->w_p_nuw = 1;
3554 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003555 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003556 {
3557 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003558 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003559 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003560 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003561 }
3562#endif
3563
Bram Moolenaar1a384422010-07-14 19:53:30 +02003564 else if (pp == &curbuf->b_p_tw)
3565 {
3566 if (curbuf->b_p_tw < 0)
3567 {
3568 errmsg = e_positive;
3569 curbuf->b_p_tw = 0;
3570 }
3571#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003572 {
3573 win_T *wp;
3574 tabpage_T *tp;
3575
3576 FOR_ALL_TAB_WINDOWS(tp, wp)
3577 check_colorcolumn(wp);
3578 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003579#endif
3580 }
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 /*
3583 * Check the bounds for numeric options here
3584 */
3585 if (Rows < min_rows() && full_screen)
3586 {
3587 if (errbuf != NULL)
3588 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003589 vim_snprintf((char *)errbuf, errbuflen,
3590 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 errmsg = errbuf;
3592 }
3593 Rows = min_rows();
3594 }
3595 if (Columns < MIN_COLUMNS && full_screen)
3596 {
3597 if (errbuf != NULL)
3598 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003599 vim_snprintf((char *)errbuf, errbuflen,
3600 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 errmsg = errbuf;
3602 }
3603 Columns = MIN_COLUMNS;
3604 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003605 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /*
3608 * If the screen (shell) height has been changed, assume it is the
3609 * physical screenheight.
3610 */
3611 if (old_Rows != Rows || old_Columns != Columns)
3612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003613 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (updating_screen)
3615 *pp = old_value;
3616 else if (full_screen
3617#ifdef FEAT_GUI
3618 && !gui.starting
3619#endif
3620 )
3621 set_shellsize((int)Columns, (int)Rows, TRUE);
3622 else
3623 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003624 // Postpone the resizing; check the size and cmdline position for
3625 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 check_shellsize();
3627 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3628 cmdline_row = Rows - p_ch;
3629 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003630 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003631 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (curbuf->b_p_ts <= 0)
3635 {
3636 errmsg = e_positive;
3637 curbuf->b_p_ts = 8;
3638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (p_tm < 0)
3640 {
3641 errmsg = e_positive;
3642 p_tm = 0;
3643 }
3644 if ((curwin->w_p_scr <= 0
3645 || (curwin->w_p_scr > curwin->w_height
3646 && curwin->w_height > 0))
3647 && full_screen)
3648 {
3649 if (pp == &(curwin->w_p_scr))
3650 {
3651 if (curwin->w_p_scr != 0)
3652 errmsg = e_scroll;
3653 win_comp_scroll(curwin);
3654 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003655 // If 'scroll' became invalid because of a side effect silently adjust
3656 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 else if (curwin->w_p_scr <= 0)
3658 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003659 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 curwin->w_p_scr = curwin->w_height;
3661 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003662 if (p_hi < 0)
3663 {
3664 errmsg = e_positive;
3665 p_hi = 0;
3666 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003667 else if (p_hi > 10000)
3668 {
3669 errmsg = e_invarg;
3670 p_hi = 10000;
3671 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003672 if (p_re < 0 || p_re > 2)
3673 {
3674 errmsg = e_invarg;
3675 p_re = 0;
3676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (p_report < 0)
3678 {
3679 errmsg = e_positive;
3680 p_report = 1;
3681 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003682 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003684 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 p_sj = Rows / 2;
3686 else
3687 {
3688 errmsg = e_scroll;
3689 p_sj = 1;
3690 }
3691 }
3692 if (p_so < 0 && full_screen)
3693 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003694 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 p_so = 0;
3696 }
3697 if (p_siso < 0 && full_screen)
3698 {
3699 errmsg = e_positive;
3700 p_siso = 0;
3701 }
3702#ifdef FEAT_CMDWIN
3703 if (p_cwh < 1)
3704 {
3705 errmsg = e_positive;
3706 p_cwh = 1;
3707 }
3708#endif
3709 if (p_ut < 0)
3710 {
3711 errmsg = e_positive;
3712 p_ut = 2000;
3713 }
3714 if (p_ss < 0)
3715 {
3716 errmsg = e_positive;
3717 p_ss = 0;
3718 }
3719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003720 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3722 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3723
3724 options[opt_idx].flags |= P_WAS_SET;
3725
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003726#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003727 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3728 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003729#endif
3730
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003731 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003732 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003733 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003734 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 check_redraw(options[opt_idx].flags);
3736
3737 return errmsg;
3738}
3739
3740/*
3741 * Called after an option changed: check if something needs to be redrawn.
3742 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003743 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003744check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003747 int doclear = (flags & P_RCLR) == P_RCLR;
3748 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003750 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
3753 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3754 changed_window_setting();
3755 if (flags & P_RBUF)
3756 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003757 if (flags & P_RWINONLY)
3758 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003759 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 redraw_all_later(CLEAR);
3761 else if (all)
3762 redraw_all_later(NOT_VALID);
3763}
3764
3765/*
3766 * Find index for option 'arg'.
3767 * Return -1 if not found.
3768 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003769 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003770findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
3772 int opt_idx;
3773 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003774 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 int is_term_opt;
3776
3777 /*
3778 * For first call: Initialize the quick-access table.
3779 * It contains the index for the first option that starts with a certain
3780 * letter. There are 26 letters, plus the first "t_" option.
3781 */
3782 if (quick_tab[1] == 0)
3783 {
3784 p = options[0].fullname;
3785 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3786 {
3787 if (s[0] != p[0])
3788 {
3789 if (s[0] == 't' && s[1] == '_')
3790 quick_tab[26] = opt_idx;
3791 else
3792 quick_tab[CharOrdLow(s[0])] = opt_idx;
3793 }
3794 p = s;
3795 }
3796 }
3797
3798 /*
3799 * Check for name starting with an illegal character.
3800 */
3801#ifdef EBCDIC
3802 if (!islower(arg[0]))
3803#else
3804 if (arg[0] < 'a' || arg[0] > 'z')
3805#endif
3806 return -1;
3807
3808 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3809 if (is_term_opt)
3810 opt_idx = quick_tab[26];
3811 else
3812 opt_idx = quick_tab[CharOrdLow(arg[0])];
3813 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003815 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 break;
3817 }
3818 if (s == NULL && !is_term_opt)
3819 {
3820 opt_idx = quick_tab[CharOrdLow(arg[0])];
3821 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3822 {
3823 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003824 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 break;
3826 s = NULL;
3827 }
3828 }
3829 if (s == NULL)
3830 opt_idx = -1;
3831 return opt_idx;
3832}
3833
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003834#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835/*
3836 * Get the value for an option.
3837 *
3838 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003839 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003840 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003841 * String option: gov_string, *stringval gets allocated string.
3842 * Hidden Number option: gov_hidden_number.
3843 * Hidden Toggle option: gov_hidden_bool.
3844 * Hidden String option: gov_hidden_string.
3845 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003847 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003848get_option_value(
3849 char_u *name,
3850 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003851 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003852 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853{
3854 int opt_idx;
3855 char_u *varp;
3856
3857 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003858 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003859 {
3860 int key;
3861
3862 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003863 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003864 {
3865 char_u key_name[2];
3866 char_u *p;
3867
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003868 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003869 if (key < 0)
3870 {
3871 key_name[0] = KEY2TERMCAP0(key);
3872 key_name[1] = KEY2TERMCAP1(key);
3873 }
3874 else
3875 {
3876 key_name[0] = KS_KEY;
3877 key_name[1] = (key & 0xff);
3878 }
3879 p = find_termcode(key_name);
3880 if (p != NULL)
3881 {
3882 if (stringval != NULL)
3883 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003884 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003885 }
3886 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003887 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3891
3892 if (options[opt_idx].flags & P_STRING)
3893 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003894 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003895 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (stringval != NULL)
3897 {
3898#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003899 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003900 if ((char_u **)varp == &curbuf->b_p_key
3901 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 *stringval = vim_strsave((char_u *)"*****");
3903 else
3904#endif
3905 *stringval = vim_strsave(*(char_u **)(varp));
3906 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003907 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003910 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003911 return (options[opt_idx].flags & P_NUM)
3912 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (options[opt_idx].flags & P_NUM)
3914 *numval = *(long *)varp;
3915 else
3916 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003917 // Special case: 'modified' is b_changed, but we also want to consider
3918 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if ((int *)varp == &curbuf->b_changed)
3920 *numval = curbufIsChanged();
3921 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003922 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003924 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925}
3926#endif
3927
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003928#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003929/*
3930 * Returns the option attributes and its value. Unlike the above function it
3931 * will return either global value or local value of the option depending on
3932 * what was requested, but it will never return global value if it was
3933 * requested to return local one and vice versa. Neither it will return
3934 * buffer-local value if it was requested to return window-local one.
3935 *
3936 * Pretends that option is absent if it is not present in the requested scope
3937 * (i.e. has no global, window-local or buffer-local value depending on
3938 * opt_type). Uses
3939 *
3940 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003941 * 0 hidden or unknown option, also option that does not have requested
3942 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003943 * see SOPT_* in vim.h for other flags
3944 *
3945 * Possible opt_type values: see SREQ_* in vim.h
3946 */
3947 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003948get_option_value_strict(
3949 char_u *name,
3950 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003951 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003952 int opt_type,
3953 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003954{
3955 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003956 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003957 struct vimoption *p;
3958 int r = 0;
3959
3960 opt_idx = findoption(name);
3961 if (opt_idx < 0)
3962 return 0;
3963
3964 p = &(options[opt_idx]);
3965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003967 if (p->var == NULL)
3968 return 0;
3969
3970 if (p->flags & P_BOOL)
3971 r |= SOPT_BOOL;
3972 else if (p->flags & P_NUM)
3973 r |= SOPT_NUM;
3974 else if (p->flags & P_STRING)
3975 r |= SOPT_STRING;
3976
3977 if (p->indir == PV_NONE)
3978 {
3979 if (opt_type == SREQ_GLOBAL)
3980 r |= SOPT_GLOBAL;
3981 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003982 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003983 }
3984 else
3985 {
3986 if (p->indir & PV_BOTH)
3987 r |= SOPT_GLOBAL;
3988 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003989 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003990
3991 if (p->indir & PV_WIN)
3992 {
3993 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003994 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003995 else
3996 r |= SOPT_WIN;
3997 }
3998 else if (p->indir & PV_BUF)
3999 {
4000 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004001 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004002 else
4003 r |= SOPT_BUF;
4004 }
4005 }
4006
4007 if (stringval == NULL)
4008 return r;
4009
4010 if (opt_type == SREQ_GLOBAL)
4011 varp = p->var;
4012 else
4013 {
4014 if (opt_type == SREQ_BUF)
4015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004016 // Special case: 'modified' is b_changed, but we also want to
4017 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004018 if (p->indir == PV_MOD)
4019 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004020 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004021 varp = NULL;
4022 }
4023#ifdef FEAT_CRYPT
4024 else if (p->indir == PV_KEY)
4025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004026 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004027 *stringval = NULL;
4028 varp = NULL;
4029 }
4030#endif
4031 else
4032 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004033 buf_T *save_curbuf = curbuf;
4034
4035 // only getting a pointer, no need to use aucmd_prepbuf()
4036 curbuf = (buf_T *)from;
4037 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004038 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004039 curbuf = save_curbuf;
4040 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004041 }
4042 }
4043 else if (opt_type == SREQ_WIN)
4044 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004045 win_T *save_curwin = curwin;
4046
4047 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004048 curbuf = curwin->w_buffer;
4049 varp = get_varp(p);
4050 curwin = save_curwin;
4051 curbuf = curwin->w_buffer;
4052 }
4053 if (varp == p->var)
4054 return (r | SOPT_UNSET);
4055 }
4056
4057 if (varp != NULL)
4058 {
4059 if (p->flags & P_STRING)
4060 *stringval = vim_strsave(*(char_u **)(varp));
4061 else if (p->flags & P_NUM)
4062 *numval = *(long *) varp;
4063 else
4064 *numval = *(int *)varp;
4065 }
4066
4067 return r;
4068}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004069
4070/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004071 * Iterate over options. First argument is a pointer to a pointer to a
4072 * structure inside options[] array, second is option type like in the above
4073 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004074 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004075 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004076 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004077 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004078 * first argument to NULL.
4079 *
4080 * Returns full option name for current option on each call.
4081 */
4082 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004083option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004084{
4085 struct vimoption *ret = NULL;
4086 do
4087 {
4088 if (*option == NULL)
4089 *option = (void *) options;
4090 else if (((struct vimoption *) (*option))->fullname == NULL)
4091 {
4092 *option = NULL;
4093 return NULL;
4094 }
4095 else
4096 *option = (void *) (((struct vimoption *) (*option)) + 1);
4097
4098 ret = ((struct vimoption *) (*option));
4099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004100 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004101 if (ret->var == NULL)
4102 {
4103 ret = NULL;
4104 continue;
4105 }
4106
4107 switch (opt_type)
4108 {
4109 case SREQ_GLOBAL:
4110 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4111 ret = NULL;
4112 break;
4113 case SREQ_BUF:
4114 if (!(ret->indir & PV_BUF))
4115 ret = NULL;
4116 break;
4117 case SREQ_WIN:
4118 if (!(ret->indir & PV_WIN))
4119 ret = NULL;
4120 break;
4121 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004122 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004123 return NULL;
4124 }
4125 }
4126 while (ret == NULL);
4127
4128 return (char_u *)ret->fullname;
4129}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004130#endif
4131
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004133 * Return the flags for the option at 'opt_idx'.
4134 */
4135 long_u
4136get_option_flags(int opt_idx)
4137{
4138 return options[opt_idx].flags;
4139}
4140
4141/*
4142 * Set a flag for the option at 'opt_idx'.
4143 */
4144 void
4145set_option_flag(int opt_idx, long_u flag)
4146{
4147 options[opt_idx].flags |= flag;
4148}
4149
4150/*
4151 * Clear a flag for the option at 'opt_idx'.
4152 */
4153 void
4154clear_option_flag(int opt_idx, long_u flag)
4155{
4156 options[opt_idx].flags &= ~flag;
4157}
4158
4159/*
4160 * Returns TRUE if the option at 'opt_idx' is a global option
4161 */
4162 int
4163is_global_option(int opt_idx)
4164{
4165 return options[opt_idx].indir == PV_NONE;
4166}
4167
4168/*
4169 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4170 * local value.
4171 */
4172 int
4173is_global_local_option(int opt_idx)
4174{
4175 return options[opt_idx].indir & PV_BOTH;
4176}
4177
4178/*
4179 * Returns TRUE if the option at 'opt_idx' is a window-local option
4180 */
4181 int
4182is_window_local_option(int opt_idx)
4183{
4184 return options[opt_idx].var == VAR_WIN;
4185}
4186
4187/*
4188 * Returns TRUE if the option at 'opt_idx' is a hidden option
4189 */
4190 int
4191is_hidden_option(int opt_idx)
4192{
4193 return options[opt_idx].var == NULL;
4194}
4195
4196#if defined(FEAT_CRYPT) || defined(PROTO)
4197/*
4198 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4199 */
4200 int
4201is_crypt_key_option(int opt_idx)
4202{
4203 return options[opt_idx].indir == PV_KEY;
4204}
4205#endif
4206
4207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 * Set the value of option "name".
4209 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004210 *
4211 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004213 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004214set_option_value(
4215 char_u *name,
4216 long number,
4217 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004218 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 int opt_idx;
4221 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004225 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004226 {
4227 int key;
4228
4229 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004230 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004231 {
4232 char_u key_name[2];
4233
4234 if (key < 0)
4235 {
4236 key_name[0] = KEY2TERMCAP0(key);
4237 key_name[1] = KEY2TERMCAP1(key);
4238 }
4239 else
4240 {
4241 key_name[0] = KS_KEY;
4242 key_name[1] = (key & 0xff);
4243 }
4244 add_termcode(key_name, string, FALSE);
4245 if (full_screen)
4246 ttest(FALSE);
4247 redraw_all_later(CLEAR);
4248 return NULL;
4249 }
4250
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 else
4254 {
4255 flags = options[opt_idx].flags;
4256#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004257 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004260 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004261 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004264 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004265 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 else
4267 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004268 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004269 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004271 if (number == 0 && string != NULL)
4272 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004273 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004274
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004275 // Either we are given a string or we are setting option
4276 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004277 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004278 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004279 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004280 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004281 // There's another character after zeros or the string
4282 // is empty. In both cases, we are trying to set a
4283 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004284 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004285 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004286 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004287
4288 }
4289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004291 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004292 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004294 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004295 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004299 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300}
4301
4302/*
4303 * Get the terminal code for a terminal option.
4304 * Returns NULL when not found.
4305 */
4306 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004307get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
4309 int opt_idx;
4310 char_u *varp;
4311
4312 if (tname[0] != 't' || tname[1] != '_' ||
4313 tname[2] == NUL || tname[3] == NUL)
4314 return NULL;
4315 if ((opt_idx = findoption(tname)) >= 0)
4316 {
4317 varp = get_varp(&(options[opt_idx]));
4318 if (varp != NULL)
4319 varp = *(char_u **)(varp);
4320 return varp;
4321 }
4322 return find_termcode(tname + 2);
4323}
4324
4325 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004326get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 int i;
4329
4330 i = findoption((char_u *)"hl");
4331 if (i >= 0)
4332 return options[i].def_val[VI_DEFAULT];
4333 return (char_u *)NULL;
4334}
4335
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004336 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004337get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004338{
4339 int i;
4340
4341 i = findoption((char_u *)"enc");
4342 if (i >= 0)
4343 return options[i].def_val[VI_DEFAULT];
4344 return (char_u *)NULL;
4345}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004346
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347/*
4348 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004349 * When "has_lt" is true there is a '<' before "*arg_arg".
4350 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 */
4352 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004353find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004355 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004357 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
4359 /*
4360 * Don't use get_special_key_code() for t_xx, we don't want it to call
4361 * add_termcap_entry().
4362 */
4363 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4364 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004365 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004367 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004369 key = find_special_key(&arg, &modifiers,
4370 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004371 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 key = 0;
4373 }
4374 return key;
4375}
4376
4377/*
4378 * if 'all' == 0: show changed options
4379 * if 'all' == 1: show all normal options
4380 * if 'all' == 2: show all terminal options
4381 */
4382 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004383showoptions(
4384 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004385 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
4387 struct vimoption *p;
4388 int col;
4389 int isterm;
4390 char_u *varp;
4391 struct vimoption **items;
4392 int item_count;
4393 int run;
4394 int row, rows;
4395 int cols;
4396 int i;
4397 int len;
4398
4399#define INC 20
4400#define GAP 3
4401
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004402 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (items == NULL)
4404 return;
4405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004406 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004408 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004410 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004412 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004414 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004417 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 * 1. display the short items
4419 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004420 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
4422 for (run = 1; run <= 2 && !got_int; ++run)
4423 {
4424 /*
4425 * collect the items in items[]
4426 */
4427 item_count = 0;
4428 for (p = &options[0]; p->fullname != NULL; p++)
4429 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004430 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004431 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004432 continue;
4433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 varp = NULL;
4435 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004436 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 if (p->indir != PV_NONE && !isterm)
4439 varp = get_varp_scope(p, opt_flags);
4440 }
4441 else
4442 varp = get_varp(p);
4443 if (varp != NULL
4444 && ((all == 2 && isterm)
4445 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004446 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004448 if (opt_flags & OPT_ONECOLUMN)
4449 len = Columns;
4450 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004451 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 else
4453 {
4454 option_value2string(p, opt_flags);
4455 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4456 }
4457 if ((len <= INC - GAP && run == 1) ||
4458 (len > INC - GAP && run == 2))
4459 items[item_count++] = p;
4460 }
4461 }
4462
4463 /*
4464 * display the items
4465 */
4466 if (run == 1)
4467 {
4468 cols = (Columns + GAP - 3) / INC;
4469 if (cols == 0)
4470 cols = 1;
4471 rows = (item_count + cols - 1) / cols;
4472 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004473 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 rows = item_count;
4475 for (row = 0; row < rows && !got_int; ++row)
4476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004477 msg_putchar('\n'); // go to next line
4478 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 break;
4480 col = 0;
4481 for (i = row; i < item_count; i += rows)
4482 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004483 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 showoneopt(items[i], opt_flags);
4485 col += INC;
4486 }
4487 out_flush();
4488 ui_breakcheck();
4489 }
4490 }
4491 vim_free(items);
4492}
4493
4494/*
4495 * Return TRUE if option "p" has its default value.
4496 */
4497 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004498optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
4500 int dvi;
4501
4502 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004503 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004504 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004506 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004508 // the cast to long is required for Manx C, long_i is
4509 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004510 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004511 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4513}
4514
4515/*
4516 * showoneopt: show the value of one option
4517 * must not be called with a hidden option!
4518 */
4519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004520showoneopt(
4521 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004522 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004524 char_u *varp;
4525 int save_silent = silent_mode;
4526
4527 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004528 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
4530 varp = get_varp_scope(p, opt_flags);
4531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004532 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4534 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004535 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004537 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004539 msg_puts(" ");
4540 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (!(p->flags & P_BOOL))
4542 {
4543 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004544 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 option_value2string(p, opt_flags);
4546 msg_outtrans(NameBuff);
4547 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004548
4549 silent_mode = save_silent;
4550 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551}
4552
4553/*
4554 * Write modified options as ":set" commands to a file.
4555 *
4556 * There are three values for "opt_flags":
4557 * OPT_GLOBAL: Write global option values and fresh values of
4558 * buffer-local options (used for start of a session
4559 * file).
4560 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4561 * curwin (used for a vimrc file).
4562 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4563 * and local values for window-local options of
4564 * curwin. Local values are also written when at the
4565 * default value, because a modeline or autocommand
4566 * may have set them when doing ":edit file" and the
4567 * user has set them back at the default or fresh
4568 * value.
4569 * When "local_only" is TRUE, don't write fresh
4570 * values, only local values (for ":mkview").
4571 * (fresh value = value used for a new buffer or window for a local option).
4572 *
4573 * Return FAIL on error, OK otherwise.
4574 */
4575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004576makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577{
4578 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004579 char_u *varp; // currently used value
4580 char_u *varp_fresh; // local value
4581 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 char *cmd;
4583 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004584 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 /*
4587 * The options that don't have a default (terminal name, columns, lines)
4588 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004589 * Do the loop over "options[]" twice: once for options with the
4590 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004592 for (pri = 1; pri >= 0; --pri)
4593 {
4594 for (p = &options[0]; !istermoption(p); p++)
4595 if (!(p->flags & P_NO_MKRC)
4596 && !istermoption(p)
4597 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004599 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4601 continue;
4602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004603 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4604 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4606 continue;
4607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004608 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004610 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 continue;
4612
4613 round = 2;
4614 if (p->indir != PV_NONE)
4615 {
4616 if (p->var == VAR_WIN)
4617 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (!(opt_flags & OPT_LOCAL))
4620 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004621 // When fresh value of window-local option is not at the
4622 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4624 {
4625 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004626 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
4628 round = 1;
4629 varp_local = varp;
4630 varp = varp_fresh;
4631 }
4632 }
4633 }
4634 }
4635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004636 // Round 1: fresh value for window-local options.
4637 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 for ( ; round <= 2; varp = varp_local, ++round)
4639 {
4640 if (round == 1 || (opt_flags & OPT_GLOBAL))
4641 cmd = "set";
4642 else
4643 cmd = "setlocal";
4644
4645 if (p->flags & P_BOOL)
4646 {
4647 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4648 return FAIL;
4649 }
4650 else if (p->flags & P_NUM)
4651 {
4652 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4653 return FAIL;
4654 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004655 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004657 int do_endif = FALSE;
4658
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004659 // Don't set 'syntax' and 'filetype' again if the value is
4660 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004661 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004662#if defined(FEAT_SYN_HL)
4663 p->indir == PV_SYN ||
4664#endif
4665 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
4667 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4668 *(char_u **)(varp)) < 0
4669 || put_eol(fd) < 0)
4670 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004671 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004674 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004676 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
4678 if (put_line(fd, "endif") == FAIL)
4679 return FAIL;
4680 }
4681 }
4682 }
4683 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 return OK;
4686}
4687
4688#if defined(FEAT_FOLDING) || defined(PROTO)
4689/*
4690 * Generate set commands for the local fold options only. Used when
4691 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4692 */
4693 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004694makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004696 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004698 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 == FAIL
4700# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004701 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004703 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 == FAIL
4705 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4706 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4707 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4708 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4709 )
4710 return FAIL;
4711
4712 return OK;
4713}
4714#endif
4715
4716 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004717put_setstring(
4718 FILE *fd,
4719 char *cmd,
4720 char *name,
4721 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004722 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004725 char_u *buf = NULL;
4726 char_u *part = NULL;
4727 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4730 return FAIL;
4731 if (*valuep != NULL)
4732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004733 // Output 'pastetoggle' as key names. For other
4734 // options some characters have to be escaped with
4735 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 if (valuep == &p_pt)
4737 {
4738 s = *valuep;
4739 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004740 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 return FAIL;
4742 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004743 // expand the option value, replace $HOME by ~
4744 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004746 int size = (int)STRLEN(*valuep) + 1;
4747
4748 // replace home directory in the whole option value into "buf"
4749 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004750 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004751 goto fail;
4752 home_replace(NULL, *valuep, buf, size, FALSE);
4753
4754 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004755 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004756 // can be expanded when read back.
4757 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4758 && vim_strchr(*valuep, ',') != NULL)
4759 {
4760 part = alloc(size);
4761 if (part == NULL)
4762 goto fail;
4763
4764 // write line break to clear the option, e.g. ':set rtp='
4765 if (put_eol(fd) == FAIL)
4766 goto fail;
4767
4768 p = buf;
4769 while (*p != NUL)
4770 {
4771 // for each comma separated option part, append value to
4772 // the option, :set rtp+=value
4773 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4774 goto fail;
4775 (void)copy_option_part(&p, part, size, ",");
4776 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4777 goto fail;
4778 }
4779 vim_free(buf);
4780 vim_free(part);
4781 return OK;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004784 {
4785 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004787 }
4788 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 else if (put_escstr(fd, *valuep, 2) == FAIL)
4791 return FAIL;
4792 }
4793 if (put_eol(fd) < 0)
4794 return FAIL;
4795 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004796fail:
4797 vim_free(buf);
4798 vim_free(part);
4799 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800}
4801
4802 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004803put_setnum(
4804 FILE *fd,
4805 char *cmd,
4806 char *name,
4807 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 long wc;
4810
4811 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4812 return FAIL;
4813 if (wc_use_keyname((char_u *)valuep, &wc))
4814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004815 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4817 return FAIL;
4818 }
4819 else if (fprintf(fd, "%ld", *valuep) < 0)
4820 return FAIL;
4821 if (put_eol(fd) < 0)
4822 return FAIL;
4823 return OK;
4824}
4825
4826 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004827put_setbool(
4828 FILE *fd,
4829 char *cmd,
4830 char *name,
4831 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004833 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004834 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4836 || put_eol(fd) < 0)
4837 return FAIL;
4838 return OK;
4839}
4840
4841/*
4842 * Clear all the terminal options.
4843 * If the option has been allocated, free the memory.
4844 * Terminal options are never hidden or indirect.
4845 */
4846 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004847clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 /*
4850 * Reset a few things before clearing the old options. This may cause
4851 * outputting a few things that the terminal doesn't understand, but the
4852 * screen will be cleared later, so this is OK.
4853 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004854 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004856 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#endif
4858#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004859 // When starting the GUI close the display opened for the clipboard.
4860 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (gui.starting)
4862 clear_xterm_clip();
4863#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004864 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004866 free_termoptions();
4867}
4868
4869 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004870free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004871{
4872 struct vimoption *p;
4873
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004874 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 if (istermoption(p))
4876 {
4877 if (p->flags & P_ALLOCED)
4878 free_string_option(*(char_u **)(p->var));
4879 if (p->flags & P_DEF_ALLOCED)
4880 free_string_option(p->def_val[VI_DEFAULT]);
4881 *(char_u **)(p->var) = empty_option;
4882 p->def_val[VI_DEFAULT] = empty_option;
4883 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004884#ifdef FEAT_EVAL
4885 // remember where the option was cleared
4886 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 clear_termcodes();
4890}
4891
4892/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004893 * Free the string for one term option, if it was allocated.
4894 * Set the string to empty_option and clear allocated flag.
4895 * "var" points to the option value.
4896 */
4897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004898free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004899{
4900 struct vimoption *p;
4901
4902 for (p = &options[0]; p->fullname != NULL; p++)
4903 if (p->var == var)
4904 {
4905 if (p->flags & P_ALLOCED)
4906 free_string_option(*(char_u **)(p->var));
4907 *(char_u **)(p->var) = empty_option;
4908 p->flags &= ~P_ALLOCED;
4909 break;
4910 }
4911}
4912
4913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 * Set the terminal option defaults to the current value.
4915 * Used after setting the terminal name.
4916 */
4917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004918set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 struct vimoption *p;
4921
4922 for (p = &options[0]; p->fullname != NULL; p++)
4923 {
4924 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4925 {
4926 if (p->flags & P_DEF_ALLOCED)
4927 {
4928 free_string_option(p->def_val[VI_DEFAULT]);
4929 p->flags &= ~P_DEF_ALLOCED;
4930 }
4931 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4932 if (p->flags & P_ALLOCED)
4933 {
4934 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004935 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 }
4938 }
4939}
4940
4941/*
4942 * return TRUE if 'p' starts with 't_'
4943 */
4944 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004945istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4948}
4949
Bram Moolenaardac13472019-09-16 21:06:21 +02004950/*
4951 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4952 */
4953 int
4954istermoption_idx(int opt_idx)
4955{
4956 return istermoption(&options[opt_idx]);
4957}
4958
Bram Moolenaar113e1072019-01-20 15:30:40 +01004959#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004961 * Unset local option value, similar to ":set opt<".
4962 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004963 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004964unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004965{
4966 struct vimoption *p;
4967 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004968 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004969
4970 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004971 if (opt_idx < 0)
4972 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004973 p = &(options[opt_idx]);
4974
4975 switch ((int)p->indir)
4976 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004977 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004978 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004979 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004980 break;
4981 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004982 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004983 break;
4984 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004985 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004986 break;
4987 case PV_AR:
4988 buf->b_p_ar = -1;
4989 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004990 case PV_BKC:
4991 clear_string_option(&buf->b_p_bkc);
4992 buf->b_bkc_flags = 0;
4993 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004994 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004995 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004996 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01004997 case PV_TC:
4998 clear_string_option(&buf->b_p_tc);
4999 buf->b_tc_flags = 0;
5000 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005001 case PV_SISO:
5002 curwin->w_p_siso = -1;
5003 break;
5004 case PV_SO:
5005 curwin->w_p_so = -1;
5006 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005007#ifdef FEAT_FIND_ID
5008 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005009 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005010 break;
5011 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005012 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005013 break;
5014#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005015 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005016 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005017 break;
5018 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005019 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005020 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005021 case PV_FP:
5022 clear_string_option(&buf->b_p_fp);
5023 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005024#ifdef FEAT_QUICKFIX
5025 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005026 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005027 break;
5028 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005029 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005030 break;
5031 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005032 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005033 break;
5034#endif
5035#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5036 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005037 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005038 break;
5039#endif
5040#if defined(FEAT_CRYPT)
5041 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005042 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005043 break;
5044#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005045#ifdef FEAT_LINEBREAK
5046 case PV_SBR:
5047 clear_string_option(&((win_T *)from)->w_p_sbr);
5048 break;
5049#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005050#ifdef FEAT_STL_OPT
5051 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005052 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005053 break;
5054#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005055 case PV_UL:
5056 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5057 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005058#ifdef FEAT_LISP
5059 case PV_LW:
5060 clear_string_option(&buf->b_p_lw);
5061 break;
5062#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005063 case PV_MENC:
5064 clear_string_option(&buf->b_p_menc);
5065 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005066 }
5067}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005068#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005069
5070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 * Get pointer to option variable, depending on local or global scope.
5072 */
5073 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005074get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
5076 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5077 {
5078 if (p->var == VAR_WIN)
5079 return (char_u *)GLOBAL_WO(get_varp(p));
5080 return p->var;
5081 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005082 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 switch ((int)p->indir)
5085 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005086 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005088 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5089 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5090 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005092 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5093 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5094 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005095 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005096 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005097 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005098 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5099 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005101 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5102 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005104 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5105 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005106#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5107 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5108#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005109#if defined(FEAT_CRYPT)
5110 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5111#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005112#ifdef FEAT_LINEBREAK
5113 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5114#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005115#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005116 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005117#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005118 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005119#ifdef FEAT_LISP
5120 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5121#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005122 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005123 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005125 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 return get_varp(p);
5128}
5129
5130/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005131 * Get pointer to option variable at 'opt_idx', depending on local or global
5132 * scope.
5133 */
5134 char_u *
5135get_option_varp_scope(int opt_idx, int opt_flags)
5136{
5137 return get_varp_scope(&(options[opt_idx]), opt_flags);
5138}
5139
5140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 * Get pointer to option variable.
5142 */
5143 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005144get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005146 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (p->var == NULL)
5148 return NULL;
5149
5150 switch ((int)p->indir)
5151 {
5152 case PV_NONE: return p->var;
5153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005154 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005155 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005157 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005159 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005161 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005163 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005165 case PV_TC: return *curbuf->b_p_tc != NUL
5166 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005167 case PV_BKC: return *curbuf->b_p_bkc != NUL
5168 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005169 case PV_SISO: return curwin->w_p_siso >= 0
5170 ? (char_u *)&(curwin->w_p_siso) : p->var;
5171 case PV_SO: return curwin->w_p_so >= 0
5172 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005174 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005176 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5178#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005179 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005181 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005183 case PV_FP: return *curbuf->b_p_fp != NUL
5184 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005186 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005188 case PV_GP: return *curbuf->b_p_gp != NUL
5189 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5190 case PV_MP: return *curbuf->b_p_mp != NUL
5191 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005193#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5194 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5195 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5196#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005197#if defined(FEAT_CRYPT)
5198 case PV_CM: return *curbuf->b_p_cm != NUL
5199 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5200#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005201#ifdef FEAT_LINEBREAK
5202 case PV_SBR: return *curwin->w_p_sbr != NUL
5203 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5204#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005205#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005206 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005207 ? (char_u *)&(curwin->w_p_stl) : p->var;
5208#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005209 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5210 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005211#ifdef FEAT_LISP
5212 case PV_LW: return *curbuf->b_p_lw != NUL
5213 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5214#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005215 case PV_MENC: return *curbuf->b_p_menc != NUL
5216 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#ifdef FEAT_ARABIC
5218 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5219#endif
5220 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005221#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005222 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5223#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005224#ifdef FEAT_SYN_HL
5225 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5226 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005227 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005228 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#ifdef FEAT_DIFF
5231 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5232#endif
5233#ifdef FEAT_FOLDING
5234 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5235 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5236 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5237 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5238 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5239 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5240 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5241# ifdef FEAT_EVAL
5242 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5243 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5244# endif
5245 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5246#endif
5247 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005248 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005249#ifdef FEAT_LINEBREAK
5250 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005253 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005254#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5256#endif
5257#ifdef FEAT_RIGHTLEFT
5258 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5259 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5260#endif
5261 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5262 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5263#ifdef FEAT_LINEBREAK
5264 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005265 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5266 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005268 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005270 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005271#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005272 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5273 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5274#endif
5275#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005276 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5277 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5278 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
5281 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5282 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5285 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5287 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5288#ifdef FEAT_CINDENT
5289 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5290 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5291 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5292#endif
5293#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5294 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297#ifdef FEAT_FOLDING
5298 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005301#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005302 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005304#ifdef FEAT_COMPL_FUNC
5305 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005306 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005307#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005308#ifdef FEAT_EVAL
5309 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005312 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005318 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5320 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5321 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5322 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5323#ifdef FEAT_FIND_ID
5324# ifdef FEAT_EVAL
5325 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5326# endif
5327#endif
5328#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5329 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5330 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5331#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005332#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005333 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335#ifdef FEAT_CRYPT
5336 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5337#endif
5338#ifdef FEAT_LISP
5339 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5340#endif
5341 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5342 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5343 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5344 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5345 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005347#ifdef FEAT_TEXTOBJ
5348 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5351#ifdef FEAT_SMARTINDENT
5352 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5356#ifdef FEAT_SEARCHPATH
5357 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5358#endif
5359 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5360#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005361 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005362 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5363#endif
5364#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005365 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5366 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5367 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005368 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369#endif
5370 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5371 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5372 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5373 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005374#ifdef FEAT_PERSISTENT_UNDO
5375 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5378#ifdef FEAT_KEYMAP
5379 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5380#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005381#ifdef FEAT_SIGNS
5382 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5383#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005384#ifdef FEAT_VARTABS
5385 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5386 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5387#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005388 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005390 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 return (char_u *)&(curbuf->b_p_wm);
5392}
5393
5394/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005395 * Return a pointer to the variable for option at 'opt_idx'
5396 */
5397 char_u *
5398get_option_var(int opt_idx)
5399{
5400 return options[opt_idx].var;
5401}
5402
5403/*
5404 * Return the full name of the option at 'opt_idx'
5405 */
5406 char_u *
5407get_option_fullname(int opt_idx)
5408{
5409 return (char_u *)options[opt_idx].fullname;
5410}
5411
5412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 * Get the value of 'equalprg', either the buffer-local one or the global one.
5414 */
5415 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005416get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 if (*curbuf->b_p_ep == NUL)
5419 return p_ep;
5420 return curbuf->b_p_ep;
5421}
5422
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423/*
5424 * Copy options from one window to another.
5425 * Used when splitting a window.
5426 */
5427 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5431 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005432 after_copy_winopt(wp_to);
5433}
5434
5435/*
5436 * After copying window options: update variables depending on options.
5437 */
5438 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005439after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005440{
5441#ifdef FEAT_LINEBREAK
5442 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005443#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005444#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005445 fill_culopt_flags(NULL, wp);
5446 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450/*
5451 * Copy the options from one winopt_T to another.
5452 * Doesn't free the old option values in "to", use clear_winopt() for that.
5453 * The 'scroll' option is not copied, because it depends on the window height.
5454 * The 'previewwindow' option is reset, there can be only one preview window.
5455 */
5456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005457copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459#ifdef FEAT_ARABIC
5460 to->wo_arab = from->wo_arab;
5461#endif
5462 to->wo_list = from->wo_list;
5463 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005464 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005465#ifdef FEAT_LINEBREAK
5466 to->wo_nuw = from->wo_nuw;
5467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468#ifdef FEAT_RIGHTLEFT
5469 to->wo_rl = from->wo_rl;
5470 to->wo_rlc = vim_strsave(from->wo_rlc);
5471#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005472#ifdef FEAT_LINEBREAK
5473 to->wo_sbr = vim_strsave(from->wo_sbr);
5474#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005475#ifdef FEAT_STL_OPT
5476 to->wo_stl = vim_strsave(from->wo_stl);
5477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005479#ifdef FEAT_DIFF
5480 to->wo_wrap_save = from->wo_wrap_save;
5481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482#ifdef FEAT_LINEBREAK
5483 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005484 to->wo_bri = from->wo_bri;
5485 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005487 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005489 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005490 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005491 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005492#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005493 to->wo_spell = from->wo_spell;
5494#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005495#ifdef FEAT_SYN_HL
5496 to->wo_cuc = from->wo_cuc;
5497 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005498 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501#ifdef FEAT_DIFF
5502 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005503 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005505#ifdef FEAT_CONCEAL
5506 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005507 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005508#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005509#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005510 to->wo_twk = vim_strsave(from->wo_twk);
5511 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#ifdef FEAT_FOLDING
5514 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005515 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005517 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 to->wo_fdi = vim_strsave(from->wo_fdi);
5519 to->wo_fml = from->wo_fml;
5520 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005521 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005523 to->wo_fdm_save = from->wo_diff_saved
5524 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 to->wo_fdn = from->wo_fdn;
5526# ifdef FEAT_EVAL
5527 to->wo_fde = vim_strsave(from->wo_fde);
5528 to->wo_fdt = vim_strsave(from->wo_fdt);
5529# endif
5530 to->wo_fmr = vim_strsave(from->wo_fmr);
5531#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005532#ifdef FEAT_SIGNS
5533 to->wo_scl = vim_strsave(from->wo_scl);
5534#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005535
5536#ifdef FEAT_EVAL
5537 // Copy the script context so that we know where the value was last set.
5538 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5539 sizeof(to->wo_script_ctx));
5540#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005541 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542}
5543
5544/*
5545 * Check string options in a window for a NULL value.
5546 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005548check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549{
5550 check_winopt(&win->w_onebuf_opt);
5551 check_winopt(&win->w_allbuf_opt);
5552}
5553
5554/*
5555 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5556 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005558check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560#ifdef FEAT_FOLDING
5561 check_string_option(&wop->wo_fdi);
5562 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005563 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564# ifdef FEAT_EVAL
5565 check_string_option(&wop->wo_fde);
5566 check_string_option(&wop->wo_fdt);
5567# endif
5568 check_string_option(&wop->wo_fmr);
5569#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005570#ifdef FEAT_SIGNS
5571 check_string_option(&wop->wo_scl);
5572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#ifdef FEAT_RIGHTLEFT
5574 check_string_option(&wop->wo_rlc);
5575#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005576#ifdef FEAT_LINEBREAK
5577 check_string_option(&wop->wo_sbr);
5578#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005579#ifdef FEAT_STL_OPT
5580 check_string_option(&wop->wo_stl);
5581#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005582#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005583 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005584 check_string_option(&wop->wo_cc);
5585#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005586#ifdef FEAT_CONCEAL
5587 check_string_option(&wop->wo_cocu);
5588#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005589#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005590 check_string_option(&wop->wo_twk);
5591 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005592#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005593#ifdef FEAT_LINEBREAK
5594 check_string_option(&wop->wo_briopt);
5595#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005596 check_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
5599/*
5600 * Free the allocated memory inside a winopt_T.
5601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005603clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605#ifdef FEAT_FOLDING
5606 clear_string_option(&wop->wo_fdi);
5607 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005608 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609# ifdef FEAT_EVAL
5610 clear_string_option(&wop->wo_fde);
5611 clear_string_option(&wop->wo_fdt);
5612# endif
5613 clear_string_option(&wop->wo_fmr);
5614#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005615#ifdef FEAT_SIGNS
5616 clear_string_option(&wop->wo_scl);
5617#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005618#ifdef FEAT_LINEBREAK
5619 clear_string_option(&wop->wo_briopt);
5620#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005621 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622#ifdef FEAT_RIGHTLEFT
5623 clear_string_option(&wop->wo_rlc);
5624#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005625#ifdef FEAT_LINEBREAK
5626 clear_string_option(&wop->wo_sbr);
5627#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005628#ifdef FEAT_STL_OPT
5629 clear_string_option(&wop->wo_stl);
5630#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005631#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005632 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633 clear_string_option(&wop->wo_cc);
5634#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005635#ifdef FEAT_CONCEAL
5636 clear_string_option(&wop->wo_cocu);
5637#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005638#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005639 clear_string_option(&wop->wo_twk);
5640 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642}
5643
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005644#ifdef FEAT_EVAL
5645// Index into the options table for a buffer-local option enum.
5646static int buf_opt_idx[BV_COUNT];
5647# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5648
5649/*
5650 * Initialize buf_opt_idx[] if not done already.
5651 */
5652 static void
5653init_buf_opt_idx(void)
5654{
5655 static int did_init_buf_opt_idx = FALSE;
5656 int i;
5657
5658 if (did_init_buf_opt_idx)
5659 return;
5660 did_init_buf_opt_idx = TRUE;
5661 for (i = 0; !istermoption_idx(i); i++)
5662 if (options[i].indir & PV_BUF)
5663 buf_opt_idx[options[i].indir & PV_MASK] = i;
5664}
5665#else
5666# define COPY_OPT_SCTX(buf, bv)
5667#endif
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669/*
5670 * Copy global option values to local options for one buffer.
5671 * Used when creating a new buffer and sometimes when entering a buffer.
5672 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005673 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5675 * appropriate.
5676 * BCO_NOHELP Don't copy the values to a help buffer.
5677 */
5678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005679buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005682 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 int dont_do_help;
5684 int did_isk = FALSE;
5685
5686 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 * Skip this when the option defaults have not been set yet. Happens when
5688 * main() allocates the first buffer.
5689 */
5690 if (p_cpo != NULL)
5691 {
5692 /*
5693 * Always copy when entering and 'cpo' contains 'S'.
5694 * Don't copy when already initialized.
5695 * Don't copy when 'cpo' contains 's' and not entering.
5696 * 'S' BCO_ENTER initialized 's' should_copy
5697 * yes yes X X TRUE
5698 * yes no yes X FALSE
5699 * no X yes X FALSE
5700 * X no no yes FALSE
5701 * X no no no TRUE
5702 * no yes no X TRUE
5703 */
5704 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5705 && (buf->b_p_initialized
5706 || (!(flags & BCO_ENTER)
5707 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5708 should_copy = FALSE;
5709
5710 if (should_copy || (flags & BCO_ALWAYS))
5711 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005712#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005713 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005714 init_buf_opt_idx();
5715#endif
5716 // Don't copy the options specific to a help buffer when
5717 // BCO_NOHELP is given or the options were initialized already
5718 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5720 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005721 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 save_p_isk = buf->b_p_isk;
5724 buf->b_p_isk = NULL;
5725 }
5726 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005727 * Always free the allocated strings. If not already initialized,
5728 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 */
5730 if (!buf->b_p_initialized)
5731 {
5732 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005733 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005736 switch (*p_ffs)
5737 {
5738 case 'm':
5739 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5740 case 'd':
5741 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5742 case 'u':
5743 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5744 default:
5745 buf->b_p_ff = vim_strsave(p_ff);
5746 }
5747 if (buf->b_p_ff != NULL)
5748 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 buf->b_p_bh = empty_option;
5750 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 else
5753 free_buf_options(buf, FALSE);
5754
5755 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005756 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 buf->b_p_ai_nopaste = p_ai_nopaste;
5758 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005759 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005761 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 buf->b_p_tw_nopaste = p_tw_nopaste;
5763 buf->b_p_tw_nobin = p_tw_nobin;
5764 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005765 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 buf->b_p_wm_nopaste = p_wm_nopaste;
5767 buf->b_p_wm_nobin = p_wm_nobin;
5768 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005769 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005770 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005771 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005772 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005773 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005775 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005777 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005779 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 buf->b_p_ml_nobin = p_ml_nobin;
5781 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005782 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005783 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005784 buf->b_p_swf = FALSE;
5785 else
5786 {
5787 buf->b_p_swf = p_swf;
5788 COPY_OPT_SCTX(buf, BV_INF);
5789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005791 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005792#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005793 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005794 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005796#ifdef FEAT_COMPL_FUNC
5797 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005798 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005799 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005800 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005801#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005802#ifdef FEAT_EVAL
5803 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005804 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005807 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005809#ifdef FEAT_VARTABS
5810 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005811 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005812 if (p_vsts && p_vsts != empty_option)
5813 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5814 else
5815 buf->b_p_vsts_array = 0;
5816 buf->b_p_vsts_nopaste = p_vsts_nopaste
5817 ? vim_strsave(p_vsts_nopaste) : NULL;
5818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005820 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005822 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823#ifdef FEAT_FOLDING
5824 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005825 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
5827 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005828 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005829 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005830 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005831 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5832 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005834 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005836 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837#ifdef FEAT_SMARTINDENT
5838 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005839 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840#endif
5841 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005842 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#ifdef FEAT_CINDENT
5844 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005845 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005847 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005849 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005851 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005854 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5856 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005857 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858#endif
5859#ifdef FEAT_LISP
5860 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005861 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#endif
5863#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005864 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005866 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005867 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005868 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005869#endif
5870#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005871 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005872 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005873 (void)compile_cap_prog(&buf->b_s);
5874 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005875 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005876 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005877 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005878 buf->b_s.b_p_spo = vim_strsave(p_spo);
5879 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
5881#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5882 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005883 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005885 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005887 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005888#if defined(FEAT_EVAL)
5889 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005890 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#ifdef FEAT_CRYPT
5893 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005894 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#endif
5896#ifdef FEAT_SEARCHPATH
5897 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005898 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#endif
5900#ifdef FEAT_KEYMAP
5901 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005902 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 buf->b_kmap_state |= KEYMAP_INIT;
5904#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005905#ifdef FEAT_TERMINAL
5906 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005907 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005908#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005909 // This isn't really an option, but copying the langmap and IME
5910 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005912 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005914 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005916 // options that are normally global but also have a local value
5917 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005919 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005920 buf->b_p_bkc = empty_option;
5921 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922#ifdef FEAT_QUICKFIX
5923 buf->b_p_gp = empty_option;
5924 buf->b_p_mp = empty_option;
5925 buf->b_p_efm = empty_option;
5926#endif
5927 buf->b_p_ep = empty_option;
5928 buf->b_p_kp = empty_option;
5929 buf->b_p_path = empty_option;
5930 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005931 buf->b_p_tc = empty_option;
5932 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933#ifdef FEAT_FIND_ID
5934 buf->b_p_def = empty_option;
5935 buf->b_p_inc = empty_option;
5936# ifdef FEAT_EVAL
5937 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005938 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939# endif
5940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 buf->b_p_dict = empty_option;
5942 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005943#ifdef FEAT_TEXTOBJ
5944 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005946#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005947#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5948 buf->b_p_bexpr = empty_option;
5949#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005950#if defined(FEAT_CRYPT)
5951 buf->b_p_cm = empty_option;
5952#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005953#ifdef FEAT_PERSISTENT_UNDO
5954 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005956#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005957#ifdef FEAT_LISP
5958 buf->b_p_lw = empty_option;
5959#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005960 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
5962 /*
5963 * Don't copy the options set by ex_help(), use the saved values,
5964 * when going from a help buffer to a non-help buffer.
5965 * Don't touch these at all when BCO_NOHELP is used and going from
5966 * or to a help buffer.
5967 */
5968 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005969 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005971#ifdef FEAT_VARTABS
5972 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5973 tabstop_set(p_vts, &buf->b_p_vts_array);
5974 else
5975 buf->b_p_vts_array = NULL;
5976#endif
5977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 else
5979 {
5980 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005981 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 did_isk = TRUE;
5983 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005984#ifdef FEAT_VARTABS
5985 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005986 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005987 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
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 if (buf->b_p_bt[0] == 'h')
5994 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005996 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 }
5998 }
5999
6000 /*
6001 * When the options should be copied (ignoring BCO_ALWAYS), set the
6002 * flag that indicates that the options have been initialized.
6003 */
6004 if (should_copy)
6005 buf->b_p_initialized = TRUE;
6006 }
6007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006008 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (did_isk)
6010 (void)buf_init_chartab(buf, FALSE);
6011}
6012
6013/*
6014 * Reset the 'modifiable' option and its default value.
6015 */
6016 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006017reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018{
6019 int opt_idx;
6020
6021 curbuf->b_p_ma = FALSE;
6022 p_ma = FALSE;
6023 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006024 if (opt_idx >= 0)
6025 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026}
6027
6028/*
6029 * Set the global value for 'iminsert' to the local value.
6030 */
6031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006032set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 p_iminsert = curbuf->b_p_iminsert;
6035}
6036
6037/*
6038 * Set the global value for 'imsearch' to the local value.
6039 */
6040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006041set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
6043 p_imsearch = curbuf->b_p_imsearch;
6044}
6045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046static int expand_option_idx = -1;
6047static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6048static int expand_option_flags = 0;
6049
6050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006051set_context_in_set_cmd(
6052 expand_T *xp,
6053 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006054 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006057 long_u flags = 0; // init for GCC
6058 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 char_u *p;
6060 char_u *s;
6061 int is_term_option = FALSE;
6062 int key;
6063
6064 expand_option_flags = opt_flags;
6065
6066 xp->xp_context = EXPAND_SETTINGS;
6067 if (*arg == NUL)
6068 {
6069 xp->xp_pattern = arg;
6070 return;
6071 }
6072 p = arg + STRLEN(arg) - 1;
6073 if (*p == ' ' && *(p - 1) != '\\')
6074 {
6075 xp->xp_pattern = p + 1;
6076 return;
6077 }
6078 while (p > arg)
6079 {
6080 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006081 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 if (*p == ' ' || *p == ',')
6083 {
6084 while (s > arg && *(s - 1) == '\\')
6085 --s;
6086 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006087 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 if (*p == ' ' && ((p - s) & 1) == 0)
6089 {
6090 ++p;
6091 break;
6092 }
6093 --p;
6094 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006095 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 {
6097 xp->xp_context = EXPAND_BOOL_SETTINGS;
6098 p += 2;
6099 }
6100 if (STRNCMP(p, "inv", 3) == 0)
6101 {
6102 xp->xp_context = EXPAND_BOOL_SETTINGS;
6103 p += 3;
6104 }
6105 xp->xp_pattern = arg = p;
6106 if (*arg == '<')
6107 {
6108 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006109 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 return;
6111 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006112 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
6114 xp->xp_context = EXPAND_NOTHING;
6115 return;
6116 }
6117 nextchar = *++p;
6118 is_term_option = TRUE;
6119 expand_option_name[2] = KEY2TERMCAP0(key);
6120 expand_option_name[3] = KEY2TERMCAP1(key);
6121 }
6122 else
6123 {
6124 if (p[0] == 't' && p[1] == '_')
6125 {
6126 p += 2;
6127 if (*p != NUL)
6128 ++p;
6129 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006130 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 nextchar = *++p;
6132 is_term_option = TRUE;
6133 expand_option_name[2] = p[-2];
6134 expand_option_name[3] = p[-1];
6135 }
6136 else
6137 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006138 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6140 p++;
6141 if (*p == NUL)
6142 return;
6143 nextchar = *p;
6144 *p = NUL;
6145 opt_idx = findoption(arg);
6146 *p = nextchar;
6147 if (opt_idx == -1 || options[opt_idx].var == NULL)
6148 {
6149 xp->xp_context = EXPAND_NOTHING;
6150 return;
6151 }
6152 flags = options[opt_idx].flags;
6153 if (flags & P_BOOL)
6154 {
6155 xp->xp_context = EXPAND_NOTHING;
6156 return;
6157 }
6158 }
6159 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006160 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6162 {
6163 ++p;
6164 nextchar = '=';
6165 }
6166 if ((nextchar != '=' && nextchar != ':')
6167 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6168 {
6169 xp->xp_context = EXPAND_UNSUCCESSFUL;
6170 return;
6171 }
6172 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6173 {
6174 xp->xp_context = EXPAND_OLD_SETTING;
6175 if (is_term_option)
6176 expand_option_idx = -1;
6177 else
6178 expand_option_idx = opt_idx;
6179 xp->xp_pattern = p + 1;
6180 return;
6181 }
6182 xp->xp_context = EXPAND_NOTHING;
6183 if (is_term_option || (flags & P_NUM))
6184 return;
6185
6186 xp->xp_pattern = p + 1;
6187
6188 if (flags & P_EXPAND)
6189 {
6190 p = options[opt_idx].var;
6191 if (p == (char_u *)&p_bdir
6192 || p == (char_u *)&p_dir
6193 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006194 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 || p == (char_u *)&p_rtp
6196#ifdef FEAT_SEARCHPATH
6197 || p == (char_u *)&p_cdpath
6198#endif
6199#ifdef FEAT_SESSION
6200 || p == (char_u *)&p_vdir
6201#endif
6202 )
6203 {
6204 xp->xp_context = EXPAND_DIRECTORIES;
6205 if (p == (char_u *)&p_path
6206#ifdef FEAT_SEARCHPATH
6207 || p == (char_u *)&p_cdpath
6208#endif
6209 )
6210 xp->xp_backslash = XP_BS_THREE;
6211 else
6212 xp->xp_backslash = XP_BS_ONE;
6213 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006214 else if (p == (char_u *)&p_ft)
6215 {
6216 xp->xp_context = EXPAND_FILETYPE;
6217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 else
6219 {
6220 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006221 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (p == (char_u *)&p_tags)
6223 xp->xp_backslash = XP_BS_THREE;
6224 else
6225 xp->xp_backslash = XP_BS_ONE;
6226 }
6227 }
6228
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006229 // For an option that is a list of file names, find the start of the
6230 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006233 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (*p == ' ' || *p == ',')
6235 {
6236 s = p;
6237 while (s > xp->xp_pattern && *(s - 1) == '\\')
6238 --s;
6239 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6240 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6241 {
6242 xp->xp_pattern = p + 1;
6243 break;
6244 }
6245 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006246
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006247#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006248 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006249 if (options[opt_idx].var == (char_u *)&p_sps
6250 && STRNCMP(p, "file:", 5) == 0)
6251 {
6252 xp->xp_pattern = p + 5;
6253 break;
6254 }
6255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 }
6257
6258 return;
6259}
6260
6261 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006262ExpandSettings(
6263 expand_T *xp,
6264 regmatch_T *regmatch,
6265 int *num_file,
6266 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006268 int num_normal = 0; // Nr of matching non-term-code settings
6269 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 int opt_idx;
6271 int match;
6272 int count = 0;
6273 char_u *str;
6274 int loop;
6275 int is_term_opt;
6276 char_u name_buf[MAX_KEY_NAME_LEN];
6277 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006278 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006280 // do this loop twice:
6281 // loop == 0: count the number of matching options
6282 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 for (loop = 0; loop <= 1; ++loop)
6284 {
6285 regmatch->rm_ic = ic;
6286 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6287 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006288 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6289 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6291 {
6292 if (loop == 0)
6293 num_normal++;
6294 else
6295 (*file)[count++] = vim_strsave((char_u *)names[match]);
6296 }
6297 }
6298 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6299 opt_idx++)
6300 {
6301 if (options[opt_idx].var == NULL)
6302 continue;
6303 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6304 && !(options[opt_idx].flags & P_BOOL))
6305 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006306 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (is_term_opt && num_normal > 0)
6308 continue;
6309 match = FALSE;
6310 if (vim_regexec(regmatch, str, (colnr_T)0)
6311 || (options[opt_idx].shortname != NULL
6312 && vim_regexec(regmatch,
6313 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6314 match = TRUE;
6315 else if (is_term_opt)
6316 {
6317 name_buf[0] = '<';
6318 name_buf[1] = 't';
6319 name_buf[2] = '_';
6320 name_buf[3] = str[2];
6321 name_buf[4] = str[3];
6322 name_buf[5] = '>';
6323 name_buf[6] = NUL;
6324 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6325 {
6326 match = TRUE;
6327 str = name_buf;
6328 }
6329 }
6330 if (match)
6331 {
6332 if (loop == 0)
6333 {
6334 if (is_term_opt)
6335 num_term++;
6336 else
6337 num_normal++;
6338 }
6339 else
6340 (*file)[count++] = vim_strsave(str);
6341 }
6342 }
6343 /*
6344 * Check terminal key codes, these are not in the option table
6345 */
6346 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6347 {
6348 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6349 {
6350 if (!isprint(str[0]) || !isprint(str[1]))
6351 continue;
6352
6353 name_buf[0] = 't';
6354 name_buf[1] = '_';
6355 name_buf[2] = str[0];
6356 name_buf[3] = str[1];
6357 name_buf[4] = NUL;
6358
6359 match = FALSE;
6360 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6361 match = TRUE;
6362 else
6363 {
6364 name_buf[0] = '<';
6365 name_buf[1] = 't';
6366 name_buf[2] = '_';
6367 name_buf[3] = str[0];
6368 name_buf[4] = str[1];
6369 name_buf[5] = '>';
6370 name_buf[6] = NUL;
6371
6372 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6373 match = TRUE;
6374 }
6375 if (match)
6376 {
6377 if (loop == 0)
6378 num_term++;
6379 else
6380 (*file)[count++] = vim_strsave(name_buf);
6381 }
6382 }
6383
6384 /*
6385 * Check special key names.
6386 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006387 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6389 {
6390 name_buf[0] = '<';
6391 STRCPY(name_buf + 1, str);
6392 STRCAT(name_buf, ">");
6393
6394 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6395 {
6396 if (loop == 0)
6397 num_term++;
6398 else
6399 (*file)[count++] = vim_strsave(name_buf);
6400 }
6401 }
6402 }
6403 if (loop == 0)
6404 {
6405 if (num_normal > 0)
6406 *num_file = num_normal;
6407 else if (num_term > 0)
6408 *num_file = num_term;
6409 else
6410 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006411 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (*file == NULL)
6413 {
6414 *file = (char_u **)"";
6415 return FAIL;
6416 }
6417 }
6418 }
6419 return OK;
6420}
6421
6422 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006425 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 char_u *buf;
6427
6428 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006429 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (*file == NULL)
6431 return FAIL;
6432
6433 /*
6434 * For a terminal key code expand_option_idx is < 0.
6435 */
6436 if (expand_option_idx < 0)
6437 {
6438 var = find_termcode(expand_option_name + 2);
6439 if (var == NULL)
6440 expand_option_idx = findoption(expand_option_name);
6441 }
6442
6443 if (expand_option_idx >= 0)
6444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006445 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 option_value2string(&options[expand_option_idx], expand_option_flags);
6447 var = NameBuff;
6448 }
6449 else if (var == NULL)
6450 var = (char_u *)"";
6451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006452 // A backslash is required before some characters. This is the reverse of
6453 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 buf = vim_strsave_escaped(var, escape_chars);
6455
6456 if (buf == NULL)
6457 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006458 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 return FAIL;
6460 }
6461
6462#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006463 // For MS-Windows et al. we don't double backslashes at the start and
6464 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006465 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (var[0] == '\\' && var[1] == '\\'
6467 && expand_option_idx >= 0
6468 && (options[expand_option_idx].flags & P_EXPAND)
6469 && vim_isfilec(var[2])
6470 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006471 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472#endif
6473
6474 *file[0] = buf;
6475 *num_file = 1;
6476 return OK;
6477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
6479/*
6480 * Get the value for the numeric or string option *opp in a nice format into
6481 * NameBuff[]. Must not be called with a hidden option!
6482 */
6483 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006484option_value2string(
6485 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006486 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 char_u *varp;
6489
6490 varp = get_varp_scope(opp, opt_flags);
6491
6492 if (opp->flags & P_NUM)
6493 {
6494 long wc = 0;
6495
6496 if (wc_use_keyname(varp, &wc))
6497 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6498 else if (wc != 0)
6499 STRCPY(NameBuff, transchar((int)wc));
6500 else
6501 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6502 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006503 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
6505 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006506 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 NameBuff[0] = NUL;
6508#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006509 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006510 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 STRCPY(NameBuff, "*****");
6512#endif
6513 else if (opp->flags & P_EXPAND)
6514 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006515 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 else if ((char_u **)opp->var == &p_pt)
6517 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6518 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006519 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
6521}
6522
6523/*
6524 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6525 * printed as a keyname.
6526 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6527 */
6528 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006529wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
6531 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6532 {
6533 *wcp = *(long *)varp;
6534 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6535 return TRUE;
6536 }
6537 return FALSE;
6538}
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 * Return TRUE if "x" is present in 'shortmess' option, or
6542 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6543 */
6544 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006545shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006547 return p_shm != NULL &&
6548 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 || (vim_strchr(p_shm, 'a') != NULL
6550 && vim_strchr((char_u *)SHM_A, x) != NULL));
6551}
6552
6553/*
6554 * paste_option_changed() - Called after p_paste was set or reset.
6555 */
6556 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006557paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
6559 static int old_p_paste = FALSE;
6560 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006561 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562#ifdef FEAT_CMDL_INFO
6563 static int save_ru = 0;
6564#endif
6565#ifdef FEAT_RIGHTLEFT
6566 static int save_ri = 0;
6567 static int save_hkmap = 0;
6568#endif
6569 buf_T *buf;
6570
6571 if (p_paste)
6572 {
6573 /*
6574 * Paste switched from off to on.
6575 * Save the current values, so they can be restored later.
6576 */
6577 if (!old_p_paste)
6578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006579 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006580 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
6582 buf->b_p_tw_nopaste = buf->b_p_tw;
6583 buf->b_p_wm_nopaste = buf->b_p_wm;
6584 buf->b_p_sts_nopaste = buf->b_p_sts;
6585 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006586 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006587#ifdef FEAT_VARTABS
6588 if (buf->b_p_vsts_nopaste)
6589 vim_free(buf->b_p_vsts_nopaste);
6590 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6591 ? vim_strsave(buf->b_p_vsts) : NULL;
6592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006595 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006597 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598#ifdef FEAT_CMDL_INFO
6599 save_ru = p_ru;
6600#endif
6601#ifdef FEAT_RIGHTLEFT
6602 save_ri = p_ri;
6603 save_hkmap = p_hkmap;
6604#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006605 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006606 p_ai_nopaste = p_ai;
6607 p_et_nopaste = p_et;
6608 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 p_tw_nopaste = p_tw;
6610 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006611#ifdef FEAT_VARTABS
6612 if (p_vsts_nopaste)
6613 vim_free(p_vsts_nopaste);
6614 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
6617
6618 /*
6619 * Always set the option values, also when 'paste' is set when it is
6620 * already on.
6621 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006622 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006623 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006625 buf->b_p_tw = 0; // textwidth is 0
6626 buf->b_p_wm = 0; // wrapmargin is 0
6627 buf->b_p_sts = 0; // softtabstop is 0
6628 buf->b_p_ai = 0; // no auto-indent
6629 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006630#ifdef FEAT_VARTABS
6631 if (buf->b_p_vsts)
6632 free_string_option(buf->b_p_vsts);
6633 buf->b_p_vsts = empty_option;
6634 if (buf->b_p_vsts_array)
6635 vim_free(buf->b_p_vsts_array);
6636 buf->b_p_vsts_array = 0;
6637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006640 // set global options
6641 p_sm = 0; // no showmatch
6642 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006645 status_redraw_all(); // redraw to remove the ruler
6646 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647#endif
6648#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006649 p_ri = 0; // no reverse insert
6650 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006652 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 p_tw = 0;
6654 p_wm = 0;
6655 p_sts = 0;
6656 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006657#ifdef FEAT_VARTABS
6658 if (p_vsts)
6659 free_string_option(p_vsts);
6660 p_vsts = empty_option;
6661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663
6664 /*
6665 * Paste switched from on to off: Restore saved values.
6666 */
6667 else if (old_p_paste)
6668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006669 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006670 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
6672 buf->b_p_tw = buf->b_p_tw_nopaste;
6673 buf->b_p_wm = buf->b_p_wm_nopaste;
6674 buf->b_p_sts = buf->b_p_sts_nopaste;
6675 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006676 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006677#ifdef FEAT_VARTABS
6678 if (buf->b_p_vsts)
6679 free_string_option(buf->b_p_vsts);
6680 buf->b_p_vsts = buf->b_p_vsts_nopaste
6681 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6682 if (buf->b_p_vsts_array)
6683 vim_free(buf->b_p_vsts_array);
6684 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6685 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6686 else
6687 buf->b_p_vsts_array = 0;
6688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006691 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006693 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006696 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 p_ru = save_ru;
6698#endif
6699#ifdef FEAT_RIGHTLEFT
6700 p_ri = save_ri;
6701 p_hkmap = save_hkmap;
6702#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006703 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006704 p_ai = p_ai_nopaste;
6705 p_et = p_et_nopaste;
6706 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 p_tw = p_tw_nopaste;
6708 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006709#ifdef FEAT_VARTABS
6710 if (p_vsts)
6711 free_string_option(p_vsts);
6712 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
6715
6716 old_p_paste = p_paste;
6717}
6718
6719/*
6720 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6721 *
6722 * Reset 'compatible' and set the values for options that didn't get set yet
6723 * to the Vim defaults.
6724 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006725 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 */
6727 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006728vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006730 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006731 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006732 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
6734 if (!option_was_set((char_u *)"cp"))
6735 {
6736 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006737 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6739 set_option_default(opt_idx, OPT_FREE, FALSE);
6740 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006741 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006743
6744 if (fname != NULL)
6745 {
6746 p = vim_getenv(envname, &dofree);
6747 if (p == NULL)
6748 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006749 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006750 p = FullName_save(fname, FALSE);
6751 if (p != NULL)
6752 {
6753 vim_setenv(envname, p);
6754 vim_free(p);
6755 }
6756 }
6757 else if (dofree)
6758 vim_free(p);
6759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760}
6761
6762/*
6763 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6764 */
6765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006766change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006768 int opt_idx;
6769
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 if (p_cp != on)
6771 {
6772 p_cp = on;
6773 compatible_set();
6774 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006775 opt_idx = findoption((char_u *)"cp");
6776 if (opt_idx >= 0)
6777 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778}
6779
6780/*
6781 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006782 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 */
6784 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006785option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 int idx;
6788
6789 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006790 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 return FALSE;
6792 if (options[idx].flags & P_WAS_SET)
6793 return TRUE;
6794 return FALSE;
6795}
6796
6797/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006798 * Reset the flag indicating option "name" was set.
6799 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006800 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006801reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006802{
6803 int idx = findoption(name);
6804
6805 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006806 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006807 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006808 return OK;
6809 }
6810 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006811}
6812
6813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 * compatible_set() - Called when 'compatible' has been set or unset.
6815 *
6816 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6817 * flag) to a Vi compatible value.
6818 * When 'compatible' is unset: Set all options that have a different default
6819 * for Vim (without the P_VI_DEF flag) to that default.
6820 */
6821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006822compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823{
6824 int opt_idx;
6825
Bram Moolenaardac13472019-09-16 21:06:21 +02006826 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6828 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6829 set_option_default(opt_idx, OPT_FREE, p_cp);
6830 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006831 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832}
6833
Bram Moolenaardac13472019-09-16 21:06:21 +02006834#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836/*
6837 * fill_breakat_flags() -- called when 'breakat' changes value.
6838 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006839 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006840fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006842 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 int i;
6844
6845 for (i = 0; i < 256; i++)
6846 breakat_flags[i] = FALSE;
6847
6848 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006849 for (p = p_breakat; *p; p++)
6850 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852#endif
6853
6854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 * Check if backspacing over something is allowed.
6856 */
6857 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006858can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006859 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006861#ifdef FEAT_JOB_CHANNEL
6862 if (what == BS_START && bt_prompt(curbuf))
6863 return FALSE;
6864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 switch (*p_bs)
6866 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006867 case '3': return TRUE;
6868 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 case '1': return (what != BS_START);
6870 case '0': return FALSE;
6871 }
6872 return vim_strchr(p_bs, what) != NULL;
6873}
6874
6875/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006876 * Return the effective 'scrolloff' value for the current window, using the
6877 * global value when appropriate.
6878 */
6879 long
6880get_scrolloff_value(void)
6881{
6882 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6883}
6884
6885/*
6886 * Return the effective 'sidescrolloff' value for the current window, using the
6887 * global value when appropriate.
6888 */
6889 long
6890get_sidescrolloff_value(void)
6891{
6892 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6893}
6894
6895/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006896 * Get the local or global value of 'backupcopy'.
6897 */
6898 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006899get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006900{
6901 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6902}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006903
Bram Moolenaaree857022019-11-09 23:26:40 +01006904#if defined(FEAT_LINEBREAK) || defined(PROTO)
6905/*
6906 * Get the local or global value of 'showbreak'.
6907 */
6908 char_u *
6909get_showbreak_value(win_T *win)
6910{
6911 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6912 return p_sbr;
6913 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6914 return empty_option;
6915 return win->w_p_sbr;
6916}
6917#endif
6918
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006919#if defined(FEAT_EVAL) || defined(PROTO)
6920/*
6921 * Get window or buffer local options.
6922 */
6923 dict_T *
6924get_winbuf_options(int bufopt)
6925{
6926 dict_T *d;
6927 int opt_idx;
6928
6929 d = dict_alloc();
6930 if (d == NULL)
6931 return NULL;
6932
Bram Moolenaardac13472019-09-16 21:06:21 +02006933 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006934 {
6935 struct vimoption *opt = &options[opt_idx];
6936
6937 if ((bufopt && (opt->indir & PV_BUF))
6938 || (!bufopt && (opt->indir & PV_WIN)))
6939 {
6940 char_u *varp = get_varp(opt);
6941
6942 if (varp != NULL)
6943 {
6944 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006945 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006946 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006947 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006948 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006949 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006950 }
6951 }
6952 }
6953
6954 return d;
6955}
6956#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006957
Bram Moolenaardac13472019-09-16 21:06:21 +02006958#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006959/*
6960 * This is called when 'culopt' is changed
6961 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006962 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006963fill_culopt_flags(char_u *val, win_T *wp)
6964{
6965 char_u *p;
6966 char_u culopt_flags_new = 0;
6967
6968 if (val == NULL)
6969 p = wp->w_p_culopt;
6970 else
6971 p = val;
6972 while (*p != NUL)
6973 {
6974 if (STRNCMP(p, "line", 4) == 0)
6975 {
6976 p += 4;
6977 culopt_flags_new |= CULOPT_LINE;
6978 }
6979 else if (STRNCMP(p, "both", 4) == 0)
6980 {
6981 p += 4;
6982 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
6983 }
6984 else if (STRNCMP(p, "number", 6) == 0)
6985 {
6986 p += 6;
6987 culopt_flags_new |= CULOPT_NBR;
6988 }
6989 else if (STRNCMP(p, "screenline", 10) == 0)
6990 {
6991 p += 10;
6992 culopt_flags_new |= CULOPT_SCRLINE;
6993 }
6994
6995 if (*p != ',' && *p != NUL)
6996 return FAIL;
6997 if (*p == ',')
6998 ++p;
6999 }
7000
7001 // Can't have both "line" and "screenline".
7002 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7003 return FAIL;
7004 wp->w_p_culopt_flags = culopt_flags_new;
7005
7006 return OK;
7007}
7008#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007009
7010/*
7011 * Get the value of 'magic' adjusted for Vim9 script.
7012 */
7013 int
7014magic_isset(void)
7015{
7016 switch (magic_overruled)
7017 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007018 case OPTION_MAGIC_ON: return TRUE;
7019 case OPTION_MAGIC_OFF: return FALSE;
7020 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007021 }
7022#ifdef FEAT_EVAL
7023 if (in_vim9script())
7024 return TRUE;
7025#endif
7026 return p_magic;
7027}