blob: 3c248c19319ffd07066b91e8d28669a6662eeb20 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
60static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void option_value2string(struct vimoption *, int opt_flags);
63static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
148 for (n = 0; n < (long)(sizeof(names) / sizeof(char *)); ++n)
149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef HAVE_AVAIL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#else
205# ifdef HAVE_TOTAL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100206 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000207 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_SEARCHPATH
226 {
227 char_u *cdpath;
228 char_u *buf;
229 int i;
230 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000231 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100233 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000234 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (cdpath != NULL)
236 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200237 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (buf != NULL)
239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100240 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 j = 1;
242 for (i = 0; cdpath[i] != NUL; ++i)
243 {
244 if (vim_ispathlistsep(cdpath[i]))
245 buf[j++] = ',';
246 else
247 {
248 if (cdpath[i] == ' ' || cdpath[i] == ',')
249 buf[j++] = '\\';
250 buf[j++] = cdpath[i];
251 }
252 }
253 buf[j] = NUL;
254 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000255 if (opt_idx >= 0)
256 {
257 options[opt_idx].def_val[VI_DEFAULT] = buf;
258 options[opt_idx].flags |= P_DEF_ALLOCED;
259 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200260 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100261 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000263 if (mustfree)
264 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
267#endif
268
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100270 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100272# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 (char_u *)"cp1252"
274# else
275# ifdef VMS
276 (char_u *)"dec-mcs"
277# else
278# ifdef EBCDIC
279 (char_u *)"ebcdic-uk"
280# else
281# ifdef MAC
282 (char_u *)"mac-roman"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100283# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 (char_u *)"hp-roman8"
285# endif
286# endif
287# endif
288# endif
289 );
290#endif
291
292#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100293 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100295# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000296 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297# else
298# ifdef VMS
299 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
300
301# else
302 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
303# endif
304# endif
305 );
306#endif
307
308 /*
309 * Set all the options (except the terminal options) to their default
310 * value. Also set the global value for local options.
311 */
312 set_options_default(0);
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
334 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
392 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000427# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100428 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000429 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# endif
432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100433 // enc_locale() will try to find the encoding of the current locale.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 p = enc_locale();
435 if (p != NULL)
436 {
437 char_u *save_enc;
438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100439 // Try setting 'encoding' and check if the value is valid.
440 // If not, go back to the default "latin1".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 save_enc = p_enc;
442 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000443 if (STRCMP(p_enc, "gb18030") == 0)
444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100445 // We don't support "gb18030", but "cp936" is a good substitute
446 // for practical purposes, thus use that. It's not an alias to
447 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000448 p_enc = vim_strsave((char_u *)"cp936");
449 vim_free(p);
450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if (mb_init() == NULL)
452 {
453 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000454 if (opt_idx >= 0)
455 {
456 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
457 options[opt_idx].flags |= P_DEF_ALLOCED;
458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
Bram Moolenaard0573012017-10-28 21:11:06 +0200460#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100463 // Adjust the default for 'isprint' and 'iskeyword' to match
464 // latin1. Also set the defaults for when 'nocompatible' is
465 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000466 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000467 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000468 set_string_option_direct((char_u *)"isk", -1,
469 ISK_LATIN1, OPT_FREE, SID_NONE);
470 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000471 if (opt_idx >= 0)
472 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000473 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000474 if (opt_idx >= 0)
475 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000476 (void)init_chartab();
477 }
478#endif
479
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200480#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100481 // Win32 console: When GetACP() returns a different value from
482 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200483 if (
484# ifdef VIMDLL
485 (!gui.in_use && !gui.starting) &&
486# endif
487 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
489 char buf[50];
490
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
492 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100493 if (GetConsoleCP() == 0)
494 sprintf(buf, "cp%ld", (long)GetACP());
495 else
496 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 p_tenc = vim_strsave((char_u *)buf);
498 if (p_tenc != NULL)
499 {
500 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000501 if (opt_idx >= 0)
502 {
503 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
504 options[opt_idx].flags |= P_DEF_ALLOCED;
505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 convert_setup(&input_conv, p_tenc, p_enc);
507 convert_setup(&output_conv, p_enc, p_tenc);
508 }
509 else
510 p_tenc = empty_option;
511 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100512#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100513#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100514 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000515 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 else
519 {
520 vim_free(p_enc);
521 p_enc = save_enc;
522 }
523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
525#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100526 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 set_helplang_default(get_mess_lang());
528#endif
529}
530
531/*
532 * Set an option to its default value.
533 * This does not take care of side effects!
534 */
535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536set_option_default(
537 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100538 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
539 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 char_u *varp; // pointer to variable for current option
542 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000544 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
546
547 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
548 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
551 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
552 if (flags & P_STRING)
553 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100554 // Use set_string_option_direct() for local options to handle
555 // freeing and allocating the value.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200556 if (options[opt_idx].indir != PV_NONE)
557 set_string_option_direct(NULL, opt_idx,
558 options[opt_idx].def_val[dvi], opt_flags, 0);
559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200561 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
562 free_string_option(*(char_u **)(varp));
563 *(char_u **)varp = options[opt_idx].def_val[dvi];
564 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 }
567 else if (flags & P_NUM)
568 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000569 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 win_comp_scroll(curwin);
571 else
572 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100573 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
574
575 if ((long *)varp == &curwin->w_p_so
576 || (long *)varp == &curwin->w_p_siso)
577 // 'scrolloff' and 'sidescrolloff' local values have a
578 // different default value than the global default.
579 *(long *)varp = -1;
580 else
581 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100582 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (both)
584 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100585 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100590 // the cast to long is required for Manx C, long_i is needed for
591 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000592 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000593#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000595 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
596 *(int *)varp = FALSE;
597#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (both)
600 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
601 *(int *)varp;
602 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000605 flagsp = insecure_flag(opt_idx, opt_flags);
606 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608
609#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200610 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#endif
612}
613
614/*
615 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200616 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100620 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000624 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaardac13472019-09-16 21:06:21 +0200626 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200627 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200628 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100629 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200630# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200631 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200632 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200633# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100634 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 set_option_default(i, opt_flags, p_cp);
636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100637 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000638 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200640#ifdef FEAT_CINDENT
641 parse_cino(curbuf);
642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643}
644
645/*
646 * Set the Vi-default value of a string option.
647 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100648 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100650 static void
651set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 char_u *p;
654 int opt_idx;
655
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100656 if (escape && vim_strchr(val, ' ') != NULL)
657 p = vim_strsave_escaped(val, (char_u *)" ");
658 else
659 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000663 if (opt_idx >= 0)
664 {
665 if (options[opt_idx].flags & P_DEF_ALLOCED)
666 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
667 options[opt_idx].def_val[VI_DEFAULT] = p;
668 options[opt_idx].flags |= P_DEF_ALLOCED;
669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671}
672
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 void
674set_string_default(char *name, char_u *val)
675{
676 set_string_default_esc(name, val, FALSE);
677}
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200680 * For an option value that contains comma separated items, find "newval" in
681 * "origval". Return NULL if not found.
682 */
683 static char_u *
684find_dup_item(char_u *origval, char_u *newval, long_u flags)
685{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200686 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200687 size_t newlen;
688 char_u *s;
689
690 if (origval == NULL)
691 return NULL;
692
693 newlen = STRLEN(newval);
694 for (s = origval; *s != NUL; ++s)
695 {
696 if ((!(flags & P_COMMA)
697 || s == origval
698 || (s[-1] == ',' && !(bs & 1)))
699 && STRNCMP(s, newval, newlen) == 0
700 && (!(flags & P_COMMA)
701 || s[newlen] == ','
702 || s[newlen] == NUL))
703 return s;
704 // Count backslashes. Only a comma with an even number of backslashes
705 // or a single backslash preceded by a comma before it is recognized as
706 // a separator.
707 if ((s > origval + 1
708 && s[-1] == '\\'
709 && s[-2] != ',')
710 || (s == origval + 1
711 && s[-1] == '\\'))
712 ++bs;
713 else
714 bs = 0;
715 }
716 return NULL;
717}
718
719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 * Set the Vi-default value of a number option.
721 * Used for 'lines' and 'columns'.
722 */
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000726 int opt_idx;
727
728 opt_idx = findoption((char_u *)name);
729 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000730 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731}
732
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200733/*
734 * Set all window-local and buffer-local options to the Vim default.
735 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200736 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200737 */
738 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200739set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200740{
741 win_T *save_curwin = curwin;
742 int i;
743
744 curwin = wp;
745 curbuf = curwin->w_buffer;
746 block_autocmds();
747
Bram Moolenaardac13472019-09-16 21:06:21 +0200748 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200749 {
750 struct vimoption *p = &(options[i]);
751 char_u *varp = get_varp_scope(p, OPT_LOCAL);
752
753 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200754 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200755 && !(options[i].flags & P_NODEFAULT)
756 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200757 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200758 }
759
760 unblock_autocmds();
761 curwin = save_curwin;
762 curbuf = curwin->w_buffer;
763}
764
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000765#if defined(EXITFREE) || defined(PROTO)
766/*
767 * Free all options.
768 */
769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100770free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000771{
772 int i;
773
Bram Moolenaardac13472019-09-16 21:06:21 +0200774 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000775 {
776 if (options[i].indir == PV_NONE)
777 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100778 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100779 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000780 free_string_option(*(char_u **)options[i].var);
781 if (options[i].flags & P_DEF_ALLOCED)
782 free_string_option(options[i].def_val[VI_DEFAULT]);
783 }
784 else if (options[i].var != VAR_WIN
785 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100786 // buffer-local option: free global value
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000787 free_string_option(*(char_u **)options[i].var);
788 }
789}
790#endif
791
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
794 * Initialize the options, part two: After getting Rows and Columns and
795 * setting 'term'.
796 */
797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100798set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000800 int idx;
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100803 * 'scroll' defaults to half the window height. The stored default is zero,
804 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000806 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000807 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000808 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 comp_col();
810
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000811 /*
812 * 'window' is only for backwards compatibility with Vi.
813 * Default is Rows - 1.
814 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000815 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000816 p_window = Rows - 1;
817 set_number_default("window", Rows - 1);
818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100820#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000821 /*
822 * If 'background' wasn't set by the user, try guessing the value,
823 * depending on the terminal name. Only need to check for terminals
824 * with a dark background, that can handle color.
825 */
826 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000827 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
828 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000830 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 // don't mark it as set, when starting the GUI it may be
832 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000833 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000836
837#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000839#endif
840#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000842#endif
843#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846}
847
848/*
849 * Initialize the options, part three: After reading the .vimrc
850 */
851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100852set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100854#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855/*
856 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
857 * This is done after other initializations, where 'shell' might have been
858 * set, but only if they have not been set before.
859 */
860 char_u *p;
861 int idx_srr;
862 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100863# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 int idx_sp;
865 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000869 if (idx_srr < 0)
870 do_srr = FALSE;
871 else
872 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100873# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000875 if (idx_sp < 0)
876 do_sp = FALSE;
877 else
878 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100879# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200880 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (p != NULL)
882 {
883 /*
884 * Default for p_sp is "| tee", for p_srr is ">".
885 * For known shells it is changed here to include stderr.
886 */
887 if ( fnamecmp(p, "csh") == 0
888 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100889# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 || fnamecmp(p, "csh.exe") == 0
891 || fnamecmp(p, "tcsh.exe") == 0
892# endif
893 )
894 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100895# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (do_sp)
897 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100898# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100900# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
904 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (do_srr)
907 {
908 p_srr = (char_u *)">&";
909 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
910 }
911 }
912 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100913 // Always use bourne shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if ( fnamecmp(p, "sh") == 0
915 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200916 || fnamecmp(p, "mksh") == 0
917 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000919 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200921 || fnamecmp(p, "fish") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100922# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 || fnamecmp(p, "cmd") == 0
924 || fnamecmp(p, "sh.exe") == 0
925 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200926 || fnamecmp(p, "mksh.exe") == 0
927 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000929 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 || fnamecmp(p, "bash.exe") == 0
931 || fnamecmp(p, "cmd.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100933 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100935# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (do_sp)
937 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100938# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100940# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
944 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (do_srr)
947 {
948 p_srr = (char_u *)">%s 2>&1";
949 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
950 }
951 }
952 vim_free(p);
953 }
954#endif
955
Bram Moolenaar4f974752019-02-17 17:44:42 +0100956#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100958 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
959 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * This is done after other initializations, where 'shell' might have been
961 * set, but only if they have not been set before. Default for p_shcf is
962 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100963 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000965 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 int idx3;
968
969 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000970 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 p_shcf = (char_u *)"-c";
973 options[idx3].def_val[VI_DEFAULT] = p_shcf;
974 }
975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000978 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 p_sxq = (char_u *)"\"";
981 options[idx3].def_val[VI_DEFAULT] = p_sxq;
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100984 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
985 {
986 int idx3;
987
988 /*
989 * cmd.exe on Windows will strip the first and last double quote given
990 * on the command line, e.g. most of the time things like:
991 * cmd /c "my path/to/echo" "my args to echo"
992 * become:
993 * my path/to/echo" "my args to echo
994 * when executed.
995 *
Bram Moolenaar034b1152012-02-19 18:19:30 +0100996 * To avoid this, set shellxquote to surround the command in
997 * parenthesis. This appears to make most commands work, without
998 * breaking commands that worked previously, such as
999 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001000 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001001 idx3 = findoption((char_u *)"sxq");
1002 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1003 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001004 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001005 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1006 }
1007
Bram Moolenaara64ba222012-02-12 23:23:31 +01001008 idx3 = findoption((char_u *)"shcf");
1009 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1010 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001011 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001012 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1013 }
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001017 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001018 {
1019 int idx_ffs = findoption((char_u *)"ffs");
1020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001021 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001022 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1023 set_fileformat(default_fileformat(), OPT_LOCAL);
1024 }
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#ifdef FEAT_TITLE
1027 set_title_defaults();
1028#endif
1029}
1030
1031#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1032/*
1033 * When 'helplang' is still at its default value, set it to "lang".
1034 * Only the first two characters of "lang" are used.
1035 */
1036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001037set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 int idx;
1040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001041 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 return;
1043 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001044 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 if (options[idx].flags & P_ALLOCED)
1047 free_string_option(p_hlg);
1048 p_hlg = vim_strsave(lang);
1049 if (p_hlg == NULL)
1050 p_hlg = empty_option;
1051 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001052 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001053 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001054 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1055 {
1056 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1057 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1058 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001059 // any C like setting, such as C.UTF-8, becomes "en"
1060 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1061 {
1062 p_hlg[0] = 'e';
1063 p_hlg[1] = 'n';
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 options[idx].flags |= P_ALLOCED;
1068 }
1069}
1070#endif
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_TITLE
1073/*
1074 * 'title' and 'icon' only default to true if they have not been set or reset
1075 * in .vimrc and we can read the old value.
1076 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1077 * they can be reset. This reduces startup time when using X on a remote
1078 * machine.
1079 */
1080 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001081set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 int idx1;
1084 long val;
1085
1086 /*
1087 * If GUI is (going to be) used, we can always set the window title and
1088 * icon name. Saves a bit of time, because the X11 display server does
1089 * not need to be contacted.
1090 */
1091 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001092 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
1094#ifdef FEAT_GUI
1095 if (gui.starting || gui.in_use)
1096 val = TRUE;
1097 else
1098#endif
1099 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001100 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 p_title = val;
1102 }
1103 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001104 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106#ifdef FEAT_GUI
1107 if (gui.starting || gui.in_use)
1108 val = TRUE;
1109 else
1110#endif
1111 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001112 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 p_icon = val;
1114 }
1115}
1116#endif
1117
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001118 void
1119ex_set(exarg_T *eap)
1120{
1121 int flags = 0;
1122
1123 if (eap->cmdidx == CMD_setlocal)
1124 flags = OPT_LOCAL;
1125 else if (eap->cmdidx == CMD_setglobal)
1126 flags = OPT_GLOBAL;
1127#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001128 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001129 ex_options(eap);
1130 else
1131#endif
1132 {
1133 if (eap->forceit)
1134 flags |= OPT_ONECOLUMN;
1135 (void)do_set(eap->arg, flags);
1136 }
1137}
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139/*
1140 * Parse 'arg' for option settings.
1141 *
1142 * 'arg' may be IObuff, but only when no errors can be present and option
1143 * does not need to be expanded with option_expand().
1144 * "opt_flags":
1145 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001146 * OPT_GLOBAL for ":setglobal"
1147 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001149 * OPT_WINONLY to only set window-local options
1150 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *
1152 * returns FAIL if an error is detected, OK otherwise
1153 */
1154 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001157 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001160 char *errmsg;
1161 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001163 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1164 int nextchar; // next non-white char after option name
1165 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 int len;
1167 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001168 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 long_u flags; // flags for current option
1171 char_u *varp = NULL; // pointer to variable for current option
1172 int did_show = FALSE; // already showed one value
1173 int adding; // "opt+=arg"
1174 int prepending; // "opt^=arg"
1175 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int cp_val = 0;
1177 char_u key_name[2];
1178
1179 if (*arg == NUL)
1180 {
1181 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001182 did_show = TRUE;
1183 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001186 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
1188 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001191 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1192 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 /*
1195 * ":set all" show all options.
1196 * ":set all&" set all options to their default value.
1197 */
1198 arg += 3;
1199 if (*arg == '&')
1200 {
1201 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001202 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001204 didset_options();
1205 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001206 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001211 did_show = TRUE;
1212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001214 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
1216 showoptions(2, opt_flags);
1217 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001218 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 arg += 7;
1220 }
1221 else
1222 {
1223 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001224 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 prefix = 0;
1227 arg += 2;
1228 }
1229 else if (STRNCMP(arg, "inv", 3) == 0)
1230 {
1231 prefix = 2;
1232 arg += 3;
1233 }
1234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 key = 0;
1237 if (*arg == '<')
1238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1242 len = 5;
1243 else
1244 {
1245 len = 1;
1246 while (arg[len] != NUL && arg[len] != '>')
1247 ++len;
1248 }
1249 if (arg[len] != '>')
1250 {
1251 errmsg = e_invarg;
1252 goto skip;
1253 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 arg[len] = NUL; // put NUL after name
1255 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001259 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 else
1262 {
1263 len = 0;
1264 /*
1265 * The two characters after "t_" may not be alphanumeric.
1266 */
1267 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1268 len = 4;
1269 else
1270 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1271 ++len;
1272 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001273 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001277 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001280 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 afterchar = arg[len];
1282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001284 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 ++len;
1286
1287 adding = FALSE;
1288 prepending = FALSE;
1289 removing = FALSE;
1290 if (arg[len] != NUL && arg[len + 1] == '=')
1291 {
1292 if (arg[len] == '+')
1293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001294 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ++len;
1296 }
1297 else if (arg[len] == '^')
1298 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001299 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 ++len;
1301 }
1302 else if (arg[len] == '-')
1303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001304 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ++len;
1306 }
1307 }
1308 nextchar = arg[len];
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001312 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 goto skip;
1314 }
1315
1316 if (opt_idx >= 0)
1317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001318 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 // Only give an error message when requesting the value of
1321 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1323 && (!(options[opt_idx].flags & P_BOOL)
1324 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001325 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 goto skip;
1327 }
1328
1329 flags = options[opt_idx].flags;
1330 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1331 }
1332 else
1333 {
1334 flags = P_STRING;
1335 if (key < 0)
1336 {
1337 key_name[0] = KEY2TERMCAP0(key);
1338 key_name[1] = KEY2TERMCAP1(key);
1339 }
1340 else
1341 {
1342 key_name[0] = KS_KEY;
1343 key_name[1] = (key & 0xff);
1344 }
1345 }
1346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001347 // Skip all options that are not window-local (used when showing
1348 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001349 if ((opt_flags & OPT_WINONLY)
1350 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1351 goto skip;
1352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001353 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001354 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1355 && options[opt_idx].var == VAR_WIN)
1356 goto skip;
1357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001358 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001359 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001361 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001362 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001363 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001364 goto skip;
1365 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001366 if ((flags & P_MLE) && !p_mle)
1367 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001368 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001369 goto skip;
1370 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001371#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001372 // In diff mode some options are overruled. This avoids that
1373 // 'foldmethod' becomes "marker" instead of "diff" and that
1374 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001375 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001376 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001377 && (
1378#ifdef FEAT_FOLDING
1379 options[opt_idx].indir == PV_FDM ||
1380#endif
1381 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001382 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
1386#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001387 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001390 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 goto skip;
1392 }
1393#endif
1394
1395 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1396 {
1397 arg += len;
1398 cp_val = p_cp;
1399 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1400 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
1403 cp_val = FALSE;
1404 arg += 3;
1405 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
1408 cp_val = TRUE;
1409 arg += 2;
1410 }
1411 }
1412 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001413 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
1415 errmsg = e_trailing;
1416 goto skip;
1417 }
1418 }
1419
1420 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001421 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001422 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
1424 if (nextchar == '?'
1425 || (prefix == 1
1426 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1427 && !(flags & P_BOOL)))
1428 {
1429 /*
1430 * print value
1431 */
1432 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001433 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else
1435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 gotocmdline(TRUE); // cursor at status line
1437 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 if (opt_idx >= 0)
1440 {
1441 showoneopt(&options[opt_idx], opt_flags);
1442#ifdef FEAT_EVAL
1443 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001445 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001446 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001447 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001448 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001449 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001450 (int)options[opt_idx].indir & PV_MASK]);
1451 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001452 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001453 (int)options[opt_idx].indir & PV_MASK]);
1454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
1456 }
1457 else
1458 {
1459 char_u *p;
1460
1461 p = find_termcode(key_name);
1462 if (p == NULL)
1463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001464 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 goto skip;
1466 }
1467 else
1468 (void)show_one_termcode(key_name, p, TRUE);
1469 }
1470 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001471 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 errmsg = e_trailing;
1473 }
1474 else
1475 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001476 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001477 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001479 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 if (nextchar == '=' || nextchar == ':')
1482 {
1483 errmsg = e_invarg;
1484 goto skip;
1485 }
1486
1487 /*
1488 * ":set opt!": invert
1489 * ":set opt&": reset to default value
1490 * ":set opt<": reset to global value
1491 */
1492 if (nextchar == '!')
1493 value = *(int *)(varp) ^ 1;
1494 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001495 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ((flags & P_VI_DEF) || cp_val)
1497 ? VI_DEFAULT : VIM_DEFAULT];
1498 else if (nextchar == '<')
1499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001500 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if ((int *)varp == &curbuf->b_p_ar
1502 && opt_flags == OPT_LOCAL)
1503 value = -1;
1504 else
1505 value = *(int *)get_varp_scope(&(options[opt_idx]),
1506 OPT_GLOBAL);
1507 }
1508 else
1509 {
1510 /*
1511 * ":set invopt": invert
1512 * ":set opt" or ":set noopt": set or reset
1513 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001514 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
1516 errmsg = e_trailing;
1517 goto skip;
1518 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001519 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 value = *(int *)(varp) ^ 1;
1521 else
1522 value = prefix;
1523 }
1524
1525 errmsg = set_bool_option(opt_idx, varp, (int)value,
1526 opt_flags);
1527 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1531 || prefix != 1)
1532 {
1533 errmsg = e_invarg;
1534 goto skip;
1535 }
1536
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001537 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
1539 /*
1540 * Different ways to set a number option:
1541 * & set to default value
1542 * < set to global value
1543 * <xx> accept special key codes for 'wildchar'
1544 * c accept any non-digit for 'wildchar'
1545 * [-]0-9 set number
1546 * other error
1547 */
1548 ++arg;
1549 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001550 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 ((flags & P_VI_DEF) || cp_val)
1552 ? VI_DEFAULT : VIM_DEFAULT];
1553 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001554 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001555 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1556 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001557 if ((long *)varp == &curbuf->b_p_ul
1558 && opt_flags == OPT_LOCAL)
1559 value = NO_LOCAL_UNDOLEVEL;
1560 else
1561 value = *(long *)get_varp_scope(
1562 &(options[opt_idx]), OPT_GLOBAL);
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 else if (((long *)varp == &p_wc
1565 || (long *)varp == &p_wcm)
1566 && (*arg == '<'
1567 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001568 || (*arg != NUL
1569 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 && !VIM_ISDIGIT(*arg))))
1571 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001572 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (value == 0 && (long *)varp != &p_wcm)
1574 {
1575 errmsg = e_invarg;
1576 goto skip;
1577 }
1578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1580 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581 // Allow negative (for 'undolevels'), octal and
1582 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001583 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001584 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001585 if (i == 0 || (arg[i] != NUL
1586 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001588 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 goto skip;
1590 }
1591 }
1592 else
1593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 goto skip;
1596 }
1597
1598 if (adding)
1599 value = *(long *)varp + value;
1600 if (prepending)
1601 value = *(long *)varp * value;
1602 if (removing)
1603 value = *(long *)varp - value;
1604 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001605 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001609 char_u *save_arg = NULL;
1610 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001611 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001612 char_u *newval;
1613 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001614 char_u *origval_l = NULL;
1615 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001616#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001617 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 char_u *saved_origval_l = NULL;
1619 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001620 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001621#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001622 unsigned newlen;
1623 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001624 int new_value_alloced; // new string option
1625 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 // When using ":set opt=val" for a global option
1628 // with a local value the local value will be
1629 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001631 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 varp = options[opt_idx].var;
1633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 // The old value is kept until we are sure that the
1635 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001637
Bram Moolenaard7c96872019-06-15 17:12:48 +02001638 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1639 {
1640 origval_l = *(char_u **)get_varp_scope(
1641 &(options[opt_idx]), OPT_LOCAL);
1642 origval_g = *(char_u **)get_varp_scope(
1643 &(options[opt_idx]), OPT_GLOBAL);
1644
1645 // A global-local string option might have an empty
1646 // option as value to indicate that the global
1647 // value should be used.
1648 if (((int)options[opt_idx].indir & PV_BOTH)
1649 && origval_l == empty_option)
1650 origval_l = origval_g;
1651 }
1652
1653 // When setting the local value of a global
1654 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001655 if (((int)options[opt_idx].indir & PV_BOTH)
1656 && (opt_flags & OPT_LOCAL))
1657 origval = *(char_u **)get_varp(
1658 &options[opt_idx]);
1659 else
1660 origval = oldval;
1661
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001662 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 newval = options[opt_idx].def_val[
1665 ((flags & P_VI_DEF) || cp_val)
1666 ? VI_DEFAULT : VIM_DEFAULT];
1667 if ((char_u **)varp == &p_bg)
1668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#ifdef FEAT_GUI
1671 if (gui.in_use)
1672 newval = gui_bg_default();
1673 else
1674#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001675 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001678 // expand environment variables and ~ (since the
1679 // default value was already expanded, only
1680 // required when an environment variable was set
1681 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (newval == NULL)
1683 newval = empty_option;
1684 else
1685 {
1686 s = option_expand(opt_idx, newval);
1687 if (s == NULL)
1688 s = newval;
1689 newval = vim_strsave(s);
1690 }
1691 new_value_alloced = TRUE;
1692 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001693 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 newval = vim_strsave(*(char_u **)get_varp_scope(
1696 &(options[opt_idx]), OPT_GLOBAL));
1697 new_value_alloced = TRUE;
1698 }
1699 else
1700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001701 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 /*
1704 * Set 'keywordprg' to ":help" if an empty
1705 * value was passed to :set by the user.
1706 * Misuse errbuf[] for the resulting string.
1707 */
1708 if (varp == (char_u *)&p_kp
1709 && (*arg == NUL || *arg == ' '))
1710 {
1711 STRCPY(errbuf, ":help");
1712 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001713 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001716 * Convert 'backspace' number to string, for
1717 * adding, prepending and removing string.
1718 */
1719 else if (varp == (char_u *)&p_bs
1720 && VIM_ISDIGIT(**(char_u **)varp))
1721 {
1722 i = getdigits((char_u **)varp);
1723 switch (i)
1724 {
1725 case 0:
1726 *(char_u **)varp = empty_option;
1727 break;
1728 case 1:
1729 *(char_u **)varp = vim_strsave(
1730 (char_u *)"indent,eol");
1731 break;
1732 case 2:
1733 *(char_u **)varp = vim_strsave(
1734 (char_u *)"indent,eol,start");
1735 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001736 case 3:
1737 *(char_u **)varp = vim_strsave(
1738 (char_u *)"indent,eol,nostop");
1739 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001740 }
1741 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001742 if (origval == oldval)
1743 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001744 if (origval_l == oldval)
1745 origval_l = *(char_u **)varp;
1746 if (origval_g == oldval)
1747 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001748 oldval = *(char_u **)varp;
1749 }
1750 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * Convert 'whichwrap' number to string, for
1752 * backwards compatibility with Vim 3.0.
1753 * Misuse errbuf[] for the resulting string.
1754 */
1755 else if (varp == (char_u *)&p_ww
1756 && VIM_ISDIGIT(*arg))
1757 {
1758 *errbuf = NUL;
1759 i = getdigits(&arg);
1760 if (i & 1)
1761 STRCAT(errbuf, "b,");
1762 if (i & 2)
1763 STRCAT(errbuf, "s,");
1764 if (i & 4)
1765 STRCAT(errbuf, "h,l,");
1766 if (i & 8)
1767 STRCAT(errbuf, "<,>,");
1768 if (i & 16)
1769 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001770 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 errbuf[STRLEN(errbuf) - 1] = NUL;
1772 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001773 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775 /*
1776 * Remove '>' before 'dir' and 'bdir', for
1777 * backwards compatibility with version 3.0
1778 */
1779 else if ( *arg == '>'
1780 && (varp == (char_u *)&p_dir
1781 || varp == (char_u *)&p_bdir))
1782 {
1783 ++arg;
1784 }
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
1787 * Copy the new string into allocated memory.
1788 * Can't use set_string_option_direct(), because
1789 * we need to remove the backslashes.
1790 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001791 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 newlen = (unsigned)STRLEN(arg) + 1;
1793 if (adding || prepending || removing)
1794 newlen += (unsigned)STRLEN(origval) + 1;
1795 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001796 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 break;
1798 s = newval;
1799
1800 /*
1801 * Copy the string, skip over escaped chars.
1802 * For MS-DOS and WIN32 backslashes before normal
1803 * file name characters are not removed, and keep
1804 * backslash at start, for "\\machine\path", but
1805 * do remove it for "\\\\machine\\path".
1806 * The reverse is found in ExpandOldSetting().
1807 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001808 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 if (*arg == '\\' && arg[1] != NUL
1811#ifdef BACKSLASH_IN_FILENAME
1812 && !((flags & P_EXPAND)
1813 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001814 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 && (arg[1] != '\\'
1816 || (s == newval
1817 && arg[2] != '\\')))
1818#endif
1819 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001822 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001824 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 mch_memmove(s, arg, (size_t)i);
1826 arg += i;
1827 s += i;
1828 }
1829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 *s++ = *arg++;
1831 }
1832 *s = NUL;
1833
1834 /*
1835 * Expand environment variables and ~.
1836 * Don't do it when adding without inserting a
1837 * comma.
1838 */
1839 if (!(adding || prepending || removing)
1840 || (flags & P_COMMA))
1841 {
1842 s = option_expand(opt_idx, newval);
1843 if (s != NULL)
1844 {
1845 vim_free(newval);
1846 newlen = (unsigned)STRLEN(s) + 1;
1847 if (adding || prepending || removing)
1848 newlen += (unsigned)STRLEN(origval) + 1;
1849 newval = alloc(newlen);
1850 if (newval == NULL)
1851 break;
1852 STRCPY(newval, s);
1853 }
1854 }
1855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001856 // locate newval[] in origval[] when removing it
1857 // and when adding to avoid duplicates
1858 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (removing || (flags & P_NODUP))
1860 {
1861 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001862 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001865 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
1867 prepending = FALSE;
1868 adding = FALSE;
1869 STRCPY(newval, origval);
1870 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001871
1872 // if no duplicate, move pointer to end of
1873 // original value
1874 if (s == NULL)
1875 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001878 // concatenate the two strings; add a ',' if
1879 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (adding || prepending)
1881 {
1882 comma = ((flags & P_COMMA) && *origval != NUL
1883 && *newval != NUL);
1884 if (adding)
1885 {
1886 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001887 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001888 if (comma && i > 1
1889 && (flags & P_ONECOMMA) == P_ONECOMMA
1890 && origval[i - 1] == ','
1891 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001892 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 mch_memmove(newval + i + comma, newval,
1894 STRLEN(newval) + 1);
1895 mch_memmove(newval, origval, (size_t)i);
1896 }
1897 else
1898 {
1899 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001900 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 if (comma)
1903 newval[i] = ',';
1904 }
1905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001906 // Remove newval[] from origval[]. (Note: "i" has
1907 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (removing)
1909 {
1910 STRCPY(newval, origval);
1911 if (*s)
1912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001913 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (flags & P_COMMA)
1915 {
1916 if (s == origval)
1917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (s[i] == ',')
1920 ++i;
1921 }
1922 else
1923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001924 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 --s;
1926 ++i;
1927 }
1928 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001929 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 }
1932
1933 if (flags & P_FLAGLIST)
1934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001935 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001936 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001938 // if options have P_FLAGLIST and
1939 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001940 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001942 if (*s != ',' && *(s + 1) == ','
1943 && vim_strchr(s + 2, *s) != NULL)
1944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 // Remove the duplicated value and
1946 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001947 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001948 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001951 else
1952 {
1953 if ((!(flags & P_COMMA) || *s != ',')
1954 && vim_strchr(s + 1, *s) != NULL)
1955 {
1956 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001957 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001958 }
1959 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001960 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 arg = save_arg;
1966 new_value_alloced = TRUE;
1967 }
1968
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001969 /*
1970 * Set the new value.
1971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 *(char_u **)(varp) = newval;
1973
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001974#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001975 if (!starting
1976# ifdef FEAT_CRYPT
1977 && options[opt_idx].indir != PV_KEY
1978# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001979 && origval != NULL && newval != NULL)
1980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001981 // origval may be freed by
1982 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02001983 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // newval (and varp) may become invalid if the
1985 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001986 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001987 if (origval_l != NULL)
1988 saved_origval_l = vim_strsave(origval_l);
1989 if (origval_g != NULL)
1990 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001991 }
Bram Moolenaar53744302015-07-17 17:38:22 +02001992#endif
1993
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001994 {
1995 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001996 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001997
1998 // When an option is set in the sandbox, from a
1999 // modeline or in secure mode, then deal with side
2000 // effects in secure mode. Also when the value was
2001 // set with the P_INSECURE flag and is not
2002 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002003 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002004#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002005 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002006#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002007 || (!value_is_replaced && (*p & P_INSECURE)))
2008 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002009
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002010 // Handle side effects, and set the global value
2011 // for ":set" on local options. Note: when setting
2012 // 'syntax' or 'filetype' autocommands may be
2013 // triggered that can cause havoc.
2014 errmsg = did_set_string_option(
2015 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002016 new_value_alloced, oldval, errbuf,
2017 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002018
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002019 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002022#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002023 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002024 trigger_optionsset_string(
2025 opt_idx, opt_flags, saved_origval,
2026 saved_origval_l, saved_origval_g,
2027 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002028 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002029 vim_free(saved_origval_l);
2030 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002031 vim_free(saved_newval);
2032#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002033 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (errmsg != NULL)
2035 goto skip;
2036 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 char_u *p;
2040
2041 if (nextchar == '&')
2042 {
2043 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002044 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 else
2047 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002048 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002049 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (*p == '\\' && p[1] != NUL)
2051 ++p;
2052 nextchar = *p;
2053 *p = NUL;
2054 add_termcode(key_name, arg, FALSE);
2055 *p = nextchar;
2056 }
2057 if (full_screen)
2058 ttest(FALSE);
2059 redraw_all_later(CLEAR);
2060 }
2061 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002064 did_set_option(
2065 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067
2068skip:
2069 /*
2070 * Advance to next argument.
2071 * - skip until a blank found, taking care of backslashes
2072 * - skip blanks
2073 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2074 */
2075 for (i = 0; i < 2 ; ++i)
2076 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002077 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (*arg++ == '\\' && *arg != NUL)
2079 ++arg;
2080 arg = skipwhite(arg);
2081 if (*arg != '=')
2082 break;
2083 }
2084 }
2085
2086 if (errmsg != NULL)
2087 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002088 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002089 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (i + (arg - startarg) < IOSIZE)
2091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 STRCAT(IObuff, ": ");
2094 mch_memmove(IObuff + i, startarg, (arg - startarg));
2095 IObuff[i + (arg - startarg)] = NUL;
2096 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002097 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 trans_characters(IObuff, IOSIZE);
2099
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 ++no_wait_return; // wait_return done later
2101 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 --no_wait_return;
2103
2104 return FAIL;
2105 }
2106
2107 arg = skipwhite(arg);
2108 }
2109
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110theend:
2111 if (silent_mode && did_show)
2112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002113 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002114 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002115 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002116 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002117 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002118 out_flush();
2119 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002120 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002121 }
2122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 return OK;
2124}
2125
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002126/*
2127 * Call this when an option has been given a new value through a user command.
2128 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2129 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131did_set_option(
2132 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002133 int opt_flags, // possibly with OPT_MODELINE
2134 int new_value, // value was replaced completely
2135 int value_checked) // value was checked to be safe, no need to set the
2136 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002137{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002138 long_u *p;
2139
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002140 options[opt_idx].flags |= P_WAS_SET;
2141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002142 // When an option is set in the sandbox, from a modeline or in secure mode
2143 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2144 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002145 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002146 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002147#ifdef HAVE_SANDBOX
2148 || sandbox != 0
2149#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002150 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002151 *p = *p | P_INSECURE;
2152 else if (new_value)
2153 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002154}
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156/*
2157 * Convert a key name or string into a key value.
2158 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002159 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002161 int
2162string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
2164 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002165 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (*arg == '^')
2167 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002168 if (multi_byte)
2169 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 return *arg;
2171}
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#ifdef FEAT_TITLE
2174/*
2175 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2176 * maketitle() to create and display it.
2177 * When switching the title or icon off, call mch_restore_title() to get
2178 * the old value back.
2179 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002180 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002181did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 if (starting != NO_SCREEN
2184#ifdef FEAT_GUI
2185 && !gui.starting
2186#endif
2187 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189}
2190#endif
2191
2192/*
2193 * set_options_bin - called when 'bin' changes value.
2194 */
2195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002196set_options_bin(
2197 int oldval,
2198 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 /*
2202 * The option values that are changed when 'bin' changes are
2203 * copied when 'bin is set and restored when 'bin' is reset.
2204 */
2205 if (newval)
2206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
2209 if (!(opt_flags & OPT_GLOBAL))
2210 {
2211 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2212 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2213 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2214 curbuf->b_p_et_nobin = curbuf->b_p_et;
2215 }
2216 if (!(opt_flags & OPT_LOCAL))
2217 {
2218 p_tw_nobin = p_tw;
2219 p_wm_nobin = p_wm;
2220 p_ml_nobin = p_ml;
2221 p_et_nobin = p_et;
2222 }
2223 }
2224
2225 if (!(opt_flags & OPT_GLOBAL))
2226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002227 curbuf->b_p_tw = 0; // no automatic line wrap
2228 curbuf->b_p_wm = 0; // no automatic line wrap
2229 curbuf->b_p_ml = 0; // no modelines
2230 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232 if (!(opt_flags & OPT_LOCAL))
2233 {
2234 p_tw = 0;
2235 p_wm = 0;
2236 p_ml = FALSE;
2237 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
2240 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 if (!(opt_flags & OPT_GLOBAL))
2244 {
2245 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2246 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2247 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2248 curbuf->b_p_et = curbuf->b_p_et_nobin;
2249 }
2250 if (!(opt_flags & OPT_LOCAL))
2251 {
2252 p_tw = p_tw_nobin;
2253 p_wm = p_wm_nobin;
2254 p_ml = p_ml_nobin;
2255 p_et = p_et_nobin;
2256 }
2257 }
2258}
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260/*
2261 * Expand environment variables for some string options.
2262 * These string options cannot be indirect!
2263 * If "val" is NULL expand the current value of the option.
2264 * Return pointer to NameBuff, or NULL when not expanded.
2265 */
2266 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002267option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2271 return NULL;
2272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 // If val is longer than MAXPATHL no meaningful expansion can be done,
2274 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (val != NULL && STRLEN(val) > MAXPATHL)
2276 return NULL;
2277
2278 if (val == NULL)
2279 val = *(char_u **)options[opt_idx].var;
2280
2281 /*
2282 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2283 * Escape spaces when expanding 'tags', they are used to separate file
2284 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002285 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 */
2287 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002288 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002289#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002290 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2291#endif
2292 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return NULL;
2295
2296 return NameBuff;
2297}
2298
2299/*
2300 * After setting various option values: recompute variables that depend on
2301 * option values.
2302 */
2303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 (void)init_chartab();
2308
Bram Moolenaardac13472019-09-16 21:06:21 +02002309 didset_string_options();
2310
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002311#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002312 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002313 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002314 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002315 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002318 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 (void)check_cedit();
2320#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002321#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002323 fill_breakat_flags();
2324#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002325 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002326}
2327
2328/*
2329 * More side effects of setting options.
2330 */
2331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002332didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002333{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002334 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002335 (void)highlight_changed();
2336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002338 check_opt_wim();
2339
Bram Moolenaareed9d462021-02-15 20:38:25 +01002340 // Parse default for 'listchars'.
2341 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002344 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002345
2346#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002348 (void)check_clipboard_option();
2349#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002350#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002351 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002352 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002353 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002354 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356}
2357
2358/*
2359 * Check for string options that are NULL (normally only termcap options).
2360 */
2361 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002362check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 int opt_idx;
2365
2366 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2367 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2368 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2369}
2370
2371/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002372 * Return the option index found by a pointer into term_strings[].
2373 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002375 int
2376get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002378 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2381 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002382 return opt_idx;
2383 return -1; // cannot happen: didn't find it!
2384}
2385
2386/*
2387 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2388 * Return the option index or -1 if not found.
2389 */
2390 int
2391set_term_option_alloced(char_u **p)
2392{
2393 int opt_idx = get_term_opt_idx(p);
2394
2395 if (opt_idx >= 0)
2396 options[opt_idx].flags |= P_ALLOCED;
2397 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398}
2399
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002400#if defined(FEAT_EVAL) || defined(PROTO)
2401/*
2402 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2403 * Return FALSE when it wasn't.
2404 * Return -1 for an unknown option.
2405 */
2406 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002407was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002408{
2409 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002410 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002411
2412 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002413 {
2414 flagp = insecure_flag(idx, opt_flags);
2415 return (*flagp & P_INSECURE) != 0;
2416 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002417 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002418 return -1;
2419}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002420
2421/*
2422 * Get a pointer to the flags used for the P_INSECURE flag of option
2423 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002424 * NOTE: Caller must make sure that "curwin" is set to the window from which
2425 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002426 */
2427 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002428insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002429{
2430 if (opt_flags & OPT_LOCAL)
2431 switch ((int)options[opt_idx].indir)
2432 {
2433#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002434 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002435#endif
2436#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002437# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002438 case PV_FDE: return &curwin->w_p_fde_flags;
2439 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002440# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002441# ifdef FEAT_BEVAL
2442 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2443# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002444# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002445 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002446# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002447 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002448# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002449 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002450# endif
2451#endif
2452 }
2453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002454 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002455 return &options[opt_idx].flags;
2456}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002457#endif
2458
Bram Moolenaardac13472019-09-16 21:06:21 +02002459#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002460/*
2461 * Redraw the window title and/or tab page text later.
2462 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002463void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002464{
2465 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002466 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002467}
2468#endif
2469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002471 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2472 * characters or characters in "allowed".
2473 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002474 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002475valid_name(char_u *val, char *allowed)
2476{
2477 char_u *s;
2478
2479 for (s = val; *s != NUL; ++s)
2480 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2481 return FALSE;
2482 return TRUE;
2483}
2484
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002485#if defined(FEAT_EVAL) || defined(PROTO)
2486/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002487 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002488 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002489 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002490 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002491set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002492{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002493 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2494 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002495 sctx_T new_script_ctx = script_ctx;
2496
Bram Moolenaar51258742020-05-03 17:19:33 +02002497 // Modeline already has the line number set.
2498 if (!(opt_flags & OPT_MODELINE))
2499 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002501 // Remember where the option was set. For local options need to do that
2502 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002503 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002504 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002505 if (both || (opt_flags & OPT_LOCAL))
2506 {
2507 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002508 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002509 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002510 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002511 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002512}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002513
2514/*
2515 * Set the script_ctx for a termcap option.
2516 * "name" must be the two character code, e.g. "RV".
2517 * When "name" is NULL use "opt_idx".
2518 */
2519 void
2520set_term_option_sctx_idx(char *name, int opt_idx)
2521{
2522 char_u buf[5];
2523 int idx;
2524
2525 if (name == NULL)
2526 idx = opt_idx;
2527 else
2528 {
2529 buf[0] = 't';
2530 buf[1] = '_';
2531 buf[2] = name[0];
2532 buf[3] = name[1];
2533 buf[4] = 0;
2534 idx = findoption(buf);
2535 }
2536 if (idx >= 0)
2537 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2538}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002539#endif
2540
Bram Moolenaarf4140482020-02-15 23:06:45 +01002541#if defined(FEAT_EVAL)
2542/*
2543 * Apply the OptionSet autocommand.
2544 */
2545 static void
2546apply_optionset_autocmd(
2547 int opt_idx,
2548 long opt_flags,
2549 long oldval,
2550 long oldval_g,
2551 long newval,
2552 char *errmsg)
2553{
2554 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2555
2556 // Don't do this while starting up, failure or recursively.
2557 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2558 return;
2559
2560 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2561 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2562 oldval_g);
2563 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2564 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2565 (opt_flags & OPT_LOCAL) ? "local" : "global");
2566 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2567 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2568 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2569 if (opt_flags & OPT_LOCAL)
2570 {
2571 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2572 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2573 }
2574 if (opt_flags & OPT_GLOBAL)
2575 {
2576 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2577 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2578 }
2579 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2580 {
2581 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2582 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2583 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2584 }
2585 if (opt_flags & OPT_MODELINE)
2586 {
2587 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2588 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2589 }
2590 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2591 NULL, FALSE, NULL);
2592 reset_v_option_vars();
2593}
2594#endif
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596/*
2597 * Set the value of a boolean option, and take care of side effects.
2598 * Returns NULL for success, or an error message for an error.
2599 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002600 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002601set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002602 int opt_idx, // index in options[] table
2603 char_u *varp, // pointer to the option variable
2604 int value, // new value
2605 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002608#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002609 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002612 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if ((secure
2614#ifdef HAVE_SANDBOX
2615 || sandbox != 0
2616#endif
2617 ) && (options[opt_idx].flags & P_SECURE))
2618 return e_secure;
2619
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002620#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002621 // Save the global value before changing anything. This is needed as for
2622 // a global-only option setting the "local value" in fact sets the global
2623 // value (since there is only one value).
2624 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2625 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2626 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002627#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002628
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002629 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002631 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002632 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002635#ifdef FEAT_GUI
2636 need_mouse_correct = TRUE;
2637#endif
2638
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2641 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2642
2643 /*
2644 * Handle side effects of changing a bool option.
2645 */
2646
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002647 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaar920694c2016-08-21 17:45:02 +02002651#ifdef FEAT_LANGMAP
2652 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002653 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002654 p_lnr = !p_lrm;
2655 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002656 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002657 p_lrm = !p_lnr;
2658#endif
2659
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002660#ifdef FEAT_SYN_HL
2661 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2662 reset_cursorline();
2663#endif
2664
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002665#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002667 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002669 // Only take action when the option was set. When reset we do not
2670 // delete the undo file, the option may be set again without making
2671 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002672 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002673 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002674 char_u hash[UNDO_HASH_SIZE];
2675 buf_T *save_curbuf = curbuf;
2676
Bram Moolenaar29323592016-07-24 22:04:11 +02002677 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002679 // When 'undofile' is set globally: for every buffer, otherwise
2680 // only for the current buffer: Try to read in the undofile,
2681 // if one exists, the buffer wasn't changed and the buffer was
2682 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002683 if ((curbuf == save_curbuf
2684 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2685 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2686 {
2687 u_compute_hash(hash);
2688 u_read_undo(NULL, hash, curbuf->b_fname);
2689 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002690 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002691 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002692 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002693 }
2694#endif
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 else if ((int *)varp == &curbuf->b_p_ro)
2697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2700 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002701
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002702 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002703 if (curbuf->b_p_ro)
2704 curbuf->b_did_warn = FALSE;
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002707 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#endif
2709 }
2710
Bram Moolenaar9c449722010-07-20 18:44:27 +02002711#ifdef FEAT_GUI
2712 else if ((int *)varp == &p_mh)
2713 {
2714 if (!p_mh)
2715 gui_mch_mousehide(FALSE);
2716 }
2717#endif
2718
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002721 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002722# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002724 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2725 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002726 {
2727 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002728 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002729 }
2730# endif
2731# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002732 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002733# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002734 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002735#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002738 {
2739 redraw_titles();
2740 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002742 else if ((int *)varp == &curbuf->b_p_fixeol)
2743 {
2744 redraw_titles();
2745 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002747 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002748 {
2749 redraw_titles();
2750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 else if ((int *)varp == &curbuf->b_p_bin)
2755 {
2756 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2757#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002758 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#endif
2760 }
2761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002762 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2764 {
2765 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2766 NULL, NULL, TRUE, curbuf);
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else if ((int *)varp == &curbuf->b_p_swf)
2771 {
2772 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2776 // buf->b_p_swf
2777 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else if ((int *)varp == &p_terse)
2782 {
2783 char_u *p;
2784
2785 p = vim_strchr(p_shm, SHM_SEARCH);
2786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002787 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (p_terse && p == NULL)
2789 {
2790 STRCPY(IObuff, p_shm);
2791 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002792 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002794 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002796 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002799 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else if ((int *)varp == &p_paste)
2801 {
2802 paste_option_changed();
2803 }
2804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002805 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else if ((int *)varp == &p_im)
2807 {
2808 if (p_im)
2809 {
2810 if ((State & INSERT) == 0)
2811 need_start_insertmode = TRUE;
2812 stop_insert_mode = FALSE;
2813 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002815 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
2817 need_start_insertmode = FALSE;
2818 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002819 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 restart_edit = 0;
2822 }
2823 }
2824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 else if ((int *)varp == &p_ic && p_hls)
2827 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002828 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830
2831#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else if ((int *)varp == &p_hls)
2834 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002835 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837#endif
2838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2840 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else if ((int *)varp == &curwin->w_p_scb)
2842 {
2843 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002846 curwin->w_scbind_pos = curwin->w_topline;
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaar4033c552017-09-16 20:54:51 +02002850#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else if ((int *)varp == &curwin->w_p_pvw)
2853 {
2854 if (curwin->w_p_pvw)
2855 {
2856 win_T *win;
2857
Bram Moolenaar29323592016-07-24 22:04:11 +02002858 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (win->w_p_pvw && win != curwin)
2860 {
2861 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002862 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864 }
2865 }
2866#endif
2867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 else if ((int *)varp == &curbuf->b_p_tx)
2870 {
2871 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2872 }
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 set_string_option_direct((char_u *)"ffs", -1,
2878 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002879 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
2882 /*
2883 * When 'lisp' option changes include/exclude '-' in
2884 * keyword characters.
2885 */
2886#ifdef FEAT_LISP
2887 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891#endif
2892
2893#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002895 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002897 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899#endif
2900
2901 else if ((int *)varp == &curbuf->b_changed)
2902 {
2903 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002906 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910
2911#ifdef BACKSLASH_IN_FILENAME
2912 else if ((int *)varp == &p_ssl)
2913 {
2914 if (p_ssl)
2915 {
2916 psepc = '/';
2917 psepcN = '\\';
2918 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 else
2921 {
2922 psepc = '\\';
2923 psepcN = '/';
2924 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 buflist_slash_adjust();
2929 alist_slash_adjust();
2930# ifdef FEAT_EVAL
2931 scriptnames_slash_adjust();
2932# endif
2933 }
2934#endif
2935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 else if ((int *)varp == &curwin->w_p_wrap)
2938 {
2939 if (curwin->w_p_wrap)
2940 curwin->w_leftcol = 0;
2941 }
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 else if ((int *)varp == &p_ea)
2944 {
2945 if (p_ea && !old_value)
2946 win_equal(curwin, FALSE, 0);
2947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 else if ((int *)varp == &p_wiv)
2950 {
2951 /*
2952 * When 'weirdinvert' changed, set/reset 't_xs'.
2953 * Then set 'weirdinvert' according to value of 't_xs'.
2954 */
2955 if (p_wiv && !old_value)
2956 T_XS = (char_u *)"y";
2957 else if (!p_wiv && old_value)
2958 T_XS = empty_option;
2959 p_wiv = (*T_XS != NUL);
2960 }
2961
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002962#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 else if ((int *)varp == &p_beval)
2964 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002965 if (!balloonEvalForTerm)
2966 {
2967 if (p_beval && !old_value)
2968 gui_mch_enable_beval_area(balloonEval);
2969 else if (!p_beval && old_value)
2970 gui_mch_disable_beval_area(balloonEval);
2971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002973#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002974#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002975 else if ((int *)varp == &p_bevalterm)
2976 {
2977 mch_bevalterm_changed();
2978 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002981#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 else if ((int *)varp == &p_acd)
2983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002985 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 }
2987#endif
2988
2989#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else if ((int *)varp == &curwin->w_p_diff)
2992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002994 diff_buf_adjust(curwin);
2995# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (foldmethodIsDiff(curwin))
2997 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
3000#endif
3001
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003002#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 else if ((int *)varp == &p_imdisable)
3005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (p_imdisable)
3008 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003009 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 // When the option is set from an autocommand, it may need to take
3011 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003012 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014#endif
3015
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003016#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003018 else if ((int *)varp == &curwin->w_p_spell)
3019 {
3020 if (curwin->w_p_spell)
3021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 char *errmsg = did_set_spelllang(curwin);
3023
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003024 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003026 }
3027 }
3028#endif
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#ifdef FEAT_ARABIC
3031 if ((int *)varp == &curwin->w_p_arab)
3032 {
3033 if (curwin->w_p_arab)
3034 {
3035 /*
3036 * 'arabic' is set, handle various sub-settings.
3037 */
3038 if (!p_tbidi)
3039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (!curwin->w_p_rl)
3042 {
3043 curwin->w_p_rl = TRUE;
3044 changed_window_setting();
3045 }
3046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (!p_arshape)
3049 {
3050 p_arshape = TRUE;
3051 redraw_later_clear();
3052 }
3053 }
3054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // Arabic requires a utf-8 encoding, inform the user if its not
3056 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003058 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003059 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3060
Bram Moolenaar8820b482017-03-16 17:23:31 +01003061 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003062 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003063#ifdef FEAT_EVAL
3064 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3065#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003072 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3074 OPT_LOCAL);
3075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077 else
3078 {
3079 /*
3080 * 'arabic' is reset, handle various sub-settings.
3081 */
3082 if (!p_tbidi)
3083 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (curwin->w_p_rl)
3086 {
3087 curwin->w_p_rl = FALSE;
3088 changed_window_setting();
3089 }
3090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // 'arabicshape' isn't reset, it is a global option and
3092 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // 'delcombine' isn't reset, it is a global option and another
3096 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 curbuf->b_p_iminsert = B_IMODE_NONE;
3101 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3102# endif
3103 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003104 }
3105
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003106#endif
3107
Bram Moolenaar44826212019-08-22 21:23:20 +02003108#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3109 else if (((int *)varp == &curwin->w_p_nu
3110 || (int *)varp == &curwin->w_p_rnu)
3111 && gui.in_use
3112 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3113 && curbuf->b_signlist != NULL)
3114 {
3115 // If the 'number' or 'relativenumber' options are modified and
3116 // 'signcolumn' is set to 'number', then clear the screen for a full
3117 // refresh. Otherwise the sign icons are not displayed properly in the
3118 // number column. If the 'number' option is set and only the
3119 // 'relativenumber' option is toggled, then don't refresh the screen
3120 // (optimization).
3121 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3122 redraw_all_later(CLEAR);
3123 }
3124#endif
3125
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003126#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003128 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003129 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003130# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003131 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003132 if (
3133# ifdef VIMDLL
3134 !gui.in_use && !gui.starting &&
3135# endif
3136 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003137 {
3138 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003139 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003140 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003141 if (is_term_win32())
3142 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003143# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003144# ifdef FEAT_GUI
3145 if (!gui.in_use && !gui.starting)
3146# endif
3147 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003148# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003150 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003151 {
3152 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003153 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003154 init_highlight(TRUE, FALSE);
3155 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003156# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003157 }
3158#endif
3159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 /*
3161 * End of handling side effects for bool options.
3162 */
3163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003164 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 options[opt_idx].flags |= P_WAS_SET;
3167
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003168#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003169 apply_optionset_autocmd(opt_idx, opt_flags,
3170 (long)(old_value ? TRUE : FALSE),
3171 (long)(old_global_value ? TRUE : FALSE),
3172 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003173#endif
3174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003176 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003177 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003178 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003179
3180 if ((opt_flags & OPT_NO_REDRAW) == 0)
3181 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 return NULL;
3184}
3185
3186/*
3187 * Set the value of a number option, and take care of side effects.
3188 * Returns NULL for success, or an error message for an error.
3189 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003190 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003191set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 int opt_idx, // index in options[] table
3193 char_u *varp, // pointer to the option variable
3194 long value, // new value
3195 char *errbuf, // buffer for error messages
3196 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003197 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3198 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003200 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003202#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003203 long old_global_value = 0; // only used when setting a local and
3204 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003205#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003206 long old_Rows = Rows; // remember old Rows
3207 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 long *pp = (long *)varp;
3209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003210 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003211 if ((secure
3212#ifdef HAVE_SANDBOX
3213 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003215 ) && (options[opt_idx].flags & P_SECURE))
3216 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003218#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003219 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003220 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003221 // value (since there is only one value).
3222 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003223 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3224 OPT_GLOBAL);
3225#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 *pp = value;
3228#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003230 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003232#ifdef FEAT_GUI
3233 need_mouse_correct = TRUE;
3234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
Bram Moolenaar14f24742012-08-08 18:01:05 +02003236 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003239#ifdef FEAT_VARTABS
3240 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3241 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3242 ? tabstop_first(curbuf->b_p_vts_array)
3243 : curbuf->b_p_ts;
3244#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
3248
3249 /*
3250 * Number options that need some action when changed
3251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (pp == &p_wh || pp == &p_hh)
3253 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003254 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (p_wh < 1)
3256 {
3257 errmsg = e_positive;
3258 p_wh = 1;
3259 }
3260 if (p_wmh > p_wh)
3261 {
3262 errmsg = e_winheight;
3263 p_wh = p_wmh;
3264 }
3265 if (p_hh < 0)
3266 {
3267 errmsg = e_positive;
3268 p_hh = 0;
3269 }
3270
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003271 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003272 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 if (pp == &p_wh && curwin->w_height < p_wh)
3275 win_setheight((int)p_wh);
3276 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3277 win_setheight((int)p_hh);
3278 }
3279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 else if (pp == &p_wmh)
3281 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003282 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (p_wmh < 0)
3284 {
3285 errmsg = e_positive;
3286 p_wmh = 0;
3287 }
3288 if (p_wmh > p_wh)
3289 {
3290 errmsg = e_winheight;
3291 p_wmh = p_wh;
3292 }
3293 win_setminheight();
3294 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003295 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003297 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (p_wiw < 1)
3299 {
3300 errmsg = e_positive;
3301 p_wiw = 1;
3302 }
3303 if (p_wmw > p_wiw)
3304 {
3305 errmsg = e_winwidth;
3306 p_wiw = p_wmw;
3307 }
3308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003309 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003310 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 win_setwidth((int)p_wiw);
3312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else if (pp == &p_wmw)
3314 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003315 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (p_wmw < 0)
3317 {
3318 errmsg = e_positive;
3319 p_wmw = 0;
3320 }
3321 if (p_wmw > p_wiw)
3322 {
3323 errmsg = e_winwidth;
3324 p_wmw = p_wiw;
3325 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003326 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003329 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 else if (pp == &p_ls)
3331 {
3332 last_status(FALSE);
3333 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003335 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003336 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
3341#ifdef FEAT_GUI
3342 else if (pp == &p_linespace)
3343 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003344 // Recompute gui.char_height and resize the Vim window to keep the
3345 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003346 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003347 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
3349#endif
3350
3351#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003352 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 else if (pp == &curwin->w_p_fdl)
3354 {
3355 if (curwin->w_p_fdl < 0)
3356 curwin->w_p_fdl = 0;
3357 newFoldLevel();
3358 }
3359
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003360 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 else if (pp == &curwin->w_p_fml)
3362 {
3363 foldUpdateAll(curwin);
3364 }
3365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003366 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else if (pp == &curwin->w_p_fdn)
3368 {
3369 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3370 foldUpdateAll(curwin);
3371 }
3372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003373 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else if (pp == &curwin->w_p_fdc)
3375 {
3376 if (curwin->w_p_fdc < 0)
3377 {
3378 errmsg = e_positive;
3379 curwin->w_p_fdc = 0;
3380 }
3381 else if (curwin->w_p_fdc > 12)
3382 {
3383 errmsg = e_invarg;
3384 curwin->w_p_fdc = 12;
3385 }
3386 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003387#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003389#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003390 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3392 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003393# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (foldmethodIsIndent(curwin))
3395 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003396# endif
3397# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003398 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3399 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003400 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3401 parse_cino(curbuf);
3402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003406 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003407 else if (pp == &p_mco)
3408 {
3409 if (p_mco > MAX_MCO)
3410 p_mco = MAX_MCO;
3411 else if (p_mco < 0)
3412 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003413 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003414 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 else if (pp == &curbuf->b_p_iminsert)
3417 {
3418 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3419 {
3420 errmsg = e_invarg;
3421 curbuf->b_p_iminsert = B_IMODE_NONE;
3422 }
3423 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003424 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003426#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 status_redraw_curbuf();
3429#endif
3430 }
3431
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003432#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003433 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003434 else if (pp == &p_imst)
3435 {
3436 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3437 errmsg = e_invarg;
3438 }
3439#endif
3440
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003441 else if (pp == &p_window)
3442 {
3443 if (p_window < 1)
3444 p_window = 1;
3445 else if (p_window >= Rows)
3446 p_window = Rows - 1;
3447 }
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 else if (pp == &curbuf->b_p_imsearch)
3450 {
3451 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3452 {
3453 errmsg = e_invarg;
3454 curbuf->b_p_imsearch = B_IMODE_NONE;
3455 }
3456 p_imsearch = curbuf->b_p_imsearch;
3457 }
3458
3459#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else if (pp == &p_titlelen)
3462 {
3463 if (p_titlelen < 0)
3464 {
3465 errmsg = e_positive;
3466 p_titlelen = 85;
3467 }
3468 if (starting != NO_SCREEN && old_value != p_titlelen)
3469 need_maketitle = TRUE;
3470 }
3471#endif
3472
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003473 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 else if (pp == &p_ch)
3475 {
3476 if (p_ch < 1)
3477 {
3478 errmsg = e_positive;
3479 p_ch = 1;
3480 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003481 if (p_ch > Rows - min_rows() + 1)
3482 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003484 // Only compute the new window layout when startup has been
3485 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (p_ch != old_value && full_screen
3487#ifdef FEAT_GUI
3488 && !gui.starting
3489#endif
3490 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003491 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else if (pp == &p_uc)
3496 {
3497 if (p_uc < 0)
3498 {
3499 errmsg = e_positive;
3500 p_uc = 100;
3501 }
3502 if (p_uc && !old_value)
3503 ml_open_files();
3504 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003505#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003506 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003507 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003508 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003509 {
3510 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003511 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003512 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003513 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003514 {
3515 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003516 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003517 }
3518 }
3519#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003520#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003521 else if (pp == &p_mzq)
3522 mzvim_reset_timer();
3523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003525#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003526 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003527 else if (pp == &p_pyx)
3528 {
3529 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3530 errmsg = e_invarg;
3531 }
3532#endif
3533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003534 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 else if (pp == &p_ul)
3536 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003537 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003539 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 p_ul = value;
3541 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003542 else if (pp == &curbuf->b_p_ul)
3543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003544 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003545 curbuf->b_p_ul = old_value;
3546 u_sync(TRUE);
3547 curbuf->b_p_ul = value;
3548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003550#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003551 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003552 else if (pp == &curwin->w_p_nuw)
3553 {
3554 if (curwin->w_p_nuw < 1)
3555 {
3556 errmsg = e_positive;
3557 curwin->w_p_nuw = 1;
3558 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003559 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003560 {
3561 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003562 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003563 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003564 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003565 }
3566#endif
3567
Bram Moolenaar1a384422010-07-14 19:53:30 +02003568 else if (pp == &curbuf->b_p_tw)
3569 {
3570 if (curbuf->b_p_tw < 0)
3571 {
3572 errmsg = e_positive;
3573 curbuf->b_p_tw = 0;
3574 }
3575#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003576 {
3577 win_T *wp;
3578 tabpage_T *tp;
3579
3580 FOR_ALL_TAB_WINDOWS(tp, wp)
3581 check_colorcolumn(wp);
3582 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003583#endif
3584 }
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 /*
3587 * Check the bounds for numeric options here
3588 */
3589 if (Rows < min_rows() && full_screen)
3590 {
3591 if (errbuf != NULL)
3592 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003593 vim_snprintf((char *)errbuf, errbuflen,
3594 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 errmsg = errbuf;
3596 }
3597 Rows = min_rows();
3598 }
3599 if (Columns < MIN_COLUMNS && full_screen)
3600 {
3601 if (errbuf != NULL)
3602 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003603 vim_snprintf((char *)errbuf, errbuflen,
3604 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 errmsg = errbuf;
3606 }
3607 Columns = MIN_COLUMNS;
3608 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003609 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 /*
3612 * If the screen (shell) height has been changed, assume it is the
3613 * physical screenheight.
3614 */
3615 if (old_Rows != Rows || old_Columns != Columns)
3616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003617 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (updating_screen)
3619 *pp = old_value;
3620 else if (full_screen
3621#ifdef FEAT_GUI
3622 && !gui.starting
3623#endif
3624 )
3625 set_shellsize((int)Columns, (int)Rows, TRUE);
3626 else
3627 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003628 // Postpone the resizing; check the size and cmdline position for
3629 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 check_shellsize();
3631 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3632 cmdline_row = Rows - p_ch;
3633 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003634 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003635 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if (curbuf->b_p_ts <= 0)
3639 {
3640 errmsg = e_positive;
3641 curbuf->b_p_ts = 8;
3642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (p_tm < 0)
3644 {
3645 errmsg = e_positive;
3646 p_tm = 0;
3647 }
3648 if ((curwin->w_p_scr <= 0
3649 || (curwin->w_p_scr > curwin->w_height
3650 && curwin->w_height > 0))
3651 && full_screen)
3652 {
3653 if (pp == &(curwin->w_p_scr))
3654 {
3655 if (curwin->w_p_scr != 0)
3656 errmsg = e_scroll;
3657 win_comp_scroll(curwin);
3658 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003659 // If 'scroll' became invalid because of a side effect silently adjust
3660 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 else if (curwin->w_p_scr <= 0)
3662 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003663 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 curwin->w_p_scr = curwin->w_height;
3665 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003666 if (p_hi < 0)
3667 {
3668 errmsg = e_positive;
3669 p_hi = 0;
3670 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003671 else if (p_hi > 10000)
3672 {
3673 errmsg = e_invarg;
3674 p_hi = 10000;
3675 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003676 if (p_re < 0 || p_re > 2)
3677 {
3678 errmsg = e_invarg;
3679 p_re = 0;
3680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (p_report < 0)
3682 {
3683 errmsg = e_positive;
3684 p_report = 1;
3685 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003686 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003688 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 p_sj = Rows / 2;
3690 else
3691 {
3692 errmsg = e_scroll;
3693 p_sj = 1;
3694 }
3695 }
3696 if (p_so < 0 && full_screen)
3697 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003698 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 p_so = 0;
3700 }
3701 if (p_siso < 0 && full_screen)
3702 {
3703 errmsg = e_positive;
3704 p_siso = 0;
3705 }
3706#ifdef FEAT_CMDWIN
3707 if (p_cwh < 1)
3708 {
3709 errmsg = e_positive;
3710 p_cwh = 1;
3711 }
3712#endif
3713 if (p_ut < 0)
3714 {
3715 errmsg = e_positive;
3716 p_ut = 2000;
3717 }
3718 if (p_ss < 0)
3719 {
3720 errmsg = e_positive;
3721 p_ss = 0;
3722 }
3723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003724 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3726 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3727
3728 options[opt_idx].flags |= P_WAS_SET;
3729
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003730#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003731 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3732 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003733#endif
3734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003735 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003736 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003737 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003738 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003739 if ((opt_flags & OPT_NO_REDRAW) == 0)
3740 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 return errmsg;
3743}
3744
3745/*
3746 * Called after an option changed: check if something needs to be redrawn.
3747 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003749check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003751 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003752 int doclear = (flags & P_RCLR) == P_RCLR;
3753 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003755 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3759 changed_window_setting();
3760 if (flags & P_RBUF)
3761 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003762 if (flags & P_RWINONLY)
3763 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003764 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 redraw_all_later(CLEAR);
3766 else if (all)
3767 redraw_all_later(NOT_VALID);
3768}
3769
3770/*
3771 * Find index for option 'arg'.
3772 * Return -1 if not found.
3773 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003775findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
3777 int opt_idx;
3778 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003779 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 int is_term_opt;
3781
3782 /*
3783 * For first call: Initialize the quick-access table.
3784 * It contains the index for the first option that starts with a certain
3785 * letter. There are 26 letters, plus the first "t_" option.
3786 */
3787 if (quick_tab[1] == 0)
3788 {
3789 p = options[0].fullname;
3790 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3791 {
3792 if (s[0] != p[0])
3793 {
3794 if (s[0] == 't' && s[1] == '_')
3795 quick_tab[26] = opt_idx;
3796 else
3797 quick_tab[CharOrdLow(s[0])] = opt_idx;
3798 }
3799 p = s;
3800 }
3801 }
3802
3803 /*
3804 * Check for name starting with an illegal character.
3805 */
3806#ifdef EBCDIC
3807 if (!islower(arg[0]))
3808#else
3809 if (arg[0] < 'a' || arg[0] > 'z')
3810#endif
3811 return -1;
3812
3813 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3814 if (is_term_opt)
3815 opt_idx = quick_tab[26];
3816 else
3817 opt_idx = quick_tab[CharOrdLow(arg[0])];
3818 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3819 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003820 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 break;
3822 }
3823 if (s == NULL && !is_term_opt)
3824 {
3825 opt_idx = quick_tab[CharOrdLow(arg[0])];
3826 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3827 {
3828 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003829 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 break;
3831 s = NULL;
3832 }
3833 }
3834 if (s == NULL)
3835 opt_idx = -1;
3836 return opt_idx;
3837}
3838
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003839#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840/*
3841 * Get the value for an option.
3842 *
3843 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003844 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003845 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003846 * String option: gov_string, *stringval gets allocated string.
3847 * Hidden Number option: gov_hidden_number.
3848 * Hidden Toggle option: gov_hidden_bool.
3849 * Hidden String option: gov_hidden_string.
3850 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003852 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003853get_option_value(
3854 char_u *name,
3855 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003856 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003857 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
3859 int opt_idx;
3860 char_u *varp;
3861
3862 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003863 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003864 {
3865 int key;
3866
3867 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003868 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003869 {
3870 char_u key_name[2];
3871 char_u *p;
3872
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003873 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003874 if (key < 0)
3875 {
3876 key_name[0] = KEY2TERMCAP0(key);
3877 key_name[1] = KEY2TERMCAP1(key);
3878 }
3879 else
3880 {
3881 key_name[0] = KS_KEY;
3882 key_name[1] = (key & 0xff);
3883 }
3884 p = find_termcode(key_name);
3885 if (p != NULL)
3886 {
3887 if (stringval != NULL)
3888 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003889 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003890 }
3891 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003892 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894
3895 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3896
3897 if (options[opt_idx].flags & P_STRING)
3898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003899 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003900 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (stringval != NULL)
3902 {
3903#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003904 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003905 if ((char_u **)varp == &curbuf->b_p_key
3906 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 *stringval = vim_strsave((char_u *)"*****");
3908 else
3909#endif
3910 *stringval = vim_strsave(*(char_u **)(varp));
3911 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003912 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003915 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003916 return (options[opt_idx].flags & P_NUM)
3917 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (options[opt_idx].flags & P_NUM)
3919 *numval = *(long *)varp;
3920 else
3921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 // Special case: 'modified' is b_changed, but we also want to consider
3923 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if ((int *)varp == &curbuf->b_changed)
3925 *numval = curbufIsChanged();
3926 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003927 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003929 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930}
3931#endif
3932
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003933#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003934/*
3935 * Returns the option attributes and its value. Unlike the above function it
3936 * will return either global value or local value of the option depending on
3937 * what was requested, but it will never return global value if it was
3938 * requested to return local one and vice versa. Neither it will return
3939 * buffer-local value if it was requested to return window-local one.
3940 *
3941 * Pretends that option is absent if it is not present in the requested scope
3942 * (i.e. has no global, window-local or buffer-local value depending on
3943 * opt_type). Uses
3944 *
3945 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003946 * 0 hidden or unknown option, also option that does not have requested
3947 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003948 * see SOPT_* in vim.h for other flags
3949 *
3950 * Possible opt_type values: see SREQ_* in vim.h
3951 */
3952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003953get_option_value_strict(
3954 char_u *name,
3955 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003956 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003957 int opt_type,
3958 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003959{
3960 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003961 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003962 struct vimoption *p;
3963 int r = 0;
3964
3965 opt_idx = findoption(name);
3966 if (opt_idx < 0)
3967 return 0;
3968
3969 p = &(options[opt_idx]);
3970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003971 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003972 if (p->var == NULL)
3973 return 0;
3974
3975 if (p->flags & P_BOOL)
3976 r |= SOPT_BOOL;
3977 else if (p->flags & P_NUM)
3978 r |= SOPT_NUM;
3979 else if (p->flags & P_STRING)
3980 r |= SOPT_STRING;
3981
3982 if (p->indir == PV_NONE)
3983 {
3984 if (opt_type == SREQ_GLOBAL)
3985 r |= SOPT_GLOBAL;
3986 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003987 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003988 }
3989 else
3990 {
3991 if (p->indir & PV_BOTH)
3992 r |= SOPT_GLOBAL;
3993 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003994 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003995
3996 if (p->indir & PV_WIN)
3997 {
3998 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003999 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004000 else
4001 r |= SOPT_WIN;
4002 }
4003 else if (p->indir & PV_BUF)
4004 {
4005 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004006 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004007 else
4008 r |= SOPT_BUF;
4009 }
4010 }
4011
4012 if (stringval == NULL)
4013 return r;
4014
4015 if (opt_type == SREQ_GLOBAL)
4016 varp = p->var;
4017 else
4018 {
4019 if (opt_type == SREQ_BUF)
4020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004021 // Special case: 'modified' is b_changed, but we also want to
4022 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004023 if (p->indir == PV_MOD)
4024 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004025 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004026 varp = NULL;
4027 }
4028#ifdef FEAT_CRYPT
4029 else if (p->indir == PV_KEY)
4030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004031 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004032 *stringval = NULL;
4033 varp = NULL;
4034 }
4035#endif
4036 else
4037 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004038 buf_T *save_curbuf = curbuf;
4039
4040 // only getting a pointer, no need to use aucmd_prepbuf()
4041 curbuf = (buf_T *)from;
4042 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004043 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004044 curbuf = save_curbuf;
4045 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004046 }
4047 }
4048 else if (opt_type == SREQ_WIN)
4049 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004050 win_T *save_curwin = curwin;
4051
4052 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004053 curbuf = curwin->w_buffer;
4054 varp = get_varp(p);
4055 curwin = save_curwin;
4056 curbuf = curwin->w_buffer;
4057 }
4058 if (varp == p->var)
4059 return (r | SOPT_UNSET);
4060 }
4061
4062 if (varp != NULL)
4063 {
4064 if (p->flags & P_STRING)
4065 *stringval = vim_strsave(*(char_u **)(varp));
4066 else if (p->flags & P_NUM)
4067 *numval = *(long *) varp;
4068 else
4069 *numval = *(int *)varp;
4070 }
4071
4072 return r;
4073}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004074
4075/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004076 * Iterate over options. First argument is a pointer to a pointer to a
4077 * structure inside options[] array, second is option type like in the above
4078 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004079 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004080 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004081 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004082 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004083 * first argument to NULL.
4084 *
4085 * Returns full option name for current option on each call.
4086 */
4087 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004088option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004089{
4090 struct vimoption *ret = NULL;
4091 do
4092 {
4093 if (*option == NULL)
4094 *option = (void *) options;
4095 else if (((struct vimoption *) (*option))->fullname == NULL)
4096 {
4097 *option = NULL;
4098 return NULL;
4099 }
4100 else
4101 *option = (void *) (((struct vimoption *) (*option)) + 1);
4102
4103 ret = ((struct vimoption *) (*option));
4104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004106 if (ret->var == NULL)
4107 {
4108 ret = NULL;
4109 continue;
4110 }
4111
4112 switch (opt_type)
4113 {
4114 case SREQ_GLOBAL:
4115 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4116 ret = NULL;
4117 break;
4118 case SREQ_BUF:
4119 if (!(ret->indir & PV_BUF))
4120 ret = NULL;
4121 break;
4122 case SREQ_WIN:
4123 if (!(ret->indir & PV_WIN))
4124 ret = NULL;
4125 break;
4126 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004127 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004128 return NULL;
4129 }
4130 }
4131 while (ret == NULL);
4132
4133 return (char_u *)ret->fullname;
4134}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004135#endif
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004138 * Return the flags for the option at 'opt_idx'.
4139 */
4140 long_u
4141get_option_flags(int opt_idx)
4142{
4143 return options[opt_idx].flags;
4144}
4145
4146/*
4147 * Set a flag for the option at 'opt_idx'.
4148 */
4149 void
4150set_option_flag(int opt_idx, long_u flag)
4151{
4152 options[opt_idx].flags |= flag;
4153}
4154
4155/*
4156 * Clear a flag for the option at 'opt_idx'.
4157 */
4158 void
4159clear_option_flag(int opt_idx, long_u flag)
4160{
4161 options[opt_idx].flags &= ~flag;
4162}
4163
4164/*
4165 * Returns TRUE if the option at 'opt_idx' is a global option
4166 */
4167 int
4168is_global_option(int opt_idx)
4169{
4170 return options[opt_idx].indir == PV_NONE;
4171}
4172
4173/*
4174 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4175 * local value.
4176 */
4177 int
4178is_global_local_option(int opt_idx)
4179{
4180 return options[opt_idx].indir & PV_BOTH;
4181}
4182
4183/*
4184 * Returns TRUE if the option at 'opt_idx' is a window-local option
4185 */
4186 int
4187is_window_local_option(int opt_idx)
4188{
4189 return options[opt_idx].var == VAR_WIN;
4190}
4191
4192/*
4193 * Returns TRUE if the option at 'opt_idx' is a hidden option
4194 */
4195 int
4196is_hidden_option(int opt_idx)
4197{
4198 return options[opt_idx].var == NULL;
4199}
4200
4201#if defined(FEAT_CRYPT) || defined(PROTO)
4202/*
4203 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4204 */
4205 int
4206is_crypt_key_option(int opt_idx)
4207{
4208 return options[opt_idx].indir == PV_KEY;
4209}
4210#endif
4211
4212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * Set the value of option "name".
4214 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004215 *
4216 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004218 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004219set_option_value(
4220 char_u *name,
4221 long number,
4222 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004223 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
4225 int opt_idx;
4226 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004227 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
4229 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004230 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004231 {
4232 int key;
4233
4234 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004235 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004236 {
4237 char_u key_name[2];
4238
4239 if (key < 0)
4240 {
4241 key_name[0] = KEY2TERMCAP0(key);
4242 key_name[1] = KEY2TERMCAP1(key);
4243 }
4244 else
4245 {
4246 key_name[0] = KS_KEY;
4247 key_name[1] = (key & 0xff);
4248 }
4249 add_termcode(key_name, string, FALSE);
4250 if (full_screen)
4251 ttest(FALSE);
4252 redraw_all_later(CLEAR);
4253 return NULL;
4254 }
4255
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004256 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 else
4259 {
4260 flags = options[opt_idx].flags;
4261#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004262 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004265 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004266 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004269 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004270 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 else
4272 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004273 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004274 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004276 if (number == 0 && string != NULL)
4277 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004278 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004280 // Either we are given a string or we are setting option
4281 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004282 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004283 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004284 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004285 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004286 // There's another character after zeros or the string
4287 // is empty. In both cases, we are trying to set a
4288 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004289 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004290 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004291 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004292
4293 }
4294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004296 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004297 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004299 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004300 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004304 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305}
4306
4307/*
4308 * Get the terminal code for a terminal option.
4309 * Returns NULL when not found.
4310 */
4311 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004312get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
4314 int opt_idx;
4315 char_u *varp;
4316
4317 if (tname[0] != 't' || tname[1] != '_' ||
4318 tname[2] == NUL || tname[3] == NUL)
4319 return NULL;
4320 if ((opt_idx = findoption(tname)) >= 0)
4321 {
4322 varp = get_varp(&(options[opt_idx]));
4323 if (varp != NULL)
4324 varp = *(char_u **)(varp);
4325 return varp;
4326 }
4327 return find_termcode(tname + 2);
4328}
4329
4330 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004331get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
4333 int i;
4334
4335 i = findoption((char_u *)"hl");
4336 if (i >= 0)
4337 return options[i].def_val[VI_DEFAULT];
4338 return (char_u *)NULL;
4339}
4340
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004341 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004342get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004343{
4344 int i;
4345
4346 i = findoption((char_u *)"enc");
4347 if (i >= 0)
4348 return options[i].def_val[VI_DEFAULT];
4349 return (char_u *)NULL;
4350}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352/*
4353 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004354 * When "has_lt" is true there is a '<' before "*arg_arg".
4355 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
4357 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004358find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004360 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004362 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 /*
4365 * Don't use get_special_key_code() for t_xx, we don't want it to call
4366 * add_termcap_entry().
4367 */
4368 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4369 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004370 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004372 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004374 key = find_special_key(&arg, &modifiers,
4375 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004376 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 key = 0;
4378 }
4379 return key;
4380}
4381
4382/*
4383 * if 'all' == 0: show changed options
4384 * if 'all' == 1: show all normal options
4385 * if 'all' == 2: show all terminal options
4386 */
4387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004388showoptions(
4389 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004390 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
4392 struct vimoption *p;
4393 int col;
4394 int isterm;
4395 char_u *varp;
4396 struct vimoption **items;
4397 int item_count;
4398 int run;
4399 int row, rows;
4400 int cols;
4401 int i;
4402 int len;
4403
4404#define INC 20
4405#define GAP 3
4406
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004407 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (items == NULL)
4409 return;
4410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004411 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004413 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004415 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004417 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004419 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004422 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 * 1. display the short items
4424 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004425 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 */
4427 for (run = 1; run <= 2 && !got_int; ++run)
4428 {
4429 /*
4430 * collect the items in items[]
4431 */
4432 item_count = 0;
4433 for (p = &options[0]; p->fullname != NULL; p++)
4434 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004435 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004436 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004437 continue;
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 varp = NULL;
4440 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004441 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 if (p->indir != PV_NONE && !isterm)
4444 varp = get_varp_scope(p, opt_flags);
4445 }
4446 else
4447 varp = get_varp(p);
4448 if (varp != NULL
4449 && ((all == 2 && isterm)
4450 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004451 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004453 if (opt_flags & OPT_ONECOLUMN)
4454 len = Columns;
4455 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004456 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 else
4458 {
4459 option_value2string(p, opt_flags);
4460 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4461 }
4462 if ((len <= INC - GAP && run == 1) ||
4463 (len > INC - GAP && run == 2))
4464 items[item_count++] = p;
4465 }
4466 }
4467
4468 /*
4469 * display the items
4470 */
4471 if (run == 1)
4472 {
4473 cols = (Columns + GAP - 3) / INC;
4474 if (cols == 0)
4475 cols = 1;
4476 rows = (item_count + cols - 1) / cols;
4477 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004478 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 rows = item_count;
4480 for (row = 0; row < rows && !got_int; ++row)
4481 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004482 msg_putchar('\n'); // go to next line
4483 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485 col = 0;
4486 for (i = row; i < item_count; i += rows)
4487 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004488 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 showoneopt(items[i], opt_flags);
4490 col += INC;
4491 }
4492 out_flush();
4493 ui_breakcheck();
4494 }
4495 }
4496 vim_free(items);
4497}
4498
4499/*
4500 * Return TRUE if option "p" has its default value.
4501 */
4502 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004503optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504{
4505 int dvi;
4506
4507 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004508 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004509 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004511 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004513 // the cast to long is required for Manx C, long_i is
4514 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004515 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004516 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4518}
4519
4520/*
4521 * showoneopt: show the value of one option
4522 * must not be called with a hidden option!
4523 */
4524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004525showoneopt(
4526 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004527 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004529 char_u *varp;
4530 int save_silent = silent_mode;
4531
4532 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004533 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 varp = get_varp_scope(p, opt_flags);
4536
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004537 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4539 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004540 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004542 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004544 msg_puts(" ");
4545 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (!(p->flags & P_BOOL))
4547 {
4548 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004549 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 option_value2string(p, opt_flags);
4551 msg_outtrans(NameBuff);
4552 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004553
4554 silent_mode = save_silent;
4555 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556}
4557
4558/*
4559 * Write modified options as ":set" commands to a file.
4560 *
4561 * There are three values for "opt_flags":
4562 * OPT_GLOBAL: Write global option values and fresh values of
4563 * buffer-local options (used for start of a session
4564 * file).
4565 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4566 * curwin (used for a vimrc file).
4567 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4568 * and local values for window-local options of
4569 * curwin. Local values are also written when at the
4570 * default value, because a modeline or autocommand
4571 * may have set them when doing ":edit file" and the
4572 * user has set them back at the default or fresh
4573 * value.
4574 * When "local_only" is TRUE, don't write fresh
4575 * values, only local values (for ":mkview").
4576 * (fresh value = value used for a new buffer or window for a local option).
4577 *
4578 * Return FAIL on error, OK otherwise.
4579 */
4580 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004581makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
4583 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004584 char_u *varp; // currently used value
4585 char_u *varp_fresh; // local value
4586 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 char *cmd;
4588 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004589 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
4591 /*
4592 * The options that don't have a default (terminal name, columns, lines)
4593 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004594 * Do the loop over "options[]" twice: once for options with the
4595 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004597 for (pri = 1; pri >= 0; --pri)
4598 {
4599 for (p = &options[0]; !istermoption(p); p++)
4600 if (!(p->flags & P_NO_MKRC)
4601 && !istermoption(p)
4602 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004604 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4606 continue;
4607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004608 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4609 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4611 continue;
4612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004613 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004615 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 continue;
4617
Bram Moolenaard23b7142021-04-17 21:04:34 +02004618 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4619 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004620 continue;
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 round = 2;
4623 if (p->indir != PV_NONE)
4624 {
4625 if (p->var == VAR_WIN)
4626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004627 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (!(opt_flags & OPT_LOCAL))
4629 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004630 // When fresh value of window-local option is not at the
4631 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4633 {
4634 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004635 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
4637 round = 1;
4638 varp_local = varp;
4639 varp = varp_fresh;
4640 }
4641 }
4642 }
4643 }
4644
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004645 // Round 1: fresh value for window-local options.
4646 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 for ( ; round <= 2; varp = varp_local, ++round)
4648 {
4649 if (round == 1 || (opt_flags & OPT_GLOBAL))
4650 cmd = "set";
4651 else
4652 cmd = "setlocal";
4653
4654 if (p->flags & P_BOOL)
4655 {
4656 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4657 return FAIL;
4658 }
4659 else if (p->flags & P_NUM)
4660 {
4661 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4662 return FAIL;
4663 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004664 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004666 int do_endif = FALSE;
4667
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004668 // Don't set 'syntax' and 'filetype' again if the value is
4669 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004670 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004671#if defined(FEAT_SYN_HL)
4672 p->indir == PV_SYN ||
4673#endif
4674 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
4676 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4677 *(char_u **)(varp)) < 0
4678 || put_eol(fd) < 0)
4679 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004680 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004683 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004685 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 if (put_line(fd, "endif") == FAIL)
4688 return FAIL;
4689 }
4690 }
4691 }
4692 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 return OK;
4695}
4696
4697#if defined(FEAT_FOLDING) || defined(PROTO)
4698/*
4699 * Generate set commands for the local fold options only. Used when
4700 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4701 */
4702 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004703makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004705 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004707 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 == FAIL
4709# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004710 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004712 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 == FAIL
4714 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4715 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4716 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4717 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4718 )
4719 return FAIL;
4720
4721 return OK;
4722}
4723#endif
4724
4725 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004726put_setstring(
4727 FILE *fd,
4728 char *cmd,
4729 char *name,
4730 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004731 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004734 char_u *buf = NULL;
4735 char_u *part = NULL;
4736 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4739 return FAIL;
4740 if (*valuep != NULL)
4741 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004742 // Output 'pastetoggle' as key names. For other
4743 // options some characters have to be escaped with
4744 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 if (valuep == &p_pt)
4746 {
4747 s = *valuep;
4748 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004749 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 return FAIL;
4751 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004752 // expand the option value, replace $HOME by ~
4753 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004755 int size = (int)STRLEN(*valuep) + 1;
4756
4757 // replace home directory in the whole option value into "buf"
4758 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004759 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004760 goto fail;
4761 home_replace(NULL, *valuep, buf, size, FALSE);
4762
4763 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004764 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004765 // can be expanded when read back.
4766 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4767 && vim_strchr(*valuep, ',') != NULL)
4768 {
4769 part = alloc(size);
4770 if (part == NULL)
4771 goto fail;
4772
4773 // write line break to clear the option, e.g. ':set rtp='
4774 if (put_eol(fd) == FAIL)
4775 goto fail;
4776
4777 p = buf;
4778 while (*p != NUL)
4779 {
4780 // for each comma separated option part, append value to
4781 // the option, :set rtp+=value
4782 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4783 goto fail;
4784 (void)copy_option_part(&p, part, size, ",");
4785 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4786 goto fail;
4787 }
4788 vim_free(buf);
4789 vim_free(part);
4790 return OK;
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004793 {
4794 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004796 }
4797 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 else if (put_escstr(fd, *valuep, 2) == FAIL)
4800 return FAIL;
4801 }
4802 if (put_eol(fd) < 0)
4803 return FAIL;
4804 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004805fail:
4806 vim_free(buf);
4807 vim_free(part);
4808 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809}
4810
4811 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004812put_setnum(
4813 FILE *fd,
4814 char *cmd,
4815 char *name,
4816 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
4818 long wc;
4819
4820 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4821 return FAIL;
4822 if (wc_use_keyname((char_u *)valuep, &wc))
4823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004824 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4826 return FAIL;
4827 }
4828 else if (fprintf(fd, "%ld", *valuep) < 0)
4829 return FAIL;
4830 if (put_eol(fd) < 0)
4831 return FAIL;
4832 return OK;
4833}
4834
4835 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004836put_setbool(
4837 FILE *fd,
4838 char *cmd,
4839 char *name,
4840 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004842 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004843 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4845 || put_eol(fd) < 0)
4846 return FAIL;
4847 return OK;
4848}
4849
4850/*
4851 * Clear all the terminal options.
4852 * If the option has been allocated, free the memory.
4853 * Terminal options are never hidden or indirect.
4854 */
4855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004856clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 /*
4859 * Reset a few things before clearing the old options. This may cause
4860 * outputting a few things that the terminal doesn't understand, but the
4861 * screen will be cleared later, so this is OK.
4862 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004863 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004865 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#endif
4867#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004868 // When starting the GUI close the display opened for the clipboard.
4869 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (gui.starting)
4871 clear_xterm_clip();
4872#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004873 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004875 free_termoptions();
4876}
4877
4878 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004879free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004880{
4881 struct vimoption *p;
4882
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004883 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (istermoption(p))
4885 {
4886 if (p->flags & P_ALLOCED)
4887 free_string_option(*(char_u **)(p->var));
4888 if (p->flags & P_DEF_ALLOCED)
4889 free_string_option(p->def_val[VI_DEFAULT]);
4890 *(char_u **)(p->var) = empty_option;
4891 p->def_val[VI_DEFAULT] = empty_option;
4892 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004893#ifdef FEAT_EVAL
4894 // remember where the option was cleared
4895 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898 clear_termcodes();
4899}
4900
4901/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004902 * Free the string for one term option, if it was allocated.
4903 * Set the string to empty_option and clear allocated flag.
4904 * "var" points to the option value.
4905 */
4906 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004907free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004908{
4909 struct vimoption *p;
4910
4911 for (p = &options[0]; p->fullname != NULL; p++)
4912 if (p->var == var)
4913 {
4914 if (p->flags & P_ALLOCED)
4915 free_string_option(*(char_u **)(p->var));
4916 *(char_u **)(p->var) = empty_option;
4917 p->flags &= ~P_ALLOCED;
4918 break;
4919 }
4920}
4921
4922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 * Set the terminal option defaults to the current value.
4924 * Used after setting the terminal name.
4925 */
4926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004927set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
4929 struct vimoption *p;
4930
4931 for (p = &options[0]; p->fullname != NULL; p++)
4932 {
4933 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4934 {
4935 if (p->flags & P_DEF_ALLOCED)
4936 {
4937 free_string_option(p->def_val[VI_DEFAULT]);
4938 p->flags &= ~P_DEF_ALLOCED;
4939 }
4940 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4941 if (p->flags & P_ALLOCED)
4942 {
4943 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004944 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 }
4947 }
4948}
4949
4950/*
4951 * return TRUE if 'p' starts with 't_'
4952 */
4953 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004954istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955{
4956 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4957}
4958
Bram Moolenaardac13472019-09-16 21:06:21 +02004959/*
4960 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4961 */
4962 int
4963istermoption_idx(int opt_idx)
4964{
4965 return istermoption(&options[opt_idx]);
4966}
4967
Bram Moolenaar113e1072019-01-20 15:30:40 +01004968#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004970 * Unset local option value, similar to ":set opt<".
4971 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004973unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004974{
4975 struct vimoption *p;
4976 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004977 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004978
4979 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004980 if (opt_idx < 0)
4981 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004982 p = &(options[opt_idx]);
4983
4984 switch ((int)p->indir)
4985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004986 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004987 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004988 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004989 break;
4990 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004991 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004992 break;
4993 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004994 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004995 break;
4996 case PV_AR:
4997 buf->b_p_ar = -1;
4998 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004999 case PV_BKC:
5000 clear_string_option(&buf->b_p_bkc);
5001 buf->b_bkc_flags = 0;
5002 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005003 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005004 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005005 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005006 case PV_TC:
5007 clear_string_option(&buf->b_p_tc);
5008 buf->b_tc_flags = 0;
5009 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005010 case PV_SISO:
5011 curwin->w_p_siso = -1;
5012 break;
5013 case PV_SO:
5014 curwin->w_p_so = -1;
5015 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005016#ifdef FEAT_FIND_ID
5017 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005018 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005019 break;
5020 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005021 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005022 break;
5023#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005024 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005025 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005026 break;
5027 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005028 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005029 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005030 case PV_FP:
5031 clear_string_option(&buf->b_p_fp);
5032 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005033#ifdef FEAT_QUICKFIX
5034 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005035 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005036 break;
5037 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005038 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005039 break;
5040 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005041 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005042 break;
5043#endif
5044#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5045 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005046 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005047 break;
5048#endif
5049#if defined(FEAT_CRYPT)
5050 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005051 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052 break;
5053#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005054#ifdef FEAT_LINEBREAK
5055 case PV_SBR:
5056 clear_string_option(&((win_T *)from)->w_p_sbr);
5057 break;
5058#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005059#ifdef FEAT_STL_OPT
5060 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005061 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005062 break;
5063#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005064 case PV_UL:
5065 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5066 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005067#ifdef FEAT_LISP
5068 case PV_LW:
5069 clear_string_option(&buf->b_p_lw);
5070 break;
5071#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005072 case PV_MENC:
5073 clear_string_option(&buf->b_p_menc);
5074 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005075 case PV_LCS:
5076 clear_string_option(&((win_T *)from)->w_p_lcs);
5077 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5078 redraw_later(NOT_VALID);
5079 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005080 }
5081}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005082#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083
5084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 * Get pointer to option variable, depending on local or global scope.
5086 */
5087 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005088get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089{
5090 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5091 {
5092 if (p->var == VAR_WIN)
5093 return (char_u *)GLOBAL_WO(get_varp(p));
5094 return p->var;
5095 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005096 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 switch ((int)p->indir)
5099 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005100 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005102 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5103 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5104 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005106 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5107 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5108 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005109 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005110 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005111 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005112 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5113 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005115 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5116 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005118 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5119 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005120#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5121 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5122#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005123#if defined(FEAT_CRYPT)
5124 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5125#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005126#ifdef FEAT_LINEBREAK
5127 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5128#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005129#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005130 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005131#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005132 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005133#ifdef FEAT_LISP
5134 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5135#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005136 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005137 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005138 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005141 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 return get_varp(p);
5144}
5145
5146/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005147 * Get pointer to option variable at 'opt_idx', depending on local or global
5148 * scope.
5149 */
5150 char_u *
5151get_option_varp_scope(int opt_idx, int opt_flags)
5152{
5153 return get_varp_scope(&(options[opt_idx]), opt_flags);
5154}
5155
5156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 * Get pointer to option variable.
5158 */
5159 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005160get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005162 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (p->var == NULL)
5164 return NULL;
5165
5166 switch ((int)p->indir)
5167 {
5168 case PV_NONE: return p->var;
5169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005171 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005173 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005175 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005177 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005179 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005181 case PV_TC: return *curbuf->b_p_tc != NUL
5182 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005183 case PV_BKC: return *curbuf->b_p_bkc != NUL
5184 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005185 case PV_SISO: return curwin->w_p_siso >= 0
5186 ? (char_u *)&(curwin->w_p_siso) : p->var;
5187 case PV_SO: return curwin->w_p_so >= 0
5188 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005190 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005192 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5194#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005195 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005197 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005199 case PV_FP: return *curbuf->b_p_fp != NUL
5200 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005202 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005204 case PV_GP: return *curbuf->b_p_gp != NUL
5205 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5206 case PV_MP: return *curbuf->b_p_mp != NUL
5207 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005209#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5210 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5211 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5212#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005213#if defined(FEAT_CRYPT)
5214 case PV_CM: return *curbuf->b_p_cm != NUL
5215 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5216#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005217#ifdef FEAT_LINEBREAK
5218 case PV_SBR: return *curwin->w_p_sbr != NUL
5219 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5220#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005221#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005222 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005223 ? (char_u *)&(curwin->w_p_stl) : p->var;
5224#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005225 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5226 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005227#ifdef FEAT_LISP
5228 case PV_LW: return *curbuf->b_p_lw != NUL
5229 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5230#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005231 case PV_MENC: return *curbuf->b_p_menc != NUL
5232 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#ifdef FEAT_ARABIC
5234 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5235#endif
5236 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005237 case PV_LCS: return *curwin->w_p_lcs != NUL
5238 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005239#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005240 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5241#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005242#ifdef FEAT_SYN_HL
5243 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5244 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005245 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005246 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#ifdef FEAT_DIFF
5249 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5250#endif
5251#ifdef FEAT_FOLDING
5252 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5253 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5254 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5255 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5256 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5257 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5258 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5259# ifdef FEAT_EVAL
5260 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5261 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5262# endif
5263 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5264#endif
5265 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005266 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005267#ifdef FEAT_LINEBREAK
5268 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005271 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005272#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5274#endif
5275#ifdef FEAT_RIGHTLEFT
5276 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5277 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5278#endif
5279 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5280 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5281#ifdef FEAT_LINEBREAK
5282 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005283 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5284 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005286 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005288 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005289#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005290 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5291 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5292#endif
5293#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005294 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5295 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5296 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5300 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5303 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5305 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5306#ifdef FEAT_CINDENT
5307 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5308 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5309 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5310#endif
5311#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5312 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#ifdef FEAT_FOLDING
5316 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005319#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005320 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005322#ifdef FEAT_COMPL_FUNC
5323 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005324 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005325#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005326#ifdef FEAT_EVAL
5327 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005330 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005336 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5338 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5339 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5340 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5341#ifdef FEAT_FIND_ID
5342# ifdef FEAT_EVAL
5343 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5344# endif
5345#endif
5346#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5347 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5348 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5349#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005350#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005351 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#ifdef FEAT_CRYPT
5354 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5355#endif
5356#ifdef FEAT_LISP
5357 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5358#endif
5359 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5360 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5361 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5362 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5363 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005365#ifdef FEAT_TEXTOBJ
5366 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5369#ifdef FEAT_SMARTINDENT
5370 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5374#ifdef FEAT_SEARCHPATH
5375 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5376#endif
5377 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5378#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005379 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005380 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5381#endif
5382#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005383 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5384 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5385 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005386 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#endif
5388 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5389 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5390 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5391 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005392#ifdef FEAT_PERSISTENT_UNDO
5393 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5396#ifdef FEAT_KEYMAP
5397 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5398#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005399#ifdef FEAT_SIGNS
5400 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5401#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005402#ifdef FEAT_VARTABS
5403 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5404 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5405#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005406 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005408 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 return (char_u *)&(curbuf->b_p_wm);
5410}
5411
5412/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005413 * Return a pointer to the variable for option at 'opt_idx'
5414 */
5415 char_u *
5416get_option_var(int opt_idx)
5417{
5418 return options[opt_idx].var;
5419}
5420
5421/*
5422 * Return the full name of the option at 'opt_idx'
5423 */
5424 char_u *
5425get_option_fullname(int opt_idx)
5426{
5427 return (char_u *)options[opt_idx].fullname;
5428}
5429
5430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 * Get the value of 'equalprg', either the buffer-local one or the global one.
5432 */
5433 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005434get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 if (*curbuf->b_p_ep == NUL)
5437 return p_ep;
5438 return curbuf->b_p_ep;
5439}
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441/*
5442 * Copy options from one window to another.
5443 * Used when splitting a window.
5444 */
5445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005446win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5449 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005450 after_copy_winopt(wp_to);
5451}
5452
5453/*
5454 * After copying window options: update variables depending on options.
5455 */
5456 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005457after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005458{
5459#ifdef FEAT_LINEBREAK
5460 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005461#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005462#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005463 fill_culopt_flags(NULL, wp);
5464 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005465#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005466 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
5469/*
5470 * Copy the options from one winopt_T to another.
5471 * Doesn't free the old option values in "to", use clear_winopt() for that.
5472 * The 'scroll' option is not copied, because it depends on the window height.
5473 * The 'previewwindow' option is reset, there can be only one preview window.
5474 */
5475 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005476copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478#ifdef FEAT_ARABIC
5479 to->wo_arab = from->wo_arab;
5480#endif
5481 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005482 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005484 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005485#ifdef FEAT_LINEBREAK
5486 to->wo_nuw = from->wo_nuw;
5487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488#ifdef FEAT_RIGHTLEFT
5489 to->wo_rl = from->wo_rl;
5490 to->wo_rlc = vim_strsave(from->wo_rlc);
5491#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005492#ifdef FEAT_LINEBREAK
5493 to->wo_sbr = vim_strsave(from->wo_sbr);
5494#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005495#ifdef FEAT_STL_OPT
5496 to->wo_stl = vim_strsave(from->wo_stl);
5497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005499#ifdef FEAT_DIFF
5500 to->wo_wrap_save = from->wo_wrap_save;
5501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#ifdef FEAT_LINEBREAK
5503 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005504 to->wo_bri = from->wo_bri;
5505 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005507 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005509 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005510 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005511 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005512#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005513 to->wo_spell = from->wo_spell;
5514#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005515#ifdef FEAT_SYN_HL
5516 to->wo_cuc = from->wo_cuc;
5517 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005518 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005519 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#ifdef FEAT_DIFF
5522 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005523 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005525#ifdef FEAT_CONCEAL
5526 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005527 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005528#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005529#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005530 to->wo_twk = vim_strsave(from->wo_twk);
5531 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533#ifdef FEAT_FOLDING
5534 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005535 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005537 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 to->wo_fdi = vim_strsave(from->wo_fdi);
5539 to->wo_fml = from->wo_fml;
5540 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005541 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005543 to->wo_fdm_save = from->wo_diff_saved
5544 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 to->wo_fdn = from->wo_fdn;
5546# ifdef FEAT_EVAL
5547 to->wo_fde = vim_strsave(from->wo_fde);
5548 to->wo_fdt = vim_strsave(from->wo_fdt);
5549# endif
5550 to->wo_fmr = vim_strsave(from->wo_fmr);
5551#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005552#ifdef FEAT_SIGNS
5553 to->wo_scl = vim_strsave(from->wo_scl);
5554#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005555
5556#ifdef FEAT_EVAL
5557 // Copy the script context so that we know where the value was last set.
5558 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5559 sizeof(to->wo_script_ctx));
5560#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005561 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562}
5563
5564/*
5565 * Check string options in a window for a NULL value.
5566 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005568check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 check_winopt(&win->w_onebuf_opt);
5571 check_winopt(&win->w_allbuf_opt);
5572}
5573
5574/*
5575 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5576 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580#ifdef FEAT_FOLDING
5581 check_string_option(&wop->wo_fdi);
5582 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005583 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584# ifdef FEAT_EVAL
5585 check_string_option(&wop->wo_fde);
5586 check_string_option(&wop->wo_fdt);
5587# endif
5588 check_string_option(&wop->wo_fmr);
5589#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005590#ifdef FEAT_SIGNS
5591 check_string_option(&wop->wo_scl);
5592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#ifdef FEAT_RIGHTLEFT
5594 check_string_option(&wop->wo_rlc);
5595#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005596#ifdef FEAT_LINEBREAK
5597 check_string_option(&wop->wo_sbr);
5598#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005599#ifdef FEAT_STL_OPT
5600 check_string_option(&wop->wo_stl);
5601#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005602#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005603 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005604 check_string_option(&wop->wo_cc);
5605#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005606#ifdef FEAT_CONCEAL
5607 check_string_option(&wop->wo_cocu);
5608#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005609#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005610 check_string_option(&wop->wo_twk);
5611 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005612#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005613#ifdef FEAT_LINEBREAK
5614 check_string_option(&wop->wo_briopt);
5615#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005616 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005617 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619
5620/*
5621 * Free the allocated memory inside a winopt_T.
5622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005624clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626#ifdef FEAT_FOLDING
5627 clear_string_option(&wop->wo_fdi);
5628 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005629 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630# ifdef FEAT_EVAL
5631 clear_string_option(&wop->wo_fde);
5632 clear_string_option(&wop->wo_fdt);
5633# endif
5634 clear_string_option(&wop->wo_fmr);
5635#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005636#ifdef FEAT_SIGNS
5637 clear_string_option(&wop->wo_scl);
5638#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005639#ifdef FEAT_LINEBREAK
5640 clear_string_option(&wop->wo_briopt);
5641#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005642 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643#ifdef FEAT_RIGHTLEFT
5644 clear_string_option(&wop->wo_rlc);
5645#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005646#ifdef FEAT_LINEBREAK
5647 clear_string_option(&wop->wo_sbr);
5648#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005649#ifdef FEAT_STL_OPT
5650 clear_string_option(&wop->wo_stl);
5651#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005653 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005654 clear_string_option(&wop->wo_cc);
5655#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005656#ifdef FEAT_CONCEAL
5657 clear_string_option(&wop->wo_cocu);
5658#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005659#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005660 clear_string_option(&wop->wo_twk);
5661 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005662#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005663 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005666#ifdef FEAT_EVAL
5667// Index into the options table for a buffer-local option enum.
5668static int buf_opt_idx[BV_COUNT];
5669# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5670
5671/*
5672 * Initialize buf_opt_idx[] if not done already.
5673 */
5674 static void
5675init_buf_opt_idx(void)
5676{
5677 static int did_init_buf_opt_idx = FALSE;
5678 int i;
5679
5680 if (did_init_buf_opt_idx)
5681 return;
5682 did_init_buf_opt_idx = TRUE;
5683 for (i = 0; !istermoption_idx(i); i++)
5684 if (options[i].indir & PV_BUF)
5685 buf_opt_idx[options[i].indir & PV_MASK] = i;
5686}
5687#else
5688# define COPY_OPT_SCTX(buf, bv)
5689#endif
5690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691/*
5692 * Copy global option values to local options for one buffer.
5693 * Used when creating a new buffer and sometimes when entering a buffer.
5694 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005695 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5697 * appropriate.
5698 * BCO_NOHELP Don't copy the values to a help buffer.
5699 */
5700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005701buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005704 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 int dont_do_help;
5706 int did_isk = FALSE;
5707
5708 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 * Skip this when the option defaults have not been set yet. Happens when
5710 * main() allocates the first buffer.
5711 */
5712 if (p_cpo != NULL)
5713 {
5714 /*
5715 * Always copy when entering and 'cpo' contains 'S'.
5716 * Don't copy when already initialized.
5717 * Don't copy when 'cpo' contains 's' and not entering.
5718 * 'S' BCO_ENTER initialized 's' should_copy
5719 * yes yes X X TRUE
5720 * yes no yes X FALSE
5721 * no X yes X FALSE
5722 * X no no yes FALSE
5723 * X no no no TRUE
5724 * no yes no X TRUE
5725 */
5726 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5727 && (buf->b_p_initialized
5728 || (!(flags & BCO_ENTER)
5729 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5730 should_copy = FALSE;
5731
5732 if (should_copy || (flags & BCO_ALWAYS))
5733 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005734#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005735 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005736 init_buf_opt_idx();
5737#endif
5738 // Don't copy the options specific to a help buffer when
5739 // BCO_NOHELP is given or the options were initialized already
5740 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5742 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005743 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
5745 save_p_isk = buf->b_p_isk;
5746 buf->b_p_isk = NULL;
5747 }
5748 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005749 * Always free the allocated strings. If not already initialized,
5750 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 */
5752 if (!buf->b_p_initialized)
5753 {
5754 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005758 switch (*p_ffs)
5759 {
5760 case 'm':
5761 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5762 case 'd':
5763 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5764 case 'u':
5765 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5766 default:
5767 buf->b_p_ff = vim_strsave(p_ff);
5768 }
5769 if (buf->b_p_ff != NULL)
5770 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 buf->b_p_bh = empty_option;
5772 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774 else
5775 free_buf_options(buf, FALSE);
5776
5777 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005778 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 buf->b_p_ai_nopaste = p_ai_nopaste;
5780 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005781 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005783 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 buf->b_p_tw_nopaste = p_tw_nopaste;
5785 buf->b_p_tw_nobin = p_tw_nobin;
5786 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005787 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 buf->b_p_wm_nopaste = p_wm_nopaste;
5789 buf->b_p_wm_nobin = p_wm_nobin;
5790 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005791 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005792 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005793 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005794 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005795 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005797 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005799 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005801 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 buf->b_p_ml_nobin = p_ml_nobin;
5803 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005804 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005805 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005806 buf->b_p_swf = FALSE;
5807 else
5808 {
5809 buf->b_p_swf = p_swf;
5810 COPY_OPT_SCTX(buf, BV_INF);
5811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005813 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005814#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005815 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005816 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005818#ifdef FEAT_COMPL_FUNC
5819 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005820 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005821 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005822 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005823#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005824#ifdef FEAT_EVAL
5825 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005826 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005829 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005831#ifdef FEAT_VARTABS
5832 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005833 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005834 if (p_vsts && p_vsts != empty_option)
5835 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5836 else
5837 buf->b_p_vsts_array = 0;
5838 buf->b_p_vsts_nopaste = p_vsts_nopaste
5839 ? vim_strsave(p_vsts_nopaste) : NULL;
5840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005842 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005844 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845#ifdef FEAT_FOLDING
5846 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005847 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#endif
5849 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005850 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005851 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005852 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005853 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5854 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005856 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005858 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859#ifdef FEAT_SMARTINDENT
5860 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005861 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#endif
5863 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005864 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#ifdef FEAT_CINDENT
5866 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005867 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005869 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005871 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005873 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005876 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5878 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005879 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
5881#ifdef FEAT_LISP
5882 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005883 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884#endif
5885#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005886 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005888 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005889 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005890 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005891#endif
5892#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005893 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005894 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005895 (void)compile_cap_prog(&buf->b_s);
5896 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005897 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005898 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005899 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005900 buf->b_s.b_p_spo = vim_strsave(p_spo);
5901 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#endif
5903#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5904 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005905 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005907 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005909 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005910#if defined(FEAT_EVAL)
5911 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005912 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914#ifdef FEAT_CRYPT
5915 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005916 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917#endif
5918#ifdef FEAT_SEARCHPATH
5919 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005920 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921#endif
5922#ifdef FEAT_KEYMAP
5923 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005924 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 buf->b_kmap_state |= KEYMAP_INIT;
5926#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005927#ifdef FEAT_TERMINAL
5928 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005929 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005930#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005931 // This isn't really an option, but copying the langmap and IME
5932 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005936 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005938 // options that are normally global but also have a local value
5939 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005941 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005942 buf->b_p_bkc = empty_option;
5943 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944#ifdef FEAT_QUICKFIX
5945 buf->b_p_gp = empty_option;
5946 buf->b_p_mp = empty_option;
5947 buf->b_p_efm = empty_option;
5948#endif
5949 buf->b_p_ep = empty_option;
5950 buf->b_p_kp = empty_option;
5951 buf->b_p_path = empty_option;
5952 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005953 buf->b_p_tc = empty_option;
5954 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#ifdef FEAT_FIND_ID
5956 buf->b_p_def = empty_option;
5957 buf->b_p_inc = empty_option;
5958# ifdef FEAT_EVAL
5959 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005960 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961# endif
5962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 buf->b_p_dict = empty_option;
5964 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005965#ifdef FEAT_TEXTOBJ
5966 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005968#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005969#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5970 buf->b_p_bexpr = empty_option;
5971#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005972#if defined(FEAT_CRYPT)
5973 buf->b_p_cm = empty_option;
5974#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005975#ifdef FEAT_PERSISTENT_UNDO
5976 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005978#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005979#ifdef FEAT_LISP
5980 buf->b_p_lw = empty_option;
5981#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005982 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
5984 /*
5985 * Don't copy the options set by ex_help(), use the saved values,
5986 * when going from a help buffer to a non-help buffer.
5987 * Don't touch these at all when BCO_NOHELP is used and going from
5988 * or to a help buffer.
5989 */
5990 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005993#ifdef FEAT_VARTABS
5994 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5995 tabstop_set(p_vts, &buf->b_p_vts_array);
5996 else
5997 buf->b_p_vts_array = NULL;
5998#endif
5999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 else
6001 {
6002 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006003 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 did_isk = TRUE;
6005 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006006#ifdef FEAT_VARTABS
6007 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006008 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006009 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6010 tabstop_set(p_vts, &buf->b_p_vts_array);
6011 else
6012 buf->b_p_vts_array = NULL;
6013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 if (buf->b_p_bt[0] == 'h')
6016 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006018 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 }
6020 }
6021
6022 /*
6023 * When the options should be copied (ignoring BCO_ALWAYS), set the
6024 * flag that indicates that the options have been initialized.
6025 */
6026 if (should_copy)
6027 buf->b_p_initialized = TRUE;
6028 }
6029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006030 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 if (did_isk)
6032 (void)buf_init_chartab(buf, FALSE);
6033}
6034
6035/*
6036 * Reset the 'modifiable' option and its default value.
6037 */
6038 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006039reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 int opt_idx;
6042
6043 curbuf->b_p_ma = FALSE;
6044 p_ma = FALSE;
6045 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006046 if (opt_idx >= 0)
6047 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048}
6049
6050/*
6051 * Set the global value for 'iminsert' to the local value.
6052 */
6053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006054set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 p_iminsert = curbuf->b_p_iminsert;
6057}
6058
6059/*
6060 * Set the global value for 'imsearch' to the local value.
6061 */
6062 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006063set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 p_imsearch = curbuf->b_p_imsearch;
6066}
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068static int expand_option_idx = -1;
6069static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6070static int expand_option_flags = 0;
6071
6072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006073set_context_in_set_cmd(
6074 expand_T *xp,
6075 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006076 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006079 long_u flags = 0; // init for GCC
6080 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 char_u *p;
6082 char_u *s;
6083 int is_term_option = FALSE;
6084 int key;
6085
6086 expand_option_flags = opt_flags;
6087
6088 xp->xp_context = EXPAND_SETTINGS;
6089 if (*arg == NUL)
6090 {
6091 xp->xp_pattern = arg;
6092 return;
6093 }
6094 p = arg + STRLEN(arg) - 1;
6095 if (*p == ' ' && *(p - 1) != '\\')
6096 {
6097 xp->xp_pattern = p + 1;
6098 return;
6099 }
6100 while (p > arg)
6101 {
6102 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006103 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (*p == ' ' || *p == ',')
6105 {
6106 while (s > arg && *(s - 1) == '\\')
6107 --s;
6108 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006109 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (*p == ' ' && ((p - s) & 1) == 0)
6111 {
6112 ++p;
6113 break;
6114 }
6115 --p;
6116 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006117 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
6119 xp->xp_context = EXPAND_BOOL_SETTINGS;
6120 p += 2;
6121 }
6122 if (STRNCMP(p, "inv", 3) == 0)
6123 {
6124 xp->xp_context = EXPAND_BOOL_SETTINGS;
6125 p += 3;
6126 }
6127 xp->xp_pattern = arg = p;
6128 if (*arg == '<')
6129 {
6130 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006131 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 return;
6133 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006134 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 xp->xp_context = EXPAND_NOTHING;
6137 return;
6138 }
6139 nextchar = *++p;
6140 is_term_option = TRUE;
6141 expand_option_name[2] = KEY2TERMCAP0(key);
6142 expand_option_name[3] = KEY2TERMCAP1(key);
6143 }
6144 else
6145 {
6146 if (p[0] == 't' && p[1] == '_')
6147 {
6148 p += 2;
6149 if (*p != NUL)
6150 ++p;
6151 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006152 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 nextchar = *++p;
6154 is_term_option = TRUE;
6155 expand_option_name[2] = p[-2];
6156 expand_option_name[3] = p[-1];
6157 }
6158 else
6159 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006160 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6162 p++;
6163 if (*p == NUL)
6164 return;
6165 nextchar = *p;
6166 *p = NUL;
6167 opt_idx = findoption(arg);
6168 *p = nextchar;
6169 if (opt_idx == -1 || options[opt_idx].var == NULL)
6170 {
6171 xp->xp_context = EXPAND_NOTHING;
6172 return;
6173 }
6174 flags = options[opt_idx].flags;
6175 if (flags & P_BOOL)
6176 {
6177 xp->xp_context = EXPAND_NOTHING;
6178 return;
6179 }
6180 }
6181 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006182 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6184 {
6185 ++p;
6186 nextchar = '=';
6187 }
6188 if ((nextchar != '=' && nextchar != ':')
6189 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6190 {
6191 xp->xp_context = EXPAND_UNSUCCESSFUL;
6192 return;
6193 }
6194 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6195 {
6196 xp->xp_context = EXPAND_OLD_SETTING;
6197 if (is_term_option)
6198 expand_option_idx = -1;
6199 else
6200 expand_option_idx = opt_idx;
6201 xp->xp_pattern = p + 1;
6202 return;
6203 }
6204 xp->xp_context = EXPAND_NOTHING;
6205 if (is_term_option || (flags & P_NUM))
6206 return;
6207
6208 xp->xp_pattern = p + 1;
6209
6210 if (flags & P_EXPAND)
6211 {
6212 p = options[opt_idx].var;
6213 if (p == (char_u *)&p_bdir
6214 || p == (char_u *)&p_dir
6215 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006216 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 || p == (char_u *)&p_rtp
6218#ifdef FEAT_SEARCHPATH
6219 || p == (char_u *)&p_cdpath
6220#endif
6221#ifdef FEAT_SESSION
6222 || p == (char_u *)&p_vdir
6223#endif
6224 )
6225 {
6226 xp->xp_context = EXPAND_DIRECTORIES;
6227 if (p == (char_u *)&p_path
6228#ifdef FEAT_SEARCHPATH
6229 || p == (char_u *)&p_cdpath
6230#endif
6231 )
6232 xp->xp_backslash = XP_BS_THREE;
6233 else
6234 xp->xp_backslash = XP_BS_ONE;
6235 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006236 else if (p == (char_u *)&p_ft)
6237 {
6238 xp->xp_context = EXPAND_FILETYPE;
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 else
6241 {
6242 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006243 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 if (p == (char_u *)&p_tags)
6245 xp->xp_backslash = XP_BS_THREE;
6246 else
6247 xp->xp_backslash = XP_BS_ONE;
6248 }
6249 }
6250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006251 // For an option that is a list of file names, find the start of the
6252 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006255 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (*p == ' ' || *p == ',')
6257 {
6258 s = p;
6259 while (s > xp->xp_pattern && *(s - 1) == '\\')
6260 --s;
6261 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6262 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6263 {
6264 xp->xp_pattern = p + 1;
6265 break;
6266 }
6267 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006268
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006269#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006270 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006271 if (options[opt_idx].var == (char_u *)&p_sps
6272 && STRNCMP(p, "file:", 5) == 0)
6273 {
6274 xp->xp_pattern = p + 5;
6275 break;
6276 }
6277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279
6280 return;
6281}
6282
6283 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006284ExpandSettings(
6285 expand_T *xp,
6286 regmatch_T *regmatch,
6287 int *num_file,
6288 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006290 int num_normal = 0; // Nr of matching non-term-code settings
6291 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 int opt_idx;
6293 int match;
6294 int count = 0;
6295 char_u *str;
6296 int loop;
6297 int is_term_opt;
6298 char_u name_buf[MAX_KEY_NAME_LEN];
6299 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006300 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006302 // do this loop twice:
6303 // loop == 0: count the number of matching options
6304 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 for (loop = 0; loop <= 1; ++loop)
6306 {
6307 regmatch->rm_ic = ic;
6308 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6309 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006310 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6311 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6313 {
6314 if (loop == 0)
6315 num_normal++;
6316 else
6317 (*file)[count++] = vim_strsave((char_u *)names[match]);
6318 }
6319 }
6320 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6321 opt_idx++)
6322 {
6323 if (options[opt_idx].var == NULL)
6324 continue;
6325 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6326 && !(options[opt_idx].flags & P_BOOL))
6327 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006328 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (is_term_opt && num_normal > 0)
6330 continue;
6331 match = FALSE;
6332 if (vim_regexec(regmatch, str, (colnr_T)0)
6333 || (options[opt_idx].shortname != NULL
6334 && vim_regexec(regmatch,
6335 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6336 match = TRUE;
6337 else if (is_term_opt)
6338 {
6339 name_buf[0] = '<';
6340 name_buf[1] = 't';
6341 name_buf[2] = '_';
6342 name_buf[3] = str[2];
6343 name_buf[4] = str[3];
6344 name_buf[5] = '>';
6345 name_buf[6] = NUL;
6346 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6347 {
6348 match = TRUE;
6349 str = name_buf;
6350 }
6351 }
6352 if (match)
6353 {
6354 if (loop == 0)
6355 {
6356 if (is_term_opt)
6357 num_term++;
6358 else
6359 num_normal++;
6360 }
6361 else
6362 (*file)[count++] = vim_strsave(str);
6363 }
6364 }
6365 /*
6366 * Check terminal key codes, these are not in the option table
6367 */
6368 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6369 {
6370 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6371 {
6372 if (!isprint(str[0]) || !isprint(str[1]))
6373 continue;
6374
6375 name_buf[0] = 't';
6376 name_buf[1] = '_';
6377 name_buf[2] = str[0];
6378 name_buf[3] = str[1];
6379 name_buf[4] = NUL;
6380
6381 match = FALSE;
6382 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6383 match = TRUE;
6384 else
6385 {
6386 name_buf[0] = '<';
6387 name_buf[1] = 't';
6388 name_buf[2] = '_';
6389 name_buf[3] = str[0];
6390 name_buf[4] = str[1];
6391 name_buf[5] = '>';
6392 name_buf[6] = NUL;
6393
6394 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6395 match = TRUE;
6396 }
6397 if (match)
6398 {
6399 if (loop == 0)
6400 num_term++;
6401 else
6402 (*file)[count++] = vim_strsave(name_buf);
6403 }
6404 }
6405
6406 /*
6407 * Check special key names.
6408 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006409 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6411 {
6412 name_buf[0] = '<';
6413 STRCPY(name_buf + 1, str);
6414 STRCAT(name_buf, ">");
6415
6416 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6417 {
6418 if (loop == 0)
6419 num_term++;
6420 else
6421 (*file)[count++] = vim_strsave(name_buf);
6422 }
6423 }
6424 }
6425 if (loop == 0)
6426 {
6427 if (num_normal > 0)
6428 *num_file = num_normal;
6429 else if (num_term > 0)
6430 *num_file = num_term;
6431 else
6432 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006433 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 if (*file == NULL)
6435 {
6436 *file = (char_u **)"";
6437 return FAIL;
6438 }
6439 }
6440 }
6441 return OK;
6442}
6443
6444 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006445ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006447 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 char_u *buf;
6449
6450 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006451 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (*file == NULL)
6453 return FAIL;
6454
6455 /*
6456 * For a terminal key code expand_option_idx is < 0.
6457 */
6458 if (expand_option_idx < 0)
6459 {
6460 var = find_termcode(expand_option_name + 2);
6461 if (var == NULL)
6462 expand_option_idx = findoption(expand_option_name);
6463 }
6464
6465 if (expand_option_idx >= 0)
6466 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 option_value2string(&options[expand_option_idx], expand_option_flags);
6469 var = NameBuff;
6470 }
6471 else if (var == NULL)
6472 var = (char_u *)"";
6473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006474 // A backslash is required before some characters. This is the reverse of
6475 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 buf = vim_strsave_escaped(var, escape_chars);
6477
6478 if (buf == NULL)
6479 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006480 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 return FAIL;
6482 }
6483
6484#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006485 // For MS-Windows et al. we don't double backslashes at the start and
6486 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006487 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if (var[0] == '\\' && var[1] == '\\'
6489 && expand_option_idx >= 0
6490 && (options[expand_option_idx].flags & P_EXPAND)
6491 && vim_isfilec(var[2])
6492 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006493 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494#endif
6495
6496 *file[0] = buf;
6497 *num_file = 1;
6498 return OK;
6499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501/*
6502 * Get the value for the numeric or string option *opp in a nice format into
6503 * NameBuff[]. Must not be called with a hidden option!
6504 */
6505 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006506option_value2string(
6507 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006508 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 char_u *varp;
6511
6512 varp = get_varp_scope(opp, opt_flags);
6513
6514 if (opp->flags & P_NUM)
6515 {
6516 long wc = 0;
6517
6518 if (wc_use_keyname(varp, &wc))
6519 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6520 else if (wc != 0)
6521 STRCPY(NameBuff, transchar((int)wc));
6522 else
6523 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6524 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006525 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
6527 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006528 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 NameBuff[0] = NUL;
6530#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006531 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006532 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 STRCPY(NameBuff, "*****");
6534#endif
6535 else if (opp->flags & P_EXPAND)
6536 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006537 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 else if ((char_u **)opp->var == &p_pt)
6539 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6540 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006541 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
6543}
6544
6545/*
6546 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6547 * printed as a keyname.
6548 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6549 */
6550 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006551wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
6553 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6554 {
6555 *wcp = *(long *)varp;
6556 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6557 return TRUE;
6558 }
6559 return FALSE;
6560}
6561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 * Return TRUE if "x" is present in 'shortmess' option, or
6564 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6565 */
6566 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006567shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006569 return p_shm != NULL &&
6570 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 || (vim_strchr(p_shm, 'a') != NULL
6572 && vim_strchr((char_u *)SHM_A, x) != NULL));
6573}
6574
6575/*
6576 * paste_option_changed() - Called after p_paste was set or reset.
6577 */
6578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006579paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 static int old_p_paste = FALSE;
6582 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006583 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584#ifdef FEAT_CMDL_INFO
6585 static int save_ru = 0;
6586#endif
6587#ifdef FEAT_RIGHTLEFT
6588 static int save_ri = 0;
6589 static int save_hkmap = 0;
6590#endif
6591 buf_T *buf;
6592
6593 if (p_paste)
6594 {
6595 /*
6596 * Paste switched from off to on.
6597 * Save the current values, so they can be restored later.
6598 */
6599 if (!old_p_paste)
6600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006602 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {
6604 buf->b_p_tw_nopaste = buf->b_p_tw;
6605 buf->b_p_wm_nopaste = buf->b_p_wm;
6606 buf->b_p_sts_nopaste = buf->b_p_sts;
6607 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006608 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006609#ifdef FEAT_VARTABS
6610 if (buf->b_p_vsts_nopaste)
6611 vim_free(buf->b_p_vsts_nopaste);
6612 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6613 ? vim_strsave(buf->b_p_vsts) : NULL;
6614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 }
6616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006617 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006619 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620#ifdef FEAT_CMDL_INFO
6621 save_ru = p_ru;
6622#endif
6623#ifdef FEAT_RIGHTLEFT
6624 save_ri = p_ri;
6625 save_hkmap = p_hkmap;
6626#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006627 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006628 p_ai_nopaste = p_ai;
6629 p_et_nopaste = p_et;
6630 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 p_tw_nopaste = p_tw;
6632 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006633#ifdef FEAT_VARTABS
6634 if (p_vsts_nopaste)
6635 vim_free(p_vsts_nopaste);
6636 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639
6640 /*
6641 * Always set the option values, also when 'paste' is set when it is
6642 * already on.
6643 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006644 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006645 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006647 buf->b_p_tw = 0; // textwidth is 0
6648 buf->b_p_wm = 0; // wrapmargin is 0
6649 buf->b_p_sts = 0; // softtabstop is 0
6650 buf->b_p_ai = 0; // no auto-indent
6651 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006652#ifdef FEAT_VARTABS
6653 if (buf->b_p_vsts)
6654 free_string_option(buf->b_p_vsts);
6655 buf->b_p_vsts = empty_option;
6656 if (buf->b_p_vsts_array)
6657 vim_free(buf->b_p_vsts_array);
6658 buf->b_p_vsts_array = 0;
6659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 }
6661
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006662 // set global options
6663 p_sm = 0; // no showmatch
6664 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 status_redraw_all(); // redraw to remove the ruler
6668 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669#endif
6670#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006671 p_ri = 0; // no reverse insert
6672 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006674 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 p_tw = 0;
6676 p_wm = 0;
6677 p_sts = 0;
6678 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006679#ifdef FEAT_VARTABS
6680 if (p_vsts)
6681 free_string_option(p_vsts);
6682 p_vsts = empty_option;
6683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 }
6685
6686 /*
6687 * Paste switched from on to off: Restore saved values.
6688 */
6689 else if (old_p_paste)
6690 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006691 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006692 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
6694 buf->b_p_tw = buf->b_p_tw_nopaste;
6695 buf->b_p_wm = buf->b_p_wm_nopaste;
6696 buf->b_p_sts = buf->b_p_sts_nopaste;
6697 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006698 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006699#ifdef FEAT_VARTABS
6700 if (buf->b_p_vsts)
6701 free_string_option(buf->b_p_vsts);
6702 buf->b_p_vsts = buf->b_p_vsts_nopaste
6703 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6704 if (buf->b_p_vsts_array)
6705 vim_free(buf->b_p_vsts_array);
6706 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6707 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6708 else
6709 buf->b_p_vsts_array = 0;
6710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006715 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006718 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 p_ru = save_ru;
6720#endif
6721#ifdef FEAT_RIGHTLEFT
6722 p_ri = save_ri;
6723 p_hkmap = save_hkmap;
6724#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006725 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006726 p_ai = p_ai_nopaste;
6727 p_et = p_et_nopaste;
6728 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 p_tw = p_tw_nopaste;
6730 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006731#ifdef FEAT_VARTABS
6732 if (p_vsts)
6733 free_string_option(p_vsts);
6734 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737
6738 old_p_paste = p_paste;
6739}
6740
6741/*
6742 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6743 *
6744 * Reset 'compatible' and set the values for options that didn't get set yet
6745 * to the Vim defaults.
6746 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006747 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 */
6749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006750vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006752 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006753 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006754 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 if (!option_was_set((char_u *)"cp"))
6757 {
6758 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006759 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6761 set_option_default(opt_idx, OPT_FREE, FALSE);
6762 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006763 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006765
6766 if (fname != NULL)
6767 {
6768 p = vim_getenv(envname, &dofree);
6769 if (p == NULL)
6770 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006771 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006772 p = FullName_save(fname, FALSE);
6773 if (p != NULL)
6774 {
6775 vim_setenv(envname, p);
6776 vim_free(p);
6777 }
6778 }
6779 else if (dofree)
6780 vim_free(p);
6781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782}
6783
6784/*
6785 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6786 */
6787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006788change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006790 int opt_idx;
6791
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 if (p_cp != on)
6793 {
6794 p_cp = on;
6795 compatible_set();
6796 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006797 opt_idx = findoption((char_u *)"cp");
6798 if (opt_idx >= 0)
6799 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800}
6801
6802/*
6803 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006804 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 */
6806 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006807option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 int idx;
6810
6811 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006812 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 return FALSE;
6814 if (options[idx].flags & P_WAS_SET)
6815 return TRUE;
6816 return FALSE;
6817}
6818
6819/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006820 * Reset the flag indicating option "name" was set.
6821 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006822 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006823reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006824{
6825 int idx = findoption(name);
6826
6827 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006828 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006829 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006830 return OK;
6831 }
6832 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006833}
6834
6835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 * compatible_set() - Called when 'compatible' has been set or unset.
6837 *
6838 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6839 * flag) to a Vi compatible value.
6840 * When 'compatible' is unset: Set all options that have a different default
6841 * for Vim (without the P_VI_DEF flag) to that default.
6842 */
6843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006844compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 int opt_idx;
6847
Bram Moolenaardac13472019-09-16 21:06:21 +02006848 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6850 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6851 set_option_default(opt_idx, OPT_FREE, p_cp);
6852 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006853 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854}
6855
Bram Moolenaardac13472019-09-16 21:06:21 +02006856#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * fill_breakat_flags() -- called when 'breakat' changes value.
6860 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 int i;
6866
6867 for (i = 0; i < 256; i++)
6868 breakat_flags[i] = FALSE;
6869
6870 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006871 for (p = p_breakat; *p; p++)
6872 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874#endif
6875
6876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 * Check if backspacing over something is allowed.
6878 */
6879 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006880can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006881 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006883#ifdef FEAT_JOB_CHANNEL
6884 if (what == BS_START && bt_prompt(curbuf))
6885 return FALSE;
6886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 switch (*p_bs)
6888 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006889 case '3': return TRUE;
6890 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 case '1': return (what != BS_START);
6892 case '0': return FALSE;
6893 }
6894 return vim_strchr(p_bs, what) != NULL;
6895}
6896
6897/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006898 * Return the effective 'scrolloff' value for the current window, using the
6899 * global value when appropriate.
6900 */
6901 long
6902get_scrolloff_value(void)
6903{
6904 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6905}
6906
6907/*
6908 * Return the effective 'sidescrolloff' value for the current window, using the
6909 * global value when appropriate.
6910 */
6911 long
6912get_sidescrolloff_value(void)
6913{
6914 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6915}
6916
6917/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006918 * Get the local or global value of 'backupcopy'.
6919 */
6920 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006921get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006922{
6923 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6924}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006925
Bram Moolenaaree857022019-11-09 23:26:40 +01006926#if defined(FEAT_LINEBREAK) || defined(PROTO)
6927/*
6928 * Get the local or global value of 'showbreak'.
6929 */
6930 char_u *
6931get_showbreak_value(win_T *win)
6932{
6933 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6934 return p_sbr;
6935 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6936 return empty_option;
6937 return win->w_p_sbr;
6938}
6939#endif
6940
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006941#if defined(FEAT_EVAL) || defined(PROTO)
6942/*
6943 * Get window or buffer local options.
6944 */
6945 dict_T *
6946get_winbuf_options(int bufopt)
6947{
6948 dict_T *d;
6949 int opt_idx;
6950
6951 d = dict_alloc();
6952 if (d == NULL)
6953 return NULL;
6954
Bram Moolenaardac13472019-09-16 21:06:21 +02006955 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006956 {
6957 struct vimoption *opt = &options[opt_idx];
6958
6959 if ((bufopt && (opt->indir & PV_BUF))
6960 || (!bufopt && (opt->indir & PV_WIN)))
6961 {
6962 char_u *varp = get_varp(opt);
6963
6964 if (varp != NULL)
6965 {
6966 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006967 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006968 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006969 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006970 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006971 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006972 }
6973 }
6974 }
6975
6976 return d;
6977}
6978#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006979
Bram Moolenaardac13472019-09-16 21:06:21 +02006980#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006981/*
6982 * This is called when 'culopt' is changed
6983 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006984 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006985fill_culopt_flags(char_u *val, win_T *wp)
6986{
6987 char_u *p;
6988 char_u culopt_flags_new = 0;
6989
6990 if (val == NULL)
6991 p = wp->w_p_culopt;
6992 else
6993 p = val;
6994 while (*p != NUL)
6995 {
6996 if (STRNCMP(p, "line", 4) == 0)
6997 {
6998 p += 4;
6999 culopt_flags_new |= CULOPT_LINE;
7000 }
7001 else if (STRNCMP(p, "both", 4) == 0)
7002 {
7003 p += 4;
7004 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7005 }
7006 else if (STRNCMP(p, "number", 6) == 0)
7007 {
7008 p += 6;
7009 culopt_flags_new |= CULOPT_NBR;
7010 }
7011 else if (STRNCMP(p, "screenline", 10) == 0)
7012 {
7013 p += 10;
7014 culopt_flags_new |= CULOPT_SCRLINE;
7015 }
7016
7017 if (*p != ',' && *p != NUL)
7018 return FAIL;
7019 if (*p == ',')
7020 ++p;
7021 }
7022
7023 // Can't have both "line" and "screenline".
7024 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7025 return FAIL;
7026 wp->w_p_culopt_flags = culopt_flags_new;
7027
7028 return OK;
7029}
7030#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007031
7032/*
7033 * Get the value of 'magic' adjusted for Vim9 script.
7034 */
7035 int
7036magic_isset(void)
7037{
7038 switch (magic_overruled)
7039 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007040 case OPTION_MAGIC_ON: return TRUE;
7041 case OPTION_MAGIC_OFF: return FALSE;
7042 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007043 }
7044#ifdef FEAT_EVAL
7045 if (in_vim9script())
7046 return TRUE;
7047#endif
7048 return p_magic;
7049}