blob: 935a3250e0662acad0b5921992dc06f440eef06e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
60static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void option_value2string(struct vimoption *, int opt_flags);
63static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
148 for (n = 0; n < (long)(sizeof(names) / sizeof(char *)); ++n)
149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef HAVE_AVAIL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#else
205# ifdef HAVE_TOTAL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100206 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000207 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_SEARCHPATH
226 {
227 char_u *cdpath;
228 char_u *buf;
229 int i;
230 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000231 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100233 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000234 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (cdpath != NULL)
236 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200237 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (buf != NULL)
239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100240 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 j = 1;
242 for (i = 0; cdpath[i] != NUL; ++i)
243 {
244 if (vim_ispathlistsep(cdpath[i]))
245 buf[j++] = ',';
246 else
247 {
248 if (cdpath[i] == ' ' || cdpath[i] == ',')
249 buf[j++] = '\\';
250 buf[j++] = cdpath[i];
251 }
252 }
253 buf[j] = NUL;
254 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000255 if (opt_idx >= 0)
256 {
257 options[opt_idx].def_val[VI_DEFAULT] = buf;
258 options[opt_idx].flags |= P_DEF_ALLOCED;
259 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200260 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100261 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000263 if (mustfree)
264 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
267#endif
268
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100270 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100272# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 (char_u *)"cp1252"
274# else
275# ifdef VMS
276 (char_u *)"dec-mcs"
277# else
278# ifdef EBCDIC
279 (char_u *)"ebcdic-uk"
280# else
281# ifdef MAC
282 (char_u *)"mac-roman"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100283# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 (char_u *)"hp-roman8"
285# endif
286# endif
287# endif
288# endif
289 );
290#endif
291
292#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100293 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100295# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000296 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297# else
298# ifdef VMS
299 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
300
301# else
302 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
303# endif
304# endif
305 );
306#endif
307
308 /*
309 * Set all the options (except the terminal options) to their default
310 * value. Also set the global value for local options.
311 */
312 set_options_default(0);
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
334 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
392 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000427# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100428 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000429 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# endif
432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100433 // enc_locale() will try to find the encoding of the current locale.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 p = enc_locale();
435 if (p != NULL)
436 {
437 char_u *save_enc;
438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100439 // Try setting 'encoding' and check if the value is valid.
440 // If not, go back to the default "latin1".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 save_enc = p_enc;
442 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000443 if (STRCMP(p_enc, "gb18030") == 0)
444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100445 // We don't support "gb18030", but "cp936" is a good substitute
446 // for practical purposes, thus use that. It's not an alias to
447 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000448 p_enc = vim_strsave((char_u *)"cp936");
449 vim_free(p);
450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if (mb_init() == NULL)
452 {
453 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000454 if (opt_idx >= 0)
455 {
456 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
457 options[opt_idx].flags |= P_DEF_ALLOCED;
458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
Bram Moolenaard0573012017-10-28 21:11:06 +0200460#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100463 // Adjust the default for 'isprint' and 'iskeyword' to match
464 // latin1. Also set the defaults for when 'nocompatible' is
465 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000466 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000467 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000468 set_string_option_direct((char_u *)"isk", -1,
469 ISK_LATIN1, OPT_FREE, SID_NONE);
470 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000471 if (opt_idx >= 0)
472 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000473 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000474 if (opt_idx >= 0)
475 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000476 (void)init_chartab();
477 }
478#endif
479
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200480#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100481 // Win32 console: When GetACP() returns a different value from
482 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200483 if (
484# ifdef VIMDLL
485 (!gui.in_use && !gui.starting) &&
486# endif
487 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
489 char buf[50];
490
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
492 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100493 if (GetConsoleCP() == 0)
494 sprintf(buf, "cp%ld", (long)GetACP());
495 else
496 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 p_tenc = vim_strsave((char_u *)buf);
498 if (p_tenc != NULL)
499 {
500 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000501 if (opt_idx >= 0)
502 {
503 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
504 options[opt_idx].flags |= P_DEF_ALLOCED;
505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 convert_setup(&input_conv, p_tenc, p_enc);
507 convert_setup(&output_conv, p_enc, p_tenc);
508 }
509 else
510 p_tenc = empty_option;
511 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100512#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100513#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100514 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000515 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518 else
519 {
520 vim_free(p_enc);
521 p_enc = save_enc;
522 }
523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
525#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100526 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 set_helplang_default(get_mess_lang());
528#endif
529}
530
531/*
532 * Set an option to its default value.
533 * This does not take care of side effects!
534 */
535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536set_option_default(
537 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100538 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
539 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 char_u *varp; // pointer to variable for current option
542 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000544 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
546
547 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
548 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
551 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
552 if (flags & P_STRING)
553 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100554 // Use set_string_option_direct() for local options to handle
555 // freeing and allocating the value.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200556 if (options[opt_idx].indir != PV_NONE)
557 set_string_option_direct(NULL, opt_idx,
558 options[opt_idx].def_val[dvi], opt_flags, 0);
559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200561 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
562 free_string_option(*(char_u **)(varp));
563 *(char_u **)varp = options[opt_idx].def_val[dvi];
564 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 }
567 else if (flags & P_NUM)
568 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000569 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 win_comp_scroll(curwin);
571 else
572 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100573 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
574
575 if ((long *)varp == &curwin->w_p_so
576 || (long *)varp == &curwin->w_p_siso)
577 // 'scrolloff' and 'sidescrolloff' local values have a
578 // different default value than the global default.
579 *(long *)varp = -1;
580 else
581 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100582 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (both)
584 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100585 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 }
587 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100588 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100590 // the cast to long is required for Manx C, long_i is needed for
591 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000592 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000593#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000595 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
596 *(int *)varp = FALSE;
597#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (both)
600 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
601 *(int *)varp;
602 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000605 flagsp = insecure_flag(opt_idx, opt_flags);
606 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608
609#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200610 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#endif
612}
613
614/*
615 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200616 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 */
618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100620 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000624 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaardac13472019-09-16 21:06:21 +0200626 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200627 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200628 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100629 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200630# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200631 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200632 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200633# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100634 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 set_option_default(i, opt_flags, p_cp);
636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100637 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000638 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200640#ifdef FEAT_CINDENT
641 parse_cino(curbuf);
642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643}
644
645/*
646 * Set the Vi-default value of a string option.
647 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100648 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100650 static void
651set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 char_u *p;
654 int opt_idx;
655
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100656 if (escape && vim_strchr(val, ' ') != NULL)
657 p = vim_strsave_escaped(val, (char_u *)" ");
658 else
659 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000663 if (opt_idx >= 0)
664 {
665 if (options[opt_idx].flags & P_DEF_ALLOCED)
666 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
667 options[opt_idx].def_val[VI_DEFAULT] = p;
668 options[opt_idx].flags |= P_DEF_ALLOCED;
669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671}
672
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 void
674set_string_default(char *name, char_u *val)
675{
676 set_string_default_esc(name, val, FALSE);
677}
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200680 * For an option value that contains comma separated items, find "newval" in
681 * "origval". Return NULL if not found.
682 */
683 static char_u *
684find_dup_item(char_u *origval, char_u *newval, long_u flags)
685{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200686 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200687 size_t newlen;
688 char_u *s;
689
690 if (origval == NULL)
691 return NULL;
692
693 newlen = STRLEN(newval);
694 for (s = origval; *s != NUL; ++s)
695 {
696 if ((!(flags & P_COMMA)
697 || s == origval
698 || (s[-1] == ',' && !(bs & 1)))
699 && STRNCMP(s, newval, newlen) == 0
700 && (!(flags & P_COMMA)
701 || s[newlen] == ','
702 || s[newlen] == NUL))
703 return s;
704 // Count backslashes. Only a comma with an even number of backslashes
705 // or a single backslash preceded by a comma before it is recognized as
706 // a separator.
707 if ((s > origval + 1
708 && s[-1] == '\\'
709 && s[-2] != ',')
710 || (s == origval + 1
711 && s[-1] == '\\'))
712 ++bs;
713 else
714 bs = 0;
715 }
716 return NULL;
717}
718
719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 * Set the Vi-default value of a number option.
721 * Used for 'lines' and 'columns'.
722 */
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000726 int opt_idx;
727
728 opt_idx = findoption((char_u *)name);
729 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000730 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731}
732
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200733/*
734 * Set all window-local and buffer-local options to the Vim default.
735 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200736 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200737 */
738 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200739set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200740{
741 win_T *save_curwin = curwin;
742 int i;
743
744 curwin = wp;
745 curbuf = curwin->w_buffer;
746 block_autocmds();
747
Bram Moolenaardac13472019-09-16 21:06:21 +0200748 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200749 {
750 struct vimoption *p = &(options[i]);
751 char_u *varp = get_varp_scope(p, OPT_LOCAL);
752
753 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200754 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200755 && !(options[i].flags & P_NODEFAULT)
756 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200757 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200758 }
759
760 unblock_autocmds();
761 curwin = save_curwin;
762 curbuf = curwin->w_buffer;
763}
764
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000765#if defined(EXITFREE) || defined(PROTO)
766/*
767 * Free all options.
768 */
769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100770free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000771{
772 int i;
773
Bram Moolenaardac13472019-09-16 21:06:21 +0200774 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000775 {
776 if (options[i].indir == PV_NONE)
777 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100778 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100779 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000780 free_string_option(*(char_u **)options[i].var);
781 if (options[i].flags & P_DEF_ALLOCED)
782 free_string_option(options[i].def_val[VI_DEFAULT]);
783 }
784 else if (options[i].var != VAR_WIN
785 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100786 // buffer-local option: free global value
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000787 free_string_option(*(char_u **)options[i].var);
788 }
789}
790#endif
791
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
794 * Initialize the options, part two: After getting Rows and Columns and
795 * setting 'term'.
796 */
797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100798set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000800 int idx;
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100803 * 'scroll' defaults to half the window height. The stored default is zero,
804 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000806 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000807 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000808 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 comp_col();
810
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000811 /*
812 * 'window' is only for backwards compatibility with Vi.
813 * Default is Rows - 1.
814 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000815 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000816 p_window = Rows - 1;
817 set_number_default("window", Rows - 1);
818
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100819 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100820#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000821 /*
822 * If 'background' wasn't set by the user, try guessing the value,
823 * depending on the terminal name. Only need to check for terminals
824 * with a dark background, that can handle color.
825 */
826 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000827 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
828 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000830 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 // don't mark it as set, when starting the GUI it may be
832 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000833 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000836
837#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000839#endif
840#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000842#endif
843#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846}
847
848/*
849 * Initialize the options, part three: After reading the .vimrc
850 */
851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100852set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100854#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855/*
856 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
857 * This is done after other initializations, where 'shell' might have been
858 * set, but only if they have not been set before.
859 */
860 char_u *p;
861 int idx_srr;
862 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100863# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 int idx_sp;
865 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000869 if (idx_srr < 0)
870 do_srr = FALSE;
871 else
872 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100873# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000875 if (idx_sp < 0)
876 do_sp = FALSE;
877 else
878 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100879# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200880 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (p != NULL)
882 {
883 /*
884 * Default for p_sp is "| tee", for p_srr is ">".
885 * For known shells it is changed here to include stderr.
886 */
887 if ( fnamecmp(p, "csh") == 0
888 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100889# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 || fnamecmp(p, "csh.exe") == 0
891 || fnamecmp(p, "tcsh.exe") == 0
892# endif
893 )
894 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100895# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (do_sp)
897 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100898# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100900# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
904 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (do_srr)
907 {
908 p_srr = (char_u *)">&";
909 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
910 }
911 }
912 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100913 // Always use bourne shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if ( fnamecmp(p, "sh") == 0
915 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200916 || fnamecmp(p, "mksh") == 0
917 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000919 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200921 || fnamecmp(p, "fish") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100922# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 || fnamecmp(p, "cmd") == 0
924 || fnamecmp(p, "sh.exe") == 0
925 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200926 || fnamecmp(p, "mksh.exe") == 0
927 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000929 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 || fnamecmp(p, "bash.exe") == 0
931 || fnamecmp(p, "cmd.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100933 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100935# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (do_sp)
937 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100938# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100940# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
944 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (do_srr)
947 {
948 p_srr = (char_u *)">%s 2>&1";
949 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
950 }
951 }
952 vim_free(p);
953 }
954#endif
955
Bram Moolenaar4f974752019-02-17 17:44:42 +0100956#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100958 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
959 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 * This is done after other initializations, where 'shell' might have been
961 * set, but only if they have not been set before. Default for p_shcf is
962 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100963 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000965 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 int idx3;
968
969 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000970 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 p_shcf = (char_u *)"-c";
973 options[idx3].def_val[VI_DEFAULT] = p_shcf;
974 }
975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100976 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000978 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 p_sxq = (char_u *)"\"";
981 options[idx3].def_val[VI_DEFAULT] = p_sxq;
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100984 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
985 {
986 int idx3;
987
988 /*
989 * cmd.exe on Windows will strip the first and last double quote given
990 * on the command line, e.g. most of the time things like:
991 * cmd /c "my path/to/echo" "my args to echo"
992 * become:
993 * my path/to/echo" "my args to echo
994 * when executed.
995 *
Bram Moolenaar034b1152012-02-19 18:19:30 +0100996 * To avoid this, set shellxquote to surround the command in
997 * parenthesis. This appears to make most commands work, without
998 * breaking commands that worked previously, such as
999 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001000 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001001 idx3 = findoption((char_u *)"sxq");
1002 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1003 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001004 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001005 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1006 }
1007
Bram Moolenaara64ba222012-02-12 23:23:31 +01001008 idx3 = findoption((char_u *)"shcf");
1009 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1010 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001011 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001012 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1013 }
1014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001017 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001018 {
1019 int idx_ffs = findoption((char_u *)"ffs");
1020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001021 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001022 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1023 set_fileformat(default_fileformat(), OPT_LOCAL);
1024 }
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#ifdef FEAT_TITLE
1027 set_title_defaults();
1028#endif
1029}
1030
1031#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1032/*
1033 * When 'helplang' is still at its default value, set it to "lang".
1034 * Only the first two characters of "lang" are used.
1035 */
1036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001037set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 int idx;
1040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001041 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 return;
1043 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001044 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 if (options[idx].flags & P_ALLOCED)
1047 free_string_option(p_hlg);
1048 p_hlg = vim_strsave(lang);
1049 if (p_hlg == NULL)
1050 p_hlg = empty_option;
1051 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001052 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001053 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001054 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1055 {
1056 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1057 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1058 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001059 // any C like setting, such as C.UTF-8, becomes "en"
1060 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1061 {
1062 p_hlg[0] = 'e';
1063 p_hlg[1] = 'n';
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 options[idx].flags |= P_ALLOCED;
1068 }
1069}
1070#endif
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#ifdef FEAT_TITLE
1073/*
1074 * 'title' and 'icon' only default to true if they have not been set or reset
1075 * in .vimrc and we can read the old value.
1076 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1077 * they can be reset. This reduces startup time when using X on a remote
1078 * machine.
1079 */
1080 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001081set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 int idx1;
1084 long val;
1085
1086 /*
1087 * If GUI is (going to be) used, we can always set the window title and
1088 * icon name. Saves a bit of time, because the X11 display server does
1089 * not need to be contacted.
1090 */
1091 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001092 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
1094#ifdef FEAT_GUI
1095 if (gui.starting || gui.in_use)
1096 val = TRUE;
1097 else
1098#endif
1099 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001100 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 p_title = val;
1102 }
1103 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001104 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106#ifdef FEAT_GUI
1107 if (gui.starting || gui.in_use)
1108 val = TRUE;
1109 else
1110#endif
1111 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001112 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 p_icon = val;
1114 }
1115}
1116#endif
1117
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001118 void
1119ex_set(exarg_T *eap)
1120{
1121 int flags = 0;
1122
1123 if (eap->cmdidx == CMD_setlocal)
1124 flags = OPT_LOCAL;
1125 else if (eap->cmdidx == CMD_setglobal)
1126 flags = OPT_GLOBAL;
1127#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001128 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001129 ex_options(eap);
1130 else
1131#endif
1132 {
1133 if (eap->forceit)
1134 flags |= OPT_ONECOLUMN;
1135 (void)do_set(eap->arg, flags);
1136 }
1137}
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139/*
1140 * Parse 'arg' for option settings.
1141 *
1142 * 'arg' may be IObuff, but only when no errors can be present and option
1143 * does not need to be expanded with option_expand().
1144 * "opt_flags":
1145 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001146 * OPT_GLOBAL for ":setglobal"
1147 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001149 * OPT_WINONLY to only set window-local options
1150 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 *
1152 * returns FAIL if an error is detected, OK otherwise
1153 */
1154 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001157 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001160 char *errmsg;
1161 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001163 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1164 int nextchar; // next non-white char after option name
1165 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 int len;
1167 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001168 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 long_u flags; // flags for current option
1171 char_u *varp = NULL; // pointer to variable for current option
1172 int did_show = FALSE; // already showed one value
1173 int adding; // "opt+=arg"
1174 int prepending; // "opt^=arg"
1175 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int cp_val = 0;
1177 char_u key_name[2];
1178
1179 if (*arg == NUL)
1180 {
1181 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001182 did_show = TRUE;
1183 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001186 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
1188 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001191 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1192 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 /*
1195 * ":set all" show all options.
1196 * ":set all&" set all options to their default value.
1197 */
1198 arg += 3;
1199 if (*arg == '&')
1200 {
1201 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001202 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001204 didset_options();
1205 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001206 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001211 did_show = TRUE;
1212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001214 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
1216 showoptions(2, opt_flags);
1217 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001218 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 arg += 7;
1220 }
1221 else
1222 {
1223 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001224 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 prefix = 0;
1227 arg += 2;
1228 }
1229 else if (STRNCMP(arg, "inv", 3) == 0)
1230 {
1231 prefix = 2;
1232 arg += 3;
1233 }
1234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 key = 0;
1237 if (*arg == '<')
1238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1242 len = 5;
1243 else
1244 {
1245 len = 1;
1246 while (arg[len] != NUL && arg[len] != '>')
1247 ++len;
1248 }
1249 if (arg[len] != '>')
1250 {
1251 errmsg = e_invarg;
1252 goto skip;
1253 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 arg[len] = NUL; // put NUL after name
1255 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001259 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 else
1262 {
1263 len = 0;
1264 /*
1265 * The two characters after "t_" may not be alphanumeric.
1266 */
1267 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1268 len = 4;
1269 else
1270 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1271 ++len;
1272 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001273 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001277 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001280 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 afterchar = arg[len];
1282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001284 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 ++len;
1286
1287 adding = FALSE;
1288 prepending = FALSE;
1289 removing = FALSE;
1290 if (arg[len] != NUL && arg[len + 1] == '=')
1291 {
1292 if (arg[len] == '+')
1293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001294 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ++len;
1296 }
1297 else if (arg[len] == '^')
1298 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001299 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 ++len;
1301 }
1302 else if (arg[len] == '-')
1303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001304 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ++len;
1306 }
1307 }
1308 nextchar = arg[len];
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001312 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 goto skip;
1314 }
1315
1316 if (opt_idx >= 0)
1317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001318 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 // Only give an error message when requesting the value of
1321 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1323 && (!(options[opt_idx].flags & P_BOOL)
1324 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001325 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 goto skip;
1327 }
1328
1329 flags = options[opt_idx].flags;
1330 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1331 }
1332 else
1333 {
1334 flags = P_STRING;
1335 if (key < 0)
1336 {
1337 key_name[0] = KEY2TERMCAP0(key);
1338 key_name[1] = KEY2TERMCAP1(key);
1339 }
1340 else
1341 {
1342 key_name[0] = KS_KEY;
1343 key_name[1] = (key & 0xff);
1344 }
1345 }
1346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001347 // Skip all options that are not window-local (used when showing
1348 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001349 if ((opt_flags & OPT_WINONLY)
1350 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1351 goto skip;
1352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001353 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001354 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1355 && options[opt_idx].var == VAR_WIN)
1356 goto skip;
1357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001358 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001359 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001361 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001362 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001363 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001364 goto skip;
1365 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001366 if ((flags & P_MLE) && !p_mle)
1367 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001368 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001369 goto skip;
1370 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001371#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001372 // In diff mode some options are overruled. This avoids that
1373 // 'foldmethod' becomes "marker" instead of "diff" and that
1374 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001375 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001376 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001377 && (
1378#ifdef FEAT_FOLDING
1379 options[opt_idx].indir == PV_FDM ||
1380#endif
1381 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001382 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
1386#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001387 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001390 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 goto skip;
1392 }
1393#endif
1394
1395 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1396 {
1397 arg += len;
1398 cp_val = p_cp;
1399 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1400 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
1403 cp_val = FALSE;
1404 arg += 3;
1405 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
1408 cp_val = TRUE;
1409 arg += 2;
1410 }
1411 }
1412 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001413 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
1415 errmsg = e_trailing;
1416 goto skip;
1417 }
1418 }
1419
1420 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001421 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001422 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
1424 if (nextchar == '?'
1425 || (prefix == 1
1426 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1427 && !(flags & P_BOOL)))
1428 {
1429 /*
1430 * print value
1431 */
1432 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001433 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else
1435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 gotocmdline(TRUE); // cursor at status line
1437 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 if (opt_idx >= 0)
1440 {
1441 showoneopt(&options[opt_idx], opt_flags);
1442#ifdef FEAT_EVAL
1443 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001445 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001446 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001447 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001448 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001449 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001450 (int)options[opt_idx].indir & PV_MASK]);
1451 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001452 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001453 (int)options[opt_idx].indir & PV_MASK]);
1454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
1456 }
1457 else
1458 {
1459 char_u *p;
1460
1461 p = find_termcode(key_name);
1462 if (p == NULL)
1463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001464 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 goto skip;
1466 }
1467 else
1468 (void)show_one_termcode(key_name, p, TRUE);
1469 }
1470 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001471 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 errmsg = e_trailing;
1473 }
1474 else
1475 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001476 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001477 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001479 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 if (nextchar == '=' || nextchar == ':')
1482 {
1483 errmsg = e_invarg;
1484 goto skip;
1485 }
1486
1487 /*
1488 * ":set opt!": invert
1489 * ":set opt&": reset to default value
1490 * ":set opt<": reset to global value
1491 */
1492 if (nextchar == '!')
1493 value = *(int *)(varp) ^ 1;
1494 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001495 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 ((flags & P_VI_DEF) || cp_val)
1497 ? VI_DEFAULT : VIM_DEFAULT];
1498 else if (nextchar == '<')
1499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001500 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if ((int *)varp == &curbuf->b_p_ar
1502 && opt_flags == OPT_LOCAL)
1503 value = -1;
1504 else
1505 value = *(int *)get_varp_scope(&(options[opt_idx]),
1506 OPT_GLOBAL);
1507 }
1508 else
1509 {
1510 /*
1511 * ":set invopt": invert
1512 * ":set opt" or ":set noopt": set or reset
1513 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001514 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
1516 errmsg = e_trailing;
1517 goto skip;
1518 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001519 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 value = *(int *)(varp) ^ 1;
1521 else
1522 value = prefix;
1523 }
1524
1525 errmsg = set_bool_option(opt_idx, varp, (int)value,
1526 opt_flags);
1527 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1531 || prefix != 1)
1532 {
1533 errmsg = e_invarg;
1534 goto skip;
1535 }
1536
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001537 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
1539 /*
1540 * Different ways to set a number option:
1541 * & set to default value
1542 * < set to global value
1543 * <xx> accept special key codes for 'wildchar'
1544 * c accept any non-digit for 'wildchar'
1545 * [-]0-9 set number
1546 * other error
1547 */
1548 ++arg;
1549 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001550 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 ((flags & P_VI_DEF) || cp_val)
1552 ? VI_DEFAULT : VIM_DEFAULT];
1553 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001554 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001555 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1556 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001557 if ((long *)varp == &curbuf->b_p_ul
1558 && opt_flags == OPT_LOCAL)
1559 value = NO_LOCAL_UNDOLEVEL;
1560 else
1561 value = *(long *)get_varp_scope(
1562 &(options[opt_idx]), OPT_GLOBAL);
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 else if (((long *)varp == &p_wc
1565 || (long *)varp == &p_wcm)
1566 && (*arg == '<'
1567 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001568 || (*arg != NUL
1569 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 && !VIM_ISDIGIT(*arg))))
1571 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001572 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (value == 0 && (long *)varp != &p_wcm)
1574 {
1575 errmsg = e_invarg;
1576 goto skip;
1577 }
1578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1580 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581 // Allow negative (for 'undolevels'), octal and
1582 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001583 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001584 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001585 if (i == 0 || (arg[i] != NUL
1586 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001588 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 goto skip;
1590 }
1591 }
1592 else
1593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 goto skip;
1596 }
1597
1598 if (adding)
1599 value = *(long *)varp + value;
1600 if (prepending)
1601 value = *(long *)varp * value;
1602 if (removing)
1603 value = *(long *)varp - value;
1604 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001605 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001609 char_u *save_arg = NULL;
1610 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001611 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001612 char_u *newval;
1613 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001614 char_u *origval_l = NULL;
1615 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001616#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001617 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001618 char_u *saved_origval_l = NULL;
1619 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001620 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001621#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001622 unsigned newlen;
1623 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001624 int new_value_alloced; // new string option
1625 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 // When using ":set opt=val" for a global option
1628 // with a local value the local value will be
1629 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001631 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 varp = options[opt_idx].var;
1633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 // The old value is kept until we are sure that the
1635 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001637
Bram Moolenaard7c96872019-06-15 17:12:48 +02001638 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1639 {
1640 origval_l = *(char_u **)get_varp_scope(
1641 &(options[opt_idx]), OPT_LOCAL);
1642 origval_g = *(char_u **)get_varp_scope(
1643 &(options[opt_idx]), OPT_GLOBAL);
1644
1645 // A global-local string option might have an empty
1646 // option as value to indicate that the global
1647 // value should be used.
1648 if (((int)options[opt_idx].indir & PV_BOTH)
1649 && origval_l == empty_option)
1650 origval_l = origval_g;
1651 }
1652
1653 // When setting the local value of a global
1654 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001655 if (((int)options[opt_idx].indir & PV_BOTH)
1656 && (opt_flags & OPT_LOCAL))
1657 origval = *(char_u **)get_varp(
1658 &options[opt_idx]);
1659 else
1660 origval = oldval;
1661
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001662 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 newval = options[opt_idx].def_val[
1665 ((flags & P_VI_DEF) || cp_val)
1666 ? VI_DEFAULT : VIM_DEFAULT];
1667 if ((char_u **)varp == &p_bg)
1668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001669 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#ifdef FEAT_GUI
1671 if (gui.in_use)
1672 newval = gui_bg_default();
1673 else
1674#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001675 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001678 // expand environment variables and ~ (since the
1679 // default value was already expanded, only
1680 // required when an environment variable was set
1681 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (newval == NULL)
1683 newval = empty_option;
1684 else
1685 {
1686 s = option_expand(opt_idx, newval);
1687 if (s == NULL)
1688 s = newval;
1689 newval = vim_strsave(s);
1690 }
1691 new_value_alloced = TRUE;
1692 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001693 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 newval = vim_strsave(*(char_u **)get_varp_scope(
1696 &(options[opt_idx]), OPT_GLOBAL));
1697 new_value_alloced = TRUE;
1698 }
1699 else
1700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001701 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 /*
1704 * Set 'keywordprg' to ":help" if an empty
1705 * value was passed to :set by the user.
1706 * Misuse errbuf[] for the resulting string.
1707 */
1708 if (varp == (char_u *)&p_kp
1709 && (*arg == NUL || *arg == ' '))
1710 {
1711 STRCPY(errbuf, ":help");
1712 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001713 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001716 * Convert 'backspace' number to string, for
1717 * adding, prepending and removing string.
1718 */
1719 else if (varp == (char_u *)&p_bs
1720 && VIM_ISDIGIT(**(char_u **)varp))
1721 {
1722 i = getdigits((char_u **)varp);
1723 switch (i)
1724 {
1725 case 0:
1726 *(char_u **)varp = empty_option;
1727 break;
1728 case 1:
1729 *(char_u **)varp = vim_strsave(
1730 (char_u *)"indent,eol");
1731 break;
1732 case 2:
1733 *(char_u **)varp = vim_strsave(
1734 (char_u *)"indent,eol,start");
1735 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001736 case 3:
1737 *(char_u **)varp = vim_strsave(
1738 (char_u *)"indent,eol,nostop");
1739 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001740 }
1741 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001742 if (origval == oldval)
1743 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001744 if (origval_l == oldval)
1745 origval_l = *(char_u **)varp;
1746 if (origval_g == oldval)
1747 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001748 oldval = *(char_u **)varp;
1749 }
1750 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * Convert 'whichwrap' number to string, for
1752 * backwards compatibility with Vim 3.0.
1753 * Misuse errbuf[] for the resulting string.
1754 */
1755 else if (varp == (char_u *)&p_ww
1756 && VIM_ISDIGIT(*arg))
1757 {
1758 *errbuf = NUL;
1759 i = getdigits(&arg);
1760 if (i & 1)
1761 STRCAT(errbuf, "b,");
1762 if (i & 2)
1763 STRCAT(errbuf, "s,");
1764 if (i & 4)
1765 STRCAT(errbuf, "h,l,");
1766 if (i & 8)
1767 STRCAT(errbuf, "<,>,");
1768 if (i & 16)
1769 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001770 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 errbuf[STRLEN(errbuf) - 1] = NUL;
1772 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001773 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775 /*
1776 * Remove '>' before 'dir' and 'bdir', for
1777 * backwards compatibility with version 3.0
1778 */
1779 else if ( *arg == '>'
1780 && (varp == (char_u *)&p_dir
1781 || varp == (char_u *)&p_bdir))
1782 {
1783 ++arg;
1784 }
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
1787 * Copy the new string into allocated memory.
1788 * Can't use set_string_option_direct(), because
1789 * we need to remove the backslashes.
1790 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001791 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 newlen = (unsigned)STRLEN(arg) + 1;
1793 if (adding || prepending || removing)
1794 newlen += (unsigned)STRLEN(origval) + 1;
1795 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001796 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 break;
1798 s = newval;
1799
1800 /*
1801 * Copy the string, skip over escaped chars.
1802 * For MS-DOS and WIN32 backslashes before normal
1803 * file name characters are not removed, and keep
1804 * backslash at start, for "\\machine\path", but
1805 * do remove it for "\\\\machine\\path".
1806 * The reverse is found in ExpandOldSetting().
1807 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001808 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 if (*arg == '\\' && arg[1] != NUL
1811#ifdef BACKSLASH_IN_FILENAME
1812 && !((flags & P_EXPAND)
1813 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001814 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 && (arg[1] != '\\'
1816 || (s == newval
1817 && arg[2] != '\\')))
1818#endif
1819 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001822 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001824 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 mch_memmove(s, arg, (size_t)i);
1826 arg += i;
1827 s += i;
1828 }
1829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 *s++ = *arg++;
1831 }
1832 *s = NUL;
1833
1834 /*
1835 * Expand environment variables and ~.
1836 * Don't do it when adding without inserting a
1837 * comma.
1838 */
1839 if (!(adding || prepending || removing)
1840 || (flags & P_COMMA))
1841 {
1842 s = option_expand(opt_idx, newval);
1843 if (s != NULL)
1844 {
1845 vim_free(newval);
1846 newlen = (unsigned)STRLEN(s) + 1;
1847 if (adding || prepending || removing)
1848 newlen += (unsigned)STRLEN(origval) + 1;
1849 newval = alloc(newlen);
1850 if (newval == NULL)
1851 break;
1852 STRCPY(newval, s);
1853 }
1854 }
1855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001856 // locate newval[] in origval[] when removing it
1857 // and when adding to avoid duplicates
1858 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (removing || (flags & P_NODUP))
1860 {
1861 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001862 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001865 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
1867 prepending = FALSE;
1868 adding = FALSE;
1869 STRCPY(newval, origval);
1870 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001871
1872 // if no duplicate, move pointer to end of
1873 // original value
1874 if (s == NULL)
1875 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001878 // concatenate the two strings; add a ',' if
1879 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (adding || prepending)
1881 {
1882 comma = ((flags & P_COMMA) && *origval != NUL
1883 && *newval != NUL);
1884 if (adding)
1885 {
1886 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001887 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001888 if (comma && i > 1
1889 && (flags & P_ONECOMMA) == P_ONECOMMA
1890 && origval[i - 1] == ','
1891 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001892 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 mch_memmove(newval + i + comma, newval,
1894 STRLEN(newval) + 1);
1895 mch_memmove(newval, origval, (size_t)i);
1896 }
1897 else
1898 {
1899 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001900 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 if (comma)
1903 newval[i] = ',';
1904 }
1905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001906 // Remove newval[] from origval[]. (Note: "i" has
1907 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (removing)
1909 {
1910 STRCPY(newval, origval);
1911 if (*s)
1912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001913 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (flags & P_COMMA)
1915 {
1916 if (s == origval)
1917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (s[i] == ',')
1920 ++i;
1921 }
1922 else
1923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001924 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 --s;
1926 ++i;
1927 }
1928 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001929 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 }
1932
1933 if (flags & P_FLAGLIST)
1934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001935 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001936 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001938 // if options have P_FLAGLIST and
1939 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001940 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001942 if (*s != ',' && *(s + 1) == ','
1943 && vim_strchr(s + 2, *s) != NULL)
1944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 // Remove the duplicated value and
1946 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001947 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001948 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001951 else
1952 {
1953 if ((!(flags & P_COMMA) || *s != ',')
1954 && vim_strchr(s + 1, *s) != NULL)
1955 {
1956 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001957 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001958 }
1959 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001960 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 arg = save_arg;
1966 new_value_alloced = TRUE;
1967 }
1968
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001969 /*
1970 * Set the new value.
1971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 *(char_u **)(varp) = newval;
1973
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001974#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001975 if (!starting
1976# ifdef FEAT_CRYPT
1977 && options[opt_idx].indir != PV_KEY
1978# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001979 && origval != NULL && newval != NULL)
1980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001981 // origval may be freed by
1982 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02001983 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // newval (and varp) may become invalid if the
1985 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001986 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001987 if (origval_l != NULL)
1988 saved_origval_l = vim_strsave(origval_l);
1989 if (origval_g != NULL)
1990 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001991 }
Bram Moolenaar53744302015-07-17 17:38:22 +02001992#endif
1993
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001994 {
1995 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01001996 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001997
1998 // When an option is set in the sandbox, from a
1999 // modeline or in secure mode, then deal with side
2000 // effects in secure mode. Also when the value was
2001 // set with the P_INSECURE flag and is not
2002 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002003 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002004#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002005 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002006#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002007 || (!value_is_replaced && (*p & P_INSECURE)))
2008 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002009
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002010 // Handle side effects, and set the global value
2011 // for ":set" on local options. Note: when setting
2012 // 'syntax' or 'filetype' autocommands may be
2013 // triggered that can cause havoc.
2014 errmsg = did_set_string_option(
2015 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002016 new_value_alloced, oldval, errbuf,
2017 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002018
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002019 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002022#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002023 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002024 trigger_optionsset_string(
2025 opt_idx, opt_flags, saved_origval,
2026 saved_origval_l, saved_origval_g,
2027 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002028 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002029 vim_free(saved_origval_l);
2030 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002031 vim_free(saved_newval);
2032#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002033 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (errmsg != NULL)
2035 goto skip;
2036 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 char_u *p;
2040
2041 if (nextchar == '&')
2042 {
2043 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002044 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 else
2047 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002048 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002049 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (*p == '\\' && p[1] != NUL)
2051 ++p;
2052 nextchar = *p;
2053 *p = NUL;
2054 add_termcode(key_name, arg, FALSE);
2055 *p = nextchar;
2056 }
2057 if (full_screen)
2058 ttest(FALSE);
2059 redraw_all_later(CLEAR);
2060 }
2061 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002064 did_set_option(
2065 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067
2068skip:
2069 /*
2070 * Advance to next argument.
2071 * - skip until a blank found, taking care of backslashes
2072 * - skip blanks
2073 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2074 */
2075 for (i = 0; i < 2 ; ++i)
2076 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002077 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (*arg++ == '\\' && *arg != NUL)
2079 ++arg;
2080 arg = skipwhite(arg);
2081 if (*arg != '=')
2082 break;
2083 }
2084 }
2085
2086 if (errmsg != NULL)
2087 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002088 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002089 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (i + (arg - startarg) < IOSIZE)
2091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 STRCAT(IObuff, ": ");
2094 mch_memmove(IObuff + i, startarg, (arg - startarg));
2095 IObuff[i + (arg - startarg)] = NUL;
2096 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002097 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 trans_characters(IObuff, IOSIZE);
2099
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 ++no_wait_return; // wait_return done later
2101 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 --no_wait_return;
2103
2104 return FAIL;
2105 }
2106
2107 arg = skipwhite(arg);
2108 }
2109
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110theend:
2111 if (silent_mode && did_show)
2112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002113 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002114 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002115 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002116 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002117 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002118 out_flush();
2119 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002120 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002121 }
2122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 return OK;
2124}
2125
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002126/*
2127 * Call this when an option has been given a new value through a user command.
2128 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2129 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131did_set_option(
2132 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002133 int opt_flags, // possibly with OPT_MODELINE
2134 int new_value, // value was replaced completely
2135 int value_checked) // value was checked to be safe, no need to set the
2136 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002137{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002138 long_u *p;
2139
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002140 options[opt_idx].flags |= P_WAS_SET;
2141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002142 // When an option is set in the sandbox, from a modeline or in secure mode
2143 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2144 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002145 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002146 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002147#ifdef HAVE_SANDBOX
2148 || sandbox != 0
2149#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002150 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002151 *p = *p | P_INSECURE;
2152 else if (new_value)
2153 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002154}
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156/*
2157 * Convert a key name or string into a key value.
2158 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002159 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002161 int
2162string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
2164 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002165 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (*arg == '^')
2167 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002168 if (multi_byte)
2169 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 return *arg;
2171}
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#ifdef FEAT_TITLE
2174/*
2175 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2176 * maketitle() to create and display it.
2177 * When switching the title or icon off, call mch_restore_title() to get
2178 * the old value back.
2179 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002180 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002181did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 if (starting != NO_SCREEN
2184#ifdef FEAT_GUI
2185 && !gui.starting
2186#endif
2187 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189}
2190#endif
2191
2192/*
2193 * set_options_bin - called when 'bin' changes value.
2194 */
2195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002196set_options_bin(
2197 int oldval,
2198 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 /*
2202 * The option values that are changed when 'bin' changes are
2203 * copied when 'bin is set and restored when 'bin' is reset.
2204 */
2205 if (newval)
2206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
2209 if (!(opt_flags & OPT_GLOBAL))
2210 {
2211 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2212 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2213 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2214 curbuf->b_p_et_nobin = curbuf->b_p_et;
2215 }
2216 if (!(opt_flags & OPT_LOCAL))
2217 {
2218 p_tw_nobin = p_tw;
2219 p_wm_nobin = p_wm;
2220 p_ml_nobin = p_ml;
2221 p_et_nobin = p_et;
2222 }
2223 }
2224
2225 if (!(opt_flags & OPT_GLOBAL))
2226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002227 curbuf->b_p_tw = 0; // no automatic line wrap
2228 curbuf->b_p_wm = 0; // no automatic line wrap
2229 curbuf->b_p_ml = 0; // no modelines
2230 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232 if (!(opt_flags & OPT_LOCAL))
2233 {
2234 p_tw = 0;
2235 p_wm = 0;
2236 p_ml = FALSE;
2237 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
2240 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 if (!(opt_flags & OPT_GLOBAL))
2244 {
2245 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2246 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2247 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2248 curbuf->b_p_et = curbuf->b_p_et_nobin;
2249 }
2250 if (!(opt_flags & OPT_LOCAL))
2251 {
2252 p_tw = p_tw_nobin;
2253 p_wm = p_wm_nobin;
2254 p_ml = p_ml_nobin;
2255 p_et = p_et_nobin;
2256 }
2257 }
2258}
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260/*
2261 * Expand environment variables for some string options.
2262 * These string options cannot be indirect!
2263 * If "val" is NULL expand the current value of the option.
2264 * Return pointer to NameBuff, or NULL when not expanded.
2265 */
2266 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002267option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2271 return NULL;
2272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 // If val is longer than MAXPATHL no meaningful expansion can be done,
2274 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (val != NULL && STRLEN(val) > MAXPATHL)
2276 return NULL;
2277
2278 if (val == NULL)
2279 val = *(char_u **)options[opt_idx].var;
2280
2281 /*
2282 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2283 * Escape spaces when expanding 'tags', they are used to separate file
2284 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002285 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 */
2287 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002288 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002289#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002290 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2291#endif
2292 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return NULL;
2295
2296 return NameBuff;
2297}
2298
2299/*
2300 * After setting various option values: recompute variables that depend on
2301 * option values.
2302 */
2303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 (void)init_chartab();
2308
Bram Moolenaardac13472019-09-16 21:06:21 +02002309 didset_string_options();
2310
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002311#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002312 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002313 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002314 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002315 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002318 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 (void)check_cedit();
2320#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002321#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002323 fill_breakat_flags();
2324#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002325 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002326}
2327
2328/*
2329 * More side effects of setting options.
2330 */
2331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002332didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002333{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002334 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002335 (void)highlight_changed();
2336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002338 check_opt_wim();
2339
2340 (void)set_chars_option(&p_lcs);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002341 // Parse default for 'fillchars'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002342 (void)set_chars_option(&p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002343
2344#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002345 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002346 (void)check_clipboard_option();
2347#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002348#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002349 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002350 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002351 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002352 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354}
2355
2356/*
2357 * Check for string options that are NULL (normally only termcap options).
2358 */
2359 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002360check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 int opt_idx;
2363
2364 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2365 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2366 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2367}
2368
2369/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002370 * Return the option index found by a pointer into term_strings[].
2371 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002373 int
2374get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002376 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2379 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002380 return opt_idx;
2381 return -1; // cannot happen: didn't find it!
2382}
2383
2384/*
2385 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2386 * Return the option index or -1 if not found.
2387 */
2388 int
2389set_term_option_alloced(char_u **p)
2390{
2391 int opt_idx = get_term_opt_idx(p);
2392
2393 if (opt_idx >= 0)
2394 options[opt_idx].flags |= P_ALLOCED;
2395 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396}
2397
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002398#if defined(FEAT_EVAL) || defined(PROTO)
2399/*
2400 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2401 * Return FALSE when it wasn't.
2402 * Return -1 for an unknown option.
2403 */
2404 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002405was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002406{
2407 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002408 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002409
2410 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002411 {
2412 flagp = insecure_flag(idx, opt_flags);
2413 return (*flagp & P_INSECURE) != 0;
2414 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002415 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002416 return -1;
2417}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002418
2419/*
2420 * Get a pointer to the flags used for the P_INSECURE flag of option
2421 * "opt_idx". For some local options a local flags field is used.
2422 */
2423 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002424insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002425{
2426 if (opt_flags & OPT_LOCAL)
2427 switch ((int)options[opt_idx].indir)
2428 {
2429#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002430 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002431#endif
2432#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002433# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002434 case PV_FDE: return &curwin->w_p_fde_flags;
2435 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002436# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002437# ifdef FEAT_BEVAL
2438 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2439# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002440# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002441 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002442# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002443 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002444# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002445 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002446# endif
2447#endif
2448 }
2449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002450 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002451 return &options[opt_idx].flags;
2452}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002453#endif
2454
Bram Moolenaardac13472019-09-16 21:06:21 +02002455#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002456/*
2457 * Redraw the window title and/or tab page text later.
2458 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002459void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002460{
2461 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002462 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002463}
2464#endif
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002467 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2468 * characters or characters in "allowed".
2469 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002470 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002471valid_name(char_u *val, char *allowed)
2472{
2473 char_u *s;
2474
2475 for (s = val; *s != NUL; ++s)
2476 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2477 return FALSE;
2478 return TRUE;
2479}
2480
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002481#if defined(FEAT_EVAL) || defined(PROTO)
2482/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002483 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002484 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002485 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002486 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002487set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002488{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002489 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2490 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002491 sctx_T new_script_ctx = script_ctx;
2492
Bram Moolenaar51258742020-05-03 17:19:33 +02002493 // Modeline already has the line number set.
2494 if (!(opt_flags & OPT_MODELINE))
2495 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002497 // Remember where the option was set. For local options need to do that
2498 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002499 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002500 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002501 if (both || (opt_flags & OPT_LOCAL))
2502 {
2503 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002504 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002505 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002506 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002507 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002508}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002509
2510/*
2511 * Set the script_ctx for a termcap option.
2512 * "name" must be the two character code, e.g. "RV".
2513 * When "name" is NULL use "opt_idx".
2514 */
2515 void
2516set_term_option_sctx_idx(char *name, int opt_idx)
2517{
2518 char_u buf[5];
2519 int idx;
2520
2521 if (name == NULL)
2522 idx = opt_idx;
2523 else
2524 {
2525 buf[0] = 't';
2526 buf[1] = '_';
2527 buf[2] = name[0];
2528 buf[3] = name[1];
2529 buf[4] = 0;
2530 idx = findoption(buf);
2531 }
2532 if (idx >= 0)
2533 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2534}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535#endif
2536
Bram Moolenaarf4140482020-02-15 23:06:45 +01002537#if defined(FEAT_EVAL)
2538/*
2539 * Apply the OptionSet autocommand.
2540 */
2541 static void
2542apply_optionset_autocmd(
2543 int opt_idx,
2544 long opt_flags,
2545 long oldval,
2546 long oldval_g,
2547 long newval,
2548 char *errmsg)
2549{
2550 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2551
2552 // Don't do this while starting up, failure or recursively.
2553 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2554 return;
2555
2556 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2557 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2558 oldval_g);
2559 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2560 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2561 (opt_flags & OPT_LOCAL) ? "local" : "global");
2562 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2563 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2564 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2565 if (opt_flags & OPT_LOCAL)
2566 {
2567 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2568 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2569 }
2570 if (opt_flags & OPT_GLOBAL)
2571 {
2572 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2573 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2574 }
2575 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2576 {
2577 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2578 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2579 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2580 }
2581 if (opt_flags & OPT_MODELINE)
2582 {
2583 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2584 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2585 }
2586 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2587 NULL, FALSE, NULL);
2588 reset_v_option_vars();
2589}
2590#endif
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592/*
2593 * Set the value of a boolean option, and take care of side effects.
2594 * Returns NULL for success, or an error message for an error.
2595 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002596 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002597set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002598 int opt_idx, // index in options[] table
2599 char_u *varp, // pointer to the option variable
2600 int value, // new value
2601 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002604#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002605 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002608 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if ((secure
2610#ifdef HAVE_SANDBOX
2611 || sandbox != 0
2612#endif
2613 ) && (options[opt_idx].flags & P_SECURE))
2614 return e_secure;
2615
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002616#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002617 // Save the global value before changing anything. This is needed as for
2618 // a global-only option setting the "local value" in fact sets the global
2619 // value (since there is only one value).
2620 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2621 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2622 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002623#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002627 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002628 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#endif
2630
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002631#ifdef FEAT_GUI
2632 need_mouse_correct = TRUE;
2633#endif
2634
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2637 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2638
2639 /*
2640 * Handle side effects of changing a bool option.
2641 */
2642
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002643 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaar920694c2016-08-21 17:45:02 +02002647#ifdef FEAT_LANGMAP
2648 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002649 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002650 p_lnr = !p_lrm;
2651 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002652 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002653 p_lrm = !p_lnr;
2654#endif
2655
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002656#ifdef FEAT_SYN_HL
2657 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2658 reset_cursorline();
2659#endif
2660
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002661#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002662 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002663 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002665 // Only take action when the option was set. When reset we do not
2666 // delete the undo file, the option may be set again without making
2667 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002668 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002669 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002670 char_u hash[UNDO_HASH_SIZE];
2671 buf_T *save_curbuf = curbuf;
2672
Bram Moolenaar29323592016-07-24 22:04:11 +02002673 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002674 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002675 // When 'undofile' is set globally: for every buffer, otherwise
2676 // only for the current buffer: Try to read in the undofile,
2677 // if one exists, the buffer wasn't changed and the buffer was
2678 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002679 if ((curbuf == save_curbuf
2680 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2681 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2682 {
2683 u_compute_hash(hash);
2684 u_read_undo(NULL, hash, curbuf->b_fname);
2685 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002686 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002687 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002688 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002689 }
2690#endif
2691
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 else if ((int *)varp == &curbuf->b_p_ro)
2693 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002694 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2696 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002697
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002699 if (curbuf->b_p_ro)
2700 curbuf->b_did_warn = FALSE;
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002703 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#endif
2705 }
2706
Bram Moolenaar9c449722010-07-20 18:44:27 +02002707#ifdef FEAT_GUI
2708 else if ((int *)varp == &p_mh)
2709 {
2710 if (!p_mh)
2711 gui_mch_mousehide(FALSE);
2712 }
2713#endif
2714
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002717 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002718# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002720 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2721 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002722 {
2723 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002724 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002725 }
2726# endif
2727# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002728 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002729# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002730 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002731#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002734 {
2735 redraw_titles();
2736 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002738 else if ((int *)varp == &curbuf->b_p_fixeol)
2739 {
2740 redraw_titles();
2741 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002742 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002743 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002744 {
2745 redraw_titles();
2746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#endif
2748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 else if ((int *)varp == &curbuf->b_p_bin)
2751 {
2752 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2753#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002754 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#endif
2756 }
2757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2760 {
2761 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2762 NULL, NULL, TRUE, curbuf);
2763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else if ((int *)varp == &curbuf->b_p_swf)
2767 {
2768 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2772 // buf->b_p_swf
2773 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002776 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 else if ((int *)varp == &p_terse)
2778 {
2779 char_u *p;
2780
2781 p = vim_strchr(p_shm, SHM_SEARCH);
2782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 if (p_terse && p == NULL)
2785 {
2786 STRCPY(IObuff, p_shm);
2787 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002788 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002792 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else if ((int *)varp == &p_paste)
2797 {
2798 paste_option_changed();
2799 }
2800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else if ((int *)varp == &p_im)
2803 {
2804 if (p_im)
2805 {
2806 if ((State & INSERT) == 0)
2807 need_start_insertmode = TRUE;
2808 stop_insert_mode = FALSE;
2809 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002810 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002811 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
2813 need_start_insertmode = FALSE;
2814 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002815 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 restart_edit = 0;
2818 }
2819 }
2820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002821 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 else if ((int *)varp == &p_ic && p_hls)
2823 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002824 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826
2827#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 else if ((int *)varp == &p_hls)
2830 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002831 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
2833#endif
2834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2836 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 else if ((int *)varp == &curwin->w_p_scb)
2838 {
2839 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002842 curwin->w_scbind_pos = curwin->w_topline;
2843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaar4033c552017-09-16 20:54:51 +02002846#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 else if ((int *)varp == &curwin->w_p_pvw)
2849 {
2850 if (curwin->w_p_pvw)
2851 {
2852 win_T *win;
2853
Bram Moolenaar29323592016-07-24 22:04:11 +02002854 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (win->w_p_pvw && win != curwin)
2856 {
2857 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002858 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860 }
2861 }
2862#endif
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 else if ((int *)varp == &curbuf->b_p_tx)
2866 {
2867 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2868 }
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 set_string_option_direct((char_u *)"ffs", -1,
2874 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002875 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878 /*
2879 * When 'lisp' option changes include/exclude '-' in
2880 * keyword characters.
2881 */
2882#ifdef FEAT_LISP
2883 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2884 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
2887#endif
2888
2889#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002891 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002893 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
2895#endif
2896
2897 else if ((int *)varp == &curbuf->b_changed)
2898 {
2899 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002902 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
2907#ifdef BACKSLASH_IN_FILENAME
2908 else if ((int *)varp == &p_ssl)
2909 {
2910 if (p_ssl)
2911 {
2912 psepc = '/';
2913 psepcN = '\\';
2914 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916 else
2917 {
2918 psepc = '\\';
2919 psepcN = '/';
2920 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 buflist_slash_adjust();
2925 alist_slash_adjust();
2926# ifdef FEAT_EVAL
2927 scriptnames_slash_adjust();
2928# endif
2929 }
2930#endif
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 else if ((int *)varp == &curwin->w_p_wrap)
2934 {
2935 if (curwin->w_p_wrap)
2936 curwin->w_leftcol = 0;
2937 }
2938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 else if ((int *)varp == &p_ea)
2940 {
2941 if (p_ea && !old_value)
2942 win_equal(curwin, FALSE, 0);
2943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945 else if ((int *)varp == &p_wiv)
2946 {
2947 /*
2948 * When 'weirdinvert' changed, set/reset 't_xs'.
2949 * Then set 'weirdinvert' according to value of 't_xs'.
2950 */
2951 if (p_wiv && !old_value)
2952 T_XS = (char_u *)"y";
2953 else if (!p_wiv && old_value)
2954 T_XS = empty_option;
2955 p_wiv = (*T_XS != NUL);
2956 }
2957
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002958#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 else if ((int *)varp == &p_beval)
2960 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002961 if (!balloonEvalForTerm)
2962 {
2963 if (p_beval && !old_value)
2964 gui_mch_enable_beval_area(balloonEval);
2965 else if (!p_beval && old_value)
2966 gui_mch_disable_beval_area(balloonEval);
2967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002969#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002970#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002971 else if ((int *)varp == &p_bevalterm)
2972 {
2973 mch_bevalterm_changed();
2974 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002977#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 else if ((int *)varp == &p_acd)
2979 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002980 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002981 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983#endif
2984
2985#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 else if ((int *)varp == &curwin->w_p_diff)
2988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002990 diff_buf_adjust(curwin);
2991# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (foldmethodIsDiff(curwin))
2993 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996#endif
2997
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002998#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 else if ((int *)varp == &p_imdisable)
3001 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003002 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (p_imdisable)
3004 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003005 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003006 // When the option is set from an autocommand, it may need to take
3007 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003008 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010#endif
3011
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003012#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003014 else if ((int *)varp == &curwin->w_p_spell)
3015 {
3016 if (curwin->w_p_spell)
3017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003018 char *errmsg = did_set_spelllang(curwin);
3019
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003020 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003021 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003022 }
3023 }
3024#endif
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026#ifdef FEAT_ARABIC
3027 if ((int *)varp == &curwin->w_p_arab)
3028 {
3029 if (curwin->w_p_arab)
3030 {
3031 /*
3032 * 'arabic' is set, handle various sub-settings.
3033 */
3034 if (!p_tbidi)
3035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 if (!curwin->w_p_rl)
3038 {
3039 curwin->w_p_rl = TRUE;
3040 changed_window_setting();
3041 }
3042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003043 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (!p_arshape)
3045 {
3046 p_arshape = TRUE;
3047 redraw_later_clear();
3048 }
3049 }
3050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003051 // Arabic requires a utf-8 encoding, inform the user if its not
3052 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003054 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003055 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3056
Bram Moolenaar8820b482017-03-16 17:23:31 +01003057 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003058 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003059#ifdef FEAT_EVAL
3060 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3061#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003064 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
3067# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3070 OPT_LOCAL);
3071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073 else
3074 {
3075 /*
3076 * 'arabic' is reset, handle various sub-settings.
3077 */
3078 if (!p_tbidi)
3079 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 if (curwin->w_p_rl)
3082 {
3083 curwin->w_p_rl = FALSE;
3084 changed_window_setting();
3085 }
3086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003087 // 'arabicshape' isn't reset, it is a global option and
3088 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // 'delcombine' isn't reset, it is a global option and another
3092 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 curbuf->b_p_iminsert = B_IMODE_NONE;
3097 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3098# endif
3099 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003100 }
3101
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003102#endif
3103
Bram Moolenaar44826212019-08-22 21:23:20 +02003104#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3105 else if (((int *)varp == &curwin->w_p_nu
3106 || (int *)varp == &curwin->w_p_rnu)
3107 && gui.in_use
3108 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3109 && curbuf->b_signlist != NULL)
3110 {
3111 // If the 'number' or 'relativenumber' options are modified and
3112 // 'signcolumn' is set to 'number', then clear the screen for a full
3113 // refresh. Otherwise the sign icons are not displayed properly in the
3114 // number column. If the 'number' option is set and only the
3115 // 'relativenumber' option is toggled, then don't refresh the screen
3116 // (optimization).
3117 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3118 redraw_all_later(CLEAR);
3119 }
3120#endif
3121
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003122#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003124 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003125 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003126# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003128 if (
3129# ifdef VIMDLL
3130 !gui.in_use && !gui.starting &&
3131# endif
3132 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003133 {
3134 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003135 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003136 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003137 if (is_term_win32())
3138 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003139# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003140# ifdef FEAT_GUI
3141 if (!gui.in_use && !gui.starting)
3142# endif
3143 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003144# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003146 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003147 {
3148 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003149 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003150 init_highlight(TRUE, FALSE);
3151 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003152# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003153 }
3154#endif
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 /*
3157 * End of handling side effects for bool options.
3158 */
3159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 options[opt_idx].flags |= P_WAS_SET;
3163
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003164#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003165 apply_optionset_autocmd(opt_idx, opt_flags,
3166 (long)(old_value ? TRUE : FALSE),
3167 (long)(old_global_value ? TRUE : FALSE),
3168 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003169#endif
3170
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003171 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003172 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003173 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003174 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 check_redraw(options[opt_idx].flags);
3176
3177 return NULL;
3178}
3179
3180/*
3181 * Set the value of a number option, and take care of side effects.
3182 * Returns NULL for success, or an error message for an error.
3183 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003184 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003185set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003186 int opt_idx, // index in options[] table
3187 char_u *varp, // pointer to the option variable
3188 long value, // new value
3189 char *errbuf, // buffer for error messages
3190 size_t errbuflen, // length of "errbuf"
3191 int opt_flags) // OPT_LOCAL, OPT_GLOBAL and
3192 // OPT_MODELINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003194 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003196#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003197 long old_global_value = 0; // only used when setting a local and
3198 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003199#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003200 long old_Rows = Rows; // remember old Rows
3201 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 long *pp = (long *)varp;
3203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003204 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003205 if ((secure
3206#ifdef HAVE_SANDBOX
3207 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003209 ) && (options[opt_idx].flags & P_SECURE))
3210 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003212#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003213 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003214 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003215 // value (since there is only one value).
3216 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003217 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3218 OPT_GLOBAL);
3219#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 *pp = value;
3222#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003223 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003224 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003226#ifdef FEAT_GUI
3227 need_mouse_correct = TRUE;
3228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar14f24742012-08-08 18:01:05 +02003230 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003233#ifdef FEAT_VARTABS
3234 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3235 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3236 ? tabstop_first(curbuf->b_p_vts_array)
3237 : curbuf->b_p_ts;
3238#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242
3243 /*
3244 * Number options that need some action when changed
3245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (pp == &p_wh || pp == &p_hh)
3247 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003248 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (p_wh < 1)
3250 {
3251 errmsg = e_positive;
3252 p_wh = 1;
3253 }
3254 if (p_wmh > p_wh)
3255 {
3256 errmsg = e_winheight;
3257 p_wh = p_wmh;
3258 }
3259 if (p_hh < 0)
3260 {
3261 errmsg = e_positive;
3262 p_hh = 0;
3263 }
3264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003265 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003266 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 if (pp == &p_wh && curwin->w_height < p_wh)
3269 win_setheight((int)p_wh);
3270 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3271 win_setheight((int)p_hh);
3272 }
3273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 else if (pp == &p_wmh)
3275 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003276 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (p_wmh < 0)
3278 {
3279 errmsg = e_positive;
3280 p_wmh = 0;
3281 }
3282 if (p_wmh > p_wh)
3283 {
3284 errmsg = e_winheight;
3285 p_wmh = p_wh;
3286 }
3287 win_setminheight();
3288 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003289 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003291 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (p_wiw < 1)
3293 {
3294 errmsg = e_positive;
3295 p_wiw = 1;
3296 }
3297 if (p_wmw > p_wiw)
3298 {
3299 errmsg = e_winwidth;
3300 p_wiw = p_wmw;
3301 }
3302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003303 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003304 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 win_setwidth((int)p_wiw);
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else if (pp == &p_wmw)
3308 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003309 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (p_wmw < 0)
3311 {
3312 errmsg = e_positive;
3313 p_wmw = 0;
3314 }
3315 if (p_wmw > p_wiw)
3316 {
3317 errmsg = e_winwidth;
3318 p_wmw = p_wiw;
3319 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003320 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003323 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 else if (pp == &p_ls)
3325 {
3326 last_status(FALSE);
3327 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003329 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003330 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003331 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003332 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
3335#ifdef FEAT_GUI
3336 else if (pp == &p_linespace)
3337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 // Recompute gui.char_height and resize the Vim window to keep the
3339 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003340 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003341 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343#endif
3344
3345#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003346 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 else if (pp == &curwin->w_p_fdl)
3348 {
3349 if (curwin->w_p_fdl < 0)
3350 curwin->w_p_fdl = 0;
3351 newFoldLevel();
3352 }
3353
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003354 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 else if (pp == &curwin->w_p_fml)
3356 {
3357 foldUpdateAll(curwin);
3358 }
3359
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003360 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 else if (pp == &curwin->w_p_fdn)
3362 {
3363 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3364 foldUpdateAll(curwin);
3365 }
3366
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003367 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 else if (pp == &curwin->w_p_fdc)
3369 {
3370 if (curwin->w_p_fdc < 0)
3371 {
3372 errmsg = e_positive;
3373 curwin->w_p_fdc = 0;
3374 }
3375 else if (curwin->w_p_fdc > 12)
3376 {
3377 errmsg = e_invarg;
3378 curwin->w_p_fdc = 12;
3379 }
3380 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003381#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003383#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003384 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3386 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003387# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (foldmethodIsIndent(curwin))
3389 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003390# endif
3391# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003392 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3393 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003394 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3395 parse_cino(curbuf);
3396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003400 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003401 else if (pp == &p_mco)
3402 {
3403 if (p_mco > MAX_MCO)
3404 p_mco = MAX_MCO;
3405 else if (p_mco < 0)
3406 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003407 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003408 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 else if (pp == &curbuf->b_p_iminsert)
3411 {
3412 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3413 {
3414 errmsg = e_invarg;
3415 curbuf->b_p_iminsert = B_IMODE_NONE;
3416 }
3417 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003418 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003420#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003421 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 status_redraw_curbuf();
3423#endif
3424 }
3425
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003426#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003428 else if (pp == &p_imst)
3429 {
3430 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3431 errmsg = e_invarg;
3432 }
3433#endif
3434
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003435 else if (pp == &p_window)
3436 {
3437 if (p_window < 1)
3438 p_window = 1;
3439 else if (p_window >= Rows)
3440 p_window = Rows - 1;
3441 }
3442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 else if (pp == &curbuf->b_p_imsearch)
3444 {
3445 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3446 {
3447 errmsg = e_invarg;
3448 curbuf->b_p_imsearch = B_IMODE_NONE;
3449 }
3450 p_imsearch = curbuf->b_p_imsearch;
3451 }
3452
3453#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else if (pp == &p_titlelen)
3456 {
3457 if (p_titlelen < 0)
3458 {
3459 errmsg = e_positive;
3460 p_titlelen = 85;
3461 }
3462 if (starting != NO_SCREEN && old_value != p_titlelen)
3463 need_maketitle = TRUE;
3464 }
3465#endif
3466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003467 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 else if (pp == &p_ch)
3469 {
3470 if (p_ch < 1)
3471 {
3472 errmsg = e_positive;
3473 p_ch = 1;
3474 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003475 if (p_ch > Rows - min_rows() + 1)
3476 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003478 // Only compute the new window layout when startup has been
3479 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (p_ch != old_value && full_screen
3481#ifdef FEAT_GUI
3482 && !gui.starting
3483#endif
3484 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003485 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003488 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 else if (pp == &p_uc)
3490 {
3491 if (p_uc < 0)
3492 {
3493 errmsg = e_positive;
3494 p_uc = 100;
3495 }
3496 if (p_uc && !old_value)
3497 ml_open_files();
3498 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003499#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003500 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003501 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003502 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003503 {
3504 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003505 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003506 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003507 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003508 {
3509 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003510 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003511 }
3512 }
3513#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003514#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003515 else if (pp == &p_mzq)
3516 mzvim_reset_timer();
3517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003519#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003520 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003521 else if (pp == &p_pyx)
3522 {
3523 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3524 errmsg = e_invarg;
3525 }
3526#endif
3527
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003528 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 else if (pp == &p_ul)
3530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003531 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003533 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 p_ul = value;
3535 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003536 else if (pp == &curbuf->b_p_ul)
3537 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003538 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003539 curbuf->b_p_ul = old_value;
3540 u_sync(TRUE);
3541 curbuf->b_p_ul = value;
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003544#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003545 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003546 else if (pp == &curwin->w_p_nuw)
3547 {
3548 if (curwin->w_p_nuw < 1)
3549 {
3550 errmsg = e_positive;
3551 curwin->w_p_nuw = 1;
3552 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003553 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003554 {
3555 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003556 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003557 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003558 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003559 }
3560#endif
3561
Bram Moolenaar1a384422010-07-14 19:53:30 +02003562 else if (pp == &curbuf->b_p_tw)
3563 {
3564 if (curbuf->b_p_tw < 0)
3565 {
3566 errmsg = e_positive;
3567 curbuf->b_p_tw = 0;
3568 }
3569#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003570 {
3571 win_T *wp;
3572 tabpage_T *tp;
3573
3574 FOR_ALL_TAB_WINDOWS(tp, wp)
3575 check_colorcolumn(wp);
3576 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003577#endif
3578 }
3579
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 /*
3581 * Check the bounds for numeric options here
3582 */
3583 if (Rows < min_rows() && full_screen)
3584 {
3585 if (errbuf != NULL)
3586 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003587 vim_snprintf((char *)errbuf, errbuflen,
3588 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 errmsg = errbuf;
3590 }
3591 Rows = min_rows();
3592 }
3593 if (Columns < MIN_COLUMNS && full_screen)
3594 {
3595 if (errbuf != NULL)
3596 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003597 vim_snprintf((char *)errbuf, errbuflen,
3598 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 errmsg = errbuf;
3600 }
3601 Columns = MIN_COLUMNS;
3602 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003603 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 /*
3606 * If the screen (shell) height has been changed, assume it is the
3607 * physical screenheight.
3608 */
3609 if (old_Rows != Rows || old_Columns != Columns)
3610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003611 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (updating_screen)
3613 *pp = old_value;
3614 else if (full_screen
3615#ifdef FEAT_GUI
3616 && !gui.starting
3617#endif
3618 )
3619 set_shellsize((int)Columns, (int)Rows, TRUE);
3620 else
3621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003622 // Postpone the resizing; check the size and cmdline position for
3623 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 check_shellsize();
3625 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3626 cmdline_row = Rows - p_ch;
3627 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003628 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003629 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 if (curbuf->b_p_ts <= 0)
3633 {
3634 errmsg = e_positive;
3635 curbuf->b_p_ts = 8;
3636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (p_tm < 0)
3638 {
3639 errmsg = e_positive;
3640 p_tm = 0;
3641 }
3642 if ((curwin->w_p_scr <= 0
3643 || (curwin->w_p_scr > curwin->w_height
3644 && curwin->w_height > 0))
3645 && full_screen)
3646 {
3647 if (pp == &(curwin->w_p_scr))
3648 {
3649 if (curwin->w_p_scr != 0)
3650 errmsg = e_scroll;
3651 win_comp_scroll(curwin);
3652 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003653 // If 'scroll' became invalid because of a side effect silently adjust
3654 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 else if (curwin->w_p_scr <= 0)
3656 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003657 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 curwin->w_p_scr = curwin->w_height;
3659 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003660 if (p_hi < 0)
3661 {
3662 errmsg = e_positive;
3663 p_hi = 0;
3664 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003665 else if (p_hi > 10000)
3666 {
3667 errmsg = e_invarg;
3668 p_hi = 10000;
3669 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003670 if (p_re < 0 || p_re > 2)
3671 {
3672 errmsg = e_invarg;
3673 p_re = 0;
3674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (p_report < 0)
3676 {
3677 errmsg = e_positive;
3678 p_report = 1;
3679 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003680 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003682 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 p_sj = Rows / 2;
3684 else
3685 {
3686 errmsg = e_scroll;
3687 p_sj = 1;
3688 }
3689 }
3690 if (p_so < 0 && full_screen)
3691 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003692 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 p_so = 0;
3694 }
3695 if (p_siso < 0 && full_screen)
3696 {
3697 errmsg = e_positive;
3698 p_siso = 0;
3699 }
3700#ifdef FEAT_CMDWIN
3701 if (p_cwh < 1)
3702 {
3703 errmsg = e_positive;
3704 p_cwh = 1;
3705 }
3706#endif
3707 if (p_ut < 0)
3708 {
3709 errmsg = e_positive;
3710 p_ut = 2000;
3711 }
3712 if (p_ss < 0)
3713 {
3714 errmsg = e_positive;
3715 p_ss = 0;
3716 }
3717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003718 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3720 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3721
3722 options[opt_idx].flags |= P_WAS_SET;
3723
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003724#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003725 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3726 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003727#endif
3728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003730 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003731 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003732 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 check_redraw(options[opt_idx].flags);
3734
3735 return errmsg;
3736}
3737
3738/*
3739 * Called after an option changed: check if something needs to be redrawn.
3740 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003742check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003744 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003745 int doclear = (flags & P_RCLR) == P_RCLR;
3746 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003748 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
3751 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3752 changed_window_setting();
3753 if (flags & P_RBUF)
3754 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003755 if (flags & P_RWINONLY)
3756 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003757 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 redraw_all_later(CLEAR);
3759 else if (all)
3760 redraw_all_later(NOT_VALID);
3761}
3762
3763/*
3764 * Find index for option 'arg'.
3765 * Return -1 if not found.
3766 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003767 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003768findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 int opt_idx;
3771 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003772 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 int is_term_opt;
3774
3775 /*
3776 * For first call: Initialize the quick-access table.
3777 * It contains the index for the first option that starts with a certain
3778 * letter. There are 26 letters, plus the first "t_" option.
3779 */
3780 if (quick_tab[1] == 0)
3781 {
3782 p = options[0].fullname;
3783 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3784 {
3785 if (s[0] != p[0])
3786 {
3787 if (s[0] == 't' && s[1] == '_')
3788 quick_tab[26] = opt_idx;
3789 else
3790 quick_tab[CharOrdLow(s[0])] = opt_idx;
3791 }
3792 p = s;
3793 }
3794 }
3795
3796 /*
3797 * Check for name starting with an illegal character.
3798 */
3799#ifdef EBCDIC
3800 if (!islower(arg[0]))
3801#else
3802 if (arg[0] < 'a' || arg[0] > 'z')
3803#endif
3804 return -1;
3805
3806 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3807 if (is_term_opt)
3808 opt_idx = quick_tab[26];
3809 else
3810 opt_idx = quick_tab[CharOrdLow(arg[0])];
3811 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 break;
3815 }
3816 if (s == NULL && !is_term_opt)
3817 {
3818 opt_idx = quick_tab[CharOrdLow(arg[0])];
3819 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3820 {
3821 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003822 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 break;
3824 s = NULL;
3825 }
3826 }
3827 if (s == NULL)
3828 opt_idx = -1;
3829 return opt_idx;
3830}
3831
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003832#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833/*
3834 * Get the value for an option.
3835 *
3836 * Returns:
3837 * Number or Toggle option: 1, *numval gets value.
3838 * String option: 0, *stringval gets allocated string.
3839 * Hidden Number or Toggle option: -1.
3840 * hidden String option: -2.
3841 * unknown option: -3.
3842 */
3843 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003844get_option_value(
3845 char_u *name,
3846 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003847 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003848 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849{
3850 int opt_idx;
3851 char_u *varp;
3852
3853 opt_idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003854 if (opt_idx < 0) // unknown option
Bram Moolenaare353c402017-02-04 19:49:16 +01003855 {
3856 int key;
3857
3858 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02003859 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003860 {
3861 char_u key_name[2];
3862 char_u *p;
3863
3864 if (key < 0)
3865 {
3866 key_name[0] = KEY2TERMCAP0(key);
3867 key_name[1] = KEY2TERMCAP1(key);
3868 }
3869 else
3870 {
3871 key_name[0] = KS_KEY;
3872 key_name[1] = (key & 0xff);
3873 }
3874 p = find_termcode(key_name);
3875 if (p != NULL)
3876 {
3877 if (stringval != NULL)
3878 *stringval = vim_strsave(p);
3879 return 0;
3880 }
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 return -3;
Bram Moolenaare353c402017-02-04 19:49:16 +01003883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
3885 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3886
3887 if (options[opt_idx].flags & P_STRING)
3888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003889 if (varp == NULL) // hidden option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return -2;
3891 if (stringval != NULL)
3892 {
3893#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003894 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003895 if ((char_u **)varp == &curbuf->b_p_key
3896 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 *stringval = vim_strsave((char_u *)"*****");
3898 else
3899#endif
3900 *stringval = vim_strsave(*(char_u **)(varp));
3901 }
3902 return 0;
3903 }
3904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003905 if (varp == NULL) // hidden option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return -1;
3907 if (options[opt_idx].flags & P_NUM)
3908 *numval = *(long *)varp;
3909 else
3910 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003911 // Special case: 'modified' is b_changed, but we also want to consider
3912 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if ((int *)varp == &curbuf->b_changed)
3914 *numval = curbufIsChanged();
3915 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003916 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 return 1;
3919}
3920#endif
3921
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003922#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003923/*
3924 * Returns the option attributes and its value. Unlike the above function it
3925 * will return either global value or local value of the option depending on
3926 * what was requested, but it will never return global value if it was
3927 * requested to return local one and vice versa. Neither it will return
3928 * buffer-local value if it was requested to return window-local one.
3929 *
3930 * Pretends that option is absent if it is not present in the requested scope
3931 * (i.e. has no global, window-local or buffer-local value depending on
3932 * opt_type). Uses
3933 *
3934 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003935 * 0 hidden or unknown option, also option that does not have requested
3936 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003937 * see SOPT_* in vim.h for other flags
3938 *
3939 * Possible opt_type values: see SREQ_* in vim.h
3940 */
3941 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003942get_option_value_strict(
3943 char_u *name,
3944 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003945 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003946 int opt_type,
3947 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003948{
3949 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003950 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003951 struct vimoption *p;
3952 int r = 0;
3953
3954 opt_idx = findoption(name);
3955 if (opt_idx < 0)
3956 return 0;
3957
3958 p = &(options[opt_idx]);
3959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003960 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003961 if (p->var == NULL)
3962 return 0;
3963
3964 if (p->flags & P_BOOL)
3965 r |= SOPT_BOOL;
3966 else if (p->flags & P_NUM)
3967 r |= SOPT_NUM;
3968 else if (p->flags & P_STRING)
3969 r |= SOPT_STRING;
3970
3971 if (p->indir == PV_NONE)
3972 {
3973 if (opt_type == SREQ_GLOBAL)
3974 r |= SOPT_GLOBAL;
3975 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003976 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003977 }
3978 else
3979 {
3980 if (p->indir & PV_BOTH)
3981 r |= SOPT_GLOBAL;
3982 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003983 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003984
3985 if (p->indir & PV_WIN)
3986 {
3987 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003988 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003989 else
3990 r |= SOPT_WIN;
3991 }
3992 else if (p->indir & PV_BUF)
3993 {
3994 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003995 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003996 else
3997 r |= SOPT_BUF;
3998 }
3999 }
4000
4001 if (stringval == NULL)
4002 return r;
4003
4004 if (opt_type == SREQ_GLOBAL)
4005 varp = p->var;
4006 else
4007 {
4008 if (opt_type == SREQ_BUF)
4009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004010 // Special case: 'modified' is b_changed, but we also want to
4011 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004012 if (p->indir == PV_MOD)
4013 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004014 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004015 varp = NULL;
4016 }
4017#ifdef FEAT_CRYPT
4018 else if (p->indir == PV_KEY)
4019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004021 *stringval = NULL;
4022 varp = NULL;
4023 }
4024#endif
4025 else
4026 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004027 buf_T *save_curbuf = curbuf;
4028
4029 // only getting a pointer, no need to use aucmd_prepbuf()
4030 curbuf = (buf_T *)from;
4031 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004032 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004033 curbuf = save_curbuf;
4034 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004035 }
4036 }
4037 else if (opt_type == SREQ_WIN)
4038 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004039 win_T *save_curwin = curwin;
4040
4041 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004042 curbuf = curwin->w_buffer;
4043 varp = get_varp(p);
4044 curwin = save_curwin;
4045 curbuf = curwin->w_buffer;
4046 }
4047 if (varp == p->var)
4048 return (r | SOPT_UNSET);
4049 }
4050
4051 if (varp != NULL)
4052 {
4053 if (p->flags & P_STRING)
4054 *stringval = vim_strsave(*(char_u **)(varp));
4055 else if (p->flags & P_NUM)
4056 *numval = *(long *) varp;
4057 else
4058 *numval = *(int *)varp;
4059 }
4060
4061 return r;
4062}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004063
4064/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004065 * Iterate over options. First argument is a pointer to a pointer to a
4066 * structure inside options[] array, second is option type like in the above
4067 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004068 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004069 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004070 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004071 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004072 * first argument to NULL.
4073 *
4074 * Returns full option name for current option on each call.
4075 */
4076 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004077option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004078{
4079 struct vimoption *ret = NULL;
4080 do
4081 {
4082 if (*option == NULL)
4083 *option = (void *) options;
4084 else if (((struct vimoption *) (*option))->fullname == NULL)
4085 {
4086 *option = NULL;
4087 return NULL;
4088 }
4089 else
4090 *option = (void *) (((struct vimoption *) (*option)) + 1);
4091
4092 ret = ((struct vimoption *) (*option));
4093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004094 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004095 if (ret->var == NULL)
4096 {
4097 ret = NULL;
4098 continue;
4099 }
4100
4101 switch (opt_type)
4102 {
4103 case SREQ_GLOBAL:
4104 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4105 ret = NULL;
4106 break;
4107 case SREQ_BUF:
4108 if (!(ret->indir & PV_BUF))
4109 ret = NULL;
4110 break;
4111 case SREQ_WIN:
4112 if (!(ret->indir & PV_WIN))
4113 ret = NULL;
4114 break;
4115 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004116 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004117 return NULL;
4118 }
4119 }
4120 while (ret == NULL);
4121
4122 return (char_u *)ret->fullname;
4123}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004124#endif
4125
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004127 * Return the flags for the option at 'opt_idx'.
4128 */
4129 long_u
4130get_option_flags(int opt_idx)
4131{
4132 return options[opt_idx].flags;
4133}
4134
4135/*
4136 * Set a flag for the option at 'opt_idx'.
4137 */
4138 void
4139set_option_flag(int opt_idx, long_u flag)
4140{
4141 options[opt_idx].flags |= flag;
4142}
4143
4144/*
4145 * Clear a flag for the option at 'opt_idx'.
4146 */
4147 void
4148clear_option_flag(int opt_idx, long_u flag)
4149{
4150 options[opt_idx].flags &= ~flag;
4151}
4152
4153/*
4154 * Returns TRUE if the option at 'opt_idx' is a global option
4155 */
4156 int
4157is_global_option(int opt_idx)
4158{
4159 return options[opt_idx].indir == PV_NONE;
4160}
4161
4162/*
4163 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4164 * local value.
4165 */
4166 int
4167is_global_local_option(int opt_idx)
4168{
4169 return options[opt_idx].indir & PV_BOTH;
4170}
4171
4172/*
4173 * Returns TRUE if the option at 'opt_idx' is a window-local option
4174 */
4175 int
4176is_window_local_option(int opt_idx)
4177{
4178 return options[opt_idx].var == VAR_WIN;
4179}
4180
4181/*
4182 * Returns TRUE if the option at 'opt_idx' is a hidden option
4183 */
4184 int
4185is_hidden_option(int opt_idx)
4186{
4187 return options[opt_idx].var == NULL;
4188}
4189
4190#if defined(FEAT_CRYPT) || defined(PROTO)
4191/*
4192 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4193 */
4194 int
4195is_crypt_key_option(int opt_idx)
4196{
4197 return options[opt_idx].indir == PV_KEY;
4198}
4199#endif
4200
4201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 * Set the value of option "name".
4203 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004204 *
4205 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004207 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004208set_option_value(
4209 char_u *name,
4210 long number,
4211 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004212 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int opt_idx;
4215 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004216 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004219 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004220 {
4221 int key;
4222
4223 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004224 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004225 {
4226 char_u key_name[2];
4227
4228 if (key < 0)
4229 {
4230 key_name[0] = KEY2TERMCAP0(key);
4231 key_name[1] = KEY2TERMCAP1(key);
4232 }
4233 else
4234 {
4235 key_name[0] = KS_KEY;
4236 key_name[1] = (key & 0xff);
4237 }
4238 add_termcode(key_name, string, FALSE);
4239 if (full_screen)
4240 ttest(FALSE);
4241 redraw_all_later(CLEAR);
4242 return NULL;
4243 }
4244
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004245 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 else
4248 {
4249 flags = options[opt_idx].flags;
4250#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004251 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004253 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004254 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004255 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004258 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004259 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 else
4261 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004262 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004263 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004265 if (number == 0 && string != NULL)
4266 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004267 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004269 // Either we are given a string or we are setting option
4270 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004271 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004272 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004273 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004274 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004275 // There's another character after zeros or the string
4276 // is empty. In both cases, we are trying to set a
4277 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004278 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004279 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004280 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004281
4282 }
4283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004285 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004286 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004288 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004289 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004293 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294}
4295
4296/*
4297 * Get the terminal code for a terminal option.
4298 * Returns NULL when not found.
4299 */
4300 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004301get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
4303 int opt_idx;
4304 char_u *varp;
4305
4306 if (tname[0] != 't' || tname[1] != '_' ||
4307 tname[2] == NUL || tname[3] == NUL)
4308 return NULL;
4309 if ((opt_idx = findoption(tname)) >= 0)
4310 {
4311 varp = get_varp(&(options[opt_idx]));
4312 if (varp != NULL)
4313 varp = *(char_u **)(varp);
4314 return varp;
4315 }
4316 return find_termcode(tname + 2);
4317}
4318
4319 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004320get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
4322 int i;
4323
4324 i = findoption((char_u *)"hl");
4325 if (i >= 0)
4326 return options[i].def_val[VI_DEFAULT];
4327 return (char_u *)NULL;
4328}
4329
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004330 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004331get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004332{
4333 int i;
4334
4335 i = findoption((char_u *)"enc");
4336 if (i >= 0)
4337 return options[i].def_val[VI_DEFAULT];
4338 return (char_u *)NULL;
4339}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004340
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341/*
4342 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004343 * When "has_lt" is true there is a '<' before "*arg_arg".
4344 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 */
4346 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004347find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004349 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004351 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
4353 /*
4354 * Don't use get_special_key_code() for t_xx, we don't want it to call
4355 * add_termcap_entry().
4356 */
4357 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4358 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004359 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004361 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004363 key = find_special_key(&arg, &modifiers,
4364 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004365 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 key = 0;
4367 }
4368 return key;
4369}
4370
4371/*
4372 * if 'all' == 0: show changed options
4373 * if 'all' == 1: show all normal options
4374 * if 'all' == 2: show all terminal options
4375 */
4376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004377showoptions(
4378 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004379 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380{
4381 struct vimoption *p;
4382 int col;
4383 int isterm;
4384 char_u *varp;
4385 struct vimoption **items;
4386 int item_count;
4387 int run;
4388 int row, rows;
4389 int cols;
4390 int i;
4391 int len;
4392
4393#define INC 20
4394#define GAP 3
4395
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004396 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (items == NULL)
4398 return;
4399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004400 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004402 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004404 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004406 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004408 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004411 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 * 1. display the short items
4413 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004414 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 */
4416 for (run = 1; run <= 2 && !got_int; ++run)
4417 {
4418 /*
4419 * collect the items in items[]
4420 */
4421 item_count = 0;
4422 for (p = &options[0]; p->fullname != NULL; p++)
4423 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004424 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004425 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004426 continue;
4427
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 varp = NULL;
4429 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004430 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 if (p->indir != PV_NONE && !isterm)
4433 varp = get_varp_scope(p, opt_flags);
4434 }
4435 else
4436 varp = get_varp(p);
4437 if (varp != NULL
4438 && ((all == 2 && isterm)
4439 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004440 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004442 if (opt_flags & OPT_ONECOLUMN)
4443 len = Columns;
4444 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004445 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 else
4447 {
4448 option_value2string(p, opt_flags);
4449 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4450 }
4451 if ((len <= INC - GAP && run == 1) ||
4452 (len > INC - GAP && run == 2))
4453 items[item_count++] = p;
4454 }
4455 }
4456
4457 /*
4458 * display the items
4459 */
4460 if (run == 1)
4461 {
4462 cols = (Columns + GAP - 3) / INC;
4463 if (cols == 0)
4464 cols = 1;
4465 rows = (item_count + cols - 1) / cols;
4466 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004467 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 rows = item_count;
4469 for (row = 0; row < rows && !got_int; ++row)
4470 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004471 msg_putchar('\n'); // go to next line
4472 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 break;
4474 col = 0;
4475 for (i = row; i < item_count; i += rows)
4476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004477 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 showoneopt(items[i], opt_flags);
4479 col += INC;
4480 }
4481 out_flush();
4482 ui_breakcheck();
4483 }
4484 }
4485 vim_free(items);
4486}
4487
4488/*
4489 * Return TRUE if option "p" has its default value.
4490 */
4491 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004492optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494 int dvi;
4495
4496 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004497 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004498 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004500 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004502 // the cast to long is required for Manx C, long_i is
4503 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004504 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4507}
4508
4509/*
4510 * showoneopt: show the value of one option
4511 * must not be called with a hidden option!
4512 */
4513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004514showoneopt(
4515 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004516 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004518 char_u *varp;
4519 int save_silent = silent_mode;
4520
4521 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004522 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 varp = get_varp_scope(p, opt_flags);
4525
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004526 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4528 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004529 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004531 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004533 msg_puts(" ");
4534 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (!(p->flags & P_BOOL))
4536 {
4537 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004538 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 option_value2string(p, opt_flags);
4540 msg_outtrans(NameBuff);
4541 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004542
4543 silent_mode = save_silent;
4544 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545}
4546
4547/*
4548 * Write modified options as ":set" commands to a file.
4549 *
4550 * There are three values for "opt_flags":
4551 * OPT_GLOBAL: Write global option values and fresh values of
4552 * buffer-local options (used for start of a session
4553 * file).
4554 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4555 * curwin (used for a vimrc file).
4556 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4557 * and local values for window-local options of
4558 * curwin. Local values are also written when at the
4559 * default value, because a modeline or autocommand
4560 * may have set them when doing ":edit file" and the
4561 * user has set them back at the default or fresh
4562 * value.
4563 * When "local_only" is TRUE, don't write fresh
4564 * values, only local values (for ":mkview").
4565 * (fresh value = value used for a new buffer or window for a local option).
4566 *
4567 * Return FAIL on error, OK otherwise.
4568 */
4569 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004570makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571{
4572 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004573 char_u *varp; // currently used value
4574 char_u *varp_fresh; // local value
4575 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 char *cmd;
4577 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004578 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
4580 /*
4581 * The options that don't have a default (terminal name, columns, lines)
4582 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004583 * Do the loop over "options[]" twice: once for options with the
4584 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004586 for (pri = 1; pri >= 0; --pri)
4587 {
4588 for (p = &options[0]; !istermoption(p); p++)
4589 if (!(p->flags & P_NO_MKRC)
4590 && !istermoption(p)
4591 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4595 continue;
4596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004597 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4598 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4600 continue;
4601
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004604 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 continue;
4606
4607 round = 2;
4608 if (p->indir != PV_NONE)
4609 {
4610 if (p->var == VAR_WIN)
4611 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004612 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (!(opt_flags & OPT_LOCAL))
4614 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 // When fresh value of window-local option is not at the
4616 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4618 {
4619 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004620 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
4622 round = 1;
4623 varp_local = varp;
4624 varp = varp_fresh;
4625 }
4626 }
4627 }
4628 }
4629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004630 // Round 1: fresh value for window-local options.
4631 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 for ( ; round <= 2; varp = varp_local, ++round)
4633 {
4634 if (round == 1 || (opt_flags & OPT_GLOBAL))
4635 cmd = "set";
4636 else
4637 cmd = "setlocal";
4638
4639 if (p->flags & P_BOOL)
4640 {
4641 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4642 return FAIL;
4643 }
4644 else if (p->flags & P_NUM)
4645 {
4646 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4647 return FAIL;
4648 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004649 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004651 int do_endif = FALSE;
4652
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004653 // Don't set 'syntax' and 'filetype' again if the value is
4654 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004655 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004656#if defined(FEAT_SYN_HL)
4657 p->indir == PV_SYN ||
4658#endif
4659 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
4661 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4662 *(char_u **)(varp)) < 0
4663 || put_eol(fd) < 0)
4664 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004665 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004668 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004670 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
4672 if (put_line(fd, "endif") == FAIL)
4673 return FAIL;
4674 }
4675 }
4676 }
4677 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 return OK;
4680}
4681
4682#if defined(FEAT_FOLDING) || defined(PROTO)
4683/*
4684 * Generate set commands for the local fold options only. Used when
4685 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4686 */
4687 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004688makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004690 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004692 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 == FAIL
4694# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004695 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004697 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 == FAIL
4699 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4700 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4701 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4702 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4703 )
4704 return FAIL;
4705
4706 return OK;
4707}
4708#endif
4709
4710 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004711put_setstring(
4712 FILE *fd,
4713 char *cmd,
4714 char *name,
4715 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004716 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004719 char_u *buf = NULL;
4720 char_u *part = NULL;
4721 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
4723 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4724 return FAIL;
4725 if (*valuep != NULL)
4726 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004727 // Output 'pastetoggle' as key names. For other
4728 // options some characters have to be escaped with
4729 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (valuep == &p_pt)
4731 {
4732 s = *valuep;
4733 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004734 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return FAIL;
4736 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004737 // expand the option value, replace $HOME by ~
4738 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004740 int size = (int)STRLEN(*valuep) + 1;
4741
4742 // replace home directory in the whole option value into "buf"
4743 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004744 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004745 goto fail;
4746 home_replace(NULL, *valuep, buf, size, FALSE);
4747
4748 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004749 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004750 // can be expanded when read back.
4751 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4752 && vim_strchr(*valuep, ',') != NULL)
4753 {
4754 part = alloc(size);
4755 if (part == NULL)
4756 goto fail;
4757
4758 // write line break to clear the option, e.g. ':set rtp='
4759 if (put_eol(fd) == FAIL)
4760 goto fail;
4761
4762 p = buf;
4763 while (*p != NUL)
4764 {
4765 // for each comma separated option part, append value to
4766 // the option, :set rtp+=value
4767 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4768 goto fail;
4769 (void)copy_option_part(&p, part, size, ",");
4770 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4771 goto fail;
4772 }
4773 vim_free(buf);
4774 vim_free(part);
4775 return OK;
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004778 {
4779 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004781 }
4782 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784 else if (put_escstr(fd, *valuep, 2) == FAIL)
4785 return FAIL;
4786 }
4787 if (put_eol(fd) < 0)
4788 return FAIL;
4789 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004790fail:
4791 vim_free(buf);
4792 vim_free(part);
4793 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794}
4795
4796 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004797put_setnum(
4798 FILE *fd,
4799 char *cmd,
4800 char *name,
4801 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802{
4803 long wc;
4804
4805 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4806 return FAIL;
4807 if (wc_use_keyname((char_u *)valuep, &wc))
4808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004809 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4811 return FAIL;
4812 }
4813 else if (fprintf(fd, "%ld", *valuep) < 0)
4814 return FAIL;
4815 if (put_eol(fd) < 0)
4816 return FAIL;
4817 return OK;
4818}
4819
4820 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004821put_setbool(
4822 FILE *fd,
4823 char *cmd,
4824 char *name,
4825 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004827 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004828 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4830 || put_eol(fd) < 0)
4831 return FAIL;
4832 return OK;
4833}
4834
4835/*
4836 * Clear all the terminal options.
4837 * If the option has been allocated, free the memory.
4838 * Terminal options are never hidden or indirect.
4839 */
4840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004841clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /*
4844 * Reset a few things before clearing the old options. This may cause
4845 * outputting a few things that the terminal doesn't understand, but the
4846 * screen will be cleared later, so this is OK.
4847 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004848 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004850 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#endif
4852#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004853 // When starting the GUI close the display opened for the clipboard.
4854 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (gui.starting)
4856 clear_xterm_clip();
4857#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004858 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004860 free_termoptions();
4861}
4862
4863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004864free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004865{
4866 struct vimoption *p;
4867
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004868 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 if (istermoption(p))
4870 {
4871 if (p->flags & P_ALLOCED)
4872 free_string_option(*(char_u **)(p->var));
4873 if (p->flags & P_DEF_ALLOCED)
4874 free_string_option(p->def_val[VI_DEFAULT]);
4875 *(char_u **)(p->var) = empty_option;
4876 p->def_val[VI_DEFAULT] = empty_option;
4877 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004878#ifdef FEAT_EVAL
4879 // remember where the option was cleared
4880 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 clear_termcodes();
4884}
4885
4886/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004887 * Free the string for one term option, if it was allocated.
4888 * Set the string to empty_option and clear allocated flag.
4889 * "var" points to the option value.
4890 */
4891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004892free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004893{
4894 struct vimoption *p;
4895
4896 for (p = &options[0]; p->fullname != NULL; p++)
4897 if (p->var == var)
4898 {
4899 if (p->flags & P_ALLOCED)
4900 free_string_option(*(char_u **)(p->var));
4901 *(char_u **)(p->var) = empty_option;
4902 p->flags &= ~P_ALLOCED;
4903 break;
4904 }
4905}
4906
4907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 * Set the terminal option defaults to the current value.
4909 * Used after setting the terminal name.
4910 */
4911 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004912set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
4914 struct vimoption *p;
4915
4916 for (p = &options[0]; p->fullname != NULL; p++)
4917 {
4918 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4919 {
4920 if (p->flags & P_DEF_ALLOCED)
4921 {
4922 free_string_option(p->def_val[VI_DEFAULT]);
4923 p->flags &= ~P_DEF_ALLOCED;
4924 }
4925 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4926 if (p->flags & P_ALLOCED)
4927 {
4928 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004929 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931 }
4932 }
4933}
4934
4935/*
4936 * return TRUE if 'p' starts with 't_'
4937 */
4938 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004939istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4942}
4943
Bram Moolenaardac13472019-09-16 21:06:21 +02004944/*
4945 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4946 */
4947 int
4948istermoption_idx(int opt_idx)
4949{
4950 return istermoption(&options[opt_idx]);
4951}
4952
Bram Moolenaar113e1072019-01-20 15:30:40 +01004953#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004955 * Unset local option value, similar to ":set opt<".
4956 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004957 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004958unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004959{
4960 struct vimoption *p;
4961 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004962 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004963
4964 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004965 if (opt_idx < 0)
4966 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004967 p = &(options[opt_idx]);
4968
4969 switch ((int)p->indir)
4970 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004971 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004972 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004973 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004974 break;
4975 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004976 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004977 break;
4978 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004979 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004980 break;
4981 case PV_AR:
4982 buf->b_p_ar = -1;
4983 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004984 case PV_BKC:
4985 clear_string_option(&buf->b_p_bkc);
4986 buf->b_bkc_flags = 0;
4987 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004988 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004989 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004990 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01004991 case PV_TC:
4992 clear_string_option(&buf->b_p_tc);
4993 buf->b_tc_flags = 0;
4994 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01004995 case PV_SISO:
4996 curwin->w_p_siso = -1;
4997 break;
4998 case PV_SO:
4999 curwin->w_p_so = -1;
5000 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005001#ifdef FEAT_FIND_ID
5002 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005003 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005004 break;
5005 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005006 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005007 break;
5008#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005009 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005010 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005011 break;
5012 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005013 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005014 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005015 case PV_FP:
5016 clear_string_option(&buf->b_p_fp);
5017 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005018#ifdef FEAT_QUICKFIX
5019 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005020 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005021 break;
5022 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005023 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005024 break;
5025 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005026 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005027 break;
5028#endif
5029#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5030 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005031 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005032 break;
5033#endif
5034#if defined(FEAT_CRYPT)
5035 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005036 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005037 break;
5038#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005039#ifdef FEAT_LINEBREAK
5040 case PV_SBR:
5041 clear_string_option(&((win_T *)from)->w_p_sbr);
5042 break;
5043#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005044#ifdef FEAT_STL_OPT
5045 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005046 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005047 break;
5048#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005049 case PV_UL:
5050 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5051 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005052#ifdef FEAT_LISP
5053 case PV_LW:
5054 clear_string_option(&buf->b_p_lw);
5055 break;
5056#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005057 case PV_MENC:
5058 clear_string_option(&buf->b_p_menc);
5059 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005060 }
5061}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005062#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005063
5064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 * Get pointer to option variable, depending on local or global scope.
5066 */
5067 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005068get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
5070 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5071 {
5072 if (p->var == VAR_WIN)
5073 return (char_u *)GLOBAL_WO(get_varp(p));
5074 return p->var;
5075 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005076 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 switch ((int)p->indir)
5079 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005080 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005082 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5083 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5084 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005086 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5087 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5088 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005089 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005090 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005091 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005092 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5093 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005095 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5096 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005098 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5099 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005100#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5101 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5102#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005103#if defined(FEAT_CRYPT)
5104 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5105#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005106#ifdef FEAT_LINEBREAK
5107 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5108#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005109#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005110 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005111#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005112 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005113#ifdef FEAT_LISP
5114 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5115#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005116 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005117 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005119 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 return get_varp(p);
5122}
5123
5124/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005125 * Get pointer to option variable at 'opt_idx', depending on local or global
5126 * scope.
5127 */
5128 char_u *
5129get_option_varp_scope(int opt_idx, int opt_flags)
5130{
5131 return get_varp_scope(&(options[opt_idx]), opt_flags);
5132}
5133
5134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 * Get pointer to option variable.
5136 */
5137 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005138get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005140 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if (p->var == NULL)
5142 return NULL;
5143
5144 switch ((int)p->indir)
5145 {
5146 case PV_NONE: return p->var;
5147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005148 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005149 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005151 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005153 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005155 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005157 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005159 case PV_TC: return *curbuf->b_p_tc != NUL
5160 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005161 case PV_BKC: return *curbuf->b_p_bkc != NUL
5162 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005163 case PV_SISO: return curwin->w_p_siso >= 0
5164 ? (char_u *)&(curwin->w_p_siso) : p->var;
5165 case PV_SO: return curwin->w_p_so >= 0
5166 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005168 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005170 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5172#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005173 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005175 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005177 case PV_FP: return *curbuf->b_p_fp != NUL
5178 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005180 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005182 case PV_GP: return *curbuf->b_p_gp != NUL
5183 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5184 case PV_MP: return *curbuf->b_p_mp != NUL
5185 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005187#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5188 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5189 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5190#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005191#if defined(FEAT_CRYPT)
5192 case PV_CM: return *curbuf->b_p_cm != NUL
5193 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5194#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005195#ifdef FEAT_LINEBREAK
5196 case PV_SBR: return *curwin->w_p_sbr != NUL
5197 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5198#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005199#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005200 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005201 ? (char_u *)&(curwin->w_p_stl) : p->var;
5202#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005203 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5204 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005205#ifdef FEAT_LISP
5206 case PV_LW: return *curbuf->b_p_lw != NUL
5207 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5208#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005209 case PV_MENC: return *curbuf->b_p_menc != NUL
5210 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#ifdef FEAT_ARABIC
5212 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5213#endif
5214 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005215#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005216 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5217#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005218#ifdef FEAT_SYN_HL
5219 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5220 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005221 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005222 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#ifdef FEAT_DIFF
5225 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5226#endif
5227#ifdef FEAT_FOLDING
5228 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5229 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5230 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5231 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5232 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5233 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5234 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5235# ifdef FEAT_EVAL
5236 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5237 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5238# endif
5239 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5240#endif
5241 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005242 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005243#ifdef FEAT_LINEBREAK
5244 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005247 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005248#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5250#endif
5251#ifdef FEAT_RIGHTLEFT
5252 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5253 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5254#endif
5255 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5256 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5257#ifdef FEAT_LINEBREAK
5258 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005259 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5260 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005262 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005264 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005265#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005266 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5267 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5268#endif
5269#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005270 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5271 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5272 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5276 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5279 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5281 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5282#ifdef FEAT_CINDENT
5283 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5284 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5285 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5286#endif
5287#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5288 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291#ifdef FEAT_FOLDING
5292 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005295#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005296 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005298#ifdef FEAT_COMPL_FUNC
5299 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005300 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005301#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005302#ifdef FEAT_EVAL
5303 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005306 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005312 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5314 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5315 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5316 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5317#ifdef FEAT_FIND_ID
5318# ifdef FEAT_EVAL
5319 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5320# endif
5321#endif
5322#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5323 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5324 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5325#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005326#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005327 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329#ifdef FEAT_CRYPT
5330 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5331#endif
5332#ifdef FEAT_LISP
5333 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5334#endif
5335 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5336 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5337 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5338 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5339 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005341#ifdef FEAT_TEXTOBJ
5342 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5345#ifdef FEAT_SMARTINDENT
5346 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5350#ifdef FEAT_SEARCHPATH
5351 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5352#endif
5353 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5354#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005355 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005356 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5357#endif
5358#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005359 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5360 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5361 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005362 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#endif
5364 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5365 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5366 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5367 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005368#ifdef FEAT_PERSISTENT_UNDO
5369 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5372#ifdef FEAT_KEYMAP
5373 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5374#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005375#ifdef FEAT_SIGNS
5376 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5377#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005378#ifdef FEAT_VARTABS
5379 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5380 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5381#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005382 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005384 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 return (char_u *)&(curbuf->b_p_wm);
5386}
5387
5388/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005389 * Return a pointer to the variable for option at 'opt_idx'
5390 */
5391 char_u *
5392get_option_var(int opt_idx)
5393{
5394 return options[opt_idx].var;
5395}
5396
5397/*
5398 * Return the full name of the option at 'opt_idx'
5399 */
5400 char_u *
5401get_option_fullname(int opt_idx)
5402{
5403 return (char_u *)options[opt_idx].fullname;
5404}
5405
5406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 * Get the value of 'equalprg', either the buffer-local one or the global one.
5408 */
5409 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005410get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 if (*curbuf->b_p_ep == NUL)
5413 return p_ep;
5414 return curbuf->b_p_ep;
5415}
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417/*
5418 * Copy options from one window to another.
5419 * Used when splitting a window.
5420 */
5421 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005422win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5425 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005426 after_copy_winopt(wp_to);
5427}
5428
5429/*
5430 * After copying window options: update variables depending on options.
5431 */
5432 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005433after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005434{
5435#ifdef FEAT_LINEBREAK
5436 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005437#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005438#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005439 fill_culopt_flags(NULL, wp);
5440 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444/*
5445 * Copy the options from one winopt_T to another.
5446 * Doesn't free the old option values in "to", use clear_winopt() for that.
5447 * The 'scroll' option is not copied, because it depends on the window height.
5448 * The 'previewwindow' option is reset, there can be only one preview window.
5449 */
5450 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005451copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453#ifdef FEAT_ARABIC
5454 to->wo_arab = from->wo_arab;
5455#endif
5456 to->wo_list = from->wo_list;
5457 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005458 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005459#ifdef FEAT_LINEBREAK
5460 to->wo_nuw = from->wo_nuw;
5461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462#ifdef FEAT_RIGHTLEFT
5463 to->wo_rl = from->wo_rl;
5464 to->wo_rlc = vim_strsave(from->wo_rlc);
5465#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005466#ifdef FEAT_LINEBREAK
5467 to->wo_sbr = vim_strsave(from->wo_sbr);
5468#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005469#ifdef FEAT_STL_OPT
5470 to->wo_stl = vim_strsave(from->wo_stl);
5471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005473#ifdef FEAT_DIFF
5474 to->wo_wrap_save = from->wo_wrap_save;
5475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476#ifdef FEAT_LINEBREAK
5477 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005478 to->wo_bri = from->wo_bri;
5479 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005481 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005483 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005484 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005485 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005486#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005487 to->wo_spell = from->wo_spell;
5488#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005489#ifdef FEAT_SYN_HL
5490 to->wo_cuc = from->wo_cuc;
5491 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005492 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005493 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495#ifdef FEAT_DIFF
5496 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005497 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005499#ifdef FEAT_CONCEAL
5500 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005501 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005502#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005503#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005504 to->wo_twk = vim_strsave(from->wo_twk);
5505 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#ifdef FEAT_FOLDING
5508 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005509 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005511 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 to->wo_fdi = vim_strsave(from->wo_fdi);
5513 to->wo_fml = from->wo_fml;
5514 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005515 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005517 to->wo_fdm_save = from->wo_diff_saved
5518 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 to->wo_fdn = from->wo_fdn;
5520# ifdef FEAT_EVAL
5521 to->wo_fde = vim_strsave(from->wo_fde);
5522 to->wo_fdt = vim_strsave(from->wo_fdt);
5523# endif
5524 to->wo_fmr = vim_strsave(from->wo_fmr);
5525#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005526#ifdef FEAT_SIGNS
5527 to->wo_scl = vim_strsave(from->wo_scl);
5528#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005529
5530#ifdef FEAT_EVAL
5531 // Copy the script context so that we know where the value was last set.
5532 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5533 sizeof(to->wo_script_ctx));
5534#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005535 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537
5538/*
5539 * Check string options in a window for a NULL value.
5540 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005541 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005542check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 check_winopt(&win->w_onebuf_opt);
5545 check_winopt(&win->w_allbuf_opt);
5546}
5547
5548/*
5549 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5550 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005552check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554#ifdef FEAT_FOLDING
5555 check_string_option(&wop->wo_fdi);
5556 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005557 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558# ifdef FEAT_EVAL
5559 check_string_option(&wop->wo_fde);
5560 check_string_option(&wop->wo_fdt);
5561# endif
5562 check_string_option(&wop->wo_fmr);
5563#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005564#ifdef FEAT_SIGNS
5565 check_string_option(&wop->wo_scl);
5566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567#ifdef FEAT_RIGHTLEFT
5568 check_string_option(&wop->wo_rlc);
5569#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005570#ifdef FEAT_LINEBREAK
5571 check_string_option(&wop->wo_sbr);
5572#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005573#ifdef FEAT_STL_OPT
5574 check_string_option(&wop->wo_stl);
5575#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005576#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005577 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005578 check_string_option(&wop->wo_cc);
5579#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005580#ifdef FEAT_CONCEAL
5581 check_string_option(&wop->wo_cocu);
5582#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005583#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005584 check_string_option(&wop->wo_twk);
5585 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005586#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005587#ifdef FEAT_LINEBREAK
5588 check_string_option(&wop->wo_briopt);
5589#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005590 check_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
5593/*
5594 * Free the allocated memory inside a winopt_T.
5595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005597clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
5599#ifdef FEAT_FOLDING
5600 clear_string_option(&wop->wo_fdi);
5601 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005602 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603# ifdef FEAT_EVAL
5604 clear_string_option(&wop->wo_fde);
5605 clear_string_option(&wop->wo_fdt);
5606# endif
5607 clear_string_option(&wop->wo_fmr);
5608#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005609#ifdef FEAT_SIGNS
5610 clear_string_option(&wop->wo_scl);
5611#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005612#ifdef FEAT_LINEBREAK
5613 clear_string_option(&wop->wo_briopt);
5614#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005615 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616#ifdef FEAT_RIGHTLEFT
5617 clear_string_option(&wop->wo_rlc);
5618#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005619#ifdef FEAT_LINEBREAK
5620 clear_string_option(&wop->wo_sbr);
5621#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005622#ifdef FEAT_STL_OPT
5623 clear_string_option(&wop->wo_stl);
5624#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005626 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005627 clear_string_option(&wop->wo_cc);
5628#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005629#ifdef FEAT_CONCEAL
5630 clear_string_option(&wop->wo_cocu);
5631#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005632#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005633 clear_string_option(&wop->wo_twk);
5634 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
5637
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005638#ifdef FEAT_EVAL
5639// Index into the options table for a buffer-local option enum.
5640static int buf_opt_idx[BV_COUNT];
5641# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5642
5643/*
5644 * Initialize buf_opt_idx[] if not done already.
5645 */
5646 static void
5647init_buf_opt_idx(void)
5648{
5649 static int did_init_buf_opt_idx = FALSE;
5650 int i;
5651
5652 if (did_init_buf_opt_idx)
5653 return;
5654 did_init_buf_opt_idx = TRUE;
5655 for (i = 0; !istermoption_idx(i); i++)
5656 if (options[i].indir & PV_BUF)
5657 buf_opt_idx[options[i].indir & PV_MASK] = i;
5658}
5659#else
5660# define COPY_OPT_SCTX(buf, bv)
5661#endif
5662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663/*
5664 * Copy global option values to local options for one buffer.
5665 * Used when creating a new buffer and sometimes when entering a buffer.
5666 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005667 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5669 * appropriate.
5670 * BCO_NOHELP Don't copy the values to a help buffer.
5671 */
5672 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005673buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005676 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 int dont_do_help;
5678 int did_isk = FALSE;
5679
5680 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 * Skip this when the option defaults have not been set yet. Happens when
5682 * main() allocates the first buffer.
5683 */
5684 if (p_cpo != NULL)
5685 {
5686 /*
5687 * Always copy when entering and 'cpo' contains 'S'.
5688 * Don't copy when already initialized.
5689 * Don't copy when 'cpo' contains 's' and not entering.
5690 * 'S' BCO_ENTER initialized 's' should_copy
5691 * yes yes X X TRUE
5692 * yes no yes X FALSE
5693 * no X yes X FALSE
5694 * X no no yes FALSE
5695 * X no no no TRUE
5696 * no yes no X TRUE
5697 */
5698 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5699 && (buf->b_p_initialized
5700 || (!(flags & BCO_ENTER)
5701 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5702 should_copy = FALSE;
5703
5704 if (should_copy || (flags & BCO_ALWAYS))
5705 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005706#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005707 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005708 init_buf_opt_idx();
5709#endif
5710 // Don't copy the options specific to a help buffer when
5711 // BCO_NOHELP is given or the options were initialized already
5712 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5714 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005715 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 save_p_isk = buf->b_p_isk;
5718 buf->b_p_isk = NULL;
5719 }
5720 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005721 * Always free the allocated strings. If not already initialized,
5722 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 */
5724 if (!buf->b_p_initialized)
5725 {
5726 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005727 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005730 switch (*p_ffs)
5731 {
5732 case 'm':
5733 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5734 case 'd':
5735 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5736 case 'u':
5737 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5738 default:
5739 buf->b_p_ff = vim_strsave(p_ff);
5740 }
5741 if (buf->b_p_ff != NULL)
5742 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 buf->b_p_bh = empty_option;
5744 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746 else
5747 free_buf_options(buf, FALSE);
5748
5749 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005750 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 buf->b_p_ai_nopaste = p_ai_nopaste;
5752 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005753 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005755 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 buf->b_p_tw_nopaste = p_tw_nopaste;
5757 buf->b_p_tw_nobin = p_tw_nobin;
5758 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005759 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 buf->b_p_wm_nopaste = p_wm_nopaste;
5761 buf->b_p_wm_nobin = p_wm_nobin;
5762 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005763 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005764 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005765 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005766 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005767 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005769 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005771 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005773 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 buf->b_p_ml_nobin = p_ml_nobin;
5775 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005776 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005777 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005778 buf->b_p_swf = FALSE;
5779 else
5780 {
5781 buf->b_p_swf = p_swf;
5782 COPY_OPT_SCTX(buf, BV_INF);
5783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005785 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005786#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005787 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005788 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005790#ifdef FEAT_COMPL_FUNC
5791 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005792 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005793 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005794 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005795#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005796#ifdef FEAT_EVAL
5797 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005798 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005801 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005803#ifdef FEAT_VARTABS
5804 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005805 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005806 if (p_vsts && p_vsts != empty_option)
5807 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5808 else
5809 buf->b_p_vsts_array = 0;
5810 buf->b_p_vsts_nopaste = p_vsts_nopaste
5811 ? vim_strsave(p_vsts_nopaste) : NULL;
5812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005814 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005816 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817#ifdef FEAT_FOLDING
5818 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005819 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820#endif
5821 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005822 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005823 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005824 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005825 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5826 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005828 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005830 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#ifdef FEAT_SMARTINDENT
5832 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005833 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834#endif
5835 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005836 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837#ifdef FEAT_CINDENT
5838 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005839 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005841 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005843 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005845 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005848 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5850 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005851 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#endif
5853#ifdef FEAT_LISP
5854 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005855 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856#endif
5857#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005858 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005860 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005861 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005862 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005863#endif
5864#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005865 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005866 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005867 (void)compile_cap_prog(&buf->b_s);
5868 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005869 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005870 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005871 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005872 buf->b_s.b_p_spo = vim_strsave(p_spo);
5873 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#endif
5875#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5876 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005877 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005879 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005881 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005882#if defined(FEAT_EVAL)
5883 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005884 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#ifdef FEAT_CRYPT
5887 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005888 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#endif
5890#ifdef FEAT_SEARCHPATH
5891 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005892 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
5894#ifdef FEAT_KEYMAP
5895 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005896 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 buf->b_kmap_state |= KEYMAP_INIT;
5898#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005899#ifdef FEAT_TERMINAL
5900 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005901 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005902#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005903 // This isn't really an option, but copying the langmap and IME
5904 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005906 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005908 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005910 // options that are normally global but also have a local value
5911 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005913 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005914 buf->b_p_bkc = empty_option;
5915 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916#ifdef FEAT_QUICKFIX
5917 buf->b_p_gp = empty_option;
5918 buf->b_p_mp = empty_option;
5919 buf->b_p_efm = empty_option;
5920#endif
5921 buf->b_p_ep = empty_option;
5922 buf->b_p_kp = empty_option;
5923 buf->b_p_path = empty_option;
5924 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005925 buf->b_p_tc = empty_option;
5926 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#ifdef FEAT_FIND_ID
5928 buf->b_p_def = empty_option;
5929 buf->b_p_inc = empty_option;
5930# ifdef FEAT_EVAL
5931 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933# endif
5934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf->b_p_dict = empty_option;
5936 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005937#ifdef FEAT_TEXTOBJ
5938 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005939 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005940#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005941#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5942 buf->b_p_bexpr = empty_option;
5943#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005944#if defined(FEAT_CRYPT)
5945 buf->b_p_cm = empty_option;
5946#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005947#ifdef FEAT_PERSISTENT_UNDO
5948 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005950#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005951#ifdef FEAT_LISP
5952 buf->b_p_lw = empty_option;
5953#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005954 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
5956 /*
5957 * Don't copy the options set by ex_help(), use the saved values,
5958 * when going from a help buffer to a non-help buffer.
5959 * Don't touch these at all when BCO_NOHELP is used and going from
5960 * or to a help buffer.
5961 */
5962 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005965#ifdef FEAT_VARTABS
5966 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5967 tabstop_set(p_vts, &buf->b_p_vts_array);
5968 else
5969 buf->b_p_vts_array = NULL;
5970#endif
5971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 else
5973 {
5974 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005975 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 did_isk = TRUE;
5977 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005978#ifdef FEAT_VARTABS
5979 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005980 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005981 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
5982 tabstop_set(p_vts, &buf->b_p_vts_array);
5983 else
5984 buf->b_p_vts_array = NULL;
5985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 if (buf->b_p_bt[0] == 'h')
5988 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 }
5992 }
5993
5994 /*
5995 * When the options should be copied (ignoring BCO_ALWAYS), set the
5996 * flag that indicates that the options have been initialized.
5997 */
5998 if (should_copy)
5999 buf->b_p_initialized = TRUE;
6000 }
6001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006002 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 if (did_isk)
6004 (void)buf_init_chartab(buf, FALSE);
6005}
6006
6007/*
6008 * Reset the 'modifiable' option and its default value.
6009 */
6010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006011reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
6013 int opt_idx;
6014
6015 curbuf->b_p_ma = FALSE;
6016 p_ma = FALSE;
6017 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006018 if (opt_idx >= 0)
6019 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020}
6021
6022/*
6023 * Set the global value for 'iminsert' to the local value.
6024 */
6025 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006026set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
6028 p_iminsert = curbuf->b_p_iminsert;
6029}
6030
6031/*
6032 * Set the global value for 'imsearch' to the local value.
6033 */
6034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006035set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036{
6037 p_imsearch = curbuf->b_p_imsearch;
6038}
6039
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040static int expand_option_idx = -1;
6041static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6042static int expand_option_flags = 0;
6043
6044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006045set_context_in_set_cmd(
6046 expand_T *xp,
6047 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006048 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006051 long_u flags = 0; // init for GCC
6052 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 char_u *p;
6054 char_u *s;
6055 int is_term_option = FALSE;
6056 int key;
6057
6058 expand_option_flags = opt_flags;
6059
6060 xp->xp_context = EXPAND_SETTINGS;
6061 if (*arg == NUL)
6062 {
6063 xp->xp_pattern = arg;
6064 return;
6065 }
6066 p = arg + STRLEN(arg) - 1;
6067 if (*p == ' ' && *(p - 1) != '\\')
6068 {
6069 xp->xp_pattern = p + 1;
6070 return;
6071 }
6072 while (p > arg)
6073 {
6074 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006075 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 if (*p == ' ' || *p == ',')
6077 {
6078 while (s > arg && *(s - 1) == '\\')
6079 --s;
6080 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006081 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 if (*p == ' ' && ((p - s) & 1) == 0)
6083 {
6084 ++p;
6085 break;
6086 }
6087 --p;
6088 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006089 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {
6091 xp->xp_context = EXPAND_BOOL_SETTINGS;
6092 p += 2;
6093 }
6094 if (STRNCMP(p, "inv", 3) == 0)
6095 {
6096 xp->xp_context = EXPAND_BOOL_SETTINGS;
6097 p += 3;
6098 }
6099 xp->xp_pattern = arg = p;
6100 if (*arg == '<')
6101 {
6102 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006103 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 return;
6105 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 xp->xp_context = EXPAND_NOTHING;
6109 return;
6110 }
6111 nextchar = *++p;
6112 is_term_option = TRUE;
6113 expand_option_name[2] = KEY2TERMCAP0(key);
6114 expand_option_name[3] = KEY2TERMCAP1(key);
6115 }
6116 else
6117 {
6118 if (p[0] == 't' && p[1] == '_')
6119 {
6120 p += 2;
6121 if (*p != NUL)
6122 ++p;
6123 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006124 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 nextchar = *++p;
6126 is_term_option = TRUE;
6127 expand_option_name[2] = p[-2];
6128 expand_option_name[3] = p[-1];
6129 }
6130 else
6131 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006132 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6134 p++;
6135 if (*p == NUL)
6136 return;
6137 nextchar = *p;
6138 *p = NUL;
6139 opt_idx = findoption(arg);
6140 *p = nextchar;
6141 if (opt_idx == -1 || options[opt_idx].var == NULL)
6142 {
6143 xp->xp_context = EXPAND_NOTHING;
6144 return;
6145 }
6146 flags = options[opt_idx].flags;
6147 if (flags & P_BOOL)
6148 {
6149 xp->xp_context = EXPAND_NOTHING;
6150 return;
6151 }
6152 }
6153 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006154 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6156 {
6157 ++p;
6158 nextchar = '=';
6159 }
6160 if ((nextchar != '=' && nextchar != ':')
6161 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6162 {
6163 xp->xp_context = EXPAND_UNSUCCESSFUL;
6164 return;
6165 }
6166 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6167 {
6168 xp->xp_context = EXPAND_OLD_SETTING;
6169 if (is_term_option)
6170 expand_option_idx = -1;
6171 else
6172 expand_option_idx = opt_idx;
6173 xp->xp_pattern = p + 1;
6174 return;
6175 }
6176 xp->xp_context = EXPAND_NOTHING;
6177 if (is_term_option || (flags & P_NUM))
6178 return;
6179
6180 xp->xp_pattern = p + 1;
6181
6182 if (flags & P_EXPAND)
6183 {
6184 p = options[opt_idx].var;
6185 if (p == (char_u *)&p_bdir
6186 || p == (char_u *)&p_dir
6187 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006188 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 || p == (char_u *)&p_rtp
6190#ifdef FEAT_SEARCHPATH
6191 || p == (char_u *)&p_cdpath
6192#endif
6193#ifdef FEAT_SESSION
6194 || p == (char_u *)&p_vdir
6195#endif
6196 )
6197 {
6198 xp->xp_context = EXPAND_DIRECTORIES;
6199 if (p == (char_u *)&p_path
6200#ifdef FEAT_SEARCHPATH
6201 || p == (char_u *)&p_cdpath
6202#endif
6203 )
6204 xp->xp_backslash = XP_BS_THREE;
6205 else
6206 xp->xp_backslash = XP_BS_ONE;
6207 }
6208 else
6209 {
6210 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006211 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 if (p == (char_u *)&p_tags)
6213 xp->xp_backslash = XP_BS_THREE;
6214 else
6215 xp->xp_backslash = XP_BS_ONE;
6216 }
6217 }
6218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006219 // For an option that is a list of file names, find the start of the
6220 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6222 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006223 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (*p == ' ' || *p == ',')
6225 {
6226 s = p;
6227 while (s > xp->xp_pattern && *(s - 1) == '\\')
6228 --s;
6229 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6230 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6231 {
6232 xp->xp_pattern = p + 1;
6233 break;
6234 }
6235 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006236
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006237#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006238 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006239 if (options[opt_idx].var == (char_u *)&p_sps
6240 && STRNCMP(p, "file:", 5) == 0)
6241 {
6242 xp->xp_pattern = p + 5;
6243 break;
6244 }
6245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
6247
6248 return;
6249}
6250
6251 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006252ExpandSettings(
6253 expand_T *xp,
6254 regmatch_T *regmatch,
6255 int *num_file,
6256 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006258 int num_normal = 0; // Nr of matching non-term-code settings
6259 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 int opt_idx;
6261 int match;
6262 int count = 0;
6263 char_u *str;
6264 int loop;
6265 int is_term_opt;
6266 char_u name_buf[MAX_KEY_NAME_LEN];
6267 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006268 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006270 // do this loop twice:
6271 // loop == 0: count the number of matching options
6272 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 for (loop = 0; loop <= 1; ++loop)
6274 {
6275 regmatch->rm_ic = ic;
6276 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6277 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006278 for (match = 0; match < (int)(sizeof(names) / sizeof(char *));
6279 ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6281 {
6282 if (loop == 0)
6283 num_normal++;
6284 else
6285 (*file)[count++] = vim_strsave((char_u *)names[match]);
6286 }
6287 }
6288 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6289 opt_idx++)
6290 {
6291 if (options[opt_idx].var == NULL)
6292 continue;
6293 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6294 && !(options[opt_idx].flags & P_BOOL))
6295 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006296 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (is_term_opt && num_normal > 0)
6298 continue;
6299 match = FALSE;
6300 if (vim_regexec(regmatch, str, (colnr_T)0)
6301 || (options[opt_idx].shortname != NULL
6302 && vim_regexec(regmatch,
6303 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6304 match = TRUE;
6305 else if (is_term_opt)
6306 {
6307 name_buf[0] = '<';
6308 name_buf[1] = 't';
6309 name_buf[2] = '_';
6310 name_buf[3] = str[2];
6311 name_buf[4] = str[3];
6312 name_buf[5] = '>';
6313 name_buf[6] = NUL;
6314 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6315 {
6316 match = TRUE;
6317 str = name_buf;
6318 }
6319 }
6320 if (match)
6321 {
6322 if (loop == 0)
6323 {
6324 if (is_term_opt)
6325 num_term++;
6326 else
6327 num_normal++;
6328 }
6329 else
6330 (*file)[count++] = vim_strsave(str);
6331 }
6332 }
6333 /*
6334 * Check terminal key codes, these are not in the option table
6335 */
6336 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6337 {
6338 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6339 {
6340 if (!isprint(str[0]) || !isprint(str[1]))
6341 continue;
6342
6343 name_buf[0] = 't';
6344 name_buf[1] = '_';
6345 name_buf[2] = str[0];
6346 name_buf[3] = str[1];
6347 name_buf[4] = NUL;
6348
6349 match = FALSE;
6350 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6351 match = TRUE;
6352 else
6353 {
6354 name_buf[0] = '<';
6355 name_buf[1] = 't';
6356 name_buf[2] = '_';
6357 name_buf[3] = str[0];
6358 name_buf[4] = str[1];
6359 name_buf[5] = '>';
6360 name_buf[6] = NUL;
6361
6362 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6363 match = TRUE;
6364 }
6365 if (match)
6366 {
6367 if (loop == 0)
6368 num_term++;
6369 else
6370 (*file)[count++] = vim_strsave(name_buf);
6371 }
6372 }
6373
6374 /*
6375 * Check special key names.
6376 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006377 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6379 {
6380 name_buf[0] = '<';
6381 STRCPY(name_buf + 1, str);
6382 STRCAT(name_buf, ">");
6383
6384 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6385 {
6386 if (loop == 0)
6387 num_term++;
6388 else
6389 (*file)[count++] = vim_strsave(name_buf);
6390 }
6391 }
6392 }
6393 if (loop == 0)
6394 {
6395 if (num_normal > 0)
6396 *num_file = num_normal;
6397 else if (num_term > 0)
6398 *num_file = num_term;
6399 else
6400 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006401 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (*file == NULL)
6403 {
6404 *file = (char_u **)"";
6405 return FAIL;
6406 }
6407 }
6408 }
6409 return OK;
6410}
6411
6412 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006413ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006415 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 char_u *buf;
6417
6418 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006419 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (*file == NULL)
6421 return FAIL;
6422
6423 /*
6424 * For a terminal key code expand_option_idx is < 0.
6425 */
6426 if (expand_option_idx < 0)
6427 {
6428 var = find_termcode(expand_option_name + 2);
6429 if (var == NULL)
6430 expand_option_idx = findoption(expand_option_name);
6431 }
6432
6433 if (expand_option_idx >= 0)
6434 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006435 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 option_value2string(&options[expand_option_idx], expand_option_flags);
6437 var = NameBuff;
6438 }
6439 else if (var == NULL)
6440 var = (char_u *)"";
6441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006442 // A backslash is required before some characters. This is the reverse of
6443 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 buf = vim_strsave_escaped(var, escape_chars);
6445
6446 if (buf == NULL)
6447 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006448 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 return FAIL;
6450 }
6451
6452#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006453 // For MS-Windows et al. we don't double backslashes at the start and
6454 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006455 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 if (var[0] == '\\' && var[1] == '\\'
6457 && expand_option_idx >= 0
6458 && (options[expand_option_idx].flags & P_EXPAND)
6459 && vim_isfilec(var[2])
6460 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006461 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462#endif
6463
6464 *file[0] = buf;
6465 *num_file = 1;
6466 return OK;
6467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
6469/*
6470 * Get the value for the numeric or string option *opp in a nice format into
6471 * NameBuff[]. Must not be called with a hidden option!
6472 */
6473 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006474option_value2string(
6475 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006476 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 char_u *varp;
6479
6480 varp = get_varp_scope(opp, opt_flags);
6481
6482 if (opp->flags & P_NUM)
6483 {
6484 long wc = 0;
6485
6486 if (wc_use_keyname(varp, &wc))
6487 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6488 else if (wc != 0)
6489 STRCPY(NameBuff, transchar((int)wc));
6490 else
6491 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6492 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006493 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006496 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 NameBuff[0] = NUL;
6498#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006499 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006500 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 STRCPY(NameBuff, "*****");
6502#endif
6503 else if (opp->flags & P_EXPAND)
6504 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006505 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 else if ((char_u **)opp->var == &p_pt)
6507 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6508 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006509 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 }
6511}
6512
6513/*
6514 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6515 * printed as a keyname.
6516 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6517 */
6518 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006519wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6522 {
6523 *wcp = *(long *)varp;
6524 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6525 return TRUE;
6526 }
6527 return FALSE;
6528}
6529
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 * Return TRUE if "x" is present in 'shortmess' option, or
6532 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6533 */
6534 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006535shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006537 return p_shm != NULL &&
6538 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 || (vim_strchr(p_shm, 'a') != NULL
6540 && vim_strchr((char_u *)SHM_A, x) != NULL));
6541}
6542
6543/*
6544 * paste_option_changed() - Called after p_paste was set or reset.
6545 */
6546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006547paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 static int old_p_paste = FALSE;
6550 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006551 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552#ifdef FEAT_CMDL_INFO
6553 static int save_ru = 0;
6554#endif
6555#ifdef FEAT_RIGHTLEFT
6556 static int save_ri = 0;
6557 static int save_hkmap = 0;
6558#endif
6559 buf_T *buf;
6560
6561 if (p_paste)
6562 {
6563 /*
6564 * Paste switched from off to on.
6565 * Save the current values, so they can be restored later.
6566 */
6567 if (!old_p_paste)
6568 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006569 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006570 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
6572 buf->b_p_tw_nopaste = buf->b_p_tw;
6573 buf->b_p_wm_nopaste = buf->b_p_wm;
6574 buf->b_p_sts_nopaste = buf->b_p_sts;
6575 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006576 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006577#ifdef FEAT_VARTABS
6578 if (buf->b_p_vsts_nopaste)
6579 vim_free(buf->b_p_vsts_nopaste);
6580 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6581 ? vim_strsave(buf->b_p_vsts) : NULL;
6582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
6584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006585 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006587 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588#ifdef FEAT_CMDL_INFO
6589 save_ru = p_ru;
6590#endif
6591#ifdef FEAT_RIGHTLEFT
6592 save_ri = p_ri;
6593 save_hkmap = p_hkmap;
6594#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006595 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006596 p_ai_nopaste = p_ai;
6597 p_et_nopaste = p_et;
6598 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 p_tw_nopaste = p_tw;
6600 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006601#ifdef FEAT_VARTABS
6602 if (p_vsts_nopaste)
6603 vim_free(p_vsts_nopaste);
6604 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
6607
6608 /*
6609 * Always set the option values, also when 'paste' is set when it is
6610 * already on.
6611 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006612 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006613 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006615 buf->b_p_tw = 0; // textwidth is 0
6616 buf->b_p_wm = 0; // wrapmargin is 0
6617 buf->b_p_sts = 0; // softtabstop is 0
6618 buf->b_p_ai = 0; // no auto-indent
6619 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006620#ifdef FEAT_VARTABS
6621 if (buf->b_p_vsts)
6622 free_string_option(buf->b_p_vsts);
6623 buf->b_p_vsts = empty_option;
6624 if (buf->b_p_vsts_array)
6625 vim_free(buf->b_p_vsts_array);
6626 buf->b_p_vsts_array = 0;
6627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 }
6629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006630 // set global options
6631 p_sm = 0; // no showmatch
6632 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006635 status_redraw_all(); // redraw to remove the ruler
6636 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#endif
6638#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006639 p_ri = 0; // no reverse insert
6640 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006642 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 p_tw = 0;
6644 p_wm = 0;
6645 p_sts = 0;
6646 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006647#ifdef FEAT_VARTABS
6648 if (p_vsts)
6649 free_string_option(p_vsts);
6650 p_vsts = empty_option;
6651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 }
6653
6654 /*
6655 * Paste switched from on to off: Restore saved values.
6656 */
6657 else if (old_p_paste)
6658 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006659 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006660 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
6662 buf->b_p_tw = buf->b_p_tw_nopaste;
6663 buf->b_p_wm = buf->b_p_wm_nopaste;
6664 buf->b_p_sts = buf->b_p_sts_nopaste;
6665 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006666 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006667#ifdef FEAT_VARTABS
6668 if (buf->b_p_vsts)
6669 free_string_option(buf->b_p_vsts);
6670 buf->b_p_vsts = buf->b_p_vsts_nopaste
6671 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6672 if (buf->b_p_vsts_array)
6673 vim_free(buf->b_p_vsts_array);
6674 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6675 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6676 else
6677 buf->b_p_vsts_array = 0;
6678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 }
6680
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006681 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006683 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006686 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 p_ru = save_ru;
6688#endif
6689#ifdef FEAT_RIGHTLEFT
6690 p_ri = save_ri;
6691 p_hkmap = save_hkmap;
6692#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006693 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006694 p_ai = p_ai_nopaste;
6695 p_et = p_et_nopaste;
6696 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 p_tw = p_tw_nopaste;
6698 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006699#ifdef FEAT_VARTABS
6700 if (p_vsts)
6701 free_string_option(p_vsts);
6702 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705
6706 old_p_paste = p_paste;
6707}
6708
6709/*
6710 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6711 *
6712 * Reset 'compatible' and set the values for options that didn't get set yet
6713 * to the Vim defaults.
6714 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006715 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 */
6717 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006718vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006720 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006721 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006722 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
6724 if (!option_was_set((char_u *)"cp"))
6725 {
6726 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006727 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6729 set_option_default(opt_idx, OPT_FREE, FALSE);
6730 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006731 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006733
6734 if (fname != NULL)
6735 {
6736 p = vim_getenv(envname, &dofree);
6737 if (p == NULL)
6738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006740 p = FullName_save(fname, FALSE);
6741 if (p != NULL)
6742 {
6743 vim_setenv(envname, p);
6744 vim_free(p);
6745 }
6746 }
6747 else if (dofree)
6748 vim_free(p);
6749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750}
6751
6752/*
6753 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6754 */
6755 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006756change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006758 int opt_idx;
6759
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (p_cp != on)
6761 {
6762 p_cp = on;
6763 compatible_set();
6764 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006765 opt_idx = findoption((char_u *)"cp");
6766 if (opt_idx >= 0)
6767 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768}
6769
6770/*
6771 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006772 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 */
6774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006775option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 int idx;
6778
6779 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006780 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 return FALSE;
6782 if (options[idx].flags & P_WAS_SET)
6783 return TRUE;
6784 return FALSE;
6785}
6786
6787/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006788 * Reset the flag indicating option "name" was set.
6789 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006790 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006791reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006792{
6793 int idx = findoption(name);
6794
6795 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006796 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006797 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006798 return OK;
6799 }
6800 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006801}
6802
6803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 * compatible_set() - Called when 'compatible' has been set or unset.
6805 *
6806 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6807 * flag) to a Vi compatible value.
6808 * When 'compatible' is unset: Set all options that have a different default
6809 * for Vim (without the P_VI_DEF flag) to that default.
6810 */
6811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006812compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
6814 int opt_idx;
6815
Bram Moolenaardac13472019-09-16 21:06:21 +02006816 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6818 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6819 set_option_default(opt_idx, OPT_FREE, p_cp);
6820 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006821 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822}
6823
Bram Moolenaardac13472019-09-16 21:06:21 +02006824#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826/*
6827 * fill_breakat_flags() -- called when 'breakat' changes value.
6828 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006829 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006830fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006832 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 int i;
6834
6835 for (i = 0; i < 256; i++)
6836 breakat_flags[i] = FALSE;
6837
6838 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006839 for (p = p_breakat; *p; p++)
6840 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842#endif
6843
6844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 * Check if backspacing over something is allowed.
6846 */
6847 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006848can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006849 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006851#ifdef FEAT_JOB_CHANNEL
6852 if (what == BS_START && bt_prompt(curbuf))
6853 return FALSE;
6854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 switch (*p_bs)
6856 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006857 case '3': return TRUE;
6858 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 case '1': return (what != BS_START);
6860 case '0': return FALSE;
6861 }
6862 return vim_strchr(p_bs, what) != NULL;
6863}
6864
6865/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006866 * Return the effective 'scrolloff' value for the current window, using the
6867 * global value when appropriate.
6868 */
6869 long
6870get_scrolloff_value(void)
6871{
6872 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6873}
6874
6875/*
6876 * Return the effective 'sidescrolloff' value for the current window, using the
6877 * global value when appropriate.
6878 */
6879 long
6880get_sidescrolloff_value(void)
6881{
6882 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6883}
6884
6885/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006886 * Get the local or global value of 'backupcopy'.
6887 */
6888 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006890{
6891 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6892}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006893
Bram Moolenaaree857022019-11-09 23:26:40 +01006894#if defined(FEAT_LINEBREAK) || defined(PROTO)
6895/*
6896 * Get the local or global value of 'showbreak'.
6897 */
6898 char_u *
6899get_showbreak_value(win_T *win)
6900{
6901 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6902 return p_sbr;
6903 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6904 return empty_option;
6905 return win->w_p_sbr;
6906}
6907#endif
6908
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006909#if defined(FEAT_EVAL) || defined(PROTO)
6910/*
6911 * Get window or buffer local options.
6912 */
6913 dict_T *
6914get_winbuf_options(int bufopt)
6915{
6916 dict_T *d;
6917 int opt_idx;
6918
6919 d = dict_alloc();
6920 if (d == NULL)
6921 return NULL;
6922
Bram Moolenaardac13472019-09-16 21:06:21 +02006923 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006924 {
6925 struct vimoption *opt = &options[opt_idx];
6926
6927 if ((bufopt && (opt->indir & PV_BUF))
6928 || (!bufopt && (opt->indir & PV_WIN)))
6929 {
6930 char_u *varp = get_varp(opt);
6931
6932 if (varp != NULL)
6933 {
6934 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006935 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006936 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006937 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006938 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006939 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006940 }
6941 }
6942 }
6943
6944 return d;
6945}
6946#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006947
Bram Moolenaardac13472019-09-16 21:06:21 +02006948#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006949/*
6950 * This is called when 'culopt' is changed
6951 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006952 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006953fill_culopt_flags(char_u *val, win_T *wp)
6954{
6955 char_u *p;
6956 char_u culopt_flags_new = 0;
6957
6958 if (val == NULL)
6959 p = wp->w_p_culopt;
6960 else
6961 p = val;
6962 while (*p != NUL)
6963 {
6964 if (STRNCMP(p, "line", 4) == 0)
6965 {
6966 p += 4;
6967 culopt_flags_new |= CULOPT_LINE;
6968 }
6969 else if (STRNCMP(p, "both", 4) == 0)
6970 {
6971 p += 4;
6972 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
6973 }
6974 else if (STRNCMP(p, "number", 6) == 0)
6975 {
6976 p += 6;
6977 culopt_flags_new |= CULOPT_NBR;
6978 }
6979 else if (STRNCMP(p, "screenline", 10) == 0)
6980 {
6981 p += 10;
6982 culopt_flags_new |= CULOPT_SCRLINE;
6983 }
6984
6985 if (*p != ',' && *p != NUL)
6986 return FAIL;
6987 if (*p == ',')
6988 ++p;
6989 }
6990
6991 // Can't have both "line" and "screenline".
6992 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
6993 return FAIL;
6994 wp->w_p_culopt_flags = culopt_flags_new;
6995
6996 return OK;
6997}
6998#endif