blob: dd44fe4ac4441b0828a928c486b563b5befff570 [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);
K.Takataeeec2542021-06-02 13:28:16 +0200148 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
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
K.Takataf883d902021-05-30 18:04:19 +0200433# ifdef MSWIN
434 // MS-Windows has builtin support for conversion to and from Unicode, using
435 // "utf-8" for 'encoding' should work best for most users.
436 p = vim_strsave((char_u *)ENC_DFLT);
437# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200439 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (p != NULL)
443 {
444 char_u *save_enc;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200447 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 save_enc = p_enc;
449 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000450 if (STRCMP(p_enc, "gb18030") == 0)
451 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // We don't support "gb18030", but "cp936" is a good substitute
453 // for practical purposes, thus use that. It's not an alias to
454 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000455 p_enc = vim_strsave((char_u *)"cp936");
456 vim_free(p);
457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 if (mb_init() == NULL)
459 {
460 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000461 if (opt_idx >= 0)
462 {
463 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
464 options[opt_idx].flags |= P_DEF_ALLOCED;
465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
Bram Moolenaard0573012017-10-28 21:11:06 +0200467#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100468 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100470 // Adjust the default for 'isprint' and 'iskeyword' to match
471 // latin1. Also set the defaults for when 'nocompatible' is
472 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000473 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000474 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000475 set_string_option_direct((char_u *)"isk", -1,
476 ISK_LATIN1, OPT_FREE, SID_NONE);
477 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000478 if (opt_idx >= 0)
479 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000480 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000483 (void)init_chartab();
484 }
485#endif
486
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200487#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 // Win32 console: When GetACP() returns a different value from
489 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200490 if (
491# ifdef VIMDLL
492 (!gui.in_use && !gui.starting) &&
493# endif
494 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 char buf[50];
497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100498 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
499 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100500 if (GetConsoleCP() == 0)
501 sprintf(buf, "cp%ld", (long)GetACP());
502 else
503 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 p_tenc = vim_strsave((char_u *)buf);
505 if (p_tenc != NULL)
506 {
507 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000508 if (opt_idx >= 0)
509 {
510 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
511 options[opt_idx].flags |= P_DEF_ALLOCED;
512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 convert_setup(&input_conv, p_tenc, p_enc);
514 convert_setup(&output_conv, p_enc, p_tenc);
515 }
516 else
517 p_tenc = empty_option;
518 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100519#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100520#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000522 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525 else
526 {
527 vim_free(p_enc);
528 p_enc = save_enc;
529 }
530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 set_helplang_default(get_mess_lang());
535#endif
536}
537
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200538static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
539
540/*
541 * Set the "fileencodings" option to the default value for when 'encoding' is
542 * utf-8.
543 */
544 void
545set_fencs_unicode()
546{
547 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
548 OPT_FREE, 0);
549}
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/*
552 * Set an option to its default value.
553 * This does not take care of side effects!
554 */
555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100556set_option_default(
557 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100558 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
559 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 char_u *varp; // pointer to variable for current option
562 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000564 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
566
567 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
568 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
572 if (flags & P_STRING)
573 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200574 // 'fencs' default value depends on 'encoding'
575 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
576 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100577 // Use set_string_option_direct() for local options to handle
578 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200579 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200580 set_string_option_direct(NULL, opt_idx,
581 options[opt_idx].def_val[dvi], opt_flags, 0);
582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200584 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
585 free_string_option(*(char_u **)(varp));
586 *(char_u **)varp = options[opt_idx].def_val[dvi];
587 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589 }
590 else if (flags & P_NUM)
591 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000592 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 win_comp_scroll(curwin);
594 else
595 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100596 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
597
598 if ((long *)varp == &curwin->w_p_so
599 || (long *)varp == &curwin->w_p_siso)
600 // 'scrolloff' and 'sidescrolloff' local values have a
601 // different default value than the global default.
602 *(long *)varp = -1;
603 else
604 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (both)
607 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100608 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 // the cast to long is required for Manx C, long_i is needed for
614 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000615 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000616#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000618 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
619 *(int *)varp = FALSE;
620#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100621 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (both)
623 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
624 *(int *)varp;
625 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000628 flagsp = insecure_flag(opt_idx, opt_flags);
629 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631
632#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200633 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635}
636
637/*
638 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200639 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 */
641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100642set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100643 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000647 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaardac13472019-09-16 21:06:21 +0200649 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200650 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200651 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100652 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200653# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200654 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200655 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200656# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100657 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 set_option_default(i, opt_flags, p_cp);
659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000661 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200663#ifdef FEAT_CINDENT
664 parse_cino(curbuf);
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
668/*
669 * Set the Vi-default value of a string option.
670 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100671 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 static void
674set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 char_u *p;
677 int opt_idx;
678
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100679 if (escape && vim_strchr(val, ' ') != NULL)
680 p = vim_strsave_escaped(val, (char_u *)" ");
681 else
682 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000686 if (opt_idx >= 0)
687 {
688 if (options[opt_idx].flags & P_DEF_ALLOCED)
689 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
690 options[opt_idx].def_val[VI_DEFAULT] = p;
691 options[opt_idx].flags |= P_DEF_ALLOCED;
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694}
695
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100696 void
697set_string_default(char *name, char_u *val)
698{
699 set_string_default_esc(name, val, FALSE);
700}
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200703 * For an option value that contains comma separated items, find "newval" in
704 * "origval". Return NULL if not found.
705 */
706 static char_u *
707find_dup_item(char_u *origval, char_u *newval, long_u flags)
708{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200709 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200710 size_t newlen;
711 char_u *s;
712
713 if (origval == NULL)
714 return NULL;
715
716 newlen = STRLEN(newval);
717 for (s = origval; *s != NUL; ++s)
718 {
719 if ((!(flags & P_COMMA)
720 || s == origval
721 || (s[-1] == ',' && !(bs & 1)))
722 && STRNCMP(s, newval, newlen) == 0
723 && (!(flags & P_COMMA)
724 || s[newlen] == ','
725 || s[newlen] == NUL))
726 return s;
727 // Count backslashes. Only a comma with an even number of backslashes
728 // or a single backslash preceded by a comma before it is recognized as
729 // a separator.
730 if ((s > origval + 1
731 && s[-1] == '\\'
732 && s[-2] != ',')
733 || (s == origval + 1
734 && s[-1] == '\\'))
735 ++bs;
736 else
737 bs = 0;
738 }
739 return NULL;
740}
741
742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * Set the Vi-default value of a number option.
744 * Used for 'lines' and 'columns'.
745 */
746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100747set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000749 int opt_idx;
750
751 opt_idx = findoption((char_u *)name);
752 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000753 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754}
755
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200756/*
757 * Set all window-local and buffer-local options to the Vim default.
758 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200759 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760 */
761 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200762set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200763{
764 win_T *save_curwin = curwin;
765 int i;
766
767 curwin = wp;
768 curbuf = curwin->w_buffer;
769 block_autocmds();
770
Bram Moolenaardac13472019-09-16 21:06:21 +0200771 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200772 {
773 struct vimoption *p = &(options[i]);
774 char_u *varp = get_varp_scope(p, OPT_LOCAL);
775
776 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200777 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 && !(options[i].flags & P_NODEFAULT)
779 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200780 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200781 }
782
783 unblock_autocmds();
784 curwin = save_curwin;
785 curbuf = curwin->w_buffer;
786}
787
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000788#if defined(EXITFREE) || defined(PROTO)
789/*
790 * Free all options.
791 */
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000794{
795 int i;
796
Bram Moolenaardac13472019-09-16 21:06:21 +0200797 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000798 {
799 if (options[i].indir == PV_NONE)
800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100802 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000803 free_string_option(*(char_u **)options[i].var);
804 if (options[i].flags & P_DEF_ALLOCED)
805 free_string_option(options[i].def_val[VI_DEFAULT]);
806 }
807 else if (options[i].var != VAR_WIN
808 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // buffer-local option: free global value
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000810 free_string_option(*(char_u **)options[i].var);
811 }
812}
813#endif
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Initialize the options, part two: After getting Rows and Columns and
818 * setting 'term'.
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 int idx;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100826 * 'scroll' defaults to half the window height. The stored default is zero,
827 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000830 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 comp_col();
833
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 /*
835 * 'window' is only for backwards compatibility with Vi.
836 * Default is Rows - 1.
837 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000838 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 p_window = Rows - 1;
840 set_number_default("window", Rows - 1);
841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000844 /*
845 * If 'background' wasn't set by the user, try guessing the value,
846 * depending on the terminal name. Only need to check for terminals
847 * with a dark background, that can handle color.
848 */
849 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000850 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
851 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000853 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // don't mark it as set, when starting the GUI it may be
855 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000859
860#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000862#endif
863#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000865#endif
866#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/*
872 * Initialize the options, part three: After reading the .vimrc
873 */
874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878/*
879 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
880 * This is done after other initializations, where 'shell' might have been
881 * set, but only if they have not been set before.
882 */
883 char_u *p;
884 int idx_srr;
885 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int idx_sp;
888 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000892 if (idx_srr < 0)
893 do_srr = FALSE;
894 else
895 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (idx_sp < 0)
899 do_sp = FALSE;
900 else
901 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200903 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (p != NULL)
905 {
906 /*
907 * Default for p_sp is "| tee", for p_srr is ">".
908 * For known shells it is changed here to include stderr.
909 */
910 if ( fnamecmp(p, "csh") == 0
911 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "csh.exe") == 0
914 || fnamecmp(p, "tcsh.exe") == 0
915# endif
916 )
917 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100918# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (do_sp)
920 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
927 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (do_srr)
930 {
931 p_srr = (char_u *)">&";
932 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
933 }
934 }
935 else
Natanael Copa56318362021-05-06 18:46:35 +0200936 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if ( fnamecmp(p, "sh") == 0
938 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200939 || fnamecmp(p, "mksh") == 0
940 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000942 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200944 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200945 || fnamecmp(p, "ash") == 0
946 || fnamecmp(p, "dash") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100947# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 || fnamecmp(p, "cmd") == 0
949 || fnamecmp(p, "sh.exe") == 0
950 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200951 || fnamecmp(p, "mksh.exe") == 0
952 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000954 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 || fnamecmp(p, "bash.exe") == 0
956 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200957 || fnamecmp(p, "dash.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100959 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100961# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (do_sp)
963 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100964# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100966# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100968# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
970 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 if (do_srr)
973 {
974 p_srr = (char_u *)">%s 2>&1";
975 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
976 }
977 }
978 vim_free(p);
979 }
980#endif
981
Bram Moolenaar4f974752019-02-17 17:44:42 +0100982#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100984 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
985 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 * This is done after other initializations, where 'shell' might have been
987 * set, but only if they have not been set before. Default for p_shcf is
988 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100989 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000991 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 int idx3;
994
995 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000996 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
998 p_shcf = (char_u *)"-c";
999 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1000 }
1001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001002 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001004 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
1006 p_sxq = (char_u *)"\"";
1007 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1011 {
1012 int idx3;
1013
1014 /*
1015 * cmd.exe on Windows will strip the first and last double quote given
1016 * on the command line, e.g. most of the time things like:
1017 * cmd /c "my path/to/echo" "my args to echo"
1018 * become:
1019 * my path/to/echo" "my args to echo
1020 * when executed.
1021 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001022 * To avoid this, set shellxquote to surround the command in
1023 * parenthesis. This appears to make most commands work, without
1024 * breaking commands that worked previously, such as
1025 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001026 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001027 idx3 = findoption((char_u *)"sxq");
1028 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1029 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001030 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001031 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1032 }
1033
Bram Moolenaara64ba222012-02-12 23:23:31 +01001034 idx3 = findoption((char_u *)"shcf");
1035 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1036 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001037 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001038 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1039 }
1040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041#endif
1042
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001043 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001044 {
1045 int idx_ffs = findoption((char_u *)"ffs");
1046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001048 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1049 set_fileformat(default_fileformat(), OPT_LOCAL);
1050 }
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052#ifdef FEAT_TITLE
1053 set_title_defaults();
1054#endif
1055}
1056
1057#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1058/*
1059 * When 'helplang' is still at its default value, set it to "lang".
1060 * Only the first two characters of "lang" are used.
1061 */
1062 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
1065 int idx;
1066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001067 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 return;
1069 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001070 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
1072 if (options[idx].flags & P_ALLOCED)
1073 free_string_option(p_hlg);
1074 p_hlg = vim_strsave(lang);
1075 if (p_hlg == NULL)
1076 p_hlg = empty_option;
1077 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001078 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001079 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001080 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1081 {
1082 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1083 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1084 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001085 // any C like setting, such as C.UTF-8, becomes "en"
1086 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1087 {
1088 p_hlg[0] = 'e';
1089 p_hlg[1] = 'n';
1090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 options[idx].flags |= P_ALLOCED;
1094 }
1095}
1096#endif
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#ifdef FEAT_TITLE
1099/*
1100 * 'title' and 'icon' only default to true if they have not been set or reset
1101 * in .vimrc and we can read the old value.
1102 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1103 * they can be reset. This reduces startup time when using X on a remote
1104 * machine.
1105 */
1106 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001107set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 int idx1;
1110 long val;
1111
1112 /*
1113 * If GUI is (going to be) used, we can always set the window title and
1114 * icon name. Saves a bit of time, because the X11 display server does
1115 * not need to be contacted.
1116 */
1117 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001118 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
1120#ifdef FEAT_GUI
1121 if (gui.starting || gui.in_use)
1122 val = TRUE;
1123 else
1124#endif
1125 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001126 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 p_title = val;
1128 }
1129 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001130 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
1132#ifdef FEAT_GUI
1133 if (gui.starting || gui.in_use)
1134 val = TRUE;
1135 else
1136#endif
1137 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001138 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 p_icon = val;
1140 }
1141}
1142#endif
1143
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001144 void
1145ex_set(exarg_T *eap)
1146{
1147 int flags = 0;
1148
1149 if (eap->cmdidx == CMD_setlocal)
1150 flags = OPT_LOCAL;
1151 else if (eap->cmdidx == CMD_setglobal)
1152 flags = OPT_GLOBAL;
1153#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001154 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001155 ex_options(eap);
1156 else
1157#endif
1158 {
1159 if (eap->forceit)
1160 flags |= OPT_ONECOLUMN;
1161 (void)do_set(eap->arg, flags);
1162 }
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165/*
1166 * Parse 'arg' for option settings.
1167 *
1168 * 'arg' may be IObuff, but only when no errors can be present and option
1169 * does not need to be expanded with option_expand().
1170 * "opt_flags":
1171 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001172 * OPT_GLOBAL for ":setglobal"
1173 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001175 * OPT_WINONLY to only set window-local options
1176 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 *
1178 * returns FAIL if an error is detected, OK otherwise
1179 */
1180 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001181do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001182 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001183 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
1185 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001186 char *errmsg;
1187 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1190 int nextchar; // next non-white char after option name
1191 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int len;
1193 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001194 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001196 long_u flags; // flags for current option
1197 char_u *varp = NULL; // pointer to variable for current option
1198 int did_show = FALSE; // already showed one value
1199 int adding; // "opt+=arg"
1200 int prepending; // "opt^=arg"
1201 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 int cp_val = 0;
1203 char_u key_name[2];
1204
1205 if (*arg == NUL)
1206 {
1207 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001208 did_show = TRUE;
1209 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001212 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
1214 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001215 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001217 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1218 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
1220 /*
1221 * ":set all" show all options.
1222 * ":set all&" set all options to their default value.
1223 */
1224 arg += 3;
1225 if (*arg == '&')
1226 {
1227 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001228 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001230 didset_options();
1231 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001232 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001237 did_show = TRUE;
1238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001240 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
1242 showoptions(2, opt_flags);
1243 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001244 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 arg += 7;
1246 }
1247 else
1248 {
1249 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001250 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 prefix = 0;
1253 arg += 2;
1254 }
1255 else if (STRNCMP(arg, "inv", 3) == 0)
1256 {
1257 prefix = 2;
1258 arg += 3;
1259 }
1260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 key = 0;
1263 if (*arg == '<')
1264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001266 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1268 len = 5;
1269 else
1270 {
1271 len = 1;
1272 while (arg[len] != NUL && arg[len] != '>')
1273 ++len;
1274 }
1275 if (arg[len] != '>')
1276 {
1277 errmsg = e_invarg;
1278 goto skip;
1279 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001280 arg[len] = NUL; // put NUL after name
1281 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001285 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 else
1288 {
1289 len = 0;
1290 /*
1291 * The two characters after "t_" may not be alphanumeric.
1292 */
1293 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1294 len = 4;
1295 else
1296 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1297 ++len;
1298 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001299 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001301 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001303 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 afterchar = arg[len];
1308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001309 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001310 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 ++len;
1312
1313 adding = FALSE;
1314 prepending = FALSE;
1315 removing = FALSE;
1316 if (arg[len] != NUL && arg[len + 1] == '=')
1317 {
1318 if (arg[len] == '+')
1319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 ++len;
1322 }
1323 else if (arg[len] == '^')
1324 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001325 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 ++len;
1327 }
1328 else if (arg[len] == '-')
1329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001330 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 ++len;
1332 }
1333 }
1334 nextchar = arg[len];
1335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001336 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001338 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 goto skip;
1340 }
1341
1342 if (opt_idx >= 0)
1343 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001344 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001346 // Only give an error message when requesting the value of
1347 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1349 && (!(options[opt_idx].flags & P_BOOL)
1350 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001351 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 goto skip;
1353 }
1354
1355 flags = options[opt_idx].flags;
1356 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1357 }
1358 else
1359 {
1360 flags = P_STRING;
1361 if (key < 0)
1362 {
1363 key_name[0] = KEY2TERMCAP0(key);
1364 key_name[1] = KEY2TERMCAP1(key);
1365 }
1366 else
1367 {
1368 key_name[0] = KS_KEY;
1369 key_name[1] = (key & 0xff);
1370 }
1371 }
1372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001373 // Skip all options that are not window-local (used when showing
1374 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001375 if ((opt_flags & OPT_WINONLY)
1376 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1377 goto skip;
1378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001379 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001380 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1381 && options[opt_idx].var == VAR_WIN)
1382 goto skip;
1383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001384 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001385 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001387 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001388 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001389 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001390 goto skip;
1391 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001392 if ((flags & P_MLE) && !p_mle)
1393 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001394 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001395 goto skip;
1396 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001397#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001398 // In diff mode some options are overruled. This avoids that
1399 // 'foldmethod' becomes "marker" instead of "diff" and that
1400 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001401 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001402 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001403 && (
1404#ifdef FEAT_FOLDING
1405 options[opt_idx].indir == PV_FDM ||
1406#endif
1407 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001408 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411
1412#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001413 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001414 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001416 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 goto skip;
1418 }
1419#endif
1420
1421 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1422 {
1423 arg += len;
1424 cp_val = p_cp;
1425 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1426 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001427 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
1429 cp_val = FALSE;
1430 arg += 3;
1431 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001432 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
1434 cp_val = TRUE;
1435 arg += 2;
1436 }
1437 }
1438 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001439 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 {
1441 errmsg = e_trailing;
1442 goto skip;
1443 }
1444 }
1445
1446 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001447 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001448 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
1450 if (nextchar == '?'
1451 || (prefix == 1
1452 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1453 && !(flags & P_BOOL)))
1454 {
1455 /*
1456 * print value
1457 */
1458 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001459 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 else
1461 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001462 gotocmdline(TRUE); // cursor at status line
1463 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 if (opt_idx >= 0)
1466 {
1467 showoneopt(&options[opt_idx], opt_flags);
1468#ifdef FEAT_EVAL
1469 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001470 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001472 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001473 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001474 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001475 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001476 (int)options[opt_idx].indir & PV_MASK]);
1477 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001478 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001479 (int)options[opt_idx].indir & PV_MASK]);
1480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481#endif
1482 }
1483 else
1484 {
1485 char_u *p;
1486
1487 p = find_termcode(key_name);
1488 if (p == NULL)
1489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001490 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 goto skip;
1492 }
1493 else
1494 (void)show_one_termcode(key_name, p, TRUE);
1495 }
1496 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001497 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 errmsg = e_trailing;
1499 }
1500 else
1501 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001502 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001503 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001504
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001505 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
1507 if (nextchar == '=' || nextchar == ':')
1508 {
1509 errmsg = e_invarg;
1510 goto skip;
1511 }
1512
1513 /*
1514 * ":set opt!": invert
1515 * ":set opt&": reset to default value
1516 * ":set opt<": reset to global value
1517 */
1518 if (nextchar == '!')
1519 value = *(int *)(varp) ^ 1;
1520 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001521 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 ((flags & P_VI_DEF) || cp_val)
1523 ? VI_DEFAULT : VIM_DEFAULT];
1524 else if (nextchar == '<')
1525 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001526 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if ((int *)varp == &curbuf->b_p_ar
1528 && opt_flags == OPT_LOCAL)
1529 value = -1;
1530 else
1531 value = *(int *)get_varp_scope(&(options[opt_idx]),
1532 OPT_GLOBAL);
1533 }
1534 else
1535 {
1536 /*
1537 * ":set invopt": invert
1538 * ":set opt" or ":set noopt": set or reset
1539 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001540 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 {
1542 errmsg = e_trailing;
1543 goto skip;
1544 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001545 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 value = *(int *)(varp) ^ 1;
1547 else
1548 value = prefix;
1549 }
1550
1551 errmsg = set_bool_option(opt_idx, varp, (int)value,
1552 opt_flags);
1553 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001554 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1557 || prefix != 1)
1558 {
1559 errmsg = e_invarg;
1560 goto skip;
1561 }
1562
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001563 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565 /*
1566 * Different ways to set a number option:
1567 * & set to default value
1568 * < set to global value
1569 * <xx> accept special key codes for 'wildchar'
1570 * c accept any non-digit for 'wildchar'
1571 * [-]0-9 set number
1572 * other error
1573 */
1574 ++arg;
1575 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001576 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 ((flags & P_VI_DEF) || cp_val)
1578 ? VI_DEFAULT : VIM_DEFAULT];
1579 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001580 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1582 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001583 if ((long *)varp == &curbuf->b_p_ul
1584 && opt_flags == OPT_LOCAL)
1585 value = NO_LOCAL_UNDOLEVEL;
1586 else
1587 value = *(long *)get_varp_scope(
1588 &(options[opt_idx]), OPT_GLOBAL);
1589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 else if (((long *)varp == &p_wc
1591 || (long *)varp == &p_wcm)
1592 && (*arg == '<'
1593 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001594 || (*arg != NUL
1595 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 && !VIM_ISDIGIT(*arg))))
1597 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001598 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (value == 0 && (long *)varp != &p_wcm)
1600 {
1601 errmsg = e_invarg;
1602 goto skip;
1603 }
1604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001607 // Allow negative (for 'undolevels'), octal and
1608 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001609 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001610 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001611 if (i == 0 || (arg[i] != NUL
1612 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001614 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 goto skip;
1616 }
1617 }
1618 else
1619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001620 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 goto skip;
1622 }
1623
1624 if (adding)
1625 value = *(long *)varp + value;
1626 if (prepending)
1627 value = *(long *)varp * value;
1628 if (removing)
1629 value = *(long *)varp - value;
1630 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001631 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001633 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001635 char_u *save_arg = NULL;
1636 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001637 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001638 char_u *newval;
1639 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001640 char_u *origval_l = NULL;
1641 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001642#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001643 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001644 char_u *saved_origval_l = NULL;
1645 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001646 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001647#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001648 unsigned newlen;
1649 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001650 int new_value_alloced; // new string option
1651 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001653 // When using ":set opt=val" for a global option
1654 // with a local value the local value will be
1655 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001657 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 varp = options[opt_idx].var;
1659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001660 // The old value is kept until we are sure that the
1661 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001663
Bram Moolenaard7c96872019-06-15 17:12:48 +02001664 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1665 {
1666 origval_l = *(char_u **)get_varp_scope(
1667 &(options[opt_idx]), OPT_LOCAL);
1668 origval_g = *(char_u **)get_varp_scope(
1669 &(options[opt_idx]), OPT_GLOBAL);
1670
1671 // A global-local string option might have an empty
1672 // option as value to indicate that the global
1673 // value should be used.
1674 if (((int)options[opt_idx].indir & PV_BOTH)
1675 && origval_l == empty_option)
1676 origval_l = origval_g;
1677 }
1678
1679 // When setting the local value of a global
1680 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001681 if (((int)options[opt_idx].indir & PV_BOTH)
1682 && (opt_flags & OPT_LOCAL))
1683 origval = *(char_u **)get_varp(
1684 &options[opt_idx]);
1685 else
1686 origval = oldval;
1687
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001688 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 newval = options[opt_idx].def_val[
1691 ((flags & P_VI_DEF) || cp_val)
1692 ? VI_DEFAULT : VIM_DEFAULT];
1693 if ((char_u **)varp == &p_bg)
1694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001695 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#ifdef FEAT_GUI
1697 if (gui.in_use)
1698 newval = gui_bg_default();
1699 else
1700#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001701 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001703 else if ((char_u **)varp == &p_fencs && enc_utf8)
1704 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001706 // expand environment variables and ~ (since the
1707 // default value was already expanded, only
1708 // required when an environment variable was set
1709 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (newval == NULL)
1711 newval = empty_option;
1712 else
1713 {
1714 s = option_expand(opt_idx, newval);
1715 if (s == NULL)
1716 s = newval;
1717 newval = vim_strsave(s);
1718 }
1719 new_value_alloced = TRUE;
1720 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001721 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 {
1723 newval = vim_strsave(*(char_u **)get_varp_scope(
1724 &(options[opt_idx]), OPT_GLOBAL));
1725 new_value_alloced = TRUE;
1726 }
1727 else
1728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 /*
1732 * Set 'keywordprg' to ":help" if an empty
1733 * value was passed to :set by the user.
1734 * Misuse errbuf[] for the resulting string.
1735 */
1736 if (varp == (char_u *)&p_kp
1737 && (*arg == NUL || *arg == ' '))
1738 {
1739 STRCPY(errbuf, ":help");
1740 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001741 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001744 * Convert 'backspace' number to string, for
1745 * adding, prepending and removing string.
1746 */
1747 else if (varp == (char_u *)&p_bs
1748 && VIM_ISDIGIT(**(char_u **)varp))
1749 {
1750 i = getdigits((char_u **)varp);
1751 switch (i)
1752 {
1753 case 0:
1754 *(char_u **)varp = empty_option;
1755 break;
1756 case 1:
1757 *(char_u **)varp = vim_strsave(
1758 (char_u *)"indent,eol");
1759 break;
1760 case 2:
1761 *(char_u **)varp = vim_strsave(
1762 (char_u *)"indent,eol,start");
1763 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001764 case 3:
1765 *(char_u **)varp = vim_strsave(
1766 (char_u *)"indent,eol,nostop");
1767 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001768 }
1769 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001770 if (origval == oldval)
1771 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001772 if (origval_l == oldval)
1773 origval_l = *(char_u **)varp;
1774 if (origval_g == oldval)
1775 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001776 oldval = *(char_u **)varp;
1777 }
1778 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 * Convert 'whichwrap' number to string, for
1780 * backwards compatibility with Vim 3.0.
1781 * Misuse errbuf[] for the resulting string.
1782 */
1783 else if (varp == (char_u *)&p_ww
1784 && VIM_ISDIGIT(*arg))
1785 {
1786 *errbuf = NUL;
1787 i = getdigits(&arg);
1788 if (i & 1)
1789 STRCAT(errbuf, "b,");
1790 if (i & 2)
1791 STRCAT(errbuf, "s,");
1792 if (i & 4)
1793 STRCAT(errbuf, "h,l,");
1794 if (i & 8)
1795 STRCAT(errbuf, "<,>,");
1796 if (i & 16)
1797 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001798 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 errbuf[STRLEN(errbuf) - 1] = NUL;
1800 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001801 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803 /*
1804 * Remove '>' before 'dir' and 'bdir', for
1805 * backwards compatibility with version 3.0
1806 */
1807 else if ( *arg == '>'
1808 && (varp == (char_u *)&p_dir
1809 || varp == (char_u *)&p_bdir))
1810 {
1811 ++arg;
1812 }
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 /*
1815 * Copy the new string into allocated memory.
1816 * Can't use set_string_option_direct(), because
1817 * we need to remove the backslashes.
1818 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001819 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 newlen = (unsigned)STRLEN(arg) + 1;
1821 if (adding || prepending || removing)
1822 newlen += (unsigned)STRLEN(origval) + 1;
1823 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001824 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 break;
1826 s = newval;
1827
1828 /*
1829 * Copy the string, skip over escaped chars.
1830 * For MS-DOS and WIN32 backslashes before normal
1831 * file name characters are not removed, and keep
1832 * backslash at start, for "\\machine\path", but
1833 * do remove it for "\\\\machine\\path".
1834 * The reverse is found in ExpandOldSetting().
1835 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001836 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
1838 if (*arg == '\\' && arg[1] != NUL
1839#ifdef BACKSLASH_IN_FILENAME
1840 && !((flags & P_EXPAND)
1841 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001842 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 && (arg[1] != '\\'
1844 || (s == newval
1845 && arg[2] != '\\')))
1846#endif
1847 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001848 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001850 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001852 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 mch_memmove(s, arg, (size_t)i);
1854 arg += i;
1855 s += i;
1856 }
1857 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 *s++ = *arg++;
1859 }
1860 *s = NUL;
1861
1862 /*
1863 * Expand environment variables and ~.
1864 * Don't do it when adding without inserting a
1865 * comma.
1866 */
1867 if (!(adding || prepending || removing)
1868 || (flags & P_COMMA))
1869 {
1870 s = option_expand(opt_idx, newval);
1871 if (s != NULL)
1872 {
1873 vim_free(newval);
1874 newlen = (unsigned)STRLEN(s) + 1;
1875 if (adding || prepending || removing)
1876 newlen += (unsigned)STRLEN(origval) + 1;
1877 newval = alloc(newlen);
1878 if (newval == NULL)
1879 break;
1880 STRCPY(newval, s);
1881 }
1882 }
1883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001884 // locate newval[] in origval[] when removing it
1885 // and when adding to avoid duplicates
1886 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (removing || (flags & P_NODUP))
1888 {
1889 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001890 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001892 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001893 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895 prepending = FALSE;
1896 adding = FALSE;
1897 STRCPY(newval, origval);
1898 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001899
1900 // if no duplicate, move pointer to end of
1901 // original value
1902 if (s == NULL)
1903 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001906 // concatenate the two strings; add a ',' if
1907 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (adding || prepending)
1909 {
1910 comma = ((flags & P_COMMA) && *origval != NUL
1911 && *newval != NUL);
1912 if (adding)
1913 {
1914 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001915 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001916 if (comma && i > 1
1917 && (flags & P_ONECOMMA) == P_ONECOMMA
1918 && origval[i - 1] == ','
1919 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001920 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 mch_memmove(newval + i + comma, newval,
1922 STRLEN(newval) + 1);
1923 mch_memmove(newval, origval, (size_t)i);
1924 }
1925 else
1926 {
1927 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001928 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930 if (comma)
1931 newval[i] = ',';
1932 }
1933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001934 // Remove newval[] from origval[]. (Note: "i" has
1935 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (removing)
1937 {
1938 STRCPY(newval, origval);
1939 if (*s)
1940 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001941 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (flags & P_COMMA)
1943 {
1944 if (s == origval)
1945 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001946 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (s[i] == ',')
1948 ++i;
1949 }
1950 else
1951 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001952 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 --s;
1954 ++i;
1955 }
1956 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001957 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 }
1960
1961 if (flags & P_FLAGLIST)
1962 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001963 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001964 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001965 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001966 // if options have P_FLAGLIST and
1967 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001968 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001970 if (*s != ',' && *(s + 1) == ','
1971 && vim_strchr(s + 2, *s) != NULL)
1972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001973 // Remove the duplicated value and
1974 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001975 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001976 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001979 else
1980 {
1981 if ((!(flags & P_COMMA) || *s != ',')
1982 && vim_strchr(s + 1, *s) != NULL)
1983 {
1984 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001985 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001986 }
1987 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001988 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001992 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 arg = save_arg;
1994 new_value_alloced = TRUE;
1995 }
1996
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001997 /*
1998 * Set the new value.
1999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 *(char_u **)(varp) = newval;
2001
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002002#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002003 if (!starting
2004# ifdef FEAT_CRYPT
2005 && options[opt_idx].indir != PV_KEY
2006# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002007 && origval != NULL && newval != NULL)
2008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002009 // origval may be freed by
2010 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002011 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002012 // newval (and varp) may become invalid if the
2013 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002014 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002015 if (origval_l != NULL)
2016 saved_origval_l = vim_strsave(origval_l);
2017 if (origval_g != NULL)
2018 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002019 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002020#endif
2021
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002022 {
2023 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002024 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002025
2026 // When an option is set in the sandbox, from a
2027 // modeline or in secure mode, then deal with side
2028 // effects in secure mode. Also when the value was
2029 // set with the P_INSECURE flag and is not
2030 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002031 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002032#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002033 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002034#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002035 || (!value_is_replaced && (*p & P_INSECURE)))
2036 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002037
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002038 // Handle side effects, and set the global value
2039 // for ":set" on local options. Note: when setting
2040 // 'syntax' or 'filetype' autocommands may be
2041 // triggered that can cause havoc.
2042 errmsg = did_set_string_option(
2043 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002044 new_value_alloced, oldval, errbuf,
2045 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002046
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002047 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002050#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002051 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002052 trigger_optionsset_string(
2053 opt_idx, opt_flags, saved_origval,
2054 saved_origval_l, saved_origval_g,
2055 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002056 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002057 vim_free(saved_origval_l);
2058 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002059 vim_free(saved_newval);
2060#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002061 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (errmsg != NULL)
2063 goto skip;
2064 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002065 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
2067 char_u *p;
2068
2069 if (nextchar == '&')
2070 {
2071 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 else
2075 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002076 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002077 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (*p == '\\' && p[1] != NUL)
2079 ++p;
2080 nextchar = *p;
2081 *p = NUL;
2082 add_termcode(key_name, arg, FALSE);
2083 *p = nextchar;
2084 }
2085 if (full_screen)
2086 ttest(FALSE);
2087 redraw_all_later(CLEAR);
2088 }
2089 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002092 did_set_option(
2093 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095
2096skip:
2097 /*
2098 * Advance to next argument.
2099 * - skip until a blank found, taking care of backslashes
2100 * - skip blanks
2101 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2102 */
2103 for (i = 0; i < 2 ; ++i)
2104 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002105 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (*arg++ == '\\' && *arg != NUL)
2107 ++arg;
2108 arg = skipwhite(arg);
2109 if (*arg != '=')
2110 break;
2111 }
2112 }
2113
2114 if (errmsg != NULL)
2115 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002116 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002117 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (i + (arg - startarg) < IOSIZE)
2119 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002120 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 STRCAT(IObuff, ": ");
2122 mch_memmove(IObuff + i, startarg, (arg - startarg));
2123 IObuff[i + (arg - startarg)] = NUL;
2124 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002125 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 trans_characters(IObuff, IOSIZE);
2127
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002128 ++no_wait_return; // wait_return done later
2129 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 --no_wait_return;
2131
2132 return FAIL;
2133 }
2134
2135 arg = skipwhite(arg);
2136 }
2137
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002138theend:
2139 if (silent_mode && did_show)
2140 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002142 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002143 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002144 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002146 out_flush();
2147 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002148 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002149 }
2150
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 return OK;
2152}
2153
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002154/*
2155 * Call this when an option has been given a new value through a user command.
2156 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2157 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002158 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002159did_set_option(
2160 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002161 int opt_flags, // possibly with OPT_MODELINE
2162 int new_value, // value was replaced completely
2163 int value_checked) // value was checked to be safe, no need to set the
2164 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002165{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002166 long_u *p;
2167
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002168 options[opt_idx].flags |= P_WAS_SET;
2169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002170 // When an option is set in the sandbox, from a modeline or in secure mode
2171 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2172 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002173 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002174 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002175#ifdef HAVE_SANDBOX
2176 || sandbox != 0
2177#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002178 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002179 *p = *p | P_INSECURE;
2180 else if (new_value)
2181 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002182}
2183
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184/*
2185 * Convert a key name or string into a key value.
2186 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002187 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002189 int
2190string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
2192 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002193 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (*arg == '^')
2195 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002196 if (multi_byte)
2197 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 return *arg;
2199}
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef FEAT_TITLE
2202/*
2203 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2204 * maketitle() to create and display it.
2205 * When switching the title or icon off, call mch_restore_title() to get
2206 * the old value back.
2207 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002208 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002209did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211 if (starting != NO_SCREEN
2212#ifdef FEAT_GUI
2213 && !gui.starting
2214#endif
2215 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217}
2218#endif
2219
2220/*
2221 * set_options_bin - called when 'bin' changes value.
2222 */
2223 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002224set_options_bin(
2225 int oldval,
2226 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002227 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 /*
2230 * The option values that are changed when 'bin' changes are
2231 * copied when 'bin is set and restored when 'bin' is reset.
2232 */
2233 if (newval)
2234 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002235 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 if (!(opt_flags & OPT_GLOBAL))
2238 {
2239 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2240 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2241 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2242 curbuf->b_p_et_nobin = curbuf->b_p_et;
2243 }
2244 if (!(opt_flags & OPT_LOCAL))
2245 {
2246 p_tw_nobin = p_tw;
2247 p_wm_nobin = p_wm;
2248 p_ml_nobin = p_ml;
2249 p_et_nobin = p_et;
2250 }
2251 }
2252
2253 if (!(opt_flags & OPT_GLOBAL))
2254 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 curbuf->b_p_tw = 0; // no automatic line wrap
2256 curbuf->b_p_wm = 0; // no automatic line wrap
2257 curbuf->b_p_ml = 0; // no modelines
2258 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 }
2260 if (!(opt_flags & OPT_LOCAL))
2261 {
2262 p_tw = 0;
2263 p_wm = 0;
2264 p_ml = FALSE;
2265 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002266 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 if (!(opt_flags & OPT_GLOBAL))
2272 {
2273 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2274 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2275 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2276 curbuf->b_p_et = curbuf->b_p_et_nobin;
2277 }
2278 if (!(opt_flags & OPT_LOCAL))
2279 {
2280 p_tw = p_tw_nobin;
2281 p_wm = p_wm_nobin;
2282 p_ml = p_ml_nobin;
2283 p_et = p_et_nobin;
2284 }
2285 }
2286}
2287
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288/*
2289 * Expand environment variables for some string options.
2290 * These string options cannot be indirect!
2291 * If "val" is NULL expand the current value of the option.
2292 * Return pointer to NameBuff, or NULL when not expanded.
2293 */
2294 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002295option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002297 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2299 return NULL;
2300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002301 // If val is longer than MAXPATHL no meaningful expansion can be done,
2302 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (val != NULL && STRLEN(val) > MAXPATHL)
2304 return NULL;
2305
2306 if (val == NULL)
2307 val = *(char_u **)options[opt_idx].var;
2308
2309 /*
2310 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2311 * Escape spaces when expanding 'tags', they are used to separate file
2312 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002313 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
2315 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002316 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002317#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002318 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2319#endif
2320 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002321 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 return NULL;
2323
2324 return NameBuff;
2325}
2326
2327/*
2328 * After setting various option values: recompute variables that depend on
2329 * option values.
2330 */
2331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002332didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002334 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 (void)init_chartab();
2336
Bram Moolenaardac13472019-09-16 21:06:21 +02002337 didset_string_options();
2338
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002339#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002340 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002341 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002342 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002343 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002346 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 (void)check_cedit();
2348#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002349#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002350 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002351 fill_breakat_flags();
2352#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002353 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002354}
2355
2356/*
2357 * More side effects of setting options.
2358 */
2359 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002360didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002361{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002362 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002363 (void)highlight_changed();
2364
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002365 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002366 check_opt_wim();
2367
Bram Moolenaareed9d462021-02-15 20:38:25 +01002368 // Parse default for 'listchars'.
2369 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2370
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002372 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002373
2374#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002376 (void)check_clipboard_option();
2377#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002378#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002379 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002380 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002381 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002382 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384}
2385
2386/*
2387 * Check for string options that are NULL (normally only termcap options).
2388 */
2389 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002390check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392 int opt_idx;
2393
2394 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2395 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2396 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2397}
2398
2399/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002400 * Return the option index found by a pointer into term_strings[].
2401 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002403 int
2404get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002406 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2409 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002410 return opt_idx;
2411 return -1; // cannot happen: didn't find it!
2412}
2413
2414/*
2415 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2416 * Return the option index or -1 if not found.
2417 */
2418 int
2419set_term_option_alloced(char_u **p)
2420{
2421 int opt_idx = get_term_opt_idx(p);
2422
2423 if (opt_idx >= 0)
2424 options[opt_idx].flags |= P_ALLOCED;
2425 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426}
2427
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002428#if defined(FEAT_EVAL) || defined(PROTO)
2429/*
2430 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2431 * Return FALSE when it wasn't.
2432 * Return -1 for an unknown option.
2433 */
2434 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002435was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002436{
2437 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002438 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002439
2440 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002441 {
2442 flagp = insecure_flag(idx, opt_flags);
2443 return (*flagp & P_INSECURE) != 0;
2444 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002445 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002446 return -1;
2447}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002448
2449/*
2450 * Get a pointer to the flags used for the P_INSECURE flag of option
2451 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002452 * NOTE: Caller must make sure that "curwin" is set to the window from which
2453 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002454 */
2455 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002456insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002457{
2458 if (opt_flags & OPT_LOCAL)
2459 switch ((int)options[opt_idx].indir)
2460 {
2461#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002462 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002463#endif
2464#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002465# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002466 case PV_FDE: return &curwin->w_p_fde_flags;
2467 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002468# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002469# ifdef FEAT_BEVAL
2470 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2471# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002472# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002473 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002474# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002475 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002476# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002477 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002478# endif
2479#endif
2480 }
2481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002482 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002483 return &options[opt_idx].flags;
2484}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002485#endif
2486
Bram Moolenaardac13472019-09-16 21:06:21 +02002487#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002488/*
2489 * Redraw the window title and/or tab page text later.
2490 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002491void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002492{
2493 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002494 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002495}
2496#endif
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002499 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2500 * characters or characters in "allowed".
2501 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002502 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002503valid_name(char_u *val, char *allowed)
2504{
2505 char_u *s;
2506
2507 for (s = val; *s != NUL; ++s)
2508 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2509 return FALSE;
2510 return TRUE;
2511}
2512
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002513#if defined(FEAT_EVAL) || defined(PROTO)
2514/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002515 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002516 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002517 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002518 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002519set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002520{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002521 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2522 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002523 sctx_T new_script_ctx = script_ctx;
2524
Bram Moolenaar51258742020-05-03 17:19:33 +02002525 // Modeline already has the line number set.
2526 if (!(opt_flags & OPT_MODELINE))
2527 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002528
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002529 // Remember where the option was set. For local options need to do that
2530 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002531 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002532 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 if (both || (opt_flags & OPT_LOCAL))
2534 {
2535 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002536 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002537 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002538 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002539 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002541
2542/*
2543 * Set the script_ctx for a termcap option.
2544 * "name" must be the two character code, e.g. "RV".
2545 * When "name" is NULL use "opt_idx".
2546 */
2547 void
2548set_term_option_sctx_idx(char *name, int opt_idx)
2549{
2550 char_u buf[5];
2551 int idx;
2552
2553 if (name == NULL)
2554 idx = opt_idx;
2555 else
2556 {
2557 buf[0] = 't';
2558 buf[1] = '_';
2559 buf[2] = name[0];
2560 buf[3] = name[1];
2561 buf[4] = 0;
2562 idx = findoption(buf);
2563 }
2564 if (idx >= 0)
2565 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2566}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567#endif
2568
Bram Moolenaarf4140482020-02-15 23:06:45 +01002569#if defined(FEAT_EVAL)
2570/*
2571 * Apply the OptionSet autocommand.
2572 */
2573 static void
2574apply_optionset_autocmd(
2575 int opt_idx,
2576 long opt_flags,
2577 long oldval,
2578 long oldval_g,
2579 long newval,
2580 char *errmsg)
2581{
2582 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2583
2584 // Don't do this while starting up, failure or recursively.
2585 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2586 return;
2587
2588 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2589 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2590 oldval_g);
2591 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2592 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2593 (opt_flags & OPT_LOCAL) ? "local" : "global");
2594 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2595 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2596 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2597 if (opt_flags & OPT_LOCAL)
2598 {
2599 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2600 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2601 }
2602 if (opt_flags & OPT_GLOBAL)
2603 {
2604 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2605 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2606 }
2607 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2608 {
2609 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2610 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2611 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2612 }
2613 if (opt_flags & OPT_MODELINE)
2614 {
2615 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2616 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2617 }
2618 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2619 NULL, FALSE, NULL);
2620 reset_v_option_vars();
2621}
2622#endif
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624/*
2625 * Set the value of a boolean option, and take care of side effects.
2626 * Returns NULL for success, or an error message for an error.
2627 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002628 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002629set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002630 int opt_idx, // index in options[] table
2631 char_u *varp, // pointer to the option variable
2632 int value, // new value
2633 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002636#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002637 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002640 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if ((secure
2642#ifdef HAVE_SANDBOX
2643 || sandbox != 0
2644#endif
2645 ) && (options[opt_idx].flags & P_SECURE))
2646 return e_secure;
2647
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002648#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002649 // Save the global value before changing anything. This is needed as for
2650 // a global-only option setting the "local value" in fact sets the global
2651 // value (since there is only one value).
2652 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2653 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2654 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002655#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002656
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002657 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002660 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#endif
2662
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002663#ifdef FEAT_GUI
2664 need_mouse_correct = TRUE;
2665#endif
2666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002667 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2669 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2670
2671 /*
2672 * Handle side effects of changing a bool option.
2673 */
2674
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002675 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaar920694c2016-08-21 17:45:02 +02002679#ifdef FEAT_LANGMAP
2680 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002681 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002682 p_lnr = !p_lrm;
2683 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002684 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002685 p_lrm = !p_lnr;
2686#endif
2687
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002688#ifdef FEAT_SYN_HL
2689 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2690 reset_cursorline();
2691#endif
2692
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002693#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002694 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002695 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002697 // Only take action when the option was set. When reset we do not
2698 // delete the undo file, the option may be set again without making
2699 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002700 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002701 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002702 char_u hash[UNDO_HASH_SIZE];
2703 buf_T *save_curbuf = curbuf;
2704
Bram Moolenaar29323592016-07-24 22:04:11 +02002705 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002707 // When 'undofile' is set globally: for every buffer, otherwise
2708 // only for the current buffer: Try to read in the undofile,
2709 // if one exists, the buffer wasn't changed and the buffer was
2710 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002711 if ((curbuf == save_curbuf
2712 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2713 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2714 {
2715 u_compute_hash(hash);
2716 u_read_undo(NULL, hash, curbuf->b_fname);
2717 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002718 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002719 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002720 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002721 }
2722#endif
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 else if ((int *)varp == &curbuf->b_p_ro)
2725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2728 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002731 if (curbuf->b_p_ro)
2732 curbuf->b_did_warn = FALSE;
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002735 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#endif
2737 }
2738
Bram Moolenaar9c449722010-07-20 18:44:27 +02002739#ifdef FEAT_GUI
2740 else if ((int *)varp == &p_mh)
2741 {
2742 if (!p_mh)
2743 gui_mch_mousehide(FALSE);
2744 }
2745#endif
2746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002747 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002749 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002750# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002752 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2753 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002754 {
2755 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002756 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002757 }
2758# endif
2759# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002760 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002761# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002762 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002763#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002766 {
2767 redraw_titles();
2768 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002770 else if ((int *)varp == &curbuf->b_p_fixeol)
2771 {
2772 redraw_titles();
2773 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002775 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002776 {
2777 redraw_titles();
2778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779#endif
2780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 else if ((int *)varp == &curbuf->b_p_bin)
2783 {
2784 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2785#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002786 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#endif
2788 }
2789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2792 {
2793 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2794 NULL, NULL, TRUE, curbuf);
2795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else if ((int *)varp == &curbuf->b_p_swf)
2799 {
2800 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2804 // buf->b_p_swf
2805 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002808 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 else if ((int *)varp == &p_terse)
2810 {
2811 char_u *p;
2812
2813 p = vim_strchr(p_shm, SHM_SEARCH);
2814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (p_terse && p == NULL)
2817 {
2818 STRCPY(IObuff, p_shm);
2819 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002820 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002822 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002824 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 else if ((int *)varp == &p_paste)
2829 {
2830 paste_option_changed();
2831 }
2832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else if ((int *)varp == &p_im)
2835 {
2836 if (p_im)
2837 {
2838 if ((State & INSERT) == 0)
2839 need_start_insertmode = TRUE;
2840 stop_insert_mode = FALSE;
2841 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002843 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
2845 need_start_insertmode = FALSE;
2846 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002847 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 restart_edit = 0;
2850 }
2851 }
2852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 else if ((int *)varp == &p_ic && p_hls)
2855 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002856 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858
2859#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 else if ((int *)varp == &p_hls)
2862 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002863 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865#endif
2866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2868 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 else if ((int *)varp == &curwin->w_p_scb)
2870 {
2871 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002874 curwin->w_scbind_pos = curwin->w_topline;
2875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar4033c552017-09-16 20:54:51 +02002878#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 else if ((int *)varp == &curwin->w_p_pvw)
2881 {
2882 if (curwin->w_p_pvw)
2883 {
2884 win_T *win;
2885
Bram Moolenaar29323592016-07-24 22:04:11 +02002886 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if (win->w_p_pvw && win != curwin)
2888 {
2889 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002890 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892 }
2893 }
2894#endif
2895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 else if ((int *)varp == &curbuf->b_p_tx)
2898 {
2899 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2900 }
2901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 set_string_option_direct((char_u *)"ffs", -1,
2906 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002907 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 /*
2911 * When 'lisp' option changes include/exclude '-' in
2912 * keyword characters.
2913 */
2914#ifdef FEAT_LISP
2915 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2916 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919#endif
2920
2921#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002923 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002925 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927#endif
2928
2929 else if ((int *)varp == &curbuf->b_changed)
2930 {
2931 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002934 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938
2939#ifdef BACKSLASH_IN_FILENAME
2940 else if ((int *)varp == &p_ssl)
2941 {
2942 if (p_ssl)
2943 {
2944 psepc = '/';
2945 psepcN = '\\';
2946 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 else
2949 {
2950 psepc = '\\';
2951 psepcN = '/';
2952 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 buflist_slash_adjust();
2957 alist_slash_adjust();
2958# ifdef FEAT_EVAL
2959 scriptnames_slash_adjust();
2960# endif
2961 }
2962#endif
2963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else if ((int *)varp == &curwin->w_p_wrap)
2966 {
2967 if (curwin->w_p_wrap)
2968 curwin->w_leftcol = 0;
2969 }
2970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 else if ((int *)varp == &p_ea)
2972 {
2973 if (p_ea && !old_value)
2974 win_equal(curwin, FALSE, 0);
2975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
2977 else if ((int *)varp == &p_wiv)
2978 {
2979 /*
2980 * When 'weirdinvert' changed, set/reset 't_xs'.
2981 * Then set 'weirdinvert' according to value of 't_xs'.
2982 */
2983 if (p_wiv && !old_value)
2984 T_XS = (char_u *)"y";
2985 else if (!p_wiv && old_value)
2986 T_XS = empty_option;
2987 p_wiv = (*T_XS != NUL);
2988 }
2989
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002990#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else if ((int *)varp == &p_beval)
2992 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002993 if (!balloonEvalForTerm)
2994 {
2995 if (p_beval && !old_value)
2996 gui_mch_enable_beval_area(balloonEval);
2997 else if (!p_beval && old_value)
2998 gui_mch_disable_beval_area(balloonEval);
2999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003001#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003002#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003003 else if ((int *)varp == &p_bevalterm)
3004 {
3005 mch_bevalterm_changed();
3006 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003009#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 else if ((int *)varp == &p_acd)
3011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003013 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015#endif
3016
3017#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 else if ((int *)varp == &curwin->w_p_diff)
3020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003022 diff_buf_adjust(curwin);
3023# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (foldmethodIsDiff(curwin))
3025 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003026# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 }
3028#endif
3029
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003030#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003031 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else if ((int *)varp == &p_imdisable)
3033 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 if (p_imdisable)
3036 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003037 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003038 // When the option is set from an autocommand, it may need to take
3039 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003040 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042#endif
3043
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003044#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003046 else if ((int *)varp == &curwin->w_p_spell)
3047 {
3048 if (curwin->w_p_spell)
3049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 char *errmsg = did_set_spelllang(curwin);
3051
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003052 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003053 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003054 }
3055 }
3056#endif
3057
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#ifdef FEAT_ARABIC
3059 if ((int *)varp == &curwin->w_p_arab)
3060 {
3061 if (curwin->w_p_arab)
3062 {
3063 /*
3064 * 'arabic' is set, handle various sub-settings.
3065 */
3066 if (!p_tbidi)
3067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 if (!curwin->w_p_rl)
3070 {
3071 curwin->w_p_rl = TRUE;
3072 changed_window_setting();
3073 }
3074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003075 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (!p_arshape)
3077 {
3078 p_arshape = TRUE;
3079 redraw_later_clear();
3080 }
3081 }
3082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // Arabic requires a utf-8 encoding, inform the user if its not
3084 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003086 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003087 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3088
Bram Moolenaar8820b482017-03-16 17:23:31 +01003089 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003090 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003091#ifdef FEAT_EVAL
3092 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3093#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003100 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3102 OPT_LOCAL);
3103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105 else
3106 {
3107 /*
3108 * 'arabic' is reset, handle various sub-settings.
3109 */
3110 if (!p_tbidi)
3111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003112 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (curwin->w_p_rl)
3114 {
3115 curwin->w_p_rl = FALSE;
3116 changed_window_setting();
3117 }
3118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003119 // 'arabicshape' isn't reset, it is a global option and
3120 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // 'delcombine' isn't reset, it is a global option and another
3124 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 curbuf->b_p_iminsert = B_IMODE_NONE;
3129 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3130# endif
3131 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003132 }
3133
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003134#endif
3135
Bram Moolenaar44826212019-08-22 21:23:20 +02003136#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3137 else if (((int *)varp == &curwin->w_p_nu
3138 || (int *)varp == &curwin->w_p_rnu)
3139 && gui.in_use
3140 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3141 && curbuf->b_signlist != NULL)
3142 {
3143 // If the 'number' or 'relativenumber' options are modified and
3144 // 'signcolumn' is set to 'number', then clear the screen for a full
3145 // refresh. Otherwise the sign icons are not displayed properly in the
3146 // number column. If the 'number' option is set and only the
3147 // 'relativenumber' option is toggled, then don't refresh the screen
3148 // (optimization).
3149 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3150 redraw_all_later(CLEAR);
3151 }
3152#endif
3153
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003154#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003156 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003157 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003158# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003160 if (
3161# ifdef VIMDLL
3162 !gui.in_use && !gui.starting &&
3163# endif
3164 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003165 {
3166 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003167 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003168 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003169 if (is_term_win32())
3170 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003171# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003172# ifdef FEAT_GUI
3173 if (!gui.in_use && !gui.starting)
3174# endif
3175 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003176# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003178 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003179 {
3180 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003181 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003182 init_highlight(TRUE, FALSE);
3183 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003184# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003185 }
3186#endif
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 /*
3189 * End of handling side effects for bool options.
3190 */
3191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 options[opt_idx].flags |= P_WAS_SET;
3195
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003196#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003197 apply_optionset_autocmd(opt_idx, opt_flags,
3198 (long)(old_value ? TRUE : FALSE),
3199 (long)(old_global_value ? TRUE : FALSE),
3200 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003201#endif
3202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003204 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003205 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003206 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003207
3208 if ((opt_flags & OPT_NO_REDRAW) == 0)
3209 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 return NULL;
3212}
3213
3214/*
3215 * Set the value of a number option, and take care of side effects.
3216 * Returns NULL for success, or an error message for an error.
3217 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003218 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003219set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 int opt_idx, // index in options[] table
3221 char_u *varp, // pointer to the option variable
3222 long value, // new value
3223 char *errbuf, // buffer for error messages
3224 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003225 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3226 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003228 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003230#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003231 long old_global_value = 0; // only used when setting a local and
3232 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003233#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003234 long old_Rows = Rows; // remember old Rows
3235 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 long *pp = (long *)varp;
3237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003239 if ((secure
3240#ifdef HAVE_SANDBOX
3241 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003243 ) && (options[opt_idx].flags & P_SECURE))
3244 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003246#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003247 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003248 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003249 // value (since there is only one value).
3250 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003251 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3252 OPT_GLOBAL);
3253#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 *pp = value;
3256#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003258 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003260#ifdef FEAT_GUI
3261 need_mouse_correct = TRUE;
3262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
Bram Moolenaar14f24742012-08-08 18:01:05 +02003264 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003267#ifdef FEAT_VARTABS
3268 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3269 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3270 ? tabstop_first(curbuf->b_p_vts_array)
3271 : curbuf->b_p_ts;
3272#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276
3277 /*
3278 * Number options that need some action when changed
3279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (pp == &p_wh || pp == &p_hh)
3281 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003282 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (p_wh < 1)
3284 {
3285 errmsg = e_positive;
3286 p_wh = 1;
3287 }
3288 if (p_wmh > p_wh)
3289 {
3290 errmsg = e_winheight;
3291 p_wh = p_wmh;
3292 }
3293 if (p_hh < 0)
3294 {
3295 errmsg = e_positive;
3296 p_hh = 0;
3297 }
3298
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003299 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003300 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
3302 if (pp == &p_wh && curwin->w_height < p_wh)
3303 win_setheight((int)p_wh);
3304 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3305 win_setheight((int)p_hh);
3306 }
3307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else if (pp == &p_wmh)
3309 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003310 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (p_wmh < 0)
3312 {
3313 errmsg = e_positive;
3314 p_wmh = 0;
3315 }
3316 if (p_wmh > p_wh)
3317 {
3318 errmsg = e_winheight;
3319 p_wmh = p_wh;
3320 }
3321 win_setminheight();
3322 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003323 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003325 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (p_wiw < 1)
3327 {
3328 errmsg = e_positive;
3329 p_wiw = 1;
3330 }
3331 if (p_wmw > p_wiw)
3332 {
3333 errmsg = e_winwidth;
3334 p_wiw = p_wmw;
3335 }
3336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003337 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003338 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 win_setwidth((int)p_wiw);
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 else if (pp == &p_wmw)
3342 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003343 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (p_wmw < 0)
3345 {
3346 errmsg = e_positive;
3347 p_wmw = 0;
3348 }
3349 if (p_wmw > p_wiw)
3350 {
3351 errmsg = e_winwidth;
3352 p_wmw = p_wiw;
3353 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003354 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003357 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else if (pp == &p_ls)
3359 {
3360 last_status(FALSE);
3361 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003363 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003364 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003365 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003366 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369#ifdef FEAT_GUI
3370 else if (pp == &p_linespace)
3371 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003372 // Recompute gui.char_height and resize the Vim window to keep the
3373 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003374 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003375 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377#endif
3378
3379#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003380 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 else if (pp == &curwin->w_p_fdl)
3382 {
3383 if (curwin->w_p_fdl < 0)
3384 curwin->w_p_fdl = 0;
3385 newFoldLevel();
3386 }
3387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003388 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 else if (pp == &curwin->w_p_fml)
3390 {
3391 foldUpdateAll(curwin);
3392 }
3393
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003394 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 else if (pp == &curwin->w_p_fdn)
3396 {
3397 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3398 foldUpdateAll(curwin);
3399 }
3400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003401 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 else if (pp == &curwin->w_p_fdc)
3403 {
3404 if (curwin->w_p_fdc < 0)
3405 {
3406 errmsg = e_positive;
3407 curwin->w_p_fdc = 0;
3408 }
3409 else if (curwin->w_p_fdc > 12)
3410 {
3411 errmsg = e_invarg;
3412 curwin->w_p_fdc = 12;
3413 }
3414 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003415#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003417#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003418 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3420 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003421# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (foldmethodIsIndent(curwin))
3423 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003424# endif
3425# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003426 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3427 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003428 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3429 parse_cino(curbuf);
3430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003434 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003435 else if (pp == &p_mco)
3436 {
3437 if (p_mco > MAX_MCO)
3438 p_mco = MAX_MCO;
3439 else if (p_mco < 0)
3440 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003441 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003442 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 else if (pp == &curbuf->b_p_iminsert)
3445 {
3446 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3447 {
3448 errmsg = e_invarg;
3449 curbuf->b_p_iminsert = B_IMODE_NONE;
3450 }
3451 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003454#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003455 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 status_redraw_curbuf();
3457#endif
3458 }
3459
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003460#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003461 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003462 else if (pp == &p_imst)
3463 {
3464 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3465 errmsg = e_invarg;
3466 }
3467#endif
3468
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003469 else if (pp == &p_window)
3470 {
3471 if (p_window < 1)
3472 p_window = 1;
3473 else if (p_window >= Rows)
3474 p_window = Rows - 1;
3475 }
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else if (pp == &curbuf->b_p_imsearch)
3478 {
3479 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3480 {
3481 errmsg = e_invarg;
3482 curbuf->b_p_imsearch = B_IMODE_NONE;
3483 }
3484 p_imsearch = curbuf->b_p_imsearch;
3485 }
3486
3487#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003488 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 else if (pp == &p_titlelen)
3490 {
3491 if (p_titlelen < 0)
3492 {
3493 errmsg = e_positive;
3494 p_titlelen = 85;
3495 }
3496 if (starting != NO_SCREEN && old_value != p_titlelen)
3497 need_maketitle = TRUE;
3498 }
3499#endif
3500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003501 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 else if (pp == &p_ch)
3503 {
3504 if (p_ch < 1)
3505 {
3506 errmsg = e_positive;
3507 p_ch = 1;
3508 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003509 if (p_ch > Rows - min_rows() + 1)
3510 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003512 // Only compute the new window layout when startup has been
3513 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (p_ch != old_value && full_screen
3515#ifdef FEAT_GUI
3516 && !gui.starting
3517#endif
3518 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003519 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003522 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 else if (pp == &p_uc)
3524 {
3525 if (p_uc < 0)
3526 {
3527 errmsg = e_positive;
3528 p_uc = 100;
3529 }
3530 if (p_uc && !old_value)
3531 ml_open_files();
3532 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003533#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003534 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003535 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003536 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003537 {
3538 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003539 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003540 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003541 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003542 {
3543 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003544 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003545 }
3546 }
3547#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003548#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003549 else if (pp == &p_mzq)
3550 mzvim_reset_timer();
3551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003553#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003554 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003555 else if (pp == &p_pyx)
3556 {
3557 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3558 errmsg = e_invarg;
3559 }
3560#endif
3561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003562 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 else if (pp == &p_ul)
3564 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003567 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 p_ul = value;
3569 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003570 else if (pp == &curbuf->b_p_ul)
3571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003572 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003573 curbuf->b_p_ul = old_value;
3574 u_sync(TRUE);
3575 curbuf->b_p_ul = value;
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003578#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003579 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003580 else if (pp == &curwin->w_p_nuw)
3581 {
3582 if (curwin->w_p_nuw < 1)
3583 {
3584 errmsg = e_positive;
3585 curwin->w_p_nuw = 1;
3586 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003587 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003588 {
3589 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003590 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003591 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003592 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003593 }
3594#endif
3595
Bram Moolenaar1a384422010-07-14 19:53:30 +02003596 else if (pp == &curbuf->b_p_tw)
3597 {
3598 if (curbuf->b_p_tw < 0)
3599 {
3600 errmsg = e_positive;
3601 curbuf->b_p_tw = 0;
3602 }
3603#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003604 {
3605 win_T *wp;
3606 tabpage_T *tp;
3607
3608 FOR_ALL_TAB_WINDOWS(tp, wp)
3609 check_colorcolumn(wp);
3610 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003611#endif
3612 }
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /*
3615 * Check the bounds for numeric options here
3616 */
3617 if (Rows < min_rows() && full_screen)
3618 {
3619 if (errbuf != NULL)
3620 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003621 vim_snprintf((char *)errbuf, errbuflen,
3622 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 errmsg = errbuf;
3624 }
3625 Rows = min_rows();
3626 }
3627 if (Columns < MIN_COLUMNS && full_screen)
3628 {
3629 if (errbuf != NULL)
3630 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003631 vim_snprintf((char *)errbuf, errbuflen,
3632 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 errmsg = errbuf;
3634 }
3635 Columns = MIN_COLUMNS;
3636 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003637 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 /*
3640 * If the screen (shell) height has been changed, assume it is the
3641 * physical screenheight.
3642 */
3643 if (old_Rows != Rows || old_Columns != Columns)
3644 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003645 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (updating_screen)
3647 *pp = old_value;
3648 else if (full_screen
3649#ifdef FEAT_GUI
3650 && !gui.starting
3651#endif
3652 )
3653 set_shellsize((int)Columns, (int)Rows, TRUE);
3654 else
3655 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 // Postpone the resizing; check the size and cmdline position for
3657 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 check_shellsize();
3659 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3660 cmdline_row = Rows - p_ch;
3661 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003662 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003663 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (curbuf->b_p_ts <= 0)
3667 {
3668 errmsg = e_positive;
3669 curbuf->b_p_ts = 8;
3670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (p_tm < 0)
3672 {
3673 errmsg = e_positive;
3674 p_tm = 0;
3675 }
3676 if ((curwin->w_p_scr <= 0
3677 || (curwin->w_p_scr > curwin->w_height
3678 && curwin->w_height > 0))
3679 && full_screen)
3680 {
3681 if (pp == &(curwin->w_p_scr))
3682 {
3683 if (curwin->w_p_scr != 0)
3684 errmsg = e_scroll;
3685 win_comp_scroll(curwin);
3686 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003687 // If 'scroll' became invalid because of a side effect silently adjust
3688 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 else if (curwin->w_p_scr <= 0)
3690 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003691 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 curwin->w_p_scr = curwin->w_height;
3693 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003694 if (p_hi < 0)
3695 {
3696 errmsg = e_positive;
3697 p_hi = 0;
3698 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003699 else if (p_hi > 10000)
3700 {
3701 errmsg = e_invarg;
3702 p_hi = 10000;
3703 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003704 if (p_re < 0 || p_re > 2)
3705 {
3706 errmsg = e_invarg;
3707 p_re = 0;
3708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 if (p_report < 0)
3710 {
3711 errmsg = e_positive;
3712 p_report = 1;
3713 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003714 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003716 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 p_sj = Rows / 2;
3718 else
3719 {
3720 errmsg = e_scroll;
3721 p_sj = 1;
3722 }
3723 }
3724 if (p_so < 0 && full_screen)
3725 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003726 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 p_so = 0;
3728 }
3729 if (p_siso < 0 && full_screen)
3730 {
3731 errmsg = e_positive;
3732 p_siso = 0;
3733 }
3734#ifdef FEAT_CMDWIN
3735 if (p_cwh < 1)
3736 {
3737 errmsg = e_positive;
3738 p_cwh = 1;
3739 }
3740#endif
3741 if (p_ut < 0)
3742 {
3743 errmsg = e_positive;
3744 p_ut = 2000;
3745 }
3746 if (p_ss < 0)
3747 {
3748 errmsg = e_positive;
3749 p_ss = 0;
3750 }
3751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003752 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3754 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3755
3756 options[opt_idx].flags |= P_WAS_SET;
3757
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003758#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003759 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3760 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003761#endif
3762
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003763 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003764 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003765 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003766 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003767 if ((opt_flags & OPT_NO_REDRAW) == 0)
3768 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 return errmsg;
3771}
3772
3773/*
3774 * Called after an option changed: check if something needs to be redrawn.
3775 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003777check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003779 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003780 int doclear = (flags & P_RCLR) == P_RCLR;
3781 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003783 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
3786 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3787 changed_window_setting();
3788 if (flags & P_RBUF)
3789 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003790 if (flags & P_RWINONLY)
3791 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003792 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 redraw_all_later(CLEAR);
3794 else if (all)
3795 redraw_all_later(NOT_VALID);
3796}
3797
3798/*
3799 * Find index for option 'arg'.
3800 * Return -1 if not found.
3801 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003802 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003803findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 int opt_idx;
3806 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003807 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 int is_term_opt;
3809
3810 /*
3811 * For first call: Initialize the quick-access table.
3812 * It contains the index for the first option that starts with a certain
3813 * letter. There are 26 letters, plus the first "t_" option.
3814 */
3815 if (quick_tab[1] == 0)
3816 {
3817 p = options[0].fullname;
3818 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3819 {
3820 if (s[0] != p[0])
3821 {
3822 if (s[0] == 't' && s[1] == '_')
3823 quick_tab[26] = opt_idx;
3824 else
3825 quick_tab[CharOrdLow(s[0])] = opt_idx;
3826 }
3827 p = s;
3828 }
3829 }
3830
3831 /*
3832 * Check for name starting with an illegal character.
3833 */
3834#ifdef EBCDIC
3835 if (!islower(arg[0]))
3836#else
3837 if (arg[0] < 'a' || arg[0] > 'z')
3838#endif
3839 return -1;
3840
3841 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3842 if (is_term_opt)
3843 opt_idx = quick_tab[26];
3844 else
3845 opt_idx = quick_tab[CharOrdLow(arg[0])];
3846 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3847 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 break;
3850 }
3851 if (s == NULL && !is_term_opt)
3852 {
3853 opt_idx = quick_tab[CharOrdLow(arg[0])];
3854 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3855 {
3856 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003857 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 break;
3859 s = NULL;
3860 }
3861 }
3862 if (s == NULL)
3863 opt_idx = -1;
3864 return opt_idx;
3865}
3866
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003867#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868/*
3869 * Get the value for an option.
3870 *
3871 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003872 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003873 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003874 * String option: gov_string, *stringval gets allocated string.
3875 * Hidden Number option: gov_hidden_number.
3876 * Hidden Toggle option: gov_hidden_bool.
3877 * Hidden String option: gov_hidden_string.
3878 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003880 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003881get_option_value(
3882 char_u *name,
3883 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003884 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003885 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
3887 int opt_idx;
3888 char_u *varp;
3889
3890 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003891 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003892 {
3893 int key;
3894
3895 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003896 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003897 {
3898 char_u key_name[2];
3899 char_u *p;
3900
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003901 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003902 if (key < 0)
3903 {
3904 key_name[0] = KEY2TERMCAP0(key);
3905 key_name[1] = KEY2TERMCAP1(key);
3906 }
3907 else
3908 {
3909 key_name[0] = KS_KEY;
3910 key_name[1] = (key & 0xff);
3911 }
3912 p = find_termcode(key_name);
3913 if (p != NULL)
3914 {
3915 if (stringval != NULL)
3916 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003917 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003918 }
3919 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003920 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3924
3925 if (options[opt_idx].flags & P_STRING)
3926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003927 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003928 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (stringval != NULL)
3930 {
3931#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003932 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003933 if ((char_u **)varp == &curbuf->b_p_key
3934 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 *stringval = vim_strsave((char_u *)"*****");
3936 else
3937#endif
3938 *stringval = vim_strsave(*(char_u **)(varp));
3939 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003940 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003943 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003944 return (options[opt_idx].flags & P_NUM)
3945 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (options[opt_idx].flags & P_NUM)
3947 *numval = *(long *)varp;
3948 else
3949 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003950 // Special case: 'modified' is b_changed, but we also want to consider
3951 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if ((int *)varp == &curbuf->b_changed)
3953 *numval = curbufIsChanged();
3954 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003955 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003957 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958}
3959#endif
3960
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003961#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003962/*
3963 * Returns the option attributes and its value. Unlike the above function it
3964 * will return either global value or local value of the option depending on
3965 * what was requested, but it will never return global value if it was
3966 * requested to return local one and vice versa. Neither it will return
3967 * buffer-local value if it was requested to return window-local one.
3968 *
3969 * Pretends that option is absent if it is not present in the requested scope
3970 * (i.e. has no global, window-local or buffer-local value depending on
3971 * opt_type). Uses
3972 *
3973 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003974 * 0 hidden or unknown option, also option that does not have requested
3975 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003976 * see SOPT_* in vim.h for other flags
3977 *
3978 * Possible opt_type values: see SREQ_* in vim.h
3979 */
3980 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003981get_option_value_strict(
3982 char_u *name,
3983 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003984 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003985 int opt_type,
3986 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003987{
3988 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003989 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003990 struct vimoption *p;
3991 int r = 0;
3992
3993 opt_idx = findoption(name);
3994 if (opt_idx < 0)
3995 return 0;
3996
3997 p = &(options[opt_idx]);
3998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003999 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004000 if (p->var == NULL)
4001 return 0;
4002
4003 if (p->flags & P_BOOL)
4004 r |= SOPT_BOOL;
4005 else if (p->flags & P_NUM)
4006 r |= SOPT_NUM;
4007 else if (p->flags & P_STRING)
4008 r |= SOPT_STRING;
4009
4010 if (p->indir == PV_NONE)
4011 {
4012 if (opt_type == SREQ_GLOBAL)
4013 r |= SOPT_GLOBAL;
4014 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004015 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004016 }
4017 else
4018 {
4019 if (p->indir & PV_BOTH)
4020 r |= SOPT_GLOBAL;
4021 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004022 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004023
4024 if (p->indir & PV_WIN)
4025 {
4026 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004028 else
4029 r |= SOPT_WIN;
4030 }
4031 else if (p->indir & PV_BUF)
4032 {
4033 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004034 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004035 else
4036 r |= SOPT_BUF;
4037 }
4038 }
4039
4040 if (stringval == NULL)
4041 return r;
4042
4043 if (opt_type == SREQ_GLOBAL)
4044 varp = p->var;
4045 else
4046 {
4047 if (opt_type == SREQ_BUF)
4048 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004049 // Special case: 'modified' is b_changed, but we also want to
4050 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004051 if (p->indir == PV_MOD)
4052 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004053 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004054 varp = NULL;
4055 }
4056#ifdef FEAT_CRYPT
4057 else if (p->indir == PV_KEY)
4058 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004059 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004060 *stringval = NULL;
4061 varp = NULL;
4062 }
4063#endif
4064 else
4065 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004066 buf_T *save_curbuf = curbuf;
4067
4068 // only getting a pointer, no need to use aucmd_prepbuf()
4069 curbuf = (buf_T *)from;
4070 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004071 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004072 curbuf = save_curbuf;
4073 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004074 }
4075 }
4076 else if (opt_type == SREQ_WIN)
4077 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004078 win_T *save_curwin = curwin;
4079
4080 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004081 curbuf = curwin->w_buffer;
4082 varp = get_varp(p);
4083 curwin = save_curwin;
4084 curbuf = curwin->w_buffer;
4085 }
4086 if (varp == p->var)
4087 return (r | SOPT_UNSET);
4088 }
4089
4090 if (varp != NULL)
4091 {
4092 if (p->flags & P_STRING)
4093 *stringval = vim_strsave(*(char_u **)(varp));
4094 else if (p->flags & P_NUM)
4095 *numval = *(long *) varp;
4096 else
4097 *numval = *(int *)varp;
4098 }
4099
4100 return r;
4101}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004102
4103/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004104 * Iterate over options. First argument is a pointer to a pointer to a
4105 * structure inside options[] array, second is option type like in the above
4106 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004107 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004108 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004109 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004110 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004111 * first argument to NULL.
4112 *
4113 * Returns full option name for current option on each call.
4114 */
4115 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004116option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004117{
4118 struct vimoption *ret = NULL;
4119 do
4120 {
4121 if (*option == NULL)
4122 *option = (void *) options;
4123 else if (((struct vimoption *) (*option))->fullname == NULL)
4124 {
4125 *option = NULL;
4126 return NULL;
4127 }
4128 else
4129 *option = (void *) (((struct vimoption *) (*option)) + 1);
4130
4131 ret = ((struct vimoption *) (*option));
4132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004133 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004134 if (ret->var == NULL)
4135 {
4136 ret = NULL;
4137 continue;
4138 }
4139
4140 switch (opt_type)
4141 {
4142 case SREQ_GLOBAL:
4143 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4144 ret = NULL;
4145 break;
4146 case SREQ_BUF:
4147 if (!(ret->indir & PV_BUF))
4148 ret = NULL;
4149 break;
4150 case SREQ_WIN:
4151 if (!(ret->indir & PV_WIN))
4152 ret = NULL;
4153 break;
4154 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004155 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004156 return NULL;
4157 }
4158 }
4159 while (ret == NULL);
4160
4161 return (char_u *)ret->fullname;
4162}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004163#endif
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004166 * Return the flags for the option at 'opt_idx'.
4167 */
4168 long_u
4169get_option_flags(int opt_idx)
4170{
4171 return options[opt_idx].flags;
4172}
4173
4174/*
4175 * Set a flag for the option at 'opt_idx'.
4176 */
4177 void
4178set_option_flag(int opt_idx, long_u flag)
4179{
4180 options[opt_idx].flags |= flag;
4181}
4182
4183/*
4184 * Clear a flag for the option at 'opt_idx'.
4185 */
4186 void
4187clear_option_flag(int opt_idx, long_u flag)
4188{
4189 options[opt_idx].flags &= ~flag;
4190}
4191
4192/*
4193 * Returns TRUE if the option at 'opt_idx' is a global option
4194 */
4195 int
4196is_global_option(int opt_idx)
4197{
4198 return options[opt_idx].indir == PV_NONE;
4199}
4200
4201/*
4202 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4203 * local value.
4204 */
4205 int
4206is_global_local_option(int opt_idx)
4207{
4208 return options[opt_idx].indir & PV_BOTH;
4209}
4210
4211/*
4212 * Returns TRUE if the option at 'opt_idx' is a window-local option
4213 */
4214 int
4215is_window_local_option(int opt_idx)
4216{
4217 return options[opt_idx].var == VAR_WIN;
4218}
4219
4220/*
4221 * Returns TRUE if the option at 'opt_idx' is a hidden option
4222 */
4223 int
4224is_hidden_option(int opt_idx)
4225{
4226 return options[opt_idx].var == NULL;
4227}
4228
4229#if defined(FEAT_CRYPT) || defined(PROTO)
4230/*
4231 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4232 */
4233 int
4234is_crypt_key_option(int opt_idx)
4235{
4236 return options[opt_idx].indir == PV_KEY;
4237}
4238#endif
4239
4240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Set the value of option "name".
4242 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004243 *
4244 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004246 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004247set_option_value(
4248 char_u *name,
4249 long number,
4250 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004251 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252{
4253 int opt_idx;
4254 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004255 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004258 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004259 {
4260 int key;
4261
4262 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004263 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004264 {
4265 char_u key_name[2];
4266
4267 if (key < 0)
4268 {
4269 key_name[0] = KEY2TERMCAP0(key);
4270 key_name[1] = KEY2TERMCAP1(key);
4271 }
4272 else
4273 {
4274 key_name[0] = KS_KEY;
4275 key_name[1] = (key & 0xff);
4276 }
4277 add_termcode(key_name, string, FALSE);
4278 if (full_screen)
4279 ttest(FALSE);
4280 redraw_all_later(CLEAR);
4281 return NULL;
4282 }
4283
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004284 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 else
4287 {
4288 flags = options[opt_idx].flags;
4289#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004290 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004293 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004294 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004297 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004298 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 else
4300 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004301 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004302 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004304 if (number == 0 && string != NULL)
4305 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004306 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004308 // Either we are given a string or we are setting option
4309 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004310 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004311 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004312 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004313 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004314 // There's another character after zeros or the string
4315 // is empty. In both cases, we are trying to set a
4316 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004317 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004318 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004319 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004320
4321 }
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004324 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004325 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004327 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004328 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 }
4331 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004332 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333}
4334
4335/*
4336 * Get the terminal code for a terminal option.
4337 * Returns NULL when not found.
4338 */
4339 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004340get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
4342 int opt_idx;
4343 char_u *varp;
4344
4345 if (tname[0] != 't' || tname[1] != '_' ||
4346 tname[2] == NUL || tname[3] == NUL)
4347 return NULL;
4348 if ((opt_idx = findoption(tname)) >= 0)
4349 {
4350 varp = get_varp(&(options[opt_idx]));
4351 if (varp != NULL)
4352 varp = *(char_u **)(varp);
4353 return varp;
4354 }
4355 return find_termcode(tname + 2);
4356}
4357
4358 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004359get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360{
4361 int i;
4362
4363 i = findoption((char_u *)"hl");
4364 if (i >= 0)
4365 return options[i].def_val[VI_DEFAULT];
4366 return (char_u *)NULL;
4367}
4368
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004369 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004370get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004371{
4372 int i;
4373
4374 i = findoption((char_u *)"enc");
4375 if (i >= 0)
4376 return options[i].def_val[VI_DEFAULT];
4377 return (char_u *)NULL;
4378}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004379
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380/*
4381 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004382 * When "has_lt" is true there is a '<' before "*arg_arg".
4383 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 */
4385 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004386find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004388 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004390 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 /*
4393 * Don't use get_special_key_code() for t_xx, we don't want it to call
4394 * add_termcap_entry().
4395 */
4396 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4397 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004398 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004400 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004402 key = find_special_key(&arg, &modifiers,
4403 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004404 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 key = 0;
4406 }
4407 return key;
4408}
4409
4410/*
4411 * if 'all' == 0: show changed options
4412 * if 'all' == 1: show all normal options
4413 * if 'all' == 2: show all terminal options
4414 */
4415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004416showoptions(
4417 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004418 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
4420 struct vimoption *p;
4421 int col;
4422 int isterm;
4423 char_u *varp;
4424 struct vimoption **items;
4425 int item_count;
4426 int run;
4427 int row, rows;
4428 int cols;
4429 int i;
4430 int len;
4431
4432#define INC 20
4433#define GAP 3
4434
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004435 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 if (items == NULL)
4437 return;
4438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004439 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004441 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004443 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004445 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004447 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004450 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 * 1. display the short items
4452 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004453 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 */
4455 for (run = 1; run <= 2 && !got_int; ++run)
4456 {
4457 /*
4458 * collect the items in items[]
4459 */
4460 item_count = 0;
4461 for (p = &options[0]; p->fullname != NULL; p++)
4462 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004463 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004464 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004465 continue;
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 varp = NULL;
4468 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004469 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
4471 if (p->indir != PV_NONE && !isterm)
4472 varp = get_varp_scope(p, opt_flags);
4473 }
4474 else
4475 varp = get_varp(p);
4476 if (varp != NULL
4477 && ((all == 2 && isterm)
4478 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004479 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004481 if (opt_flags & OPT_ONECOLUMN)
4482 len = Columns;
4483 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004484 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 else
4486 {
4487 option_value2string(p, opt_flags);
4488 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4489 }
4490 if ((len <= INC - GAP && run == 1) ||
4491 (len > INC - GAP && run == 2))
4492 items[item_count++] = p;
4493 }
4494 }
4495
4496 /*
4497 * display the items
4498 */
4499 if (run == 1)
4500 {
4501 cols = (Columns + GAP - 3) / INC;
4502 if (cols == 0)
4503 cols = 1;
4504 rows = (item_count + cols - 1) / cols;
4505 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004506 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 rows = item_count;
4508 for (row = 0; row < rows && !got_int; ++row)
4509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004510 msg_putchar('\n'); // go to next line
4511 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 break;
4513 col = 0;
4514 for (i = row; i < item_count; i += rows)
4515 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004516 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 showoneopt(items[i], opt_flags);
4518 col += INC;
4519 }
4520 out_flush();
4521 ui_breakcheck();
4522 }
4523 }
4524 vim_free(items);
4525}
4526
4527/*
4528 * Return TRUE if option "p" has its default value.
4529 */
4530 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004531optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532{
4533 int dvi;
4534
4535 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004536 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004537 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004539 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004541 // the cast to long is required for Manx C, long_i is
4542 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004543 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004544 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4546}
4547
4548/*
4549 * showoneopt: show the value of one option
4550 * must not be called with a hidden option!
4551 */
4552 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004553showoneopt(
4554 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004555 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004557 char_u *varp;
4558 int save_silent = silent_mode;
4559
4560 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004561 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 varp = get_varp_scope(p, opt_flags);
4564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004565 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4567 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004568 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004570 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004572 msg_puts(" ");
4573 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (!(p->flags & P_BOOL))
4575 {
4576 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004577 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 option_value2string(p, opt_flags);
4579 msg_outtrans(NameBuff);
4580 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004581
4582 silent_mode = save_silent;
4583 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584}
4585
4586/*
4587 * Write modified options as ":set" commands to a file.
4588 *
4589 * There are three values for "opt_flags":
4590 * OPT_GLOBAL: Write global option values and fresh values of
4591 * buffer-local options (used for start of a session
4592 * file).
4593 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4594 * curwin (used for a vimrc file).
4595 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4596 * and local values for window-local options of
4597 * curwin. Local values are also written when at the
4598 * default value, because a modeline or autocommand
4599 * may have set them when doing ":edit file" and the
4600 * user has set them back at the default or fresh
4601 * value.
4602 * When "local_only" is TRUE, don't write fresh
4603 * values, only local values (for ":mkview").
4604 * (fresh value = value used for a new buffer or window for a local option).
4605 *
4606 * Return FAIL on error, OK otherwise.
4607 */
4608 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004609makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004612 char_u *varp; // currently used value
4613 char_u *varp_fresh; // local value
4614 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char *cmd;
4616 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004617 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 /*
4620 * The options that don't have a default (terminal name, columns, lines)
4621 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004622 * Do the loop over "options[]" twice: once for options with the
4623 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004625 for (pri = 1; pri >= 0; --pri)
4626 {
4627 for (p = &options[0]; !istermoption(p); p++)
4628 if (!(p->flags & P_NO_MKRC)
4629 && !istermoption(p)
4630 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004632 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4634 continue;
4635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004636 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4637 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4639 continue;
4640
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004641 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004643 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 continue;
4645
Bram Moolenaard23b7142021-04-17 21:04:34 +02004646 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4647 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004648 continue;
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 round = 2;
4651 if (p->indir != PV_NONE)
4652 {
4653 if (p->var == VAR_WIN)
4654 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004655 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (!(opt_flags & OPT_LOCAL))
4657 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004658 // When fresh value of window-local option is not at the
4659 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4661 {
4662 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004663 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
4665 round = 1;
4666 varp_local = varp;
4667 varp = varp_fresh;
4668 }
4669 }
4670 }
4671 }
4672
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004673 // Round 1: fresh value for window-local options.
4674 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 for ( ; round <= 2; varp = varp_local, ++round)
4676 {
4677 if (round == 1 || (opt_flags & OPT_GLOBAL))
4678 cmd = "set";
4679 else
4680 cmd = "setlocal";
4681
4682 if (p->flags & P_BOOL)
4683 {
4684 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4685 return FAIL;
4686 }
4687 else if (p->flags & P_NUM)
4688 {
4689 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4690 return FAIL;
4691 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004692 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004694 int do_endif = FALSE;
4695
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004696 // Don't set 'syntax' and 'filetype' again if the value is
4697 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004698 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004699#if defined(FEAT_SYN_HL)
4700 p->indir == PV_SYN ||
4701#endif
4702 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4705 *(char_u **)(varp)) < 0
4706 || put_eol(fd) < 0)
4707 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004708 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004711 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004713 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 if (put_line(fd, "endif") == FAIL)
4716 return FAIL;
4717 }
4718 }
4719 }
4720 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return OK;
4723}
4724
4725#if defined(FEAT_FOLDING) || defined(PROTO)
4726/*
4727 * Generate set commands for the local fold options only. Used when
4728 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4729 */
4730 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004731makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004733 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004735 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 == FAIL
4737# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004738 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004740 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 == FAIL
4742 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4743 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4744 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4745 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4746 )
4747 return FAIL;
4748
4749 return OK;
4750}
4751#endif
4752
4753 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004754put_setstring(
4755 FILE *fd,
4756 char *cmd,
4757 char *name,
4758 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004759 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004762 char_u *buf = NULL;
4763 char_u *part = NULL;
4764 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4767 return FAIL;
4768 if (*valuep != NULL)
4769 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004770 // Output 'pastetoggle' as key names. For other
4771 // options some characters have to be escaped with
4772 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (valuep == &p_pt)
4774 {
4775 s = *valuep;
4776 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004777 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 return FAIL;
4779 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004780 // expand the option value, replace $HOME by ~
4781 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004783 int size = (int)STRLEN(*valuep) + 1;
4784
4785 // replace home directory in the whole option value into "buf"
4786 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004787 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004788 goto fail;
4789 home_replace(NULL, *valuep, buf, size, FALSE);
4790
4791 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004792 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004793 // can be expanded when read back.
4794 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4795 && vim_strchr(*valuep, ',') != NULL)
4796 {
4797 part = alloc(size);
4798 if (part == NULL)
4799 goto fail;
4800
4801 // write line break to clear the option, e.g. ':set rtp='
4802 if (put_eol(fd) == FAIL)
4803 goto fail;
4804
4805 p = buf;
4806 while (*p != NUL)
4807 {
4808 // for each comma separated option part, append value to
4809 // the option, :set rtp+=value
4810 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4811 goto fail;
4812 (void)copy_option_part(&p, part, size, ",");
4813 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4814 goto fail;
4815 }
4816 vim_free(buf);
4817 vim_free(part);
4818 return OK;
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004821 {
4822 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004824 }
4825 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827 else if (put_escstr(fd, *valuep, 2) == FAIL)
4828 return FAIL;
4829 }
4830 if (put_eol(fd) < 0)
4831 return FAIL;
4832 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004833fail:
4834 vim_free(buf);
4835 vim_free(part);
4836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837}
4838
4839 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004840put_setnum(
4841 FILE *fd,
4842 char *cmd,
4843 char *name,
4844 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
4846 long wc;
4847
4848 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4849 return FAIL;
4850 if (wc_use_keyname((char_u *)valuep, &wc))
4851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004852 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4854 return FAIL;
4855 }
4856 else if (fprintf(fd, "%ld", *valuep) < 0)
4857 return FAIL;
4858 if (put_eol(fd) < 0)
4859 return FAIL;
4860 return OK;
4861}
4862
4863 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004864put_setbool(
4865 FILE *fd,
4866 char *cmd,
4867 char *name,
4868 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004870 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004871 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4873 || put_eol(fd) < 0)
4874 return FAIL;
4875 return OK;
4876}
4877
4878/*
4879 * Clear all the terminal options.
4880 * If the option has been allocated, free the memory.
4881 * Terminal options are never hidden or indirect.
4882 */
4883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004884clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 /*
4887 * Reset a few things before clearing the old options. This may cause
4888 * outputting a few things that the terminal doesn't understand, but the
4889 * screen will be cleared later, so this is OK.
4890 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004891 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004893 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#endif
4895#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004896 // When starting the GUI close the display opened for the clipboard.
4897 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (gui.starting)
4899 clear_xterm_clip();
4900#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004901 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004903 free_termoptions();
4904}
4905
4906 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004907free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004908{
4909 struct vimoption *p;
4910
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004911 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 if (istermoption(p))
4913 {
4914 if (p->flags & P_ALLOCED)
4915 free_string_option(*(char_u **)(p->var));
4916 if (p->flags & P_DEF_ALLOCED)
4917 free_string_option(p->def_val[VI_DEFAULT]);
4918 *(char_u **)(p->var) = empty_option;
4919 p->def_val[VI_DEFAULT] = empty_option;
4920 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004921#ifdef FEAT_EVAL
4922 // remember where the option was cleared
4923 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 clear_termcodes();
4927}
4928
4929/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004930 * Free the string for one term option, if it was allocated.
4931 * Set the string to empty_option and clear allocated flag.
4932 * "var" points to the option value.
4933 */
4934 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004935free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004936{
4937 struct vimoption *p;
4938
4939 for (p = &options[0]; p->fullname != NULL; p++)
4940 if (p->var == var)
4941 {
4942 if (p->flags & P_ALLOCED)
4943 free_string_option(*(char_u **)(p->var));
4944 *(char_u **)(p->var) = empty_option;
4945 p->flags &= ~P_ALLOCED;
4946 break;
4947 }
4948}
4949
4950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 * Set the terminal option defaults to the current value.
4952 * Used after setting the terminal name.
4953 */
4954 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004955set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
4957 struct vimoption *p;
4958
4959 for (p = &options[0]; p->fullname != NULL; p++)
4960 {
4961 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4962 {
4963 if (p->flags & P_DEF_ALLOCED)
4964 {
4965 free_string_option(p->def_val[VI_DEFAULT]);
4966 p->flags &= ~P_DEF_ALLOCED;
4967 }
4968 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4969 if (p->flags & P_ALLOCED)
4970 {
4971 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004972 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975 }
4976}
4977
4978/*
4979 * return TRUE if 'p' starts with 't_'
4980 */
4981 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004982istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
4984 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4985}
4986
Bram Moolenaardac13472019-09-16 21:06:21 +02004987/*
4988 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4989 */
4990 int
4991istermoption_idx(int opt_idx)
4992{
4993 return istermoption(&options[opt_idx]);
4994}
4995
Bram Moolenaar113e1072019-01-20 15:30:40 +01004996#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004998 * Unset local option value, similar to ":set opt<".
4999 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005001unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005002{
5003 struct vimoption *p;
5004 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005005 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005006
5007 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005008 if (opt_idx < 0)
5009 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005010 p = &(options[opt_idx]);
5011
5012 switch ((int)p->indir)
5013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005014 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005015 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005016 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005017 break;
5018 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005019 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005020 break;
5021 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005022 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005023 break;
5024 case PV_AR:
5025 buf->b_p_ar = -1;
5026 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005027 case PV_BKC:
5028 clear_string_option(&buf->b_p_bkc);
5029 buf->b_bkc_flags = 0;
5030 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005031 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005032 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005033 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005034 case PV_TC:
5035 clear_string_option(&buf->b_p_tc);
5036 buf->b_tc_flags = 0;
5037 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005038 case PV_SISO:
5039 curwin->w_p_siso = -1;
5040 break;
5041 case PV_SO:
5042 curwin->w_p_so = -1;
5043 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005044#ifdef FEAT_FIND_ID
5045 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005046 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005047 break;
5048 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005049 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005050 break;
5051#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005053 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005054 break;
5055 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005056 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005057 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005058 case PV_FP:
5059 clear_string_option(&buf->b_p_fp);
5060 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005061#ifdef FEAT_QUICKFIX
5062 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005063 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005064 break;
5065 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005066 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005067 break;
5068 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005069 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005070 break;
5071#endif
5072#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5073 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005074 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005075 break;
5076#endif
5077#if defined(FEAT_CRYPT)
5078 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005079 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005080 break;
5081#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005082#ifdef FEAT_LINEBREAK
5083 case PV_SBR:
5084 clear_string_option(&((win_T *)from)->w_p_sbr);
5085 break;
5086#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005087#ifdef FEAT_STL_OPT
5088 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005089 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005090 break;
5091#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005092 case PV_UL:
5093 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5094 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005095#ifdef FEAT_LISP
5096 case PV_LW:
5097 clear_string_option(&buf->b_p_lw);
5098 break;
5099#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005100 case PV_MENC:
5101 clear_string_option(&buf->b_p_menc);
5102 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005103 case PV_LCS:
5104 clear_string_option(&((win_T *)from)->w_p_lcs);
5105 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5106 redraw_later(NOT_VALID);
5107 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005108 }
5109}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005110#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005111
5112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 * Get pointer to option variable, depending on local or global scope.
5114 */
5115 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005116get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
5118 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5119 {
5120 if (p->var == VAR_WIN)
5121 return (char_u *)GLOBAL_WO(get_varp(p));
5122 return p->var;
5123 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005124 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 switch ((int)p->indir)
5127 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005128 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005130 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5131 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5132 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005134 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5135 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5136 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005137 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005138 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005139 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005140 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5141 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005143 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5144 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005146 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5147 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005148#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5149 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5150#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005151#if defined(FEAT_CRYPT)
5152 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5153#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005154#ifdef FEAT_LINEBREAK
5155 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5156#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005157#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005158 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005159#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005160 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005161#ifdef FEAT_LISP
5162 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5163#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005164 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005165 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005166 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005169 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171 return get_varp(p);
5172}
5173
5174/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005175 * Get pointer to option variable at 'opt_idx', depending on local or global
5176 * scope.
5177 */
5178 char_u *
5179get_option_varp_scope(int opt_idx, int opt_flags)
5180{
5181 return get_varp_scope(&(options[opt_idx]), opt_flags);
5182}
5183
5184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 * Get pointer to option variable.
5186 */
5187 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005188get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005190 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (p->var == NULL)
5192 return NULL;
5193
5194 switch ((int)p->indir)
5195 {
5196 case PV_NONE: return p->var;
5197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005198 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005199 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005201 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005203 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005205 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005207 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005209 case PV_TC: return *curbuf->b_p_tc != NUL
5210 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005211 case PV_BKC: return *curbuf->b_p_bkc != NUL
5212 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005213 case PV_SISO: return curwin->w_p_siso >= 0
5214 ? (char_u *)&(curwin->w_p_siso) : p->var;
5215 case PV_SO: return curwin->w_p_so >= 0
5216 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005218 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005220 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5222#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005223 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005225 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005227 case PV_FP: return *curbuf->b_p_fp != NUL
5228 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005230 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005232 case PV_GP: return *curbuf->b_p_gp != NUL
5233 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5234 case PV_MP: return *curbuf->b_p_mp != NUL
5235 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005237#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5238 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5239 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5240#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005241#if defined(FEAT_CRYPT)
5242 case PV_CM: return *curbuf->b_p_cm != NUL
5243 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5244#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005245#ifdef FEAT_LINEBREAK
5246 case PV_SBR: return *curwin->w_p_sbr != NUL
5247 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5248#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005249#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005250 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005251 ? (char_u *)&(curwin->w_p_stl) : p->var;
5252#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005253 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5254 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005255#ifdef FEAT_LISP
5256 case PV_LW: return *curbuf->b_p_lw != NUL
5257 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5258#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005259 case PV_MENC: return *curbuf->b_p_menc != NUL
5260 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261#ifdef FEAT_ARABIC
5262 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5263#endif
5264 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005265 case PV_LCS: return *curwin->w_p_lcs != NUL
5266 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005267#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005268 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5269#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005270#ifdef FEAT_SYN_HL
5271 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5272 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005273 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005274 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#ifdef FEAT_DIFF
5277 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5278#endif
5279#ifdef FEAT_FOLDING
5280 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5281 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5282 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5283 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5284 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5285 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5286 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5287# ifdef FEAT_EVAL
5288 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5289 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5290# endif
5291 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5292#endif
5293 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005294 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005295#ifdef FEAT_LINEBREAK
5296 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005299 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005300#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5302#endif
5303#ifdef FEAT_RIGHTLEFT
5304 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5305 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5306#endif
5307 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5308 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5309#ifdef FEAT_LINEBREAK
5310 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005311 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5312 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005314 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005316 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005317#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005318 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5319 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5320#endif
5321#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005322 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5323 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5324 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5328 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5331 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5333 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5334#ifdef FEAT_CINDENT
5335 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5336 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5337 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5338#endif
5339#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5340 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#ifdef FEAT_FOLDING
5344 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005347#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005348 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005350#ifdef FEAT_COMPL_FUNC
5351 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005352 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005353#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005354#ifdef FEAT_EVAL
5355 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005358 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005364 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5366 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5367 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5368 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5369#ifdef FEAT_FIND_ID
5370# ifdef FEAT_EVAL
5371 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5372# endif
5373#endif
5374#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5375 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5376 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5377#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005378#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005379 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381#ifdef FEAT_CRYPT
5382 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5383#endif
5384#ifdef FEAT_LISP
5385 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5386#endif
5387 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5388 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5389 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5390 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5391 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005393#ifdef FEAT_TEXTOBJ
5394 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5397#ifdef FEAT_SMARTINDENT
5398 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5402#ifdef FEAT_SEARCHPATH
5403 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5404#endif
5405 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5406#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005407 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005408 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5409#endif
5410#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005411 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5412 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5413 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005414 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#endif
5416 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5417 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5418 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5419 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005420#ifdef FEAT_PERSISTENT_UNDO
5421 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5424#ifdef FEAT_KEYMAP
5425 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5426#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005427#ifdef FEAT_SIGNS
5428 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5429#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005430#ifdef FEAT_VARTABS
5431 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5432 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5433#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005434 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005436 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 return (char_u *)&(curbuf->b_p_wm);
5438}
5439
5440/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005441 * Return a pointer to the variable for option at 'opt_idx'
5442 */
5443 char_u *
5444get_option_var(int opt_idx)
5445{
5446 return options[opt_idx].var;
5447}
5448
5449/*
5450 * Return the full name of the option at 'opt_idx'
5451 */
5452 char_u *
5453get_option_fullname(int opt_idx)
5454{
5455 return (char_u *)options[opt_idx].fullname;
5456}
5457
5458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 * Get the value of 'equalprg', either the buffer-local one or the global one.
5460 */
5461 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005462get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 if (*curbuf->b_p_ep == NUL)
5465 return p_ep;
5466 return curbuf->b_p_ep;
5467}
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469/*
5470 * Copy options from one window to another.
5471 * Used when splitting a window.
5472 */
5473 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5477 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005478 after_copy_winopt(wp_to);
5479}
5480
5481/*
5482 * After copying window options: update variables depending on options.
5483 */
5484 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005485after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005486{
5487#ifdef FEAT_LINEBREAK
5488 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005489#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005490#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005491 fill_culopt_flags(NULL, wp);
5492 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005493#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005494 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497/*
5498 * Copy the options from one winopt_T to another.
5499 * Doesn't free the old option values in "to", use clear_winopt() for that.
5500 * The 'scroll' option is not copied, because it depends on the window height.
5501 * The 'previewwindow' option is reset, there can be only one preview window.
5502 */
5503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506#ifdef FEAT_ARABIC
5507 to->wo_arab = from->wo_arab;
5508#endif
5509 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005510 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005512 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005513#ifdef FEAT_LINEBREAK
5514 to->wo_nuw = from->wo_nuw;
5515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#ifdef FEAT_RIGHTLEFT
5517 to->wo_rl = from->wo_rl;
5518 to->wo_rlc = vim_strsave(from->wo_rlc);
5519#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005520#ifdef FEAT_LINEBREAK
5521 to->wo_sbr = vim_strsave(from->wo_sbr);
5522#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005523#ifdef FEAT_STL_OPT
5524 to->wo_stl = vim_strsave(from->wo_stl);
5525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005527#ifdef FEAT_DIFF
5528 to->wo_wrap_save = from->wo_wrap_save;
5529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530#ifdef FEAT_LINEBREAK
5531 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005532 to->wo_bri = from->wo_bri;
5533 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005535 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005537 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005538 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005539 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005540#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005541 to->wo_spell = from->wo_spell;
5542#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005543#ifdef FEAT_SYN_HL
5544 to->wo_cuc = from->wo_cuc;
5545 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005546 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005547 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549#ifdef FEAT_DIFF
5550 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005551 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005553#ifdef FEAT_CONCEAL
5554 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005555 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005556#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005557#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005558 to->wo_twk = vim_strsave(from->wo_twk);
5559 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561#ifdef FEAT_FOLDING
5562 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005563 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005565 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 to->wo_fdi = vim_strsave(from->wo_fdi);
5567 to->wo_fml = from->wo_fml;
5568 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005569 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005571 to->wo_fdm_save = from->wo_diff_saved
5572 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 to->wo_fdn = from->wo_fdn;
5574# ifdef FEAT_EVAL
5575 to->wo_fde = vim_strsave(from->wo_fde);
5576 to->wo_fdt = vim_strsave(from->wo_fdt);
5577# endif
5578 to->wo_fmr = vim_strsave(from->wo_fmr);
5579#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005580#ifdef FEAT_SIGNS
5581 to->wo_scl = vim_strsave(from->wo_scl);
5582#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005583
5584#ifdef FEAT_EVAL
5585 // Copy the script context so that we know where the value was last set.
5586 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5587 sizeof(to->wo_script_ctx));
5588#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005589 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590}
5591
5592/*
5593 * Check string options in a window for a NULL value.
5594 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005596check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 check_winopt(&win->w_onebuf_opt);
5599 check_winopt(&win->w_allbuf_opt);
5600}
5601
5602/*
5603 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5604 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005606check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
5608#ifdef FEAT_FOLDING
5609 check_string_option(&wop->wo_fdi);
5610 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005611 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612# ifdef FEAT_EVAL
5613 check_string_option(&wop->wo_fde);
5614 check_string_option(&wop->wo_fdt);
5615# endif
5616 check_string_option(&wop->wo_fmr);
5617#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005618#ifdef FEAT_SIGNS
5619 check_string_option(&wop->wo_scl);
5620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621#ifdef FEAT_RIGHTLEFT
5622 check_string_option(&wop->wo_rlc);
5623#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005624#ifdef FEAT_LINEBREAK
5625 check_string_option(&wop->wo_sbr);
5626#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005627#ifdef FEAT_STL_OPT
5628 check_string_option(&wop->wo_stl);
5629#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005631 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005632 check_string_option(&wop->wo_cc);
5633#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005634#ifdef FEAT_CONCEAL
5635 check_string_option(&wop->wo_cocu);
5636#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005637#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005638 check_string_option(&wop->wo_twk);
5639 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005640#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005641#ifdef FEAT_LINEBREAK
5642 check_string_option(&wop->wo_briopt);
5643#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005644 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005645 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646}
5647
5648/*
5649 * Free the allocated memory inside a winopt_T.
5650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005652clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654#ifdef FEAT_FOLDING
5655 clear_string_option(&wop->wo_fdi);
5656 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005657 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658# ifdef FEAT_EVAL
5659 clear_string_option(&wop->wo_fde);
5660 clear_string_option(&wop->wo_fdt);
5661# endif
5662 clear_string_option(&wop->wo_fmr);
5663#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005664#ifdef FEAT_SIGNS
5665 clear_string_option(&wop->wo_scl);
5666#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005667#ifdef FEAT_LINEBREAK
5668 clear_string_option(&wop->wo_briopt);
5669#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005670 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_RIGHTLEFT
5672 clear_string_option(&wop->wo_rlc);
5673#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005674#ifdef FEAT_LINEBREAK
5675 clear_string_option(&wop->wo_sbr);
5676#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005677#ifdef FEAT_STL_OPT
5678 clear_string_option(&wop->wo_stl);
5679#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005680#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005681 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005682 clear_string_option(&wop->wo_cc);
5683#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005684#ifdef FEAT_CONCEAL
5685 clear_string_option(&wop->wo_cocu);
5686#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005687#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005688 clear_string_option(&wop->wo_twk);
5689 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005690#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005691 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692}
5693
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005694#ifdef FEAT_EVAL
5695// Index into the options table for a buffer-local option enum.
5696static int buf_opt_idx[BV_COUNT];
5697# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5698
5699/*
5700 * Initialize buf_opt_idx[] if not done already.
5701 */
5702 static void
5703init_buf_opt_idx(void)
5704{
5705 static int did_init_buf_opt_idx = FALSE;
5706 int i;
5707
5708 if (did_init_buf_opt_idx)
5709 return;
5710 did_init_buf_opt_idx = TRUE;
5711 for (i = 0; !istermoption_idx(i); i++)
5712 if (options[i].indir & PV_BUF)
5713 buf_opt_idx[options[i].indir & PV_MASK] = i;
5714}
5715#else
5716# define COPY_OPT_SCTX(buf, bv)
5717#endif
5718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719/*
5720 * Copy global option values to local options for one buffer.
5721 * Used when creating a new buffer and sometimes when entering a buffer.
5722 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005723 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5725 * appropriate.
5726 * BCO_NOHELP Don't copy the values to a help buffer.
5727 */
5728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005729buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005732 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 int dont_do_help;
5734 int did_isk = FALSE;
5735
5736 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 * Skip this when the option defaults have not been set yet. Happens when
5738 * main() allocates the first buffer.
5739 */
5740 if (p_cpo != NULL)
5741 {
5742 /*
5743 * Always copy when entering and 'cpo' contains 'S'.
5744 * Don't copy when already initialized.
5745 * Don't copy when 'cpo' contains 's' and not entering.
5746 * 'S' BCO_ENTER initialized 's' should_copy
5747 * yes yes X X TRUE
5748 * yes no yes X FALSE
5749 * no X yes X FALSE
5750 * X no no yes FALSE
5751 * X no no no TRUE
5752 * no yes no X TRUE
5753 */
5754 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5755 && (buf->b_p_initialized
5756 || (!(flags & BCO_ENTER)
5757 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5758 should_copy = FALSE;
5759
5760 if (should_copy || (flags & BCO_ALWAYS))
5761 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005762#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005763 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005764 init_buf_opt_idx();
5765#endif
5766 // Don't copy the options specific to a help buffer when
5767 // BCO_NOHELP is given or the options were initialized already
5768 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5770 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005771 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
5773 save_p_isk = buf->b_p_isk;
5774 buf->b_p_isk = NULL;
5775 }
5776 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005777 * Always free the allocated strings. If not already initialized,
5778 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 */
5780 if (!buf->b_p_initialized)
5781 {
5782 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005783 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005786 switch (*p_ffs)
5787 {
5788 case 'm':
5789 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5790 case 'd':
5791 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5792 case 'u':
5793 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5794 default:
5795 buf->b_p_ff = vim_strsave(p_ff);
5796 }
5797 if (buf->b_p_ff != NULL)
5798 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 buf->b_p_bh = empty_option;
5800 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
5802 else
5803 free_buf_options(buf, FALSE);
5804
5805 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005806 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 buf->b_p_ai_nopaste = p_ai_nopaste;
5808 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005809 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005811 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 buf->b_p_tw_nopaste = p_tw_nopaste;
5813 buf->b_p_tw_nobin = p_tw_nobin;
5814 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005815 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 buf->b_p_wm_nopaste = p_wm_nopaste;
5817 buf->b_p_wm_nobin = p_wm_nobin;
5818 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005819 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005820 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005821 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005822 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005823 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005825 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005827 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005829 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 buf->b_p_ml_nobin = p_ml_nobin;
5831 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005832 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005833 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005834 buf->b_p_swf = FALSE;
5835 else
5836 {
5837 buf->b_p_swf = p_swf;
5838 COPY_OPT_SCTX(buf, BV_INF);
5839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005841 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005842#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005843 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005844 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005846#ifdef FEAT_COMPL_FUNC
5847 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005848 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005849 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005850 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005851#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005852#ifdef FEAT_EVAL
5853 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005854 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005857 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005859#ifdef FEAT_VARTABS
5860 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005861 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005862 if (p_vsts && p_vsts != empty_option)
5863 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5864 else
5865 buf->b_p_vsts_array = 0;
5866 buf->b_p_vsts_nopaste = p_vsts_nopaste
5867 ? vim_strsave(p_vsts_nopaste) : NULL;
5868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005870 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005872 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873#ifdef FEAT_FOLDING
5874 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005875 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876#endif
5877 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005878 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005879 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005880 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005881 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5882 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005884 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005886 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#ifdef FEAT_SMARTINDENT
5888 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005889 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
5891 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005892 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#ifdef FEAT_CINDENT
5894 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005895 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005897 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005899 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005901 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005904 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5906 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005907 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#endif
5909#ifdef FEAT_LISP
5910 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005911 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912#endif
5913#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005914 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005916 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005917 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005918 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005919#endif
5920#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005921 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005922 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005923 (void)compile_cap_prog(&buf->b_s);
5924 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005925 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005926 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005927 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005928 buf->b_s.b_p_spo = vim_strsave(p_spo);
5929 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930#endif
5931#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5932 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005933 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005935 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005937 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005938#if defined(FEAT_EVAL)
5939 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942#ifdef FEAT_CRYPT
5943 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945#endif
5946#ifdef FEAT_SEARCHPATH
5947 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005948 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949#endif
5950#ifdef FEAT_KEYMAP
5951 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005952 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 buf->b_kmap_state |= KEYMAP_INIT;
5954#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005955#ifdef FEAT_TERMINAL
5956 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005958#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005959 // This isn't really an option, but copying the langmap and IME
5960 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005962 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005964 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005966 // options that are normally global but also have a local value
5967 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005969 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005970 buf->b_p_bkc = empty_option;
5971 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972#ifdef FEAT_QUICKFIX
5973 buf->b_p_gp = empty_option;
5974 buf->b_p_mp = empty_option;
5975 buf->b_p_efm = empty_option;
5976#endif
5977 buf->b_p_ep = empty_option;
5978 buf->b_p_kp = empty_option;
5979 buf->b_p_path = empty_option;
5980 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005981 buf->b_p_tc = empty_option;
5982 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983#ifdef FEAT_FIND_ID
5984 buf->b_p_def = empty_option;
5985 buf->b_p_inc = empty_option;
5986# ifdef FEAT_EVAL
5987 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005988 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989# endif
5990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 buf->b_p_dict = empty_option;
5992 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005993#ifdef FEAT_TEXTOBJ
5994 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005995 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005996#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005997#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5998 buf->b_p_bexpr = empty_option;
5999#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006000#if defined(FEAT_CRYPT)
6001 buf->b_p_cm = empty_option;
6002#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006003#ifdef FEAT_PERSISTENT_UNDO
6004 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006005 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006006#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006007#ifdef FEAT_LISP
6008 buf->b_p_lw = empty_option;
6009#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006010 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011
6012 /*
6013 * Don't copy the options set by ex_help(), use the saved values,
6014 * when going from a help buffer to a non-help buffer.
6015 * Don't touch these at all when BCO_NOHELP is used and going from
6016 * or to a help buffer.
6017 */
6018 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006021#ifdef FEAT_VARTABS
6022 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6023 tabstop_set(p_vts, &buf->b_p_vts_array);
6024 else
6025 buf->b_p_vts_array = NULL;
6026#endif
6027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 else
6029 {
6030 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 did_isk = TRUE;
6033 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006034#ifdef FEAT_VARTABS
6035 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006037 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6038 tabstop_set(p_vts, &buf->b_p_vts_array);
6039 else
6040 buf->b_p_vts_array = NULL;
6041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 if (buf->b_p_bt[0] == 'h')
6044 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006046 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 }
6048 }
6049
6050 /*
6051 * When the options should be copied (ignoring BCO_ALWAYS), set the
6052 * flag that indicates that the options have been initialized.
6053 */
6054 if (should_copy)
6055 buf->b_p_initialized = TRUE;
6056 }
6057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006058 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 if (did_isk)
6060 (void)buf_init_chartab(buf, FALSE);
6061}
6062
6063/*
6064 * Reset the 'modifiable' option and its default value.
6065 */
6066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006067reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 int opt_idx;
6070
6071 curbuf->b_p_ma = FALSE;
6072 p_ma = FALSE;
6073 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006074 if (opt_idx >= 0)
6075 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076}
6077
6078/*
6079 * Set the global value for 'iminsert' to the local value.
6080 */
6081 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006082set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 p_iminsert = curbuf->b_p_iminsert;
6085}
6086
6087/*
6088 * Set the global value for 'imsearch' to the local value.
6089 */
6090 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006091set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
6093 p_imsearch = curbuf->b_p_imsearch;
6094}
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096static int expand_option_idx = -1;
6097static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6098static int expand_option_flags = 0;
6099
6100 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006101set_context_in_set_cmd(
6102 expand_T *xp,
6103 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006104 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105{
6106 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006107 long_u flags = 0; // init for GCC
6108 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 char_u *p;
6110 char_u *s;
6111 int is_term_option = FALSE;
6112 int key;
6113
6114 expand_option_flags = opt_flags;
6115
6116 xp->xp_context = EXPAND_SETTINGS;
6117 if (*arg == NUL)
6118 {
6119 xp->xp_pattern = arg;
6120 return;
6121 }
6122 p = arg + STRLEN(arg) - 1;
6123 if (*p == ' ' && *(p - 1) != '\\')
6124 {
6125 xp->xp_pattern = p + 1;
6126 return;
6127 }
6128 while (p > arg)
6129 {
6130 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006131 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (*p == ' ' || *p == ',')
6133 {
6134 while (s > arg && *(s - 1) == '\\')
6135 --s;
6136 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006137 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (*p == ' ' && ((p - s) & 1) == 0)
6139 {
6140 ++p;
6141 break;
6142 }
6143 --p;
6144 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006145 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
6147 xp->xp_context = EXPAND_BOOL_SETTINGS;
6148 p += 2;
6149 }
6150 if (STRNCMP(p, "inv", 3) == 0)
6151 {
6152 xp->xp_context = EXPAND_BOOL_SETTINGS;
6153 p += 3;
6154 }
6155 xp->xp_pattern = arg = p;
6156 if (*arg == '<')
6157 {
6158 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006159 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return;
6161 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006162 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
6164 xp->xp_context = EXPAND_NOTHING;
6165 return;
6166 }
6167 nextchar = *++p;
6168 is_term_option = TRUE;
6169 expand_option_name[2] = KEY2TERMCAP0(key);
6170 expand_option_name[3] = KEY2TERMCAP1(key);
6171 }
6172 else
6173 {
6174 if (p[0] == 't' && p[1] == '_')
6175 {
6176 p += 2;
6177 if (*p != NUL)
6178 ++p;
6179 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006180 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 nextchar = *++p;
6182 is_term_option = TRUE;
6183 expand_option_name[2] = p[-2];
6184 expand_option_name[3] = p[-1];
6185 }
6186 else
6187 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6190 p++;
6191 if (*p == NUL)
6192 return;
6193 nextchar = *p;
6194 *p = NUL;
6195 opt_idx = findoption(arg);
6196 *p = nextchar;
6197 if (opt_idx == -1 || options[opt_idx].var == NULL)
6198 {
6199 xp->xp_context = EXPAND_NOTHING;
6200 return;
6201 }
6202 flags = options[opt_idx].flags;
6203 if (flags & P_BOOL)
6204 {
6205 xp->xp_context = EXPAND_NOTHING;
6206 return;
6207 }
6208 }
6209 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006210 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6212 {
6213 ++p;
6214 nextchar = '=';
6215 }
6216 if ((nextchar != '=' && nextchar != ':')
6217 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6218 {
6219 xp->xp_context = EXPAND_UNSUCCESSFUL;
6220 return;
6221 }
6222 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6223 {
6224 xp->xp_context = EXPAND_OLD_SETTING;
6225 if (is_term_option)
6226 expand_option_idx = -1;
6227 else
6228 expand_option_idx = opt_idx;
6229 xp->xp_pattern = p + 1;
6230 return;
6231 }
6232 xp->xp_context = EXPAND_NOTHING;
6233 if (is_term_option || (flags & P_NUM))
6234 return;
6235
6236 xp->xp_pattern = p + 1;
6237
6238 if (flags & P_EXPAND)
6239 {
6240 p = options[opt_idx].var;
6241 if (p == (char_u *)&p_bdir
6242 || p == (char_u *)&p_dir
6243 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006244 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 || p == (char_u *)&p_rtp
6246#ifdef FEAT_SEARCHPATH
6247 || p == (char_u *)&p_cdpath
6248#endif
6249#ifdef FEAT_SESSION
6250 || p == (char_u *)&p_vdir
6251#endif
6252 )
6253 {
6254 xp->xp_context = EXPAND_DIRECTORIES;
6255 if (p == (char_u *)&p_path
6256#ifdef FEAT_SEARCHPATH
6257 || p == (char_u *)&p_cdpath
6258#endif
6259 )
6260 xp->xp_backslash = XP_BS_THREE;
6261 else
6262 xp->xp_backslash = XP_BS_ONE;
6263 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006264 else if (p == (char_u *)&p_ft)
6265 {
6266 xp->xp_context = EXPAND_FILETYPE;
6267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 else
6269 {
6270 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006271 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 if (p == (char_u *)&p_tags)
6273 xp->xp_backslash = XP_BS_THREE;
6274 else
6275 xp->xp_backslash = XP_BS_ONE;
6276 }
6277 }
6278
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006279 // For an option that is a list of file names, find the start of the
6280 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6282 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006283 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (*p == ' ' || *p == ',')
6285 {
6286 s = p;
6287 while (s > xp->xp_pattern && *(s - 1) == '\\')
6288 --s;
6289 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6290 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6291 {
6292 xp->xp_pattern = p + 1;
6293 break;
6294 }
6295 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006296
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006297#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006298 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006299 if (options[opt_idx].var == (char_u *)&p_sps
6300 && STRNCMP(p, "file:", 5) == 0)
6301 {
6302 xp->xp_pattern = p + 5;
6303 break;
6304 }
6305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307
6308 return;
6309}
6310
6311 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006312ExpandSettings(
6313 expand_T *xp,
6314 regmatch_T *regmatch,
6315 int *num_file,
6316 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006318 int num_normal = 0; // Nr of matching non-term-code settings
6319 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 int opt_idx;
6321 int match;
6322 int count = 0;
6323 char_u *str;
6324 int loop;
6325 int is_term_opt;
6326 char_u name_buf[MAX_KEY_NAME_LEN];
6327 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006328 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006330 // do this loop twice:
6331 // loop == 0: count the number of matching options
6332 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 for (loop = 0; loop <= 1; ++loop)
6334 {
6335 regmatch->rm_ic = ic;
6336 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6337 {
K.Takataeeec2542021-06-02 13:28:16 +02006338 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6340 {
6341 if (loop == 0)
6342 num_normal++;
6343 else
6344 (*file)[count++] = vim_strsave((char_u *)names[match]);
6345 }
6346 }
6347 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6348 opt_idx++)
6349 {
6350 if (options[opt_idx].var == NULL)
6351 continue;
6352 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6353 && !(options[opt_idx].flags & P_BOOL))
6354 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006355 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (is_term_opt && num_normal > 0)
6357 continue;
6358 match = FALSE;
6359 if (vim_regexec(regmatch, str, (colnr_T)0)
6360 || (options[opt_idx].shortname != NULL
6361 && vim_regexec(regmatch,
6362 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6363 match = TRUE;
6364 else if (is_term_opt)
6365 {
6366 name_buf[0] = '<';
6367 name_buf[1] = 't';
6368 name_buf[2] = '_';
6369 name_buf[3] = str[2];
6370 name_buf[4] = str[3];
6371 name_buf[5] = '>';
6372 name_buf[6] = NUL;
6373 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6374 {
6375 match = TRUE;
6376 str = name_buf;
6377 }
6378 }
6379 if (match)
6380 {
6381 if (loop == 0)
6382 {
6383 if (is_term_opt)
6384 num_term++;
6385 else
6386 num_normal++;
6387 }
6388 else
6389 (*file)[count++] = vim_strsave(str);
6390 }
6391 }
6392 /*
6393 * Check terminal key codes, these are not in the option table
6394 */
6395 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6396 {
6397 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6398 {
6399 if (!isprint(str[0]) || !isprint(str[1]))
6400 continue;
6401
6402 name_buf[0] = 't';
6403 name_buf[1] = '_';
6404 name_buf[2] = str[0];
6405 name_buf[3] = str[1];
6406 name_buf[4] = NUL;
6407
6408 match = FALSE;
6409 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6410 match = TRUE;
6411 else
6412 {
6413 name_buf[0] = '<';
6414 name_buf[1] = 't';
6415 name_buf[2] = '_';
6416 name_buf[3] = str[0];
6417 name_buf[4] = str[1];
6418 name_buf[5] = '>';
6419 name_buf[6] = NUL;
6420
6421 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6422 match = TRUE;
6423 }
6424 if (match)
6425 {
6426 if (loop == 0)
6427 num_term++;
6428 else
6429 (*file)[count++] = vim_strsave(name_buf);
6430 }
6431 }
6432
6433 /*
6434 * Check special key names.
6435 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006436 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6438 {
6439 name_buf[0] = '<';
6440 STRCPY(name_buf + 1, str);
6441 STRCAT(name_buf, ">");
6442
6443 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6444 {
6445 if (loop == 0)
6446 num_term++;
6447 else
6448 (*file)[count++] = vim_strsave(name_buf);
6449 }
6450 }
6451 }
6452 if (loop == 0)
6453 {
6454 if (num_normal > 0)
6455 *num_file = num_normal;
6456 else if (num_term > 0)
6457 *num_file = num_term;
6458 else
6459 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006460 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (*file == NULL)
6462 {
6463 *file = (char_u **)"";
6464 return FAIL;
6465 }
6466 }
6467 }
6468 return OK;
6469}
6470
6471 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006474 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 char_u *buf;
6476
6477 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006478 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (*file == NULL)
6480 return FAIL;
6481
6482 /*
6483 * For a terminal key code expand_option_idx is < 0.
6484 */
6485 if (expand_option_idx < 0)
6486 {
6487 var = find_termcode(expand_option_name + 2);
6488 if (var == NULL)
6489 expand_option_idx = findoption(expand_option_name);
6490 }
6491
6492 if (expand_option_idx >= 0)
6493 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006494 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 option_value2string(&options[expand_option_idx], expand_option_flags);
6496 var = NameBuff;
6497 }
6498 else if (var == NULL)
6499 var = (char_u *)"";
6500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006501 // A backslash is required before some characters. This is the reverse of
6502 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 buf = vim_strsave_escaped(var, escape_chars);
6504
6505 if (buf == NULL)
6506 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006507 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 return FAIL;
6509 }
6510
6511#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006512 // For MS-Windows et al. we don't double backslashes at the start and
6513 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006514 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (var[0] == '\\' && var[1] == '\\'
6516 && expand_option_idx >= 0
6517 && (options[expand_option_idx].flags & P_EXPAND)
6518 && vim_isfilec(var[2])
6519 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006520 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521#endif
6522
6523 *file[0] = buf;
6524 *num_file = 1;
6525 return OK;
6526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527
6528/*
6529 * Get the value for the numeric or string option *opp in a nice format into
6530 * NameBuff[]. Must not be called with a hidden option!
6531 */
6532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006533option_value2string(
6534 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006535 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 char_u *varp;
6538
6539 varp = get_varp_scope(opp, opt_flags);
6540
6541 if (opp->flags & P_NUM)
6542 {
6543 long wc = 0;
6544
6545 if (wc_use_keyname(varp, &wc))
6546 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6547 else if (wc != 0)
6548 STRCPY(NameBuff, transchar((int)wc));
6549 else
6550 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6551 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006552 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
6554 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006555 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 NameBuff[0] = NUL;
6557#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006558 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006559 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 STRCPY(NameBuff, "*****");
6561#endif
6562 else if (opp->flags & P_EXPAND)
6563 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006564 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 else if ((char_u **)opp->var == &p_pt)
6566 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6567 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006568 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 }
6570}
6571
6572/*
6573 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6574 * printed as a keyname.
6575 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6576 */
6577 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006578wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
6580 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6581 {
6582 *wcp = *(long *)varp;
6583 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6584 return TRUE;
6585 }
6586 return FALSE;
6587}
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 * Return TRUE if "x" is present in 'shortmess' option, or
6591 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6592 */
6593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006594shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006596 return p_shm != NULL &&
6597 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 || (vim_strchr(p_shm, 'a') != NULL
6599 && vim_strchr((char_u *)SHM_A, x) != NULL));
6600}
6601
6602/*
6603 * paste_option_changed() - Called after p_paste was set or reset.
6604 */
6605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006606paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 static int old_p_paste = FALSE;
6609 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006610 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611#ifdef FEAT_CMDL_INFO
6612 static int save_ru = 0;
6613#endif
6614#ifdef FEAT_RIGHTLEFT
6615 static int save_ri = 0;
6616 static int save_hkmap = 0;
6617#endif
6618 buf_T *buf;
6619
6620 if (p_paste)
6621 {
6622 /*
6623 * Paste switched from off to on.
6624 * Save the current values, so they can be restored later.
6625 */
6626 if (!old_p_paste)
6627 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006628 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006629 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 {
6631 buf->b_p_tw_nopaste = buf->b_p_tw;
6632 buf->b_p_wm_nopaste = buf->b_p_wm;
6633 buf->b_p_sts_nopaste = buf->b_p_sts;
6634 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006635 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006636#ifdef FEAT_VARTABS
6637 if (buf->b_p_vsts_nopaste)
6638 vim_free(buf->b_p_vsts_nopaste);
6639 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6640 ? vim_strsave(buf->b_p_vsts) : NULL;
6641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006644 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006646 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647#ifdef FEAT_CMDL_INFO
6648 save_ru = p_ru;
6649#endif
6650#ifdef FEAT_RIGHTLEFT
6651 save_ri = p_ri;
6652 save_hkmap = p_hkmap;
6653#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006654 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006655 p_ai_nopaste = p_ai;
6656 p_et_nopaste = p_et;
6657 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 p_tw_nopaste = p_tw;
6659 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006660#ifdef FEAT_VARTABS
6661 if (p_vsts_nopaste)
6662 vim_free(p_vsts_nopaste);
6663 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 }
6666
6667 /*
6668 * Always set the option values, also when 'paste' is set when it is
6669 * already on.
6670 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006671 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006672 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006674 buf->b_p_tw = 0; // textwidth is 0
6675 buf->b_p_wm = 0; // wrapmargin is 0
6676 buf->b_p_sts = 0; // softtabstop is 0
6677 buf->b_p_ai = 0; // no auto-indent
6678 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006679#ifdef FEAT_VARTABS
6680 if (buf->b_p_vsts)
6681 free_string_option(buf->b_p_vsts);
6682 buf->b_p_vsts = empty_option;
6683 if (buf->b_p_vsts_array)
6684 vim_free(buf->b_p_vsts_array);
6685 buf->b_p_vsts_array = 0;
6686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006689 // set global options
6690 p_sm = 0; // no showmatch
6691 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006694 status_redraw_all(); // redraw to remove the ruler
6695 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696#endif
6697#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006698 p_ri = 0; // no reverse insert
6699 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006701 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 p_tw = 0;
6703 p_wm = 0;
6704 p_sts = 0;
6705 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006706#ifdef FEAT_VARTABS
6707 if (p_vsts)
6708 free_string_option(p_vsts);
6709 p_vsts = empty_option;
6710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712
6713 /*
6714 * Paste switched from on to off: Restore saved values.
6715 */
6716 else if (old_p_paste)
6717 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006718 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006719 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
6721 buf->b_p_tw = buf->b_p_tw_nopaste;
6722 buf->b_p_wm = buf->b_p_wm_nopaste;
6723 buf->b_p_sts = buf->b_p_sts_nopaste;
6724 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006725 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006726#ifdef FEAT_VARTABS
6727 if (buf->b_p_vsts)
6728 free_string_option(buf->b_p_vsts);
6729 buf->b_p_vsts = buf->b_p_vsts_nopaste
6730 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6731 if (buf->b_p_vsts_array)
6732 vim_free(buf->b_p_vsts_array);
6733 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6734 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6735 else
6736 buf->b_p_vsts_array = 0;
6737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006740 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006742 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 p_ru = save_ru;
6747#endif
6748#ifdef FEAT_RIGHTLEFT
6749 p_ri = save_ri;
6750 p_hkmap = save_hkmap;
6751#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006752 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006753 p_ai = p_ai_nopaste;
6754 p_et = p_et_nopaste;
6755 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 p_tw = p_tw_nopaste;
6757 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006758#ifdef FEAT_VARTABS
6759 if (p_vsts)
6760 free_string_option(p_vsts);
6761 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
6764
6765 old_p_paste = p_paste;
6766}
6767
6768/*
6769 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6770 *
6771 * Reset 'compatible' and set the values for options that didn't get set yet
6772 * to the Vim defaults.
6773 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006774 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 */
6776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006777vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006780 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006781 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 if (!option_was_set((char_u *)"cp"))
6784 {
6785 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006786 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6788 set_option_default(opt_idx, OPT_FREE, FALSE);
6789 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006790 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006792
6793 if (fname != NULL)
6794 {
6795 p = vim_getenv(envname, &dofree);
6796 if (p == NULL)
6797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006798 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006799 p = FullName_save(fname, FALSE);
6800 if (p != NULL)
6801 {
6802 vim_setenv(envname, p);
6803 vim_free(p);
6804 }
6805 }
6806 else if (dofree)
6807 vim_free(p);
6808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809}
6810
6811/*
6812 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6813 */
6814 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006815change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006817 int opt_idx;
6818
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (p_cp != on)
6820 {
6821 p_cp = on;
6822 compatible_set();
6823 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006824 opt_idx = findoption((char_u *)"cp");
6825 if (opt_idx >= 0)
6826 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827}
6828
6829/*
6830 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006831 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 */
6833 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006834option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
6836 int idx;
6837
6838 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006839 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 return FALSE;
6841 if (options[idx].flags & P_WAS_SET)
6842 return TRUE;
6843 return FALSE;
6844}
6845
6846/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006847 * Reset the flag indicating option "name" was set.
6848 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006849 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006850reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006851{
6852 int idx = findoption(name);
6853
6854 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006855 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006857 return OK;
6858 }
6859 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006860}
6861
6862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 * compatible_set() - Called when 'compatible' has been set or unset.
6864 *
6865 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6866 * flag) to a Vi compatible value.
6867 * When 'compatible' is unset: Set all options that have a different default
6868 * for Vim (without the P_VI_DEF flag) to that default.
6869 */
6870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 int opt_idx;
6874
Bram Moolenaardac13472019-09-16 21:06:21 +02006875 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6877 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6878 set_option_default(opt_idx, OPT_FREE, p_cp);
6879 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006880 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881}
6882
Bram Moolenaardac13472019-09-16 21:06:21 +02006883#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885/*
6886 * fill_breakat_flags() -- called when 'breakat' changes value.
6887 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006891 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 int i;
6893
6894 for (i = 0; i < 256; i++)
6895 breakat_flags[i] = FALSE;
6896
6897 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006898 for (p = p_breakat; *p; p++)
6899 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901#endif
6902
6903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 * Check if backspacing over something is allowed.
6905 */
6906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006907can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006908 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006910#ifdef FEAT_JOB_CHANNEL
6911 if (what == BS_START && bt_prompt(curbuf))
6912 return FALSE;
6913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 switch (*p_bs)
6915 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006916 case '3': return TRUE;
6917 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 case '1': return (what != BS_START);
6919 case '0': return FALSE;
6920 }
6921 return vim_strchr(p_bs, what) != NULL;
6922}
6923
6924/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006925 * Return the effective 'scrolloff' value for the current window, using the
6926 * global value when appropriate.
6927 */
6928 long
6929get_scrolloff_value(void)
6930{
6931 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6932}
6933
6934/*
6935 * Return the effective 'sidescrolloff' value for the current window, using the
6936 * global value when appropriate.
6937 */
6938 long
6939get_sidescrolloff_value(void)
6940{
6941 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6942}
6943
6944/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006945 * Get the local or global value of 'backupcopy'.
6946 */
6947 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006948get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006949{
6950 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6951}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006952
Bram Moolenaaree857022019-11-09 23:26:40 +01006953#if defined(FEAT_LINEBREAK) || defined(PROTO)
6954/*
6955 * Get the local or global value of 'showbreak'.
6956 */
6957 char_u *
6958get_showbreak_value(win_T *win)
6959{
6960 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6961 return p_sbr;
6962 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6963 return empty_option;
6964 return win->w_p_sbr;
6965}
6966#endif
6967
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006968#if defined(FEAT_EVAL) || defined(PROTO)
6969/*
6970 * Get window or buffer local options.
6971 */
6972 dict_T *
6973get_winbuf_options(int bufopt)
6974{
6975 dict_T *d;
6976 int opt_idx;
6977
6978 d = dict_alloc();
6979 if (d == NULL)
6980 return NULL;
6981
Bram Moolenaardac13472019-09-16 21:06:21 +02006982 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006983 {
6984 struct vimoption *opt = &options[opt_idx];
6985
6986 if ((bufopt && (opt->indir & PV_BUF))
6987 || (!bufopt && (opt->indir & PV_WIN)))
6988 {
6989 char_u *varp = get_varp(opt);
6990
6991 if (varp != NULL)
6992 {
6993 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006994 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006995 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006996 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006997 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006998 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006999 }
7000 }
7001 }
7002
7003 return d;
7004}
7005#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007006
Bram Moolenaardac13472019-09-16 21:06:21 +02007007#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007008/*
7009 * This is called when 'culopt' is changed
7010 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007011 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007012fill_culopt_flags(char_u *val, win_T *wp)
7013{
7014 char_u *p;
7015 char_u culopt_flags_new = 0;
7016
7017 if (val == NULL)
7018 p = wp->w_p_culopt;
7019 else
7020 p = val;
7021 while (*p != NUL)
7022 {
7023 if (STRNCMP(p, "line", 4) == 0)
7024 {
7025 p += 4;
7026 culopt_flags_new |= CULOPT_LINE;
7027 }
7028 else if (STRNCMP(p, "both", 4) == 0)
7029 {
7030 p += 4;
7031 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7032 }
7033 else if (STRNCMP(p, "number", 6) == 0)
7034 {
7035 p += 6;
7036 culopt_flags_new |= CULOPT_NBR;
7037 }
7038 else if (STRNCMP(p, "screenline", 10) == 0)
7039 {
7040 p += 10;
7041 culopt_flags_new |= CULOPT_SCRLINE;
7042 }
7043
7044 if (*p != ',' && *p != NUL)
7045 return FAIL;
7046 if (*p == ',')
7047 ++p;
7048 }
7049
7050 // Can't have both "line" and "screenline".
7051 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7052 return FAIL;
7053 wp->w_p_culopt_flags = culopt_flags_new;
7054
7055 return OK;
7056}
7057#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007058
7059/*
7060 * Get the value of 'magic' adjusted for Vim9 script.
7061 */
7062 int
7063magic_isset(void)
7064{
7065 switch (magic_overruled)
7066 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007067 case OPTION_MAGIC_ON: return TRUE;
7068 case OPTION_MAGIC_OFF: return FALSE;
7069 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007070 }
7071#ifdef FEAT_EVAL
7072 if (in_vim9script())
7073 return TRUE;
7074#endif
7075 return p_magic;
7076}