blob: 5fc059e29de182f5d88a0a410f7baec0f045cab7 [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 }
Mike Williams12795022021-06-28 20:53:58 +0200935# ifdef MSWIN
936 // PowerShell 5.1/.NET outputs UTF-16 with BOM so re-encode to the
937 // current codepage
938 else if ( fnamecmp(p, "powershell") == 0
939 || fnamecmp(p, "powershell.exe") == 0
940 )
941 {
942# if defined(FEAT_QUICKFIX)
943 if (do_sp)
944 {
945 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
948# endif
949 if (do_srr)
950 {
951 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Natanael Copa56318362021-05-06 18:46:35 +0200957 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ( fnamecmp(p, "sh") == 0
959 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200960 || fnamecmp(p, "mksh") == 0
961 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000963 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200965 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200966 || fnamecmp(p, "ash") == 0
967 || fnamecmp(p, "dash") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100968# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 || fnamecmp(p, "cmd") == 0
970 || fnamecmp(p, "sh.exe") == 0
971 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200972 || fnamecmp(p, "mksh.exe") == 0
973 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000975 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 || fnamecmp(p, "bash.exe") == 0
977 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200978 || fnamecmp(p, "dash.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100980 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (do_sp)
984 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100985# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100987# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
991 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (do_srr)
994 {
995 p_srr = (char_u *)">%s 2>&1";
996 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
997 }
998 }
999 vim_free(p);
1000 }
1001#endif
1002
Bram Moolenaar4f974752019-02-17 17:44:42 +01001003#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001005 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1006 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001008 * set, but only if they have not been set before.
1009 * Default values depend on shell (cmd.exe is default shell):
1010 *
1011 * p_shcf p_sxq
1012 * cmd.exe - "/c" "("
1013 * powershell.exe - "-Command" "\""
1014 * "sh" like shells - "-c" "\""
1015 *
1016 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Mike Williams12795022021-06-28 20:53:58 +02001018 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1019 {
1020 int idx_opt;
1021
1022 idx_opt = findoption((char_u *)"shcf");
1023 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1024 {
1025 p_shcf = (char_u*)"-Command";
1026 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1027 }
1028
1029 idx_opt = findoption((char_u *)"sxq");
1030 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1031 {
1032 p_sxq = (char_u*)"\"";
1033 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1034 }
1035 }
1036 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
1038 int idx3;
1039
1040 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001041 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 p_shcf = (char_u *)"-c";
1044 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1045 }
1046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001049 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 p_sxq = (char_u *)"\"";
1052 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001055 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1056 {
1057 int idx3;
1058
1059 /*
1060 * cmd.exe on Windows will strip the first and last double quote given
1061 * on the command line, e.g. most of the time things like:
1062 * cmd /c "my path/to/echo" "my args to echo"
1063 * become:
1064 * my path/to/echo" "my args to echo
1065 * when executed.
1066 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001067 * To avoid this, set shellxquote to surround the command in
1068 * parenthesis. This appears to make most commands work, without
1069 * breaking commands that worked previously, such as
1070 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001071 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001072 idx3 = findoption((char_u *)"sxq");
1073 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1074 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001075 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001076 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1077 }
1078
Bram Moolenaara64ba222012-02-12 23:23:31 +01001079 idx3 = findoption((char_u *)"shcf");
1080 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1081 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001082 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001083 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1084 }
1085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086#endif
1087
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001088 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001089 {
1090 int idx_ffs = findoption((char_u *)"ffs");
1091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001092 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001093 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1094 set_fileformat(default_fileformat(), OPT_LOCAL);
1095 }
1096
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097#ifdef FEAT_TITLE
1098 set_title_defaults();
1099#endif
1100}
1101
1102#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1103/*
1104 * When 'helplang' is still at its default value, set it to "lang".
1105 * Only the first two characters of "lang" are used.
1106 */
1107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001108set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 int idx;
1111
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001112 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 return;
1114 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001115 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 if (options[idx].flags & P_ALLOCED)
1118 free_string_option(p_hlg);
1119 p_hlg = vim_strsave(lang);
1120 if (p_hlg == NULL)
1121 p_hlg = empty_option;
1122 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001123 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001124 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001125 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1126 {
1127 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1128 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1129 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001130 // any C like setting, such as C.UTF-8, becomes "en"
1131 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1132 {
1133 p_hlg[0] = 'e';
1134 p_hlg[1] = 'n';
1135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 options[idx].flags |= P_ALLOCED;
1139 }
1140}
1141#endif
1142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#ifdef FEAT_TITLE
1144/*
1145 * 'title' and 'icon' only default to true if they have not been set or reset
1146 * in .vimrc and we can read the old value.
1147 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1148 * they can be reset. This reduces startup time when using X on a remote
1149 * machine.
1150 */
1151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001152set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
1154 int idx1;
1155 long val;
1156
1157 /*
1158 * If GUI is (going to be) used, we can always set the window title and
1159 * icon name. Saves a bit of time, because the X11 display server does
1160 * not need to be contacted.
1161 */
1162 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001163 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
1165#ifdef FEAT_GUI
1166 if (gui.starting || gui.in_use)
1167 val = TRUE;
1168 else
1169#endif
1170 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001171 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 p_title = val;
1173 }
1174 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001175 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177#ifdef FEAT_GUI
1178 if (gui.starting || gui.in_use)
1179 val = TRUE;
1180 else
1181#endif
1182 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001183 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 p_icon = val;
1185 }
1186}
1187#endif
1188
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001189 void
1190ex_set(exarg_T *eap)
1191{
1192 int flags = 0;
1193
1194 if (eap->cmdidx == CMD_setlocal)
1195 flags = OPT_LOCAL;
1196 else if (eap->cmdidx == CMD_setglobal)
1197 flags = OPT_GLOBAL;
1198#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001199 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001200 ex_options(eap);
1201 else
1202#endif
1203 {
1204 if (eap->forceit)
1205 flags |= OPT_ONECOLUMN;
1206 (void)do_set(eap->arg, flags);
1207 }
1208}
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210/*
1211 * Parse 'arg' for option settings.
1212 *
1213 * 'arg' may be IObuff, but only when no errors can be present and option
1214 * does not need to be expanded with option_expand().
1215 * "opt_flags":
1216 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001217 * OPT_GLOBAL for ":setglobal"
1218 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001220 * OPT_WINONLY to only set window-local options
1221 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 *
1223 * returns FAIL if an error is detected, OK otherwise
1224 */
1225 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001226do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001228 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
1230 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001231 char *errmsg;
1232 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001234 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1235 int nextchar; // next non-white char after option name
1236 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 int len;
1238 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001239 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001241 long_u flags; // flags for current option
1242 char_u *varp = NULL; // pointer to variable for current option
1243 int did_show = FALSE; // already showed one value
1244 int adding; // "opt+=arg"
1245 int prepending; // "opt^=arg"
1246 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 int cp_val = 0;
1248 char_u key_name[2];
1249
1250 if (*arg == NUL)
1251 {
1252 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001253 did_show = TRUE;
1254 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001257 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001260 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001262 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1263 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 /*
1266 * ":set all" show all options.
1267 * ":set all&" set all options to their default value.
1268 */
1269 arg += 3;
1270 if (*arg == '&')
1271 {
1272 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001273 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001275 didset_options();
1276 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001277 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001282 did_show = TRUE;
1283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001285 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
1287 showoptions(2, opt_flags);
1288 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001289 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 arg += 7;
1291 }
1292 else
1293 {
1294 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001295 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
1297 prefix = 0;
1298 arg += 2;
1299 }
1300 else if (STRNCMP(arg, "inv", 3) == 0)
1301 {
1302 prefix = 2;
1303 arg += 3;
1304 }
1305
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 key = 0;
1308 if (*arg == '<')
1309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001311 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1313 len = 5;
1314 else
1315 {
1316 len = 1;
1317 while (arg[len] != NUL && arg[len] != '>')
1318 ++len;
1319 }
1320 if (arg[len] != '>')
1321 {
1322 errmsg = e_invarg;
1323 goto skip;
1324 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001325 arg[len] = NUL; // put NUL after name
1326 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001328 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001330 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 else
1333 {
1334 len = 0;
1335 /*
1336 * The two characters after "t_" may not be alphanumeric.
1337 */
1338 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1339 len = 4;
1340 else
1341 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1342 ++len;
1343 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001344 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001346 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001348 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001351 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 afterchar = arg[len];
1353
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001354 if (!in_vim9script())
1355 // skip white space, allow ":set ai ?", ":set hlsearch !"
1356 while (VIM_ISWHITE(arg[len]))
1357 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 adding = FALSE;
1360 prepending = FALSE;
1361 removing = FALSE;
1362 if (arg[len] != NUL && arg[len + 1] == '=')
1363 {
1364 if (arg[len] == '+')
1365 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001366 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 ++len;
1368 }
1369 else if (arg[len] == '^')
1370 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001371 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ++len;
1373 }
1374 else if (arg[len] == '-')
1375 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001376 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 ++len;
1378 }
1379 }
1380 nextchar = arg[len];
1381
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001382 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001384 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 goto skip;
1386 }
1387
1388 if (opt_idx >= 0)
1389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001392 // Only give an error message when requesting the value of
1393 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1395 && (!(options[opt_idx].flags & P_BOOL)
1396 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001397 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 goto skip;
1399 }
1400
1401 flags = options[opt_idx].flags;
1402 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1403 }
1404 else
1405 {
1406 flags = P_STRING;
1407 if (key < 0)
1408 {
1409 key_name[0] = KEY2TERMCAP0(key);
1410 key_name[1] = KEY2TERMCAP1(key);
1411 }
1412 else
1413 {
1414 key_name[0] = KS_KEY;
1415 key_name[1] = (key & 0xff);
1416 }
1417 }
1418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001419 // Skip all options that are not window-local (used when showing
1420 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001421 if ((opt_flags & OPT_WINONLY)
1422 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1423 goto skip;
1424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001426 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1427 && options[opt_idx].var == VAR_WIN)
1428 goto skip;
1429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001430 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001431 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001433 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001434 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001435 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001436 goto skip;
1437 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001438 if ((flags & P_MLE) && !p_mle)
1439 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001440 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001441 goto skip;
1442 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001443#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001444 // In diff mode some options are overruled. This avoids that
1445 // 'foldmethod' becomes "marker" instead of "diff" and that
1446 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001447 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001448 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001449 && (
1450#ifdef FEAT_FOLDING
1451 options[opt_idx].indir == PV_FDM ||
1452#endif
1453 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001454 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457
1458#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001459 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001460 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001462 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 goto skip;
1464 }
1465#endif
1466
1467 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1468 {
1469 arg += len;
1470 cp_val = p_cp;
1471 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1472 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001473 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 cp_val = FALSE;
1476 arg += 3;
1477 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001478 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
1480 cp_val = TRUE;
1481 arg += 2;
1482 }
1483 }
1484 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001485 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 {
1487 errmsg = e_trailing;
1488 goto skip;
1489 }
1490 }
1491
1492 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001493 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001494 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 if (nextchar == '?'
1497 || (prefix == 1
1498 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1499 && !(flags & P_BOOL)))
1500 {
1501 /*
1502 * print value
1503 */
1504 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001505 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 else
1507 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001508 gotocmdline(TRUE); // cursor at status line
1509 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 if (opt_idx >= 0)
1512 {
1513 showoneopt(&options[opt_idx], opt_flags);
1514#ifdef FEAT_EVAL
1515 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001516 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001517 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001518 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001519 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001520 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001521 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001522 (int)options[opt_idx].indir & PV_MASK]);
1523 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001524 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001525 (int)options[opt_idx].indir & PV_MASK]);
1526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#endif
1528 }
1529 else
1530 {
1531 char_u *p;
1532
1533 p = find_termcode(key_name);
1534 if (p == NULL)
1535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001536 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 goto skip;
1538 }
1539 else
1540 (void)show_one_termcode(key_name, p, TRUE);
1541 }
1542 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001543 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 errmsg = e_trailing;
1545 }
1546 else
1547 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001548 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001549 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001551 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
1553 if (nextchar == '=' || nextchar == ':')
1554 {
1555 errmsg = e_invarg;
1556 goto skip;
1557 }
1558
1559 /*
1560 * ":set opt!": invert
1561 * ":set opt&": reset to default value
1562 * ":set opt<": reset to global value
1563 */
1564 if (nextchar == '!')
1565 value = *(int *)(varp) ^ 1;
1566 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001567 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 ((flags & P_VI_DEF) || cp_val)
1569 ? VI_DEFAULT : VIM_DEFAULT];
1570 else if (nextchar == '<')
1571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001572 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if ((int *)varp == &curbuf->b_p_ar
1574 && opt_flags == OPT_LOCAL)
1575 value = -1;
1576 else
1577 value = *(int *)get_varp_scope(&(options[opt_idx]),
1578 OPT_GLOBAL);
1579 }
1580 else
1581 {
1582 /*
1583 * ":set invopt": invert
1584 * ":set opt" or ":set noopt": set or reset
1585 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001586 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588 errmsg = e_trailing;
1589 goto skip;
1590 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 value = *(int *)(varp) ^ 1;
1593 else
1594 value = prefix;
1595 }
1596
1597 errmsg = set_bool_option(opt_idx, varp, (int)value,
1598 opt_flags);
1599 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001600 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
1602 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1603 || prefix != 1)
1604 {
1605 errmsg = e_invarg;
1606 goto skip;
1607 }
1608
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001609 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 /*
1612 * Different ways to set a number option:
1613 * & set to default value
1614 * < set to global value
1615 * <xx> accept special key codes for 'wildchar'
1616 * c accept any non-digit for 'wildchar'
1617 * [-]0-9 set number
1618 * other error
1619 */
1620 ++arg;
1621 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001622 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 ((flags & P_VI_DEF) || cp_val)
1624 ? VI_DEFAULT : VIM_DEFAULT];
1625 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1628 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001629 if ((long *)varp == &curbuf->b_p_ul
1630 && opt_flags == OPT_LOCAL)
1631 value = NO_LOCAL_UNDOLEVEL;
1632 else
1633 value = *(long *)get_varp_scope(
1634 &(options[opt_idx]), OPT_GLOBAL);
1635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 else if (((long *)varp == &p_wc
1637 || (long *)varp == &p_wcm)
1638 && (*arg == '<'
1639 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001640 || (*arg != NUL
1641 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 && !VIM_ISDIGIT(*arg))))
1643 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001644 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (value == 0 && (long *)varp != &p_wcm)
1646 {
1647 errmsg = e_invarg;
1648 goto skip;
1649 }
1650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1652 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001653 // Allow negative (for 'undolevels'), octal and
1654 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001655 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001656 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001657 if (i == 0 || (arg[i] != NUL
1658 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001660 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 goto skip;
1662 }
1663 }
1664 else
1665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001666 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 goto skip;
1668 }
1669
1670 if (adding)
1671 value = *(long *)varp + value;
1672 if (prepending)
1673 value = *(long *)varp * value;
1674 if (removing)
1675 value = *(long *)varp - value;
1676 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001677 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001679 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001681 char_u *save_arg = NULL;
1682 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001683 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001684 char_u *newval;
1685 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001686 char_u *origval_l = NULL;
1687 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001688#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001689 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001690 char_u *saved_origval_l = NULL;
1691 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001692 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001693#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001694 unsigned newlen;
1695 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001696 int new_value_alloced; // new string option
1697 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001699 // When using ":set opt=val" for a global option
1700 // with a local value the local value will be
1701 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001703 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 varp = options[opt_idx].var;
1705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001706 // The old value is kept until we are sure that the
1707 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001709
Bram Moolenaard7c96872019-06-15 17:12:48 +02001710 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1711 {
1712 origval_l = *(char_u **)get_varp_scope(
1713 &(options[opt_idx]), OPT_LOCAL);
1714 origval_g = *(char_u **)get_varp_scope(
1715 &(options[opt_idx]), OPT_GLOBAL);
1716
1717 // A global-local string option might have an empty
1718 // option as value to indicate that the global
1719 // value should be used.
1720 if (((int)options[opt_idx].indir & PV_BOTH)
1721 && origval_l == empty_option)
1722 origval_l = origval_g;
1723 }
1724
1725 // When setting the local value of a global
1726 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001727 if (((int)options[opt_idx].indir & PV_BOTH)
1728 && (opt_flags & OPT_LOCAL))
1729 origval = *(char_u **)get_varp(
1730 &options[opt_idx]);
1731 else
1732 origval = oldval;
1733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001734 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
1736 newval = options[opt_idx].def_val[
1737 ((flags & P_VI_DEF) || cp_val)
1738 ? VI_DEFAULT : VIM_DEFAULT];
1739 if ((char_u **)varp == &p_bg)
1740 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001741 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#ifdef FEAT_GUI
1743 if (gui.in_use)
1744 newval = gui_bg_default();
1745 else
1746#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001747 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001749 else if ((char_u **)varp == &p_fencs && enc_utf8)
1750 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001752 // expand environment variables and ~ (since the
1753 // default value was already expanded, only
1754 // required when an environment variable was set
1755 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (newval == NULL)
1757 newval = empty_option;
1758 else
1759 {
1760 s = option_expand(opt_idx, newval);
1761 if (s == NULL)
1762 s = newval;
1763 newval = vim_strsave(s);
1764 }
1765 new_value_alloced = TRUE;
1766 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001767 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
1769 newval = vim_strsave(*(char_u **)get_varp_scope(
1770 &(options[opt_idx]), OPT_GLOBAL));
1771 new_value_alloced = TRUE;
1772 }
1773 else
1774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001775 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 /*
1778 * Set 'keywordprg' to ":help" if an empty
1779 * value was passed to :set by the user.
1780 * Misuse errbuf[] for the resulting string.
1781 */
1782 if (varp == (char_u *)&p_kp
1783 && (*arg == NUL || *arg == ' '))
1784 {
1785 STRCPY(errbuf, ":help");
1786 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001787 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 }
1789 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001790 * Convert 'backspace' number to string, for
1791 * adding, prepending and removing string.
1792 */
1793 else if (varp == (char_u *)&p_bs
1794 && VIM_ISDIGIT(**(char_u **)varp))
1795 {
1796 i = getdigits((char_u **)varp);
1797 switch (i)
1798 {
1799 case 0:
1800 *(char_u **)varp = empty_option;
1801 break;
1802 case 1:
1803 *(char_u **)varp = vim_strsave(
1804 (char_u *)"indent,eol");
1805 break;
1806 case 2:
1807 *(char_u **)varp = vim_strsave(
1808 (char_u *)"indent,eol,start");
1809 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001810 case 3:
1811 *(char_u **)varp = vim_strsave(
1812 (char_u *)"indent,eol,nostop");
1813 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001814 }
1815 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001816 if (origval == oldval)
1817 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001818 if (origval_l == oldval)
1819 origval_l = *(char_u **)varp;
1820 if (origval_g == oldval)
1821 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001822 oldval = *(char_u **)varp;
1823 }
1824 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 * Convert 'whichwrap' number to string, for
1826 * backwards compatibility with Vim 3.0.
1827 * Misuse errbuf[] for the resulting string.
1828 */
1829 else if (varp == (char_u *)&p_ww
1830 && VIM_ISDIGIT(*arg))
1831 {
1832 *errbuf = NUL;
1833 i = getdigits(&arg);
1834 if (i & 1)
1835 STRCAT(errbuf, "b,");
1836 if (i & 2)
1837 STRCAT(errbuf, "s,");
1838 if (i & 4)
1839 STRCAT(errbuf, "h,l,");
1840 if (i & 8)
1841 STRCAT(errbuf, "<,>,");
1842 if (i & 16)
1843 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001844 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 errbuf[STRLEN(errbuf) - 1] = NUL;
1846 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001847 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 /*
1850 * Remove '>' before 'dir' and 'bdir', for
1851 * backwards compatibility with version 3.0
1852 */
1853 else if ( *arg == '>'
1854 && (varp == (char_u *)&p_dir
1855 || varp == (char_u *)&p_bdir))
1856 {
1857 ++arg;
1858 }
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 /*
1861 * Copy the new string into allocated memory.
1862 * Can't use set_string_option_direct(), because
1863 * we need to remove the backslashes.
1864 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001865 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 newlen = (unsigned)STRLEN(arg) + 1;
1867 if (adding || prepending || removing)
1868 newlen += (unsigned)STRLEN(origval) + 1;
1869 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001870 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 break;
1872 s = newval;
1873
1874 /*
1875 * Copy the string, skip over escaped chars.
1876 * For MS-DOS and WIN32 backslashes before normal
1877 * file name characters are not removed, and keep
1878 * backslash at start, for "\\machine\path", but
1879 * do remove it for "\\\\machine\\path".
1880 * The reverse is found in ExpandOldSetting().
1881 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001882 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 if (*arg == '\\' && arg[1] != NUL
1885#ifdef BACKSLASH_IN_FILENAME
1886 && !((flags & P_EXPAND)
1887 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001888 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 && (arg[1] != '\\'
1890 || (s == newval
1891 && arg[2] != '\\')))
1892#endif
1893 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001894 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001896 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001898 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 mch_memmove(s, arg, (size_t)i);
1900 arg += i;
1901 s += i;
1902 }
1903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 *s++ = *arg++;
1905 }
1906 *s = NUL;
1907
1908 /*
1909 * Expand environment variables and ~.
1910 * Don't do it when adding without inserting a
1911 * comma.
1912 */
1913 if (!(adding || prepending || removing)
1914 || (flags & P_COMMA))
1915 {
1916 s = option_expand(opt_idx, newval);
1917 if (s != NULL)
1918 {
1919 vim_free(newval);
1920 newlen = (unsigned)STRLEN(s) + 1;
1921 if (adding || prepending || removing)
1922 newlen += (unsigned)STRLEN(origval) + 1;
1923 newval = alloc(newlen);
1924 if (newval == NULL)
1925 break;
1926 STRCPY(newval, s);
1927 }
1928 }
1929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001930 // locate newval[] in origval[] when removing it
1931 // and when adding to avoid duplicates
1932 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (removing || (flags & P_NODUP))
1934 {
1935 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001936 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001938 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001939 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 prepending = FALSE;
1942 adding = FALSE;
1943 STRCPY(newval, origval);
1944 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001945
1946 // if no duplicate, move pointer to end of
1947 // original value
1948 if (s == NULL)
1949 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001952 // concatenate the two strings; add a ',' if
1953 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (adding || prepending)
1955 {
1956 comma = ((flags & P_COMMA) && *origval != NUL
1957 && *newval != NUL);
1958 if (adding)
1959 {
1960 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001962 if (comma && i > 1
1963 && (flags & P_ONECOMMA) == P_ONECOMMA
1964 && origval[i - 1] == ','
1965 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001966 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 mch_memmove(newval + i + comma, newval,
1968 STRLEN(newval) + 1);
1969 mch_memmove(newval, origval, (size_t)i);
1970 }
1971 else
1972 {
1973 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001974 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 if (comma)
1977 newval[i] = ',';
1978 }
1979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001980 // Remove newval[] from origval[]. (Note: "i" has
1981 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (removing)
1983 {
1984 STRCPY(newval, origval);
1985 if (*s)
1986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001987 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (flags & P_COMMA)
1989 {
1990 if (s == origval)
1991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001992 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (s[i] == ',')
1994 ++i;
1995 }
1996 else
1997 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001998 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 --s;
2000 ++i;
2001 }
2002 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002003 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
2005 }
2006
2007 if (flags & P_FLAGLIST)
2008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002009 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002010 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002012 // if options have P_FLAGLIST and
2013 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002014 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002016 if (*s != ',' && *(s + 1) == ','
2017 && vim_strchr(s + 2, *s) != NULL)
2018 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002019 // Remove the duplicated value and
2020 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002021 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002022 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002025 else
2026 {
2027 if ((!(flags & P_COMMA) || *s != ',')
2028 && vim_strchr(s + 1, *s) != NULL)
2029 {
2030 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002031 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002032 }
2033 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002034 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002038 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 arg = save_arg;
2040 new_value_alloced = TRUE;
2041 }
2042
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002043 /*
2044 * Set the new value.
2045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 *(char_u **)(varp) = newval;
2047
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002048#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002049 if (!starting
2050# ifdef FEAT_CRYPT
2051 && options[opt_idx].indir != PV_KEY
2052# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002053 && origval != NULL && newval != NULL)
2054 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002055 // origval may be freed by
2056 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002057 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 // newval (and varp) may become invalid if the
2059 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002060 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002061 if (origval_l != NULL)
2062 saved_origval_l = vim_strsave(origval_l);
2063 if (origval_g != NULL)
2064 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002065 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002066#endif
2067
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002068 {
2069 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002070 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002071
2072 // When an option is set in the sandbox, from a
2073 // modeline or in secure mode, then deal with side
2074 // effects in secure mode. Also when the value was
2075 // set with the P_INSECURE flag and is not
2076 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002077 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002078#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002079 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002080#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002081 || (!value_is_replaced && (*p & P_INSECURE)))
2082 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002083
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002084 // Handle side effects, and set the global value
2085 // for ":set" on local options. Note: when setting
2086 // 'syntax' or 'filetype' autocommands may be
2087 // triggered that can cause havoc.
2088 errmsg = did_set_string_option(
2089 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002090 new_value_alloced, oldval, errbuf,
2091 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002092
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002093 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002096#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002097 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002098 trigger_optionsset_string(
2099 opt_idx, opt_flags, saved_origval,
2100 saved_origval_l, saved_origval_g,
2101 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002102 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002103 vim_free(saved_origval_l);
2104 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002105 vim_free(saved_newval);
2106#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002107 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (errmsg != NULL)
2109 goto skip;
2110 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002111 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
2113 char_u *p;
2114
2115 if (nextchar == '&')
2116 {
2117 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002118 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120 else
2121 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002122 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002123 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (*p == '\\' && p[1] != NUL)
2125 ++p;
2126 nextchar = *p;
2127 *p = NUL;
2128 add_termcode(key_name, arg, FALSE);
2129 *p = nextchar;
2130 }
2131 if (full_screen)
2132 ttest(FALSE);
2133 redraw_all_later(CLEAR);
2134 }
2135 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002136
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002138 did_set_option(
2139 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141
2142skip:
2143 /*
2144 * Advance to next argument.
2145 * - skip until a blank found, taking care of backslashes
2146 * - skip blanks
2147 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2148 */
2149 for (i = 0; i < 2 ; ++i)
2150 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002151 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (*arg++ == '\\' && *arg != NUL)
2153 ++arg;
2154 arg = skipwhite(arg);
2155 if (*arg != '=')
2156 break;
2157 }
2158 }
2159
2160 if (errmsg != NULL)
2161 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002162 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002163 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (i + (arg - startarg) < IOSIZE)
2165 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002166 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 STRCAT(IObuff, ": ");
2168 mch_memmove(IObuff + i, startarg, (arg - startarg));
2169 IObuff[i + (arg - startarg)] = NUL;
2170 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002171 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 trans_characters(IObuff, IOSIZE);
2173
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002174 ++no_wait_return; // wait_return done later
2175 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 --no_wait_return;
2177
2178 return FAIL;
2179 }
2180
2181 arg = skipwhite(arg);
2182 }
2183
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002184theend:
2185 if (silent_mode && did_show)
2186 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002187 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002188 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002189 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002190 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002191 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002192 out_flush();
2193 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002195 }
2196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 return OK;
2198}
2199
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002200/*
2201 * Call this when an option has been given a new value through a user command.
2202 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2203 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002205did_set_option(
2206 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002207 int opt_flags, // possibly with OPT_MODELINE
2208 int new_value, // value was replaced completely
2209 int value_checked) // value was checked to be safe, no need to set the
2210 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002211{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002212 long_u *p;
2213
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002214 options[opt_idx].flags |= P_WAS_SET;
2215
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002216 // When an option is set in the sandbox, from a modeline or in secure mode
2217 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2218 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002219 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002220 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002221#ifdef HAVE_SANDBOX
2222 || sandbox != 0
2223#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002224 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002225 *p = *p | P_INSECURE;
2226 else if (new_value)
2227 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002228}
2229
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230/*
2231 * Convert a key name or string into a key value.
2232 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002233 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002235 int
2236string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
2238 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002239 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (*arg == '^')
2241 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002242 if (multi_byte)
2243 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 return *arg;
2245}
2246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247#ifdef FEAT_TITLE
2248/*
2249 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2250 * maketitle() to create and display it.
2251 * When switching the title or icon off, call mch_restore_title() to get
2252 * the old value back.
2253 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002254 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002255did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 if (starting != NO_SCREEN
2258#ifdef FEAT_GUI
2259 && !gui.starting
2260#endif
2261 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263}
2264#endif
2265
2266/*
2267 * set_options_bin - called when 'bin' changes value.
2268 */
2269 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002270set_options_bin(
2271 int oldval,
2272 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 /*
2276 * The option values that are changed when 'bin' changes are
2277 * copied when 'bin is set and restored when 'bin' is reset.
2278 */
2279 if (newval)
2280 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002281 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 if (!(opt_flags & OPT_GLOBAL))
2284 {
2285 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2286 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2287 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2288 curbuf->b_p_et_nobin = curbuf->b_p_et;
2289 }
2290 if (!(opt_flags & OPT_LOCAL))
2291 {
2292 p_tw_nobin = p_tw;
2293 p_wm_nobin = p_wm;
2294 p_ml_nobin = p_ml;
2295 p_et_nobin = p_et;
2296 }
2297 }
2298
2299 if (!(opt_flags & OPT_GLOBAL))
2300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002301 curbuf->b_p_tw = 0; // no automatic line wrap
2302 curbuf->b_p_wm = 0; // no automatic line wrap
2303 curbuf->b_p_ml = 0; // no modelines
2304 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306 if (!(opt_flags & OPT_LOCAL))
2307 {
2308 p_tw = 0;
2309 p_wm = 0;
2310 p_ml = FALSE;
2311 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002312 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002315 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 if (!(opt_flags & OPT_GLOBAL))
2318 {
2319 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2320 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2321 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2322 curbuf->b_p_et = curbuf->b_p_et_nobin;
2323 }
2324 if (!(opt_flags & OPT_LOCAL))
2325 {
2326 p_tw = p_tw_nobin;
2327 p_wm = p_wm_nobin;
2328 p_ml = p_ml_nobin;
2329 p_et = p_et_nobin;
2330 }
2331 }
2332}
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334/*
2335 * Expand environment variables for some string options.
2336 * These string options cannot be indirect!
2337 * If "val" is NULL expand the current value of the option.
2338 * Return pointer to NameBuff, or NULL when not expanded.
2339 */
2340 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002341option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2345 return NULL;
2346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 // If val is longer than MAXPATHL no meaningful expansion can be done,
2348 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (val != NULL && STRLEN(val) > MAXPATHL)
2350 return NULL;
2351
2352 if (val == NULL)
2353 val = *(char_u **)options[opt_idx].var;
2354
2355 /*
2356 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2357 * Escape spaces when expanding 'tags', they are used to separate file
2358 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002359 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
2361 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002362 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002363#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002364 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2365#endif
2366 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002367 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 return NULL;
2369
2370 return NameBuff;
2371}
2372
2373/*
2374 * After setting various option values: recompute variables that depend on
2375 * option values.
2376 */
2377 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002378didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 (void)init_chartab();
2382
Bram Moolenaardac13472019-09-16 21:06:21 +02002383 didset_string_options();
2384
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002385#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002386 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002387 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002388 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002389 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002392 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 (void)check_cedit();
2394#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002395#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002396 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002397 fill_breakat_flags();
2398#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002399 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002400}
2401
2402/*
2403 * More side effects of setting options.
2404 */
2405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002406didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002407{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002408 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002409 (void)highlight_changed();
2410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002411 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002412 check_opt_wim();
2413
Bram Moolenaareed9d462021-02-15 20:38:25 +01002414 // Parse default for 'listchars'.
2415 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002418 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002419
2420#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002421 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002422 (void)check_clipboard_option();
2423#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002424#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002425 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002426 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002427 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002428 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
2432/*
2433 * Check for string options that are NULL (normally only termcap options).
2434 */
2435 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002436check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 int opt_idx;
2439
2440 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2441 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2442 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2443}
2444
2445/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002446 * Return the option index found by a pointer into term_strings[].
2447 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002449 int
2450get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002452 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2455 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002456 return opt_idx;
2457 return -1; // cannot happen: didn't find it!
2458}
2459
2460/*
2461 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2462 * Return the option index or -1 if not found.
2463 */
2464 int
2465set_term_option_alloced(char_u **p)
2466{
2467 int opt_idx = get_term_opt_idx(p);
2468
2469 if (opt_idx >= 0)
2470 options[opt_idx].flags |= P_ALLOCED;
2471 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472}
2473
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002474#if defined(FEAT_EVAL) || defined(PROTO)
2475/*
2476 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2477 * Return FALSE when it wasn't.
2478 * Return -1 for an unknown option.
2479 */
2480 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002481was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002482{
2483 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002484 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002485
2486 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002487 {
2488 flagp = insecure_flag(idx, opt_flags);
2489 return (*flagp & P_INSECURE) != 0;
2490 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002491 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002492 return -1;
2493}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002494
2495/*
2496 * Get a pointer to the flags used for the P_INSECURE flag of option
2497 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002498 * NOTE: Caller must make sure that "curwin" is set to the window from which
2499 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500 */
2501 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002502insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002503{
2504 if (opt_flags & OPT_LOCAL)
2505 switch ((int)options[opt_idx].indir)
2506 {
2507#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002508 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002509#endif
2510#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002511# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002512 case PV_FDE: return &curwin->w_p_fde_flags;
2513 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002514# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002515# ifdef FEAT_BEVAL
2516 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2517# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002518# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002519 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002520# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002521 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002522# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002523 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002524# endif
2525#endif
2526 }
2527
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002528 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002529 return &options[opt_idx].flags;
2530}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002531#endif
2532
Bram Moolenaardac13472019-09-16 21:06:21 +02002533#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002534/*
2535 * Redraw the window title and/or tab page text later.
2536 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002537void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002538{
2539 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002540 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002541}
2542#endif
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002545 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2546 * characters or characters in "allowed".
2547 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002548 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002549valid_name(char_u *val, char *allowed)
2550{
2551 char_u *s;
2552
2553 for (s = val; *s != NUL; ++s)
2554 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2555 return FALSE;
2556 return TRUE;
2557}
2558
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002559#if defined(FEAT_EVAL) || defined(PROTO)
2560/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002561 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002562 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002563 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002564 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002565set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002566{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002567 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2568 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002569 sctx_T new_script_ctx = script_ctx;
2570
Bram Moolenaar51258742020-05-03 17:19:33 +02002571 // Modeline already has the line number set.
2572 if (!(opt_flags & OPT_MODELINE))
2573 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002575 // Remember where the option was set. For local options need to do that
2576 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002577 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002578 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002579 if (both || (opt_flags & OPT_LOCAL))
2580 {
2581 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002582 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002583 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002584 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002585 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002586}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002587
2588/*
2589 * Set the script_ctx for a termcap option.
2590 * "name" must be the two character code, e.g. "RV".
2591 * When "name" is NULL use "opt_idx".
2592 */
2593 void
2594set_term_option_sctx_idx(char *name, int opt_idx)
2595{
2596 char_u buf[5];
2597 int idx;
2598
2599 if (name == NULL)
2600 idx = opt_idx;
2601 else
2602 {
2603 buf[0] = 't';
2604 buf[1] = '_';
2605 buf[2] = name[0];
2606 buf[3] = name[1];
2607 buf[4] = 0;
2608 idx = findoption(buf);
2609 }
2610 if (idx >= 0)
2611 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2612}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002613#endif
2614
Bram Moolenaarf4140482020-02-15 23:06:45 +01002615#if defined(FEAT_EVAL)
2616/*
2617 * Apply the OptionSet autocommand.
2618 */
2619 static void
2620apply_optionset_autocmd(
2621 int opt_idx,
2622 long opt_flags,
2623 long oldval,
2624 long oldval_g,
2625 long newval,
2626 char *errmsg)
2627{
2628 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2629
2630 // Don't do this while starting up, failure or recursively.
2631 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2632 return;
2633
2634 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2635 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2636 oldval_g);
2637 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2638 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2639 (opt_flags & OPT_LOCAL) ? "local" : "global");
2640 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2641 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2642 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2643 if (opt_flags & OPT_LOCAL)
2644 {
2645 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2646 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2647 }
2648 if (opt_flags & OPT_GLOBAL)
2649 {
2650 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2651 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2652 }
2653 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2654 {
2655 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2656 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2657 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2658 }
2659 if (opt_flags & OPT_MODELINE)
2660 {
2661 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2662 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2663 }
2664 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2665 NULL, FALSE, NULL);
2666 reset_v_option_vars();
2667}
2668#endif
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
2671 * Set the value of a boolean option, and take care of side effects.
2672 * Returns NULL for success, or an error message for an error.
2673 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002675set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002676 int opt_idx, // index in options[] table
2677 char_u *varp, // pointer to the option variable
2678 int value, // new value
2679 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002682#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002683 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002686 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if ((secure
2688#ifdef HAVE_SANDBOX
2689 || sandbox != 0
2690#endif
2691 ) && (options[opt_idx].flags & P_SECURE))
2692 return e_secure;
2693
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002694#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002695 // Save the global value before changing anything. This is needed as for
2696 // a global-only option setting the "local value" in fact sets the global
2697 // value (since there is only one value).
2698 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2699 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2700 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002701#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002702
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002706 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707#endif
2708
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002709#ifdef FEAT_GUI
2710 need_mouse_correct = TRUE;
2711#endif
2712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2715 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2716
2717 /*
2718 * Handle side effects of changing a bool option.
2719 */
2720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
Bram Moolenaar920694c2016-08-21 17:45:02 +02002725#ifdef FEAT_LANGMAP
2726 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002728 p_lnr = !p_lrm;
2729 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002731 p_lrm = !p_lnr;
2732#endif
2733
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002734#ifdef FEAT_SYN_HL
2735 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2736 reset_cursorline();
2737#endif
2738
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002739#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002741 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // Only take action when the option was set. When reset we do not
2744 // delete the undo file, the option may be set again without making
2745 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002746 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002747 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002748 char_u hash[UNDO_HASH_SIZE];
2749 buf_T *save_curbuf = curbuf;
2750
Bram Moolenaar29323592016-07-24 22:04:11 +02002751 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002752 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // When 'undofile' is set globally: for every buffer, otherwise
2754 // only for the current buffer: Try to read in the undofile,
2755 // if one exists, the buffer wasn't changed and the buffer was
2756 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002757 if ((curbuf == save_curbuf
2758 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2759 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2760 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002761#ifdef FEAT_CRYPT
2762 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2763 continue;
2764#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002765 u_compute_hash(hash);
2766 u_read_undo(NULL, hash, curbuf->b_fname);
2767 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002768 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002769 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002770 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002771 }
2772#endif
2773
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else if ((int *)varp == &curbuf->b_p_ro)
2775 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002776 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2778 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002781 if (curbuf->b_p_ro)
2782 curbuf->b_did_warn = FALSE;
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002785 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786#endif
2787 }
2788
Bram Moolenaar9c449722010-07-20 18:44:27 +02002789#ifdef FEAT_GUI
2790 else if ((int *)varp == &p_mh)
2791 {
2792 if (!p_mh)
2793 gui_mch_mousehide(FALSE);
2794 }
2795#endif
2796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002799 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002800# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002802 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2803 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002804 {
2805 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002806 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002807 }
2808# endif
2809# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002810 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002811# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002812 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002813#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002814 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002816 {
2817 redraw_titles();
2818 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002819 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002820 else if ((int *)varp == &curbuf->b_p_fixeol)
2821 {
2822 redraw_titles();
2823 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002825 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002826 {
2827 redraw_titles();
2828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#endif
2830
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 else if ((int *)varp == &curbuf->b_p_bin)
2833 {
2834 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2835#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002836 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#endif
2838 }
2839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2842 {
2843 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2844 NULL, NULL, TRUE, curbuf);
2845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 else if ((int *)varp == &curbuf->b_p_swf)
2849 {
2850 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2854 // buf->b_p_swf
2855 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 else if ((int *)varp == &p_terse)
2860 {
2861 char_u *p;
2862
2863 p = vim_strchr(p_shm, SHM_SEARCH);
2864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (p_terse && p == NULL)
2867 {
2868 STRCPY(IObuff, p_shm);
2869 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002870 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002874 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else if ((int *)varp == &p_paste)
2879 {
2880 paste_option_changed();
2881 }
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else if ((int *)varp == &p_im)
2885 {
2886 if (p_im)
2887 {
2888 if ((State & INSERT) == 0)
2889 need_start_insertmode = TRUE;
2890 stop_insert_mode = FALSE;
2891 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002893 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
2895 need_start_insertmode = FALSE;
2896 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002897 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 restart_edit = 0;
2900 }
2901 }
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else if ((int *)varp == &p_ic && p_hls)
2905 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002906 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908
2909#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 else if ((int *)varp == &p_hls)
2912 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002913 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915#endif
2916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2918 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 else if ((int *)varp == &curwin->w_p_scb)
2920 {
2921 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002924 curwin->w_scbind_pos = curwin->w_topline;
2925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
Bram Moolenaar4033c552017-09-16 20:54:51 +02002928#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 else if ((int *)varp == &curwin->w_p_pvw)
2931 {
2932 if (curwin->w_p_pvw)
2933 {
2934 win_T *win;
2935
Bram Moolenaar29323592016-07-24 22:04:11 +02002936 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 if (win->w_p_pvw && win != curwin)
2938 {
2939 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002940 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
2942 }
2943 }
2944#endif
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 else if ((int *)varp == &curbuf->b_p_tx)
2948 {
2949 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2950 }
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 set_string_option_direct((char_u *)"ffs", -1,
2956 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002957 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /*
2961 * When 'lisp' option changes include/exclude '-' in
2962 * keyword characters.
2963 */
2964#ifdef FEAT_LISP
2965 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969#endif
2970
2971#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002973 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002975 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977#endif
2978
2979 else if ((int *)varp == &curbuf->b_changed)
2980 {
2981 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002984 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
2988
2989#ifdef BACKSLASH_IN_FILENAME
2990 else if ((int *)varp == &p_ssl)
2991 {
2992 if (p_ssl)
2993 {
2994 psepc = '/';
2995 psepcN = '\\';
2996 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998 else
2999 {
3000 psepc = '\\';
3001 psepcN = '/';
3002 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 buflist_slash_adjust();
3007 alist_slash_adjust();
3008# ifdef FEAT_EVAL
3009 scriptnames_slash_adjust();
3010# endif
3011 }
3012#endif
3013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003014 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 else if ((int *)varp == &curwin->w_p_wrap)
3016 {
3017 if (curwin->w_p_wrap)
3018 curwin->w_leftcol = 0;
3019 }
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 else if ((int *)varp == &p_ea)
3022 {
3023 if (p_ea && !old_value)
3024 win_equal(curwin, FALSE, 0);
3025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027 else if ((int *)varp == &p_wiv)
3028 {
3029 /*
3030 * When 'weirdinvert' changed, set/reset 't_xs'.
3031 * Then set 'weirdinvert' according to value of 't_xs'.
3032 */
3033 if (p_wiv && !old_value)
3034 T_XS = (char_u *)"y";
3035 else if (!p_wiv && old_value)
3036 T_XS = empty_option;
3037 p_wiv = (*T_XS != NUL);
3038 }
3039
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003040#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else if ((int *)varp == &p_beval)
3042 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003043 if (!balloonEvalForTerm)
3044 {
3045 if (p_beval && !old_value)
3046 gui_mch_enable_beval_area(balloonEval);
3047 else if (!p_beval && old_value)
3048 gui_mch_disable_beval_area(balloonEval);
3049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003051#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003052#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003053 else if ((int *)varp == &p_bevalterm)
3054 {
3055 mch_bevalterm_changed();
3056 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003059#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 else if ((int *)varp == &p_acd)
3061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003062 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003063 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 }
3065#endif
3066
3067#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 else if ((int *)varp == &curwin->w_p_diff)
3070 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003071 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003072 diff_buf_adjust(curwin);
3073# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (foldmethodIsDiff(curwin))
3075 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
3078#endif
3079
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003080#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 else if ((int *)varp == &p_imdisable)
3083 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (p_imdisable)
3086 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003087 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // When the option is set from an autocommand, it may need to take
3089 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003090 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092#endif
3093
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003094#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003096 else if ((int *)varp == &curwin->w_p_spell)
3097 {
3098 if (curwin->w_p_spell)
3099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003100 char *errmsg = did_set_spelllang(curwin);
3101
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003102 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003103 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003104 }
3105 }
3106#endif
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#ifdef FEAT_ARABIC
3109 if ((int *)varp == &curwin->w_p_arab)
3110 {
3111 if (curwin->w_p_arab)
3112 {
3113 /*
3114 * 'arabic' is set, handle various sub-settings.
3115 */
3116 if (!p_tbidi)
3117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (!curwin->w_p_rl)
3120 {
3121 curwin->w_p_rl = TRUE;
3122 changed_window_setting();
3123 }
3124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003125 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (!p_arshape)
3127 {
3128 p_arshape = TRUE;
3129 redraw_later_clear();
3130 }
3131 }
3132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // Arabic requires a utf-8 encoding, inform the user if its not
3134 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003136 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003137 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3138
Bram Moolenaar8820b482017-03-16 17:23:31 +01003139 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003140 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003141#ifdef FEAT_EVAL
3142 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3143#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003150 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3152 OPT_LOCAL);
3153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
3155 else
3156 {
3157 /*
3158 * 'arabic' is reset, handle various sub-settings.
3159 */
3160 if (!p_tbidi)
3161 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003162 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (curwin->w_p_rl)
3164 {
3165 curwin->w_p_rl = FALSE;
3166 changed_window_setting();
3167 }
3168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003169 // 'arabicshape' isn't reset, it is a global option and
3170 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // 'delcombine' isn't reset, it is a global option and another
3174 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 curbuf->b_p_iminsert = B_IMODE_NONE;
3179 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3180# endif
3181 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003182 }
3183
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003184#endif
3185
Bram Moolenaar44826212019-08-22 21:23:20 +02003186#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3187 else if (((int *)varp == &curwin->w_p_nu
3188 || (int *)varp == &curwin->w_p_rnu)
3189 && gui.in_use
3190 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3191 && curbuf->b_signlist != NULL)
3192 {
3193 // If the 'number' or 'relativenumber' options are modified and
3194 // 'signcolumn' is set to 'number', then clear the screen for a full
3195 // refresh. Otherwise the sign icons are not displayed properly in the
3196 // number column. If the 'number' option is set and only the
3197 // 'relativenumber' option is toggled, then don't refresh the screen
3198 // (optimization).
3199 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3200 redraw_all_later(CLEAR);
3201 }
3202#endif
3203
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003204#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003206 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003207 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003208# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003209 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003210 if (
3211# ifdef VIMDLL
3212 !gui.in_use && !gui.starting &&
3213# endif
3214 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003215 {
3216 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003217 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003218 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003219 if (is_term_win32())
3220 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003221# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003222# ifdef FEAT_GUI
3223 if (!gui.in_use && !gui.starting)
3224# endif
3225 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003226# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003227 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003228 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003229 {
3230 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003231 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003232 init_highlight(TRUE, FALSE);
3233 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003234# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003235 }
3236#endif
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 /*
3239 * End of handling side effects for bool options.
3240 */
3241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003242 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 options[opt_idx].flags |= P_WAS_SET;
3245
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003246#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003247 apply_optionset_autocmd(opt_idx, opt_flags,
3248 (long)(old_value ? TRUE : FALSE),
3249 (long)(old_global_value ? TRUE : FALSE),
3250 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003251#endif
3252
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003253 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003254 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003255 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003256 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003257
3258 if ((opt_flags & OPT_NO_REDRAW) == 0)
3259 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 return NULL;
3262}
3263
3264/*
3265 * Set the value of a number option, and take care of side effects.
3266 * Returns NULL for success, or an error message for an error.
3267 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003268 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003269set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003270 int opt_idx, // index in options[] table
3271 char_u *varp, // pointer to the option variable
3272 long value, // new value
3273 char *errbuf, // buffer for error messages
3274 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003275 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3276 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003278 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003280#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003281 long old_global_value = 0; // only used when setting a local and
3282 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003283#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003284 long old_Rows = Rows; // remember old Rows
3285 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 long *pp = (long *)varp;
3287
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003288 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003289 if ((secure
3290#ifdef HAVE_SANDBOX
3291 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003293 ) && (options[opt_idx].flags & P_SECURE))
3294 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003296#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003297 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003298 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003299 // value (since there is only one value).
3300 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003301 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3302 OPT_GLOBAL);
3303#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 *pp = value;
3306#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003307 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003308 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003310#ifdef FEAT_GUI
3311 need_mouse_correct = TRUE;
3312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaar14f24742012-08-08 18:01:05 +02003314 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
3316 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003317#ifdef FEAT_VARTABS
3318 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3319 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3320 ? tabstop_first(curbuf->b_p_vts_array)
3321 : curbuf->b_p_ts;
3322#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326
3327 /*
3328 * Number options that need some action when changed
3329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 if (pp == &p_wh || pp == &p_hh)
3331 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003332 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (p_wh < 1)
3334 {
3335 errmsg = e_positive;
3336 p_wh = 1;
3337 }
3338 if (p_wmh > p_wh)
3339 {
3340 errmsg = e_winheight;
3341 p_wh = p_wmh;
3342 }
3343 if (p_hh < 0)
3344 {
3345 errmsg = e_positive;
3346 p_hh = 0;
3347 }
3348
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003349 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003350 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
3352 if (pp == &p_wh && curwin->w_height < p_wh)
3353 win_setheight((int)p_wh);
3354 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3355 win_setheight((int)p_hh);
3356 }
3357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else if (pp == &p_wmh)
3359 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003360 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 if (p_wmh < 0)
3362 {
3363 errmsg = e_positive;
3364 p_wmh = 0;
3365 }
3366 if (p_wmh > p_wh)
3367 {
3368 errmsg = e_winheight;
3369 p_wmh = p_wh;
3370 }
3371 win_setminheight();
3372 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003373 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003375 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (p_wiw < 1)
3377 {
3378 errmsg = e_positive;
3379 p_wiw = 1;
3380 }
3381 if (p_wmw > p_wiw)
3382 {
3383 errmsg = e_winwidth;
3384 p_wiw = p_wmw;
3385 }
3386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003387 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003388 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 win_setwidth((int)p_wiw);
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else if (pp == &p_wmw)
3392 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003393 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (p_wmw < 0)
3395 {
3396 errmsg = e_positive;
3397 p_wmw = 0;
3398 }
3399 if (p_wmw > p_wiw)
3400 {
3401 errmsg = e_winwidth;
3402 p_wmw = p_wiw;
3403 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003404 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003407 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 else if (pp == &p_ls)
3409 {
3410 last_status(FALSE);
3411 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003413 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003414 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003416 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419#ifdef FEAT_GUI
3420 else if (pp == &p_linespace)
3421 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003422 // Recompute gui.char_height and resize the Vim window to keep the
3423 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003424 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003425 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427#endif
3428
3429#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003430 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 else if (pp == &curwin->w_p_fdl)
3432 {
3433 if (curwin->w_p_fdl < 0)
3434 curwin->w_p_fdl = 0;
3435 newFoldLevel();
3436 }
3437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003438 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 else if (pp == &curwin->w_p_fml)
3440 {
3441 foldUpdateAll(curwin);
3442 }
3443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else if (pp == &curwin->w_p_fdn)
3446 {
3447 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3448 foldUpdateAll(curwin);
3449 }
3450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003451 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 else if (pp == &curwin->w_p_fdc)
3453 {
3454 if (curwin->w_p_fdc < 0)
3455 {
3456 errmsg = e_positive;
3457 curwin->w_p_fdc = 0;
3458 }
3459 else if (curwin->w_p_fdc > 12)
3460 {
3461 errmsg = e_invarg;
3462 curwin->w_p_fdc = 12;
3463 }
3464 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003467#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3470 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003471# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (foldmethodIsIndent(curwin))
3473 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003474# endif
3475# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003476 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3477 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003478 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3479 parse_cino(curbuf);
3480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003484 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003485 else if (pp == &p_mco)
3486 {
3487 if (p_mco > MAX_MCO)
3488 p_mco = MAX_MCO;
3489 else if (p_mco < 0)
3490 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003491 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003492 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 else if (pp == &curbuf->b_p_iminsert)
3495 {
3496 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3497 {
3498 errmsg = e_invarg;
3499 curbuf->b_p_iminsert = B_IMODE_NONE;
3500 }
3501 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003502 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003504#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003505 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 status_redraw_curbuf();
3507#endif
3508 }
3509
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003510#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003511 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003512 else if (pp == &p_imst)
3513 {
3514 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3515 errmsg = e_invarg;
3516 }
3517#endif
3518
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003519 else if (pp == &p_window)
3520 {
3521 if (p_window < 1)
3522 p_window = 1;
3523 else if (p_window >= Rows)
3524 p_window = Rows - 1;
3525 }
3526
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 else if (pp == &curbuf->b_p_imsearch)
3528 {
3529 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3530 {
3531 errmsg = e_invarg;
3532 curbuf->b_p_imsearch = B_IMODE_NONE;
3533 }
3534 p_imsearch = curbuf->b_p_imsearch;
3535 }
3536
3537#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003538 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 else if (pp == &p_titlelen)
3540 {
3541 if (p_titlelen < 0)
3542 {
3543 errmsg = e_positive;
3544 p_titlelen = 85;
3545 }
3546 if (starting != NO_SCREEN && old_value != p_titlelen)
3547 need_maketitle = TRUE;
3548 }
3549#endif
3550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003551 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else if (pp == &p_ch)
3553 {
3554 if (p_ch < 1)
3555 {
3556 errmsg = e_positive;
3557 p_ch = 1;
3558 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003559 if (p_ch > Rows - min_rows() + 1)
3560 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003562 // Only compute the new window layout when startup has been
3563 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (p_ch != old_value && full_screen
3565#ifdef FEAT_GUI
3566 && !gui.starting
3567#endif
3568 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003569 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003572 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 else if (pp == &p_uc)
3574 {
3575 if (p_uc < 0)
3576 {
3577 errmsg = e_positive;
3578 p_uc = 100;
3579 }
3580 if (p_uc && !old_value)
3581 ml_open_files();
3582 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003583#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003584 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003585 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003586 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003587 {
3588 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003589 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003590 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003591 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003592 {
3593 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003594 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595 }
3596 }
3597#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003598#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003599 else if (pp == &p_mzq)
3600 mzvim_reset_timer();
3601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003603#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003604 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003605 else if (pp == &p_pyx)
3606 {
3607 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3608 errmsg = e_invarg;
3609 }
3610#endif
3611
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003612 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 else if (pp == &p_ul)
3614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003615 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003617 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 p_ul = value;
3619 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003620 else if (pp == &curbuf->b_p_ul)
3621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003622 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003623 curbuf->b_p_ul = old_value;
3624 u_sync(TRUE);
3625 curbuf->b_p_ul = value;
3626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003628#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003629 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003630 else if (pp == &curwin->w_p_nuw)
3631 {
3632 if (curwin->w_p_nuw < 1)
3633 {
3634 errmsg = e_positive;
3635 curwin->w_p_nuw = 1;
3636 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003637 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003638 {
3639 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003640 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003641 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003642 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003643 }
3644#endif
3645
Bram Moolenaar1a384422010-07-14 19:53:30 +02003646 else if (pp == &curbuf->b_p_tw)
3647 {
3648 if (curbuf->b_p_tw < 0)
3649 {
3650 errmsg = e_positive;
3651 curbuf->b_p_tw = 0;
3652 }
3653#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003654 {
3655 win_T *wp;
3656 tabpage_T *tp;
3657
3658 FOR_ALL_TAB_WINDOWS(tp, wp)
3659 check_colorcolumn(wp);
3660 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003661#endif
3662 }
3663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 /*
3665 * Check the bounds for numeric options here
3666 */
3667 if (Rows < min_rows() && full_screen)
3668 {
3669 if (errbuf != NULL)
3670 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003671 vim_snprintf((char *)errbuf, errbuflen,
3672 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 errmsg = errbuf;
3674 }
3675 Rows = min_rows();
3676 }
3677 if (Columns < MIN_COLUMNS && full_screen)
3678 {
3679 if (errbuf != NULL)
3680 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003681 vim_snprintf((char *)errbuf, errbuflen,
3682 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 errmsg = errbuf;
3684 }
3685 Columns = MIN_COLUMNS;
3686 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003687 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 /*
3690 * If the screen (shell) height has been changed, assume it is the
3691 * physical screenheight.
3692 */
3693 if (old_Rows != Rows || old_Columns != Columns)
3694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003695 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (updating_screen)
3697 *pp = old_value;
3698 else if (full_screen
3699#ifdef FEAT_GUI
3700 && !gui.starting
3701#endif
3702 )
3703 set_shellsize((int)Columns, (int)Rows, TRUE);
3704 else
3705 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003706 // Postpone the resizing; check the size and cmdline position for
3707 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 check_shellsize();
3709 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3710 cmdline_row = Rows - p_ch;
3711 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003712 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003713 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (curbuf->b_p_ts <= 0)
3717 {
3718 errmsg = e_positive;
3719 curbuf->b_p_ts = 8;
3720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (p_tm < 0)
3722 {
3723 errmsg = e_positive;
3724 p_tm = 0;
3725 }
3726 if ((curwin->w_p_scr <= 0
3727 || (curwin->w_p_scr > curwin->w_height
3728 && curwin->w_height > 0))
3729 && full_screen)
3730 {
3731 if (pp == &(curwin->w_p_scr))
3732 {
3733 if (curwin->w_p_scr != 0)
3734 errmsg = e_scroll;
3735 win_comp_scroll(curwin);
3736 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003737 // If 'scroll' became invalid because of a side effect silently adjust
3738 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else if (curwin->w_p_scr <= 0)
3740 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003741 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 curwin->w_p_scr = curwin->w_height;
3743 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003744 if (p_hi < 0)
3745 {
3746 errmsg = e_positive;
3747 p_hi = 0;
3748 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003749 else if (p_hi > 10000)
3750 {
3751 errmsg = e_invarg;
3752 p_hi = 10000;
3753 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754 if (p_re < 0 || p_re > 2)
3755 {
3756 errmsg = e_invarg;
3757 p_re = 0;
3758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 if (p_report < 0)
3760 {
3761 errmsg = e_positive;
3762 p_report = 1;
3763 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003764 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003766 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 p_sj = Rows / 2;
3768 else
3769 {
3770 errmsg = e_scroll;
3771 p_sj = 1;
3772 }
3773 }
3774 if (p_so < 0 && full_screen)
3775 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003776 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 p_so = 0;
3778 }
3779 if (p_siso < 0 && full_screen)
3780 {
3781 errmsg = e_positive;
3782 p_siso = 0;
3783 }
3784#ifdef FEAT_CMDWIN
3785 if (p_cwh < 1)
3786 {
3787 errmsg = e_positive;
3788 p_cwh = 1;
3789 }
3790#endif
3791 if (p_ut < 0)
3792 {
3793 errmsg = e_positive;
3794 p_ut = 2000;
3795 }
3796 if (p_ss < 0)
3797 {
3798 errmsg = e_positive;
3799 p_ss = 0;
3800 }
3801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003802 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3804 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3805
3806 options[opt_idx].flags |= P_WAS_SET;
3807
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003808#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003809 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3810 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003811#endif
3812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003814 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003815 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003816 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003817 if ((opt_flags & OPT_NO_REDRAW) == 0)
3818 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 return errmsg;
3821}
3822
3823/*
3824 * Called after an option changed: check if something needs to be redrawn.
3825 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003827check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003829 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003830 int doclear = (flags & P_RCLR) == P_RCLR;
3831 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003833 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3837 changed_window_setting();
3838 if (flags & P_RBUF)
3839 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003840 if (flags & P_RWINONLY)
3841 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003842 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 redraw_all_later(CLEAR);
3844 else if (all)
3845 redraw_all_later(NOT_VALID);
3846}
3847
3848/*
3849 * Find index for option 'arg'.
3850 * Return -1 if not found.
3851 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003852 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003853findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 int opt_idx;
3856 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003857 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 int is_term_opt;
3859
3860 /*
3861 * For first call: Initialize the quick-access table.
3862 * It contains the index for the first option that starts with a certain
3863 * letter. There are 26 letters, plus the first "t_" option.
3864 */
3865 if (quick_tab[1] == 0)
3866 {
3867 p = options[0].fullname;
3868 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3869 {
3870 if (s[0] != p[0])
3871 {
3872 if (s[0] == 't' && s[1] == '_')
3873 quick_tab[26] = opt_idx;
3874 else
3875 quick_tab[CharOrdLow(s[0])] = opt_idx;
3876 }
3877 p = s;
3878 }
3879 }
3880
3881 /*
3882 * Check for name starting with an illegal character.
3883 */
3884#ifdef EBCDIC
3885 if (!islower(arg[0]))
3886#else
3887 if (arg[0] < 'a' || arg[0] > 'z')
3888#endif
3889 return -1;
3890
3891 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3892 if (is_term_opt)
3893 opt_idx = quick_tab[26];
3894 else
3895 opt_idx = quick_tab[CharOrdLow(arg[0])];
3896 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003898 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 break;
3900 }
3901 if (s == NULL && !is_term_opt)
3902 {
3903 opt_idx = quick_tab[CharOrdLow(arg[0])];
3904 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3905 {
3906 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003907 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 break;
3909 s = NULL;
3910 }
3911 }
3912 if (s == NULL)
3913 opt_idx = -1;
3914 return opt_idx;
3915}
3916
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003917#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918/*
3919 * Get the value for an option.
3920 *
3921 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003922 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003923 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003924 * String option: gov_string, *stringval gets allocated string.
3925 * Hidden Number option: gov_hidden_number.
3926 * Hidden Toggle option: gov_hidden_bool.
3927 * Hidden String option: gov_hidden_string.
3928 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003930 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003931get_option_value(
3932 char_u *name,
3933 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003934 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003935 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 int opt_idx;
3938 char_u *varp;
3939
3940 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003941 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003942 {
3943 int key;
3944
3945 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003946 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003947 {
3948 char_u key_name[2];
3949 char_u *p;
3950
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003951 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003952 if (key < 0)
3953 {
3954 key_name[0] = KEY2TERMCAP0(key);
3955 key_name[1] = KEY2TERMCAP1(key);
3956 }
3957 else
3958 {
3959 key_name[0] = KS_KEY;
3960 key_name[1] = (key & 0xff);
3961 }
3962 p = find_termcode(key_name);
3963 if (p != NULL)
3964 {
3965 if (stringval != NULL)
3966 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003967 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003968 }
3969 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003970 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972
3973 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3974
3975 if (options[opt_idx].flags & P_STRING)
3976 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003977 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003978 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (stringval != NULL)
3980 {
3981#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003982 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003983 if ((char_u **)varp == &curbuf->b_p_key
3984 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 *stringval = vim_strsave((char_u *)"*****");
3986 else
3987#endif
3988 *stringval = vim_strsave(*(char_u **)(varp));
3989 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003990 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003993 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003994 return (options[opt_idx].flags & P_NUM)
3995 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (options[opt_idx].flags & P_NUM)
3997 *numval = *(long *)varp;
3998 else
3999 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004000 // Special case: 'modified' is b_changed, but we also want to consider
4001 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if ((int *)varp == &curbuf->b_changed)
4003 *numval = curbufIsChanged();
4004 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004005 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004007 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008}
4009#endif
4010
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004011#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004012/*
4013 * Returns the option attributes and its value. Unlike the above function it
4014 * will return either global value or local value of the option depending on
4015 * what was requested, but it will never return global value if it was
4016 * requested to return local one and vice versa. Neither it will return
4017 * buffer-local value if it was requested to return window-local one.
4018 *
4019 * Pretends that option is absent if it is not present in the requested scope
4020 * (i.e. has no global, window-local or buffer-local value depending on
4021 * opt_type). Uses
4022 *
4023 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004024 * 0 hidden or unknown option, also option that does not have requested
4025 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004026 * see SOPT_* in vim.h for other flags
4027 *
4028 * Possible opt_type values: see SREQ_* in vim.h
4029 */
4030 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004031get_option_value_strict(
4032 char_u *name,
4033 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004034 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004035 int opt_type,
4036 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004037{
4038 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004039 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004040 struct vimoption *p;
4041 int r = 0;
4042
4043 opt_idx = findoption(name);
4044 if (opt_idx < 0)
4045 return 0;
4046
4047 p = &(options[opt_idx]);
4048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004049 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004050 if (p->var == NULL)
4051 return 0;
4052
4053 if (p->flags & P_BOOL)
4054 r |= SOPT_BOOL;
4055 else if (p->flags & P_NUM)
4056 r |= SOPT_NUM;
4057 else if (p->flags & P_STRING)
4058 r |= SOPT_STRING;
4059
4060 if (p->indir == PV_NONE)
4061 {
4062 if (opt_type == SREQ_GLOBAL)
4063 r |= SOPT_GLOBAL;
4064 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004065 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004066 }
4067 else
4068 {
4069 if (p->indir & PV_BOTH)
4070 r |= SOPT_GLOBAL;
4071 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004072 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004073
4074 if (p->indir & PV_WIN)
4075 {
4076 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004077 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004078 else
4079 r |= SOPT_WIN;
4080 }
4081 else if (p->indir & PV_BUF)
4082 {
4083 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004084 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004085 else
4086 r |= SOPT_BUF;
4087 }
4088 }
4089
4090 if (stringval == NULL)
4091 return r;
4092
4093 if (opt_type == SREQ_GLOBAL)
4094 varp = p->var;
4095 else
4096 {
4097 if (opt_type == SREQ_BUF)
4098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 // Special case: 'modified' is b_changed, but we also want to
4100 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004101 if (p->indir == PV_MOD)
4102 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004103 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004104 varp = NULL;
4105 }
4106#ifdef FEAT_CRYPT
4107 else if (p->indir == PV_KEY)
4108 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004110 *stringval = NULL;
4111 varp = NULL;
4112 }
4113#endif
4114 else
4115 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004116 buf_T *save_curbuf = curbuf;
4117
4118 // only getting a pointer, no need to use aucmd_prepbuf()
4119 curbuf = (buf_T *)from;
4120 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004121 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004122 curbuf = save_curbuf;
4123 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004124 }
4125 }
4126 else if (opt_type == SREQ_WIN)
4127 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004128 win_T *save_curwin = curwin;
4129
4130 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004131 curbuf = curwin->w_buffer;
4132 varp = get_varp(p);
4133 curwin = save_curwin;
4134 curbuf = curwin->w_buffer;
4135 }
4136 if (varp == p->var)
4137 return (r | SOPT_UNSET);
4138 }
4139
4140 if (varp != NULL)
4141 {
4142 if (p->flags & P_STRING)
4143 *stringval = vim_strsave(*(char_u **)(varp));
4144 else if (p->flags & P_NUM)
4145 *numval = *(long *) varp;
4146 else
4147 *numval = *(int *)varp;
4148 }
4149
4150 return r;
4151}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004152
4153/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004154 * Iterate over options. First argument is a pointer to a pointer to a
4155 * structure inside options[] array, second is option type like in the above
4156 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004157 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004158 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004159 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004160 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004161 * first argument to NULL.
4162 *
4163 * Returns full option name for current option on each call.
4164 */
4165 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004166option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004167{
4168 struct vimoption *ret = NULL;
4169 do
4170 {
4171 if (*option == NULL)
4172 *option = (void *) options;
4173 else if (((struct vimoption *) (*option))->fullname == NULL)
4174 {
4175 *option = NULL;
4176 return NULL;
4177 }
4178 else
4179 *option = (void *) (((struct vimoption *) (*option)) + 1);
4180
4181 ret = ((struct vimoption *) (*option));
4182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004183 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004184 if (ret->var == NULL)
4185 {
4186 ret = NULL;
4187 continue;
4188 }
4189
4190 switch (opt_type)
4191 {
4192 case SREQ_GLOBAL:
4193 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4194 ret = NULL;
4195 break;
4196 case SREQ_BUF:
4197 if (!(ret->indir & PV_BUF))
4198 ret = NULL;
4199 break;
4200 case SREQ_WIN:
4201 if (!(ret->indir & PV_WIN))
4202 ret = NULL;
4203 break;
4204 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004205 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004206 return NULL;
4207 }
4208 }
4209 while (ret == NULL);
4210
4211 return (char_u *)ret->fullname;
4212}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004213#endif
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004216 * Return the flags for the option at 'opt_idx'.
4217 */
4218 long_u
4219get_option_flags(int opt_idx)
4220{
4221 return options[opt_idx].flags;
4222}
4223
4224/*
4225 * Set a flag for the option at 'opt_idx'.
4226 */
4227 void
4228set_option_flag(int opt_idx, long_u flag)
4229{
4230 options[opt_idx].flags |= flag;
4231}
4232
4233/*
4234 * Clear a flag for the option at 'opt_idx'.
4235 */
4236 void
4237clear_option_flag(int opt_idx, long_u flag)
4238{
4239 options[opt_idx].flags &= ~flag;
4240}
4241
4242/*
4243 * Returns TRUE if the option at 'opt_idx' is a global option
4244 */
4245 int
4246is_global_option(int opt_idx)
4247{
4248 return options[opt_idx].indir == PV_NONE;
4249}
4250
4251/*
4252 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4253 * local value.
4254 */
4255 int
4256is_global_local_option(int opt_idx)
4257{
4258 return options[opt_idx].indir & PV_BOTH;
4259}
4260
4261/*
4262 * Returns TRUE if the option at 'opt_idx' is a window-local option
4263 */
4264 int
4265is_window_local_option(int opt_idx)
4266{
4267 return options[opt_idx].var == VAR_WIN;
4268}
4269
4270/*
4271 * Returns TRUE if the option at 'opt_idx' is a hidden option
4272 */
4273 int
4274is_hidden_option(int opt_idx)
4275{
4276 return options[opt_idx].var == NULL;
4277}
4278
4279#if defined(FEAT_CRYPT) || defined(PROTO)
4280/*
4281 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4282 */
4283 int
4284is_crypt_key_option(int opt_idx)
4285{
4286 return options[opt_idx].indir == PV_KEY;
4287}
4288#endif
4289
4290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 * Set the value of option "name".
4292 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004293 *
4294 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004296 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004297set_option_value(
4298 char_u *name,
4299 long number,
4300 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004301 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
4303 int opt_idx;
4304 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004305 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004308 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004309 {
4310 int key;
4311
4312 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004313 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004314 {
4315 char_u key_name[2];
4316
4317 if (key < 0)
4318 {
4319 key_name[0] = KEY2TERMCAP0(key);
4320 key_name[1] = KEY2TERMCAP1(key);
4321 }
4322 else
4323 {
4324 key_name[0] = KS_KEY;
4325 key_name[1] = (key & 0xff);
4326 }
4327 add_termcode(key_name, string, FALSE);
4328 if (full_screen)
4329 ttest(FALSE);
4330 redraw_all_later(CLEAR);
4331 return NULL;
4332 }
4333
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004334 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 else
4337 {
4338 flags = options[opt_idx].flags;
4339#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004340 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004343 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004344 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004347 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004348 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 else
4350 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004351 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004352 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004354 if (number == 0 && string != NULL)
4355 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004356 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004358 // Either we are given a string or we are setting option
4359 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004360 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004361 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004362 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004363 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004364 // There's another character after zeros or the string
4365 // is empty. In both cases, we are trying to set a
4366 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004367 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004368 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004369 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004370
4371 }
4372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004374 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004375 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004377 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004378 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 }
4381 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004382 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383}
4384
4385/*
4386 * Get the terminal code for a terminal option.
4387 * Returns NULL when not found.
4388 */
4389 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004390get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
4392 int opt_idx;
4393 char_u *varp;
4394
4395 if (tname[0] != 't' || tname[1] != '_' ||
4396 tname[2] == NUL || tname[3] == NUL)
4397 return NULL;
4398 if ((opt_idx = findoption(tname)) >= 0)
4399 {
4400 varp = get_varp(&(options[opt_idx]));
4401 if (varp != NULL)
4402 varp = *(char_u **)(varp);
4403 return varp;
4404 }
4405 return find_termcode(tname + 2);
4406}
4407
4408 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004409get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 int i;
4412
4413 i = findoption((char_u *)"hl");
4414 if (i >= 0)
4415 return options[i].def_val[VI_DEFAULT];
4416 return (char_u *)NULL;
4417}
4418
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004419 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004421{
4422 int i;
4423
4424 i = findoption((char_u *)"enc");
4425 if (i >= 0)
4426 return options[i].def_val[VI_DEFAULT];
4427 return (char_u *)NULL;
4428}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430/*
4431 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004432 * When "has_lt" is true there is a '<' before "*arg_arg".
4433 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 */
4435 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004436find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004438 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004440 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 /*
4443 * Don't use get_special_key_code() for t_xx, we don't want it to call
4444 * add_termcap_entry().
4445 */
4446 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4447 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004448 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004450 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004452 key = find_special_key(&arg, &modifiers,
4453 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004454 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 key = 0;
4456 }
4457 return key;
4458}
4459
4460/*
4461 * if 'all' == 0: show changed options
4462 * if 'all' == 1: show all normal options
4463 * if 'all' == 2: show all terminal options
4464 */
4465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004466showoptions(
4467 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004468 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
4470 struct vimoption *p;
4471 int col;
4472 int isterm;
4473 char_u *varp;
4474 struct vimoption **items;
4475 int item_count;
4476 int run;
4477 int row, rows;
4478 int cols;
4479 int i;
4480 int len;
4481
4482#define INC 20
4483#define GAP 3
4484
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004485 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (items == NULL)
4487 return;
4488
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004489 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004491 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004493 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004495 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004497 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004500 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 * 1. display the short items
4502 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004503 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 */
4505 for (run = 1; run <= 2 && !got_int; ++run)
4506 {
4507 /*
4508 * collect the items in items[]
4509 */
4510 item_count = 0;
4511 for (p = &options[0]; p->fullname != NULL; p++)
4512 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004513 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004514 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004515 continue;
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 varp = NULL;
4518 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004519 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 if (p->indir != PV_NONE && !isterm)
4522 varp = get_varp_scope(p, opt_flags);
4523 }
4524 else
4525 varp = get_varp(p);
4526 if (varp != NULL
4527 && ((all == 2 && isterm)
4528 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004529 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004531 if (opt_flags & OPT_ONECOLUMN)
4532 len = Columns;
4533 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004534 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 else
4536 {
4537 option_value2string(p, opt_flags);
4538 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4539 }
4540 if ((len <= INC - GAP && run == 1) ||
4541 (len > INC - GAP && run == 2))
4542 items[item_count++] = p;
4543 }
4544 }
4545
4546 /*
4547 * display the items
4548 */
4549 if (run == 1)
4550 {
4551 cols = (Columns + GAP - 3) / INC;
4552 if (cols == 0)
4553 cols = 1;
4554 rows = (item_count + cols - 1) / cols;
4555 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004556 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 rows = item_count;
4558 for (row = 0; row < rows && !got_int; ++row)
4559 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004560 msg_putchar('\n'); // go to next line
4561 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 break;
4563 col = 0;
4564 for (i = row; i < item_count; i += rows)
4565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 showoneopt(items[i], opt_flags);
4568 col += INC;
4569 }
4570 out_flush();
4571 ui_breakcheck();
4572 }
4573 }
4574 vim_free(items);
4575}
4576
4577/*
4578 * Return TRUE if option "p" has its default value.
4579 */
4580 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004581optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
4583 int dvi;
4584
4585 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004586 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004587 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004589 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004591 // the cast to long is required for Manx C, long_i is
4592 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004593 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004594 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4596}
4597
4598/*
4599 * showoneopt: show the value of one option
4600 * must not be called with a hidden option!
4601 */
4602 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004603showoneopt(
4604 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004605 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004607 char_u *varp;
4608 int save_silent = silent_mode;
4609
4610 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 varp = get_varp_scope(p, opt_flags);
4614
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4617 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004618 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004620 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004622 msg_puts(" ");
4623 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (!(p->flags & P_BOOL))
4625 {
4626 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004627 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 option_value2string(p, opt_flags);
4629 msg_outtrans(NameBuff);
4630 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004631
4632 silent_mode = save_silent;
4633 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634}
4635
4636/*
4637 * Write modified options as ":set" commands to a file.
4638 *
4639 * There are three values for "opt_flags":
4640 * OPT_GLOBAL: Write global option values and fresh values of
4641 * buffer-local options (used for start of a session
4642 * file).
4643 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4644 * curwin (used for a vimrc file).
4645 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4646 * and local values for window-local options of
4647 * curwin. Local values are also written when at the
4648 * default value, because a modeline or autocommand
4649 * may have set them when doing ":edit file" and the
4650 * user has set them back at the default or fresh
4651 * value.
4652 * When "local_only" is TRUE, don't write fresh
4653 * values, only local values (for ":mkview").
4654 * (fresh value = value used for a new buffer or window for a local option).
4655 *
4656 * Return FAIL on error, OK otherwise.
4657 */
4658 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004659makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004662 char_u *varp; // currently used value
4663 char_u *varp_fresh; // local value
4664 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 char *cmd;
4666 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004667 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /*
4670 * The options that don't have a default (terminal name, columns, lines)
4671 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004672 * Do the loop over "options[]" twice: once for options with the
4673 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004675 for (pri = 1; pri >= 0; --pri)
4676 {
4677 for (p = &options[0]; !istermoption(p); p++)
4678 if (!(p->flags & P_NO_MKRC)
4679 && !istermoption(p)
4680 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004682 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4684 continue;
4685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004686 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4687 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4689 continue;
4690
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004691 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004693 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 continue;
4695
Bram Moolenaard23b7142021-04-17 21:04:34 +02004696 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4697 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004698 continue;
4699
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 round = 2;
4701 if (p->indir != PV_NONE)
4702 {
4703 if (p->var == VAR_WIN)
4704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004705 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 if (!(opt_flags & OPT_LOCAL))
4707 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004708 // When fresh value of window-local option is not at the
4709 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4711 {
4712 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004713 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 round = 1;
4716 varp_local = varp;
4717 varp = varp_fresh;
4718 }
4719 }
4720 }
4721 }
4722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004723 // Round 1: fresh value for window-local options.
4724 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 for ( ; round <= 2; varp = varp_local, ++round)
4726 {
4727 if (round == 1 || (opt_flags & OPT_GLOBAL))
4728 cmd = "set";
4729 else
4730 cmd = "setlocal";
4731
4732 if (p->flags & P_BOOL)
4733 {
4734 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4735 return FAIL;
4736 }
4737 else if (p->flags & P_NUM)
4738 {
4739 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4740 return FAIL;
4741 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004742 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004744 int do_endif = FALSE;
4745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004746 // Don't set 'syntax' and 'filetype' again if the value is
4747 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004748 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004749#if defined(FEAT_SYN_HL)
4750 p->indir == PV_SYN ||
4751#endif
4752 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4755 *(char_u **)(varp)) < 0
4756 || put_eol(fd) < 0)
4757 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004758 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004761 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004763 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 if (put_line(fd, "endif") == FAIL)
4766 return FAIL;
4767 }
4768 }
4769 }
4770 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 return OK;
4773}
4774
4775#if defined(FEAT_FOLDING) || defined(PROTO)
4776/*
4777 * Generate set commands for the local fold options only. Used when
4778 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4779 */
4780 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004781makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004783 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004785 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 == FAIL
4787# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004788 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004790 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 == FAIL
4792 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4793 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4794 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4795 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4796 )
4797 return FAIL;
4798
4799 return OK;
4800}
4801#endif
4802
4803 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004804put_setstring(
4805 FILE *fd,
4806 char *cmd,
4807 char *name,
4808 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004809 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004812 char_u *buf = NULL;
4813 char_u *part = NULL;
4814 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4817 return FAIL;
4818 if (*valuep != NULL)
4819 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004820 // Output 'pastetoggle' as key names. For other
4821 // options some characters have to be escaped with
4822 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (valuep == &p_pt)
4824 {
4825 s = *valuep;
4826 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004827 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 return FAIL;
4829 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004830 // expand the option value, replace $HOME by ~
4831 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004833 int size = (int)STRLEN(*valuep) + 1;
4834
4835 // replace home directory in the whole option value into "buf"
4836 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004837 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004838 goto fail;
4839 home_replace(NULL, *valuep, buf, size, FALSE);
4840
4841 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004842 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004843 // can be expanded when read back.
4844 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4845 && vim_strchr(*valuep, ',') != NULL)
4846 {
4847 part = alloc(size);
4848 if (part == NULL)
4849 goto fail;
4850
4851 // write line break to clear the option, e.g. ':set rtp='
4852 if (put_eol(fd) == FAIL)
4853 goto fail;
4854
4855 p = buf;
4856 while (*p != NUL)
4857 {
4858 // for each comma separated option part, append value to
4859 // the option, :set rtp+=value
4860 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4861 goto fail;
4862 (void)copy_option_part(&p, part, size, ",");
4863 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4864 goto fail;
4865 }
4866 vim_free(buf);
4867 vim_free(part);
4868 return OK;
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004871 {
4872 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004874 }
4875 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 else if (put_escstr(fd, *valuep, 2) == FAIL)
4878 return FAIL;
4879 }
4880 if (put_eol(fd) < 0)
4881 return FAIL;
4882 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004883fail:
4884 vim_free(buf);
4885 vim_free(part);
4886 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887}
4888
4889 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004890put_setnum(
4891 FILE *fd,
4892 char *cmd,
4893 char *name,
4894 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 long wc;
4897
4898 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4899 return FAIL;
4900 if (wc_use_keyname((char_u *)valuep, &wc))
4901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004902 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4904 return FAIL;
4905 }
4906 else if (fprintf(fd, "%ld", *valuep) < 0)
4907 return FAIL;
4908 if (put_eol(fd) < 0)
4909 return FAIL;
4910 return OK;
4911}
4912
4913 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004914put_setbool(
4915 FILE *fd,
4916 char *cmd,
4917 char *name,
4918 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004920 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004921 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4923 || put_eol(fd) < 0)
4924 return FAIL;
4925 return OK;
4926}
4927
4928/*
4929 * Clear all the terminal options.
4930 * If the option has been allocated, free the memory.
4931 * Terminal options are never hidden or indirect.
4932 */
4933 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004934clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /*
4937 * Reset a few things before clearing the old options. This may cause
4938 * outputting a few things that the terminal doesn't understand, but the
4939 * screen will be cleared later, so this is OK.
4940 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004941 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004943 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004946 // When starting the GUI close the display opened for the clipboard.
4947 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (gui.starting)
4949 clear_xterm_clip();
4950#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004951 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004953 free_termoptions();
4954}
4955
4956 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004957free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004958{
4959 struct vimoption *p;
4960
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004961 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (istermoption(p))
4963 {
4964 if (p->flags & P_ALLOCED)
4965 free_string_option(*(char_u **)(p->var));
4966 if (p->flags & P_DEF_ALLOCED)
4967 free_string_option(p->def_val[VI_DEFAULT]);
4968 *(char_u **)(p->var) = empty_option;
4969 p->def_val[VI_DEFAULT] = empty_option;
4970 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004971#ifdef FEAT_EVAL
4972 // remember where the option was cleared
4973 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 clear_termcodes();
4977}
4978
4979/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004980 * Free the string for one term option, if it was allocated.
4981 * Set the string to empty_option and clear allocated flag.
4982 * "var" points to the option value.
4983 */
4984 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004985free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004986{
4987 struct vimoption *p;
4988
4989 for (p = &options[0]; p->fullname != NULL; p++)
4990 if (p->var == var)
4991 {
4992 if (p->flags & P_ALLOCED)
4993 free_string_option(*(char_u **)(p->var));
4994 *(char_u **)(p->var) = empty_option;
4995 p->flags &= ~P_ALLOCED;
4996 break;
4997 }
4998}
4999
5000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 * Set the terminal option defaults to the current value.
5002 * Used after setting the terminal name.
5003 */
5004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005005set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006{
5007 struct vimoption *p;
5008
5009 for (p = &options[0]; p->fullname != NULL; p++)
5010 {
5011 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5012 {
5013 if (p->flags & P_DEF_ALLOCED)
5014 {
5015 free_string_option(p->def_val[VI_DEFAULT]);
5016 p->flags &= ~P_DEF_ALLOCED;
5017 }
5018 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5019 if (p->flags & P_ALLOCED)
5020 {
5021 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005022 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 }
5025 }
5026}
5027
5028/*
5029 * return TRUE if 'p' starts with 't_'
5030 */
5031 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5035}
5036
Bram Moolenaardac13472019-09-16 21:06:21 +02005037/*
5038 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5039 */
5040 int
5041istermoption_idx(int opt_idx)
5042{
5043 return istermoption(&options[opt_idx]);
5044}
5045
Bram Moolenaar113e1072019-01-20 15:30:40 +01005046#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005048 * Unset local option value, similar to ":set opt<".
5049 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005051unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052{
5053 struct vimoption *p;
5054 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005055 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005056
5057 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005058 if (opt_idx < 0)
5059 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005060 p = &(options[opt_idx]);
5061
5062 switch ((int)p->indir)
5063 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005064 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005065 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005066 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005067 break;
5068 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005069 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005070 break;
5071 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005072 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073 break;
5074 case PV_AR:
5075 buf->b_p_ar = -1;
5076 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005077 case PV_BKC:
5078 clear_string_option(&buf->b_p_bkc);
5079 buf->b_bkc_flags = 0;
5080 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005081 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005082 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005084 case PV_TC:
5085 clear_string_option(&buf->b_p_tc);
5086 buf->b_tc_flags = 0;
5087 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005088 case PV_SISO:
5089 curwin->w_p_siso = -1;
5090 break;
5091 case PV_SO:
5092 curwin->w_p_so = -1;
5093 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005094#ifdef FEAT_FIND_ID
5095 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005096 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 break;
5098 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005099 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005100 break;
5101#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005103 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005104 break;
5105 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005106 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005107 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005108 case PV_FP:
5109 clear_string_option(&buf->b_p_fp);
5110 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005111#ifdef FEAT_QUICKFIX
5112 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005113 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005114 break;
5115 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005116 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117 break;
5118 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005119 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120 break;
5121#endif
5122#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5123 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005124 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005125 break;
5126#endif
5127#if defined(FEAT_CRYPT)
5128 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005129 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130 break;
5131#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005132#ifdef FEAT_LINEBREAK
5133 case PV_SBR:
5134 clear_string_option(&((win_T *)from)->w_p_sbr);
5135 break;
5136#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005137#ifdef FEAT_STL_OPT
5138 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005139 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005140 break;
5141#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005142 case PV_UL:
5143 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5144 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005145#ifdef FEAT_LISP
5146 case PV_LW:
5147 clear_string_option(&buf->b_p_lw);
5148 break;
5149#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005150 case PV_MENC:
5151 clear_string_option(&buf->b_p_menc);
5152 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005153 case PV_LCS:
5154 clear_string_option(&((win_T *)from)->w_p_lcs);
5155 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5156 redraw_later(NOT_VALID);
5157 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 }
5159}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005160#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161
5162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 * Get pointer to option variable, depending on local or global scope.
5164 */
5165 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005166get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5169 {
5170 if (p->var == VAR_WIN)
5171 return (char_u *)GLOBAL_WO(get_varp(p));
5172 return p->var;
5173 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005174 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 switch ((int)p->indir)
5177 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005178 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005180 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5181 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5182 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005184 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5185 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5186 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005187 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005188 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005189 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005190 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5191 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005193 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5194 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005196 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5197 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005198#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5199 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5200#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005201#if defined(FEAT_CRYPT)
5202 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5203#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005204#ifdef FEAT_LINEBREAK
5205 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5206#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005207#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005208 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005209#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005210 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005211#ifdef FEAT_LISP
5212 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5213#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005214 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005215 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005216 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005219 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221 return get_varp(p);
5222}
5223
5224/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005225 * Get pointer to option variable at 'opt_idx', depending on local or global
5226 * scope.
5227 */
5228 char_u *
5229get_option_varp_scope(int opt_idx, int opt_flags)
5230{
5231 return get_varp_scope(&(options[opt_idx]), opt_flags);
5232}
5233
5234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 * Get pointer to option variable.
5236 */
5237 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005238get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (p->var == NULL)
5242 return NULL;
5243
5244 switch ((int)p->indir)
5245 {
5246 case PV_NONE: return p->var;
5247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005248 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005249 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005251 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005253 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005255 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005257 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005259 case PV_TC: return *curbuf->b_p_tc != NUL
5260 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005261 case PV_BKC: return *curbuf->b_p_bkc != NUL
5262 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005263 case PV_SISO: return curwin->w_p_siso >= 0
5264 ? (char_u *)&(curwin->w_p_siso) : p->var;
5265 case PV_SO: return curwin->w_p_so >= 0
5266 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005268 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005270 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5272#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005273 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005275 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005277 case PV_FP: return *curbuf->b_p_fp != NUL
5278 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005280 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005282 case PV_GP: return *curbuf->b_p_gp != NUL
5283 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5284 case PV_MP: return *curbuf->b_p_mp != NUL
5285 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005287#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5288 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5289 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5290#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005291#if defined(FEAT_CRYPT)
5292 case PV_CM: return *curbuf->b_p_cm != NUL
5293 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5294#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005295#ifdef FEAT_LINEBREAK
5296 case PV_SBR: return *curwin->w_p_sbr != NUL
5297 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5298#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005299#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005300 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005301 ? (char_u *)&(curwin->w_p_stl) : p->var;
5302#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005303 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5304 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005305#ifdef FEAT_LISP
5306 case PV_LW: return *curbuf->b_p_lw != NUL
5307 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5308#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005309 case PV_MENC: return *curbuf->b_p_menc != NUL
5310 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311#ifdef FEAT_ARABIC
5312 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5313#endif
5314 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005315 case PV_LCS: return *curwin->w_p_lcs != NUL
5316 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005317#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005318 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5319#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005320#ifdef FEAT_SYN_HL
5321 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5322 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005323 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005324 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#ifdef FEAT_DIFF
5327 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5328#endif
5329#ifdef FEAT_FOLDING
5330 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5331 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5332 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5333 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5334 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5335 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5336 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5337# ifdef FEAT_EVAL
5338 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5339 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5340# endif
5341 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5342#endif
5343 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005344 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005345#ifdef FEAT_LINEBREAK
5346 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005349 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005350#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5352#endif
5353#ifdef FEAT_RIGHTLEFT
5354 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5355 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5356#endif
5357 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5358 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5359#ifdef FEAT_LINEBREAK
5360 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005361 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5362 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005364 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005366 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005367#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005368 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5369 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5370#endif
5371#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005372 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5373 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5374 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5378 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5381 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5383 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5384#ifdef FEAT_CINDENT
5385 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5386 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5387 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5388#endif
5389#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5390 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_FOLDING
5394 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005397#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005398 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005400#ifdef FEAT_COMPL_FUNC
5401 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005402 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005403#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005404#ifdef FEAT_EVAL
5405 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005408 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005414 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5416 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5417 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5418 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5419#ifdef FEAT_FIND_ID
5420# ifdef FEAT_EVAL
5421 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5422# endif
5423#endif
5424#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5425 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5426 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5427#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005428#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005429 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431#ifdef FEAT_CRYPT
5432 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5433#endif
5434#ifdef FEAT_LISP
5435 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5436#endif
5437 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5438 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5439 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5440 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5441 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005443#ifdef FEAT_TEXTOBJ
5444 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5447#ifdef FEAT_SMARTINDENT
5448 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5452#ifdef FEAT_SEARCHPATH
5453 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5454#endif
5455 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5456#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005457 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005458 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5459#endif
5460#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005461 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5462 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5463 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005464 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465#endif
5466 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5467 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5468 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5469 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005470#ifdef FEAT_PERSISTENT_UNDO
5471 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5474#ifdef FEAT_KEYMAP
5475 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5476#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005477#ifdef FEAT_SIGNS
5478 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5479#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005480#ifdef FEAT_VARTABS
5481 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5482 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5483#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005484 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005486 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 return (char_u *)&(curbuf->b_p_wm);
5488}
5489
5490/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005491 * Return a pointer to the variable for option at 'opt_idx'
5492 */
5493 char_u *
5494get_option_var(int opt_idx)
5495{
5496 return options[opt_idx].var;
5497}
5498
5499/*
5500 * Return the full name of the option at 'opt_idx'
5501 */
5502 char_u *
5503get_option_fullname(int opt_idx)
5504{
5505 return (char_u *)options[opt_idx].fullname;
5506}
5507
5508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * Get the value of 'equalprg', either the buffer-local one or the global one.
5510 */
5511 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005512get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 if (*curbuf->b_p_ep == NUL)
5515 return p_ep;
5516 return curbuf->b_p_ep;
5517}
5518
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519/*
5520 * Copy options from one window to another.
5521 * Used when splitting a window.
5522 */
5523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005524win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5527 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005528 after_copy_winopt(wp_to);
5529}
5530
5531/*
5532 * After copying window options: update variables depending on options.
5533 */
5534 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005535after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005536{
5537#ifdef FEAT_LINEBREAK
5538 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005539#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005540#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005541 fill_culopt_flags(NULL, wp);
5542 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005543#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005544 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547/*
5548 * Copy the options from one winopt_T to another.
5549 * Doesn't free the old option values in "to", use clear_winopt() for that.
5550 * The 'scroll' option is not copied, because it depends on the window height.
5551 * The 'previewwindow' option is reset, there can be only one preview window.
5552 */
5553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005554copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
5556#ifdef FEAT_ARABIC
5557 to->wo_arab = from->wo_arab;
5558#endif
5559 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005560 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005562 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005563#ifdef FEAT_LINEBREAK
5564 to->wo_nuw = from->wo_nuw;
5565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566#ifdef FEAT_RIGHTLEFT
5567 to->wo_rl = from->wo_rl;
5568 to->wo_rlc = vim_strsave(from->wo_rlc);
5569#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005570#ifdef FEAT_LINEBREAK
5571 to->wo_sbr = vim_strsave(from->wo_sbr);
5572#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005573#ifdef FEAT_STL_OPT
5574 to->wo_stl = vim_strsave(from->wo_stl);
5575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005577#ifdef FEAT_DIFF
5578 to->wo_wrap_save = from->wo_wrap_save;
5579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580#ifdef FEAT_LINEBREAK
5581 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005582 to->wo_bri = from->wo_bri;
5583 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005585 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005587 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005588 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005589 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005590#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005591 to->wo_spell = from->wo_spell;
5592#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005593#ifdef FEAT_SYN_HL
5594 to->wo_cuc = from->wo_cuc;
5595 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005596 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005597 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599#ifdef FEAT_DIFF
5600 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005601 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005603#ifdef FEAT_CONCEAL
5604 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005605 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005606#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005607#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005608 to->wo_twk = vim_strsave(from->wo_twk);
5609 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611#ifdef FEAT_FOLDING
5612 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005613 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005615 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 to->wo_fdi = vim_strsave(from->wo_fdi);
5617 to->wo_fml = from->wo_fml;
5618 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005619 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005621 to->wo_fdm_save = from->wo_diff_saved
5622 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 to->wo_fdn = from->wo_fdn;
5624# ifdef FEAT_EVAL
5625 to->wo_fde = vim_strsave(from->wo_fde);
5626 to->wo_fdt = vim_strsave(from->wo_fdt);
5627# endif
5628 to->wo_fmr = vim_strsave(from->wo_fmr);
5629#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005630#ifdef FEAT_SIGNS
5631 to->wo_scl = vim_strsave(from->wo_scl);
5632#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005633
5634#ifdef FEAT_EVAL
5635 // Copy the script context so that we know where the value was last set.
5636 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5637 sizeof(to->wo_script_ctx));
5638#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
5642/*
5643 * Check string options in a window for a NULL value.
5644 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005646check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 check_winopt(&win->w_onebuf_opt);
5649 check_winopt(&win->w_allbuf_opt);
5650}
5651
5652/*
5653 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5654 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005655 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005656check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658#ifdef FEAT_FOLDING
5659 check_string_option(&wop->wo_fdi);
5660 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005661 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662# ifdef FEAT_EVAL
5663 check_string_option(&wop->wo_fde);
5664 check_string_option(&wop->wo_fdt);
5665# endif
5666 check_string_option(&wop->wo_fmr);
5667#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005668#ifdef FEAT_SIGNS
5669 check_string_option(&wop->wo_scl);
5670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_RIGHTLEFT
5672 check_string_option(&wop->wo_rlc);
5673#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005674#ifdef FEAT_LINEBREAK
5675 check_string_option(&wop->wo_sbr);
5676#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005677#ifdef FEAT_STL_OPT
5678 check_string_option(&wop->wo_stl);
5679#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005680#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005681 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005682 check_string_option(&wop->wo_cc);
5683#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005684#ifdef FEAT_CONCEAL
5685 check_string_option(&wop->wo_cocu);
5686#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005687#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005688 check_string_option(&wop->wo_twk);
5689 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005690#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005691#ifdef FEAT_LINEBREAK
5692 check_string_option(&wop->wo_briopt);
5693#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005694 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005695 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696}
5697
5698/*
5699 * Free the allocated memory inside a winopt_T.
5700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005702clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704#ifdef FEAT_FOLDING
5705 clear_string_option(&wop->wo_fdi);
5706 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005707 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708# ifdef FEAT_EVAL
5709 clear_string_option(&wop->wo_fde);
5710 clear_string_option(&wop->wo_fdt);
5711# endif
5712 clear_string_option(&wop->wo_fmr);
5713#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005714#ifdef FEAT_SIGNS
5715 clear_string_option(&wop->wo_scl);
5716#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005717#ifdef FEAT_LINEBREAK
5718 clear_string_option(&wop->wo_briopt);
5719#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005720 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721#ifdef FEAT_RIGHTLEFT
5722 clear_string_option(&wop->wo_rlc);
5723#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005724#ifdef FEAT_LINEBREAK
5725 clear_string_option(&wop->wo_sbr);
5726#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005727#ifdef FEAT_STL_OPT
5728 clear_string_option(&wop->wo_stl);
5729#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005730#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005731 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005732 clear_string_option(&wop->wo_cc);
5733#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005734#ifdef FEAT_CONCEAL
5735 clear_string_option(&wop->wo_cocu);
5736#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005737#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005738 clear_string_option(&wop->wo_twk);
5739 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005740#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005741 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742}
5743
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005744#ifdef FEAT_EVAL
5745// Index into the options table for a buffer-local option enum.
5746static int buf_opt_idx[BV_COUNT];
5747# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5748
5749/*
5750 * Initialize buf_opt_idx[] if not done already.
5751 */
5752 static void
5753init_buf_opt_idx(void)
5754{
5755 static int did_init_buf_opt_idx = FALSE;
5756 int i;
5757
5758 if (did_init_buf_opt_idx)
5759 return;
5760 did_init_buf_opt_idx = TRUE;
5761 for (i = 0; !istermoption_idx(i); i++)
5762 if (options[i].indir & PV_BUF)
5763 buf_opt_idx[options[i].indir & PV_MASK] = i;
5764}
5765#else
5766# define COPY_OPT_SCTX(buf, bv)
5767#endif
5768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769/*
5770 * Copy global option values to local options for one buffer.
5771 * Used when creating a new buffer and sometimes when entering a buffer.
5772 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005773 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5775 * appropriate.
5776 * BCO_NOHELP Don't copy the values to a help buffer.
5777 */
5778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005779buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005782 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 int dont_do_help;
5784 int did_isk = FALSE;
5785
5786 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 * Skip this when the option defaults have not been set yet. Happens when
5788 * main() allocates the first buffer.
5789 */
5790 if (p_cpo != NULL)
5791 {
5792 /*
5793 * Always copy when entering and 'cpo' contains 'S'.
5794 * Don't copy when already initialized.
5795 * Don't copy when 'cpo' contains 's' and not entering.
5796 * 'S' BCO_ENTER initialized 's' should_copy
5797 * yes yes X X TRUE
5798 * yes no yes X FALSE
5799 * no X yes X FALSE
5800 * X no no yes FALSE
5801 * X no no no TRUE
5802 * no yes no X TRUE
5803 */
5804 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5805 && (buf->b_p_initialized
5806 || (!(flags & BCO_ENTER)
5807 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5808 should_copy = FALSE;
5809
5810 if (should_copy || (flags & BCO_ALWAYS))
5811 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005812#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005813 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005814 init_buf_opt_idx();
5815#endif
5816 // Don't copy the options specific to a help buffer when
5817 // BCO_NOHELP is given or the options were initialized already
5818 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5820 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005821 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 {
5823 save_p_isk = buf->b_p_isk;
5824 buf->b_p_isk = NULL;
5825 }
5826 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005827 * Always free the allocated strings. If not already initialized,
5828 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 */
5830 if (!buf->b_p_initialized)
5831 {
5832 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005833 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005836 switch (*p_ffs)
5837 {
5838 case 'm':
5839 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5840 case 'd':
5841 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5842 case 'u':
5843 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5844 default:
5845 buf->b_p_ff = vim_strsave(p_ff);
5846 }
5847 if (buf->b_p_ff != NULL)
5848 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 buf->b_p_bh = empty_option;
5850 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
5852 else
5853 free_buf_options(buf, FALSE);
5854
5855 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005856 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 buf->b_p_ai_nopaste = p_ai_nopaste;
5858 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005859 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005861 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 buf->b_p_tw_nopaste = p_tw_nopaste;
5863 buf->b_p_tw_nobin = p_tw_nobin;
5864 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005865 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 buf->b_p_wm_nopaste = p_wm_nopaste;
5867 buf->b_p_wm_nobin = p_wm_nobin;
5868 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005869 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005870 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005871 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005872 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005873 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005875 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005877 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005879 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 buf->b_p_ml_nobin = p_ml_nobin;
5881 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005882 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005883 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005884 buf->b_p_swf = FALSE;
5885 else
5886 {
5887 buf->b_p_swf = p_swf;
5888 COPY_OPT_SCTX(buf, BV_INF);
5889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005891 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005892#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005893 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005894 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005896#ifdef FEAT_COMPL_FUNC
5897 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005898 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005899 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005901#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005902#ifdef FEAT_EVAL
5903 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005904 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005907 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005909#ifdef FEAT_VARTABS
5910 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005911 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005912 if (p_vsts && p_vsts != empty_option)
5913 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5914 else
5915 buf->b_p_vsts_array = 0;
5916 buf->b_p_vsts_nopaste = p_vsts_nopaste
5917 ? vim_strsave(p_vsts_nopaste) : NULL;
5918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005920 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005922 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923#ifdef FEAT_FOLDING
5924 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005925 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926#endif
5927 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005928 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005929 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005930 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005931 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5932 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005936 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937#ifdef FEAT_SMARTINDENT
5938 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005939 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940#endif
5941 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005942 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943#ifdef FEAT_CINDENT
5944 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005947 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005951 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005954 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5956 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#endif
5959#ifdef FEAT_LISP
5960 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005961 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962#endif
5963#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005964 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005966 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005968 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005969#endif
5970#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005971 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005972 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005973 (void)compile_cap_prog(&buf->b_s);
5974 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005975 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005976 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005978 buf->b_s.b_p_spo = vim_strsave(p_spo);
5979 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980#endif
5981#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5982 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005983 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005985 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005987 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005988#if defined(FEAT_EVAL)
5989 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992#ifdef FEAT_CRYPT
5993 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005994 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995#endif
5996#ifdef FEAT_SEARCHPATH
5997 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005998 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999#endif
6000#ifdef FEAT_KEYMAP
6001 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006002 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 buf->b_kmap_state |= KEYMAP_INIT;
6004#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006005#ifdef FEAT_TERMINAL
6006 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006008#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006009 // This isn't really an option, but copying the langmap and IME
6010 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006012 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006014 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006016 // options that are normally global but also have a local value
6017 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006019 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006020 buf->b_p_bkc = empty_option;
6021 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022#ifdef FEAT_QUICKFIX
6023 buf->b_p_gp = empty_option;
6024 buf->b_p_mp = empty_option;
6025 buf->b_p_efm = empty_option;
6026#endif
6027 buf->b_p_ep = empty_option;
6028 buf->b_p_kp = empty_option;
6029 buf->b_p_path = empty_option;
6030 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006031 buf->b_p_tc = empty_option;
6032 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033#ifdef FEAT_FIND_ID
6034 buf->b_p_def = empty_option;
6035 buf->b_p_inc = empty_option;
6036# ifdef FEAT_EVAL
6037 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006038 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039# endif
6040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 buf->b_p_dict = empty_option;
6042 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006043#ifdef FEAT_TEXTOBJ
6044 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006045 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006046#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006047#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6048 buf->b_p_bexpr = empty_option;
6049#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006050#if defined(FEAT_CRYPT)
6051 buf->b_p_cm = empty_option;
6052#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006053#ifdef FEAT_PERSISTENT_UNDO
6054 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006055 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006056#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006057#ifdef FEAT_LISP
6058 buf->b_p_lw = empty_option;
6059#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006060 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
6062 /*
6063 * Don't copy the options set by ex_help(), use the saved values,
6064 * when going from a help buffer to a non-help buffer.
6065 * Don't touch these at all when BCO_NOHELP is used and going from
6066 * or to a help buffer.
6067 */
6068 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006071#ifdef FEAT_VARTABS
6072 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6073 tabstop_set(p_vts, &buf->b_p_vts_array);
6074 else
6075 buf->b_p_vts_array = NULL;
6076#endif
6077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 else
6079 {
6080 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006081 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 did_isk = TRUE;
6083 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006084#ifdef FEAT_VARTABS
6085 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006086 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006087 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6088 tabstop_set(p_vts, &buf->b_p_vts_array);
6089 else
6090 buf->b_p_vts_array = NULL;
6091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (buf->b_p_bt[0] == 'h')
6094 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006096 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 }
6098 }
6099
6100 /*
6101 * When the options should be copied (ignoring BCO_ALWAYS), set the
6102 * flag that indicates that the options have been initialized.
6103 */
6104 if (should_copy)
6105 buf->b_p_initialized = TRUE;
6106 }
6107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006108 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (did_isk)
6110 (void)buf_init_chartab(buf, FALSE);
6111}
6112
6113/*
6114 * Reset the 'modifiable' option and its default value.
6115 */
6116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006117reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 int opt_idx;
6120
6121 curbuf->b_p_ma = FALSE;
6122 p_ma = FALSE;
6123 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006124 if (opt_idx >= 0)
6125 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126}
6127
6128/*
6129 * Set the global value for 'iminsert' to the local value.
6130 */
6131 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006132set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 p_iminsert = curbuf->b_p_iminsert;
6135}
6136
6137/*
6138 * Set the global value for 'imsearch' to the local value.
6139 */
6140 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006141set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
6143 p_imsearch = curbuf->b_p_imsearch;
6144}
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146static int expand_option_idx = -1;
6147static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6148static int expand_option_flags = 0;
6149
6150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006151set_context_in_set_cmd(
6152 expand_T *xp,
6153 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006154 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006157 long_u flags = 0; // init for GCC
6158 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 char_u *p;
6160 char_u *s;
6161 int is_term_option = FALSE;
6162 int key;
6163
6164 expand_option_flags = opt_flags;
6165
6166 xp->xp_context = EXPAND_SETTINGS;
6167 if (*arg == NUL)
6168 {
6169 xp->xp_pattern = arg;
6170 return;
6171 }
6172 p = arg + STRLEN(arg) - 1;
6173 if (*p == ' ' && *(p - 1) != '\\')
6174 {
6175 xp->xp_pattern = p + 1;
6176 return;
6177 }
6178 while (p > arg)
6179 {
6180 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006181 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (*p == ' ' || *p == ',')
6183 {
6184 while (s > arg && *(s - 1) == '\\')
6185 --s;
6186 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006187 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (*p == ' ' && ((p - s) & 1) == 0)
6189 {
6190 ++p;
6191 break;
6192 }
6193 --p;
6194 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006195 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 xp->xp_context = EXPAND_BOOL_SETTINGS;
6198 p += 2;
6199 }
6200 if (STRNCMP(p, "inv", 3) == 0)
6201 {
6202 xp->xp_context = EXPAND_BOOL_SETTINGS;
6203 p += 3;
6204 }
6205 xp->xp_pattern = arg = p;
6206 if (*arg == '<')
6207 {
6208 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006209 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 return;
6211 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006212 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
6214 xp->xp_context = EXPAND_NOTHING;
6215 return;
6216 }
6217 nextchar = *++p;
6218 is_term_option = TRUE;
6219 expand_option_name[2] = KEY2TERMCAP0(key);
6220 expand_option_name[3] = KEY2TERMCAP1(key);
6221 }
6222 else
6223 {
6224 if (p[0] == 't' && p[1] == '_')
6225 {
6226 p += 2;
6227 if (*p != NUL)
6228 ++p;
6229 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006230 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 nextchar = *++p;
6232 is_term_option = TRUE;
6233 expand_option_name[2] = p[-2];
6234 expand_option_name[3] = p[-1];
6235 }
6236 else
6237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006238 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6240 p++;
6241 if (*p == NUL)
6242 return;
6243 nextchar = *p;
6244 *p = NUL;
6245 opt_idx = findoption(arg);
6246 *p = nextchar;
6247 if (opt_idx == -1 || options[opt_idx].var == NULL)
6248 {
6249 xp->xp_context = EXPAND_NOTHING;
6250 return;
6251 }
6252 flags = options[opt_idx].flags;
6253 if (flags & P_BOOL)
6254 {
6255 xp->xp_context = EXPAND_NOTHING;
6256 return;
6257 }
6258 }
6259 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006260 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6262 {
6263 ++p;
6264 nextchar = '=';
6265 }
6266 if ((nextchar != '=' && nextchar != ':')
6267 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6268 {
6269 xp->xp_context = EXPAND_UNSUCCESSFUL;
6270 return;
6271 }
6272 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6273 {
6274 xp->xp_context = EXPAND_OLD_SETTING;
6275 if (is_term_option)
6276 expand_option_idx = -1;
6277 else
6278 expand_option_idx = opt_idx;
6279 xp->xp_pattern = p + 1;
6280 return;
6281 }
6282 xp->xp_context = EXPAND_NOTHING;
6283 if (is_term_option || (flags & P_NUM))
6284 return;
6285
6286 xp->xp_pattern = p + 1;
6287
6288 if (flags & P_EXPAND)
6289 {
6290 p = options[opt_idx].var;
6291 if (p == (char_u *)&p_bdir
6292 || p == (char_u *)&p_dir
6293 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006294 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 || p == (char_u *)&p_rtp
6296#ifdef FEAT_SEARCHPATH
6297 || p == (char_u *)&p_cdpath
6298#endif
6299#ifdef FEAT_SESSION
6300 || p == (char_u *)&p_vdir
6301#endif
6302 )
6303 {
6304 xp->xp_context = EXPAND_DIRECTORIES;
6305 if (p == (char_u *)&p_path
6306#ifdef FEAT_SEARCHPATH
6307 || p == (char_u *)&p_cdpath
6308#endif
6309 )
6310 xp->xp_backslash = XP_BS_THREE;
6311 else
6312 xp->xp_backslash = XP_BS_ONE;
6313 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006314 else if (p == (char_u *)&p_ft)
6315 {
6316 xp->xp_context = EXPAND_FILETYPE;
6317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 else
6319 {
6320 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006321 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (p == (char_u *)&p_tags)
6323 xp->xp_backslash = XP_BS_THREE;
6324 else
6325 xp->xp_backslash = XP_BS_ONE;
6326 }
6327 }
6328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006329 // For an option that is a list of file names, find the start of the
6330 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006333 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (*p == ' ' || *p == ',')
6335 {
6336 s = p;
6337 while (s > xp->xp_pattern && *(s - 1) == '\\')
6338 --s;
6339 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6340 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6341 {
6342 xp->xp_pattern = p + 1;
6343 break;
6344 }
6345 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006348 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006349 if (options[opt_idx].var == (char_u *)&p_sps
6350 && STRNCMP(p, "file:", 5) == 0)
6351 {
6352 xp->xp_pattern = p + 5;
6353 break;
6354 }
6355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357
6358 return;
6359}
6360
6361 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006362ExpandSettings(
6363 expand_T *xp,
6364 regmatch_T *regmatch,
6365 int *num_file,
6366 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006368 int num_normal = 0; // Nr of matching non-term-code settings
6369 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 int opt_idx;
6371 int match;
6372 int count = 0;
6373 char_u *str;
6374 int loop;
6375 int is_term_opt;
6376 char_u name_buf[MAX_KEY_NAME_LEN];
6377 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006378 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006380 // do this loop twice:
6381 // loop == 0: count the number of matching options
6382 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 for (loop = 0; loop <= 1; ++loop)
6384 {
6385 regmatch->rm_ic = ic;
6386 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6387 {
K.Takataeeec2542021-06-02 13:28:16 +02006388 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6390 {
6391 if (loop == 0)
6392 num_normal++;
6393 else
6394 (*file)[count++] = vim_strsave((char_u *)names[match]);
6395 }
6396 }
6397 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6398 opt_idx++)
6399 {
6400 if (options[opt_idx].var == NULL)
6401 continue;
6402 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6403 && !(options[opt_idx].flags & P_BOOL))
6404 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006405 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (is_term_opt && num_normal > 0)
6407 continue;
6408 match = FALSE;
6409 if (vim_regexec(regmatch, str, (colnr_T)0)
6410 || (options[opt_idx].shortname != NULL
6411 && vim_regexec(regmatch,
6412 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6413 match = TRUE;
6414 else if (is_term_opt)
6415 {
6416 name_buf[0] = '<';
6417 name_buf[1] = 't';
6418 name_buf[2] = '_';
6419 name_buf[3] = str[2];
6420 name_buf[4] = str[3];
6421 name_buf[5] = '>';
6422 name_buf[6] = NUL;
6423 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6424 {
6425 match = TRUE;
6426 str = name_buf;
6427 }
6428 }
6429 if (match)
6430 {
6431 if (loop == 0)
6432 {
6433 if (is_term_opt)
6434 num_term++;
6435 else
6436 num_normal++;
6437 }
6438 else
6439 (*file)[count++] = vim_strsave(str);
6440 }
6441 }
6442 /*
6443 * Check terminal key codes, these are not in the option table
6444 */
6445 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6446 {
6447 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6448 {
6449 if (!isprint(str[0]) || !isprint(str[1]))
6450 continue;
6451
6452 name_buf[0] = 't';
6453 name_buf[1] = '_';
6454 name_buf[2] = str[0];
6455 name_buf[3] = str[1];
6456 name_buf[4] = NUL;
6457
6458 match = FALSE;
6459 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6460 match = TRUE;
6461 else
6462 {
6463 name_buf[0] = '<';
6464 name_buf[1] = 't';
6465 name_buf[2] = '_';
6466 name_buf[3] = str[0];
6467 name_buf[4] = str[1];
6468 name_buf[5] = '>';
6469 name_buf[6] = NUL;
6470
6471 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6472 match = TRUE;
6473 }
6474 if (match)
6475 {
6476 if (loop == 0)
6477 num_term++;
6478 else
6479 (*file)[count++] = vim_strsave(name_buf);
6480 }
6481 }
6482
6483 /*
6484 * Check special key names.
6485 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006486 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6488 {
6489 name_buf[0] = '<';
6490 STRCPY(name_buf + 1, str);
6491 STRCAT(name_buf, ">");
6492
6493 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6494 {
6495 if (loop == 0)
6496 num_term++;
6497 else
6498 (*file)[count++] = vim_strsave(name_buf);
6499 }
6500 }
6501 }
6502 if (loop == 0)
6503 {
6504 if (num_normal > 0)
6505 *num_file = num_normal;
6506 else if (num_term > 0)
6507 *num_file = num_term;
6508 else
6509 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006510 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 if (*file == NULL)
6512 {
6513 *file = (char_u **)"";
6514 return FAIL;
6515 }
6516 }
6517 }
6518 return OK;
6519}
6520
6521 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006522ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006524 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 char_u *buf;
6526
6527 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006528 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (*file == NULL)
6530 return FAIL;
6531
6532 /*
6533 * For a terminal key code expand_option_idx is < 0.
6534 */
6535 if (expand_option_idx < 0)
6536 {
6537 var = find_termcode(expand_option_name + 2);
6538 if (var == NULL)
6539 expand_option_idx = findoption(expand_option_name);
6540 }
6541
6542 if (expand_option_idx >= 0)
6543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006544 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 option_value2string(&options[expand_option_idx], expand_option_flags);
6546 var = NameBuff;
6547 }
6548 else if (var == NULL)
6549 var = (char_u *)"";
6550
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006551 // A backslash is required before some characters. This is the reverse of
6552 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 buf = vim_strsave_escaped(var, escape_chars);
6554
6555 if (buf == NULL)
6556 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006557 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 return FAIL;
6559 }
6560
6561#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006562 // For MS-Windows et al. we don't double backslashes at the start and
6563 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006564 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (var[0] == '\\' && var[1] == '\\'
6566 && expand_option_idx >= 0
6567 && (options[expand_option_idx].flags & P_EXPAND)
6568 && vim_isfilec(var[2])
6569 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006570 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571#endif
6572
6573 *file[0] = buf;
6574 *num_file = 1;
6575 return OK;
6576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578/*
6579 * Get the value for the numeric or string option *opp in a nice format into
6580 * NameBuff[]. Must not be called with a hidden option!
6581 */
6582 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006583option_value2string(
6584 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006585 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 char_u *varp;
6588
6589 varp = get_varp_scope(opp, opt_flags);
6590
6591 if (opp->flags & P_NUM)
6592 {
6593 long wc = 0;
6594
6595 if (wc_use_keyname(varp, &wc))
6596 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6597 else if (wc != 0)
6598 STRCPY(NameBuff, transchar((int)wc));
6599 else
6600 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6601 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006602 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {
6604 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006605 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 NameBuff[0] = NUL;
6607#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006608 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006609 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 STRCPY(NameBuff, "*****");
6611#endif
6612 else if (opp->flags & P_EXPAND)
6613 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006614 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 else if ((char_u **)opp->var == &p_pt)
6616 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6617 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006618 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 }
6620}
6621
6622/*
6623 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6624 * printed as a keyname.
6625 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6626 */
6627 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006628wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
6630 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6631 {
6632 *wcp = *(long *)varp;
6633 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6634 return TRUE;
6635 }
6636 return FALSE;
6637}
6638
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 * Return TRUE if "x" is present in 'shortmess' option, or
6641 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6642 */
6643 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006644shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006646 return p_shm != NULL &&
6647 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 || (vim_strchr(p_shm, 'a') != NULL
6649 && vim_strchr((char_u *)SHM_A, x) != NULL));
6650}
6651
6652/*
6653 * paste_option_changed() - Called after p_paste was set or reset.
6654 */
6655 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006656paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 static int old_p_paste = FALSE;
6659 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006660 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661#ifdef FEAT_CMDL_INFO
6662 static int save_ru = 0;
6663#endif
6664#ifdef FEAT_RIGHTLEFT
6665 static int save_ri = 0;
6666 static int save_hkmap = 0;
6667#endif
6668 buf_T *buf;
6669
6670 if (p_paste)
6671 {
6672 /*
6673 * Paste switched from off to on.
6674 * Save the current values, so they can be restored later.
6675 */
6676 if (!old_p_paste)
6677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006678 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006679 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
6681 buf->b_p_tw_nopaste = buf->b_p_tw;
6682 buf->b_p_wm_nopaste = buf->b_p_wm;
6683 buf->b_p_sts_nopaste = buf->b_p_sts;
6684 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006685 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006686#ifdef FEAT_VARTABS
6687 if (buf->b_p_vsts_nopaste)
6688 vim_free(buf->b_p_vsts_nopaste);
6689 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6690 ? vim_strsave(buf->b_p_vsts) : NULL;
6691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 }
6693
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006694 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006696 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697#ifdef FEAT_CMDL_INFO
6698 save_ru = p_ru;
6699#endif
6700#ifdef FEAT_RIGHTLEFT
6701 save_ri = p_ri;
6702 save_hkmap = p_hkmap;
6703#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006705 p_ai_nopaste = p_ai;
6706 p_et_nopaste = p_et;
6707 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 p_tw_nopaste = p_tw;
6709 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006710#ifdef FEAT_VARTABS
6711 if (p_vsts_nopaste)
6712 vim_free(p_vsts_nopaste);
6713 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716
6717 /*
6718 * Always set the option values, also when 'paste' is set when it is
6719 * already on.
6720 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006721 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006722 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006724 buf->b_p_tw = 0; // textwidth is 0
6725 buf->b_p_wm = 0; // wrapmargin is 0
6726 buf->b_p_sts = 0; // softtabstop is 0
6727 buf->b_p_ai = 0; // no auto-indent
6728 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006729#ifdef FEAT_VARTABS
6730 if (buf->b_p_vsts)
6731 free_string_option(buf->b_p_vsts);
6732 buf->b_p_vsts = empty_option;
6733 if (buf->b_p_vsts_array)
6734 vim_free(buf->b_p_vsts_array);
6735 buf->b_p_vsts_array = 0;
6736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
6738
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 // set global options
6740 p_sm = 0; // no showmatch
6741 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 status_redraw_all(); // redraw to remove the ruler
6745 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746#endif
6747#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006748 p_ri = 0; // no reverse insert
6749 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006751 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 p_tw = 0;
6753 p_wm = 0;
6754 p_sts = 0;
6755 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006756#ifdef FEAT_VARTABS
6757 if (p_vsts)
6758 free_string_option(p_vsts);
6759 p_vsts = empty_option;
6760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
6762
6763 /*
6764 * Paste switched from on to off: Restore saved values.
6765 */
6766 else if (old_p_paste)
6767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006768 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006769 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {
6771 buf->b_p_tw = buf->b_p_tw_nopaste;
6772 buf->b_p_wm = buf->b_p_wm_nopaste;
6773 buf->b_p_sts = buf->b_p_sts_nopaste;
6774 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006775 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006776#ifdef FEAT_VARTABS
6777 if (buf->b_p_vsts)
6778 free_string_option(buf->b_p_vsts);
6779 buf->b_p_vsts = buf->b_p_vsts_nopaste
6780 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6781 if (buf->b_p_vsts_array)
6782 vim_free(buf->b_p_vsts_array);
6783 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6784 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6785 else
6786 buf->b_p_vsts_array = 0;
6787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 }
6789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006790 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006792 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006795 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 p_ru = save_ru;
6797#endif
6798#ifdef FEAT_RIGHTLEFT
6799 p_ri = save_ri;
6800 p_hkmap = save_hkmap;
6801#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006803 p_ai = p_ai_nopaste;
6804 p_et = p_et_nopaste;
6805 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 p_tw = p_tw_nopaste;
6807 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006808#ifdef FEAT_VARTABS
6809 if (p_vsts)
6810 free_string_option(p_vsts);
6811 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814
6815 old_p_paste = p_paste;
6816}
6817
6818/*
6819 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6820 *
6821 * Reset 'compatible' and set the values for options that didn't get set yet
6822 * to the Vim defaults.
6823 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006824 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 */
6826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006827vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006829 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006830 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
6833 if (!option_was_set((char_u *)"cp"))
6834 {
6835 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006836 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6838 set_option_default(opt_idx, OPT_FREE, FALSE);
6839 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006840 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006842
6843 if (fname != NULL)
6844 {
6845 p = vim_getenv(envname, &dofree);
6846 if (p == NULL)
6847 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006848 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006849 p = FullName_save(fname, FALSE);
6850 if (p != NULL)
6851 {
6852 vim_setenv(envname, p);
6853 vim_free(p);
6854 }
6855 }
6856 else if (dofree)
6857 vim_free(p);
6858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859}
6860
6861/*
6862 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6863 */
6864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006865change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006867 int opt_idx;
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (p_cp != on)
6870 {
6871 p_cp = on;
6872 compatible_set();
6873 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006874 opt_idx = findoption((char_u *)"cp");
6875 if (opt_idx >= 0)
6876 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877}
6878
6879/*
6880 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006881 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 */
6883 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006884option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 int idx;
6887
6888 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006889 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 return FALSE;
6891 if (options[idx].flags & P_WAS_SET)
6892 return TRUE;
6893 return FALSE;
6894}
6895
6896/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006897 * Reset the flag indicating option "name" was set.
6898 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006899 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006900reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006901{
6902 int idx = findoption(name);
6903
6904 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006905 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006906 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006907 return OK;
6908 }
6909 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006910}
6911
6912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 * compatible_set() - Called when 'compatible' has been set or unset.
6914 *
6915 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6916 * flag) to a Vi compatible value.
6917 * When 'compatible' is unset: Set all options that have a different default
6918 * for Vim (without the P_VI_DEF flag) to that default.
6919 */
6920 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006921compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922{
6923 int opt_idx;
6924
Bram Moolenaardac13472019-09-16 21:06:21 +02006925 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6927 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6928 set_option_default(opt_idx, OPT_FREE, p_cp);
6929 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006930 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931}
6932
Bram Moolenaardac13472019-09-16 21:06:21 +02006933#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935/*
6936 * fill_breakat_flags() -- called when 'breakat' changes value.
6937 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006939fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006941 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 int i;
6943
6944 for (i = 0; i < 256; i++)
6945 breakat_flags[i] = FALSE;
6946
6947 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006948 for (p = p_breakat; *p; p++)
6949 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951#endif
6952
6953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 * Check if backspacing over something is allowed.
6955 */
6956 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006957can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006958 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006960#ifdef FEAT_JOB_CHANNEL
6961 if (what == BS_START && bt_prompt(curbuf))
6962 return FALSE;
6963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 switch (*p_bs)
6965 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006966 case '3': return TRUE;
6967 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 case '1': return (what != BS_START);
6969 case '0': return FALSE;
6970 }
6971 return vim_strchr(p_bs, what) != NULL;
6972}
6973
6974/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006975 * Return the effective 'scrolloff' value for the current window, using the
6976 * global value when appropriate.
6977 */
6978 long
6979get_scrolloff_value(void)
6980{
6981 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6982}
6983
6984/*
6985 * Return the effective 'sidescrolloff' value for the current window, using the
6986 * global value when appropriate.
6987 */
6988 long
6989get_sidescrolloff_value(void)
6990{
6991 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6992}
6993
6994/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006995 * Get the local or global value of 'backupcopy'.
6996 */
6997 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006998get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006999{
7000 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7001}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007002
Bram Moolenaaree857022019-11-09 23:26:40 +01007003#if defined(FEAT_LINEBREAK) || defined(PROTO)
7004/*
7005 * Get the local or global value of 'showbreak'.
7006 */
7007 char_u *
7008get_showbreak_value(win_T *win)
7009{
7010 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7011 return p_sbr;
7012 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7013 return empty_option;
7014 return win->w_p_sbr;
7015}
7016#endif
7017
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007018#if defined(FEAT_EVAL) || defined(PROTO)
7019/*
7020 * Get window or buffer local options.
7021 */
7022 dict_T *
7023get_winbuf_options(int bufopt)
7024{
7025 dict_T *d;
7026 int opt_idx;
7027
7028 d = dict_alloc();
7029 if (d == NULL)
7030 return NULL;
7031
Bram Moolenaardac13472019-09-16 21:06:21 +02007032 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007033 {
7034 struct vimoption *opt = &options[opt_idx];
7035
7036 if ((bufopt && (opt->indir & PV_BUF))
7037 || (!bufopt && (opt->indir & PV_WIN)))
7038 {
7039 char_u *varp = get_varp(opt);
7040
7041 if (varp != NULL)
7042 {
7043 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007044 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007045 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007046 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007047 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007048 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007049 }
7050 }
7051 }
7052
7053 return d;
7054}
7055#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007056
Bram Moolenaardac13472019-09-16 21:06:21 +02007057#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007058/*
7059 * This is called when 'culopt' is changed
7060 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007061 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007062fill_culopt_flags(char_u *val, win_T *wp)
7063{
7064 char_u *p;
7065 char_u culopt_flags_new = 0;
7066
7067 if (val == NULL)
7068 p = wp->w_p_culopt;
7069 else
7070 p = val;
7071 while (*p != NUL)
7072 {
7073 if (STRNCMP(p, "line", 4) == 0)
7074 {
7075 p += 4;
7076 culopt_flags_new |= CULOPT_LINE;
7077 }
7078 else if (STRNCMP(p, "both", 4) == 0)
7079 {
7080 p += 4;
7081 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7082 }
7083 else if (STRNCMP(p, "number", 6) == 0)
7084 {
7085 p += 6;
7086 culopt_flags_new |= CULOPT_NBR;
7087 }
7088 else if (STRNCMP(p, "screenline", 10) == 0)
7089 {
7090 p += 10;
7091 culopt_flags_new |= CULOPT_SCRLINE;
7092 }
7093
7094 if (*p != ',' && *p != NUL)
7095 return FAIL;
7096 if (*p == ',')
7097 ++p;
7098 }
7099
7100 // Can't have both "line" and "screenline".
7101 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7102 return FAIL;
7103 wp->w_p_culopt_flags = culopt_flags_new;
7104
7105 return OK;
7106}
7107#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007108
7109/*
7110 * Get the value of 'magic' adjusted for Vim9 script.
7111 */
7112 int
7113magic_isset(void)
7114{
7115 switch (magic_overruled)
7116 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007117 case OPTION_MAGIC_ON: return TRUE;
7118 case OPTION_MAGIC_OFF: return FALSE;
7119 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007120 }
7121#ifdef FEAT_EVAL
7122 if (in_vim9script())
7123 return TRUE;
7124#endif
7125 return p_magic;
7126}