blob: 07bb71e4d6b46f455e1d8a333d92c491d605695a [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 Moolenaar208f0b42021-06-20 12:40:08 +02001309 if (!in_vim9script())
1310 // skip white space, allow ":set ai ?", ":set hlsearch !"
1311 while (VIM_ISWHITE(arg[len]))
1312 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314 adding = FALSE;
1315 prepending = FALSE;
1316 removing = FALSE;
1317 if (arg[len] != NUL && arg[len + 1] == '=')
1318 {
1319 if (arg[len] == '+')
1320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001321 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 ++len;
1323 }
1324 else if (arg[len] == '^')
1325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001326 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 ++len;
1328 }
1329 else if (arg[len] == '-')
1330 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001331 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 ++len;
1333 }
1334 }
1335 nextchar = arg[len];
1336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001337 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001339 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 goto skip;
1341 }
1342
1343 if (opt_idx >= 0)
1344 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001345 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001347 // Only give an error message when requesting the value of
1348 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1350 && (!(options[opt_idx].flags & P_BOOL)
1351 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001352 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 goto skip;
1354 }
1355
1356 flags = options[opt_idx].flags;
1357 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1358 }
1359 else
1360 {
1361 flags = P_STRING;
1362 if (key < 0)
1363 {
1364 key_name[0] = KEY2TERMCAP0(key);
1365 key_name[1] = KEY2TERMCAP1(key);
1366 }
1367 else
1368 {
1369 key_name[0] = KS_KEY;
1370 key_name[1] = (key & 0xff);
1371 }
1372 }
1373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001374 // Skip all options that are not window-local (used when showing
1375 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001376 if ((opt_flags & OPT_WINONLY)
1377 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1378 goto skip;
1379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001380 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001381 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1382 && options[opt_idx].var == VAR_WIN)
1383 goto skip;
1384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001385 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001386 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001388 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001389 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001390 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001391 goto skip;
1392 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001393 if ((flags & P_MLE) && !p_mle)
1394 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001395 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001396 goto skip;
1397 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001398#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001399 // In diff mode some options are overruled. This avoids that
1400 // 'foldmethod' becomes "marker" instead of "diff" and that
1401 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001402 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001403 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001404 && (
1405#ifdef FEAT_FOLDING
1406 options[opt_idx].indir == PV_FDM ||
1407#endif
1408 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001409 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412
1413#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001414 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001415 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001417 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 goto skip;
1419 }
1420#endif
1421
1422 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1423 {
1424 arg += len;
1425 cp_val = p_cp;
1426 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001428 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430 cp_val = FALSE;
1431 arg += 3;
1432 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001433 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
1435 cp_val = TRUE;
1436 arg += 2;
1437 }
1438 }
1439 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001440 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 {
1442 errmsg = e_trailing;
1443 goto skip;
1444 }
1445 }
1446
1447 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001448 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001449 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 */
1451 if (nextchar == '?'
1452 || (prefix == 1
1453 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1454 && !(flags & P_BOOL)))
1455 {
1456 /*
1457 * print value
1458 */
1459 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001460 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 else
1462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001463 gotocmdline(TRUE); // cursor at status line
1464 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466 if (opt_idx >= 0)
1467 {
1468 showoneopt(&options[opt_idx], opt_flags);
1469#ifdef FEAT_EVAL
1470 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001471 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001472 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001473 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001474 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001475 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001476 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001477 (int)options[opt_idx].indir & PV_MASK]);
1478 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001479 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001480 (int)options[opt_idx].indir & PV_MASK]);
1481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#endif
1483 }
1484 else
1485 {
1486 char_u *p;
1487
1488 p = find_termcode(key_name);
1489 if (p == NULL)
1490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001491 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 goto skip;
1493 }
1494 else
1495 (void)show_one_termcode(key_name, p, TRUE);
1496 }
1497 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001498 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 errmsg = e_trailing;
1500 }
1501 else
1502 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001503 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001504 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001505
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001506 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 if (nextchar == '=' || nextchar == ':')
1509 {
1510 errmsg = e_invarg;
1511 goto skip;
1512 }
1513
1514 /*
1515 * ":set opt!": invert
1516 * ":set opt&": reset to default value
1517 * ":set opt<": reset to global value
1518 */
1519 if (nextchar == '!')
1520 value = *(int *)(varp) ^ 1;
1521 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001522 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 ((flags & P_VI_DEF) || cp_val)
1524 ? VI_DEFAULT : VIM_DEFAULT];
1525 else if (nextchar == '<')
1526 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001527 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if ((int *)varp == &curbuf->b_p_ar
1529 && opt_flags == OPT_LOCAL)
1530 value = -1;
1531 else
1532 value = *(int *)get_varp_scope(&(options[opt_idx]),
1533 OPT_GLOBAL);
1534 }
1535 else
1536 {
1537 /*
1538 * ":set invopt": invert
1539 * ":set opt" or ":set noopt": set or reset
1540 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001541 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 errmsg = e_trailing;
1544 goto skip;
1545 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001546 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 value = *(int *)(varp) ^ 1;
1548 else
1549 value = prefix;
1550 }
1551
1552 errmsg = set_bool_option(opt_idx, varp, (int)value,
1553 opt_flags);
1554 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001555 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1558 || prefix != 1)
1559 {
1560 errmsg = e_invarg;
1561 goto skip;
1562 }
1563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001564 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
1566 /*
1567 * Different ways to set a number option:
1568 * & set to default value
1569 * < set to global value
1570 * <xx> accept special key codes for 'wildchar'
1571 * c accept any non-digit for 'wildchar'
1572 * [-]0-9 set number
1573 * other error
1574 */
1575 ++arg;
1576 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 ((flags & P_VI_DEF) || cp_val)
1579 ? VI_DEFAULT : VIM_DEFAULT];
1580 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001581 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001582 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1583 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001584 if ((long *)varp == &curbuf->b_p_ul
1585 && opt_flags == OPT_LOCAL)
1586 value = NO_LOCAL_UNDOLEVEL;
1587 else
1588 value = *(long *)get_varp_scope(
1589 &(options[opt_idx]), OPT_GLOBAL);
1590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 else if (((long *)varp == &p_wc
1592 || (long *)varp == &p_wcm)
1593 && (*arg == '<'
1594 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001595 || (*arg != NUL
1596 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 && !VIM_ISDIGIT(*arg))))
1598 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001599 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (value == 0 && (long *)varp != &p_wcm)
1601 {
1602 errmsg = e_invarg;
1603 goto skip;
1604 }
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1607 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001608 // Allow negative (for 'undolevels'), octal and
1609 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001610 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001611 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001612 if (i == 0 || (arg[i] != NUL
1613 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001615 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 goto skip;
1617 }
1618 }
1619 else
1620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001621 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 goto skip;
1623 }
1624
1625 if (adding)
1626 value = *(long *)varp + value;
1627 if (prepending)
1628 value = *(long *)varp * value;
1629 if (removing)
1630 value = *(long *)varp - value;
1631 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001632 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001636 char_u *save_arg = NULL;
1637 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001638 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001639 char_u *newval;
1640 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001641 char_u *origval_l = NULL;
1642 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001643#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001644 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001645 char_u *saved_origval_l = NULL;
1646 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001647 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001648#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001649 unsigned newlen;
1650 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001651 int new_value_alloced; // new string option
1652 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001654 // When using ":set opt=val" for a global option
1655 // with a local value the local value will be
1656 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001658 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 varp = options[opt_idx].var;
1660
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001661 // The old value is kept until we are sure that the
1662 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001664
Bram Moolenaard7c96872019-06-15 17:12:48 +02001665 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1666 {
1667 origval_l = *(char_u **)get_varp_scope(
1668 &(options[opt_idx]), OPT_LOCAL);
1669 origval_g = *(char_u **)get_varp_scope(
1670 &(options[opt_idx]), OPT_GLOBAL);
1671
1672 // A global-local string option might have an empty
1673 // option as value to indicate that the global
1674 // value should be used.
1675 if (((int)options[opt_idx].indir & PV_BOTH)
1676 && origval_l == empty_option)
1677 origval_l = origval_g;
1678 }
1679
1680 // When setting the local value of a global
1681 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001682 if (((int)options[opt_idx].indir & PV_BOTH)
1683 && (opt_flags & OPT_LOCAL))
1684 origval = *(char_u **)get_varp(
1685 &options[opt_idx]);
1686 else
1687 origval = oldval;
1688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001689 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 newval = options[opt_idx].def_val[
1692 ((flags & P_VI_DEF) || cp_val)
1693 ? VI_DEFAULT : VIM_DEFAULT];
1694 if ((char_u **)varp == &p_bg)
1695 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001696 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#ifdef FEAT_GUI
1698 if (gui.in_use)
1699 newval = gui_bg_default();
1700 else
1701#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001702 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001704 else if ((char_u **)varp == &p_fencs && enc_utf8)
1705 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001707 // expand environment variables and ~ (since the
1708 // default value was already expanded, only
1709 // required when an environment variable was set
1710 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if (newval == NULL)
1712 newval = empty_option;
1713 else
1714 {
1715 s = option_expand(opt_idx, newval);
1716 if (s == NULL)
1717 s = newval;
1718 newval = vim_strsave(s);
1719 }
1720 new_value_alloced = TRUE;
1721 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
1724 newval = vim_strsave(*(char_u **)get_varp_scope(
1725 &(options[opt_idx]), OPT_GLOBAL));
1726 new_value_alloced = TRUE;
1727 }
1728 else
1729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 /*
1733 * Set 'keywordprg' to ":help" if an empty
1734 * value was passed to :set by the user.
1735 * Misuse errbuf[] for the resulting string.
1736 */
1737 if (varp == (char_u *)&p_kp
1738 && (*arg == NUL || *arg == ' '))
1739 {
1740 STRCPY(errbuf, ":help");
1741 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001742 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001745 * Convert 'backspace' number to string, for
1746 * adding, prepending and removing string.
1747 */
1748 else if (varp == (char_u *)&p_bs
1749 && VIM_ISDIGIT(**(char_u **)varp))
1750 {
1751 i = getdigits((char_u **)varp);
1752 switch (i)
1753 {
1754 case 0:
1755 *(char_u **)varp = empty_option;
1756 break;
1757 case 1:
1758 *(char_u **)varp = vim_strsave(
1759 (char_u *)"indent,eol");
1760 break;
1761 case 2:
1762 *(char_u **)varp = vim_strsave(
1763 (char_u *)"indent,eol,start");
1764 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001765 case 3:
1766 *(char_u **)varp = vim_strsave(
1767 (char_u *)"indent,eol,nostop");
1768 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001769 }
1770 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001771 if (origval == oldval)
1772 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001773 if (origval_l == oldval)
1774 origval_l = *(char_u **)varp;
1775 if (origval_g == oldval)
1776 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001777 oldval = *(char_u **)varp;
1778 }
1779 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 * Convert 'whichwrap' number to string, for
1781 * backwards compatibility with Vim 3.0.
1782 * Misuse errbuf[] for the resulting string.
1783 */
1784 else if (varp == (char_u *)&p_ww
1785 && VIM_ISDIGIT(*arg))
1786 {
1787 *errbuf = NUL;
1788 i = getdigits(&arg);
1789 if (i & 1)
1790 STRCAT(errbuf, "b,");
1791 if (i & 2)
1792 STRCAT(errbuf, "s,");
1793 if (i & 4)
1794 STRCAT(errbuf, "h,l,");
1795 if (i & 8)
1796 STRCAT(errbuf, "<,>,");
1797 if (i & 16)
1798 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 errbuf[STRLEN(errbuf) - 1] = NUL;
1801 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001802 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 /*
1805 * Remove '>' before 'dir' and 'bdir', for
1806 * backwards compatibility with version 3.0
1807 */
1808 else if ( *arg == '>'
1809 && (varp == (char_u *)&p_dir
1810 || varp == (char_u *)&p_bdir))
1811 {
1812 ++arg;
1813 }
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 /*
1816 * Copy the new string into allocated memory.
1817 * Can't use set_string_option_direct(), because
1818 * we need to remove the backslashes.
1819 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001820 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 newlen = (unsigned)STRLEN(arg) + 1;
1822 if (adding || prepending || removing)
1823 newlen += (unsigned)STRLEN(origval) + 1;
1824 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001825 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 break;
1827 s = newval;
1828
1829 /*
1830 * Copy the string, skip over escaped chars.
1831 * For MS-DOS and WIN32 backslashes before normal
1832 * file name characters are not removed, and keep
1833 * backslash at start, for "\\machine\path", but
1834 * do remove it for "\\\\machine\\path".
1835 * The reverse is found in ExpandOldSetting().
1836 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001837 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 if (*arg == '\\' && arg[1] != NUL
1840#ifdef BACKSLASH_IN_FILENAME
1841 && !((flags & P_EXPAND)
1842 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001843 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 && (arg[1] != '\\'
1845 || (s == newval
1846 && arg[2] != '\\')))
1847#endif
1848 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001849 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001851 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001853 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 mch_memmove(s, arg, (size_t)i);
1855 arg += i;
1856 s += i;
1857 }
1858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 *s++ = *arg++;
1860 }
1861 *s = NUL;
1862
1863 /*
1864 * Expand environment variables and ~.
1865 * Don't do it when adding without inserting a
1866 * comma.
1867 */
1868 if (!(adding || prepending || removing)
1869 || (flags & P_COMMA))
1870 {
1871 s = option_expand(opt_idx, newval);
1872 if (s != NULL)
1873 {
1874 vim_free(newval);
1875 newlen = (unsigned)STRLEN(s) + 1;
1876 if (adding || prepending || removing)
1877 newlen += (unsigned)STRLEN(origval) + 1;
1878 newval = alloc(newlen);
1879 if (newval == NULL)
1880 break;
1881 STRCPY(newval, s);
1882 }
1883 }
1884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001885 // locate newval[] in origval[] when removing it
1886 // and when adding to avoid duplicates
1887 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (removing || (flags & P_NODUP))
1889 {
1890 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001891 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001894 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 prepending = FALSE;
1897 adding = FALSE;
1898 STRCPY(newval, origval);
1899 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001900
1901 // if no duplicate, move pointer to end of
1902 // original value
1903 if (s == NULL)
1904 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001907 // concatenate the two strings; add a ',' if
1908 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (adding || prepending)
1910 {
1911 comma = ((flags & P_COMMA) && *origval != NUL
1912 && *newval != NUL);
1913 if (adding)
1914 {
1915 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001916 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001917 if (comma && i > 1
1918 && (flags & P_ONECOMMA) == P_ONECOMMA
1919 && origval[i - 1] == ','
1920 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001921 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 mch_memmove(newval + i + comma, newval,
1923 STRLEN(newval) + 1);
1924 mch_memmove(newval, origval, (size_t)i);
1925 }
1926 else
1927 {
1928 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001929 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 if (comma)
1932 newval[i] = ',';
1933 }
1934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001935 // Remove newval[] from origval[]. (Note: "i" has
1936 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (removing)
1938 {
1939 STRCPY(newval, origval);
1940 if (*s)
1941 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001942 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (flags & P_COMMA)
1944 {
1945 if (s == origval)
1946 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (s[i] == ',')
1949 ++i;
1950 }
1951 else
1952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001953 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 --s;
1955 ++i;
1956 }
1957 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001958 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960 }
1961
1962 if (flags & P_FLAGLIST)
1963 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001965 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001967 // if options have P_FLAGLIST and
1968 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001969 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001971 if (*s != ',' && *(s + 1) == ','
1972 && vim_strchr(s + 2, *s) != NULL)
1973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001974 // Remove the duplicated value and
1975 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001976 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001977 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001980 else
1981 {
1982 if ((!(flags & P_COMMA) || *s != ',')
1983 && vim_strchr(s + 1, *s) != NULL)
1984 {
1985 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001986 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001987 }
1988 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001989 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001993 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 arg = save_arg;
1995 new_value_alloced = TRUE;
1996 }
1997
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001998 /*
1999 * Set the new value.
2000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 *(char_u **)(varp) = newval;
2002
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002003#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002004 if (!starting
2005# ifdef FEAT_CRYPT
2006 && options[opt_idx].indir != PV_KEY
2007# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002008 && origval != NULL && newval != NULL)
2009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002010 // origval may be freed by
2011 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002012 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002013 // newval (and varp) may become invalid if the
2014 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002015 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002016 if (origval_l != NULL)
2017 saved_origval_l = vim_strsave(origval_l);
2018 if (origval_g != NULL)
2019 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002020 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002021#endif
2022
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002023 {
2024 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002025 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002026
2027 // When an option is set in the sandbox, from a
2028 // modeline or in secure mode, then deal with side
2029 // effects in secure mode. Also when the value was
2030 // set with the P_INSECURE flag and is not
2031 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002032 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002033#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002034 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002035#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002036 || (!value_is_replaced && (*p & P_INSECURE)))
2037 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002038
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002039 // Handle side effects, and set the global value
2040 // for ":set" on local options. Note: when setting
2041 // 'syntax' or 'filetype' autocommands may be
2042 // triggered that can cause havoc.
2043 errmsg = did_set_string_option(
2044 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002045 new_value_alloced, oldval, errbuf,
2046 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002047
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002048 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002051#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002052 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002053 trigger_optionsset_string(
2054 opt_idx, opt_flags, saved_origval,
2055 saved_origval_l, saved_origval_g,
2056 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002057 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002058 vim_free(saved_origval_l);
2059 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002060 vim_free(saved_newval);
2061#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002062 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (errmsg != NULL)
2064 goto skip;
2065 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002066 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 char_u *p;
2069
2070 if (nextchar == '&')
2071 {
2072 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075 else
2076 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002077 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002078 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (*p == '\\' && p[1] != NUL)
2080 ++p;
2081 nextchar = *p;
2082 *p = NUL;
2083 add_termcode(key_name, arg, FALSE);
2084 *p = nextchar;
2085 }
2086 if (full_screen)
2087 ttest(FALSE);
2088 redraw_all_later(CLEAR);
2089 }
2090 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002093 did_set_option(
2094 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096
2097skip:
2098 /*
2099 * Advance to next argument.
2100 * - skip until a blank found, taking care of backslashes
2101 * - skip blanks
2102 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2103 */
2104 for (i = 0; i < 2 ; ++i)
2105 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002106 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (*arg++ == '\\' && *arg != NUL)
2108 ++arg;
2109 arg = skipwhite(arg);
2110 if (*arg != '=')
2111 break;
2112 }
2113 }
2114
2115 if (errmsg != NULL)
2116 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002117 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002118 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (i + (arg - startarg) < IOSIZE)
2120 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002121 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 STRCAT(IObuff, ": ");
2123 mch_memmove(IObuff + i, startarg, (arg - startarg));
2124 IObuff[i + (arg - startarg)] = NUL;
2125 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002126 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 trans_characters(IObuff, IOSIZE);
2128
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 ++no_wait_return; // wait_return done later
2130 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 --no_wait_return;
2132
2133 return FAIL;
2134 }
2135
2136 arg = skipwhite(arg);
2137 }
2138
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002139theend:
2140 if (silent_mode && did_show)
2141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002142 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002143 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002144 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002145 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002146 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002147 out_flush();
2148 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002149 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002150 }
2151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 return OK;
2153}
2154
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002155/*
2156 * Call this when an option has been given a new value through a user command.
2157 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2158 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002159 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002160did_set_option(
2161 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002162 int opt_flags, // possibly with OPT_MODELINE
2163 int new_value, // value was replaced completely
2164 int value_checked) // value was checked to be safe, no need to set the
2165 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002166{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002167 long_u *p;
2168
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002169 options[opt_idx].flags |= P_WAS_SET;
2170
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002171 // When an option is set in the sandbox, from a modeline or in secure mode
2172 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2173 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002174 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002175 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002176#ifdef HAVE_SANDBOX
2177 || sandbox != 0
2178#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002179 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002180 *p = *p | P_INSECURE;
2181 else if (new_value)
2182 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002183}
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185/*
2186 * Convert a key name or string into a key value.
2187 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002188 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002190 int
2191string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002194 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (*arg == '^')
2196 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002197 if (multi_byte)
2198 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 return *arg;
2200}
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#ifdef FEAT_TITLE
2203/*
2204 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2205 * maketitle() to create and display it.
2206 * When switching the title or icon off, call mch_restore_title() to get
2207 * the old value back.
2208 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002209 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002210did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 if (starting != NO_SCREEN
2213#ifdef FEAT_GUI
2214 && !gui.starting
2215#endif
2216 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218}
2219#endif
2220
2221/*
2222 * set_options_bin - called when 'bin' changes value.
2223 */
2224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002225set_options_bin(
2226 int oldval,
2227 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002228 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 /*
2231 * The option values that are changed when 'bin' changes are
2232 * copied when 'bin is set and restored when 'bin' is reset.
2233 */
2234 if (newval)
2235 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002236 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 if (!(opt_flags & OPT_GLOBAL))
2239 {
2240 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2241 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2242 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2243 curbuf->b_p_et_nobin = curbuf->b_p_et;
2244 }
2245 if (!(opt_flags & OPT_LOCAL))
2246 {
2247 p_tw_nobin = p_tw;
2248 p_wm_nobin = p_wm;
2249 p_ml_nobin = p_ml;
2250 p_et_nobin = p_et;
2251 }
2252 }
2253
2254 if (!(opt_flags & OPT_GLOBAL))
2255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002256 curbuf->b_p_tw = 0; // no automatic line wrap
2257 curbuf->b_p_wm = 0; // no automatic line wrap
2258 curbuf->b_p_ml = 0; // no modelines
2259 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261 if (!(opt_flags & OPT_LOCAL))
2262 {
2263 p_tw = 0;
2264 p_wm = 0;
2265 p_ml = FALSE;
2266 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002267 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002270 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 if (!(opt_flags & OPT_GLOBAL))
2273 {
2274 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2275 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2276 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2277 curbuf->b_p_et = curbuf->b_p_et_nobin;
2278 }
2279 if (!(opt_flags & OPT_LOCAL))
2280 {
2281 p_tw = p_tw_nobin;
2282 p_wm = p_wm_nobin;
2283 p_ml = p_ml_nobin;
2284 p_et = p_et_nobin;
2285 }
2286 }
2287}
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289/*
2290 * Expand environment variables for some string options.
2291 * These string options cannot be indirect!
2292 * If "val" is NULL expand the current value of the option.
2293 * Return pointer to NameBuff, or NULL when not expanded.
2294 */
2295 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002296option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002298 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2300 return NULL;
2301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 // If val is longer than MAXPATHL no meaningful expansion can be done,
2303 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (val != NULL && STRLEN(val) > MAXPATHL)
2305 return NULL;
2306
2307 if (val == NULL)
2308 val = *(char_u **)options[opt_idx].var;
2309
2310 /*
2311 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2312 * Escape spaces when expanding 'tags', they are used to separate file
2313 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002314 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 */
2316 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002317 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002318#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002319 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2320#endif
2321 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 return NULL;
2324
2325 return NameBuff;
2326}
2327
2328/*
2329 * After setting various option values: recompute variables that depend on
2330 * option values.
2331 */
2332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002333didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002335 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 (void)init_chartab();
2337
Bram Moolenaardac13472019-09-16 21:06:21 +02002338 didset_string_options();
2339
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002340#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002341 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002342 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002343 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002344 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002347 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 (void)check_cedit();
2349#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002350#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002351 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002352 fill_breakat_flags();
2353#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002354 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002355}
2356
2357/*
2358 * More side effects of setting options.
2359 */
2360 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002361didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002362{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002363 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002364 (void)highlight_changed();
2365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002366 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002367 check_opt_wim();
2368
Bram Moolenaareed9d462021-02-15 20:38:25 +01002369 // Parse default for 'listchars'.
2370 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2371
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002372 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002373 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002374
2375#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002376 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002377 (void)check_clipboard_option();
2378#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002379#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002380 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002381 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002382 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002383 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385}
2386
2387/*
2388 * Check for string options that are NULL (normally only termcap options).
2389 */
2390 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002391check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393 int opt_idx;
2394
2395 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2396 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2397 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2398}
2399
2400/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002401 * Return the option index found by a pointer into term_strings[].
2402 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002404 int
2405get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002407 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2410 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002411 return opt_idx;
2412 return -1; // cannot happen: didn't find it!
2413}
2414
2415/*
2416 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2417 * Return the option index or -1 if not found.
2418 */
2419 int
2420set_term_option_alloced(char_u **p)
2421{
2422 int opt_idx = get_term_opt_idx(p);
2423
2424 if (opt_idx >= 0)
2425 options[opt_idx].flags |= P_ALLOCED;
2426 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427}
2428
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002429#if defined(FEAT_EVAL) || defined(PROTO)
2430/*
2431 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2432 * Return FALSE when it wasn't.
2433 * Return -1 for an unknown option.
2434 */
2435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002436was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002437{
2438 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002439 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002440
2441 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002442 {
2443 flagp = insecure_flag(idx, opt_flags);
2444 return (*flagp & P_INSECURE) != 0;
2445 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002446 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002447 return -1;
2448}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002449
2450/*
2451 * Get a pointer to the flags used for the P_INSECURE flag of option
2452 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002453 * NOTE: Caller must make sure that "curwin" is set to the window from which
2454 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002455 */
2456 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002457insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002458{
2459 if (opt_flags & OPT_LOCAL)
2460 switch ((int)options[opt_idx].indir)
2461 {
2462#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002463 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002464#endif
2465#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002466# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002467 case PV_FDE: return &curwin->w_p_fde_flags;
2468 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002469# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002470# ifdef FEAT_BEVAL
2471 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2472# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002473# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002474 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002475# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002476 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002477# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002478 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002479# endif
2480#endif
2481 }
2482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002483 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002484 return &options[opt_idx].flags;
2485}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002486#endif
2487
Bram Moolenaardac13472019-09-16 21:06:21 +02002488#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002489/*
2490 * Redraw the window title and/or tab page text later.
2491 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002492void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002493{
2494 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002495 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002496}
2497#endif
2498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002500 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2501 * characters or characters in "allowed".
2502 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002503 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002504valid_name(char_u *val, char *allowed)
2505{
2506 char_u *s;
2507
2508 for (s = val; *s != NUL; ++s)
2509 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2510 return FALSE;
2511 return TRUE;
2512}
2513
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002514#if defined(FEAT_EVAL) || defined(PROTO)
2515/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002516 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002517 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002518 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002519 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002520set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002521{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002522 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2523 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002524 sctx_T new_script_ctx = script_ctx;
2525
Bram Moolenaar51258742020-05-03 17:19:33 +02002526 // Modeline already has the line number set.
2527 if (!(opt_flags & OPT_MODELINE))
2528 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002529
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002530 // Remember where the option was set. For local options need to do that
2531 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002532 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002533 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002534 if (both || (opt_flags & OPT_LOCAL))
2535 {
2536 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002537 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002538 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002539 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002540 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002542
2543/*
2544 * Set the script_ctx for a termcap option.
2545 * "name" must be the two character code, e.g. "RV".
2546 * When "name" is NULL use "opt_idx".
2547 */
2548 void
2549set_term_option_sctx_idx(char *name, int opt_idx)
2550{
2551 char_u buf[5];
2552 int idx;
2553
2554 if (name == NULL)
2555 idx = opt_idx;
2556 else
2557 {
2558 buf[0] = 't';
2559 buf[1] = '_';
2560 buf[2] = name[0];
2561 buf[3] = name[1];
2562 buf[4] = 0;
2563 idx = findoption(buf);
2564 }
2565 if (idx >= 0)
2566 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2567}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002568#endif
2569
Bram Moolenaarf4140482020-02-15 23:06:45 +01002570#if defined(FEAT_EVAL)
2571/*
2572 * Apply the OptionSet autocommand.
2573 */
2574 static void
2575apply_optionset_autocmd(
2576 int opt_idx,
2577 long opt_flags,
2578 long oldval,
2579 long oldval_g,
2580 long newval,
2581 char *errmsg)
2582{
2583 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2584
2585 // Don't do this while starting up, failure or recursively.
2586 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2587 return;
2588
2589 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2590 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2591 oldval_g);
2592 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2593 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2594 (opt_flags & OPT_LOCAL) ? "local" : "global");
2595 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2596 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2597 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2598 if (opt_flags & OPT_LOCAL)
2599 {
2600 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2601 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2602 }
2603 if (opt_flags & OPT_GLOBAL)
2604 {
2605 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2606 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2607 }
2608 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2609 {
2610 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2611 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2612 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2613 }
2614 if (opt_flags & OPT_MODELINE)
2615 {
2616 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2617 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2618 }
2619 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2620 NULL, FALSE, NULL);
2621 reset_v_option_vars();
2622}
2623#endif
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625/*
2626 * Set the value of a boolean option, and take care of side effects.
2627 * Returns NULL for success, or an error message for an error.
2628 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002629 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002630set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002631 int opt_idx, // index in options[] table
2632 char_u *varp, // pointer to the option variable
2633 int value, // new value
2634 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002637#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002638 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002641 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if ((secure
2643#ifdef HAVE_SANDBOX
2644 || sandbox != 0
2645#endif
2646 ) && (options[opt_idx].flags & P_SECURE))
2647 return e_secure;
2648
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002649#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002650 // Save the global value before changing anything. This is needed as for
2651 // a global-only option setting the "local value" in fact sets the global
2652 // value (since there is only one value).
2653 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2654 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2655 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002656#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002657
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002660 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002661 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#endif
2663
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002664#ifdef FEAT_GUI
2665 need_mouse_correct = TRUE;
2666#endif
2667
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002668 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2670 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2671
2672 /*
2673 * Handle side effects of changing a bool option.
2674 */
2675
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002676 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar920694c2016-08-21 17:45:02 +02002680#ifdef FEAT_LANGMAP
2681 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002682 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002683 p_lnr = !p_lrm;
2684 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002685 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002686 p_lrm = !p_lnr;
2687#endif
2688
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002689#ifdef FEAT_SYN_HL
2690 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2691 reset_cursorline();
2692#endif
2693
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002694#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002695 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002696 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // Only take action when the option was set. When reset we do not
2699 // delete the undo file, the option may be set again without making
2700 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002701 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002702 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002703 char_u hash[UNDO_HASH_SIZE];
2704 buf_T *save_curbuf = curbuf;
2705
Bram Moolenaar29323592016-07-24 22:04:11 +02002706 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002707 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002708 // When 'undofile' is set globally: for every buffer, otherwise
2709 // only for the current buffer: Try to read in the undofile,
2710 // if one exists, the buffer wasn't changed and the buffer was
2711 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002712 if ((curbuf == save_curbuf
2713 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2714 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2715 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002716#ifdef FEAT_CRYPT
2717 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2718 continue;
2719#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002720 u_compute_hash(hash);
2721 u_read_undo(NULL, hash, curbuf->b_fname);
2722 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002723 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002724 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002725 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002726 }
2727#endif
2728
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 else if ((int *)varp == &curbuf->b_p_ro)
2730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2733 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002736 if (curbuf->b_p_ro)
2737 curbuf->b_did_warn = FALSE;
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002740 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#endif
2742 }
2743
Bram Moolenaar9c449722010-07-20 18:44:27 +02002744#ifdef FEAT_GUI
2745 else if ((int *)varp == &p_mh)
2746 {
2747 if (!p_mh)
2748 gui_mch_mousehide(FALSE);
2749 }
2750#endif
2751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002752 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002754 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002755# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002757 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2758 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002759 {
2760 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002761 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002762 }
2763# endif
2764# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002765 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002766# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002767 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002768#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002771 {
2772 redraw_titles();
2773 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002775 else if ((int *)varp == &curbuf->b_p_fixeol)
2776 {
2777 redraw_titles();
2778 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002780 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002781 {
2782 redraw_titles();
2783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784#endif
2785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002786 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 else if ((int *)varp == &curbuf->b_p_bin)
2788 {
2789 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2790#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002791 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792#endif
2793 }
2794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2797 {
2798 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2799 NULL, NULL, TRUE, curbuf);
2800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002802 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else if ((int *)varp == &curbuf->b_p_swf)
2804 {
2805 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002808 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2809 // buf->b_p_swf
2810 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002813 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 else if ((int *)varp == &p_terse)
2815 {
2816 char_u *p;
2817
2818 p = vim_strchr(p_shm, SHM_SEARCH);
2819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (p_terse && p == NULL)
2822 {
2823 STRCPY(IObuff, p_shm);
2824 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002825 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002829 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
2831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else if ((int *)varp == &p_paste)
2834 {
2835 paste_option_changed();
2836 }
2837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 else if ((int *)varp == &p_im)
2840 {
2841 if (p_im)
2842 {
2843 if ((State & INSERT) == 0)
2844 need_start_insertmode = TRUE;
2845 stop_insert_mode = FALSE;
2846 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002848 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
2850 need_start_insertmode = FALSE;
2851 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002852 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 restart_edit = 0;
2855 }
2856 }
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 else if ((int *)varp == &p_ic && p_hls)
2860 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002861 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863
2864#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 else if ((int *)varp == &p_hls)
2867 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002868 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870#endif
2871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2873 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 else if ((int *)varp == &curwin->w_p_scb)
2875 {
2876 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002879 curwin->w_scbind_pos = curwin->w_topline;
2880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaar4033c552017-09-16 20:54:51 +02002883#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else if ((int *)varp == &curwin->w_p_pvw)
2886 {
2887 if (curwin->w_p_pvw)
2888 {
2889 win_T *win;
2890
Bram Moolenaar29323592016-07-24 22:04:11 +02002891 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (win->w_p_pvw && win != curwin)
2893 {
2894 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002895 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897 }
2898 }
2899#endif
2900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 else if ((int *)varp == &curbuf->b_p_tx)
2903 {
2904 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2905 }
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 set_string_option_direct((char_u *)"ffs", -1,
2911 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002912 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915 /*
2916 * When 'lisp' option changes include/exclude '-' in
2917 * keyword characters.
2918 */
2919#ifdef FEAT_LISP
2920 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924#endif
2925
2926#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002928 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002930 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932#endif
2933
2934 else if ((int *)varp == &curbuf->b_changed)
2935 {
2936 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002939 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943
2944#ifdef BACKSLASH_IN_FILENAME
2945 else if ((int *)varp == &p_ssl)
2946 {
2947 if (p_ssl)
2948 {
2949 psepc = '/';
2950 psepcN = '\\';
2951 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 else
2954 {
2955 psepc = '\\';
2956 psepcN = '/';
2957 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 buflist_slash_adjust();
2962 alist_slash_adjust();
2963# ifdef FEAT_EVAL
2964 scriptnames_slash_adjust();
2965# endif
2966 }
2967#endif
2968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 else if ((int *)varp == &curwin->w_p_wrap)
2971 {
2972 if (curwin->w_p_wrap)
2973 curwin->w_leftcol = 0;
2974 }
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 else if ((int *)varp == &p_ea)
2977 {
2978 if (p_ea && !old_value)
2979 win_equal(curwin, FALSE, 0);
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
2982 else if ((int *)varp == &p_wiv)
2983 {
2984 /*
2985 * When 'weirdinvert' changed, set/reset 't_xs'.
2986 * Then set 'weirdinvert' according to value of 't_xs'.
2987 */
2988 if (p_wiv && !old_value)
2989 T_XS = (char_u *)"y";
2990 else if (!p_wiv && old_value)
2991 T_XS = empty_option;
2992 p_wiv = (*T_XS != NUL);
2993 }
2994
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002995#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else if ((int *)varp == &p_beval)
2997 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002998 if (!balloonEvalForTerm)
2999 {
3000 if (p_beval && !old_value)
3001 gui_mch_enable_beval_area(balloonEval);
3002 else if (!p_beval && old_value)
3003 gui_mch_disable_beval_area(balloonEval);
3004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003006#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003007#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003008 else if ((int *)varp == &p_bevalterm)
3009 {
3010 mch_bevalterm_changed();
3011 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003014#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 else if ((int *)varp == &p_acd)
3016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003018 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
3020#endif
3021
3022#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 else if ((int *)varp == &curwin->w_p_diff)
3025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003026 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003027 diff_buf_adjust(curwin);
3028# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (foldmethodIsDiff(curwin))
3030 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033#endif
3034
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003035#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 else if ((int *)varp == &p_imdisable)
3038 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (p_imdisable)
3041 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003042 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003043 // When the option is set from an autocommand, it may need to take
3044 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003045 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047#endif
3048
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003049#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003051 else if ((int *)varp == &curwin->w_p_spell)
3052 {
3053 if (curwin->w_p_spell)
3054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003055 char *errmsg = did_set_spelllang(curwin);
3056
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003057 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003058 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003059 }
3060 }
3061#endif
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#ifdef FEAT_ARABIC
3064 if ((int *)varp == &curwin->w_p_arab)
3065 {
3066 if (curwin->w_p_arab)
3067 {
3068 /*
3069 * 'arabic' is set, handle various sub-settings.
3070 */
3071 if (!p_tbidi)
3072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003073 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (!curwin->w_p_rl)
3075 {
3076 curwin->w_p_rl = TRUE;
3077 changed_window_setting();
3078 }
3079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 if (!p_arshape)
3082 {
3083 p_arshape = TRUE;
3084 redraw_later_clear();
3085 }
3086 }
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // Arabic requires a utf-8 encoding, inform the user if its not
3089 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003091 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003092 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3093
Bram Moolenaar8820b482017-03-16 17:23:31 +01003094 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003095 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003096#ifdef FEAT_EVAL
3097 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3098#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003105 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3107 OPT_LOCAL);
3108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110 else
3111 {
3112 /*
3113 * 'arabic' is reset, handle various sub-settings.
3114 */
3115 if (!p_tbidi)
3116 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003117 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if (curwin->w_p_rl)
3119 {
3120 curwin->w_p_rl = FALSE;
3121 changed_window_setting();
3122 }
3123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 // 'arabicshape' isn't reset, it is a global option and
3125 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
3127
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003128 // 'delcombine' isn't reset, it is a global option and another
3129 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003132 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 curbuf->b_p_iminsert = B_IMODE_NONE;
3134 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3135# endif
3136 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003137 }
3138
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003139#endif
3140
Bram Moolenaar44826212019-08-22 21:23:20 +02003141#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3142 else if (((int *)varp == &curwin->w_p_nu
3143 || (int *)varp == &curwin->w_p_rnu)
3144 && gui.in_use
3145 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3146 && curbuf->b_signlist != NULL)
3147 {
3148 // If the 'number' or 'relativenumber' options are modified and
3149 // 'signcolumn' is set to 'number', then clear the screen for a full
3150 // refresh. Otherwise the sign icons are not displayed properly in the
3151 // number column. If the 'number' option is set and only the
3152 // 'relativenumber' option is toggled, then don't refresh the screen
3153 // (optimization).
3154 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3155 redraw_all_later(CLEAR);
3156 }
3157#endif
3158
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003159#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003161 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003162 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003163# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003164 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003165 if (
3166# ifdef VIMDLL
3167 !gui.in_use && !gui.starting &&
3168# endif
3169 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003170 {
3171 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003172 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003173 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003174 if (is_term_win32())
3175 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003176# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003177# ifdef FEAT_GUI
3178 if (!gui.in_use && !gui.starting)
3179# endif
3180 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003181# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003183 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003184 {
3185 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003186 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003187 init_highlight(TRUE, FALSE);
3188 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003189# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003190 }
3191#endif
3192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 /*
3194 * End of handling side effects for bool options.
3195 */
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 options[opt_idx].flags |= P_WAS_SET;
3200
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003201#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003202 apply_optionset_autocmd(opt_idx, opt_flags,
3203 (long)(old_value ? TRUE : FALSE),
3204 (long)(old_global_value ? TRUE : FALSE),
3205 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003206#endif
3207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003208 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003209 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003210 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003211 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003212
3213 if ((opt_flags & OPT_NO_REDRAW) == 0)
3214 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216 return NULL;
3217}
3218
3219/*
3220 * Set the value of a number option, and take care of side effects.
3221 * Returns NULL for success, or an error message for an error.
3222 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003223 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003224set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 int opt_idx, // index in options[] table
3226 char_u *varp, // pointer to the option variable
3227 long value, // new value
3228 char *errbuf, // buffer for error messages
3229 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003230 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3231 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003233 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003235#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003236 long old_global_value = 0; // only used when setting a local and
3237 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003238#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003239 long old_Rows = Rows; // remember old Rows
3240 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 long *pp = (long *)varp;
3242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003243 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003244 if ((secure
3245#ifdef HAVE_SANDBOX
3246 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003248 ) && (options[opt_idx].flags & P_SECURE))
3249 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003251#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003252 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003253 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003254 // value (since there is only one value).
3255 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003256 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3257 OPT_GLOBAL);
3258#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 *pp = value;
3261#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003262 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003263 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003265#ifdef FEAT_GUI
3266 need_mouse_correct = TRUE;
3267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar14f24742012-08-08 18:01:05 +02003269 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003272#ifdef FEAT_VARTABS
3273 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3274 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3275 ? tabstop_first(curbuf->b_p_vts_array)
3276 : curbuf->b_p_ts;
3277#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281
3282 /*
3283 * Number options that need some action when changed
3284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (pp == &p_wh || pp == &p_hh)
3286 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003287 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (p_wh < 1)
3289 {
3290 errmsg = e_positive;
3291 p_wh = 1;
3292 }
3293 if (p_wmh > p_wh)
3294 {
3295 errmsg = e_winheight;
3296 p_wh = p_wmh;
3297 }
3298 if (p_hh < 0)
3299 {
3300 errmsg = e_positive;
3301 p_hh = 0;
3302 }
3303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003305 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
3307 if (pp == &p_wh && curwin->w_height < p_wh)
3308 win_setheight((int)p_wh);
3309 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3310 win_setheight((int)p_hh);
3311 }
3312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else if (pp == &p_wmh)
3314 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003315 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (p_wmh < 0)
3317 {
3318 errmsg = e_positive;
3319 p_wmh = 0;
3320 }
3321 if (p_wmh > p_wh)
3322 {
3323 errmsg = e_winheight;
3324 p_wmh = p_wh;
3325 }
3326 win_setminheight();
3327 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003328 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003330 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (p_wiw < 1)
3332 {
3333 errmsg = e_positive;
3334 p_wiw = 1;
3335 }
3336 if (p_wmw > p_wiw)
3337 {
3338 errmsg = e_winwidth;
3339 p_wiw = p_wmw;
3340 }
3341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003342 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003343 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 win_setwidth((int)p_wiw);
3345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 else if (pp == &p_wmw)
3347 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003348 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (p_wmw < 0)
3350 {
3351 errmsg = e_positive;
3352 p_wmw = 0;
3353 }
3354 if (p_wmw > p_wiw)
3355 {
3356 errmsg = e_winwidth;
3357 p_wmw = p_wiw;
3358 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003359 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003362 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else if (pp == &p_ls)
3364 {
3365 last_status(FALSE);
3366 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003368 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003369 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003370 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003371 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374#ifdef FEAT_GUI
3375 else if (pp == &p_linespace)
3376 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003377 // Recompute gui.char_height and resize the Vim window to keep the
3378 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003379 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003380 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382#endif
3383
3384#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003385 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 else if (pp == &curwin->w_p_fdl)
3387 {
3388 if (curwin->w_p_fdl < 0)
3389 curwin->w_p_fdl = 0;
3390 newFoldLevel();
3391 }
3392
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003393 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 else if (pp == &curwin->w_p_fml)
3395 {
3396 foldUpdateAll(curwin);
3397 }
3398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003399 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 else if (pp == &curwin->w_p_fdn)
3401 {
3402 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3403 foldUpdateAll(curwin);
3404 }
3405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003406 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 else if (pp == &curwin->w_p_fdc)
3408 {
3409 if (curwin->w_p_fdc < 0)
3410 {
3411 errmsg = e_positive;
3412 curwin->w_p_fdc = 0;
3413 }
3414 else if (curwin->w_p_fdc > 12)
3415 {
3416 errmsg = e_invarg;
3417 curwin->w_p_fdc = 12;
3418 }
3419 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003420#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003422#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3425 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003426# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (foldmethodIsIndent(curwin))
3428 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003429# endif
3430# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003431 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3432 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003433 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3434 parse_cino(curbuf);
3435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003440 else if (pp == &p_mco)
3441 {
3442 if (p_mco > MAX_MCO)
3443 p_mco = MAX_MCO;
3444 else if (p_mco < 0)
3445 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003446 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003447 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 else if (pp == &curbuf->b_p_iminsert)
3450 {
3451 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3452 {
3453 errmsg = e_invarg;
3454 curbuf->b_p_iminsert = B_IMODE_NONE;
3455 }
3456 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003459#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 status_redraw_curbuf();
3462#endif
3463 }
3464
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003465#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003466 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003467 else if (pp == &p_imst)
3468 {
3469 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3470 errmsg = e_invarg;
3471 }
3472#endif
3473
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003474 else if (pp == &p_window)
3475 {
3476 if (p_window < 1)
3477 p_window = 1;
3478 else if (p_window >= Rows)
3479 p_window = Rows - 1;
3480 }
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else if (pp == &curbuf->b_p_imsearch)
3483 {
3484 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3485 {
3486 errmsg = e_invarg;
3487 curbuf->b_p_imsearch = B_IMODE_NONE;
3488 }
3489 p_imsearch = curbuf->b_p_imsearch;
3490 }
3491
3492#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003493 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 else if (pp == &p_titlelen)
3495 {
3496 if (p_titlelen < 0)
3497 {
3498 errmsg = e_positive;
3499 p_titlelen = 85;
3500 }
3501 if (starting != NO_SCREEN && old_value != p_titlelen)
3502 need_maketitle = TRUE;
3503 }
3504#endif
3505
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003506 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 else if (pp == &p_ch)
3508 {
3509 if (p_ch < 1)
3510 {
3511 errmsg = e_positive;
3512 p_ch = 1;
3513 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003514 if (p_ch > Rows - min_rows() + 1)
3515 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 // Only compute the new window layout when startup has been
3518 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (p_ch != old_value && full_screen
3520#ifdef FEAT_GUI
3521 && !gui.starting
3522#endif
3523 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003524 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003527 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 else if (pp == &p_uc)
3529 {
3530 if (p_uc < 0)
3531 {
3532 errmsg = e_positive;
3533 p_uc = 100;
3534 }
3535 if (p_uc && !old_value)
3536 ml_open_files();
3537 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003538#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003539 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003540 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003541 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003542 {
3543 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003544 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003545 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003546 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003547 {
3548 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003549 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003550 }
3551 }
3552#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003553#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003554 else if (pp == &p_mzq)
3555 mzvim_reset_timer();
3556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003558#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003559 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003560 else if (pp == &p_pyx)
3561 {
3562 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3563 errmsg = e_invarg;
3564 }
3565#endif
3566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 else if (pp == &p_ul)
3569 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003570 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003572 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 p_ul = value;
3574 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003575 else if (pp == &curbuf->b_p_ul)
3576 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003577 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003578 curbuf->b_p_ul = old_value;
3579 u_sync(TRUE);
3580 curbuf->b_p_ul = value;
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003583#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003584 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003585 else if (pp == &curwin->w_p_nuw)
3586 {
3587 if (curwin->w_p_nuw < 1)
3588 {
3589 errmsg = e_positive;
3590 curwin->w_p_nuw = 1;
3591 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003592 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003593 {
3594 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003595 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003596 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003598 }
3599#endif
3600
Bram Moolenaar1a384422010-07-14 19:53:30 +02003601 else if (pp == &curbuf->b_p_tw)
3602 {
3603 if (curbuf->b_p_tw < 0)
3604 {
3605 errmsg = e_positive;
3606 curbuf->b_p_tw = 0;
3607 }
3608#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003609 {
3610 win_T *wp;
3611 tabpage_T *tp;
3612
3613 FOR_ALL_TAB_WINDOWS(tp, wp)
3614 check_colorcolumn(wp);
3615 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003616#endif
3617 }
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /*
3620 * Check the bounds for numeric options here
3621 */
3622 if (Rows < min_rows() && full_screen)
3623 {
3624 if (errbuf != NULL)
3625 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003626 vim_snprintf((char *)errbuf, errbuflen,
3627 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 errmsg = errbuf;
3629 }
3630 Rows = min_rows();
3631 }
3632 if (Columns < MIN_COLUMNS && full_screen)
3633 {
3634 if (errbuf != NULL)
3635 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003636 vim_snprintf((char *)errbuf, errbuflen,
3637 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 errmsg = errbuf;
3639 }
3640 Columns = MIN_COLUMNS;
3641 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003642 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /*
3645 * If the screen (shell) height has been changed, assume it is the
3646 * physical screenheight.
3647 */
3648 if (old_Rows != Rows || old_Columns != Columns)
3649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003650 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (updating_screen)
3652 *pp = old_value;
3653 else if (full_screen
3654#ifdef FEAT_GUI
3655 && !gui.starting
3656#endif
3657 )
3658 set_shellsize((int)Columns, (int)Rows, TRUE);
3659 else
3660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003661 // Postpone the resizing; check the size and cmdline position for
3662 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 check_shellsize();
3664 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3665 cmdline_row = Rows - p_ch;
3666 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003667 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003668 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (curbuf->b_p_ts <= 0)
3672 {
3673 errmsg = e_positive;
3674 curbuf->b_p_ts = 8;
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 if (p_tm < 0)
3677 {
3678 errmsg = e_positive;
3679 p_tm = 0;
3680 }
3681 if ((curwin->w_p_scr <= 0
3682 || (curwin->w_p_scr > curwin->w_height
3683 && curwin->w_height > 0))
3684 && full_screen)
3685 {
3686 if (pp == &(curwin->w_p_scr))
3687 {
3688 if (curwin->w_p_scr != 0)
3689 errmsg = e_scroll;
3690 win_comp_scroll(curwin);
3691 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003692 // If 'scroll' became invalid because of a side effect silently adjust
3693 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 else if (curwin->w_p_scr <= 0)
3695 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003696 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 curwin->w_p_scr = curwin->w_height;
3698 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003699 if (p_hi < 0)
3700 {
3701 errmsg = e_positive;
3702 p_hi = 0;
3703 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003704 else if (p_hi > 10000)
3705 {
3706 errmsg = e_invarg;
3707 p_hi = 10000;
3708 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003709 if (p_re < 0 || p_re > 2)
3710 {
3711 errmsg = e_invarg;
3712 p_re = 0;
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (p_report < 0)
3715 {
3716 errmsg = e_positive;
3717 p_report = 1;
3718 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003719 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003721 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 p_sj = Rows / 2;
3723 else
3724 {
3725 errmsg = e_scroll;
3726 p_sj = 1;
3727 }
3728 }
3729 if (p_so < 0 && full_screen)
3730 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003731 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 p_so = 0;
3733 }
3734 if (p_siso < 0 && full_screen)
3735 {
3736 errmsg = e_positive;
3737 p_siso = 0;
3738 }
3739#ifdef FEAT_CMDWIN
3740 if (p_cwh < 1)
3741 {
3742 errmsg = e_positive;
3743 p_cwh = 1;
3744 }
3745#endif
3746 if (p_ut < 0)
3747 {
3748 errmsg = e_positive;
3749 p_ut = 2000;
3750 }
3751 if (p_ss < 0)
3752 {
3753 errmsg = e_positive;
3754 p_ss = 0;
3755 }
3756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003757 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3759 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3760
3761 options[opt_idx].flags |= P_WAS_SET;
3762
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003763#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003764 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3765 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003766#endif
3767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003768 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003769 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003770 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003771 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003772 if ((opt_flags & OPT_NO_REDRAW) == 0)
3773 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
3775 return errmsg;
3776}
3777
3778/*
3779 * Called after an option changed: check if something needs to be redrawn.
3780 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003782check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003784 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003785 int doclear = (flags & P_RCLR) == P_RCLR;
3786 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003788 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3792 changed_window_setting();
3793 if (flags & P_RBUF)
3794 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003795 if (flags & P_RWINONLY)
3796 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003797 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 redraw_all_later(CLEAR);
3799 else if (all)
3800 redraw_all_later(NOT_VALID);
3801}
3802
3803/*
3804 * Find index for option 'arg'.
3805 * Return -1 if not found.
3806 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003807 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003808findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
3810 int opt_idx;
3811 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003812 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 int is_term_opt;
3814
3815 /*
3816 * For first call: Initialize the quick-access table.
3817 * It contains the index for the first option that starts with a certain
3818 * letter. There are 26 letters, plus the first "t_" option.
3819 */
3820 if (quick_tab[1] == 0)
3821 {
3822 p = options[0].fullname;
3823 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3824 {
3825 if (s[0] != p[0])
3826 {
3827 if (s[0] == 't' && s[1] == '_')
3828 quick_tab[26] = opt_idx;
3829 else
3830 quick_tab[CharOrdLow(s[0])] = opt_idx;
3831 }
3832 p = s;
3833 }
3834 }
3835
3836 /*
3837 * Check for name starting with an illegal character.
3838 */
3839#ifdef EBCDIC
3840 if (!islower(arg[0]))
3841#else
3842 if (arg[0] < 'a' || arg[0] > 'z')
3843#endif
3844 return -1;
3845
3846 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3847 if (is_term_opt)
3848 opt_idx = quick_tab[26];
3849 else
3850 opt_idx = quick_tab[CharOrdLow(arg[0])];
3851 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003853 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 break;
3855 }
3856 if (s == NULL && !is_term_opt)
3857 {
3858 opt_idx = quick_tab[CharOrdLow(arg[0])];
3859 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3860 {
3861 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003862 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 break;
3864 s = NULL;
3865 }
3866 }
3867 if (s == NULL)
3868 opt_idx = -1;
3869 return opt_idx;
3870}
3871
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003872#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873/*
3874 * Get the value for an option.
3875 *
3876 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003877 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003878 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003879 * String option: gov_string, *stringval gets allocated string.
3880 * Hidden Number option: gov_hidden_number.
3881 * Hidden Toggle option: gov_hidden_bool.
3882 * Hidden String option: gov_hidden_string.
3883 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003885 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003886get_option_value(
3887 char_u *name,
3888 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003889 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003890 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
3892 int opt_idx;
3893 char_u *varp;
3894
3895 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003896 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003897 {
3898 int key;
3899
3900 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003901 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003902 {
3903 char_u key_name[2];
3904 char_u *p;
3905
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003906 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003907 if (key < 0)
3908 {
3909 key_name[0] = KEY2TERMCAP0(key);
3910 key_name[1] = KEY2TERMCAP1(key);
3911 }
3912 else
3913 {
3914 key_name[0] = KS_KEY;
3915 key_name[1] = (key & 0xff);
3916 }
3917 p = find_termcode(key_name);
3918 if (p != NULL)
3919 {
3920 if (stringval != NULL)
3921 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003922 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003923 }
3924 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003925 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3929
3930 if (options[opt_idx].flags & P_STRING)
3931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003932 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003933 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (stringval != NULL)
3935 {
3936#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003938 if ((char_u **)varp == &curbuf->b_p_key
3939 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 *stringval = vim_strsave((char_u *)"*****");
3941 else
3942#endif
3943 *stringval = vim_strsave(*(char_u **)(varp));
3944 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003945 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003948 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003949 return (options[opt_idx].flags & P_NUM)
3950 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (options[opt_idx].flags & P_NUM)
3952 *numval = *(long *)varp;
3953 else
3954 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003955 // Special case: 'modified' is b_changed, but we also want to consider
3956 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 if ((int *)varp == &curbuf->b_changed)
3958 *numval = curbufIsChanged();
3959 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003960 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003962 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963}
3964#endif
3965
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003966#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003967/*
3968 * Returns the option attributes and its value. Unlike the above function it
3969 * will return either global value or local value of the option depending on
3970 * what was requested, but it will never return global value if it was
3971 * requested to return local one and vice versa. Neither it will return
3972 * buffer-local value if it was requested to return window-local one.
3973 *
3974 * Pretends that option is absent if it is not present in the requested scope
3975 * (i.e. has no global, window-local or buffer-local value depending on
3976 * opt_type). Uses
3977 *
3978 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003979 * 0 hidden or unknown option, also option that does not have requested
3980 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003981 * see SOPT_* in vim.h for other flags
3982 *
3983 * Possible opt_type values: see SREQ_* in vim.h
3984 */
3985 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003986get_option_value_strict(
3987 char_u *name,
3988 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003989 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003990 int opt_type,
3991 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003992{
3993 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003994 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003995 struct vimoption *p;
3996 int r = 0;
3997
3998 opt_idx = findoption(name);
3999 if (opt_idx < 0)
4000 return 0;
4001
4002 p = &(options[opt_idx]);
4003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004004 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004005 if (p->var == NULL)
4006 return 0;
4007
4008 if (p->flags & P_BOOL)
4009 r |= SOPT_BOOL;
4010 else if (p->flags & P_NUM)
4011 r |= SOPT_NUM;
4012 else if (p->flags & P_STRING)
4013 r |= SOPT_STRING;
4014
4015 if (p->indir == PV_NONE)
4016 {
4017 if (opt_type == SREQ_GLOBAL)
4018 r |= SOPT_GLOBAL;
4019 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004021 }
4022 else
4023 {
4024 if (p->indir & PV_BOTH)
4025 r |= SOPT_GLOBAL;
4026 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004028
4029 if (p->indir & PV_WIN)
4030 {
4031 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004033 else
4034 r |= SOPT_WIN;
4035 }
4036 else if (p->indir & PV_BUF)
4037 {
4038 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004039 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004040 else
4041 r |= SOPT_BUF;
4042 }
4043 }
4044
4045 if (stringval == NULL)
4046 return r;
4047
4048 if (opt_type == SREQ_GLOBAL)
4049 varp = p->var;
4050 else
4051 {
4052 if (opt_type == SREQ_BUF)
4053 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004054 // Special case: 'modified' is b_changed, but we also want to
4055 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004056 if (p->indir == PV_MOD)
4057 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004058 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004059 varp = NULL;
4060 }
4061#ifdef FEAT_CRYPT
4062 else if (p->indir == PV_KEY)
4063 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004064 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004065 *stringval = NULL;
4066 varp = NULL;
4067 }
4068#endif
4069 else
4070 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004071 buf_T *save_curbuf = curbuf;
4072
4073 // only getting a pointer, no need to use aucmd_prepbuf()
4074 curbuf = (buf_T *)from;
4075 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004076 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004077 curbuf = save_curbuf;
4078 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004079 }
4080 }
4081 else if (opt_type == SREQ_WIN)
4082 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004083 win_T *save_curwin = curwin;
4084
4085 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004086 curbuf = curwin->w_buffer;
4087 varp = get_varp(p);
4088 curwin = save_curwin;
4089 curbuf = curwin->w_buffer;
4090 }
4091 if (varp == p->var)
4092 return (r | SOPT_UNSET);
4093 }
4094
4095 if (varp != NULL)
4096 {
4097 if (p->flags & P_STRING)
4098 *stringval = vim_strsave(*(char_u **)(varp));
4099 else if (p->flags & P_NUM)
4100 *numval = *(long *) varp;
4101 else
4102 *numval = *(int *)varp;
4103 }
4104
4105 return r;
4106}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004107
4108/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004109 * Iterate over options. First argument is a pointer to a pointer to a
4110 * structure inside options[] array, second is option type like in the above
4111 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004112 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004113 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004114 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004115 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004116 * first argument to NULL.
4117 *
4118 * Returns full option name for current option on each call.
4119 */
4120 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004121option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004122{
4123 struct vimoption *ret = NULL;
4124 do
4125 {
4126 if (*option == NULL)
4127 *option = (void *) options;
4128 else if (((struct vimoption *) (*option))->fullname == NULL)
4129 {
4130 *option = NULL;
4131 return NULL;
4132 }
4133 else
4134 *option = (void *) (((struct vimoption *) (*option)) + 1);
4135
4136 ret = ((struct vimoption *) (*option));
4137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004138 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004139 if (ret->var == NULL)
4140 {
4141 ret = NULL;
4142 continue;
4143 }
4144
4145 switch (opt_type)
4146 {
4147 case SREQ_GLOBAL:
4148 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4149 ret = NULL;
4150 break;
4151 case SREQ_BUF:
4152 if (!(ret->indir & PV_BUF))
4153 ret = NULL;
4154 break;
4155 case SREQ_WIN:
4156 if (!(ret->indir & PV_WIN))
4157 ret = NULL;
4158 break;
4159 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004160 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004161 return NULL;
4162 }
4163 }
4164 while (ret == NULL);
4165
4166 return (char_u *)ret->fullname;
4167}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004168#endif
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004171 * Return the flags for the option at 'opt_idx'.
4172 */
4173 long_u
4174get_option_flags(int opt_idx)
4175{
4176 return options[opt_idx].flags;
4177}
4178
4179/*
4180 * Set a flag for the option at 'opt_idx'.
4181 */
4182 void
4183set_option_flag(int opt_idx, long_u flag)
4184{
4185 options[opt_idx].flags |= flag;
4186}
4187
4188/*
4189 * Clear a flag for the option at 'opt_idx'.
4190 */
4191 void
4192clear_option_flag(int opt_idx, long_u flag)
4193{
4194 options[opt_idx].flags &= ~flag;
4195}
4196
4197/*
4198 * Returns TRUE if the option at 'opt_idx' is a global option
4199 */
4200 int
4201is_global_option(int opt_idx)
4202{
4203 return options[opt_idx].indir == PV_NONE;
4204}
4205
4206/*
4207 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4208 * local value.
4209 */
4210 int
4211is_global_local_option(int opt_idx)
4212{
4213 return options[opt_idx].indir & PV_BOTH;
4214}
4215
4216/*
4217 * Returns TRUE if the option at 'opt_idx' is a window-local option
4218 */
4219 int
4220is_window_local_option(int opt_idx)
4221{
4222 return options[opt_idx].var == VAR_WIN;
4223}
4224
4225/*
4226 * Returns TRUE if the option at 'opt_idx' is a hidden option
4227 */
4228 int
4229is_hidden_option(int opt_idx)
4230{
4231 return options[opt_idx].var == NULL;
4232}
4233
4234#if defined(FEAT_CRYPT) || defined(PROTO)
4235/*
4236 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4237 */
4238 int
4239is_crypt_key_option(int opt_idx)
4240{
4241 return options[opt_idx].indir == PV_KEY;
4242}
4243#endif
4244
4245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 * Set the value of option "name".
4247 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004248 *
4249 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004252set_option_value(
4253 char_u *name,
4254 long number,
4255 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004256 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
4258 int opt_idx;
4259 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004260 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004263 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004264 {
4265 int key;
4266
4267 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004268 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004269 {
4270 char_u key_name[2];
4271
4272 if (key < 0)
4273 {
4274 key_name[0] = KEY2TERMCAP0(key);
4275 key_name[1] = KEY2TERMCAP1(key);
4276 }
4277 else
4278 {
4279 key_name[0] = KS_KEY;
4280 key_name[1] = (key & 0xff);
4281 }
4282 add_termcode(key_name, string, FALSE);
4283 if (full_screen)
4284 ttest(FALSE);
4285 redraw_all_later(CLEAR);
4286 return NULL;
4287 }
4288
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004289 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 else
4292 {
4293 flags = options[opt_idx].flags;
4294#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004295 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004298 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004299 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004302 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004303 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 else
4305 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004306 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004307 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004309 if (number == 0 && string != NULL)
4310 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004311 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004313 // Either we are given a string or we are setting option
4314 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004315 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004316 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004317 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004318 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004319 // There's another character after zeros or the string
4320 // is empty. In both cases, we are trying to set a
4321 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004322 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004323 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004324 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004325
4326 }
4327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004329 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004330 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004332 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004333 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 }
4336 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004337 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338}
4339
4340/*
4341 * Get the terminal code for a terminal option.
4342 * Returns NULL when not found.
4343 */
4344 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004345get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
4347 int opt_idx;
4348 char_u *varp;
4349
4350 if (tname[0] != 't' || tname[1] != '_' ||
4351 tname[2] == NUL || tname[3] == NUL)
4352 return NULL;
4353 if ((opt_idx = findoption(tname)) >= 0)
4354 {
4355 varp = get_varp(&(options[opt_idx]));
4356 if (varp != NULL)
4357 varp = *(char_u **)(varp);
4358 return varp;
4359 }
4360 return find_termcode(tname + 2);
4361}
4362
4363 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004364get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
4366 int i;
4367
4368 i = findoption((char_u *)"hl");
4369 if (i >= 0)
4370 return options[i].def_val[VI_DEFAULT];
4371 return (char_u *)NULL;
4372}
4373
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004374 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004375get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004376{
4377 int i;
4378
4379 i = findoption((char_u *)"enc");
4380 if (i >= 0)
4381 return options[i].def_val[VI_DEFAULT];
4382 return (char_u *)NULL;
4383}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385/*
4386 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004387 * When "has_lt" is true there is a '<' before "*arg_arg".
4388 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 */
4390 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004391find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004393 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004395 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 /*
4398 * Don't use get_special_key_code() for t_xx, we don't want it to call
4399 * add_termcap_entry().
4400 */
4401 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4402 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004403 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004405 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004407 key = find_special_key(&arg, &modifiers,
4408 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004409 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 key = 0;
4411 }
4412 return key;
4413}
4414
4415/*
4416 * if 'all' == 0: show changed options
4417 * if 'all' == 1: show all normal options
4418 * if 'all' == 2: show all terminal options
4419 */
4420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004421showoptions(
4422 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004423 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 struct vimoption *p;
4426 int col;
4427 int isterm;
4428 char_u *varp;
4429 struct vimoption **items;
4430 int item_count;
4431 int run;
4432 int row, rows;
4433 int cols;
4434 int i;
4435 int len;
4436
4437#define INC 20
4438#define GAP 3
4439
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004440 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (items == NULL)
4442 return;
4443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004444 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004446 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004448 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004450 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004452 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004455 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 * 1. display the short items
4457 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004458 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 */
4460 for (run = 1; run <= 2 && !got_int; ++run)
4461 {
4462 /*
4463 * collect the items in items[]
4464 */
4465 item_count = 0;
4466 for (p = &options[0]; p->fullname != NULL; p++)
4467 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004468 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004469 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004470 continue;
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 varp = NULL;
4473 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004474 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
4476 if (p->indir != PV_NONE && !isterm)
4477 varp = get_varp_scope(p, opt_flags);
4478 }
4479 else
4480 varp = get_varp(p);
4481 if (varp != NULL
4482 && ((all == 2 && isterm)
4483 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004484 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004486 if (opt_flags & OPT_ONECOLUMN)
4487 len = Columns;
4488 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004489 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 else
4491 {
4492 option_value2string(p, opt_flags);
4493 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4494 }
4495 if ((len <= INC - GAP && run == 1) ||
4496 (len > INC - GAP && run == 2))
4497 items[item_count++] = p;
4498 }
4499 }
4500
4501 /*
4502 * display the items
4503 */
4504 if (run == 1)
4505 {
4506 cols = (Columns + GAP - 3) / INC;
4507 if (cols == 0)
4508 cols = 1;
4509 rows = (item_count + cols - 1) / cols;
4510 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004511 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 rows = item_count;
4513 for (row = 0; row < rows && !got_int; ++row)
4514 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004515 msg_putchar('\n'); // go to next line
4516 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 break;
4518 col = 0;
4519 for (i = row; i < item_count; i += rows)
4520 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004521 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 showoneopt(items[i], opt_flags);
4523 col += INC;
4524 }
4525 out_flush();
4526 ui_breakcheck();
4527 }
4528 }
4529 vim_free(items);
4530}
4531
4532/*
4533 * Return TRUE if option "p" has its default value.
4534 */
4535 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004536optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
4538 int dvi;
4539
4540 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004541 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004542 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004544 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004546 // the cast to long is required for Manx C, long_i is
4547 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004548 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004549 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4551}
4552
4553/*
4554 * showoneopt: show the value of one option
4555 * must not be called with a hidden option!
4556 */
4557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004558showoneopt(
4559 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004560 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004562 char_u *varp;
4563 int save_silent = silent_mode;
4564
4565 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 varp = get_varp_scope(p, opt_flags);
4569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004570 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4572 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004573 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004575 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004577 msg_puts(" ");
4578 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (!(p->flags & P_BOOL))
4580 {
4581 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004582 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 option_value2string(p, opt_flags);
4584 msg_outtrans(NameBuff);
4585 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004586
4587 silent_mode = save_silent;
4588 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589}
4590
4591/*
4592 * Write modified options as ":set" commands to a file.
4593 *
4594 * There are three values for "opt_flags":
4595 * OPT_GLOBAL: Write global option values and fresh values of
4596 * buffer-local options (used for start of a session
4597 * file).
4598 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4599 * curwin (used for a vimrc file).
4600 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4601 * and local values for window-local options of
4602 * curwin. Local values are also written when at the
4603 * default value, because a modeline or autocommand
4604 * may have set them when doing ":edit file" and the
4605 * user has set them back at the default or fresh
4606 * value.
4607 * When "local_only" is TRUE, don't write fresh
4608 * values, only local values (for ":mkview").
4609 * (fresh value = value used for a new buffer or window for a local option).
4610 *
4611 * Return FAIL on error, OK otherwise.
4612 */
4613 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004614makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
4616 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 char_u *varp; // currently used value
4618 char_u *varp_fresh; // local value
4619 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 char *cmd;
4621 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004622 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624 /*
4625 * The options that don't have a default (terminal name, columns, lines)
4626 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004627 * Do the loop over "options[]" twice: once for options with the
4628 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004630 for (pri = 1; pri >= 0; --pri)
4631 {
4632 for (p = &options[0]; !istermoption(p); p++)
4633 if (!(p->flags & P_NO_MKRC)
4634 && !istermoption(p)
4635 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4639 continue;
4640
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004641 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4642 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4644 continue;
4645
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004646 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004648 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 continue;
4650
Bram Moolenaard23b7142021-04-17 21:04:34 +02004651 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4652 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004653 continue;
4654
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 round = 2;
4656 if (p->indir != PV_NONE)
4657 {
4658 if (p->var == VAR_WIN)
4659 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004660 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (!(opt_flags & OPT_LOCAL))
4662 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004663 // When fresh value of window-local option is not at the
4664 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4666 {
4667 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004668 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 round = 1;
4671 varp_local = varp;
4672 varp = varp_fresh;
4673 }
4674 }
4675 }
4676 }
4677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // Round 1: fresh value for window-local options.
4679 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 for ( ; round <= 2; varp = varp_local, ++round)
4681 {
4682 if (round == 1 || (opt_flags & OPT_GLOBAL))
4683 cmd = "set";
4684 else
4685 cmd = "setlocal";
4686
4687 if (p->flags & P_BOOL)
4688 {
4689 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4690 return FAIL;
4691 }
4692 else if (p->flags & P_NUM)
4693 {
4694 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4695 return FAIL;
4696 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004697 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004699 int do_endif = FALSE;
4700
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004701 // Don't set 'syntax' and 'filetype' again if the value is
4702 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004703 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004704#if defined(FEAT_SYN_HL)
4705 p->indir == PV_SYN ||
4706#endif
4707 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4710 *(char_u **)(varp)) < 0
4711 || put_eol(fd) < 0)
4712 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004713 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004716 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004718 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
4720 if (put_line(fd, "endif") == FAIL)
4721 return FAIL;
4722 }
4723 }
4724 }
4725 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 return OK;
4728}
4729
4730#if defined(FEAT_FOLDING) || defined(PROTO)
4731/*
4732 * Generate set commands for the local fold options only. Used when
4733 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4734 */
4735 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004736makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004738 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004740 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 == FAIL
4742# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004743 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004745 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 == FAIL
4747 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4748 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4749 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4750 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4751 )
4752 return FAIL;
4753
4754 return OK;
4755}
4756#endif
4757
4758 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004759put_setstring(
4760 FILE *fd,
4761 char *cmd,
4762 char *name,
4763 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004764 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004767 char_u *buf = NULL;
4768 char_u *part = NULL;
4769 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
4771 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4772 return FAIL;
4773 if (*valuep != NULL)
4774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004775 // Output 'pastetoggle' as key names. For other
4776 // options some characters have to be escaped with
4777 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (valuep == &p_pt)
4779 {
4780 s = *valuep;
4781 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004782 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 return FAIL;
4784 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004785 // expand the option value, replace $HOME by ~
4786 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004788 int size = (int)STRLEN(*valuep) + 1;
4789
4790 // replace home directory in the whole option value into "buf"
4791 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004792 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004793 goto fail;
4794 home_replace(NULL, *valuep, buf, size, FALSE);
4795
4796 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004797 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004798 // can be expanded when read back.
4799 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4800 && vim_strchr(*valuep, ',') != NULL)
4801 {
4802 part = alloc(size);
4803 if (part == NULL)
4804 goto fail;
4805
4806 // write line break to clear the option, e.g. ':set rtp='
4807 if (put_eol(fd) == FAIL)
4808 goto fail;
4809
4810 p = buf;
4811 while (*p != NUL)
4812 {
4813 // for each comma separated option part, append value to
4814 // the option, :set rtp+=value
4815 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4816 goto fail;
4817 (void)copy_option_part(&p, part, size, ",");
4818 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4819 goto fail;
4820 }
4821 vim_free(buf);
4822 vim_free(part);
4823 return OK;
4824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004826 {
4827 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004829 }
4830 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832 else if (put_escstr(fd, *valuep, 2) == FAIL)
4833 return FAIL;
4834 }
4835 if (put_eol(fd) < 0)
4836 return FAIL;
4837 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004838fail:
4839 vim_free(buf);
4840 vim_free(part);
4841 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842}
4843
4844 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004845put_setnum(
4846 FILE *fd,
4847 char *cmd,
4848 char *name,
4849 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
4851 long wc;
4852
4853 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4854 return FAIL;
4855 if (wc_use_keyname((char_u *)valuep, &wc))
4856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004857 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4859 return FAIL;
4860 }
4861 else if (fprintf(fd, "%ld", *valuep) < 0)
4862 return FAIL;
4863 if (put_eol(fd) < 0)
4864 return FAIL;
4865 return OK;
4866}
4867
4868 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004869put_setbool(
4870 FILE *fd,
4871 char *cmd,
4872 char *name,
4873 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004875 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004876 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4878 || put_eol(fd) < 0)
4879 return FAIL;
4880 return OK;
4881}
4882
4883/*
4884 * Clear all the terminal options.
4885 * If the option has been allocated, free the memory.
4886 * Terminal options are never hidden or indirect.
4887 */
4888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004889clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /*
4892 * Reset a few things before clearing the old options. This may cause
4893 * outputting a few things that the terminal doesn't understand, but the
4894 * screen will be cleared later, so this is OK.
4895 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004896 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004898 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#endif
4900#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004901 // When starting the GUI close the display opened for the clipboard.
4902 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (gui.starting)
4904 clear_xterm_clip();
4905#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004906 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004908 free_termoptions();
4909}
4910
4911 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004912free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004913{
4914 struct vimoption *p;
4915
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004916 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 if (istermoption(p))
4918 {
4919 if (p->flags & P_ALLOCED)
4920 free_string_option(*(char_u **)(p->var));
4921 if (p->flags & P_DEF_ALLOCED)
4922 free_string_option(p->def_val[VI_DEFAULT]);
4923 *(char_u **)(p->var) = empty_option;
4924 p->def_val[VI_DEFAULT] = empty_option;
4925 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004926#ifdef FEAT_EVAL
4927 // remember where the option was cleared
4928 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931 clear_termcodes();
4932}
4933
4934/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004935 * Free the string for one term option, if it was allocated.
4936 * Set the string to empty_option and clear allocated flag.
4937 * "var" points to the option value.
4938 */
4939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004940free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004941{
4942 struct vimoption *p;
4943
4944 for (p = &options[0]; p->fullname != NULL; p++)
4945 if (p->var == var)
4946 {
4947 if (p->flags & P_ALLOCED)
4948 free_string_option(*(char_u **)(p->var));
4949 *(char_u **)(p->var) = empty_option;
4950 p->flags &= ~P_ALLOCED;
4951 break;
4952 }
4953}
4954
4955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 * Set the terminal option defaults to the current value.
4957 * Used after setting the terminal name.
4958 */
4959 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004960set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 struct vimoption *p;
4963
4964 for (p = &options[0]; p->fullname != NULL; p++)
4965 {
4966 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4967 {
4968 if (p->flags & P_DEF_ALLOCED)
4969 {
4970 free_string_option(p->def_val[VI_DEFAULT]);
4971 p->flags &= ~P_DEF_ALLOCED;
4972 }
4973 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4974 if (p->flags & P_ALLOCED)
4975 {
4976 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004977 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 }
4980 }
4981}
4982
4983/*
4984 * return TRUE if 'p' starts with 't_'
4985 */
4986 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004987istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
4989 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4990}
4991
Bram Moolenaardac13472019-09-16 21:06:21 +02004992/*
4993 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4994 */
4995 int
4996istermoption_idx(int opt_idx)
4997{
4998 return istermoption(&options[opt_idx]);
4999}
5000
Bram Moolenaar113e1072019-01-20 15:30:40 +01005001#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005003 * Unset local option value, similar to ":set opt<".
5004 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005006unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005007{
5008 struct vimoption *p;
5009 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005010 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005011
5012 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005013 if (opt_idx < 0)
5014 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005015 p = &(options[opt_idx]);
5016
5017 switch ((int)p->indir)
5018 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005019 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005020 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005021 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005022 break;
5023 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005024 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005025 break;
5026 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005027 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005028 break;
5029 case PV_AR:
5030 buf->b_p_ar = -1;
5031 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005032 case PV_BKC:
5033 clear_string_option(&buf->b_p_bkc);
5034 buf->b_bkc_flags = 0;
5035 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005036 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005037 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005038 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005039 case PV_TC:
5040 clear_string_option(&buf->b_p_tc);
5041 buf->b_tc_flags = 0;
5042 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005043 case PV_SISO:
5044 curwin->w_p_siso = -1;
5045 break;
5046 case PV_SO:
5047 curwin->w_p_so = -1;
5048 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005049#ifdef FEAT_FIND_ID
5050 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005051 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052 break;
5053 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005054 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005055 break;
5056#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005057 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005058 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005059 break;
5060 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005061 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005062 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005063 case PV_FP:
5064 clear_string_option(&buf->b_p_fp);
5065 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005066#ifdef FEAT_QUICKFIX
5067 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005068 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005069 break;
5070 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005071 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005072 break;
5073 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005074 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005075 break;
5076#endif
5077#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5078 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005079 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005080 break;
5081#endif
5082#if defined(FEAT_CRYPT)
5083 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005084 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005085 break;
5086#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005087#ifdef FEAT_LINEBREAK
5088 case PV_SBR:
5089 clear_string_option(&((win_T *)from)->w_p_sbr);
5090 break;
5091#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005092#ifdef FEAT_STL_OPT
5093 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005094 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005095 break;
5096#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005097 case PV_UL:
5098 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5099 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005100#ifdef FEAT_LISP
5101 case PV_LW:
5102 clear_string_option(&buf->b_p_lw);
5103 break;
5104#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005105 case PV_MENC:
5106 clear_string_option(&buf->b_p_menc);
5107 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005108 case PV_LCS:
5109 clear_string_option(&((win_T *)from)->w_p_lcs);
5110 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5111 redraw_later(NOT_VALID);
5112 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113 }
5114}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005115#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005116
5117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 * Get pointer to option variable, depending on local or global scope.
5119 */
5120 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005121get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5124 {
5125 if (p->var == VAR_WIN)
5126 return (char_u *)GLOBAL_WO(get_varp(p));
5127 return p->var;
5128 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005129 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 switch ((int)p->indir)
5132 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005133 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005135 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5136 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5137 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005139 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5140 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5141 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005142 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005143 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005144 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005145 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5146 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005148 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5149 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005151 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5152 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005153#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5154 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5155#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005156#if defined(FEAT_CRYPT)
5157 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5158#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005159#ifdef FEAT_LINEBREAK
5160 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5161#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005162#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005163 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005164#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005165 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005166#ifdef FEAT_LISP
5167 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5168#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005169 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005170 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005171 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5172
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005174 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176 return get_varp(p);
5177}
5178
5179/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005180 * Get pointer to option variable at 'opt_idx', depending on local or global
5181 * scope.
5182 */
5183 char_u *
5184get_option_varp_scope(int opt_idx, int opt_flags)
5185{
5186 return get_varp_scope(&(options[opt_idx]), opt_flags);
5187}
5188
5189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 * Get pointer to option variable.
5191 */
5192 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005193get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005195 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (p->var == NULL)
5197 return NULL;
5198
5199 switch ((int)p->indir)
5200 {
5201 case PV_NONE: return p->var;
5202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005203 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005204 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005206 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005208 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005210 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005212 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005214 case PV_TC: return *curbuf->b_p_tc != NUL
5215 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005216 case PV_BKC: return *curbuf->b_p_bkc != NUL
5217 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005218 case PV_SISO: return curwin->w_p_siso >= 0
5219 ? (char_u *)&(curwin->w_p_siso) : p->var;
5220 case PV_SO: return curwin->w_p_so >= 0
5221 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005223 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005225 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5227#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005228 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005230 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005232 case PV_FP: return *curbuf->b_p_fp != NUL
5233 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005235 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005237 case PV_GP: return *curbuf->b_p_gp != NUL
5238 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5239 case PV_MP: return *curbuf->b_p_mp != NUL
5240 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005242#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5243 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5244 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5245#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005246#if defined(FEAT_CRYPT)
5247 case PV_CM: return *curbuf->b_p_cm != NUL
5248 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5249#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005250#ifdef FEAT_LINEBREAK
5251 case PV_SBR: return *curwin->w_p_sbr != NUL
5252 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5253#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005254#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005255 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005256 ? (char_u *)&(curwin->w_p_stl) : p->var;
5257#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005258 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5259 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005260#ifdef FEAT_LISP
5261 case PV_LW: return *curbuf->b_p_lw != NUL
5262 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5263#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005264 case PV_MENC: return *curbuf->b_p_menc != NUL
5265 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#ifdef FEAT_ARABIC
5267 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5268#endif
5269 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005270 case PV_LCS: return *curwin->w_p_lcs != NUL
5271 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005272#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005273 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5274#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005275#ifdef FEAT_SYN_HL
5276 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5277 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005278 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005279 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281#ifdef FEAT_DIFF
5282 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5283#endif
5284#ifdef FEAT_FOLDING
5285 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5286 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5287 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5288 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5289 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5290 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5291 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5292# ifdef FEAT_EVAL
5293 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5294 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5295# endif
5296 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5297#endif
5298 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005299 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005300#ifdef FEAT_LINEBREAK
5301 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005304 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005305#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5307#endif
5308#ifdef FEAT_RIGHTLEFT
5309 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5310 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5311#endif
5312 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5313 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5314#ifdef FEAT_LINEBREAK
5315 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005316 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5317 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005319 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005322#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005323 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5324 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5325#endif
5326#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005327 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5328 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5329 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5333 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5336 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5338 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5339#ifdef FEAT_CINDENT
5340 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5341 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5342 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5343#endif
5344#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5345 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#ifdef FEAT_FOLDING
5349 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005352#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005353 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005355#ifdef FEAT_COMPL_FUNC
5356 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005357 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005358#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005359#ifdef FEAT_EVAL
5360 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005363 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005369 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5371 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5372 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5373 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5374#ifdef FEAT_FIND_ID
5375# ifdef FEAT_EVAL
5376 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5377# endif
5378#endif
5379#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5380 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5381 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5382#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005383#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005384 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386#ifdef FEAT_CRYPT
5387 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5388#endif
5389#ifdef FEAT_LISP
5390 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5391#endif
5392 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5393 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5394 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5395 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5396 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005398#ifdef FEAT_TEXTOBJ
5399 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5402#ifdef FEAT_SMARTINDENT
5403 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5407#ifdef FEAT_SEARCHPATH
5408 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5409#endif
5410 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5411#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005412 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005413 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5414#endif
5415#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005416 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5417 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5418 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005419 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#endif
5421 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5422 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5423 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5424 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005425#ifdef FEAT_PERSISTENT_UNDO
5426 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5429#ifdef FEAT_KEYMAP
5430 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5431#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005432#ifdef FEAT_SIGNS
5433 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5434#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005435#ifdef FEAT_VARTABS
5436 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5437 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5438#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005439 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 return (char_u *)&(curbuf->b_p_wm);
5443}
5444
5445/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005446 * Return a pointer to the variable for option at 'opt_idx'
5447 */
5448 char_u *
5449get_option_var(int opt_idx)
5450{
5451 return options[opt_idx].var;
5452}
5453
5454/*
5455 * Return the full name of the option at 'opt_idx'
5456 */
5457 char_u *
5458get_option_fullname(int opt_idx)
5459{
5460 return (char_u *)options[opt_idx].fullname;
5461}
5462
5463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 * Get the value of 'equalprg', either the buffer-local one or the global one.
5465 */
5466 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005467get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 if (*curbuf->b_p_ep == NUL)
5470 return p_ep;
5471 return curbuf->b_p_ep;
5472}
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474/*
5475 * Copy options from one window to another.
5476 * Used when splitting a window.
5477 */
5478 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005479win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480{
5481 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5482 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005483 after_copy_winopt(wp_to);
5484}
5485
5486/*
5487 * After copying window options: update variables depending on options.
5488 */
5489 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005490after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005491{
5492#ifdef FEAT_LINEBREAK
5493 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005494#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005495#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005496 fill_culopt_flags(NULL, wp);
5497 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005498#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005499 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502/*
5503 * Copy the options from one winopt_T to another.
5504 * Doesn't free the old option values in "to", use clear_winopt() for that.
5505 * The 'scroll' option is not copied, because it depends on the window height.
5506 * The 'previewwindow' option is reset, there can be only one preview window.
5507 */
5508 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005509copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511#ifdef FEAT_ARABIC
5512 to->wo_arab = from->wo_arab;
5513#endif
5514 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005515 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005517 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005518#ifdef FEAT_LINEBREAK
5519 to->wo_nuw = from->wo_nuw;
5520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#ifdef FEAT_RIGHTLEFT
5522 to->wo_rl = from->wo_rl;
5523 to->wo_rlc = vim_strsave(from->wo_rlc);
5524#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005525#ifdef FEAT_LINEBREAK
5526 to->wo_sbr = vim_strsave(from->wo_sbr);
5527#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005528#ifdef FEAT_STL_OPT
5529 to->wo_stl = vim_strsave(from->wo_stl);
5530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005532#ifdef FEAT_DIFF
5533 to->wo_wrap_save = from->wo_wrap_save;
5534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#ifdef FEAT_LINEBREAK
5536 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005537 to->wo_bri = from->wo_bri;
5538 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005540 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005542 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005543 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005544 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005545#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005546 to->wo_spell = from->wo_spell;
5547#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005548#ifdef FEAT_SYN_HL
5549 to->wo_cuc = from->wo_cuc;
5550 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005551 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005552 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554#ifdef FEAT_DIFF
5555 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005556 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005558#ifdef FEAT_CONCEAL
5559 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005560 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005561#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005562#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005563 to->wo_twk = vim_strsave(from->wo_twk);
5564 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566#ifdef FEAT_FOLDING
5567 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005568 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005570 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 to->wo_fdi = vim_strsave(from->wo_fdi);
5572 to->wo_fml = from->wo_fml;
5573 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005574 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005576 to->wo_fdm_save = from->wo_diff_saved
5577 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 to->wo_fdn = from->wo_fdn;
5579# ifdef FEAT_EVAL
5580 to->wo_fde = vim_strsave(from->wo_fde);
5581 to->wo_fdt = vim_strsave(from->wo_fdt);
5582# endif
5583 to->wo_fmr = vim_strsave(from->wo_fmr);
5584#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005585#ifdef FEAT_SIGNS
5586 to->wo_scl = vim_strsave(from->wo_scl);
5587#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005588
5589#ifdef FEAT_EVAL
5590 // Copy the script context so that we know where the value was last set.
5591 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5592 sizeof(to->wo_script_ctx));
5593#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005594 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597/*
5598 * Check string options in a window for a NULL value.
5599 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005600 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005601check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 check_winopt(&win->w_onebuf_opt);
5604 check_winopt(&win->w_allbuf_opt);
5605}
5606
5607/*
5608 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5609 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005611check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613#ifdef FEAT_FOLDING
5614 check_string_option(&wop->wo_fdi);
5615 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005616 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617# ifdef FEAT_EVAL
5618 check_string_option(&wop->wo_fde);
5619 check_string_option(&wop->wo_fdt);
5620# endif
5621 check_string_option(&wop->wo_fmr);
5622#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005623#ifdef FEAT_SIGNS
5624 check_string_option(&wop->wo_scl);
5625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626#ifdef FEAT_RIGHTLEFT
5627 check_string_option(&wop->wo_rlc);
5628#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005629#ifdef FEAT_LINEBREAK
5630 check_string_option(&wop->wo_sbr);
5631#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005632#ifdef FEAT_STL_OPT
5633 check_string_option(&wop->wo_stl);
5634#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005635#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005636 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637 check_string_option(&wop->wo_cc);
5638#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005639#ifdef FEAT_CONCEAL
5640 check_string_option(&wop->wo_cocu);
5641#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005642#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005643 check_string_option(&wop->wo_twk);
5644 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005645#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005646#ifdef FEAT_LINEBREAK
5647 check_string_option(&wop->wo_briopt);
5648#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005649 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005650 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651}
5652
5653/*
5654 * Free the allocated memory inside a winopt_T.
5655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005657clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659#ifdef FEAT_FOLDING
5660 clear_string_option(&wop->wo_fdi);
5661 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005662 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663# ifdef FEAT_EVAL
5664 clear_string_option(&wop->wo_fde);
5665 clear_string_option(&wop->wo_fdt);
5666# endif
5667 clear_string_option(&wop->wo_fmr);
5668#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005669#ifdef FEAT_SIGNS
5670 clear_string_option(&wop->wo_scl);
5671#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005672#ifdef FEAT_LINEBREAK
5673 clear_string_option(&wop->wo_briopt);
5674#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005675 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676#ifdef FEAT_RIGHTLEFT
5677 clear_string_option(&wop->wo_rlc);
5678#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005679#ifdef FEAT_LINEBREAK
5680 clear_string_option(&wop->wo_sbr);
5681#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005682#ifdef FEAT_STL_OPT
5683 clear_string_option(&wop->wo_stl);
5684#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005685#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005686 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005687 clear_string_option(&wop->wo_cc);
5688#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005689#ifdef FEAT_CONCEAL
5690 clear_string_option(&wop->wo_cocu);
5691#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005692#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005693 clear_string_option(&wop->wo_twk);
5694 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005695#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005696 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697}
5698
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005699#ifdef FEAT_EVAL
5700// Index into the options table for a buffer-local option enum.
5701static int buf_opt_idx[BV_COUNT];
5702# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5703
5704/*
5705 * Initialize buf_opt_idx[] if not done already.
5706 */
5707 static void
5708init_buf_opt_idx(void)
5709{
5710 static int did_init_buf_opt_idx = FALSE;
5711 int i;
5712
5713 if (did_init_buf_opt_idx)
5714 return;
5715 did_init_buf_opt_idx = TRUE;
5716 for (i = 0; !istermoption_idx(i); i++)
5717 if (options[i].indir & PV_BUF)
5718 buf_opt_idx[options[i].indir & PV_MASK] = i;
5719}
5720#else
5721# define COPY_OPT_SCTX(buf, bv)
5722#endif
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724/*
5725 * Copy global option values to local options for one buffer.
5726 * Used when creating a new buffer and sometimes when entering a buffer.
5727 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005728 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5730 * appropriate.
5731 * BCO_NOHELP Don't copy the values to a help buffer.
5732 */
5733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005734buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005737 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 int dont_do_help;
5739 int did_isk = FALSE;
5740
5741 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 * Skip this when the option defaults have not been set yet. Happens when
5743 * main() allocates the first buffer.
5744 */
5745 if (p_cpo != NULL)
5746 {
5747 /*
5748 * Always copy when entering and 'cpo' contains 'S'.
5749 * Don't copy when already initialized.
5750 * Don't copy when 'cpo' contains 's' and not entering.
5751 * 'S' BCO_ENTER initialized 's' should_copy
5752 * yes yes X X TRUE
5753 * yes no yes X FALSE
5754 * no X yes X FALSE
5755 * X no no yes FALSE
5756 * X no no no TRUE
5757 * no yes no X TRUE
5758 */
5759 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5760 && (buf->b_p_initialized
5761 || (!(flags & BCO_ENTER)
5762 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5763 should_copy = FALSE;
5764
5765 if (should_copy || (flags & BCO_ALWAYS))
5766 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005767#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005768 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005769 init_buf_opt_idx();
5770#endif
5771 // Don't copy the options specific to a help buffer when
5772 // BCO_NOHELP is given or the options were initialized already
5773 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5775 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005776 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 save_p_isk = buf->b_p_isk;
5779 buf->b_p_isk = NULL;
5780 }
5781 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005782 * Always free the allocated strings. If not already initialized,
5783 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 */
5785 if (!buf->b_p_initialized)
5786 {
5787 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005788 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005791 switch (*p_ffs)
5792 {
5793 case 'm':
5794 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5795 case 'd':
5796 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5797 case 'u':
5798 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5799 default:
5800 buf->b_p_ff = vim_strsave(p_ff);
5801 }
5802 if (buf->b_p_ff != NULL)
5803 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 buf->b_p_bh = empty_option;
5805 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807 else
5808 free_buf_options(buf, FALSE);
5809
5810 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005811 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 buf->b_p_ai_nopaste = p_ai_nopaste;
5813 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005814 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005816 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 buf->b_p_tw_nopaste = p_tw_nopaste;
5818 buf->b_p_tw_nobin = p_tw_nobin;
5819 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005820 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 buf->b_p_wm_nopaste = p_wm_nopaste;
5822 buf->b_p_wm_nobin = p_wm_nobin;
5823 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005824 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005825 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005826 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005827 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005828 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005830 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005832 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005834 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 buf->b_p_ml_nobin = p_ml_nobin;
5836 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005837 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005838 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005839 buf->b_p_swf = FALSE;
5840 else
5841 {
5842 buf->b_p_swf = p_swf;
5843 COPY_OPT_SCTX(buf, BV_INF);
5844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005846 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005847#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005848 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005849 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005851#ifdef FEAT_COMPL_FUNC
5852 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005853 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005854 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005855 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005856#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005857#ifdef FEAT_EVAL
5858 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005859 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005862 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005864#ifdef FEAT_VARTABS
5865 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005866 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005867 if (p_vsts && p_vsts != empty_option)
5868 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5869 else
5870 buf->b_p_vsts_array = 0;
5871 buf->b_p_vsts_nopaste = p_vsts_nopaste
5872 ? vim_strsave(p_vsts_nopaste) : NULL;
5873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005875 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005877 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878#ifdef FEAT_FOLDING
5879 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005880 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#endif
5882 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005883 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005884 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005885 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005886 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5887 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005889 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005891 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#ifdef FEAT_SMARTINDENT
5893 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005894 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#endif
5896 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005897 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#ifdef FEAT_CINDENT
5899 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005902 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005904 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005906 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005909 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5911 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005912 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913#endif
5914#ifdef FEAT_LISP
5915 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005916 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917#endif
5918#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005919 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005921 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005922 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005923 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005924#endif
5925#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005926 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005927 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005928 (void)compile_cap_prog(&buf->b_s);
5929 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005930 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005931 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005933 buf->b_s.b_p_spo = vim_strsave(p_spo);
5934 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935#endif
5936#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5937 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005938 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005942 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005943#if defined(FEAT_EVAL)
5944 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947#ifdef FEAT_CRYPT
5948 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#endif
5951#ifdef FEAT_SEARCHPATH
5952 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#endif
5955#ifdef FEAT_KEYMAP
5956 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 buf->b_kmap_state |= KEYMAP_INIT;
5959#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005960#ifdef FEAT_TERMINAL
5961 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005962 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005963#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005964 // This isn't really an option, but copying the langmap and IME
5965 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005969 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005971 // options that are normally global but also have a local value
5972 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005974 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005975 buf->b_p_bkc = empty_option;
5976 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#ifdef FEAT_QUICKFIX
5978 buf->b_p_gp = empty_option;
5979 buf->b_p_mp = empty_option;
5980 buf->b_p_efm = empty_option;
5981#endif
5982 buf->b_p_ep = empty_option;
5983 buf->b_p_kp = empty_option;
5984 buf->b_p_path = empty_option;
5985 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005986 buf->b_p_tc = empty_option;
5987 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988#ifdef FEAT_FIND_ID
5989 buf->b_p_def = empty_option;
5990 buf->b_p_inc = empty_option;
5991# ifdef FEAT_EVAL
5992 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005993 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994# endif
5995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 buf->b_p_dict = empty_option;
5997 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005998#ifdef FEAT_TEXTOBJ
5999 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006000 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006001#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006002#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6003 buf->b_p_bexpr = empty_option;
6004#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006005#if defined(FEAT_CRYPT)
6006 buf->b_p_cm = empty_option;
6007#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006008#ifdef FEAT_PERSISTENT_UNDO
6009 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006010 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006011#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006012#ifdef FEAT_LISP
6013 buf->b_p_lw = empty_option;
6014#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006015 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
6017 /*
6018 * Don't copy the options set by ex_help(), use the saved values,
6019 * when going from a help buffer to a non-help buffer.
6020 * Don't touch these at all when BCO_NOHELP is used and going from
6021 * or to a help buffer.
6022 */
6023 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006026#ifdef FEAT_VARTABS
6027 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6028 tabstop_set(p_vts, &buf->b_p_vts_array);
6029 else
6030 buf->b_p_vts_array = NULL;
6031#endif
6032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 else
6034 {
6035 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 did_isk = TRUE;
6038 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006039#ifdef FEAT_VARTABS
6040 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006041 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006042 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6043 tabstop_set(p_vts, &buf->b_p_vts_array);
6044 else
6045 buf->b_p_vts_array = NULL;
6046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 if (buf->b_p_bt[0] == 'h')
6049 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006051 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 }
6053 }
6054
6055 /*
6056 * When the options should be copied (ignoring BCO_ALWAYS), set the
6057 * flag that indicates that the options have been initialized.
6058 */
6059 if (should_copy)
6060 buf->b_p_initialized = TRUE;
6061 }
6062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006063 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 if (did_isk)
6065 (void)buf_init_chartab(buf, FALSE);
6066}
6067
6068/*
6069 * Reset the 'modifiable' option and its default value.
6070 */
6071 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006072reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
6074 int opt_idx;
6075
6076 curbuf->b_p_ma = FALSE;
6077 p_ma = FALSE;
6078 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006079 if (opt_idx >= 0)
6080 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081}
6082
6083/*
6084 * Set the global value for 'iminsert' to the local value.
6085 */
6086 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006087set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 p_iminsert = curbuf->b_p_iminsert;
6090}
6091
6092/*
6093 * Set the global value for 'imsearch' to the local value.
6094 */
6095 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006096set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 p_imsearch = curbuf->b_p_imsearch;
6099}
6100
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101static int expand_option_idx = -1;
6102static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6103static int expand_option_flags = 0;
6104
6105 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006106set_context_in_set_cmd(
6107 expand_T *xp,
6108 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006109 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110{
6111 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006112 long_u flags = 0; // init for GCC
6113 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 char_u *p;
6115 char_u *s;
6116 int is_term_option = FALSE;
6117 int key;
6118
6119 expand_option_flags = opt_flags;
6120
6121 xp->xp_context = EXPAND_SETTINGS;
6122 if (*arg == NUL)
6123 {
6124 xp->xp_pattern = arg;
6125 return;
6126 }
6127 p = arg + STRLEN(arg) - 1;
6128 if (*p == ' ' && *(p - 1) != '\\')
6129 {
6130 xp->xp_pattern = p + 1;
6131 return;
6132 }
6133 while (p > arg)
6134 {
6135 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006136 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 if (*p == ' ' || *p == ',')
6138 {
6139 while (s > arg && *(s - 1) == '\\')
6140 --s;
6141 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006142 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (*p == ' ' && ((p - s) & 1) == 0)
6144 {
6145 ++p;
6146 break;
6147 }
6148 --p;
6149 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006150 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
6152 xp->xp_context = EXPAND_BOOL_SETTINGS;
6153 p += 2;
6154 }
6155 if (STRNCMP(p, "inv", 3) == 0)
6156 {
6157 xp->xp_context = EXPAND_BOOL_SETTINGS;
6158 p += 3;
6159 }
6160 xp->xp_pattern = arg = p;
6161 if (*arg == '<')
6162 {
6163 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006164 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 return;
6166 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006167 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
6169 xp->xp_context = EXPAND_NOTHING;
6170 return;
6171 }
6172 nextchar = *++p;
6173 is_term_option = TRUE;
6174 expand_option_name[2] = KEY2TERMCAP0(key);
6175 expand_option_name[3] = KEY2TERMCAP1(key);
6176 }
6177 else
6178 {
6179 if (p[0] == 't' && p[1] == '_')
6180 {
6181 p += 2;
6182 if (*p != NUL)
6183 ++p;
6184 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006185 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 nextchar = *++p;
6187 is_term_option = TRUE;
6188 expand_option_name[2] = p[-2];
6189 expand_option_name[3] = p[-1];
6190 }
6191 else
6192 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006193 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6195 p++;
6196 if (*p == NUL)
6197 return;
6198 nextchar = *p;
6199 *p = NUL;
6200 opt_idx = findoption(arg);
6201 *p = nextchar;
6202 if (opt_idx == -1 || options[opt_idx].var == NULL)
6203 {
6204 xp->xp_context = EXPAND_NOTHING;
6205 return;
6206 }
6207 flags = options[opt_idx].flags;
6208 if (flags & P_BOOL)
6209 {
6210 xp->xp_context = EXPAND_NOTHING;
6211 return;
6212 }
6213 }
6214 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006215 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6217 {
6218 ++p;
6219 nextchar = '=';
6220 }
6221 if ((nextchar != '=' && nextchar != ':')
6222 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6223 {
6224 xp->xp_context = EXPAND_UNSUCCESSFUL;
6225 return;
6226 }
6227 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6228 {
6229 xp->xp_context = EXPAND_OLD_SETTING;
6230 if (is_term_option)
6231 expand_option_idx = -1;
6232 else
6233 expand_option_idx = opt_idx;
6234 xp->xp_pattern = p + 1;
6235 return;
6236 }
6237 xp->xp_context = EXPAND_NOTHING;
6238 if (is_term_option || (flags & P_NUM))
6239 return;
6240
6241 xp->xp_pattern = p + 1;
6242
6243 if (flags & P_EXPAND)
6244 {
6245 p = options[opt_idx].var;
6246 if (p == (char_u *)&p_bdir
6247 || p == (char_u *)&p_dir
6248 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006249 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 || p == (char_u *)&p_rtp
6251#ifdef FEAT_SEARCHPATH
6252 || p == (char_u *)&p_cdpath
6253#endif
6254#ifdef FEAT_SESSION
6255 || p == (char_u *)&p_vdir
6256#endif
6257 )
6258 {
6259 xp->xp_context = EXPAND_DIRECTORIES;
6260 if (p == (char_u *)&p_path
6261#ifdef FEAT_SEARCHPATH
6262 || p == (char_u *)&p_cdpath
6263#endif
6264 )
6265 xp->xp_backslash = XP_BS_THREE;
6266 else
6267 xp->xp_backslash = XP_BS_ONE;
6268 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006269 else if (p == (char_u *)&p_ft)
6270 {
6271 xp->xp_context = EXPAND_FILETYPE;
6272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 else
6274 {
6275 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006276 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (p == (char_u *)&p_tags)
6278 xp->xp_backslash = XP_BS_THREE;
6279 else
6280 xp->xp_backslash = XP_BS_ONE;
6281 }
6282 }
6283
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006284 // For an option that is a list of file names, find the start of the
6285 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6287 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006288 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (*p == ' ' || *p == ',')
6290 {
6291 s = p;
6292 while (s > xp->xp_pattern && *(s - 1) == '\\')
6293 --s;
6294 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6295 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6296 {
6297 xp->xp_pattern = p + 1;
6298 break;
6299 }
6300 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006301
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006302#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006304 if (options[opt_idx].var == (char_u *)&p_sps
6305 && STRNCMP(p, "file:", 5) == 0)
6306 {
6307 xp->xp_pattern = p + 5;
6308 break;
6309 }
6310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
6312
6313 return;
6314}
6315
6316 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006317ExpandSettings(
6318 expand_T *xp,
6319 regmatch_T *regmatch,
6320 int *num_file,
6321 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006323 int num_normal = 0; // Nr of matching non-term-code settings
6324 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 int opt_idx;
6326 int match;
6327 int count = 0;
6328 char_u *str;
6329 int loop;
6330 int is_term_opt;
6331 char_u name_buf[MAX_KEY_NAME_LEN];
6332 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006333 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006335 // do this loop twice:
6336 // loop == 0: count the number of matching options
6337 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 for (loop = 0; loop <= 1; ++loop)
6339 {
6340 regmatch->rm_ic = ic;
6341 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6342 {
K.Takataeeec2542021-06-02 13:28:16 +02006343 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6345 {
6346 if (loop == 0)
6347 num_normal++;
6348 else
6349 (*file)[count++] = vim_strsave((char_u *)names[match]);
6350 }
6351 }
6352 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6353 opt_idx++)
6354 {
6355 if (options[opt_idx].var == NULL)
6356 continue;
6357 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6358 && !(options[opt_idx].flags & P_BOOL))
6359 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006360 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 if (is_term_opt && num_normal > 0)
6362 continue;
6363 match = FALSE;
6364 if (vim_regexec(regmatch, str, (colnr_T)0)
6365 || (options[opt_idx].shortname != NULL
6366 && vim_regexec(regmatch,
6367 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6368 match = TRUE;
6369 else if (is_term_opt)
6370 {
6371 name_buf[0] = '<';
6372 name_buf[1] = 't';
6373 name_buf[2] = '_';
6374 name_buf[3] = str[2];
6375 name_buf[4] = str[3];
6376 name_buf[5] = '>';
6377 name_buf[6] = NUL;
6378 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6379 {
6380 match = TRUE;
6381 str = name_buf;
6382 }
6383 }
6384 if (match)
6385 {
6386 if (loop == 0)
6387 {
6388 if (is_term_opt)
6389 num_term++;
6390 else
6391 num_normal++;
6392 }
6393 else
6394 (*file)[count++] = vim_strsave(str);
6395 }
6396 }
6397 /*
6398 * Check terminal key codes, these are not in the option table
6399 */
6400 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6401 {
6402 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6403 {
6404 if (!isprint(str[0]) || !isprint(str[1]))
6405 continue;
6406
6407 name_buf[0] = 't';
6408 name_buf[1] = '_';
6409 name_buf[2] = str[0];
6410 name_buf[3] = str[1];
6411 name_buf[4] = NUL;
6412
6413 match = FALSE;
6414 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6415 match = TRUE;
6416 else
6417 {
6418 name_buf[0] = '<';
6419 name_buf[1] = 't';
6420 name_buf[2] = '_';
6421 name_buf[3] = str[0];
6422 name_buf[4] = str[1];
6423 name_buf[5] = '>';
6424 name_buf[6] = NUL;
6425
6426 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6427 match = TRUE;
6428 }
6429 if (match)
6430 {
6431 if (loop == 0)
6432 num_term++;
6433 else
6434 (*file)[count++] = vim_strsave(name_buf);
6435 }
6436 }
6437
6438 /*
6439 * Check special key names.
6440 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006441 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6443 {
6444 name_buf[0] = '<';
6445 STRCPY(name_buf + 1, str);
6446 STRCAT(name_buf, ">");
6447
6448 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6449 {
6450 if (loop == 0)
6451 num_term++;
6452 else
6453 (*file)[count++] = vim_strsave(name_buf);
6454 }
6455 }
6456 }
6457 if (loop == 0)
6458 {
6459 if (num_normal > 0)
6460 *num_file = num_normal;
6461 else if (num_term > 0)
6462 *num_file = num_term;
6463 else
6464 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006465 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (*file == NULL)
6467 {
6468 *file = (char_u **)"";
6469 return FAIL;
6470 }
6471 }
6472 }
6473 return OK;
6474}
6475
6476 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006477ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006479 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 char_u *buf;
6481
6482 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006483 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (*file == NULL)
6485 return FAIL;
6486
6487 /*
6488 * For a terminal key code expand_option_idx is < 0.
6489 */
6490 if (expand_option_idx < 0)
6491 {
6492 var = find_termcode(expand_option_name + 2);
6493 if (var == NULL)
6494 expand_option_idx = findoption(expand_option_name);
6495 }
6496
6497 if (expand_option_idx >= 0)
6498 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006499 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 option_value2string(&options[expand_option_idx], expand_option_flags);
6501 var = NameBuff;
6502 }
6503 else if (var == NULL)
6504 var = (char_u *)"";
6505
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006506 // A backslash is required before some characters. This is the reverse of
6507 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 buf = vim_strsave_escaped(var, escape_chars);
6509
6510 if (buf == NULL)
6511 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006512 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 return FAIL;
6514 }
6515
6516#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006517 // For MS-Windows et al. we don't double backslashes at the start and
6518 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006519 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (var[0] == '\\' && var[1] == '\\'
6521 && expand_option_idx >= 0
6522 && (options[expand_option_idx].flags & P_EXPAND)
6523 && vim_isfilec(var[2])
6524 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006525 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526#endif
6527
6528 *file[0] = buf;
6529 *num_file = 1;
6530 return OK;
6531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
6533/*
6534 * Get the value for the numeric or string option *opp in a nice format into
6535 * NameBuff[]. Must not be called with a hidden option!
6536 */
6537 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006538option_value2string(
6539 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006540 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 char_u *varp;
6543
6544 varp = get_varp_scope(opp, opt_flags);
6545
6546 if (opp->flags & P_NUM)
6547 {
6548 long wc = 0;
6549
6550 if (wc_use_keyname(varp, &wc))
6551 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6552 else if (wc != 0)
6553 STRCPY(NameBuff, transchar((int)wc));
6554 else
6555 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6556 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006557 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
6559 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006560 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 NameBuff[0] = NUL;
6562#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006563 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006564 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 STRCPY(NameBuff, "*****");
6566#endif
6567 else if (opp->flags & P_EXPAND)
6568 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006569 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 else if ((char_u **)opp->var == &p_pt)
6571 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6572 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006573 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575}
6576
6577/*
6578 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6579 * printed as a keyname.
6580 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6581 */
6582 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006583wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6586 {
6587 *wcp = *(long *)varp;
6588 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6589 return TRUE;
6590 }
6591 return FALSE;
6592}
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 * Return TRUE if "x" is present in 'shortmess' option, or
6596 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6597 */
6598 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006599shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006601 return p_shm != NULL &&
6602 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 || (vim_strchr(p_shm, 'a') != NULL
6604 && vim_strchr((char_u *)SHM_A, x) != NULL));
6605}
6606
6607/*
6608 * paste_option_changed() - Called after p_paste was set or reset.
6609 */
6610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
6613 static int old_p_paste = FALSE;
6614 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006615 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616#ifdef FEAT_CMDL_INFO
6617 static int save_ru = 0;
6618#endif
6619#ifdef FEAT_RIGHTLEFT
6620 static int save_ri = 0;
6621 static int save_hkmap = 0;
6622#endif
6623 buf_T *buf;
6624
6625 if (p_paste)
6626 {
6627 /*
6628 * Paste switched from off to on.
6629 * Save the current values, so they can be restored later.
6630 */
6631 if (!old_p_paste)
6632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006633 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006634 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
6636 buf->b_p_tw_nopaste = buf->b_p_tw;
6637 buf->b_p_wm_nopaste = buf->b_p_wm;
6638 buf->b_p_sts_nopaste = buf->b_p_sts;
6639 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006640 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006641#ifdef FEAT_VARTABS
6642 if (buf->b_p_vsts_nopaste)
6643 vim_free(buf->b_p_vsts_nopaste);
6644 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6645 ? vim_strsave(buf->b_p_vsts) : NULL;
6646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 }
6648
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006649 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006651 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652#ifdef FEAT_CMDL_INFO
6653 save_ru = p_ru;
6654#endif
6655#ifdef FEAT_RIGHTLEFT
6656 save_ri = p_ri;
6657 save_hkmap = p_hkmap;
6658#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006659 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006660 p_ai_nopaste = p_ai;
6661 p_et_nopaste = p_et;
6662 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 p_tw_nopaste = p_tw;
6664 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006665#ifdef FEAT_VARTABS
6666 if (p_vsts_nopaste)
6667 vim_free(p_vsts_nopaste);
6668 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
6671
6672 /*
6673 * Always set the option values, also when 'paste' is set when it is
6674 * already on.
6675 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006676 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006677 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006679 buf->b_p_tw = 0; // textwidth is 0
6680 buf->b_p_wm = 0; // wrapmargin is 0
6681 buf->b_p_sts = 0; // softtabstop is 0
6682 buf->b_p_ai = 0; // no auto-indent
6683 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006684#ifdef FEAT_VARTABS
6685 if (buf->b_p_vsts)
6686 free_string_option(buf->b_p_vsts);
6687 buf->b_p_vsts = empty_option;
6688 if (buf->b_p_vsts_array)
6689 vim_free(buf->b_p_vsts_array);
6690 buf->b_p_vsts_array = 0;
6691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 }
6693
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006694 // set global options
6695 p_sm = 0; // no showmatch
6696 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006699 status_redraw_all(); // redraw to remove the ruler
6700 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701#endif
6702#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006703 p_ri = 0; // no reverse insert
6704 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006706 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 p_tw = 0;
6708 p_wm = 0;
6709 p_sts = 0;
6710 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006711#ifdef FEAT_VARTABS
6712 if (p_vsts)
6713 free_string_option(p_vsts);
6714 p_vsts = empty_option;
6715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 }
6717
6718 /*
6719 * Paste switched from on to off: Restore saved values.
6720 */
6721 else if (old_p_paste)
6722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006723 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006724 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {
6726 buf->b_p_tw = buf->b_p_tw_nopaste;
6727 buf->b_p_wm = buf->b_p_wm_nopaste;
6728 buf->b_p_sts = buf->b_p_sts_nopaste;
6729 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006730 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006731#ifdef FEAT_VARTABS
6732 if (buf->b_p_vsts)
6733 free_string_option(buf->b_p_vsts);
6734 buf->b_p_vsts = buf->b_p_vsts_nopaste
6735 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6736 if (buf->b_p_vsts_array)
6737 vim_free(buf->b_p_vsts_array);
6738 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6739 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6740 else
6741 buf->b_p_vsts_array = 0;
6742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006747 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006750 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 p_ru = save_ru;
6752#endif
6753#ifdef FEAT_RIGHTLEFT
6754 p_ri = save_ri;
6755 p_hkmap = save_hkmap;
6756#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006757 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006758 p_ai = p_ai_nopaste;
6759 p_et = p_et_nopaste;
6760 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 p_tw = p_tw_nopaste;
6762 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006763#ifdef FEAT_VARTABS
6764 if (p_vsts)
6765 free_string_option(p_vsts);
6766 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 }
6769
6770 old_p_paste = p_paste;
6771}
6772
6773/*
6774 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6775 *
6776 * Reset 'compatible' and set the values for options that didn't get set yet
6777 * to the Vim defaults.
6778 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 */
6781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006782vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006784 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006785 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788 if (!option_was_set((char_u *)"cp"))
6789 {
6790 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006791 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6793 set_option_default(opt_idx, OPT_FREE, FALSE);
6794 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006795 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006797
6798 if (fname != NULL)
6799 {
6800 p = vim_getenv(envname, &dofree);
6801 if (p == NULL)
6802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006803 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006804 p = FullName_save(fname, FALSE);
6805 if (p != NULL)
6806 {
6807 vim_setenv(envname, p);
6808 vim_free(p);
6809 }
6810 }
6811 else if (dofree)
6812 vim_free(p);
6813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
6816/*
6817 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6818 */
6819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006820change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006822 int opt_idx;
6823
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (p_cp != on)
6825 {
6826 p_cp = on;
6827 compatible_set();
6828 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006829 opt_idx = findoption((char_u *)"cp");
6830 if (opt_idx >= 0)
6831 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832}
6833
6834/*
6835 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006836 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 */
6838 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006839option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840{
6841 int idx;
6842
6843 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006844 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 return FALSE;
6846 if (options[idx].flags & P_WAS_SET)
6847 return TRUE;
6848 return FALSE;
6849}
6850
6851/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006852 * Reset the flag indicating option "name" was set.
6853 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006854 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006855reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006856{
6857 int idx = findoption(name);
6858
6859 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006860 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006861 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006862 return OK;
6863 }
6864 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006865}
6866
6867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 * compatible_set() - Called when 'compatible' has been set or unset.
6869 *
6870 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6871 * flag) to a Vi compatible value.
6872 * When 'compatible' is unset: Set all options that have a different default
6873 * for Vim (without the P_VI_DEF flag) to that default.
6874 */
6875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006876compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 int opt_idx;
6879
Bram Moolenaardac13472019-09-16 21:06:21 +02006880 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6882 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6883 set_option_default(opt_idx, OPT_FREE, p_cp);
6884 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006885 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886}
6887
Bram Moolenaardac13472019-09-16 21:06:21 +02006888#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890/*
6891 * fill_breakat_flags() -- called when 'breakat' changes value.
6892 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006894fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006896 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 int i;
6898
6899 for (i = 0; i < 256; i++)
6900 breakat_flags[i] = FALSE;
6901
6902 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006903 for (p = p_breakat; *p; p++)
6904 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906#endif
6907
6908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 * Check if backspacing over something is allowed.
6910 */
6911 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006912can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006913 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006915#ifdef FEAT_JOB_CHANNEL
6916 if (what == BS_START && bt_prompt(curbuf))
6917 return FALSE;
6918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 switch (*p_bs)
6920 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006921 case '3': return TRUE;
6922 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 case '1': return (what != BS_START);
6924 case '0': return FALSE;
6925 }
6926 return vim_strchr(p_bs, what) != NULL;
6927}
6928
6929/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006930 * Return the effective 'scrolloff' value for the current window, using the
6931 * global value when appropriate.
6932 */
6933 long
6934get_scrolloff_value(void)
6935{
6936 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6937}
6938
6939/*
6940 * Return the effective 'sidescrolloff' value for the current window, using the
6941 * global value when appropriate.
6942 */
6943 long
6944get_sidescrolloff_value(void)
6945{
6946 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6947}
6948
6949/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006950 * Get the local or global value of 'backupcopy'.
6951 */
6952 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006953get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006954{
6955 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6956}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006957
Bram Moolenaaree857022019-11-09 23:26:40 +01006958#if defined(FEAT_LINEBREAK) || defined(PROTO)
6959/*
6960 * Get the local or global value of 'showbreak'.
6961 */
6962 char_u *
6963get_showbreak_value(win_T *win)
6964{
6965 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6966 return p_sbr;
6967 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6968 return empty_option;
6969 return win->w_p_sbr;
6970}
6971#endif
6972
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006973#if defined(FEAT_EVAL) || defined(PROTO)
6974/*
6975 * Get window or buffer local options.
6976 */
6977 dict_T *
6978get_winbuf_options(int bufopt)
6979{
6980 dict_T *d;
6981 int opt_idx;
6982
6983 d = dict_alloc();
6984 if (d == NULL)
6985 return NULL;
6986
Bram Moolenaardac13472019-09-16 21:06:21 +02006987 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006988 {
6989 struct vimoption *opt = &options[opt_idx];
6990
6991 if ((bufopt && (opt->indir & PV_BUF))
6992 || (!bufopt && (opt->indir & PV_WIN)))
6993 {
6994 char_u *varp = get_varp(opt);
6995
6996 if (varp != NULL)
6997 {
6998 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006999 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007000 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007001 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007002 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007003 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007004 }
7005 }
7006 }
7007
7008 return d;
7009}
7010#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007011
Bram Moolenaardac13472019-09-16 21:06:21 +02007012#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007013/*
7014 * This is called when 'culopt' is changed
7015 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007016 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007017fill_culopt_flags(char_u *val, win_T *wp)
7018{
7019 char_u *p;
7020 char_u culopt_flags_new = 0;
7021
7022 if (val == NULL)
7023 p = wp->w_p_culopt;
7024 else
7025 p = val;
7026 while (*p != NUL)
7027 {
7028 if (STRNCMP(p, "line", 4) == 0)
7029 {
7030 p += 4;
7031 culopt_flags_new |= CULOPT_LINE;
7032 }
7033 else if (STRNCMP(p, "both", 4) == 0)
7034 {
7035 p += 4;
7036 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7037 }
7038 else if (STRNCMP(p, "number", 6) == 0)
7039 {
7040 p += 6;
7041 culopt_flags_new |= CULOPT_NBR;
7042 }
7043 else if (STRNCMP(p, "screenline", 10) == 0)
7044 {
7045 p += 10;
7046 culopt_flags_new |= CULOPT_SCRLINE;
7047 }
7048
7049 if (*p != ',' && *p != NUL)
7050 return FAIL;
7051 if (*p == ',')
7052 ++p;
7053 }
7054
7055 // Can't have both "line" and "screenline".
7056 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7057 return FAIL;
7058 wp->w_p_culopt_flags = culopt_flags_new;
7059
7060 return OK;
7061}
7062#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007063
7064/*
7065 * Get the value of 'magic' adjusted for Vim9 script.
7066 */
7067 int
7068magic_isset(void)
7069{
7070 switch (magic_overruled)
7071 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007072 case OPTION_MAGIC_ON: return TRUE;
7073 case OPTION_MAGIC_OFF: return FALSE;
7074 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007075 }
7076#ifdef FEAT_EVAL
7077 if (in_vim9script())
7078 return TRUE;
7079#endif
7080 return p_magic;
7081}