blob: 4f080a3739d2dca2c6d5cf4f5ce11ee232cf0e54 [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 Moolenaar77111e22021-07-29 21:11:30 +0200810 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000811 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000812 free_operatorfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000813}
814#endif
815
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817/*
818 * Initialize the options, part two: After getting Rows and Columns and
819 * setting 'term'.
820 */
821 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100822set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000824 int idx;
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100827 * 'scroll' defaults to half the window height. The stored default is zero,
828 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000830 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000831 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000832 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 comp_col();
834
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000835 /*
836 * 'window' is only for backwards compatibility with Vi.
837 * Default is Rows - 1.
838 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000839 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000840 p_window = Rows - 1;
841 set_number_default("window", Rows - 1);
842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100843 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100844#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000845 /*
846 * If 'background' wasn't set by the user, try guessing the value,
847 * depending on the terminal name. Only need to check for terminals
848 * with a dark background, that can handle color.
849 */
850 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000851 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
852 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000854 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 // don't mark it as set, when starting the GUI it may be
856 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000857 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000860
861#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000863#endif
864#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000866#endif
867#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100868 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870}
871
872/*
873 * Initialize the options, part three: After reading the .vimrc
874 */
875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100876set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100878#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879/*
880 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
881 * This is done after other initializations, where 'shell' might have been
882 * set, but only if they have not been set before.
883 */
884 char_u *p;
885 int idx_srr;
886 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100887# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 int idx_sp;
889 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000893 if (idx_srr < 0)
894 do_srr = FALSE;
895 else
896 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100897# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000899 if (idx_sp < 0)
900 do_sp = FALSE;
901 else
902 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100903# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200904 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (p != NULL)
906 {
907 /*
908 * Default for p_sp is "| tee", for p_srr is ">".
909 * For known shells it is changed here to include stderr.
910 */
911 if ( fnamecmp(p, "csh") == 0
912 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100913# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 || fnamecmp(p, "csh.exe") == 0
915 || fnamecmp(p, "tcsh.exe") == 0
916# endif
917 )
918 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100919# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (do_sp)
921 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100922# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100924# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
928 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (do_srr)
931 {
932 p_srr = (char_u *)">&";
933 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
934 }
935 }
Mike Williams12795022021-06-28 20:53:58 +0200936# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200937 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
938 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200939 else if ( fnamecmp(p, "powershell") == 0
940 || fnamecmp(p, "powershell.exe") == 0
941 )
942 {
943# if defined(FEAT_QUICKFIX)
944 if (do_sp)
945 {
946 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
947 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
948 }
949# endif
950 if (do_srr)
951 {
952 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
953 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
954 }
955 }
956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 else
Natanael Copa56318362021-05-06 18:46:35 +0200958 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if ( fnamecmp(p, "sh") == 0
960 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200961 || fnamecmp(p, "mksh") == 0
962 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000964 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200966 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200967 || fnamecmp(p, "ash") == 0
968 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200969 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100970# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 || fnamecmp(p, "cmd") == 0
972 || fnamecmp(p, "sh.exe") == 0
973 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200974 || fnamecmp(p, "mksh.exe") == 0
975 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000977 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 || fnamecmp(p, "bash.exe") == 0
979 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200980 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200981 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100983 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100985# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if (do_sp)
987 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100988# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100990# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200991 if (fnamecmp(p, "pwsh") == 0)
992 p_sp = (char_u *)">%s 2>&1";
993 else
994 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
997 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (do_srr)
1000 {
1001 p_srr = (char_u *)">%s 2>&1";
1002 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1003 }
1004 }
1005 vim_free(p);
1006 }
1007#endif
1008
Bram Moolenaar4f974752019-02-17 17:44:42 +01001009#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001011 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1012 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001014 * set, but only if they have not been set before.
1015 * Default values depend on shell (cmd.exe is default shell):
1016 *
1017 * p_shcf p_sxq
1018 * cmd.exe - "/c" "("
1019 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001020 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001021 * "sh" like shells - "-c" "\""
1022 *
1023 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 */
Mike Williams12795022021-06-28 20:53:58 +02001025 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1026 {
1027 int idx_opt;
1028
1029 idx_opt = findoption((char_u *)"shcf");
1030 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1031 {
1032 p_shcf = (char_u*)"-Command";
1033 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1034 }
1035
1036 idx_opt = findoption((char_u *)"sxq");
1037 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1038 {
1039 p_sxq = (char_u*)"\"";
1040 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1041 }
1042 }
1043 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
1045 int idx3;
1046
1047 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001048 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
1050 p_shcf = (char_u *)"-c";
1051 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1052 }
1053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001054 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001056 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 {
1058 p_sxq = (char_u *)"\"";
1059 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001062 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1063 {
1064 int idx3;
1065
1066 /*
1067 * cmd.exe on Windows will strip the first and last double quote given
1068 * on the command line, e.g. most of the time things like:
1069 * cmd /c "my path/to/echo" "my args to echo"
1070 * become:
1071 * my path/to/echo" "my args to echo
1072 * when executed.
1073 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001074 * To avoid this, set shellxquote to surround the command in
1075 * parenthesis. This appears to make most commands work, without
1076 * breaking commands that worked previously, such as
1077 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001079 idx3 = findoption((char_u *)"sxq");
1080 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1081 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001082 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001083 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1084 }
1085
Bram Moolenaara64ba222012-02-12 23:23:31 +01001086 idx3 = findoption((char_u *)"shcf");
1087 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1088 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001089 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001090 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1091 }
1092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#endif
1094
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001095 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001096 {
1097 int idx_ffs = findoption((char_u *)"ffs");
1098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001099 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001100 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1101 set_fileformat(default_fileformat(), OPT_LOCAL);
1102 }
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#ifdef FEAT_TITLE
1105 set_title_defaults();
1106#endif
1107}
1108
1109#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1110/*
1111 * When 'helplang' is still at its default value, set it to "lang".
1112 * Only the first two characters of "lang" are used.
1113 */
1114 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001115set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 int idx;
1118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001119 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 return;
1121 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001122 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 if (options[idx].flags & P_ALLOCED)
1125 free_string_option(p_hlg);
1126 p_hlg = vim_strsave(lang);
1127 if (p_hlg == NULL)
1128 p_hlg = empty_option;
1129 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001130 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001131 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001132 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1133 {
1134 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1135 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1136 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001137 // any C like setting, such as C.UTF-8, becomes "en"
1138 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1139 {
1140 p_hlg[0] = 'e';
1141 p_hlg[1] = 'n';
1142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 options[idx].flags |= P_ALLOCED;
1146 }
1147}
1148#endif
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#ifdef FEAT_TITLE
1151/*
1152 * 'title' and 'icon' only default to true if they have not been set or reset
1153 * in .vimrc and we can read the old value.
1154 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1155 * they can be reset. This reduces startup time when using X on a remote
1156 * machine.
1157 */
1158 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001159set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160{
1161 int idx1;
1162 long val;
1163
1164 /*
1165 * If GUI is (going to be) used, we can always set the window title and
1166 * icon name. Saves a bit of time, because the X11 display server does
1167 * not need to be contacted.
1168 */
1169 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001170 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172#ifdef FEAT_GUI
1173 if (gui.starting || gui.in_use)
1174 val = TRUE;
1175 else
1176#endif
1177 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001178 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 p_title = val;
1180 }
1181 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001182 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184#ifdef FEAT_GUI
1185 if (gui.starting || gui.in_use)
1186 val = TRUE;
1187 else
1188#endif
1189 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001190 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 p_icon = val;
1192 }
1193}
1194#endif
1195
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001196 void
1197ex_set(exarg_T *eap)
1198{
1199 int flags = 0;
1200
1201 if (eap->cmdidx == CMD_setlocal)
1202 flags = OPT_LOCAL;
1203 else if (eap->cmdidx == CMD_setglobal)
1204 flags = OPT_GLOBAL;
1205#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001206 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001207 ex_options(eap);
1208 else
1209#endif
1210 {
1211 if (eap->forceit)
1212 flags |= OPT_ONECOLUMN;
1213 (void)do_set(eap->arg, flags);
1214 }
1215}
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217/*
1218 * Parse 'arg' for option settings.
1219 *
1220 * 'arg' may be IObuff, but only when no errors can be present and option
1221 * does not need to be expanded with option_expand().
1222 * "opt_flags":
1223 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001224 * OPT_GLOBAL for ":setglobal"
1225 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001227 * OPT_WINONLY to only set window-local options
1228 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 *
1230 * returns FAIL if an error is detected, OK otherwise
1231 */
1232 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001233do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001234 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001235 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001237 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001239 char *errmsg;
1240 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001242 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1243 int nextchar; // next non-white char after option name
1244 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 int len;
1246 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001247 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001249 long_u flags; // flags for current option
1250 char_u *varp = NULL; // pointer to variable for current option
1251 int did_show = FALSE; // already showed one value
1252 int adding; // "opt+=arg"
1253 int prepending; // "opt^=arg"
1254 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 int cp_val = 0;
1256 char_u key_name[2];
1257
1258 if (*arg == NUL)
1259 {
1260 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001261 did_show = TRUE;
1262 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001265 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
1267 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001268 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001270 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1271 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 {
1273 /*
1274 * ":set all" show all options.
1275 * ":set all&" set all options to their default value.
1276 */
1277 arg += 3;
1278 if (*arg == '&')
1279 {
1280 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001281 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001283 didset_options();
1284 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001285 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001290 did_show = TRUE;
1291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001293 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {
1295 showoptions(2, opt_flags);
1296 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001297 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 arg += 7;
1299 }
1300 else
1301 {
1302 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001303 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
1305 prefix = 0;
1306 arg += 2;
1307 }
1308 else if (STRNCMP(arg, "inv", 3) == 0)
1309 {
1310 prefix = 2;
1311 arg += 3;
1312 }
1313
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001314 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 key = 0;
1316 if (*arg == '<')
1317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001319 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1321 len = 5;
1322 else
1323 {
1324 len = 1;
1325 while (arg[len] != NUL && arg[len] != '>')
1326 ++len;
1327 }
1328 if (arg[len] != '>')
1329 {
1330 errmsg = e_invarg;
1331 goto skip;
1332 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001333 arg[len] = NUL; // put NUL after name
1334 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001336 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001338 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340 else
1341 {
1342 len = 0;
1343 /*
1344 * The two characters after "t_" may not be alphanumeric.
1345 */
1346 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1347 len = 4;
1348 else
1349 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1350 ++len;
1351 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001352 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001354 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001356 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001359 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 afterchar = arg[len];
1361
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001362 if (in_vim9script())
1363 {
1364 char_u *p = skipwhite(arg + len);
1365
1366 // disallow white space before =val, +=val, -=val, ^=val
1367 if (p > arg + len && (p[0] == '='
1368 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1369 && p[1] == '=')))
1370 {
1371 errmsg = e_no_white_space_allowed_between_option_and;
1372 arg = p;
1373 startarg = p;
1374 goto skip;
1375 }
1376 }
1377 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001378 // skip white space, allow ":set ai ?", ":set hlsearch !"
1379 while (VIM_ISWHITE(arg[len]))
1380 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 adding = FALSE;
1383 prepending = FALSE;
1384 removing = FALSE;
1385 if (arg[len] != NUL && arg[len + 1] == '=')
1386 {
1387 if (arg[len] == '+')
1388 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001389 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 ++len;
1391 }
1392 else if (arg[len] == '^')
1393 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001394 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 ++len;
1396 }
1397 else if (arg[len] == '-')
1398 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001399 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 ++len;
1401 }
1402 }
1403 nextchar = arg[len];
1404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001405 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001407 if (in_vim9script() && arg > arg_start
1408 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1409 errmsg = e_no_white_space_allowed_between_option_and;
1410 else
1411 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 goto skip;
1413 }
1414
1415 if (opt_idx >= 0)
1416 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001417 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001419 // Only give an error message when requesting the value of
1420 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1422 && (!(options[opt_idx].flags & P_BOOL)
1423 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001424 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 goto skip;
1426 }
1427
1428 flags = options[opt_idx].flags;
1429 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1430 }
1431 else
1432 {
1433 flags = P_STRING;
1434 if (key < 0)
1435 {
1436 key_name[0] = KEY2TERMCAP0(key);
1437 key_name[1] = KEY2TERMCAP1(key);
1438 }
1439 else
1440 {
1441 key_name[0] = KS_KEY;
1442 key_name[1] = (key & 0xff);
1443 }
1444 }
1445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001446 // Skip all options that are not window-local (used when showing
1447 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001448 if ((opt_flags & OPT_WINONLY)
1449 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1450 goto skip;
1451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001452 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001453 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1454 && options[opt_idx].var == VAR_WIN)
1455 goto skip;
1456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001457 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001458 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001460 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001461 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001462 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001463 goto skip;
1464 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001465 if ((flags & P_MLE) && !p_mle)
1466 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001467 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001468 goto skip;
1469 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001470#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 // In diff mode some options are overruled. This avoids that
1472 // 'foldmethod' becomes "marker" instead of "diff" and that
1473 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001474 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001476 && (
1477#ifdef FEAT_FOLDING
1478 options[opt_idx].indir == PV_FDM ||
1479#endif
1480 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001481 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484
1485#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001486 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001487 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001489 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 goto skip;
1491 }
1492#endif
1493
1494 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1495 {
1496 arg += len;
1497 cp_val = p_cp;
1498 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001500 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 cp_val = FALSE;
1503 arg += 3;
1504 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001505 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
1507 cp_val = TRUE;
1508 arg += 2;
1509 }
1510 }
1511 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001512 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 errmsg = e_trailing;
1515 goto skip;
1516 }
1517 }
1518
1519 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001520 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001521 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
1523 if (nextchar == '?'
1524 || (prefix == 1
1525 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1526 && !(flags & P_BOOL)))
1527 {
1528 /*
1529 * print value
1530 */
1531 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001532 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 else
1534 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001535 gotocmdline(TRUE); // cursor at status line
1536 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 if (opt_idx >= 0)
1539 {
1540 showoneopt(&options[opt_idx], opt_flags);
1541#ifdef FEAT_EVAL
1542 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001544 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001545 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001546 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001547 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001548 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001549 (int)options[opt_idx].indir & PV_MASK]);
1550 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001551 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001552 (int)options[opt_idx].indir & PV_MASK]);
1553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554#endif
1555 }
1556 else
1557 {
1558 char_u *p;
1559
1560 p = find_termcode(key_name);
1561 if (p == NULL)
1562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001563 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 goto skip;
1565 }
1566 else
1567 (void)show_one_termcode(key_name, p, TRUE);
1568 }
1569 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001570 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 errmsg = e_trailing;
1572 }
1573 else
1574 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001575 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001576 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001578 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 if (nextchar == '=' || nextchar == ':')
1581 {
1582 errmsg = e_invarg;
1583 goto skip;
1584 }
1585
1586 /*
1587 * ":set opt!": invert
1588 * ":set opt&": reset to default value
1589 * ":set opt<": reset to global value
1590 */
1591 if (nextchar == '!')
1592 value = *(int *)(varp) ^ 1;
1593 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001594 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 ((flags & P_VI_DEF) || cp_val)
1596 ? VI_DEFAULT : VIM_DEFAULT];
1597 else if (nextchar == '<')
1598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001599 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if ((int *)varp == &curbuf->b_p_ar
1601 && opt_flags == OPT_LOCAL)
1602 value = -1;
1603 else
1604 value = *(int *)get_varp_scope(&(options[opt_idx]),
1605 OPT_GLOBAL);
1606 }
1607 else
1608 {
1609 /*
1610 * ":set invopt": invert
1611 * ":set opt" or ":set noopt": set or reset
1612 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001613 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 errmsg = e_trailing;
1616 goto skip;
1617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001618 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 value = *(int *)(varp) ^ 1;
1620 else
1621 value = prefix;
1622 }
1623
1624 errmsg = set_bool_option(opt_idx, varp, (int)value,
1625 opt_flags);
1626 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1630 || prefix != 1)
1631 {
1632 errmsg = e_invarg;
1633 goto skip;
1634 }
1635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001636 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 /*
1639 * Different ways to set a number option:
1640 * & set to default value
1641 * < set to global value
1642 * <xx> accept special key codes for 'wildchar'
1643 * c accept any non-digit for 'wildchar'
1644 * [-]0-9 set number
1645 * other error
1646 */
1647 ++arg;
1648 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001649 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 ((flags & P_VI_DEF) || cp_val)
1651 ? VI_DEFAULT : VIM_DEFAULT];
1652 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001653 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001654 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1655 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001656 if ((long *)varp == &curbuf->b_p_ul
1657 && opt_flags == OPT_LOCAL)
1658 value = NO_LOCAL_UNDOLEVEL;
1659 else
1660 value = *(long *)get_varp_scope(
1661 &(options[opt_idx]), OPT_GLOBAL);
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 else if (((long *)varp == &p_wc
1664 || (long *)varp == &p_wcm)
1665 && (*arg == '<'
1666 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001667 || (*arg != NUL
1668 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 && !VIM_ISDIGIT(*arg))))
1670 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001671 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (value == 0 && (long *)varp != &p_wcm)
1673 {
1674 errmsg = e_invarg;
1675 goto skip;
1676 }
1677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001680 // Allow negative (for 'undolevels'), octal and
1681 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001682 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001683 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001684 if (i == 0 || (arg[i] != NUL
1685 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001687 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 goto skip;
1689 }
1690 }
1691 else
1692 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001693 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 goto skip;
1695 }
1696
1697 if (adding)
1698 value = *(long *)varp + value;
1699 if (prepending)
1700 value = *(long *)varp * value;
1701 if (removing)
1702 value = *(long *)varp - value;
1703 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001704 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001706 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001708 char_u *save_arg = NULL;
1709 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001710 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001711 char_u *newval;
1712 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001713 char_u *origval_l = NULL;
1714 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001715#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001716 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001717 char_u *saved_origval_l = NULL;
1718 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001719 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001720#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001721 unsigned newlen;
1722 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001723 int new_value_alloced; // new string option
1724 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001726 // When using ":set opt=val" for a global option
1727 // with a local value the local value will be
1728 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001730 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 varp = options[opt_idx].var;
1732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001733 // The old value is kept until we are sure that the
1734 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001736
Bram Moolenaard7c96872019-06-15 17:12:48 +02001737 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1738 {
1739 origval_l = *(char_u **)get_varp_scope(
1740 &(options[opt_idx]), OPT_LOCAL);
1741 origval_g = *(char_u **)get_varp_scope(
1742 &(options[opt_idx]), OPT_GLOBAL);
1743
1744 // A global-local string option might have an empty
1745 // option as value to indicate that the global
1746 // value should be used.
1747 if (((int)options[opt_idx].indir & PV_BOTH)
1748 && origval_l == empty_option)
1749 origval_l = origval_g;
1750 }
1751
1752 // When setting the local value of a global
1753 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001754 if (((int)options[opt_idx].indir & PV_BOTH)
1755 && (opt_flags & OPT_LOCAL))
1756 origval = *(char_u **)get_varp(
1757 &options[opt_idx]);
1758 else
1759 origval = oldval;
1760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001761 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 {
1763 newval = options[opt_idx].def_val[
1764 ((flags & P_VI_DEF) || cp_val)
1765 ? VI_DEFAULT : VIM_DEFAULT];
1766 if ((char_u **)varp == &p_bg)
1767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001768 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#ifdef FEAT_GUI
1770 if (gui.in_use)
1771 newval = gui_bg_default();
1772 else
1773#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001774 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001776 else if ((char_u **)varp == &p_fencs && enc_utf8)
1777 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001779 // expand environment variables and ~ (since the
1780 // default value was already expanded, only
1781 // required when an environment variable was set
1782 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (newval == NULL)
1784 newval = empty_option;
1785 else
1786 {
1787 s = option_expand(opt_idx, newval);
1788 if (s == NULL)
1789 s = newval;
1790 newval = vim_strsave(s);
1791 }
1792 new_value_alloced = TRUE;
1793 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001794 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796 newval = vim_strsave(*(char_u **)get_varp_scope(
1797 &(options[opt_idx]), OPT_GLOBAL));
1798 new_value_alloced = TRUE;
1799 }
1800 else
1801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001802 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
1804 /*
1805 * Set 'keywordprg' to ":help" if an empty
1806 * value was passed to :set by the user.
1807 * Misuse errbuf[] for the resulting string.
1808 */
1809 if (varp == (char_u *)&p_kp
1810 && (*arg == NUL || *arg == ' '))
1811 {
1812 STRCPY(errbuf, ":help");
1813 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001814 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001817 * Convert 'backspace' number to string, for
1818 * adding, prepending and removing string.
1819 */
1820 else if (varp == (char_u *)&p_bs
1821 && VIM_ISDIGIT(**(char_u **)varp))
1822 {
1823 i = getdigits((char_u **)varp);
1824 switch (i)
1825 {
1826 case 0:
1827 *(char_u **)varp = empty_option;
1828 break;
1829 case 1:
1830 *(char_u **)varp = vim_strsave(
1831 (char_u *)"indent,eol");
1832 break;
1833 case 2:
1834 *(char_u **)varp = vim_strsave(
1835 (char_u *)"indent,eol,start");
1836 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001837 case 3:
1838 *(char_u **)varp = vim_strsave(
1839 (char_u *)"indent,eol,nostop");
1840 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001841 }
1842 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001843 if (origval == oldval)
1844 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001845 if (origval_l == oldval)
1846 origval_l = *(char_u **)varp;
1847 if (origval_g == oldval)
1848 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001849 oldval = *(char_u **)varp;
1850 }
1851 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 * Convert 'whichwrap' number to string, for
1853 * backwards compatibility with Vim 3.0.
1854 * Misuse errbuf[] for the resulting string.
1855 */
1856 else if (varp == (char_u *)&p_ww
1857 && VIM_ISDIGIT(*arg))
1858 {
1859 *errbuf = NUL;
1860 i = getdigits(&arg);
1861 if (i & 1)
1862 STRCAT(errbuf, "b,");
1863 if (i & 2)
1864 STRCAT(errbuf, "s,");
1865 if (i & 4)
1866 STRCAT(errbuf, "h,l,");
1867 if (i & 8)
1868 STRCAT(errbuf, "<,>,");
1869 if (i & 16)
1870 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001871 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 errbuf[STRLEN(errbuf) - 1] = NUL;
1873 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001874 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 /*
1877 * Remove '>' before 'dir' and 'bdir', for
1878 * backwards compatibility with version 3.0
1879 */
1880 else if ( *arg == '>'
1881 && (varp == (char_u *)&p_dir
1882 || varp == (char_u *)&p_bdir))
1883 {
1884 ++arg;
1885 }
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 /*
1888 * Copy the new string into allocated memory.
1889 * Can't use set_string_option_direct(), because
1890 * we need to remove the backslashes.
1891 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001892 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 newlen = (unsigned)STRLEN(arg) + 1;
1894 if (adding || prepending || removing)
1895 newlen += (unsigned)STRLEN(origval) + 1;
1896 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001897 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 break;
1899 s = newval;
1900
1901 /*
1902 * Copy the string, skip over escaped chars.
1903 * For MS-DOS and WIN32 backslashes before normal
1904 * file name characters are not removed, and keep
1905 * backslash at start, for "\\machine\path", but
1906 * do remove it for "\\\\machine\\path".
1907 * The reverse is found in ExpandOldSetting().
1908 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001909 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 if (*arg == '\\' && arg[1] != NUL
1912#ifdef BACKSLASH_IN_FILENAME
1913 && !((flags & P_EXPAND)
1914 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001915 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 && (arg[1] != '\\'
1917 || (s == newval
1918 && arg[2] != '\\')))
1919#endif
1920 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001921 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001923 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001925 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 mch_memmove(s, arg, (size_t)i);
1927 arg += i;
1928 s += i;
1929 }
1930 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 *s++ = *arg++;
1932 }
1933 *s = NUL;
1934
1935 /*
1936 * Expand environment variables and ~.
1937 * Don't do it when adding without inserting a
1938 * comma.
1939 */
1940 if (!(adding || prepending || removing)
1941 || (flags & P_COMMA))
1942 {
1943 s = option_expand(opt_idx, newval);
1944 if (s != NULL)
1945 {
1946 vim_free(newval);
1947 newlen = (unsigned)STRLEN(s) + 1;
1948 if (adding || prepending || removing)
1949 newlen += (unsigned)STRLEN(origval) + 1;
1950 newval = alloc(newlen);
1951 if (newval == NULL)
1952 break;
1953 STRCPY(newval, s);
1954 }
1955 }
1956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001957 // locate newval[] in origval[] when removing it
1958 // and when adding to avoid duplicates
1959 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (removing || (flags & P_NODUP))
1961 {
1962 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001963 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001965 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001966 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 prepending = FALSE;
1969 adding = FALSE;
1970 STRCPY(newval, origval);
1971 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001972
1973 // if no duplicate, move pointer to end of
1974 // original value
1975 if (s == NULL)
1976 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001979 // concatenate the two strings; add a ',' if
1980 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (adding || prepending)
1982 {
1983 comma = ((flags & P_COMMA) && *origval != NUL
1984 && *newval != NUL);
1985 if (adding)
1986 {
1987 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001988 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001989 if (comma && i > 1
1990 && (flags & P_ONECOMMA) == P_ONECOMMA
1991 && origval[i - 1] == ','
1992 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001993 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 mch_memmove(newval + i + comma, newval,
1995 STRLEN(newval) + 1);
1996 mch_memmove(newval, origval, (size_t)i);
1997 }
1998 else
1999 {
2000 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002001 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 if (comma)
2004 newval[i] = ',';
2005 }
2006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002007 // Remove newval[] from origval[]. (Note: "i" has
2008 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (removing)
2010 {
2011 STRCPY(newval, origval);
2012 if (*s)
2013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002014 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (flags & P_COMMA)
2016 {
2017 if (s == origval)
2018 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002019 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 if (s[i] == ',')
2021 ++i;
2022 }
2023 else
2024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002025 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 --s;
2027 ++i;
2028 }
2029 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002030 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 }
2033
2034 if (flags & P_FLAGLIST)
2035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002037 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002038 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002039 // if options have P_FLAGLIST and
2040 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002041 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002043 if (*s != ',' && *(s + 1) == ','
2044 && vim_strchr(s + 2, *s) != NULL)
2045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002046 // Remove the duplicated value and
2047 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002049 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002052 else
2053 {
2054 if ((!(flags & P_COMMA) || *s != ',')
2055 && vim_strchr(s + 1, *s) != NULL)
2056 {
2057 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002058 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002059 }
2060 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002061 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002065 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 arg = save_arg;
2067 new_value_alloced = TRUE;
2068 }
2069
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002070 /*
2071 * Set the new value.
2072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 *(char_u **)(varp) = newval;
2074
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002075#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002076 if (!starting
2077# ifdef FEAT_CRYPT
2078 && options[opt_idx].indir != PV_KEY
2079# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002080 && origval != NULL && newval != NULL)
2081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002082 // origval may be freed by
2083 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002084 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002085 // newval (and varp) may become invalid if the
2086 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002087 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002088 if (origval_l != NULL)
2089 saved_origval_l = vim_strsave(origval_l);
2090 if (origval_g != NULL)
2091 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002092 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002093#endif
2094
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002095 {
2096 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002097 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002098
2099 // When an option is set in the sandbox, from a
2100 // modeline or in secure mode, then deal with side
2101 // effects in secure mode. Also when the value was
2102 // set with the P_INSECURE flag and is not
2103 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002104 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002105#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002106 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002107#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002108 || (!value_is_replaced && (*p & P_INSECURE)))
2109 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002110
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002111 // Handle side effects, and set the global value
2112 // for ":set" on local options. Note: when setting
2113 // 'syntax' or 'filetype' autocommands may be
2114 // triggered that can cause havoc.
2115 errmsg = did_set_string_option(
2116 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002117 new_value_alloced, oldval, errbuf,
2118 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002119
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002120 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002123#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002124 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002125 trigger_optionsset_string(
2126 opt_idx, opt_flags, saved_origval,
2127 saved_origval_l, saved_origval_g,
2128 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002129 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002130 vim_free(saved_origval_l);
2131 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002132 vim_free(saved_newval);
2133#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002134 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (errmsg != NULL)
2136 goto skip;
2137 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002138 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 char_u *p;
2141
2142 if (nextchar == '&')
2143 {
2144 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002145 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147 else
2148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002149 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002150 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (*p == '\\' && p[1] != NUL)
2152 ++p;
2153 nextchar = *p;
2154 *p = NUL;
2155 add_termcode(key_name, arg, FALSE);
2156 *p = nextchar;
2157 }
2158 if (full_screen)
2159 ttest(FALSE);
2160 redraw_all_later(CLEAR);
2161 }
2162 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002163
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002165 did_set_option(
2166 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
2168
2169skip:
2170 /*
2171 * Advance to next argument.
2172 * - skip until a blank found, taking care of backslashes
2173 * - skip blanks
2174 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2175 */
2176 for (i = 0; i < 2 ; ++i)
2177 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002178 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (*arg++ == '\\' && *arg != NUL)
2180 ++arg;
2181 arg = skipwhite(arg);
2182 if (*arg != '=')
2183 break;
2184 }
2185 }
2186
2187 if (errmsg != NULL)
2188 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002189 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002190 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (i + (arg - startarg) < IOSIZE)
2192 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 STRCAT(IObuff, ": ");
2195 mch_memmove(IObuff + i, startarg, (arg - startarg));
2196 IObuff[i + (arg - startarg)] = NUL;
2197 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002198 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 trans_characters(IObuff, IOSIZE);
2200
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 ++no_wait_return; // wait_return done later
2202 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 --no_wait_return;
2204
2205 return FAIL;
2206 }
2207
2208 arg = skipwhite(arg);
2209 }
2210
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211theend:
2212 if (silent_mode && did_show)
2213 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002216 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002217 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 out_flush();
2220 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002221 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 }
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 return OK;
2225}
2226
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002227/*
2228 * Call this when an option has been given a new value through a user command.
2229 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2230 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002231 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002232did_set_option(
2233 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002234 int opt_flags, // possibly with OPT_MODELINE
2235 int new_value, // value was replaced completely
2236 int value_checked) // value was checked to be safe, no need to set the
2237 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002238{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002239 long_u *p;
2240
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002241 options[opt_idx].flags |= P_WAS_SET;
2242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002243 // When an option is set in the sandbox, from a modeline or in secure mode
2244 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2245 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002246 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002247 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002248#ifdef HAVE_SANDBOX
2249 || sandbox != 0
2250#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002251 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002252 *p = *p | P_INSECURE;
2253 else if (new_value)
2254 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002255}
2256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257/*
2258 * Convert a key name or string into a key value.
2259 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002260 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002262 int
2263string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002266 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (*arg == '^')
2268 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002269 if (multi_byte)
2270 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 return *arg;
2272}
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#ifdef FEAT_TITLE
2275/*
2276 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2277 * maketitle() to create and display it.
2278 * When switching the title or icon off, call mch_restore_title() to get
2279 * the old value back.
2280 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002281 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002282did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 if (starting != NO_SCREEN
2285#ifdef FEAT_GUI
2286 && !gui.starting
2287#endif
2288 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290}
2291#endif
2292
2293/*
2294 * set_options_bin - called when 'bin' changes value.
2295 */
2296 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002297set_options_bin(
2298 int oldval,
2299 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002300 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 /*
2303 * The option values that are changed when 'bin' changes are
2304 * copied when 'bin is set and restored when 'bin' is reset.
2305 */
2306 if (newval)
2307 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002308 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 if (!(opt_flags & OPT_GLOBAL))
2311 {
2312 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2313 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2314 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2315 curbuf->b_p_et_nobin = curbuf->b_p_et;
2316 }
2317 if (!(opt_flags & OPT_LOCAL))
2318 {
2319 p_tw_nobin = p_tw;
2320 p_wm_nobin = p_wm;
2321 p_ml_nobin = p_ml;
2322 p_et_nobin = p_et;
2323 }
2324 }
2325
2326 if (!(opt_flags & OPT_GLOBAL))
2327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002328 curbuf->b_p_tw = 0; // no automatic line wrap
2329 curbuf->b_p_wm = 0; // no automatic line wrap
2330 curbuf->b_p_ml = 0; // no modelines
2331 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333 if (!(opt_flags & OPT_LOCAL))
2334 {
2335 p_tw = 0;
2336 p_wm = 0;
2337 p_ml = FALSE;
2338 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002339 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 if (!(opt_flags & OPT_GLOBAL))
2345 {
2346 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2347 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2348 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2349 curbuf->b_p_et = curbuf->b_p_et_nobin;
2350 }
2351 if (!(opt_flags & OPT_LOCAL))
2352 {
2353 p_tw = p_tw_nobin;
2354 p_wm = p_wm_nobin;
2355 p_ml = p_ml_nobin;
2356 p_et = p_et_nobin;
2357 }
2358 }
2359}
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361/*
2362 * Expand environment variables for some string options.
2363 * These string options cannot be indirect!
2364 * If "val" is NULL expand the current value of the option.
2365 * Return pointer to NameBuff, or NULL when not expanded.
2366 */
2367 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002368option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002370 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2372 return NULL;
2373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002374 // If val is longer than MAXPATHL no meaningful expansion can be done,
2375 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (val != NULL && STRLEN(val) > MAXPATHL)
2377 return NULL;
2378
2379 if (val == NULL)
2380 val = *(char_u **)options[opt_idx].var;
2381
2382 /*
2383 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2384 * Escape spaces when expanding 'tags', they are used to separate file
2385 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002386 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 */
2388 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002389 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002390#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002391 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2392#endif
2393 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002394 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 return NULL;
2396
2397 return NameBuff;
2398}
2399
2400/*
2401 * After setting various option values: recompute variables that depend on
2402 * option values.
2403 */
2404 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002405didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 (void)init_chartab();
2409
Bram Moolenaardac13472019-09-16 21:06:21 +02002410 didset_string_options();
2411
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002412#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002413 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002414 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002415 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002416 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002419 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 (void)check_cedit();
2421#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002422#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002423 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002424 fill_breakat_flags();
2425#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002426 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002427}
2428
2429/*
2430 * More side effects of setting options.
2431 */
2432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002433didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002434{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002435 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002436 (void)highlight_changed();
2437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002439 check_opt_wim();
2440
Bram Moolenaareed9d462021-02-15 20:38:25 +01002441 // Parse default for 'listchars'.
2442 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002444 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002445 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002446
2447#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002448 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002449 (void)check_clipboard_option();
2450#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002451#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002452 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002453 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002454 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002455 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457}
2458
2459/*
2460 * Check for string options that are NULL (normally only termcap options).
2461 */
2462 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 int opt_idx;
2466
2467 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2468 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2469 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2470}
2471
2472/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002473 * Return the option index found by a pointer into term_strings[].
2474 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002476 int
2477get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002479 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2482 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002483 return opt_idx;
2484 return -1; // cannot happen: didn't find it!
2485}
2486
2487/*
2488 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2489 * Return the option index or -1 if not found.
2490 */
2491 int
2492set_term_option_alloced(char_u **p)
2493{
2494 int opt_idx = get_term_opt_idx(p);
2495
2496 if (opt_idx >= 0)
2497 options[opt_idx].flags |= P_ALLOCED;
2498 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499}
2500
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002501#if defined(FEAT_EVAL) || defined(PROTO)
2502/*
2503 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2504 * Return FALSE when it wasn't.
2505 * Return -1 for an unknown option.
2506 */
2507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002508was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002509{
2510 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002511 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002512
2513 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002514 {
2515 flagp = insecure_flag(idx, opt_flags);
2516 return (*flagp & P_INSECURE) != 0;
2517 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002518 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002519 return -1;
2520}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002521
2522/*
2523 * Get a pointer to the flags used for the P_INSECURE flag of option
2524 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002525 * NOTE: Caller must make sure that "curwin" is set to the window from which
2526 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002527 */
2528 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002529insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002530{
2531 if (opt_flags & OPT_LOCAL)
2532 switch ((int)options[opt_idx].indir)
2533 {
2534#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002535 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002536#endif
2537#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002538# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002539 case PV_FDE: return &curwin->w_p_fde_flags;
2540 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002541# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002542# ifdef FEAT_BEVAL
2543 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2544# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002546 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002547# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002548 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002549# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002550 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002551# endif
2552#endif
2553 }
2554
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002555 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002556 return &options[opt_idx].flags;
2557}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002558#endif
2559
Bram Moolenaardac13472019-09-16 21:06:21 +02002560#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002561/*
2562 * Redraw the window title and/or tab page text later.
2563 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002564void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002565{
2566 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002567 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002568}
2569#endif
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002572 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2573 * characters or characters in "allowed".
2574 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002575 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002576valid_name(char_u *val, char *allowed)
2577{
2578 char_u *s;
2579
2580 for (s = val; *s != NUL; ++s)
2581 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2582 return FALSE;
2583 return TRUE;
2584}
2585
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002586#if defined(FEAT_EVAL) || defined(PROTO)
2587/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002588 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002589 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002590 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002591 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002592set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002593{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002594 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2595 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002596 sctx_T new_script_ctx = script_ctx;
2597
Bram Moolenaar51258742020-05-03 17:19:33 +02002598 // Modeline already has the line number set.
2599 if (!(opt_flags & OPT_MODELINE))
2600 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002601
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002602 // Remember where the option was set. For local options need to do that
2603 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002604 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002605 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002606 if (both || (opt_flags & OPT_LOCAL))
2607 {
2608 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002609 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002610 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002611 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002612 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002613}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002614
2615/*
2616 * Set the script_ctx for a termcap option.
2617 * "name" must be the two character code, e.g. "RV".
2618 * When "name" is NULL use "opt_idx".
2619 */
2620 void
2621set_term_option_sctx_idx(char *name, int opt_idx)
2622{
2623 char_u buf[5];
2624 int idx;
2625
2626 if (name == NULL)
2627 idx = opt_idx;
2628 else
2629 {
2630 buf[0] = 't';
2631 buf[1] = '_';
2632 buf[2] = name[0];
2633 buf[3] = name[1];
2634 buf[4] = 0;
2635 idx = findoption(buf);
2636 }
2637 if (idx >= 0)
2638 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2639}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002640#endif
2641
Bram Moolenaarf4140482020-02-15 23:06:45 +01002642#if defined(FEAT_EVAL)
2643/*
2644 * Apply the OptionSet autocommand.
2645 */
2646 static void
2647apply_optionset_autocmd(
2648 int opt_idx,
2649 long opt_flags,
2650 long oldval,
2651 long oldval_g,
2652 long newval,
2653 char *errmsg)
2654{
2655 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2656
2657 // Don't do this while starting up, failure or recursively.
2658 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2659 return;
2660
2661 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2662 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2663 oldval_g);
2664 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2665 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2666 (opt_flags & OPT_LOCAL) ? "local" : "global");
2667 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2668 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2669 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2670 if (opt_flags & OPT_LOCAL)
2671 {
2672 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2673 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2674 }
2675 if (opt_flags & OPT_GLOBAL)
2676 {
2677 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2678 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2679 }
2680 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2681 {
2682 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2683 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2684 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2685 }
2686 if (opt_flags & OPT_MODELINE)
2687 {
2688 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2689 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2690 }
2691 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2692 NULL, FALSE, NULL);
2693 reset_v_option_vars();
2694}
2695#endif
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * Set the value of a boolean option, and take care of side effects.
2699 * Returns NULL for success, or an error message for an error.
2700 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002701 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002702set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 int opt_idx, // index in options[] table
2704 char_u *varp, // pointer to the option variable
2705 int value, // new value
2706 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002709#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002710 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if ((secure
2715#ifdef HAVE_SANDBOX
2716 || sandbox != 0
2717#endif
2718 ) && (options[opt_idx].flags & P_SECURE))
2719 return e_secure;
2720
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002721#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002722 // Save the global value before changing anything. This is needed as for
2723 // a global-only option setting the "local value" in fact sets the global
2724 // value (since there is only one value).
2725 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2726 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2727 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002728#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002732 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002733 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#endif
2735
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002736#ifdef FEAT_GUI
2737 need_mouse_correct = TRUE;
2738#endif
2739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2742 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2743
2744 /*
2745 * Handle side effects of changing a bool option.
2746 */
2747
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002748 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaar920694c2016-08-21 17:45:02 +02002752#ifdef FEAT_LANGMAP
2753 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002755 p_lnr = !p_lrm;
2756 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002757 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002758 p_lrm = !p_lnr;
2759#endif
2760
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002761#ifdef FEAT_SYN_HL
2762 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2763 reset_cursorline();
2764#endif
2765
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002766#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002768 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2769 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // Only take action when the option was set. When reset we do not
2771 // delete the undo file, the option may be set again without making
2772 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002773 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002774 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002775 char_u hash[UNDO_HASH_SIZE];
2776 buf_T *save_curbuf = curbuf;
2777
Bram Moolenaar29323592016-07-24 22:04:11 +02002778 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // When 'undofile' is set globally: for every buffer, otherwise
2781 // only for the current buffer: Try to read in the undofile,
2782 // if one exists, the buffer wasn't changed and the buffer was
2783 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002784 if ((curbuf == save_curbuf
2785 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2786 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2787 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002788#ifdef FEAT_CRYPT
2789 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2790 continue;
2791#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002792 u_compute_hash(hash);
2793 u_read_undo(NULL, hash, curbuf->b_fname);
2794 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002795 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002796 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002797 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002798 }
2799#endif
2800
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else if ((int *)varp == &curbuf->b_p_ro)
2802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002803 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2805 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002807 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002808 if (curbuf->b_p_ro)
2809 curbuf->b_did_warn = FALSE;
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002812 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813#endif
2814 }
2815
Bram Moolenaar9c449722010-07-20 18:44:27 +02002816#ifdef FEAT_GUI
2817 else if ((int *)varp == &p_mh)
2818 {
2819 if (!p_mh)
2820 gui_mch_mousehide(FALSE);
2821 }
2822#endif
2823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002826 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002827# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002829 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2830 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002831 {
2832 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002833 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002834 }
2835# endif
2836# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002837 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002838# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002839 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002840#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002843 {
2844 redraw_titles();
2845 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002847 else if ((int *)varp == &curbuf->b_p_fixeol)
2848 {
2849 redraw_titles();
2850 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002852 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002853 {
2854 redraw_titles();
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856#endif
2857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 else if ((int *)varp == &curbuf->b_p_bin)
2860 {
2861 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2862#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002863 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#endif
2865 }
2866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2869 {
2870 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2871 NULL, NULL, TRUE, curbuf);
2872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else if ((int *)varp == &curbuf->b_p_swf)
2876 {
2877 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002880 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2881 // buf->b_p_swf
2882 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 else if ((int *)varp == &p_terse)
2887 {
2888 char_u *p;
2889
2890 p = vim_strchr(p_shm, SHM_SEARCH);
2891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 if (p_terse && p == NULL)
2894 {
2895 STRCPY(IObuff, p_shm);
2896 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002897 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002901 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
2903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 else if ((int *)varp == &p_paste)
2906 {
2907 paste_option_changed();
2908 }
2909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 else if ((int *)varp == &p_im)
2912 {
2913 if (p_im)
2914 {
2915 if ((State & INSERT) == 0)
2916 need_start_insertmode = TRUE;
2917 stop_insert_mode = FALSE;
2918 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002920 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 need_start_insertmode = FALSE;
2923 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002924 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 restart_edit = 0;
2927 }
2928 }
2929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 else if ((int *)varp == &p_ic && p_hls)
2932 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002933 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935
2936#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 else if ((int *)varp == &p_hls)
2939 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002940 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
2942#endif
2943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002944 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2945 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 else if ((int *)varp == &curwin->w_p_scb)
2947 {
2948 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002951 curwin->w_scbind_pos = curwin->w_topline;
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
Bram Moolenaar4033c552017-09-16 20:54:51 +02002955#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 else if ((int *)varp == &curwin->w_p_pvw)
2958 {
2959 if (curwin->w_p_pvw)
2960 {
2961 win_T *win;
2962
Bram Moolenaar29323592016-07-24 22:04:11 +02002963 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (win->w_p_pvw && win != curwin)
2965 {
2966 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002967 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969 }
2970 }
2971#endif
2972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 else if ((int *)varp == &curbuf->b_p_tx)
2975 {
2976 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2977 }
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 set_string_option_direct((char_u *)"ffs", -1,
2983 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002984 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 /*
2988 * When 'lisp' option changes include/exclude '-' in
2989 * keyword characters.
2990 */
2991#ifdef FEAT_LISP
2992 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996#endif
2997
2998#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003000 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003002 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004#endif
3005
3006 else if ((int *)varp == &curbuf->b_changed)
3007 {
3008 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003009 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003011 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015
3016#ifdef BACKSLASH_IN_FILENAME
3017 else if ((int *)varp == &p_ssl)
3018 {
3019 if (p_ssl)
3020 {
3021 psepc = '/';
3022 psepcN = '\\';
3023 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
3025 else
3026 {
3027 psepc = '\\';
3028 psepcN = '/';
3029 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 }
3031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003032 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 buflist_slash_adjust();
3034 alist_slash_adjust();
3035# ifdef FEAT_EVAL
3036 scriptnames_slash_adjust();
3037# endif
3038 }
3039#endif
3040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003041 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 else if ((int *)varp == &curwin->w_p_wrap)
3043 {
3044 if (curwin->w_p_wrap)
3045 curwin->w_leftcol = 0;
3046 }
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 else if ((int *)varp == &p_ea)
3049 {
3050 if (p_ea && !old_value)
3051 win_equal(curwin, FALSE, 0);
3052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 else if ((int *)varp == &p_wiv)
3055 {
3056 /*
3057 * When 'weirdinvert' changed, set/reset 't_xs'.
3058 * Then set 'weirdinvert' according to value of 't_xs'.
3059 */
3060 if (p_wiv && !old_value)
3061 T_XS = (char_u *)"y";
3062 else if (!p_wiv && old_value)
3063 T_XS = empty_option;
3064 p_wiv = (*T_XS != NUL);
3065 }
3066
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003067#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 else if ((int *)varp == &p_beval)
3069 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003070 if (!balloonEvalForTerm)
3071 {
3072 if (p_beval && !old_value)
3073 gui_mch_enable_beval_area(balloonEval);
3074 else if (!p_beval && old_value)
3075 gui_mch_disable_beval_area(balloonEval);
3076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003078#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003079#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003080 else if ((int *)varp == &p_bevalterm)
3081 {
3082 mch_bevalterm_changed();
3083 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003086#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 else if ((int *)varp == &p_acd)
3088 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003090 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092#endif
3093
3094#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 else if ((int *)varp == &curwin->w_p_diff)
3097 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003099 diff_buf_adjust(curwin);
3100# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (foldmethodIsDiff(curwin))
3102 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105#endif
3106
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003107#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 else if ((int *)varp == &p_imdisable)
3110 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003111 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 if (p_imdisable)
3113 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003114 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // When the option is set from an autocommand, it may need to take
3116 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003117 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119#endif
3120
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003121#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003122 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003123 else if ((int *)varp == &curwin->w_p_spell)
3124 {
3125 if (curwin->w_p_spell)
3126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003127 char *errmsg = did_set_spelllang(curwin);
3128
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003129 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003130 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003131 }
3132 }
3133#endif
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#ifdef FEAT_ARABIC
3136 if ((int *)varp == &curwin->w_p_arab)
3137 {
3138 if (curwin->w_p_arab)
3139 {
3140 /*
3141 * 'arabic' is set, handle various sub-settings.
3142 */
3143 if (!p_tbidi)
3144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (!curwin->w_p_rl)
3147 {
3148 curwin->w_p_rl = TRUE;
3149 changed_window_setting();
3150 }
3151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (!p_arshape)
3154 {
3155 p_arshape = TRUE;
3156 redraw_later_clear();
3157 }
3158 }
3159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // Arabic requires a utf-8 encoding, inform the user if its not
3161 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003163 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003164 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3165
Bram Moolenaar8820b482017-03-16 17:23:31 +01003166 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003167 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003168#ifdef FEAT_EVAL
3169 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3170#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3179 OPT_LOCAL);
3180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 }
3182 else
3183 {
3184 /*
3185 * 'arabic' is reset, handle various sub-settings.
3186 */
3187 if (!p_tbidi)
3188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003189 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (curwin->w_p_rl)
3191 {
3192 curwin->w_p_rl = FALSE;
3193 changed_window_setting();
3194 }
3195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003196 // 'arabicshape' isn't reset, it is a global option and
3197 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // 'delcombine' isn't reset, it is a global option and another
3201 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003204 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 curbuf->b_p_iminsert = B_IMODE_NONE;
3206 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3207# endif
3208 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003209 }
3210
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003211#endif
3212
Bram Moolenaar44826212019-08-22 21:23:20 +02003213#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3214 else if (((int *)varp == &curwin->w_p_nu
3215 || (int *)varp == &curwin->w_p_rnu)
3216 && gui.in_use
3217 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3218 && curbuf->b_signlist != NULL)
3219 {
3220 // If the 'number' or 'relativenumber' options are modified and
3221 // 'signcolumn' is set to 'number', then clear the screen for a full
3222 // refresh. Otherwise the sign icons are not displayed properly in the
3223 // number column. If the 'number' option is set and only the
3224 // 'relativenumber' option is toggled, then don't refresh the screen
3225 // (optimization).
3226 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3227 redraw_all_later(CLEAR);
3228 }
3229#endif
3230
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003231#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003233 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003234 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003235# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003236 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003237 if (
3238# ifdef VIMDLL
3239 !gui.in_use && !gui.starting &&
3240# endif
3241 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003242 {
3243 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003244 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003245 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003246 if (is_term_win32())
3247 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003248# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003249# ifdef FEAT_GUI
3250 if (!gui.in_use && !gui.starting)
3251# endif
3252 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003253# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003254 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003255 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003256 {
3257 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003258 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003259 init_highlight(TRUE, FALSE);
3260 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003261# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003262 }
3263#endif
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 /*
3266 * End of handling side effects for bool options.
3267 */
3268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003269 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 options[opt_idx].flags |= P_WAS_SET;
3272
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003273#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003274 apply_optionset_autocmd(opt_idx, opt_flags,
3275 (long)(old_value ? TRUE : FALSE),
3276 (long)(old_global_value ? TRUE : FALSE),
3277 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003278#endif
3279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003280 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003281 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003282 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003283 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003284
3285 if ((opt_flags & OPT_NO_REDRAW) == 0)
3286 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 return NULL;
3289}
3290
3291/*
3292 * Set the value of a number option, and take care of side effects.
3293 * Returns NULL for success, or an error message for an error.
3294 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003295 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003296set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003297 int opt_idx, // index in options[] table
3298 char_u *varp, // pointer to the option variable
3299 long value, // new value
3300 char *errbuf, // buffer for error messages
3301 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003302 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3303 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003305 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003307#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003308 long old_global_value = 0; // only used when setting a local and
3309 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003310#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003311 long old_Rows = Rows; // remember old Rows
3312 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 long *pp = (long *)varp;
3314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003315 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003316 if ((secure
3317#ifdef HAVE_SANDBOX
3318 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003320 ) && (options[opt_idx].flags & P_SECURE))
3321 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003323#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003324 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003325 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003326 // value (since there is only one value).
3327 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003328 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3329 OPT_GLOBAL);
3330#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 *pp = value;
3333#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003334 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003335 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003337#ifdef FEAT_GUI
3338 need_mouse_correct = TRUE;
3339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar14f24742012-08-08 18:01:05 +02003341 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003344#ifdef FEAT_VARTABS
3345 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3346 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3347 ? tabstop_first(curbuf->b_p_vts_array)
3348 : curbuf->b_p_ts;
3349#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353
3354 /*
3355 * Number options that need some action when changed
3356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (pp == &p_wh || pp == &p_hh)
3358 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003359 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (p_wh < 1)
3361 {
3362 errmsg = e_positive;
3363 p_wh = 1;
3364 }
3365 if (p_wmh > p_wh)
3366 {
3367 errmsg = e_winheight;
3368 p_wh = p_wmh;
3369 }
3370 if (p_hh < 0)
3371 {
3372 errmsg = e_positive;
3373 p_hh = 0;
3374 }
3375
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003376 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003377 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
3379 if (pp == &p_wh && curwin->w_height < p_wh)
3380 win_setheight((int)p_wh);
3381 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3382 win_setheight((int)p_hh);
3383 }
3384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 else if (pp == &p_wmh)
3386 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003387 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (p_wmh < 0)
3389 {
3390 errmsg = e_positive;
3391 p_wmh = 0;
3392 }
3393 if (p_wmh > p_wh)
3394 {
3395 errmsg = e_winheight;
3396 p_wmh = p_wh;
3397 }
3398 win_setminheight();
3399 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003400 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003402 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (p_wiw < 1)
3404 {
3405 errmsg = e_positive;
3406 p_wiw = 1;
3407 }
3408 if (p_wmw > p_wiw)
3409 {
3410 errmsg = e_winwidth;
3411 p_wiw = p_wmw;
3412 }
3413
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003414 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003415 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 win_setwidth((int)p_wiw);
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else if (pp == &p_wmw)
3419 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003420 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (p_wmw < 0)
3422 {
3423 errmsg = e_positive;
3424 p_wmw = 0;
3425 }
3426 if (p_wmw > p_wiw)
3427 {
3428 errmsg = e_winwidth;
3429 p_wmw = p_wiw;
3430 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003431 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003434 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 else if (pp == &p_ls)
3436 {
3437 last_status(FALSE);
3438 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003440 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003441 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003442 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446#ifdef FEAT_GUI
3447 else if (pp == &p_linespace)
3448 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003449 // Recompute gui.char_height and resize the Vim window to keep the
3450 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003451 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003452 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454#endif
3455
3456#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else if (pp == &curwin->w_p_fdl)
3459 {
3460 if (curwin->w_p_fdl < 0)
3461 curwin->w_p_fdl = 0;
3462 newFoldLevel();
3463 }
3464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 else if (pp == &curwin->w_p_fml)
3467 {
3468 foldUpdateAll(curwin);
3469 }
3470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003471 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 else if (pp == &curwin->w_p_fdn)
3473 {
3474 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3475 foldUpdateAll(curwin);
3476 }
3477
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003478 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 else if (pp == &curwin->w_p_fdc)
3480 {
3481 if (curwin->w_p_fdc < 0)
3482 {
3483 errmsg = e_positive;
3484 curwin->w_p_fdc = 0;
3485 }
3486 else if (curwin->w_p_fdc > 12)
3487 {
3488 errmsg = e_invarg;
3489 curwin->w_p_fdc = 12;
3490 }
3491 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003492#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003494#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003495 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3497 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003498# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (foldmethodIsIndent(curwin))
3500 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003501# endif
3502# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003503 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3504 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003505 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3506 parse_cino(curbuf);
3507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003511 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003512 else if (pp == &p_mco)
3513 {
3514 if (p_mco > MAX_MCO)
3515 p_mco = MAX_MCO;
3516 else if (p_mco < 0)
3517 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003518 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003519 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 else if (pp == &curbuf->b_p_iminsert)
3522 {
3523 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3524 {
3525 errmsg = e_invarg;
3526 curbuf->b_p_iminsert = B_IMODE_NONE;
3527 }
3528 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003529 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003531#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003532 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 status_redraw_curbuf();
3534#endif
3535 }
3536
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003537#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003538 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003539 else if (pp == &p_imst)
3540 {
3541 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3542 errmsg = e_invarg;
3543 }
3544#endif
3545
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003546 else if (pp == &p_window)
3547 {
3548 if (p_window < 1)
3549 p_window = 1;
3550 else if (p_window >= Rows)
3551 p_window = Rows - 1;
3552 }
3553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 else if (pp == &curbuf->b_p_imsearch)
3555 {
3556 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3557 {
3558 errmsg = e_invarg;
3559 curbuf->b_p_imsearch = B_IMODE_NONE;
3560 }
3561 p_imsearch = curbuf->b_p_imsearch;
3562 }
3563
3564#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 else if (pp == &p_titlelen)
3567 {
3568 if (p_titlelen < 0)
3569 {
3570 errmsg = e_positive;
3571 p_titlelen = 85;
3572 }
3573 if (starting != NO_SCREEN && old_value != p_titlelen)
3574 need_maketitle = TRUE;
3575 }
3576#endif
3577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003578 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 else if (pp == &p_ch)
3580 {
3581 if (p_ch < 1)
3582 {
3583 errmsg = e_positive;
3584 p_ch = 1;
3585 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003586 if (p_ch > Rows - min_rows() + 1)
3587 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003589 // Only compute the new window layout when startup has been
3590 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (p_ch != old_value && full_screen
3592#ifdef FEAT_GUI
3593 && !gui.starting
3594#endif
3595 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003596 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003599 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 else if (pp == &p_uc)
3601 {
3602 if (p_uc < 0)
3603 {
3604 errmsg = e_positive;
3605 p_uc = 100;
3606 }
3607 if (p_uc && !old_value)
3608 ml_open_files();
3609 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003610#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003611 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003612 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003613 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003614 {
3615 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003616 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003618 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619 {
3620 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003621 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003622 }
3623 }
3624#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003625#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003626 else if (pp == &p_mzq)
3627 mzvim_reset_timer();
3628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003630#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003631 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003632 else if (pp == &p_pyx)
3633 {
3634 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3635 errmsg = e_invarg;
3636 }
3637#endif
3638
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003639 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 else if (pp == &p_ul)
3641 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003642 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003644 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 p_ul = value;
3646 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003647 else if (pp == &curbuf->b_p_ul)
3648 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003649 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003650 curbuf->b_p_ul = old_value;
3651 u_sync(TRUE);
3652 curbuf->b_p_ul = value;
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003655#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003657 else if (pp == &curwin->w_p_nuw)
3658 {
3659 if (curwin->w_p_nuw < 1)
3660 {
3661 errmsg = e_positive;
3662 curwin->w_p_nuw = 1;
3663 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003664 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003665 {
3666 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003667 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003668 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003669 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003670 }
3671#endif
3672
Bram Moolenaar1a384422010-07-14 19:53:30 +02003673 else if (pp == &curbuf->b_p_tw)
3674 {
3675 if (curbuf->b_p_tw < 0)
3676 {
3677 errmsg = e_positive;
3678 curbuf->b_p_tw = 0;
3679 }
3680#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003681 {
3682 win_T *wp;
3683 tabpage_T *tp;
3684
3685 FOR_ALL_TAB_WINDOWS(tp, wp)
3686 check_colorcolumn(wp);
3687 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003688#endif
3689 }
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 /*
3692 * Check the bounds for numeric options here
3693 */
3694 if (Rows < min_rows() && full_screen)
3695 {
3696 if (errbuf != NULL)
3697 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003698 vim_snprintf((char *)errbuf, errbuflen,
3699 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 errmsg = errbuf;
3701 }
3702 Rows = min_rows();
3703 }
3704 if (Columns < MIN_COLUMNS && full_screen)
3705 {
3706 if (errbuf != NULL)
3707 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003708 vim_snprintf((char *)errbuf, errbuflen,
3709 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 errmsg = errbuf;
3711 }
3712 Columns = MIN_COLUMNS;
3713 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003714 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 /*
3717 * If the screen (shell) height has been changed, assume it is the
3718 * physical screenheight.
3719 */
3720 if (old_Rows != Rows || old_Columns != Columns)
3721 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003722 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (updating_screen)
3724 *pp = old_value;
3725 else if (full_screen
3726#ifdef FEAT_GUI
3727 && !gui.starting
3728#endif
3729 )
3730 set_shellsize((int)Columns, (int)Rows, TRUE);
3731 else
3732 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003733 // Postpone the resizing; check the size and cmdline position for
3734 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 check_shellsize();
3736 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3737 cmdline_row = Rows - p_ch;
3738 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003739 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003740 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (curbuf->b_p_ts <= 0)
3744 {
3745 errmsg = e_positive;
3746 curbuf->b_p_ts = 8;
3747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (p_tm < 0)
3749 {
3750 errmsg = e_positive;
3751 p_tm = 0;
3752 }
3753 if ((curwin->w_p_scr <= 0
3754 || (curwin->w_p_scr > curwin->w_height
3755 && curwin->w_height > 0))
3756 && full_screen)
3757 {
3758 if (pp == &(curwin->w_p_scr))
3759 {
3760 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003761 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 win_comp_scroll(curwin);
3763 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003764 // If 'scroll' became invalid because of a side effect silently adjust
3765 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 else if (curwin->w_p_scr <= 0)
3767 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003768 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 curwin->w_p_scr = curwin->w_height;
3770 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003771 if (p_hi < 0)
3772 {
3773 errmsg = e_positive;
3774 p_hi = 0;
3775 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003776 else if (p_hi > 10000)
3777 {
3778 errmsg = e_invarg;
3779 p_hi = 10000;
3780 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003781 if (p_re < 0 || p_re > 2)
3782 {
3783 errmsg = e_invarg;
3784 p_re = 0;
3785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (p_report < 0)
3787 {
3788 errmsg = e_positive;
3789 p_report = 1;
3790 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003791 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003793 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 p_sj = Rows / 2;
3795 else
3796 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003797 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 p_sj = 1;
3799 }
3800 }
3801 if (p_so < 0 && full_screen)
3802 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003803 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 p_so = 0;
3805 }
3806 if (p_siso < 0 && full_screen)
3807 {
3808 errmsg = e_positive;
3809 p_siso = 0;
3810 }
3811#ifdef FEAT_CMDWIN
3812 if (p_cwh < 1)
3813 {
3814 errmsg = e_positive;
3815 p_cwh = 1;
3816 }
3817#endif
3818 if (p_ut < 0)
3819 {
3820 errmsg = e_positive;
3821 p_ut = 2000;
3822 }
3823 if (p_ss < 0)
3824 {
3825 errmsg = e_positive;
3826 p_ss = 0;
3827 }
3828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003829 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3831 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3832
3833 options[opt_idx].flags |= P_WAS_SET;
3834
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003835#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003836 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3837 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003838#endif
3839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003840 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003841 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003842 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003843 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003844 if ((opt_flags & OPT_NO_REDRAW) == 0)
3845 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 return errmsg;
3848}
3849
3850/*
3851 * Called after an option changed: check if something needs to be redrawn.
3852 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003854check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003856 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003857 int doclear = (flags & P_RCLR) == P_RCLR;
3858 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003860 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3864 changed_window_setting();
3865 if (flags & P_RBUF)
3866 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003867 if (flags & P_RWINONLY)
3868 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003869 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 redraw_all_later(CLEAR);
3871 else if (all)
3872 redraw_all_later(NOT_VALID);
3873}
3874
3875/*
3876 * Find index for option 'arg'.
3877 * Return -1 if not found.
3878 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003879 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003880findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
3882 int opt_idx;
3883 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003884 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 int is_term_opt;
3886
3887 /*
3888 * For first call: Initialize the quick-access table.
3889 * It contains the index for the first option that starts with a certain
3890 * letter. There are 26 letters, plus the first "t_" option.
3891 */
3892 if (quick_tab[1] == 0)
3893 {
3894 p = options[0].fullname;
3895 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3896 {
3897 if (s[0] != p[0])
3898 {
3899 if (s[0] == 't' && s[1] == '_')
3900 quick_tab[26] = opt_idx;
3901 else
3902 quick_tab[CharOrdLow(s[0])] = opt_idx;
3903 }
3904 p = s;
3905 }
3906 }
3907
3908 /*
3909 * Check for name starting with an illegal character.
3910 */
3911#ifdef EBCDIC
3912 if (!islower(arg[0]))
3913#else
3914 if (arg[0] < 'a' || arg[0] > 'z')
3915#endif
3916 return -1;
3917
3918 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3919 if (is_term_opt)
3920 opt_idx = quick_tab[26];
3921 else
3922 opt_idx = quick_tab[CharOrdLow(arg[0])];
3923 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003925 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 break;
3927 }
3928 if (s == NULL && !is_term_opt)
3929 {
3930 opt_idx = quick_tab[CharOrdLow(arg[0])];
3931 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3932 {
3933 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003934 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936 s = NULL;
3937 }
3938 }
3939 if (s == NULL)
3940 opt_idx = -1;
3941 return opt_idx;
3942}
3943
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003944#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945/*
3946 * Get the value for an option.
3947 *
3948 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003949 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003950 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003951 * String option: gov_string, *stringval gets allocated string.
3952 * Hidden Number option: gov_hidden_number.
3953 * Hidden Toggle option: gov_hidden_bool.
3954 * Hidden String option: gov_hidden_string.
3955 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003957 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003958get_option_value(
3959 char_u *name,
3960 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003961 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003962 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963{
3964 int opt_idx;
3965 char_u *varp;
3966
3967 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003968 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003969 {
3970 int key;
3971
3972 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003973 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003974 {
3975 char_u key_name[2];
3976 char_u *p;
3977
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003978 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003979 if (key < 0)
3980 {
3981 key_name[0] = KEY2TERMCAP0(key);
3982 key_name[1] = KEY2TERMCAP1(key);
3983 }
3984 else
3985 {
3986 key_name[0] = KS_KEY;
3987 key_name[1] = (key & 0xff);
3988 }
3989 p = find_termcode(key_name);
3990 if (p != NULL)
3991 {
3992 if (stringval != NULL)
3993 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003994 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003995 }
3996 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003997 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4001
4002 if (options[opt_idx].flags & P_STRING)
4003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004004 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004005 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (stringval != NULL)
4007 {
4008#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004010 if ((char_u **)varp == &curbuf->b_p_key
4011 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 *stringval = vim_strsave((char_u *)"*****");
4013 else
4014#endif
4015 *stringval = vim_strsave(*(char_u **)(varp));
4016 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004017 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004020 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004021 return (options[opt_idx].flags & P_NUM)
4022 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 if (options[opt_idx].flags & P_NUM)
4024 *numval = *(long *)varp;
4025 else
4026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 // Special case: 'modified' is b_changed, but we also want to consider
4028 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if ((int *)varp == &curbuf->b_changed)
4030 *numval = curbufIsChanged();
4031 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004032 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004034 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035}
4036#endif
4037
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004038#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004039/*
4040 * Returns the option attributes and its value. Unlike the above function it
4041 * will return either global value or local value of the option depending on
4042 * what was requested, but it will never return global value if it was
4043 * requested to return local one and vice versa. Neither it will return
4044 * buffer-local value if it was requested to return window-local one.
4045 *
4046 * Pretends that option is absent if it is not present in the requested scope
4047 * (i.e. has no global, window-local or buffer-local value depending on
4048 * opt_type). Uses
4049 *
4050 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004051 * 0 hidden or unknown option, also option that does not have requested
4052 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004053 * see SOPT_* in vim.h for other flags
4054 *
4055 * Possible opt_type values: see SREQ_* in vim.h
4056 */
4057 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004058get_option_value_strict(
4059 char_u *name,
4060 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004061 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004062 int opt_type,
4063 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004064{
4065 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004066 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004067 struct vimoption *p;
4068 int r = 0;
4069
4070 opt_idx = findoption(name);
4071 if (opt_idx < 0)
4072 return 0;
4073
4074 p = &(options[opt_idx]);
4075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004076 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004077 if (p->var == NULL)
4078 return 0;
4079
4080 if (p->flags & P_BOOL)
4081 r |= SOPT_BOOL;
4082 else if (p->flags & P_NUM)
4083 r |= SOPT_NUM;
4084 else if (p->flags & P_STRING)
4085 r |= SOPT_STRING;
4086
4087 if (p->indir == PV_NONE)
4088 {
4089 if (opt_type == SREQ_GLOBAL)
4090 r |= SOPT_GLOBAL;
4091 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004092 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004093 }
4094 else
4095 {
4096 if (p->indir & PV_BOTH)
4097 r |= SOPT_GLOBAL;
4098 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004100
4101 if (p->indir & PV_WIN)
4102 {
4103 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004104 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004105 else
4106 r |= SOPT_WIN;
4107 }
4108 else if (p->indir & PV_BUF)
4109 {
4110 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004111 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004112 else
4113 r |= SOPT_BUF;
4114 }
4115 }
4116
4117 if (stringval == NULL)
4118 return r;
4119
4120 if (opt_type == SREQ_GLOBAL)
4121 varp = p->var;
4122 else
4123 {
4124 if (opt_type == SREQ_BUF)
4125 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 // Special case: 'modified' is b_changed, but we also want to
4127 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004128 if (p->indir == PV_MOD)
4129 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004130 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004131 varp = NULL;
4132 }
4133#ifdef FEAT_CRYPT
4134 else if (p->indir == PV_KEY)
4135 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004137 *stringval = NULL;
4138 varp = NULL;
4139 }
4140#endif
4141 else
4142 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004143 buf_T *save_curbuf = curbuf;
4144
4145 // only getting a pointer, no need to use aucmd_prepbuf()
4146 curbuf = (buf_T *)from;
4147 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004148 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004149 curbuf = save_curbuf;
4150 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004151 }
4152 }
4153 else if (opt_type == SREQ_WIN)
4154 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004155 win_T *save_curwin = curwin;
4156
4157 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004158 curbuf = curwin->w_buffer;
4159 varp = get_varp(p);
4160 curwin = save_curwin;
4161 curbuf = curwin->w_buffer;
4162 }
4163 if (varp == p->var)
4164 return (r | SOPT_UNSET);
4165 }
4166
4167 if (varp != NULL)
4168 {
4169 if (p->flags & P_STRING)
4170 *stringval = vim_strsave(*(char_u **)(varp));
4171 else if (p->flags & P_NUM)
4172 *numval = *(long *) varp;
4173 else
4174 *numval = *(int *)varp;
4175 }
4176
4177 return r;
4178}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004179
4180/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004181 * Iterate over options. First argument is a pointer to a pointer to a
4182 * structure inside options[] array, second is option type like in the above
4183 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004184 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004185 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004186 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004187 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004188 * first argument to NULL.
4189 *
4190 * Returns full option name for current option on each call.
4191 */
4192 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004193option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004194{
4195 struct vimoption *ret = NULL;
4196 do
4197 {
4198 if (*option == NULL)
4199 *option = (void *) options;
4200 else if (((struct vimoption *) (*option))->fullname == NULL)
4201 {
4202 *option = NULL;
4203 return NULL;
4204 }
4205 else
4206 *option = (void *) (((struct vimoption *) (*option)) + 1);
4207
4208 ret = ((struct vimoption *) (*option));
4209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004210 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004211 if (ret->var == NULL)
4212 {
4213 ret = NULL;
4214 continue;
4215 }
4216
4217 switch (opt_type)
4218 {
4219 case SREQ_GLOBAL:
4220 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4221 ret = NULL;
4222 break;
4223 case SREQ_BUF:
4224 if (!(ret->indir & PV_BUF))
4225 ret = NULL;
4226 break;
4227 case SREQ_WIN:
4228 if (!(ret->indir & PV_WIN))
4229 ret = NULL;
4230 break;
4231 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004232 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004233 return NULL;
4234 }
4235 }
4236 while (ret == NULL);
4237
4238 return (char_u *)ret->fullname;
4239}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004240#endif
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004243 * Return the flags for the option at 'opt_idx'.
4244 */
4245 long_u
4246get_option_flags(int opt_idx)
4247{
4248 return options[opt_idx].flags;
4249}
4250
4251/*
4252 * Set a flag for the option at 'opt_idx'.
4253 */
4254 void
4255set_option_flag(int opt_idx, long_u flag)
4256{
4257 options[opt_idx].flags |= flag;
4258}
4259
4260/*
4261 * Clear a flag for the option at 'opt_idx'.
4262 */
4263 void
4264clear_option_flag(int opt_idx, long_u flag)
4265{
4266 options[opt_idx].flags &= ~flag;
4267}
4268
4269/*
4270 * Returns TRUE if the option at 'opt_idx' is a global option
4271 */
4272 int
4273is_global_option(int opt_idx)
4274{
4275 return options[opt_idx].indir == PV_NONE;
4276}
4277
4278/*
4279 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4280 * local value.
4281 */
4282 int
4283is_global_local_option(int opt_idx)
4284{
4285 return options[opt_idx].indir & PV_BOTH;
4286}
4287
4288/*
4289 * Returns TRUE if the option at 'opt_idx' is a window-local option
4290 */
4291 int
4292is_window_local_option(int opt_idx)
4293{
4294 return options[opt_idx].var == VAR_WIN;
4295}
4296
4297/*
4298 * Returns TRUE if the option at 'opt_idx' is a hidden option
4299 */
4300 int
4301is_hidden_option(int opt_idx)
4302{
4303 return options[opt_idx].var == NULL;
4304}
4305
4306#if defined(FEAT_CRYPT) || defined(PROTO)
4307/*
4308 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4309 */
4310 int
4311is_crypt_key_option(int opt_idx)
4312{
4313 return options[opt_idx].indir == PV_KEY;
4314}
4315#endif
4316
4317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 * Set the value of option "name".
4319 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004320 *
4321 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004323 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004324set_option_value(
4325 char_u *name,
4326 long number,
4327 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004328 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
4330 int opt_idx;
4331 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004332 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004335 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004336 {
4337 int key;
4338
4339 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004340 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004341 {
4342 char_u key_name[2];
4343
4344 if (key < 0)
4345 {
4346 key_name[0] = KEY2TERMCAP0(key);
4347 key_name[1] = KEY2TERMCAP1(key);
4348 }
4349 else
4350 {
4351 key_name[0] = KS_KEY;
4352 key_name[1] = (key & 0xff);
4353 }
4354 add_termcode(key_name, string, FALSE);
4355 if (full_screen)
4356 ttest(FALSE);
4357 redraw_all_later(CLEAR);
4358 return NULL;
4359 }
4360
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004361 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 else
4364 {
4365 flags = options[opt_idx].flags;
4366#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004367 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004369 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004370 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004371 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004374 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004375 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 else
4377 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004378 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004379 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004381 if (number == 0 && string != NULL)
4382 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004383 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004385 // Either we are given a string or we are setting option
4386 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004387 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004388 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004389 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004390 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004391 // There's another character after zeros or the string
4392 // is empty. In both cases, we are trying to set a
4393 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004394 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004395 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004396 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004397
4398 }
4399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004401 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004402 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004404 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004405 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407 }
4408 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004409 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410}
4411
4412/*
4413 * Get the terminal code for a terminal option.
4414 * Returns NULL when not found.
4415 */
4416 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004417get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418{
4419 int opt_idx;
4420 char_u *varp;
4421
4422 if (tname[0] != 't' || tname[1] != '_' ||
4423 tname[2] == NUL || tname[3] == NUL)
4424 return NULL;
4425 if ((opt_idx = findoption(tname)) >= 0)
4426 {
4427 varp = get_varp(&(options[opt_idx]));
4428 if (varp != NULL)
4429 varp = *(char_u **)(varp);
4430 return varp;
4431 }
4432 return find_termcode(tname + 2);
4433}
4434
4435 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004436get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438 int i;
4439
4440 i = findoption((char_u *)"hl");
4441 if (i >= 0)
4442 return options[i].def_val[VI_DEFAULT];
4443 return (char_u *)NULL;
4444}
4445
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004446 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004447get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004448{
4449 int i;
4450
4451 i = findoption((char_u *)"enc");
4452 if (i >= 0)
4453 return options[i].def_val[VI_DEFAULT];
4454 return (char_u *)NULL;
4455}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457/*
4458 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004459 * When "has_lt" is true there is a '<' before "*arg_arg".
4460 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
4462 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004463find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004465 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004467 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 /*
4470 * Don't use get_special_key_code() for t_xx, we don't want it to call
4471 * add_termcap_entry().
4472 */
4473 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4474 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004475 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004477 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004479 key = find_special_key(&arg, &modifiers,
4480 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004481 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 key = 0;
4483 }
4484 return key;
4485}
4486
4487/*
4488 * if 'all' == 0: show changed options
4489 * if 'all' == 1: show all normal options
4490 * if 'all' == 2: show all terminal options
4491 */
4492 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004493showoptions(
4494 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004495 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496{
4497 struct vimoption *p;
4498 int col;
4499 int isterm;
4500 char_u *varp;
4501 struct vimoption **items;
4502 int item_count;
4503 int run;
4504 int row, rows;
4505 int cols;
4506 int i;
4507 int len;
4508
4509#define INC 20
4510#define GAP 3
4511
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004512 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (items == NULL)
4514 return;
4515
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004516 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004518 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004520 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004522 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004524 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004527 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 * 1. display the short items
4529 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004530 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 */
4532 for (run = 1; run <= 2 && !got_int; ++run)
4533 {
4534 /*
4535 * collect the items in items[]
4536 */
4537 item_count = 0;
4538 for (p = &options[0]; p->fullname != NULL; p++)
4539 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004540 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004541 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004542 continue;
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 varp = NULL;
4545 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004546 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 {
4548 if (p->indir != PV_NONE && !isterm)
4549 varp = get_varp_scope(p, opt_flags);
4550 }
4551 else
4552 varp = get_varp(p);
4553 if (varp != NULL
4554 && ((all == 2 && isterm)
4555 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004556 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004558 if (opt_flags & OPT_ONECOLUMN)
4559 len = Columns;
4560 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004561 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else
4563 {
4564 option_value2string(p, opt_flags);
4565 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4566 }
4567 if ((len <= INC - GAP && run == 1) ||
4568 (len > INC - GAP && run == 2))
4569 items[item_count++] = p;
4570 }
4571 }
4572
4573 /*
4574 * display the items
4575 */
4576 if (run == 1)
4577 {
4578 cols = (Columns + GAP - 3) / INC;
4579 if (cols == 0)
4580 cols = 1;
4581 rows = (item_count + cols - 1) / cols;
4582 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004583 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 rows = item_count;
4585 for (row = 0; row < rows && !got_int; ++row)
4586 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004587 msg_putchar('\n'); // go to next line
4588 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 break;
4590 col = 0;
4591 for (i = row; i < item_count; i += rows)
4592 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 showoneopt(items[i], opt_flags);
4595 col += INC;
4596 }
4597 out_flush();
4598 ui_breakcheck();
4599 }
4600 }
4601 vim_free(items);
4602}
4603
4604/*
4605 * Return TRUE if option "p" has its default value.
4606 */
4607 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004608optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 int dvi;
4611
4612 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004613 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004614 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004616 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 // the cast to long is required for Manx C, long_i is
4619 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004620 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004621 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4623}
4624
4625/*
4626 * showoneopt: show the value of one option
4627 * must not be called with a hidden option!
4628 */
4629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004630showoneopt(
4631 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004632 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004634 char_u *varp;
4635 int save_silent = silent_mode;
4636
4637 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004638 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
4640 varp = get_varp_scope(p, opt_flags);
4641
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004642 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4644 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004645 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004647 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004649 msg_puts(" ");
4650 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (!(p->flags & P_BOOL))
4652 {
4653 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 option_value2string(p, opt_flags);
4656 msg_outtrans(NameBuff);
4657 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004658
4659 silent_mode = save_silent;
4660 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661}
4662
4663/*
4664 * Write modified options as ":set" commands to a file.
4665 *
4666 * There are three values for "opt_flags":
4667 * OPT_GLOBAL: Write global option values and fresh values of
4668 * buffer-local options (used for start of a session
4669 * file).
4670 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4671 * curwin (used for a vimrc file).
4672 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4673 * and local values for window-local options of
4674 * curwin. Local values are also written when at the
4675 * default value, because a modeline or autocommand
4676 * may have set them when doing ":edit file" and the
4677 * user has set them back at the default or fresh
4678 * value.
4679 * When "local_only" is TRUE, don't write fresh
4680 * values, only local values (for ":mkview").
4681 * (fresh value = value used for a new buffer or window for a local option).
4682 *
4683 * Return FAIL on error, OK otherwise.
4684 */
4685 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004686makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004689 char_u *varp; // currently used value
4690 char_u *varp_fresh; // local value
4691 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 char *cmd;
4693 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004694 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 /*
4697 * The options that don't have a default (terminal name, columns, lines)
4698 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004699 * Do the loop over "options[]" twice: once for options with the
4700 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004702 for (pri = 1; pri >= 0; --pri)
4703 {
4704 for (p = &options[0]; !istermoption(p); p++)
4705 if (!(p->flags & P_NO_MKRC)
4706 && !istermoption(p)
4707 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004709 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4711 continue;
4712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004713 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4714 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4716 continue;
4717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004718 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004720 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 continue;
4722
Bram Moolenaard23b7142021-04-17 21:04:34 +02004723 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4724 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004725 continue;
4726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 round = 2;
4728 if (p->indir != PV_NONE)
4729 {
4730 if (p->var == VAR_WIN)
4731 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004732 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 if (!(opt_flags & OPT_LOCAL))
4734 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 // When fresh value of window-local option is not at the
4736 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4738 {
4739 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004740 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
4742 round = 1;
4743 varp_local = varp;
4744 varp = varp_fresh;
4745 }
4746 }
4747 }
4748 }
4749
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004750 // Round 1: fresh value for window-local options.
4751 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 for ( ; round <= 2; varp = varp_local, ++round)
4753 {
4754 if (round == 1 || (opt_flags & OPT_GLOBAL))
4755 cmd = "set";
4756 else
4757 cmd = "setlocal";
4758
4759 if (p->flags & P_BOOL)
4760 {
4761 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4762 return FAIL;
4763 }
4764 else if (p->flags & P_NUM)
4765 {
4766 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4767 return FAIL;
4768 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004771 int do_endif = FALSE;
4772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004773 // Don't set 'syntax' and 'filetype' again if the value is
4774 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004775 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004776#if defined(FEAT_SYN_HL)
4777 p->indir == PV_SYN ||
4778#endif
4779 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
4781 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4782 *(char_u **)(varp)) < 0
4783 || put_eol(fd) < 0)
4784 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004785 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004788 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004790 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
4792 if (put_line(fd, "endif") == FAIL)
4793 return FAIL;
4794 }
4795 }
4796 }
4797 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 return OK;
4800}
4801
4802#if defined(FEAT_FOLDING) || defined(PROTO)
4803/*
4804 * Generate set commands for the local fold options only. Used when
4805 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4806 */
4807 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004808makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004810 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004812 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 == FAIL
4814# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004815 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004817 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 == FAIL
4819 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4820 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4821 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4822 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4823 )
4824 return FAIL;
4825
4826 return OK;
4827}
4828#endif
4829
4830 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004831put_setstring(
4832 FILE *fd,
4833 char *cmd,
4834 char *name,
4835 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004836 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
4838 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004839 char_u *buf = NULL;
4840 char_u *part = NULL;
4841 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4844 return FAIL;
4845 if (*valuep != NULL)
4846 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004847 // Output 'pastetoggle' as key names. For other
4848 // options some characters have to be escaped with
4849 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (valuep == &p_pt)
4851 {
4852 s = *valuep;
4853 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004854 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return FAIL;
4856 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004857 // expand the option value, replace $HOME by ~
4858 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004860 int size = (int)STRLEN(*valuep) + 1;
4861
4862 // replace home directory in the whole option value into "buf"
4863 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004864 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004865 goto fail;
4866 home_replace(NULL, *valuep, buf, size, FALSE);
4867
4868 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004869 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004870 // can be expanded when read back.
4871 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4872 && vim_strchr(*valuep, ',') != NULL)
4873 {
4874 part = alloc(size);
4875 if (part == NULL)
4876 goto fail;
4877
4878 // write line break to clear the option, e.g. ':set rtp='
4879 if (put_eol(fd) == FAIL)
4880 goto fail;
4881
4882 p = buf;
4883 while (*p != NUL)
4884 {
4885 // for each comma separated option part, append value to
4886 // the option, :set rtp+=value
4887 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4888 goto fail;
4889 (void)copy_option_part(&p, part, size, ",");
4890 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4891 goto fail;
4892 }
4893 vim_free(buf);
4894 vim_free(part);
4895 return OK;
4896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004898 {
4899 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004901 }
4902 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 else if (put_escstr(fd, *valuep, 2) == FAIL)
4905 return FAIL;
4906 }
4907 if (put_eol(fd) < 0)
4908 return FAIL;
4909 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004910fail:
4911 vim_free(buf);
4912 vim_free(part);
4913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914}
4915
4916 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004917put_setnum(
4918 FILE *fd,
4919 char *cmd,
4920 char *name,
4921 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 long wc;
4924
4925 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4926 return FAIL;
4927 if (wc_use_keyname((char_u *)valuep, &wc))
4928 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004929 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4931 return FAIL;
4932 }
4933 else if (fprintf(fd, "%ld", *valuep) < 0)
4934 return FAIL;
4935 if (put_eol(fd) < 0)
4936 return FAIL;
4937 return OK;
4938}
4939
4940 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004941put_setbool(
4942 FILE *fd,
4943 char *cmd,
4944 char *name,
4945 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004947 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004948 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4950 || put_eol(fd) < 0)
4951 return FAIL;
4952 return OK;
4953}
4954
4955/*
4956 * Clear all the terminal options.
4957 * If the option has been allocated, free the memory.
4958 * Terminal options are never hidden or indirect.
4959 */
4960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004961clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 /*
4964 * Reset a few things before clearing the old options. This may cause
4965 * outputting a few things that the terminal doesn't understand, but the
4966 * screen will be cleared later, so this is OK.
4967 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004968 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004970 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971#endif
4972#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004973 // When starting the GUI close the display opened for the clipboard.
4974 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (gui.starting)
4976 clear_xterm_clip();
4977#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004978 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004980 free_termoptions();
4981}
4982
4983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004984free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004985{
4986 struct vimoption *p;
4987
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004988 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 if (istermoption(p))
4990 {
4991 if (p->flags & P_ALLOCED)
4992 free_string_option(*(char_u **)(p->var));
4993 if (p->flags & P_DEF_ALLOCED)
4994 free_string_option(p->def_val[VI_DEFAULT]);
4995 *(char_u **)(p->var) = empty_option;
4996 p->def_val[VI_DEFAULT] = empty_option;
4997 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004998#ifdef FEAT_EVAL
4999 // remember where the option was cleared
5000 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 clear_termcodes();
5004}
5005
5006/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005007 * Free the string for one term option, if it was allocated.
5008 * Set the string to empty_option and clear allocated flag.
5009 * "var" points to the option value.
5010 */
5011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005012free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005013{
5014 struct vimoption *p;
5015
5016 for (p = &options[0]; p->fullname != NULL; p++)
5017 if (p->var == var)
5018 {
5019 if (p->flags & P_ALLOCED)
5020 free_string_option(*(char_u **)(p->var));
5021 *(char_u **)(p->var) = empty_option;
5022 p->flags &= ~P_ALLOCED;
5023 break;
5024 }
5025}
5026
5027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 * Set the terminal option defaults to the current value.
5029 * Used after setting the terminal name.
5030 */
5031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 struct vimoption *p;
5035
5036 for (p = &options[0]; p->fullname != NULL; p++)
5037 {
5038 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5039 {
5040 if (p->flags & P_DEF_ALLOCED)
5041 {
5042 free_string_option(p->def_val[VI_DEFAULT]);
5043 p->flags &= ~P_DEF_ALLOCED;
5044 }
5045 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5046 if (p->flags & P_ALLOCED)
5047 {
5048 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005049 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051 }
5052 }
5053}
5054
5055/*
5056 * return TRUE if 'p' starts with 't_'
5057 */
5058 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005059istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5062}
5063
Bram Moolenaardac13472019-09-16 21:06:21 +02005064/*
5065 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5066 */
5067 int
5068istermoption_idx(int opt_idx)
5069{
5070 return istermoption(&options[opt_idx]);
5071}
5072
Bram Moolenaar113e1072019-01-20 15:30:40 +01005073#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005075 * Unset local option value, similar to ":set opt<".
5076 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005078unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005079{
5080 struct vimoption *p;
5081 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005082 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005083
5084 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005085 if (opt_idx < 0)
5086 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005087 p = &(options[opt_idx]);
5088
5089 switch ((int)p->indir)
5090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005091 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005092 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005093 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005094 break;
5095 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005096 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 break;
5098 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005099 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005100 break;
5101 case PV_AR:
5102 buf->b_p_ar = -1;
5103 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005104 case PV_BKC:
5105 clear_string_option(&buf->b_p_bkc);
5106 buf->b_bkc_flags = 0;
5107 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005108 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005109 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005110 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005111 case PV_TC:
5112 clear_string_option(&buf->b_p_tc);
5113 buf->b_tc_flags = 0;
5114 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005115 case PV_SISO:
5116 curwin->w_p_siso = -1;
5117 break;
5118 case PV_SO:
5119 curwin->w_p_so = -1;
5120 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005121#ifdef FEAT_FIND_ID
5122 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005123 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005124 break;
5125 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005126 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005127 break;
5128#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005129 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005130 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 break;
5132 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005133 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005134 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005135#ifdef FEAT_COMPL_FUNC
5136 case PV_TSRFU:
5137 clear_string_option(&buf->b_p_tsrfu);
5138 break;
5139#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005140 case PV_FP:
5141 clear_string_option(&buf->b_p_fp);
5142 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005143#ifdef FEAT_QUICKFIX
5144 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005145 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005146 break;
5147 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005148 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005149 break;
5150 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005151 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005152 break;
5153#endif
5154#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5155 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005156 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 break;
5158#endif
5159#if defined(FEAT_CRYPT)
5160 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005161 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005162 break;
5163#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005164#ifdef FEAT_LINEBREAK
5165 case PV_SBR:
5166 clear_string_option(&((win_T *)from)->w_p_sbr);
5167 break;
5168#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005169#ifdef FEAT_STL_OPT
5170 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005171 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005172 break;
5173#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005174 case PV_UL:
5175 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5176 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005177#ifdef FEAT_LISP
5178 case PV_LW:
5179 clear_string_option(&buf->b_p_lw);
5180 break;
5181#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005182 case PV_MENC:
5183 clear_string_option(&buf->b_p_menc);
5184 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005185 case PV_LCS:
5186 clear_string_option(&((win_T *)from)->w_p_lcs);
5187 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5188 redraw_later(NOT_VALID);
5189 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005190 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005191 clear_string_option(&((win_T *)from)->w_p_ve);
5192 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005193 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005194 }
5195}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005196#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005197
5198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 * Get pointer to option variable, depending on local or global scope.
5200 */
5201 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
5204 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5205 {
5206 if (p->var == VAR_WIN)
5207 return (char_u *)GLOBAL_WO(get_varp(p));
5208 return p->var;
5209 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005210 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
5212 switch ((int)p->indir)
5213 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005214 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005216 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5217 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5218 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005220 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5221 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5222 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005223 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005224 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005225 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005226 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5227 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005229 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5230 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005232 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5233 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005234#ifdef FEAT_COMPL_FUNC
5235 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5236#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005237#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5238 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5239#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005240#if defined(FEAT_CRYPT)
5241 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5242#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005243#ifdef FEAT_LINEBREAK
5244 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5245#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005246#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005247 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005248#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005249 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005250#ifdef FEAT_LISP
5251 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5252#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005253 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005254 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005255 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005256 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005259 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261 return get_varp(p);
5262}
5263
5264/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005265 * Get pointer to option variable at 'opt_idx', depending on local or global
5266 * scope.
5267 */
5268 char_u *
5269get_option_varp_scope(int opt_idx, int opt_flags)
5270{
5271 return get_varp_scope(&(options[opt_idx]), opt_flags);
5272}
5273
5274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 * Get pointer to option variable.
5276 */
5277 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005280 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (p->var == NULL)
5282 return NULL;
5283
5284 switch ((int)p->indir)
5285 {
5286 case PV_NONE: return p->var;
5287
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005288 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005289 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005291 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005293 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005295 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005297 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005299 case PV_TC: return *curbuf->b_p_tc != NUL
5300 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005301 case PV_BKC: return *curbuf->b_p_bkc != NUL
5302 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005303 case PV_SISO: return curwin->w_p_siso >= 0
5304 ? (char_u *)&(curwin->w_p_siso) : p->var;
5305 case PV_SO: return curwin->w_p_so >= 0
5306 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005308 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005310 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5312#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005313 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005315 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005317#ifdef FEAT_COMPL_FUNC
5318 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5319 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5320#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005321 case PV_FP: return *curbuf->b_p_fp != NUL
5322 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005324 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005326 case PV_GP: return *curbuf->b_p_gp != NUL
5327 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5328 case PV_MP: return *curbuf->b_p_mp != NUL
5329 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005331#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5332 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5333 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5334#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005335#if defined(FEAT_CRYPT)
5336 case PV_CM: return *curbuf->b_p_cm != NUL
5337 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5338#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005339#ifdef FEAT_LINEBREAK
5340 case PV_SBR: return *curwin->w_p_sbr != NUL
5341 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5342#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005343#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005344 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005345 ? (char_u *)&(curwin->w_p_stl) : p->var;
5346#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005347 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5348 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005349#ifdef FEAT_LISP
5350 case PV_LW: return *curbuf->b_p_lw != NUL
5351 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5352#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005353 case PV_MENC: return *curbuf->b_p_menc != NUL
5354 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#ifdef FEAT_ARABIC
5356 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5357#endif
5358 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005359 case PV_LCS: return *curwin->w_p_lcs != NUL
5360 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005361 case PV_VE: return *curwin->w_p_ve != NUL
5362 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005363#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005364 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5365#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005366#ifdef FEAT_SYN_HL
5367 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5368 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005369 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005370 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#ifdef FEAT_DIFF
5373 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5374#endif
5375#ifdef FEAT_FOLDING
5376 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5377 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5378 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5379 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5380 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5381 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5382 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5383# ifdef FEAT_EVAL
5384 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5385 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5386# endif
5387 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5388#endif
5389 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005390 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005391#ifdef FEAT_LINEBREAK
5392 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005395 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005396#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5398#endif
5399#ifdef FEAT_RIGHTLEFT
5400 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5401 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5402#endif
5403 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5404 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5405#ifdef FEAT_LINEBREAK
5406 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005407 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5408 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005410 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005412 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005413#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005414 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5415 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5416#endif
5417#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005418 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5419 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5420 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5424 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5427 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5429 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5430#ifdef FEAT_CINDENT
5431 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5432 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5433 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5434#endif
5435#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5436 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#ifdef FEAT_FOLDING
5440 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005443#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005444 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005446#ifdef FEAT_COMPL_FUNC
5447 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005448 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005449#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005450#ifdef FEAT_EVAL
5451 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005454 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005460 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5462 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5463 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5464 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5465#ifdef FEAT_FIND_ID
5466# ifdef FEAT_EVAL
5467 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5468# endif
5469#endif
5470#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5471 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5472 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5473#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005474#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005475 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#ifdef FEAT_CRYPT
5478 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5479#endif
5480#ifdef FEAT_LISP
5481 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5482#endif
5483 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5484 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5485 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5486 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5487 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005489#ifdef FEAT_TEXTOBJ
5490 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5493#ifdef FEAT_SMARTINDENT
5494 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5498#ifdef FEAT_SEARCHPATH
5499 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5500#endif
5501 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5502#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005503 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005504 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5505#endif
5506#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005507 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5508 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5509 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005510 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511#endif
5512 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5513 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5514 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5515 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005516#ifdef FEAT_PERSISTENT_UNDO
5517 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5520#ifdef FEAT_KEYMAP
5521 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5522#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005523#ifdef FEAT_SIGNS
5524 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5525#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005526#ifdef FEAT_VARTABS
5527 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5528 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5529#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005530 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005532 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 return (char_u *)&(curbuf->b_p_wm);
5534}
5535
5536/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005537 * Return a pointer to the variable for option at 'opt_idx'
5538 */
5539 char_u *
5540get_option_var(int opt_idx)
5541{
5542 return options[opt_idx].var;
5543}
5544
5545/*
5546 * Return the full name of the option at 'opt_idx'
5547 */
5548 char_u *
5549get_option_fullname(int opt_idx)
5550{
5551 return (char_u *)options[opt_idx].fullname;
5552}
5553
5554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 * Get the value of 'equalprg', either the buffer-local one or the global one.
5556 */
5557 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005558get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560 if (*curbuf->b_p_ep == NUL)
5561 return p_ep;
5562 return curbuf->b_p_ep;
5563}
5564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565/*
5566 * Copy options from one window to another.
5567 * Used when splitting a window.
5568 */
5569 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005570win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5573 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005574 after_copy_winopt(wp_to);
5575}
5576
5577/*
5578 * After copying window options: update variables depending on options.
5579 */
5580 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005581after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005582{
5583#ifdef FEAT_LINEBREAK
5584 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005585#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005586#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005587 fill_culopt_flags(NULL, wp);
5588 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005589#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005590 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
5593/*
5594 * Copy the options from one winopt_T to another.
5595 * Doesn't free the old option values in "to", use clear_winopt() for that.
5596 * The 'scroll' option is not copied, because it depends on the window height.
5597 * The 'previewwindow' option is reset, there can be only one preview window.
5598 */
5599 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005600copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602#ifdef FEAT_ARABIC
5603 to->wo_arab = from->wo_arab;
5604#endif
5605 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005606 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005608 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005609 to->wo_ve = vim_strsave(from->wo_ve);
5610 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005611#ifdef FEAT_LINEBREAK
5612 to->wo_nuw = from->wo_nuw;
5613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614#ifdef FEAT_RIGHTLEFT
5615 to->wo_rl = from->wo_rl;
5616 to->wo_rlc = vim_strsave(from->wo_rlc);
5617#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005618#ifdef FEAT_LINEBREAK
5619 to->wo_sbr = vim_strsave(from->wo_sbr);
5620#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005621#ifdef FEAT_STL_OPT
5622 to->wo_stl = vim_strsave(from->wo_stl);
5623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005625#ifdef FEAT_DIFF
5626 to->wo_wrap_save = from->wo_wrap_save;
5627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628#ifdef FEAT_LINEBREAK
5629 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005630 to->wo_bri = from->wo_bri;
5631 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005633 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005635 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005636 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005637 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005638#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005639 to->wo_spell = from->wo_spell;
5640#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005641#ifdef FEAT_SYN_HL
5642 to->wo_cuc = from->wo_cuc;
5643 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005644 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647#ifdef FEAT_DIFF
5648 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005649 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005651#ifdef FEAT_CONCEAL
5652 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005653 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005654#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005655#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005656 to->wo_twk = vim_strsave(from->wo_twk);
5657 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659#ifdef FEAT_FOLDING
5660 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005661 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005663 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 to->wo_fdi = vim_strsave(from->wo_fdi);
5665 to->wo_fml = from->wo_fml;
5666 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005667 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005669 to->wo_fdm_save = from->wo_diff_saved
5670 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 to->wo_fdn = from->wo_fdn;
5672# ifdef FEAT_EVAL
5673 to->wo_fde = vim_strsave(from->wo_fde);
5674 to->wo_fdt = vim_strsave(from->wo_fdt);
5675# endif
5676 to->wo_fmr = vim_strsave(from->wo_fmr);
5677#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005678#ifdef FEAT_SIGNS
5679 to->wo_scl = vim_strsave(from->wo_scl);
5680#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005681
5682#ifdef FEAT_EVAL
5683 // Copy the script context so that we know where the value was last set.
5684 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5685 sizeof(to->wo_script_ctx));
5686#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005687 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688}
5689
5690/*
5691 * Check string options in a window for a NULL value.
5692 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005694check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
5696 check_winopt(&win->w_onebuf_opt);
5697 check_winopt(&win->w_allbuf_opt);
5698}
5699
5700/*
5701 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5702 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005704check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
5706#ifdef FEAT_FOLDING
5707 check_string_option(&wop->wo_fdi);
5708 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005709 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710# ifdef FEAT_EVAL
5711 check_string_option(&wop->wo_fde);
5712 check_string_option(&wop->wo_fdt);
5713# endif
5714 check_string_option(&wop->wo_fmr);
5715#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005716#ifdef FEAT_SIGNS
5717 check_string_option(&wop->wo_scl);
5718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#ifdef FEAT_RIGHTLEFT
5720 check_string_option(&wop->wo_rlc);
5721#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005722#ifdef FEAT_LINEBREAK
5723 check_string_option(&wop->wo_sbr);
5724#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005725#ifdef FEAT_STL_OPT
5726 check_string_option(&wop->wo_stl);
5727#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005728#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005729 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005730 check_string_option(&wop->wo_cc);
5731#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005732#ifdef FEAT_CONCEAL
5733 check_string_option(&wop->wo_cocu);
5734#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005735#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005736 check_string_option(&wop->wo_twk);
5737 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005738#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005739#ifdef FEAT_LINEBREAK
5740 check_string_option(&wop->wo_briopt);
5741#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005742 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005743 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005744 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745}
5746
5747/*
5748 * Free the allocated memory inside a winopt_T.
5749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005751clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753#ifdef FEAT_FOLDING
5754 clear_string_option(&wop->wo_fdi);
5755 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005756 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757# ifdef FEAT_EVAL
5758 clear_string_option(&wop->wo_fde);
5759 clear_string_option(&wop->wo_fdt);
5760# endif
5761 clear_string_option(&wop->wo_fmr);
5762#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005763#ifdef FEAT_SIGNS
5764 clear_string_option(&wop->wo_scl);
5765#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005766#ifdef FEAT_LINEBREAK
5767 clear_string_option(&wop->wo_briopt);
5768#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005769 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770#ifdef FEAT_RIGHTLEFT
5771 clear_string_option(&wop->wo_rlc);
5772#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005773#ifdef FEAT_LINEBREAK
5774 clear_string_option(&wop->wo_sbr);
5775#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005776#ifdef FEAT_STL_OPT
5777 clear_string_option(&wop->wo_stl);
5778#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005779#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005780 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005781 clear_string_option(&wop->wo_cc);
5782#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005783#ifdef FEAT_CONCEAL
5784 clear_string_option(&wop->wo_cocu);
5785#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005786#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005787 clear_string_option(&wop->wo_twk);
5788 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005789#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005790 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005791 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792}
5793
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005794#ifdef FEAT_EVAL
5795// Index into the options table for a buffer-local option enum.
5796static int buf_opt_idx[BV_COUNT];
5797# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5798
5799/*
5800 * Initialize buf_opt_idx[] if not done already.
5801 */
5802 static void
5803init_buf_opt_idx(void)
5804{
5805 static int did_init_buf_opt_idx = FALSE;
5806 int i;
5807
5808 if (did_init_buf_opt_idx)
5809 return;
5810 did_init_buf_opt_idx = TRUE;
5811 for (i = 0; !istermoption_idx(i); i++)
5812 if (options[i].indir & PV_BUF)
5813 buf_opt_idx[options[i].indir & PV_MASK] = i;
5814}
5815#else
5816# define COPY_OPT_SCTX(buf, bv)
5817#endif
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819/*
5820 * Copy global option values to local options for one buffer.
5821 * Used when creating a new buffer and sometimes when entering a buffer.
5822 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005823 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5825 * appropriate.
5826 * BCO_NOHELP Don't copy the values to a help buffer.
5827 */
5828 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005829buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005832 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 int dont_do_help;
5834 int did_isk = FALSE;
5835
5836 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 * Skip this when the option defaults have not been set yet. Happens when
5838 * main() allocates the first buffer.
5839 */
5840 if (p_cpo != NULL)
5841 {
5842 /*
5843 * Always copy when entering and 'cpo' contains 'S'.
5844 * Don't copy when already initialized.
5845 * Don't copy when 'cpo' contains 's' and not entering.
5846 * 'S' BCO_ENTER initialized 's' should_copy
5847 * yes yes X X TRUE
5848 * yes no yes X FALSE
5849 * no X yes X FALSE
5850 * X no no yes FALSE
5851 * X no no no TRUE
5852 * no yes no X TRUE
5853 */
5854 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5855 && (buf->b_p_initialized
5856 || (!(flags & BCO_ENTER)
5857 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5858 should_copy = FALSE;
5859
5860 if (should_copy || (flags & BCO_ALWAYS))
5861 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005862#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005863 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005864 init_buf_opt_idx();
5865#endif
5866 // Don't copy the options specific to a help buffer when
5867 // BCO_NOHELP is given or the options were initialized already
5868 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5870 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005871 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
5873 save_p_isk = buf->b_p_isk;
5874 buf->b_p_isk = NULL;
5875 }
5876 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005877 * Always free the allocated strings. If not already initialized,
5878 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 */
5880 if (!buf->b_p_initialized)
5881 {
5882 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005883 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005886 switch (*p_ffs)
5887 {
5888 case 'm':
5889 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5890 case 'd':
5891 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5892 case 'u':
5893 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5894 default:
5895 buf->b_p_ff = vim_strsave(p_ff);
5896 }
5897 if (buf->b_p_ff != NULL)
5898 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 buf->b_p_bh = empty_option;
5900 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902 else
5903 free_buf_options(buf, FALSE);
5904
5905 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005906 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 buf->b_p_ai_nopaste = p_ai_nopaste;
5908 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005909 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005911 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 buf->b_p_tw_nopaste = p_tw_nopaste;
5913 buf->b_p_tw_nobin = p_tw_nobin;
5914 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005915 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 buf->b_p_wm_nopaste = p_wm_nopaste;
5917 buf->b_p_wm_nobin = p_wm_nobin;
5918 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005919 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005920 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005921 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005922 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005923 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005925 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005927 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005929 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 buf->b_p_ml_nobin = p_ml_nobin;
5931 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005933 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 buf->b_p_swf = FALSE;
5935 else
5936 {
5937 buf->b_p_swf = p_swf;
5938 COPY_OPT_SCTX(buf, BV_INF);
5939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005941 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005942#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005943 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005946#ifdef FEAT_COMPL_FUNC
5947 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005948 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005949 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005950 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005951#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005952#ifdef FEAT_EVAL
5953 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005954 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005959#ifdef FEAT_VARTABS
5960 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005961 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005962 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005963 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005964 else
5965 buf->b_p_vsts_array = 0;
5966 buf->b_p_vsts_nopaste = p_vsts_nopaste
5967 ? vim_strsave(p_vsts_nopaste) : NULL;
5968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005972 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#ifdef FEAT_FOLDING
5974 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005975 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976#endif
5977 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005978 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005979 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005980 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005981 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5982 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005984 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005986 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987#ifdef FEAT_SMARTINDENT
5988 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005989 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990#endif
5991 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005992 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#ifdef FEAT_CINDENT
5994 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005995 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005997 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005999 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006004 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6006 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#endif
6009#ifdef FEAT_LISP
6010 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006011 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012#endif
6013#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006014 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006016 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006018 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006019#endif
6020#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006021 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006022 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006023 (void)compile_cap_prog(&buf->b_s);
6024 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006026 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006027 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006028 buf->b_s.b_p_spo = vim_strsave(p_spo);
6029 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030#endif
6031#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6032 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006035 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006037 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038#if defined(FEAT_EVAL)
6039 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006040 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#ifdef FEAT_CRYPT
6043 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006044 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045#endif
6046#ifdef FEAT_SEARCHPATH
6047 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006048 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#endif
6050#ifdef FEAT_KEYMAP
6051 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006052 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 buf->b_kmap_state |= KEYMAP_INIT;
6054#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006055#ifdef FEAT_TERMINAL
6056 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006057 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006058#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006059 // This isn't really an option, but copying the langmap and IME
6060 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006062 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006064 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006066 // options that are normally global but also have a local value
6067 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006069 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006070 buf->b_p_bkc = empty_option;
6071 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072#ifdef FEAT_QUICKFIX
6073 buf->b_p_gp = empty_option;
6074 buf->b_p_mp = empty_option;
6075 buf->b_p_efm = empty_option;
6076#endif
6077 buf->b_p_ep = empty_option;
6078 buf->b_p_kp = empty_option;
6079 buf->b_p_path = empty_option;
6080 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006081 buf->b_p_tc = empty_option;
6082 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083#ifdef FEAT_FIND_ID
6084 buf->b_p_def = empty_option;
6085 buf->b_p_inc = empty_option;
6086# ifdef FEAT_EVAL
6087 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006088 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089# endif
6090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 buf->b_p_dict = empty_option;
6092 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006093#ifdef FEAT_COMPL_FUNC
6094 buf->b_p_tsrfu = empty_option;
6095#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006096#ifdef FEAT_TEXTOBJ
6097 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006098 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006099#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006100#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6101 buf->b_p_bexpr = empty_option;
6102#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006103#if defined(FEAT_CRYPT)
6104 buf->b_p_cm = empty_option;
6105#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006106#ifdef FEAT_PERSISTENT_UNDO
6107 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006108 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006109#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006110#ifdef FEAT_LISP
6111 buf->b_p_lw = empty_option;
6112#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006113 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
6115 /*
6116 * Don't copy the options set by ex_help(), use the saved values,
6117 * when going from a help buffer to a non-help buffer.
6118 * Don't touch these at all when BCO_NOHELP is used and going from
6119 * or to a help buffer.
6120 */
6121 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006124#ifdef FEAT_VARTABS
6125 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006126 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006127 else
6128 buf->b_p_vts_array = NULL;
6129#endif
6130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 else
6132 {
6133 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006134 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 did_isk = TRUE;
6136 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006137#ifdef FEAT_VARTABS
6138 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006139 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006140 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006141 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006142 else
6143 buf->b_p_vts_array = NULL;
6144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (buf->b_p_bt[0] == 'h')
6147 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006149 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 }
6151 }
6152
6153 /*
6154 * When the options should be copied (ignoring BCO_ALWAYS), set the
6155 * flag that indicates that the options have been initialized.
6156 */
6157 if (should_copy)
6158 buf->b_p_initialized = TRUE;
6159 }
6160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006161 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (did_isk)
6163 (void)buf_init_chartab(buf, FALSE);
6164}
6165
6166/*
6167 * Reset the 'modifiable' option and its default value.
6168 */
6169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006170reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 int opt_idx;
6173
6174 curbuf->b_p_ma = FALSE;
6175 p_ma = FALSE;
6176 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006177 if (opt_idx >= 0)
6178 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179}
6180
6181/*
6182 * Set the global value for 'iminsert' to the local value.
6183 */
6184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006185set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
6187 p_iminsert = curbuf->b_p_iminsert;
6188}
6189
6190/*
6191 * Set the global value for 'imsearch' to the local value.
6192 */
6193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006194set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 p_imsearch = curbuf->b_p_imsearch;
6197}
6198
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199static int expand_option_idx = -1;
6200static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6201static int expand_option_flags = 0;
6202
6203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006204set_context_in_set_cmd(
6205 expand_T *xp,
6206 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006207 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006210 long_u flags = 0; // init for GCC
6211 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 char_u *p;
6213 char_u *s;
6214 int is_term_option = FALSE;
6215 int key;
6216
6217 expand_option_flags = opt_flags;
6218
6219 xp->xp_context = EXPAND_SETTINGS;
6220 if (*arg == NUL)
6221 {
6222 xp->xp_pattern = arg;
6223 return;
6224 }
6225 p = arg + STRLEN(arg) - 1;
6226 if (*p == ' ' && *(p - 1) != '\\')
6227 {
6228 xp->xp_pattern = p + 1;
6229 return;
6230 }
6231 while (p > arg)
6232 {
6233 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006234 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (*p == ' ' || *p == ',')
6236 {
6237 while (s > arg && *(s - 1) == '\\')
6238 --s;
6239 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006240 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (*p == ' ' && ((p - s) & 1) == 0)
6242 {
6243 ++p;
6244 break;
6245 }
6246 --p;
6247 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006248 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
6250 xp->xp_context = EXPAND_BOOL_SETTINGS;
6251 p += 2;
6252 }
6253 if (STRNCMP(p, "inv", 3) == 0)
6254 {
6255 xp->xp_context = EXPAND_BOOL_SETTINGS;
6256 p += 3;
6257 }
6258 xp->xp_pattern = arg = p;
6259 if (*arg == '<')
6260 {
6261 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006262 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 return;
6264 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006265 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
6267 xp->xp_context = EXPAND_NOTHING;
6268 return;
6269 }
6270 nextchar = *++p;
6271 is_term_option = TRUE;
6272 expand_option_name[2] = KEY2TERMCAP0(key);
6273 expand_option_name[3] = KEY2TERMCAP1(key);
6274 }
6275 else
6276 {
6277 if (p[0] == 't' && p[1] == '_')
6278 {
6279 p += 2;
6280 if (*p != NUL)
6281 ++p;
6282 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006283 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 nextchar = *++p;
6285 is_term_option = TRUE;
6286 expand_option_name[2] = p[-2];
6287 expand_option_name[3] = p[-1];
6288 }
6289 else
6290 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006291 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6293 p++;
6294 if (*p == NUL)
6295 return;
6296 nextchar = *p;
6297 *p = NUL;
6298 opt_idx = findoption(arg);
6299 *p = nextchar;
6300 if (opt_idx == -1 || options[opt_idx].var == NULL)
6301 {
6302 xp->xp_context = EXPAND_NOTHING;
6303 return;
6304 }
6305 flags = options[opt_idx].flags;
6306 if (flags & P_BOOL)
6307 {
6308 xp->xp_context = EXPAND_NOTHING;
6309 return;
6310 }
6311 }
6312 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006313 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6315 {
6316 ++p;
6317 nextchar = '=';
6318 }
6319 if ((nextchar != '=' && nextchar != ':')
6320 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6321 {
6322 xp->xp_context = EXPAND_UNSUCCESSFUL;
6323 return;
6324 }
6325 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6326 {
6327 xp->xp_context = EXPAND_OLD_SETTING;
6328 if (is_term_option)
6329 expand_option_idx = -1;
6330 else
6331 expand_option_idx = opt_idx;
6332 xp->xp_pattern = p + 1;
6333 return;
6334 }
6335 xp->xp_context = EXPAND_NOTHING;
6336 if (is_term_option || (flags & P_NUM))
6337 return;
6338
6339 xp->xp_pattern = p + 1;
6340
6341 if (flags & P_EXPAND)
6342 {
6343 p = options[opt_idx].var;
6344 if (p == (char_u *)&p_bdir
6345 || p == (char_u *)&p_dir
6346 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006347 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 || p == (char_u *)&p_rtp
6349#ifdef FEAT_SEARCHPATH
6350 || p == (char_u *)&p_cdpath
6351#endif
6352#ifdef FEAT_SESSION
6353 || p == (char_u *)&p_vdir
6354#endif
6355 )
6356 {
6357 xp->xp_context = EXPAND_DIRECTORIES;
6358 if (p == (char_u *)&p_path
6359#ifdef FEAT_SEARCHPATH
6360 || p == (char_u *)&p_cdpath
6361#endif
6362 )
6363 xp->xp_backslash = XP_BS_THREE;
6364 else
6365 xp->xp_backslash = XP_BS_ONE;
6366 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006367 else if (p == (char_u *)&p_ft)
6368 {
6369 xp->xp_context = EXPAND_FILETYPE;
6370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 else
6372 {
6373 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006374 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 if (p == (char_u *)&p_tags)
6376 xp->xp_backslash = XP_BS_THREE;
6377 else
6378 xp->xp_backslash = XP_BS_ONE;
6379 }
6380 }
6381
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006382 // For an option that is a list of file names, find the start of the
6383 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6385 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006386 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 if (*p == ' ' || *p == ',')
6388 {
6389 s = p;
6390 while (s > xp->xp_pattern && *(s - 1) == '\\')
6391 --s;
6392 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6393 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6394 {
6395 xp->xp_pattern = p + 1;
6396 break;
6397 }
6398 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006399
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006400#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006401 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006402 if (options[opt_idx].var == (char_u *)&p_sps
6403 && STRNCMP(p, "file:", 5) == 0)
6404 {
6405 xp->xp_pattern = p + 5;
6406 break;
6407 }
6408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
6412 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006413ExpandSettings(
6414 expand_T *xp,
6415 regmatch_T *regmatch,
6416 int *num_file,
6417 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006419 int num_normal = 0; // Nr of matching non-term-code settings
6420 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 int opt_idx;
6422 int match;
6423 int count = 0;
6424 char_u *str;
6425 int loop;
6426 int is_term_opt;
6427 char_u name_buf[MAX_KEY_NAME_LEN];
6428 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006429 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006431 // do this loop twice:
6432 // loop == 0: count the number of matching options
6433 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 for (loop = 0; loop <= 1; ++loop)
6435 {
6436 regmatch->rm_ic = ic;
6437 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6438 {
K.Takataeeec2542021-06-02 13:28:16 +02006439 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6441 {
6442 if (loop == 0)
6443 num_normal++;
6444 else
6445 (*file)[count++] = vim_strsave((char_u *)names[match]);
6446 }
6447 }
6448 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6449 opt_idx++)
6450 {
6451 if (options[opt_idx].var == NULL)
6452 continue;
6453 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6454 && !(options[opt_idx].flags & P_BOOL))
6455 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006456 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (is_term_opt && num_normal > 0)
6458 continue;
6459 match = FALSE;
6460 if (vim_regexec(regmatch, str, (colnr_T)0)
6461 || (options[opt_idx].shortname != NULL
6462 && vim_regexec(regmatch,
6463 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6464 match = TRUE;
6465 else if (is_term_opt)
6466 {
6467 name_buf[0] = '<';
6468 name_buf[1] = 't';
6469 name_buf[2] = '_';
6470 name_buf[3] = str[2];
6471 name_buf[4] = str[3];
6472 name_buf[5] = '>';
6473 name_buf[6] = NUL;
6474 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6475 {
6476 match = TRUE;
6477 str = name_buf;
6478 }
6479 }
6480 if (match)
6481 {
6482 if (loop == 0)
6483 {
6484 if (is_term_opt)
6485 num_term++;
6486 else
6487 num_normal++;
6488 }
6489 else
6490 (*file)[count++] = vim_strsave(str);
6491 }
6492 }
6493 /*
6494 * Check terminal key codes, these are not in the option table
6495 */
6496 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6497 {
6498 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6499 {
6500 if (!isprint(str[0]) || !isprint(str[1]))
6501 continue;
6502
6503 name_buf[0] = 't';
6504 name_buf[1] = '_';
6505 name_buf[2] = str[0];
6506 name_buf[3] = str[1];
6507 name_buf[4] = NUL;
6508
6509 match = FALSE;
6510 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6511 match = TRUE;
6512 else
6513 {
6514 name_buf[0] = '<';
6515 name_buf[1] = 't';
6516 name_buf[2] = '_';
6517 name_buf[3] = str[0];
6518 name_buf[4] = str[1];
6519 name_buf[5] = '>';
6520 name_buf[6] = NUL;
6521
6522 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6523 match = TRUE;
6524 }
6525 if (match)
6526 {
6527 if (loop == 0)
6528 num_term++;
6529 else
6530 (*file)[count++] = vim_strsave(name_buf);
6531 }
6532 }
6533
6534 /*
6535 * Check special key names.
6536 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006537 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6539 {
6540 name_buf[0] = '<';
6541 STRCPY(name_buf + 1, str);
6542 STRCAT(name_buf, ">");
6543
6544 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6545 {
6546 if (loop == 0)
6547 num_term++;
6548 else
6549 (*file)[count++] = vim_strsave(name_buf);
6550 }
6551 }
6552 }
6553 if (loop == 0)
6554 {
6555 if (num_normal > 0)
6556 *num_file = num_normal;
6557 else if (num_term > 0)
6558 *num_file = num_term;
6559 else
6560 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006561 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (*file == NULL)
6563 {
6564 *file = (char_u **)"";
6565 return FAIL;
6566 }
6567 }
6568 }
6569 return OK;
6570}
6571
6572 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006573ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006575 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 char_u *buf;
6577
6578 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006579 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 if (*file == NULL)
6581 return FAIL;
6582
6583 /*
6584 * For a terminal key code expand_option_idx is < 0.
6585 */
6586 if (expand_option_idx < 0)
6587 {
6588 var = find_termcode(expand_option_name + 2);
6589 if (var == NULL)
6590 expand_option_idx = findoption(expand_option_name);
6591 }
6592
6593 if (expand_option_idx >= 0)
6594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006595 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 option_value2string(&options[expand_option_idx], expand_option_flags);
6597 var = NameBuff;
6598 }
6599 else if (var == NULL)
6600 var = (char_u *)"";
6601
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006602 // A backslash is required before some characters. This is the reverse of
6603 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 buf = vim_strsave_escaped(var, escape_chars);
6605
6606 if (buf == NULL)
6607 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006608 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 return FAIL;
6610 }
6611
6612#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006613 // For MS-Windows et al. we don't double backslashes at the start and
6614 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006615 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (var[0] == '\\' && var[1] == '\\'
6617 && expand_option_idx >= 0
6618 && (options[expand_option_idx].flags & P_EXPAND)
6619 && vim_isfilec(var[2])
6620 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006621 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622#endif
6623
6624 *file[0] = buf;
6625 *num_file = 1;
6626 return OK;
6627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629/*
6630 * Get the value for the numeric or string option *opp in a nice format into
6631 * NameBuff[]. Must not be called with a hidden option!
6632 */
6633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006634option_value2string(
6635 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006636 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 char_u *varp;
6639
6640 varp = get_varp_scope(opp, opt_flags);
6641
6642 if (opp->flags & P_NUM)
6643 {
6644 long wc = 0;
6645
6646 if (wc_use_keyname(varp, &wc))
6647 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6648 else if (wc != 0)
6649 STRCPY(NameBuff, transchar((int)wc));
6650 else
6651 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6652 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006653 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
6655 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006656 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 NameBuff[0] = NUL;
6658#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006659 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006660 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 STRCPY(NameBuff, "*****");
6662#endif
6663 else if (opp->flags & P_EXPAND)
6664 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006665 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 else if ((char_u **)opp->var == &p_pt)
6667 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6668 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006669 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
6671}
6672
6673/*
6674 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6675 * printed as a keyname.
6676 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6677 */
6678 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006679wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6682 {
6683 *wcp = *(long *)varp;
6684 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6685 return TRUE;
6686 }
6687 return FALSE;
6688}
6689
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 * Return TRUE if "x" is present in 'shortmess' option, or
6692 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6693 */
6694 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006695shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006697 return p_shm != NULL &&
6698 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 || (vim_strchr(p_shm, 'a') != NULL
6700 && vim_strchr((char_u *)SHM_A, x) != NULL));
6701}
6702
6703/*
6704 * paste_option_changed() - Called after p_paste was set or reset.
6705 */
6706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006707paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709 static int old_p_paste = FALSE;
6710 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006711 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712#ifdef FEAT_CMDL_INFO
6713 static int save_ru = 0;
6714#endif
6715#ifdef FEAT_RIGHTLEFT
6716 static int save_ri = 0;
6717 static int save_hkmap = 0;
6718#endif
6719 buf_T *buf;
6720
6721 if (p_paste)
6722 {
6723 /*
6724 * Paste switched from off to on.
6725 * Save the current values, so they can be restored later.
6726 */
6727 if (!old_p_paste)
6728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006729 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006730 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {
6732 buf->b_p_tw_nopaste = buf->b_p_tw;
6733 buf->b_p_wm_nopaste = buf->b_p_wm;
6734 buf->b_p_sts_nopaste = buf->b_p_sts;
6735 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006736 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006737#ifdef FEAT_VARTABS
6738 if (buf->b_p_vsts_nopaste)
6739 vim_free(buf->b_p_vsts_nopaste);
6740 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6741 ? vim_strsave(buf->b_p_vsts) : NULL;
6742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006747 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#ifdef FEAT_CMDL_INFO
6749 save_ru = p_ru;
6750#endif
6751#ifdef FEAT_RIGHTLEFT
6752 save_ri = p_ri;
6753 save_hkmap = p_hkmap;
6754#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006756 p_ai_nopaste = p_ai;
6757 p_et_nopaste = p_et;
6758 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 p_tw_nopaste = p_tw;
6760 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006761#ifdef FEAT_VARTABS
6762 if (p_vsts_nopaste)
6763 vim_free(p_vsts_nopaste);
6764 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 }
6767
6768 /*
6769 * Always set the option values, also when 'paste' is set when it is
6770 * already on.
6771 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006772 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006773 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006775 buf->b_p_tw = 0; // textwidth is 0
6776 buf->b_p_wm = 0; // wrapmargin is 0
6777 buf->b_p_sts = 0; // softtabstop is 0
6778 buf->b_p_ai = 0; // no auto-indent
6779 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006780#ifdef FEAT_VARTABS
6781 if (buf->b_p_vsts)
6782 free_string_option(buf->b_p_vsts);
6783 buf->b_p_vsts = empty_option;
6784 if (buf->b_p_vsts_array)
6785 vim_free(buf->b_p_vsts_array);
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 // set global options
6791 p_sm = 0; // no showmatch
6792 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006795 status_redraw_all(); // redraw to remove the ruler
6796 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797#endif
6798#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 p_ri = 0; // no reverse insert
6800 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 p_tw = 0;
6804 p_wm = 0;
6805 p_sts = 0;
6806 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006807#ifdef FEAT_VARTABS
6808 if (p_vsts)
6809 free_string_option(p_vsts);
6810 p_vsts = empty_option;
6811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 }
6813
6814 /*
6815 * Paste switched from on to off: Restore saved values.
6816 */
6817 else if (old_p_paste)
6818 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006819 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006820 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 {
6822 buf->b_p_tw = buf->b_p_tw_nopaste;
6823 buf->b_p_wm = buf->b_p_wm_nopaste;
6824 buf->b_p_sts = buf->b_p_sts_nopaste;
6825 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006826 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006827#ifdef FEAT_VARTABS
6828 if (buf->b_p_vsts)
6829 free_string_option(buf->b_p_vsts);
6830 buf->b_p_vsts = buf->b_p_vsts_nopaste
6831 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6832 if (buf->b_p_vsts_array)
6833 vim_free(buf->b_p_vsts_array);
6834 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006835 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006836 else
6837 buf->b_p_vsts_array = 0;
6838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 }
6840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006841 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006843 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006846 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 p_ru = save_ru;
6848#endif
6849#ifdef FEAT_RIGHTLEFT
6850 p_ri = save_ri;
6851 p_hkmap = save_hkmap;
6852#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006853 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006854 p_ai = p_ai_nopaste;
6855 p_et = p_et_nopaste;
6856 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 p_tw = p_tw_nopaste;
6858 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006859#ifdef FEAT_VARTABS
6860 if (p_vsts)
6861 free_string_option(p_vsts);
6862 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 }
6865
6866 old_p_paste = p_paste;
6867}
6868
6869/*
6870 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6871 *
6872 * Reset 'compatible' and set the values for options that didn't get set yet
6873 * to the Vim defaults.
6874 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006875 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 */
6877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006878vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006880 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006881 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006882 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 if (!option_was_set((char_u *)"cp"))
6885 {
6886 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006887 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6889 set_option_default(opt_idx, OPT_FREE, FALSE);
6890 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006891 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893
6894 if (fname != NULL)
6895 {
6896 p = vim_getenv(envname, &dofree);
6897 if (p == NULL)
6898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006899 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006900 p = FullName_save(fname, FALSE);
6901 if (p != NULL)
6902 {
6903 vim_setenv(envname, p);
6904 vim_free(p);
6905 }
6906 }
6907 else if (dofree)
6908 vim_free(p);
6909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910}
6911
6912/*
6913 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6914 */
6915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006916change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006918 int opt_idx;
6919
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (p_cp != on)
6921 {
6922 p_cp = on;
6923 compatible_set();
6924 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006925 opt_idx = findoption((char_u *)"cp");
6926 if (opt_idx >= 0)
6927 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
6929
6930/*
6931 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006932 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 */
6934 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006935option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 int idx;
6938
6939 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006940 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 return FALSE;
6942 if (options[idx].flags & P_WAS_SET)
6943 return TRUE;
6944 return FALSE;
6945}
6946
6947/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006948 * Reset the flag indicating option "name" was set.
6949 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006951reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006952{
6953 int idx = findoption(name);
6954
6955 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006956 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006957 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006958 return OK;
6959 }
6960 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006961}
6962
6963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 * compatible_set() - Called when 'compatible' has been set or unset.
6965 *
6966 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6967 * flag) to a Vi compatible value.
6968 * When 'compatible' is unset: Set all options that have a different default
6969 * for Vim (without the P_VI_DEF flag) to that default.
6970 */
6971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006972compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 int opt_idx;
6975
Bram Moolenaardac13472019-09-16 21:06:21 +02006976 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6978 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6979 set_option_default(opt_idx, OPT_FREE, p_cp);
6980 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006981 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982}
6983
Bram Moolenaardac13472019-09-16 21:06:21 +02006984#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986/*
6987 * fill_breakat_flags() -- called when 'breakat' changes value.
6988 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006990fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006992 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 int i;
6994
6995 for (i = 0; i < 256; i++)
6996 breakat_flags[i] = FALSE;
6997
6998 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006999 for (p = p_breakat; *p; p++)
7000 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002#endif
7003
7004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 * Check if backspacing over something is allowed.
7006 */
7007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007008can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007009 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007011#ifdef FEAT_JOB_CHANNEL
7012 if (what == BS_START && bt_prompt(curbuf))
7013 return FALSE;
7014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 switch (*p_bs)
7016 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007017 case '3': return TRUE;
7018 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 case '1': return (what != BS_START);
7020 case '0': return FALSE;
7021 }
7022 return vim_strchr(p_bs, what) != NULL;
7023}
7024
7025/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007026 * Return the effective 'scrolloff' value for the current window, using the
7027 * global value when appropriate.
7028 */
7029 long
7030get_scrolloff_value(void)
7031{
7032 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7033}
7034
7035/*
7036 * Return the effective 'sidescrolloff' value for the current window, using the
7037 * global value when appropriate.
7038 */
7039 long
7040get_sidescrolloff_value(void)
7041{
7042 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7043}
7044
7045/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007046 * Get the local or global value of 'backupcopy'.
7047 */
7048 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007049get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007050{
7051 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7052}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007053
Gary Johnson53ba05b2021-07-26 22:19:10 +02007054/*
7055 * Get the local or global value of the 'virtualedit' flags.
7056 */
7057 unsigned int
7058get_ve_flags(void)
7059{
Gary Johnson51ad8502021-08-03 18:33:08 +02007060 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7061 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007062}
7063
Bram Moolenaaree857022019-11-09 23:26:40 +01007064#if defined(FEAT_LINEBREAK) || defined(PROTO)
7065/*
7066 * Get the local or global value of 'showbreak'.
7067 */
7068 char_u *
7069get_showbreak_value(win_T *win)
7070{
7071 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7072 return p_sbr;
7073 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7074 return empty_option;
7075 return win->w_p_sbr;
7076}
7077#endif
7078
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007079#if defined(FEAT_EVAL) || defined(PROTO)
7080/*
7081 * Get window or buffer local options.
7082 */
7083 dict_T *
7084get_winbuf_options(int bufopt)
7085{
7086 dict_T *d;
7087 int opt_idx;
7088
7089 d = dict_alloc();
7090 if (d == NULL)
7091 return NULL;
7092
Bram Moolenaardac13472019-09-16 21:06:21 +02007093 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007094 {
7095 struct vimoption *opt = &options[opt_idx];
7096
7097 if ((bufopt && (opt->indir & PV_BUF))
7098 || (!bufopt && (opt->indir & PV_WIN)))
7099 {
7100 char_u *varp = get_varp(opt);
7101
7102 if (varp != NULL)
7103 {
7104 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007105 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007106 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007107 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007108 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007109 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007110 }
7111 }
7112 }
7113
7114 return d;
7115}
7116#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007117
Bram Moolenaardac13472019-09-16 21:06:21 +02007118#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007119/*
7120 * This is called when 'culopt' is changed
7121 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007122 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007123fill_culopt_flags(char_u *val, win_T *wp)
7124{
7125 char_u *p;
7126 char_u culopt_flags_new = 0;
7127
7128 if (val == NULL)
7129 p = wp->w_p_culopt;
7130 else
7131 p = val;
7132 while (*p != NUL)
7133 {
7134 if (STRNCMP(p, "line", 4) == 0)
7135 {
7136 p += 4;
7137 culopt_flags_new |= CULOPT_LINE;
7138 }
7139 else if (STRNCMP(p, "both", 4) == 0)
7140 {
7141 p += 4;
7142 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7143 }
7144 else if (STRNCMP(p, "number", 6) == 0)
7145 {
7146 p += 6;
7147 culopt_flags_new |= CULOPT_NBR;
7148 }
7149 else if (STRNCMP(p, "screenline", 10) == 0)
7150 {
7151 p += 10;
7152 culopt_flags_new |= CULOPT_SCRLINE;
7153 }
7154
7155 if (*p != ',' && *p != NUL)
7156 return FAIL;
7157 if (*p == ',')
7158 ++p;
7159 }
7160
7161 // Can't have both "line" and "screenline".
7162 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7163 return FAIL;
7164 wp->w_p_culopt_flags = culopt_flags_new;
7165
7166 return OK;
7167}
7168#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007169
7170/*
7171 * Get the value of 'magic' adjusted for Vim9 script.
7172 */
7173 int
7174magic_isset(void)
7175{
7176 switch (magic_overruled)
7177 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007178 case OPTION_MAGIC_ON: return TRUE;
7179 case OPTION_MAGIC_OFF: return FALSE;
7180 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007181 }
7182#ifdef FEAT_EVAL
7183 if (in_vim9script())
7184 return TRUE;
7185#endif
7186 return p_magic;
7187}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007188
7189/*
7190 * Set the callback function value for an option that accepts a function name,
7191 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7192 * Returns OK if the option is successfully set to a function, otherwise
7193 * returns FAIL.
7194 */
7195 int
7196option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7197{
7198#ifdef FEAT_EVAL
7199 typval_T *tv;
7200 callback_T cb;
7201
7202 if (optval == NULL || *optval == NUL)
7203 {
7204 free_callback(optcb);
7205 return OK;
7206 }
7207
7208 if (*optval == '{'
7209 || (STRNCMP(optval, "function(", 9) == 0)
7210 || (STRNCMP(optval, "funcref(", 8) == 0))
7211 // Lambda expression or a funcref
7212 tv = eval_expr(optval, NULL);
7213 else
7214 // treat everything else as a function name string
7215 tv = alloc_string_tv(vim_strsave(optval));
7216 if (tv == NULL)
7217 return FAIL;
7218
7219 cb = get_callback(tv);
7220 if (cb.cb_name == NULL)
7221 {
7222 free_tv(tv);
7223 return FAIL;
7224 }
7225
7226 free_callback(optcb);
7227 set_callback(optcb, &cb);
7228 free_tv(tv);
7229 return OK;
7230#else
7231 return FAIL;
7232#endif
7233}