blob: 4422634e2ad0403bd7fb2f000ecb1e3e2e527c78 [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();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000813 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000814}
815#endif
816
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818/*
819 * Initialize the options, part two: After getting Rows and Columns and
820 * setting 'term'.
821 */
822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000825 int idx;
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100828 * 'scroll' defaults to half the window height. The stored default is zero,
829 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000832 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000833 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 comp_col();
835
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000836 /*
837 * 'window' is only for backwards compatibility with Vi.
838 * Default is Rows - 1.
839 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000840 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000841 p_window = Rows - 1;
842 set_number_default("window", Rows - 1);
843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100845#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000846 /*
847 * If 'background' wasn't set by the user, try guessing the value,
848 * depending on the terminal name. Only need to check for terminals
849 * with a dark background, that can handle color.
850 */
851 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000852 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
853 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000855 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 // don't mark it as set, when starting the GUI it may be
857 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000858 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000861
862#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000864#endif
865#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100866 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000867#endif
868#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871}
872
873/*
874 * Initialize the options, part three: After reading the .vimrc
875 */
876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100879#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880/*
881 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
882 * This is done after other initializations, where 'shell' might have been
883 * set, but only if they have not been set before.
884 */
885 char_u *p;
886 int idx_srr;
887 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100888# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 int idx_sp;
890 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000894 if (idx_srr < 0)
895 do_srr = FALSE;
896 else
897 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100898# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000900 if (idx_sp < 0)
901 do_sp = FALSE;
902 else
903 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100904# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200905 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (p != NULL)
907 {
908 /*
909 * Default for p_sp is "| tee", for p_srr is ">".
910 * For known shells it is changed here to include stderr.
911 */
912 if ( fnamecmp(p, "csh") == 0
913 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100914# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 || fnamecmp(p, "csh.exe") == 0
916 || fnamecmp(p, "tcsh.exe") == 0
917# endif
918 )
919 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100920# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (do_sp)
922 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100923# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
929 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (do_srr)
932 {
933 p_srr = (char_u *)">&";
934 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
935 }
936 }
Mike Williams12795022021-06-28 20:53:58 +0200937# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200938 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
939 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200940 else if ( fnamecmp(p, "powershell") == 0
941 || fnamecmp(p, "powershell.exe") == 0
942 )
943 {
944# if defined(FEAT_QUICKFIX)
945 if (do_sp)
946 {
947 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
948 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
949 }
950# endif
951 if (do_srr)
952 {
953 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
954 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
955 }
956 }
957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 else
Natanael Copa56318362021-05-06 18:46:35 +0200959 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if ( fnamecmp(p, "sh") == 0
961 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200962 || fnamecmp(p, "mksh") == 0
963 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000965 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200967 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200968 || fnamecmp(p, "ash") == 0
969 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200970 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100971# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 || fnamecmp(p, "cmd") == 0
973 || fnamecmp(p, "sh.exe") == 0
974 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200975 || fnamecmp(p, "mksh.exe") == 0
976 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000978 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 || fnamecmp(p, "bash.exe") == 0
980 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200981 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200982 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100986# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (do_sp)
988 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100989# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200992 if (fnamecmp(p, "pwsh") == 0)
993 p_sp = (char_u *)">%s 2>&1";
994 else
995 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
998 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (do_srr)
1001 {
1002 p_srr = (char_u *)">%s 2>&1";
1003 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1004 }
1005 }
1006 vim_free(p);
1007 }
1008#endif
1009
Bram Moolenaar4f974752019-02-17 17:44:42 +01001010#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001012 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1013 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001015 * set, but only if they have not been set before.
1016 * Default values depend on shell (cmd.exe is default shell):
1017 *
1018 * p_shcf p_sxq
1019 * cmd.exe - "/c" "("
1020 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001021 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001022 * "sh" like shells - "-c" "\""
1023 *
1024 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 */
Mike Williams12795022021-06-28 20:53:58 +02001026 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1027 {
1028 int idx_opt;
1029
1030 idx_opt = findoption((char_u *)"shcf");
1031 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1032 {
1033 p_shcf = (char_u*)"-Command";
1034 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1035 }
1036
1037 idx_opt = findoption((char_u *)"sxq");
1038 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1039 {
1040 p_sxq = (char_u*)"\"";
1041 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1042 }
1043 }
1044 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 int idx3;
1047
1048 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001049 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 p_shcf = (char_u *)"-c";
1052 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1053 }
1054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001055 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001057 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
1059 p_sxq = (char_u *)"\"";
1060 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001063 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1064 {
1065 int idx3;
1066
1067 /*
1068 * cmd.exe on Windows will strip the first and last double quote given
1069 * on the command line, e.g. most of the time things like:
1070 * cmd /c "my path/to/echo" "my args to echo"
1071 * become:
1072 * my path/to/echo" "my args to echo
1073 * when executed.
1074 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001075 * To avoid this, set shellxquote to surround the command in
1076 * parenthesis. This appears to make most commands work, without
1077 * breaking commands that worked previously, such as
1078 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001079 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001080 idx3 = findoption((char_u *)"sxq");
1081 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1082 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001083 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001084 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1085 }
1086
Bram Moolenaara64ba222012-02-12 23:23:31 +01001087 idx3 = findoption((char_u *)"shcf");
1088 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1089 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001090 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001091 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1092 }
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094#endif
1095
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001096 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001097 {
1098 int idx_ffs = findoption((char_u *)"ffs");
1099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001101 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1102 set_fileformat(default_fileformat(), OPT_LOCAL);
1103 }
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105#ifdef FEAT_TITLE
1106 set_title_defaults();
1107#endif
1108}
1109
1110#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1111/*
1112 * When 'helplang' is still at its default value, set it to "lang".
1113 * Only the first two characters of "lang" are used.
1114 */
1115 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001116set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 int idx;
1119
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001120 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 return;
1122 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001123 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
1125 if (options[idx].flags & P_ALLOCED)
1126 free_string_option(p_hlg);
1127 p_hlg = vim_strsave(lang);
1128 if (p_hlg == NULL)
1129 p_hlg = empty_option;
1130 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001131 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001132 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001133 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1134 {
1135 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1136 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1137 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001138 // any C like setting, such as C.UTF-8, becomes "en"
1139 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1140 {
1141 p_hlg[0] = 'e';
1142 p_hlg[1] = 'n';
1143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 options[idx].flags |= P_ALLOCED;
1147 }
1148}
1149#endif
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151#ifdef FEAT_TITLE
1152/*
1153 * 'title' and 'icon' only default to true if they have not been set or reset
1154 * in .vimrc and we can read the old value.
1155 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1156 * they can be reset. This reduces startup time when using X on a remote
1157 * machine.
1158 */
1159 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001160set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161{
1162 int idx1;
1163 long val;
1164
1165 /*
1166 * If GUI is (going to be) used, we can always set the window title and
1167 * icon name. Saves a bit of time, because the X11 display server does
1168 * not need to be contacted.
1169 */
1170 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001171 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173#ifdef FEAT_GUI
1174 if (gui.starting || gui.in_use)
1175 val = TRUE;
1176 else
1177#endif
1178 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001179 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 p_title = val;
1181 }
1182 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001183 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
1185#ifdef FEAT_GUI
1186 if (gui.starting || gui.in_use)
1187 val = TRUE;
1188 else
1189#endif
1190 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001191 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 p_icon = val;
1193 }
1194}
1195#endif
1196
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001197 void
1198ex_set(exarg_T *eap)
1199{
1200 int flags = 0;
1201
1202 if (eap->cmdidx == CMD_setlocal)
1203 flags = OPT_LOCAL;
1204 else if (eap->cmdidx == CMD_setglobal)
1205 flags = OPT_GLOBAL;
1206#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001207 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001208 ex_options(eap);
1209 else
1210#endif
1211 {
1212 if (eap->forceit)
1213 flags |= OPT_ONECOLUMN;
1214 (void)do_set(eap->arg, flags);
1215 }
1216}
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218/*
1219 * Parse 'arg' for option settings.
1220 *
1221 * 'arg' may be IObuff, but only when no errors can be present and option
1222 * does not need to be expanded with option_expand().
1223 * "opt_flags":
1224 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001225 * OPT_GLOBAL for ":setglobal"
1226 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001228 * OPT_WINONLY to only set window-local options
1229 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 *
1231 * returns FAIL if an error is detected, OK otherwise
1232 */
1233 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001234do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001235 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001236 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001238 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001240 char *errmsg;
1241 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001243 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1244 int nextchar; // next non-white char after option name
1245 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int len;
1247 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001248 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 long_u flags; // flags for current option
1251 char_u *varp = NULL; // pointer to variable for current option
1252 int did_show = FALSE; // already showed one value
1253 int adding; // "opt+=arg"
1254 int prepending; // "opt^=arg"
1255 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 int cp_val = 0;
1257 char_u key_name[2];
1258
1259 if (*arg == NUL)
1260 {
1261 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001262 did_show = TRUE;
1263 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001266 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 {
1268 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001269 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001271 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1272 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
1274 /*
1275 * ":set all" show all options.
1276 * ":set all&" set all options to their default value.
1277 */
1278 arg += 3;
1279 if (*arg == '&')
1280 {
1281 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001282 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001284 didset_options();
1285 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001286 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001291 did_show = TRUE;
1292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001294 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 {
1296 showoptions(2, opt_flags);
1297 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001298 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 arg += 7;
1300 }
1301 else
1302 {
1303 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001304 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
1306 prefix = 0;
1307 arg += 2;
1308 }
1309 else if (STRNCMP(arg, "inv", 3) == 0)
1310 {
1311 prefix = 2;
1312 arg += 3;
1313 }
1314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001315 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 key = 0;
1317 if (*arg == '<')
1318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1322 len = 5;
1323 else
1324 {
1325 len = 1;
1326 while (arg[len] != NUL && arg[len] != '>')
1327 ++len;
1328 }
1329 if (arg[len] != '>')
1330 {
1331 errmsg = e_invarg;
1332 goto skip;
1333 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001334 arg[len] = NUL; // put NUL after name
1335 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001337 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001339 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
1341 else
1342 {
1343 len = 0;
1344 /*
1345 * The two characters after "t_" may not be alphanumeric.
1346 */
1347 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1348 len = 4;
1349 else
1350 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1351 ++len;
1352 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001353 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001355 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001357 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001360 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 afterchar = arg[len];
1362
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001363 if (in_vim9script())
1364 {
1365 char_u *p = skipwhite(arg + len);
1366
1367 // disallow white space before =val, +=val, -=val, ^=val
1368 if (p > arg + len && (p[0] == '='
1369 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1370 && p[1] == '=')))
1371 {
1372 errmsg = e_no_white_space_allowed_between_option_and;
1373 arg = p;
1374 startarg = p;
1375 goto skip;
1376 }
1377 }
1378 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001379 // skip white space, allow ":set ai ?", ":set hlsearch !"
1380 while (VIM_ISWHITE(arg[len]))
1381 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
1383 adding = FALSE;
1384 prepending = FALSE;
1385 removing = FALSE;
1386 if (arg[len] != NUL && arg[len + 1] == '=')
1387 {
1388 if (arg[len] == '+')
1389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 ++len;
1392 }
1393 else if (arg[len] == '^')
1394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001395 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ++len;
1397 }
1398 else if (arg[len] == '-')
1399 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001400 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 ++len;
1402 }
1403 }
1404 nextchar = arg[len];
1405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001408 if (in_vim9script() && arg > arg_start
1409 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1410 errmsg = e_no_white_space_allowed_between_option_and;
1411 else
1412 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 goto skip;
1414 }
1415
1416 if (opt_idx >= 0)
1417 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001418 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001420 // Only give an error message when requesting the value of
1421 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1423 && (!(options[opt_idx].flags & P_BOOL)
1424 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001425 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 goto skip;
1427 }
1428
1429 flags = options[opt_idx].flags;
1430 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1431 }
1432 else
1433 {
1434 flags = P_STRING;
1435 if (key < 0)
1436 {
1437 key_name[0] = KEY2TERMCAP0(key);
1438 key_name[1] = KEY2TERMCAP1(key);
1439 }
1440 else
1441 {
1442 key_name[0] = KS_KEY;
1443 key_name[1] = (key & 0xff);
1444 }
1445 }
1446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001447 // Skip all options that are not window-local (used when showing
1448 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001449 if ((opt_flags & OPT_WINONLY)
1450 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1451 goto skip;
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001454 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1455 && options[opt_idx].var == VAR_WIN)
1456 goto skip;
1457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001458 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001459 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001461 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001462 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001463 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001464 goto skip;
1465 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001466 if ((flags & P_MLE) && !p_mle)
1467 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001468 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001469 goto skip;
1470 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001471#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001472 // In diff mode some options are overruled. This avoids that
1473 // 'foldmethod' becomes "marker" instead of "diff" and that
1474 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001475 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001477 && (
1478#ifdef FEAT_FOLDING
1479 options[opt_idx].indir == PV_FDM ||
1480#endif
1481 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001482 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485
1486#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001487 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001488 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001490 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 goto skip;
1492 }
1493#endif
1494
1495 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1496 {
1497 arg += len;
1498 cp_val = p_cp;
1499 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1500 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001501 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 cp_val = FALSE;
1504 arg += 3;
1505 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001506 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 cp_val = TRUE;
1509 arg += 2;
1510 }
1511 }
1512 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001513 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
1515 errmsg = e_trailing;
1516 goto skip;
1517 }
1518 }
1519
1520 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001521 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001522 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 */
1524 if (nextchar == '?'
1525 || (prefix == 1
1526 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1527 && !(flags & P_BOOL)))
1528 {
1529 /*
1530 * print value
1531 */
1532 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001533 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 else
1535 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001536 gotocmdline(TRUE); // cursor at status line
1537 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
1539 if (opt_idx >= 0)
1540 {
1541 showoneopt(&options[opt_idx], opt_flags);
1542#ifdef FEAT_EVAL
1543 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001544 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001545 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001546 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001547 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001548 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001549 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001550 (int)options[opt_idx].indir & PV_MASK]);
1551 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001552 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001553 (int)options[opt_idx].indir & PV_MASK]);
1554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#endif
1556 }
1557 else
1558 {
1559 char_u *p;
1560
1561 p = find_termcode(key_name);
1562 if (p == NULL)
1563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001564 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 goto skip;
1566 }
1567 else
1568 (void)show_one_termcode(key_name, p, TRUE);
1569 }
1570 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001571 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 errmsg = e_trailing;
1573 }
1574 else
1575 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001576 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001577 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001578
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001579 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
1581 if (nextchar == '=' || nextchar == ':')
1582 {
1583 errmsg = e_invarg;
1584 goto skip;
1585 }
1586
1587 /*
1588 * ":set opt!": invert
1589 * ":set opt&": reset to default value
1590 * ":set opt<": reset to global value
1591 */
1592 if (nextchar == '!')
1593 value = *(int *)(varp) ^ 1;
1594 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001595 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 ((flags & P_VI_DEF) || cp_val)
1597 ? VI_DEFAULT : VIM_DEFAULT];
1598 else if (nextchar == '<')
1599 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001600 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if ((int *)varp == &curbuf->b_p_ar
1602 && opt_flags == OPT_LOCAL)
1603 value = -1;
1604 else
1605 value = *(int *)get_varp_scope(&(options[opt_idx]),
1606 OPT_GLOBAL);
1607 }
1608 else
1609 {
1610 /*
1611 * ":set invopt": invert
1612 * ":set opt" or ":set noopt": set or reset
1613 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001614 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 errmsg = e_trailing;
1617 goto skip;
1618 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001619 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 value = *(int *)(varp) ^ 1;
1621 else
1622 value = prefix;
1623 }
1624
1625 errmsg = set_bool_option(opt_idx, varp, (int)value,
1626 opt_flags);
1627 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001628 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 {
1630 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1631 || prefix != 1)
1632 {
1633 errmsg = e_invarg;
1634 goto skip;
1635 }
1636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001637 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
1639 /*
1640 * Different ways to set a number option:
1641 * & set to default value
1642 * < set to global value
1643 * <xx> accept special key codes for 'wildchar'
1644 * c accept any non-digit for 'wildchar'
1645 * [-]0-9 set number
1646 * other error
1647 */
1648 ++arg;
1649 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001650 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 ((flags & P_VI_DEF) || cp_val)
1652 ? VI_DEFAULT : VIM_DEFAULT];
1653 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001654 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001655 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1656 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001657 if ((long *)varp == &curbuf->b_p_ul
1658 && opt_flags == OPT_LOCAL)
1659 value = NO_LOCAL_UNDOLEVEL;
1660 else
1661 value = *(long *)get_varp_scope(
1662 &(options[opt_idx]), OPT_GLOBAL);
1663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 else if (((long *)varp == &p_wc
1665 || (long *)varp == &p_wcm)
1666 && (*arg == '<'
1667 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001668 || (*arg != NUL
1669 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 && !VIM_ISDIGIT(*arg))))
1671 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001672 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (value == 0 && (long *)varp != &p_wcm)
1674 {
1675 errmsg = e_invarg;
1676 goto skip;
1677 }
1678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001681 // Allow negative (for 'undolevels'), octal and
1682 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001683 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001684 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001685 if (i == 0 || (arg[i] != NUL
1686 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001688 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 goto skip;
1690 }
1691 }
1692 else
1693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001694 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 goto skip;
1696 }
1697
1698 if (adding)
1699 value = *(long *)varp + value;
1700 if (prepending)
1701 value = *(long *)varp * value;
1702 if (removing)
1703 value = *(long *)varp - value;
1704 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001705 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001707 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001709 char_u *save_arg = NULL;
1710 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001711 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001712 char_u *newval;
1713 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001714 char_u *origval_l = NULL;
1715 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001716#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001717 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001718 char_u *saved_origval_l = NULL;
1719 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001720 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001721#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001722 unsigned newlen;
1723 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001724 int new_value_alloced; // new string option
1725 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001727 // When using ":set opt=val" for a global option
1728 // with a local value the local value will be
1729 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001731 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 varp = options[opt_idx].var;
1733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001734 // The old value is kept until we are sure that the
1735 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001737
Bram Moolenaard7c96872019-06-15 17:12:48 +02001738 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1739 {
1740 origval_l = *(char_u **)get_varp_scope(
1741 &(options[opt_idx]), OPT_LOCAL);
1742 origval_g = *(char_u **)get_varp_scope(
1743 &(options[opt_idx]), OPT_GLOBAL);
1744
1745 // A global-local string option might have an empty
1746 // option as value to indicate that the global
1747 // value should be used.
1748 if (((int)options[opt_idx].indir & PV_BOTH)
1749 && origval_l == empty_option)
1750 origval_l = origval_g;
1751 }
1752
1753 // When setting the local value of a global
1754 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001755 if (((int)options[opt_idx].indir & PV_BOTH)
1756 && (opt_flags & OPT_LOCAL))
1757 origval = *(char_u **)get_varp(
1758 &options[opt_idx]);
1759 else
1760 origval = oldval;
1761
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001762 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 {
1764 newval = options[opt_idx].def_val[
1765 ((flags & P_VI_DEF) || cp_val)
1766 ? VI_DEFAULT : VIM_DEFAULT];
1767 if ((char_u **)varp == &p_bg)
1768 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001769 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#ifdef FEAT_GUI
1771 if (gui.in_use)
1772 newval = gui_bg_default();
1773 else
1774#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001775 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001777 else if ((char_u **)varp == &p_fencs && enc_utf8)
1778 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001780 // expand environment variables and ~ (since the
1781 // default value was already expanded, only
1782 // required when an environment variable was set
1783 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (newval == NULL)
1785 newval = empty_option;
1786 else
1787 {
1788 s = option_expand(opt_idx, newval);
1789 if (s == NULL)
1790 s = newval;
1791 newval = vim_strsave(s);
1792 }
1793 new_value_alloced = TRUE;
1794 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001795 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
1797 newval = vim_strsave(*(char_u **)get_varp_scope(
1798 &(options[opt_idx]), OPT_GLOBAL));
1799 new_value_alloced = TRUE;
1800 }
1801 else
1802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001803 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
1805 /*
1806 * Set 'keywordprg' to ":help" if an empty
1807 * value was passed to :set by the user.
1808 * Misuse errbuf[] for the resulting string.
1809 */
1810 if (varp == (char_u *)&p_kp
1811 && (*arg == NUL || *arg == ' '))
1812 {
1813 STRCPY(errbuf, ":help");
1814 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001815 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
1817 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001818 * Convert 'backspace' number to string, for
1819 * adding, prepending and removing string.
1820 */
1821 else if (varp == (char_u *)&p_bs
1822 && VIM_ISDIGIT(**(char_u **)varp))
1823 {
1824 i = getdigits((char_u **)varp);
1825 switch (i)
1826 {
1827 case 0:
1828 *(char_u **)varp = empty_option;
1829 break;
1830 case 1:
1831 *(char_u **)varp = vim_strsave(
1832 (char_u *)"indent,eol");
1833 break;
1834 case 2:
1835 *(char_u **)varp = vim_strsave(
1836 (char_u *)"indent,eol,start");
1837 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001838 case 3:
1839 *(char_u **)varp = vim_strsave(
1840 (char_u *)"indent,eol,nostop");
1841 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001842 }
1843 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001844 if (origval == oldval)
1845 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001846 if (origval_l == oldval)
1847 origval_l = *(char_u **)varp;
1848 if (origval_g == oldval)
1849 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001850 oldval = *(char_u **)varp;
1851 }
1852 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 * Convert 'whichwrap' number to string, for
1854 * backwards compatibility with Vim 3.0.
1855 * Misuse errbuf[] for the resulting string.
1856 */
1857 else if (varp == (char_u *)&p_ww
1858 && VIM_ISDIGIT(*arg))
1859 {
1860 *errbuf = NUL;
1861 i = getdigits(&arg);
1862 if (i & 1)
1863 STRCAT(errbuf, "b,");
1864 if (i & 2)
1865 STRCAT(errbuf, "s,");
1866 if (i & 4)
1867 STRCAT(errbuf, "h,l,");
1868 if (i & 8)
1869 STRCAT(errbuf, "<,>,");
1870 if (i & 16)
1871 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001872 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 errbuf[STRLEN(errbuf) - 1] = NUL;
1874 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001875 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 /*
1878 * Remove '>' before 'dir' and 'bdir', for
1879 * backwards compatibility with version 3.0
1880 */
1881 else if ( *arg == '>'
1882 && (varp == (char_u *)&p_dir
1883 || varp == (char_u *)&p_bdir))
1884 {
1885 ++arg;
1886 }
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 /*
1889 * Copy the new string into allocated memory.
1890 * Can't use set_string_option_direct(), because
1891 * we need to remove the backslashes.
1892 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 newlen = (unsigned)STRLEN(arg) + 1;
1895 if (adding || prepending || removing)
1896 newlen += (unsigned)STRLEN(origval) + 1;
1897 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001898 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 break;
1900 s = newval;
1901
1902 /*
1903 * Copy the string, skip over escaped chars.
1904 * For MS-DOS and WIN32 backslashes before normal
1905 * file name characters are not removed, and keep
1906 * backslash at start, for "\\machine\path", but
1907 * do remove it for "\\\\machine\\path".
1908 * The reverse is found in ExpandOldSetting().
1909 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001910 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 if (*arg == '\\' && arg[1] != NUL
1913#ifdef BACKSLASH_IN_FILENAME
1914 && !((flags & P_EXPAND)
1915 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001916 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 && (arg[1] != '\\'
1918 || (s == newval
1919 && arg[2] != '\\')))
1920#endif
1921 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001922 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001924 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001926 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 mch_memmove(s, arg, (size_t)i);
1928 arg += i;
1929 s += i;
1930 }
1931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 *s++ = *arg++;
1933 }
1934 *s = NUL;
1935
1936 /*
1937 * Expand environment variables and ~.
1938 * Don't do it when adding without inserting a
1939 * comma.
1940 */
1941 if (!(adding || prepending || removing)
1942 || (flags & P_COMMA))
1943 {
1944 s = option_expand(opt_idx, newval);
1945 if (s != NULL)
1946 {
1947 vim_free(newval);
1948 newlen = (unsigned)STRLEN(s) + 1;
1949 if (adding || prepending || removing)
1950 newlen += (unsigned)STRLEN(origval) + 1;
1951 newval = alloc(newlen);
1952 if (newval == NULL)
1953 break;
1954 STRCPY(newval, s);
1955 }
1956 }
1957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001958 // locate newval[] in origval[] when removing it
1959 // and when adding to avoid duplicates
1960 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if (removing || (flags & P_NODUP))
1962 {
1963 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001964 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001966 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001967 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 prepending = FALSE;
1970 adding = FALSE;
1971 STRCPY(newval, origval);
1972 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001973
1974 // if no duplicate, move pointer to end of
1975 // original value
1976 if (s == NULL)
1977 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001980 // concatenate the two strings; add a ',' if
1981 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (adding || prepending)
1983 {
1984 comma = ((flags & P_COMMA) && *origval != NUL
1985 && *newval != NUL);
1986 if (adding)
1987 {
1988 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001989 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001990 if (comma && i > 1
1991 && (flags & P_ONECOMMA) == P_ONECOMMA
1992 && origval[i - 1] == ','
1993 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001994 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 mch_memmove(newval + i + comma, newval,
1996 STRLEN(newval) + 1);
1997 mch_memmove(newval, origval, (size_t)i);
1998 }
1999 else
2000 {
2001 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002002 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 if (comma)
2005 newval[i] = ',';
2006 }
2007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002008 // Remove newval[] from origval[]. (Note: "i" has
2009 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (removing)
2011 {
2012 STRCPY(newval, origval);
2013 if (*s)
2014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002015 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (flags & P_COMMA)
2017 {
2018 if (s == origval)
2019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002020 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 if (s[i] == ',')
2022 ++i;
2023 }
2024 else
2025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002026 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 --s;
2028 ++i;
2029 }
2030 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002031 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033 }
2034
2035 if (flags & P_FLAGLIST)
2036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002038 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002040 // if options have P_FLAGLIST and
2041 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002042 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002044 if (*s != ',' && *(s + 1) == ','
2045 && vim_strchr(s + 2, *s) != NULL)
2046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002047 // Remove the duplicated value and
2048 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002049 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002050 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002053 else
2054 {
2055 if ((!(flags & P_COMMA) || *s != ',')
2056 && vim_strchr(s + 1, *s) != NULL)
2057 {
2058 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002059 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002060 }
2061 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002062 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002066 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 arg = save_arg;
2068 new_value_alloced = TRUE;
2069 }
2070
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002071 /*
2072 * Set the new value.
2073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 *(char_u **)(varp) = newval;
2075
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002076#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002077 if (!starting
2078# ifdef FEAT_CRYPT
2079 && options[opt_idx].indir != PV_KEY
2080# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002081 && origval != NULL && newval != NULL)
2082 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002083 // origval may be freed by
2084 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002085 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002086 // newval (and varp) may become invalid if the
2087 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002088 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002089 if (origval_l != NULL)
2090 saved_origval_l = vim_strsave(origval_l);
2091 if (origval_g != NULL)
2092 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002093 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002094#endif
2095
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002096 {
2097 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002098 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002099
2100 // When an option is set in the sandbox, from a
2101 // modeline or in secure mode, then deal with side
2102 // effects in secure mode. Also when the value was
2103 // set with the P_INSECURE flag and is not
2104 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002105 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002106#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002107 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002108#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002109 || (!value_is_replaced && (*p & P_INSECURE)))
2110 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002111
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002112 // Handle side effects, and set the global value
2113 // for ":set" on local options. Note: when setting
2114 // 'syntax' or 'filetype' autocommands may be
2115 // triggered that can cause havoc.
2116 errmsg = did_set_string_option(
2117 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002118 new_value_alloced, oldval, errbuf,
2119 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002120
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002121 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002124#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002125 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002126 trigger_optionsset_string(
2127 opt_idx, opt_flags, saved_origval,
2128 saved_origval_l, saved_origval_g,
2129 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002130 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002131 vim_free(saved_origval_l);
2132 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002133 vim_free(saved_newval);
2134#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (errmsg != NULL)
2137 goto skip;
2138 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002139 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 char_u *p;
2142
2143 if (nextchar == '&')
2144 {
2145 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002146 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
2148 else
2149 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002150 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002151 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (*p == '\\' && p[1] != NUL)
2153 ++p;
2154 nextchar = *p;
2155 *p = NUL;
2156 add_termcode(key_name, arg, FALSE);
2157 *p = nextchar;
2158 }
2159 if (full_screen)
2160 ttest(FALSE);
2161 redraw_all_later(CLEAR);
2162 }
2163 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002166 did_set_option(
2167 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169
2170skip:
2171 /*
2172 * Advance to next argument.
2173 * - skip until a blank found, taking care of backslashes
2174 * - skip blanks
2175 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2176 */
2177 for (i = 0; i < 2 ; ++i)
2178 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002179 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (*arg++ == '\\' && *arg != NUL)
2181 ++arg;
2182 arg = skipwhite(arg);
2183 if (*arg != '=')
2184 break;
2185 }
2186 }
2187
2188 if (errmsg != NULL)
2189 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002190 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002191 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 if (i + (arg - startarg) < IOSIZE)
2193 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 STRCAT(IObuff, ": ");
2196 mch_memmove(IObuff + i, startarg, (arg - startarg));
2197 IObuff[i + (arg - startarg)] = NUL;
2198 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 trans_characters(IObuff, IOSIZE);
2201
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 ++no_wait_return; // wait_return done later
2203 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 --no_wait_return;
2205
2206 return FAIL;
2207 }
2208
2209 arg = skipwhite(arg);
2210 }
2211
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002212theend:
2213 if (silent_mode && did_show)
2214 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002215 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002216 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002219 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002220 out_flush();
2221 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002223 }
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 return OK;
2226}
2227
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002228/*
2229 * Call this when an option has been given a new value through a user command.
2230 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2231 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002232 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002233did_set_option(
2234 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002235 int opt_flags, // possibly with OPT_MODELINE
2236 int new_value, // value was replaced completely
2237 int value_checked) // value was checked to be safe, no need to set the
2238 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002239{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002240 long_u *p;
2241
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002242 options[opt_idx].flags |= P_WAS_SET;
2243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // When an option is set in the sandbox, from a modeline or in secure mode
2245 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2246 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002247 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002248 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002249#ifdef HAVE_SANDBOX
2250 || sandbox != 0
2251#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002252 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002253 *p = *p | P_INSECURE;
2254 else if (new_value)
2255 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002256}
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258/*
2259 * Convert a key name or string into a key value.
2260 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002261 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002263 int
2264string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002267 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (*arg == '^')
2269 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002270 if (multi_byte)
2271 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return *arg;
2273}
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#ifdef FEAT_TITLE
2276/*
2277 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2278 * maketitle() to create and display it.
2279 * When switching the title or icon off, call mch_restore_title() to get
2280 * the old value back.
2281 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002282 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002283did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 if (starting != NO_SCREEN
2286#ifdef FEAT_GUI
2287 && !gui.starting
2288#endif
2289 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291}
2292#endif
2293
2294/*
2295 * set_options_bin - called when 'bin' changes value.
2296 */
2297 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002298set_options_bin(
2299 int oldval,
2300 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002301 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 /*
2304 * The option values that are changed when 'bin' changes are
2305 * copied when 'bin is set and restored when 'bin' is reset.
2306 */
2307 if (newval)
2308 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002309 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 if (!(opt_flags & OPT_GLOBAL))
2312 {
2313 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2314 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2315 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2316 curbuf->b_p_et_nobin = curbuf->b_p_et;
2317 }
2318 if (!(opt_flags & OPT_LOCAL))
2319 {
2320 p_tw_nobin = p_tw;
2321 p_wm_nobin = p_wm;
2322 p_ml_nobin = p_ml;
2323 p_et_nobin = p_et;
2324 }
2325 }
2326
2327 if (!(opt_flags & OPT_GLOBAL))
2328 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002329 curbuf->b_p_tw = 0; // no automatic line wrap
2330 curbuf->b_p_wm = 0; // no automatic line wrap
2331 curbuf->b_p_ml = 0; // no modelines
2332 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334 if (!(opt_flags & OPT_LOCAL))
2335 {
2336 p_tw = 0;
2337 p_wm = 0;
2338 p_ml = FALSE;
2339 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 if (!(opt_flags & OPT_GLOBAL))
2346 {
2347 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2348 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2349 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2350 curbuf->b_p_et = curbuf->b_p_et_nobin;
2351 }
2352 if (!(opt_flags & OPT_LOCAL))
2353 {
2354 p_tw = p_tw_nobin;
2355 p_wm = p_wm_nobin;
2356 p_ml = p_ml_nobin;
2357 p_et = p_et_nobin;
2358 }
2359 }
2360}
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362/*
2363 * Expand environment variables for some string options.
2364 * These string options cannot be indirect!
2365 * If "val" is NULL expand the current value of the option.
2366 * Return pointer to NameBuff, or NULL when not expanded.
2367 */
2368 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002369option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2373 return NULL;
2374
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 // If val is longer than MAXPATHL no meaningful expansion can be done,
2376 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (val != NULL && STRLEN(val) > MAXPATHL)
2378 return NULL;
2379
2380 if (val == NULL)
2381 val = *(char_u **)options[opt_idx].var;
2382
2383 /*
2384 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2385 * Escape spaces when expanding 'tags', they are used to separate file
2386 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002387 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 */
2389 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002390 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002391#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002392 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2393#endif
2394 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 return NULL;
2397
2398 return NameBuff;
2399}
2400
2401/*
2402 * After setting various option values: recompute variables that depend on
2403 * option values.
2404 */
2405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002406didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002408 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 (void)init_chartab();
2410
Bram Moolenaardac13472019-09-16 21:06:21 +02002411 didset_string_options();
2412
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002413#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002414 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002415 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002416 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002417 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002420 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 (void)check_cedit();
2422#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002423#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002424 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002425 fill_breakat_flags();
2426#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002427 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002428}
2429
2430/*
2431 * More side effects of setting options.
2432 */
2433 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002434didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002435{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002436 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002437 (void)highlight_changed();
2438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002439 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002440 check_opt_wim();
2441
Bram Moolenaareed9d462021-02-15 20:38:25 +01002442 // Parse default for 'listchars'.
2443 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002446 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002447
2448#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002449 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002450 (void)check_clipboard_option();
2451#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002452#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002453 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002454 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002455 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002456 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458}
2459
2460/*
2461 * Check for string options that are NULL (normally only termcap options).
2462 */
2463 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002464check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 int opt_idx;
2467
2468 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2469 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2470 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2471}
2472
2473/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002474 * Return the option index found by a pointer into term_strings[].
2475 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002477 int
2478get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002480 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
2482 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2483 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002484 return opt_idx;
2485 return -1; // cannot happen: didn't find it!
2486}
2487
2488/*
2489 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2490 * Return the option index or -1 if not found.
2491 */
2492 int
2493set_term_option_alloced(char_u **p)
2494{
2495 int opt_idx = get_term_opt_idx(p);
2496
2497 if (opt_idx >= 0)
2498 options[opt_idx].flags |= P_ALLOCED;
2499 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002502#if defined(FEAT_EVAL) || defined(PROTO)
2503/*
2504 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2505 * Return FALSE when it wasn't.
2506 * Return -1 for an unknown option.
2507 */
2508 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002509was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002510{
2511 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002512 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002513
2514 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515 {
2516 flagp = insecure_flag(idx, opt_flags);
2517 return (*flagp & P_INSECURE) != 0;
2518 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002519 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002520 return -1;
2521}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002522
2523/*
2524 * Get a pointer to the flags used for the P_INSECURE flag of option
2525 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002526 * NOTE: Caller must make sure that "curwin" is set to the window from which
2527 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002528 */
2529 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002530insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002531{
2532 if (opt_flags & OPT_LOCAL)
2533 switch ((int)options[opt_idx].indir)
2534 {
2535#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002536 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002537#endif
2538#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002539# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002540 case PV_FDE: return &curwin->w_p_fde_flags;
2541 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002542# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002543# ifdef FEAT_BEVAL
2544 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2545# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002546# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002547 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002548# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002549 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002550# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002551 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002552# endif
2553#endif
2554 }
2555
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002556 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002557 return &options[opt_idx].flags;
2558}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002559#endif
2560
Bram Moolenaardac13472019-09-16 21:06:21 +02002561#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002562/*
2563 * Redraw the window title and/or tab page text later.
2564 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002565void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002566{
2567 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002568 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002569}
2570#endif
2571
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002573 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2574 * characters or characters in "allowed".
2575 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002576 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002577valid_name(char_u *val, char *allowed)
2578{
2579 char_u *s;
2580
2581 for (s = val; *s != NUL; ++s)
2582 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2583 return FALSE;
2584 return TRUE;
2585}
2586
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002587#if defined(FEAT_EVAL) || defined(PROTO)
2588/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002589 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002590 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002591 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002592 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002593set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002594{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002595 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2596 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002597 sctx_T new_script_ctx = script_ctx;
2598
Bram Moolenaar51258742020-05-03 17:19:33 +02002599 // Modeline already has the line number set.
2600 if (!(opt_flags & OPT_MODELINE))
2601 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002603 // Remember where the option was set. For local options need to do that
2604 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002605 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002606 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002607 if (both || (opt_flags & OPT_LOCAL))
2608 {
2609 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002610 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002611 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002612 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002613 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002614}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002615
2616/*
2617 * Set the script_ctx for a termcap option.
2618 * "name" must be the two character code, e.g. "RV".
2619 * When "name" is NULL use "opt_idx".
2620 */
2621 void
2622set_term_option_sctx_idx(char *name, int opt_idx)
2623{
2624 char_u buf[5];
2625 int idx;
2626
2627 if (name == NULL)
2628 idx = opt_idx;
2629 else
2630 {
2631 buf[0] = 't';
2632 buf[1] = '_';
2633 buf[2] = name[0];
2634 buf[3] = name[1];
2635 buf[4] = 0;
2636 idx = findoption(buf);
2637 }
2638 if (idx >= 0)
2639 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2640}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002641#endif
2642
Bram Moolenaarf4140482020-02-15 23:06:45 +01002643#if defined(FEAT_EVAL)
2644/*
2645 * Apply the OptionSet autocommand.
2646 */
2647 static void
2648apply_optionset_autocmd(
2649 int opt_idx,
2650 long opt_flags,
2651 long oldval,
2652 long oldval_g,
2653 long newval,
2654 char *errmsg)
2655{
2656 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2657
2658 // Don't do this while starting up, failure or recursively.
2659 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2660 return;
2661
2662 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2663 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2664 oldval_g);
2665 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2666 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2667 (opt_flags & OPT_LOCAL) ? "local" : "global");
2668 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2669 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2670 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2671 if (opt_flags & OPT_LOCAL)
2672 {
2673 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2674 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2675 }
2676 if (opt_flags & OPT_GLOBAL)
2677 {
2678 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2679 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2680 }
2681 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2682 {
2683 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2684 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2685 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2686 }
2687 if (opt_flags & OPT_MODELINE)
2688 {
2689 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2690 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2691 }
2692 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2693 NULL, FALSE, NULL);
2694 reset_v_option_vars();
2695}
2696#endif
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698/*
2699 * Set the value of a boolean option, and take care of side effects.
2700 * Returns NULL for success, or an error message for an error.
2701 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002702 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002703set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002704 int opt_idx, // index in options[] table
2705 char_u *varp, // pointer to the option variable
2706 int value, // new value
2707 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002710#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002711 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002714 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if ((secure
2716#ifdef HAVE_SANDBOX
2717 || sandbox != 0
2718#endif
2719 ) && (options[opt_idx].flags & P_SECURE))
2720 return e_secure;
2721
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002722#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002723 // Save the global value before changing anything. This is needed as for
2724 // a global-only option setting the "local value" in fact sets the global
2725 // value (since there is only one value).
2726 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2727 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2728 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002729#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002730
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002734 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#endif
2736
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002737#ifdef FEAT_GUI
2738 need_mouse_correct = TRUE;
2739#endif
2740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2743 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2744
2745 /*
2746 * Handle side effects of changing a bool option.
2747 */
2748
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaar920694c2016-08-21 17:45:02 +02002753#ifdef FEAT_LANGMAP
2754 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002756 p_lnr = !p_lrm;
2757 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002758 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002759 p_lrm = !p_lnr;
2760#endif
2761
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002762#ifdef FEAT_SYN_HL
2763 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2764 reset_cursorline();
2765#endif
2766
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002767#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002769 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2770 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // Only take action when the option was set. When reset we do not
2772 // delete the undo file, the option may be set again without making
2773 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002774 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002775 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002776 char_u hash[UNDO_HASH_SIZE];
2777 buf_T *save_curbuf = curbuf;
2778
Bram Moolenaar29323592016-07-24 22:04:11 +02002779 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002780 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // When 'undofile' is set globally: for every buffer, otherwise
2782 // only for the current buffer: Try to read in the undofile,
2783 // if one exists, the buffer wasn't changed and the buffer was
2784 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002785 if ((curbuf == save_curbuf
2786 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2787 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2788 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002789#ifdef FEAT_CRYPT
2790 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2791 continue;
2792#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002793 u_compute_hash(hash);
2794 u_read_undo(NULL, hash, curbuf->b_fname);
2795 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002796 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002797 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002798 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002799 }
2800#endif
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else if ((int *)varp == &curbuf->b_p_ro)
2803 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2806 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002807
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002808 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002809 if (curbuf->b_p_ro)
2810 curbuf->b_did_warn = FALSE;
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002813 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#endif
2815 }
2816
Bram Moolenaar9c449722010-07-20 18:44:27 +02002817#ifdef FEAT_GUI
2818 else if ((int *)varp == &p_mh)
2819 {
2820 if (!p_mh)
2821 gui_mch_mousehide(FALSE);
2822 }
2823#endif
2824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002827 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002828# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002830 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2831 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002832 {
2833 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002834 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002835 }
2836# endif
2837# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002838 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002839# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002840 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002841#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002844 {
2845 redraw_titles();
2846 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002848 else if ((int *)varp == &curbuf->b_p_fixeol)
2849 {
2850 redraw_titles();
2851 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002853 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002854 {
2855 redraw_titles();
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857#endif
2858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 else if ((int *)varp == &curbuf->b_p_bin)
2861 {
2862 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2863#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002864 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865#endif
2866 }
2867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2870 {
2871 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2872 NULL, NULL, TRUE, curbuf);
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 else if ((int *)varp == &curbuf->b_p_swf)
2877 {
2878 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2882 // buf->b_p_swf
2883 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002886 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 else if ((int *)varp == &p_terse)
2888 {
2889 char_u *p;
2890
2891 p = vim_strchr(p_shm, SHM_SEARCH);
2892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002893 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (p_terse && p == NULL)
2895 {
2896 STRCPY(IObuff, p_shm);
2897 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002898 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002902 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 else if ((int *)varp == &p_paste)
2907 {
2908 paste_option_changed();
2909 }
2910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 else if ((int *)varp == &p_im)
2913 {
2914 if (p_im)
2915 {
2916 if ((State & INSERT) == 0)
2917 need_start_insertmode = TRUE;
2918 stop_insert_mode = FALSE;
2919 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002921 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 {
2923 need_start_insertmode = FALSE;
2924 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 restart_edit = 0;
2928 }
2929 }
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 else if ((int *)varp == &p_ic && p_hls)
2933 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002934 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936
2937#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002938 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 else if ((int *)varp == &p_hls)
2940 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002941 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943#endif
2944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2946 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 else if ((int *)varp == &curwin->w_p_scb)
2948 {
2949 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002952 curwin->w_scbind_pos = curwin->w_topline;
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaar4033c552017-09-16 20:54:51 +02002956#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 else if ((int *)varp == &curwin->w_p_pvw)
2959 {
2960 if (curwin->w_p_pvw)
2961 {
2962 win_T *win;
2963
Bram Moolenaar29323592016-07-24 22:04:11 +02002964 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 if (win->w_p_pvw && win != curwin)
2966 {
2967 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002968 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
2970 }
2971 }
2972#endif
2973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 else if ((int *)varp == &curbuf->b_p_tx)
2976 {
2977 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2978 }
2979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002980 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 set_string_option_direct((char_u *)"ffs", -1,
2984 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002985 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987
2988 /*
2989 * When 'lisp' option changes include/exclude '-' in
2990 * keyword characters.
2991 */
2992#ifdef FEAT_LISP
2993 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997#endif
2998
2999#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003001 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003003 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005#endif
3006
3007 else if ((int *)varp == &curbuf->b_changed)
3008 {
3009 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003012 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016
3017#ifdef BACKSLASH_IN_FILENAME
3018 else if ((int *)varp == &p_ssl)
3019 {
3020 if (p_ssl)
3021 {
3022 psepc = '/';
3023 psepcN = '\\';
3024 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026 else
3027 {
3028 psepc = '\\';
3029 psepcN = '/';
3030 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
3032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 buflist_slash_adjust();
3035 alist_slash_adjust();
3036# ifdef FEAT_EVAL
3037 scriptnames_slash_adjust();
3038# endif
3039 }
3040#endif
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else if ((int *)varp == &curwin->w_p_wrap)
3044 {
3045 if (curwin->w_p_wrap)
3046 curwin->w_leftcol = 0;
3047 }
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 else if ((int *)varp == &p_ea)
3050 {
3051 if (p_ea && !old_value)
3052 win_equal(curwin, FALSE, 0);
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 else if ((int *)varp == &p_wiv)
3056 {
3057 /*
3058 * When 'weirdinvert' changed, set/reset 't_xs'.
3059 * Then set 'weirdinvert' according to value of 't_xs'.
3060 */
3061 if (p_wiv && !old_value)
3062 T_XS = (char_u *)"y";
3063 else if (!p_wiv && old_value)
3064 T_XS = empty_option;
3065 p_wiv = (*T_XS != NUL);
3066 }
3067
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003068#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 else if ((int *)varp == &p_beval)
3070 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003071 if (!balloonEvalForTerm)
3072 {
3073 if (p_beval && !old_value)
3074 gui_mch_enable_beval_area(balloonEval);
3075 else if (!p_beval && old_value)
3076 gui_mch_disable_beval_area(balloonEval);
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003079#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003080#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003081 else if ((int *)varp == &p_bevalterm)
3082 {
3083 mch_bevalterm_changed();
3084 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003087#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else if ((int *)varp == &p_acd)
3089 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003091 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
3093#endif
3094
3095#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else if ((int *)varp == &curwin->w_p_diff)
3098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003100 diff_buf_adjust(curwin);
3101# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (foldmethodIsDiff(curwin))
3103 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106#endif
3107
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003108#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else if ((int *)varp == &p_imdisable)
3111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003112 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (p_imdisable)
3114 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003115 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003116 // When the option is set from an autocommand, it may need to take
3117 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003118 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
3120#endif
3121
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003122#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003124 else if ((int *)varp == &curwin->w_p_spell)
3125 {
3126 if (curwin->w_p_spell)
3127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 char *errmsg = did_set_spelllang(curwin);
3129
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003130 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003131 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003132 }
3133 }
3134#endif
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136#ifdef FEAT_ARABIC
3137 if ((int *)varp == &curwin->w_p_arab)
3138 {
3139 if (curwin->w_p_arab)
3140 {
3141 /*
3142 * 'arabic' is set, handle various sub-settings.
3143 */
3144 if (!p_tbidi)
3145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (!curwin->w_p_rl)
3148 {
3149 curwin->w_p_rl = TRUE;
3150 changed_window_setting();
3151 }
3152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003153 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (!p_arshape)
3155 {
3156 p_arshape = TRUE;
3157 redraw_later_clear();
3158 }
3159 }
3160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 // Arabic requires a utf-8 encoding, inform the user if its not
3162 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003164 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003165 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3166
Bram Moolenaar8820b482017-03-16 17:23:31 +01003167 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003168 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003169#ifdef FEAT_EVAL
3170 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3171#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
3177# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3180 OPT_LOCAL);
3181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 else
3184 {
3185 /*
3186 * 'arabic' is reset, handle various sub-settings.
3187 */
3188 if (!p_tbidi)
3189 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (curwin->w_p_rl)
3192 {
3193 curwin->w_p_rl = FALSE;
3194 changed_window_setting();
3195 }
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // 'arabicshape' isn't reset, it is a global option and
3198 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003201 // 'delcombine' isn't reset, it is a global option and another
3202 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 curbuf->b_p_iminsert = B_IMODE_NONE;
3207 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3208# endif
3209 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003210 }
3211
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003212#endif
3213
Bram Moolenaar44826212019-08-22 21:23:20 +02003214#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3215 else if (((int *)varp == &curwin->w_p_nu
3216 || (int *)varp == &curwin->w_p_rnu)
3217 && gui.in_use
3218 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3219 && curbuf->b_signlist != NULL)
3220 {
3221 // If the 'number' or 'relativenumber' options are modified and
3222 // 'signcolumn' is set to 'number', then clear the screen for a full
3223 // refresh. Otherwise the sign icons are not displayed properly in the
3224 // number column. If the 'number' option is set and only the
3225 // 'relativenumber' option is toggled, then don't refresh the screen
3226 // (optimization).
3227 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3228 redraw_all_later(CLEAR);
3229 }
3230#endif
3231
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003232#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003234 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003235 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003236# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003237 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003238 if (
3239# ifdef VIMDLL
3240 !gui.in_use && !gui.starting &&
3241# endif
3242 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003243 {
3244 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003245 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003246 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003247 if (is_term_win32())
3248 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003249# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003250# ifdef FEAT_GUI
3251 if (!gui.in_use && !gui.starting)
3252# endif
3253 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003254# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003255 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003256 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003257 {
3258 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003259 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003260 init_highlight(TRUE, FALSE);
3261 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003262# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003263# ifdef FEAT_TERMINAL
3264 term_update_colors_all();
3265 term_update_wincolor_all();
3266# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003267 }
3268#endif
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 /*
3271 * End of handling side effects for bool options.
3272 */
3273
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003274 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 options[opt_idx].flags |= P_WAS_SET;
3277
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003278#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003279 apply_optionset_autocmd(opt_idx, opt_flags,
3280 (long)(old_value ? TRUE : FALSE),
3281 (long)(old_global_value ? TRUE : FALSE),
3282 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003283#endif
3284
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003285 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003286 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003287 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003288 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003289
3290 if ((opt_flags & OPT_NO_REDRAW) == 0)
3291 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 return NULL;
3294}
3295
3296/*
3297 * Set the value of a number option, and take care of side effects.
3298 * Returns NULL for success, or an error message for an error.
3299 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003300 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003301set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003302 int opt_idx, // index in options[] table
3303 char_u *varp, // pointer to the option variable
3304 long value, // new value
3305 char *errbuf, // buffer for error messages
3306 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003307 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3308 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003310 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003312#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003313 long old_global_value = 0; // only used when setting a local and
3314 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003315#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003316 long old_Rows = Rows; // remember old Rows
3317 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 long *pp = (long *)varp;
3319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003320 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003321 if ((secure
3322#ifdef HAVE_SANDBOX
3323 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003325 ) && (options[opt_idx].flags & P_SECURE))
3326 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003328#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003329 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003330 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003331 // value (since there is only one value).
3332 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003333 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3334 OPT_GLOBAL);
3335#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 *pp = value;
3338#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003340 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003342#ifdef FEAT_GUI
3343 need_mouse_correct = TRUE;
3344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaar14f24742012-08-08 18:01:05 +02003346 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003349#ifdef FEAT_VARTABS
3350 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3351 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3352 ? tabstop_first(curbuf->b_p_vts_array)
3353 : curbuf->b_p_ts;
3354#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358
3359 /*
3360 * Number options that need some action when changed
3361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (pp == &p_wh || pp == &p_hh)
3363 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003364 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (p_wh < 1)
3366 {
3367 errmsg = e_positive;
3368 p_wh = 1;
3369 }
3370 if (p_wmh > p_wh)
3371 {
3372 errmsg = e_winheight;
3373 p_wh = p_wmh;
3374 }
3375 if (p_hh < 0)
3376 {
3377 errmsg = e_positive;
3378 p_hh = 0;
3379 }
3380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003381 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003382 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 if (pp == &p_wh && curwin->w_height < p_wh)
3385 win_setheight((int)p_wh);
3386 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3387 win_setheight((int)p_hh);
3388 }
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 else if (pp == &p_wmh)
3391 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003392 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if (p_wmh < 0)
3394 {
3395 errmsg = e_positive;
3396 p_wmh = 0;
3397 }
3398 if (p_wmh > p_wh)
3399 {
3400 errmsg = e_winheight;
3401 p_wmh = p_wh;
3402 }
3403 win_setminheight();
3404 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003405 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003407 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (p_wiw < 1)
3409 {
3410 errmsg = e_positive;
3411 p_wiw = 1;
3412 }
3413 if (p_wmw > p_wiw)
3414 {
3415 errmsg = e_winwidth;
3416 p_wiw = p_wmw;
3417 }
3418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003420 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 win_setwidth((int)p_wiw);
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else if (pp == &p_wmw)
3424 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003425 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (p_wmw < 0)
3427 {
3428 errmsg = e_positive;
3429 p_wmw = 0;
3430 }
3431 if (p_wmw > p_wiw)
3432 {
3433 errmsg = e_winwidth;
3434 p_wmw = p_wiw;
3435 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003436 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 else if (pp == &p_ls)
3441 {
3442 last_status(FALSE);
3443 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003445 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003446 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451#ifdef FEAT_GUI
3452 else if (pp == &p_linespace)
3453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // Recompute gui.char_height and resize the Vim window to keep the
3455 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003456 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003457 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459#endif
3460
3461#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 else if (pp == &curwin->w_p_fdl)
3464 {
3465 if (curwin->w_p_fdl < 0)
3466 curwin->w_p_fdl = 0;
3467 newFoldLevel();
3468 }
3469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003470 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else if (pp == &curwin->w_p_fml)
3472 {
3473 foldUpdateAll(curwin);
3474 }
3475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003476 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else if (pp == &curwin->w_p_fdn)
3478 {
3479 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3480 foldUpdateAll(curwin);
3481 }
3482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003483 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else if (pp == &curwin->w_p_fdc)
3485 {
3486 if (curwin->w_p_fdc < 0)
3487 {
3488 errmsg = e_positive;
3489 curwin->w_p_fdc = 0;
3490 }
3491 else if (curwin->w_p_fdc > 12)
3492 {
3493 errmsg = e_invarg;
3494 curwin->w_p_fdc = 12;
3495 }
3496 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003497#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003499#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3502 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003503# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (foldmethodIsIndent(curwin))
3505 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003506# endif
3507# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003508 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3509 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003510 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3511 parse_cino(curbuf);
3512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003516 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003517 else if (pp == &p_mco)
3518 {
3519 if (p_mco > MAX_MCO)
3520 p_mco = MAX_MCO;
3521 else if (p_mco < 0)
3522 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003523 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003524 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 else if (pp == &curbuf->b_p_iminsert)
3527 {
3528 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3529 {
3530 errmsg = e_invarg;
3531 curbuf->b_p_iminsert = B_IMODE_NONE;
3532 }
3533 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003534 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003536#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003537 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 status_redraw_curbuf();
3539#endif
3540 }
3541
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003542#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003543 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003544 else if (pp == &p_imst)
3545 {
3546 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3547 errmsg = e_invarg;
3548 }
3549#endif
3550
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003551 else if (pp == &p_window)
3552 {
3553 if (p_window < 1)
3554 p_window = 1;
3555 else if (p_window >= Rows)
3556 p_window = Rows - 1;
3557 }
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 else if (pp == &curbuf->b_p_imsearch)
3560 {
3561 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3562 {
3563 errmsg = e_invarg;
3564 curbuf->b_p_imsearch = B_IMODE_NONE;
3565 }
3566 p_imsearch = curbuf->b_p_imsearch;
3567 }
3568
3569#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003570 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 else if (pp == &p_titlelen)
3572 {
3573 if (p_titlelen < 0)
3574 {
3575 errmsg = e_positive;
3576 p_titlelen = 85;
3577 }
3578 if (starting != NO_SCREEN && old_value != p_titlelen)
3579 need_maketitle = TRUE;
3580 }
3581#endif
3582
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 else if (pp == &p_ch)
3585 {
3586 if (p_ch < 1)
3587 {
3588 errmsg = e_positive;
3589 p_ch = 1;
3590 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003591 if (p_ch > Rows - min_rows() + 1)
3592 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 // Only compute the new window layout when startup has been
3595 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (p_ch != old_value && full_screen
3597#ifdef FEAT_GUI
3598 && !gui.starting
3599#endif
3600 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003601 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003604 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 else if (pp == &p_uc)
3606 {
3607 if (p_uc < 0)
3608 {
3609 errmsg = e_positive;
3610 p_uc = 100;
3611 }
3612 if (p_uc && !old_value)
3613 ml_open_files();
3614 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003615#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003616 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003618 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619 {
3620 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003621 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003622 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003623 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003624 {
3625 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003626 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003627 }
3628 }
3629#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003630#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003631 else if (pp == &p_mzq)
3632 mzvim_reset_timer();
3633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003635#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003636 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003637 else if (pp == &p_pyx)
3638 {
3639 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3640 errmsg = e_invarg;
3641 }
3642#endif
3643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003644 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 else if (pp == &p_ul)
3646 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003647 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003649 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 p_ul = value;
3651 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003652 else if (pp == &curbuf->b_p_ul)
3653 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003654 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003655 curbuf->b_p_ul = old_value;
3656 u_sync(TRUE);
3657 curbuf->b_p_ul = value;
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003660#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003661 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003662 else if (pp == &curwin->w_p_nuw)
3663 {
3664 if (curwin->w_p_nuw < 1)
3665 {
3666 errmsg = e_positive;
3667 curwin->w_p_nuw = 1;
3668 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003669 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003670 {
3671 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003672 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003673 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003674 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003675 }
3676#endif
3677
Bram Moolenaar1a384422010-07-14 19:53:30 +02003678 else if (pp == &curbuf->b_p_tw)
3679 {
3680 if (curbuf->b_p_tw < 0)
3681 {
3682 errmsg = e_positive;
3683 curbuf->b_p_tw = 0;
3684 }
3685#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003686 {
3687 win_T *wp;
3688 tabpage_T *tp;
3689
3690 FOR_ALL_TAB_WINDOWS(tp, wp)
3691 check_colorcolumn(wp);
3692 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003693#endif
3694 }
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 /*
3697 * Check the bounds for numeric options here
3698 */
3699 if (Rows < min_rows() && full_screen)
3700 {
3701 if (errbuf != NULL)
3702 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003703 vim_snprintf((char *)errbuf, errbuflen,
3704 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 errmsg = errbuf;
3706 }
3707 Rows = min_rows();
3708 }
3709 if (Columns < MIN_COLUMNS && full_screen)
3710 {
3711 if (errbuf != NULL)
3712 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003713 vim_snprintf((char *)errbuf, errbuflen,
3714 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 errmsg = errbuf;
3716 }
3717 Columns = MIN_COLUMNS;
3718 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003719 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 /*
3722 * If the screen (shell) height has been changed, assume it is the
3723 * physical screenheight.
3724 */
3725 if (old_Rows != Rows || old_Columns != Columns)
3726 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003727 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (updating_screen)
3729 *pp = old_value;
3730 else if (full_screen
3731#ifdef FEAT_GUI
3732 && !gui.starting
3733#endif
3734 )
3735 set_shellsize((int)Columns, (int)Rows, TRUE);
3736 else
3737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 // Postpone the resizing; check the size and cmdline position for
3739 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 check_shellsize();
3741 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3742 cmdline_row = Rows - p_ch;
3743 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003744 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003745 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (curbuf->b_p_ts <= 0)
3749 {
3750 errmsg = e_positive;
3751 curbuf->b_p_ts = 8;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (p_tm < 0)
3754 {
3755 errmsg = e_positive;
3756 p_tm = 0;
3757 }
3758 if ((curwin->w_p_scr <= 0
3759 || (curwin->w_p_scr > curwin->w_height
3760 && curwin->w_height > 0))
3761 && full_screen)
3762 {
3763 if (pp == &(curwin->w_p_scr))
3764 {
3765 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003766 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 win_comp_scroll(curwin);
3768 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003769 // If 'scroll' became invalid because of a side effect silently adjust
3770 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 else if (curwin->w_p_scr <= 0)
3772 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003773 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 curwin->w_p_scr = curwin->w_height;
3775 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003776 if (p_hi < 0)
3777 {
3778 errmsg = e_positive;
3779 p_hi = 0;
3780 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003781 else if (p_hi > 10000)
3782 {
3783 errmsg = e_invarg;
3784 p_hi = 10000;
3785 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003786 if (p_re < 0 || p_re > 2)
3787 {
3788 errmsg = e_invarg;
3789 p_re = 0;
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (p_report < 0)
3792 {
3793 errmsg = e_positive;
3794 p_report = 1;
3795 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003796 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003798 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 p_sj = Rows / 2;
3800 else
3801 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003802 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 p_sj = 1;
3804 }
3805 }
3806 if (p_so < 0 && full_screen)
3807 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003808 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 p_so = 0;
3810 }
3811 if (p_siso < 0 && full_screen)
3812 {
3813 errmsg = e_positive;
3814 p_siso = 0;
3815 }
3816#ifdef FEAT_CMDWIN
3817 if (p_cwh < 1)
3818 {
3819 errmsg = e_positive;
3820 p_cwh = 1;
3821 }
3822#endif
3823 if (p_ut < 0)
3824 {
3825 errmsg = e_positive;
3826 p_ut = 2000;
3827 }
3828 if (p_ss < 0)
3829 {
3830 errmsg = e_positive;
3831 p_ss = 0;
3832 }
3833
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003834 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3836 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3837
3838 options[opt_idx].flags |= P_WAS_SET;
3839
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003840#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003841 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3842 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003843#endif
3844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003845 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003846 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003847 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003848 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003849 if ((opt_flags & OPT_NO_REDRAW) == 0)
3850 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
3852 return errmsg;
3853}
3854
3855/*
3856 * Called after an option changed: check if something needs to be redrawn.
3857 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003859check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003861 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003862 int doclear = (flags & P_RCLR) == P_RCLR;
3863 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003865 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3869 changed_window_setting();
3870 if (flags & P_RBUF)
3871 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003872 if (flags & P_RWINONLY)
3873 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003874 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 redraw_all_later(CLEAR);
3876 else if (all)
3877 redraw_all_later(NOT_VALID);
3878}
3879
3880/*
3881 * Find index for option 'arg'.
3882 * Return -1 if not found.
3883 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003885findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
3887 int opt_idx;
3888 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003889 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 int is_term_opt;
3891
3892 /*
3893 * For first call: Initialize the quick-access table.
3894 * It contains the index for the first option that starts with a certain
3895 * letter. There are 26 letters, plus the first "t_" option.
3896 */
3897 if (quick_tab[1] == 0)
3898 {
3899 p = options[0].fullname;
3900 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3901 {
3902 if (s[0] != p[0])
3903 {
3904 if (s[0] == 't' && s[1] == '_')
3905 quick_tab[26] = opt_idx;
3906 else
3907 quick_tab[CharOrdLow(s[0])] = opt_idx;
3908 }
3909 p = s;
3910 }
3911 }
3912
3913 /*
3914 * Check for name starting with an illegal character.
3915 */
3916#ifdef EBCDIC
3917 if (!islower(arg[0]))
3918#else
3919 if (arg[0] < 'a' || arg[0] > 'z')
3920#endif
3921 return -1;
3922
3923 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3924 if (is_term_opt)
3925 opt_idx = quick_tab[26];
3926 else
3927 opt_idx = quick_tab[CharOrdLow(arg[0])];
3928 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3929 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003930 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932 }
3933 if (s == NULL && !is_term_opt)
3934 {
3935 opt_idx = quick_tab[CharOrdLow(arg[0])];
3936 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3937 {
3938 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003939 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 break;
3941 s = NULL;
3942 }
3943 }
3944 if (s == NULL)
3945 opt_idx = -1;
3946 return opt_idx;
3947}
3948
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003949#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950/*
3951 * Get the value for an option.
3952 *
3953 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003954 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003955 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003956 * String option: gov_string, *stringval gets allocated string.
3957 * Hidden Number option: gov_hidden_number.
3958 * Hidden Toggle option: gov_hidden_bool.
3959 * Hidden String option: gov_hidden_string.
3960 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003962 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003963get_option_value(
3964 char_u *name,
3965 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003967 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
3969 int opt_idx;
3970 char_u *varp;
3971
3972 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003973 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003974 {
3975 int key;
3976
3977 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003978 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003979 {
3980 char_u key_name[2];
3981 char_u *p;
3982
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003983 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003984 if (key < 0)
3985 {
3986 key_name[0] = KEY2TERMCAP0(key);
3987 key_name[1] = KEY2TERMCAP1(key);
3988 }
3989 else
3990 {
3991 key_name[0] = KS_KEY;
3992 key_name[1] = (key & 0xff);
3993 }
3994 p = find_termcode(key_name);
3995 if (p != NULL)
3996 {
3997 if (stringval != NULL)
3998 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003999 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004000 }
4001 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004002 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
4006
4007 if (options[opt_idx].flags & P_STRING)
4008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004010 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (stringval != NULL)
4012 {
4013#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004015 if ((char_u **)varp == &curbuf->b_p_key
4016 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 *stringval = vim_strsave((char_u *)"*****");
4018 else
4019#endif
4020 *stringval = vim_strsave(*(char_u **)(varp));
4021 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004022 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004025 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004026 return (options[opt_idx].flags & P_NUM)
4027 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (options[opt_idx].flags & P_NUM)
4029 *numval = *(long *)varp;
4030 else
4031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // Special case: 'modified' is b_changed, but we also want to consider
4033 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if ((int *)varp == &curbuf->b_changed)
4035 *numval = curbufIsChanged();
4036 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004037 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004039 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040}
4041#endif
4042
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004043#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004044/*
4045 * Returns the option attributes and its value. Unlike the above function it
4046 * will return either global value or local value of the option depending on
4047 * what was requested, but it will never return global value if it was
4048 * requested to return local one and vice versa. Neither it will return
4049 * buffer-local value if it was requested to return window-local one.
4050 *
4051 * Pretends that option is absent if it is not present in the requested scope
4052 * (i.e. has no global, window-local or buffer-local value depending on
4053 * opt_type). Uses
4054 *
4055 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004056 * 0 hidden or unknown option, also option that does not have requested
4057 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004058 * see SOPT_* in vim.h for other flags
4059 *
4060 * Possible opt_type values: see SREQ_* in vim.h
4061 */
4062 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004063get_option_value_strict(
4064 char_u *name,
4065 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004066 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004067 int opt_type,
4068 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004069{
4070 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004071 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004072 struct vimoption *p;
4073 int r = 0;
4074
4075 opt_idx = findoption(name);
4076 if (opt_idx < 0)
4077 return 0;
4078
4079 p = &(options[opt_idx]);
4080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004081 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004082 if (p->var == NULL)
4083 return 0;
4084
4085 if (p->flags & P_BOOL)
4086 r |= SOPT_BOOL;
4087 else if (p->flags & P_NUM)
4088 r |= SOPT_NUM;
4089 else if (p->flags & P_STRING)
4090 r |= SOPT_STRING;
4091
4092 if (p->indir == PV_NONE)
4093 {
4094 if (opt_type == SREQ_GLOBAL)
4095 r |= SOPT_GLOBAL;
4096 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004097 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004098 }
4099 else
4100 {
4101 if (p->indir & PV_BOTH)
4102 r |= SOPT_GLOBAL;
4103 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004104 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004105
4106 if (p->indir & PV_WIN)
4107 {
4108 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004110 else
4111 r |= SOPT_WIN;
4112 }
4113 else if (p->indir & PV_BUF)
4114 {
4115 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004116 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004117 else
4118 r |= SOPT_BUF;
4119 }
4120 }
4121
4122 if (stringval == NULL)
4123 return r;
4124
4125 if (opt_type == SREQ_GLOBAL)
4126 varp = p->var;
4127 else
4128 {
4129 if (opt_type == SREQ_BUF)
4130 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004131 // Special case: 'modified' is b_changed, but we also want to
4132 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004133 if (p->indir == PV_MOD)
4134 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004135 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004136 varp = NULL;
4137 }
4138#ifdef FEAT_CRYPT
4139 else if (p->indir == PV_KEY)
4140 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004141 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004142 *stringval = NULL;
4143 varp = NULL;
4144 }
4145#endif
4146 else
4147 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004148 buf_T *save_curbuf = curbuf;
4149
4150 // only getting a pointer, no need to use aucmd_prepbuf()
4151 curbuf = (buf_T *)from;
4152 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004153 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004154 curbuf = save_curbuf;
4155 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004156 }
4157 }
4158 else if (opt_type == SREQ_WIN)
4159 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004160 win_T *save_curwin = curwin;
4161
4162 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004163 curbuf = curwin->w_buffer;
4164 varp = get_varp(p);
4165 curwin = save_curwin;
4166 curbuf = curwin->w_buffer;
4167 }
4168 if (varp == p->var)
4169 return (r | SOPT_UNSET);
4170 }
4171
4172 if (varp != NULL)
4173 {
4174 if (p->flags & P_STRING)
4175 *stringval = vim_strsave(*(char_u **)(varp));
4176 else if (p->flags & P_NUM)
4177 *numval = *(long *) varp;
4178 else
4179 *numval = *(int *)varp;
4180 }
4181
4182 return r;
4183}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004184
4185/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004186 * Iterate over options. First argument is a pointer to a pointer to a
4187 * structure inside options[] array, second is option type like in the above
4188 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004189 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004190 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004191 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004192 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004193 * first argument to NULL.
4194 *
4195 * Returns full option name for current option on each call.
4196 */
4197 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004198option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004199{
4200 struct vimoption *ret = NULL;
4201 do
4202 {
4203 if (*option == NULL)
4204 *option = (void *) options;
4205 else if (((struct vimoption *) (*option))->fullname == NULL)
4206 {
4207 *option = NULL;
4208 return NULL;
4209 }
4210 else
4211 *option = (void *) (((struct vimoption *) (*option)) + 1);
4212
4213 ret = ((struct vimoption *) (*option));
4214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004215 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004216 if (ret->var == NULL)
4217 {
4218 ret = NULL;
4219 continue;
4220 }
4221
4222 switch (opt_type)
4223 {
4224 case SREQ_GLOBAL:
4225 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4226 ret = NULL;
4227 break;
4228 case SREQ_BUF:
4229 if (!(ret->indir & PV_BUF))
4230 ret = NULL;
4231 break;
4232 case SREQ_WIN:
4233 if (!(ret->indir & PV_WIN))
4234 ret = NULL;
4235 break;
4236 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004237 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004238 return NULL;
4239 }
4240 }
4241 while (ret == NULL);
4242
4243 return (char_u *)ret->fullname;
4244}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004245#endif
4246
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004248 * Return the flags for the option at 'opt_idx'.
4249 */
4250 long_u
4251get_option_flags(int opt_idx)
4252{
4253 return options[opt_idx].flags;
4254}
4255
4256/*
4257 * Set a flag for the option at 'opt_idx'.
4258 */
4259 void
4260set_option_flag(int opt_idx, long_u flag)
4261{
4262 options[opt_idx].flags |= flag;
4263}
4264
4265/*
4266 * Clear a flag for the option at 'opt_idx'.
4267 */
4268 void
4269clear_option_flag(int opt_idx, long_u flag)
4270{
4271 options[opt_idx].flags &= ~flag;
4272}
4273
4274/*
4275 * Returns TRUE if the option at 'opt_idx' is a global option
4276 */
4277 int
4278is_global_option(int opt_idx)
4279{
4280 return options[opt_idx].indir == PV_NONE;
4281}
4282
4283/*
4284 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4285 * local value.
4286 */
4287 int
4288is_global_local_option(int opt_idx)
4289{
4290 return options[opt_idx].indir & PV_BOTH;
4291}
4292
4293/*
4294 * Returns TRUE if the option at 'opt_idx' is a window-local option
4295 */
4296 int
4297is_window_local_option(int opt_idx)
4298{
4299 return options[opt_idx].var == VAR_WIN;
4300}
4301
4302/*
4303 * Returns TRUE if the option at 'opt_idx' is a hidden option
4304 */
4305 int
4306is_hidden_option(int opt_idx)
4307{
4308 return options[opt_idx].var == NULL;
4309}
4310
4311#if defined(FEAT_CRYPT) || defined(PROTO)
4312/*
4313 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4314 */
4315 int
4316is_crypt_key_option(int opt_idx)
4317{
4318 return options[opt_idx].indir == PV_KEY;
4319}
4320#endif
4321
4322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 * Set the value of option "name".
4324 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004325 *
4326 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004328 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004329set_option_value(
4330 char_u *name,
4331 long number,
4332 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004333 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334{
4335 int opt_idx;
4336 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004337 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004340 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004341 {
4342 int key;
4343
4344 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004345 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004346 {
4347 char_u key_name[2];
4348
4349 if (key < 0)
4350 {
4351 key_name[0] = KEY2TERMCAP0(key);
4352 key_name[1] = KEY2TERMCAP1(key);
4353 }
4354 else
4355 {
4356 key_name[0] = KS_KEY;
4357 key_name[1] = (key & 0xff);
4358 }
4359 add_termcode(key_name, string, FALSE);
4360 if (full_screen)
4361 ttest(FALSE);
4362 redraw_all_later(CLEAR);
4363 return NULL;
4364 }
4365
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004366 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 else
4369 {
4370 flags = options[opt_idx].flags;
4371#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004372 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004374 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004375 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004376 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004379 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004380 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 else
4382 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004383 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004384 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004386 if (number == 0 && string != NULL)
4387 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004388 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004390 // Either we are given a string or we are setting option
4391 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004392 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004393 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004394 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004396 // There's another character after zeros or the string
4397 // is empty. In both cases, we are trying to set a
4398 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004399 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004400 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004401 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004402
4403 }
4404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004406 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004407 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004409 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004410 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 }
4413 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004414 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415}
4416
4417/*
4418 * Get the terminal code for a terminal option.
4419 * Returns NULL when not found.
4420 */
4421 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004422get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 int opt_idx;
4425 char_u *varp;
4426
4427 if (tname[0] != 't' || tname[1] != '_' ||
4428 tname[2] == NUL || tname[3] == NUL)
4429 return NULL;
4430 if ((opt_idx = findoption(tname)) >= 0)
4431 {
4432 varp = get_varp(&(options[opt_idx]));
4433 if (varp != NULL)
4434 varp = *(char_u **)(varp);
4435 return varp;
4436 }
4437 return find_termcode(tname + 2);
4438}
4439
4440 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004441get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
4443 int i;
4444
4445 i = findoption((char_u *)"hl");
4446 if (i >= 0)
4447 return options[i].def_val[VI_DEFAULT];
4448 return (char_u *)NULL;
4449}
4450
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004451 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004452get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004453{
4454 int i;
4455
4456 i = findoption((char_u *)"enc");
4457 if (i >= 0)
4458 return options[i].def_val[VI_DEFAULT];
4459 return (char_u *)NULL;
4460}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462/*
4463 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004464 * When "has_lt" is true there is a '<' before "*arg_arg".
4465 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
4467 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004468find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004470 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004472 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /*
4475 * Don't use get_special_key_code() for t_xx, we don't want it to call
4476 * add_termcap_entry().
4477 */
4478 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4479 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004480 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004482 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004484 key = find_special_key(&arg, &modifiers,
4485 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004486 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 key = 0;
4488 }
4489 return key;
4490}
4491
4492/*
4493 * if 'all' == 0: show changed options
4494 * if 'all' == 1: show all normal options
4495 * if 'all' == 2: show all terminal options
4496 */
4497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004498showoptions(
4499 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004500 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
4502 struct vimoption *p;
4503 int col;
4504 int isterm;
4505 char_u *varp;
4506 struct vimoption **items;
4507 int item_count;
4508 int run;
4509 int row, rows;
4510 int cols;
4511 int i;
4512 int len;
4513
4514#define INC 20
4515#define GAP 3
4516
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004517 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (items == NULL)
4519 return;
4520
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004521 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004523 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004525 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004527 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004529 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004532 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 * 1. display the short items
4534 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004535 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 */
4537 for (run = 1; run <= 2 && !got_int; ++run)
4538 {
4539 /*
4540 * collect the items in items[]
4541 */
4542 item_count = 0;
4543 for (p = &options[0]; p->fullname != NULL; p++)
4544 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004545 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004546 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004547 continue;
4548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 varp = NULL;
4550 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004551 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
4553 if (p->indir != PV_NONE && !isterm)
4554 varp = get_varp_scope(p, opt_flags);
4555 }
4556 else
4557 varp = get_varp(p);
4558 if (varp != NULL
4559 && ((all == 2 && isterm)
4560 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004561 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004563 if (opt_flags & OPT_ONECOLUMN)
4564 len = Columns;
4565 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 else
4568 {
4569 option_value2string(p, opt_flags);
4570 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4571 }
4572 if ((len <= INC - GAP && run == 1) ||
4573 (len > INC - GAP && run == 2))
4574 items[item_count++] = p;
4575 }
4576 }
4577
4578 /*
4579 * display the items
4580 */
4581 if (run == 1)
4582 {
4583 cols = (Columns + GAP - 3) / INC;
4584 if (cols == 0)
4585 cols = 1;
4586 rows = (item_count + cols - 1) / cols;
4587 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004588 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 rows = item_count;
4590 for (row = 0; row < rows && !got_int; ++row)
4591 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004592 msg_putchar('\n'); // go to next line
4593 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 break;
4595 col = 0;
4596 for (i = row; i < item_count; i += rows)
4597 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004598 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 showoneopt(items[i], opt_flags);
4600 col += INC;
4601 }
4602 out_flush();
4603 ui_breakcheck();
4604 }
4605 }
4606 vim_free(items);
4607}
4608
4609/*
4610 * Return TRUE if option "p" has its default value.
4611 */
4612 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004613optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
4615 int dvi;
4616
4617 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004619 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004621 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004623 // the cast to long is required for Manx C, long_i is
4624 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004625 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004626 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4628}
4629
4630/*
4631 * showoneopt: show the value of one option
4632 * must not be called with a hidden option!
4633 */
4634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004635showoneopt(
4636 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004639 char_u *varp;
4640 int save_silent = silent_mode;
4641
4642 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004643 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
4645 varp = get_varp_scope(p, opt_flags);
4646
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004647 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4649 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004650 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004652 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004654 msg_puts(" ");
4655 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (!(p->flags & P_BOOL))
4657 {
4658 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004659 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 option_value2string(p, opt_flags);
4661 msg_outtrans(NameBuff);
4662 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004663
4664 silent_mode = save_silent;
4665 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666}
4667
4668/*
4669 * Write modified options as ":set" commands to a file.
4670 *
4671 * There are three values for "opt_flags":
4672 * OPT_GLOBAL: Write global option values and fresh values of
4673 * buffer-local options (used for start of a session
4674 * file).
4675 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4676 * curwin (used for a vimrc file).
4677 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4678 * and local values for window-local options of
4679 * curwin. Local values are also written when at the
4680 * default value, because a modeline or autocommand
4681 * may have set them when doing ":edit file" and the
4682 * user has set them back at the default or fresh
4683 * value.
4684 * When "local_only" is TRUE, don't write fresh
4685 * values, only local values (for ":mkview").
4686 * (fresh value = value used for a new buffer or window for a local option).
4687 *
4688 * Return FAIL on error, OK otherwise.
4689 */
4690 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004691makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004694 char_u *varp; // currently used value
4695 char_u *varp_fresh; // local value
4696 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 char *cmd;
4698 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004699 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701 /*
4702 * The options that don't have a default (terminal name, columns, lines)
4703 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004704 * Do the loop over "options[]" twice: once for options with the
4705 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004707 for (pri = 1; pri >= 0; --pri)
4708 {
4709 for (p = &options[0]; !istermoption(p); p++)
4710 if (!(p->flags & P_NO_MKRC)
4711 && !istermoption(p)
4712 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4716 continue;
4717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004718 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4719 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4721 continue;
4722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004723 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004725 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 continue;
4727
Bram Moolenaard23b7142021-04-17 21:04:34 +02004728 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4729 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004730 continue;
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 round = 2;
4733 if (p->indir != PV_NONE)
4734 {
4735 if (p->var == VAR_WIN)
4736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004737 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (!(opt_flags & OPT_LOCAL))
4739 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004740 // When fresh value of window-local option is not at the
4741 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4743 {
4744 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004745 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
4747 round = 1;
4748 varp_local = varp;
4749 varp = varp_fresh;
4750 }
4751 }
4752 }
4753 }
4754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004755 // Round 1: fresh value for window-local options.
4756 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 for ( ; round <= 2; varp = varp_local, ++round)
4758 {
4759 if (round == 1 || (opt_flags & OPT_GLOBAL))
4760 cmd = "set";
4761 else
4762 cmd = "setlocal";
4763
4764 if (p->flags & P_BOOL)
4765 {
4766 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4767 return FAIL;
4768 }
4769 else if (p->flags & P_NUM)
4770 {
4771 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4772 return FAIL;
4773 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004776 int do_endif = FALSE;
4777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004778 // Don't set 'syntax' and 'filetype' again if the value is
4779 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004780 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004781#if defined(FEAT_SYN_HL)
4782 p->indir == PV_SYN ||
4783#endif
4784 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4787 *(char_u **)(varp)) < 0
4788 || put_eol(fd) < 0)
4789 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004790 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004793 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004795 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
4797 if (put_line(fd, "endif") == FAIL)
4798 return FAIL;
4799 }
4800 }
4801 }
4802 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return OK;
4805}
4806
4807#if defined(FEAT_FOLDING) || defined(PROTO)
4808/*
4809 * Generate set commands for the local fold options only. Used when
4810 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4811 */
4812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004813makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004815 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004817 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 == FAIL
4819# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004820 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004822 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 == FAIL
4824 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4825 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4826 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4827 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4828 )
4829 return FAIL;
4830
4831 return OK;
4832}
4833#endif
4834
4835 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004836put_setstring(
4837 FILE *fd,
4838 char *cmd,
4839 char *name,
4840 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004841 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004844 char_u *buf = NULL;
4845 char_u *part = NULL;
4846 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4849 return FAIL;
4850 if (*valuep != NULL)
4851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004852 // Output 'pastetoggle' as key names. For other
4853 // options some characters have to be escaped with
4854 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (valuep == &p_pt)
4856 {
4857 s = *valuep;
4858 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004859 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 return FAIL;
4861 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004862 // expand the option value, replace $HOME by ~
4863 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004865 int size = (int)STRLEN(*valuep) + 1;
4866
4867 // replace home directory in the whole option value into "buf"
4868 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004869 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004870 goto fail;
4871 home_replace(NULL, *valuep, buf, size, FALSE);
4872
4873 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004874 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004875 // can be expanded when read back.
4876 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4877 && vim_strchr(*valuep, ',') != NULL)
4878 {
4879 part = alloc(size);
4880 if (part == NULL)
4881 goto fail;
4882
4883 // write line break to clear the option, e.g. ':set rtp='
4884 if (put_eol(fd) == FAIL)
4885 goto fail;
4886
4887 p = buf;
4888 while (*p != NUL)
4889 {
4890 // for each comma separated option part, append value to
4891 // the option, :set rtp+=value
4892 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4893 goto fail;
4894 (void)copy_option_part(&p, part, size, ",");
4895 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4896 goto fail;
4897 }
4898 vim_free(buf);
4899 vim_free(part);
4900 return OK;
4901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004903 {
4904 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004906 }
4907 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else if (put_escstr(fd, *valuep, 2) == FAIL)
4910 return FAIL;
4911 }
4912 if (put_eol(fd) < 0)
4913 return FAIL;
4914 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004915fail:
4916 vim_free(buf);
4917 vim_free(part);
4918 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919}
4920
4921 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004922put_setnum(
4923 FILE *fd,
4924 char *cmd,
4925 char *name,
4926 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
4928 long wc;
4929
4930 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4931 return FAIL;
4932 if (wc_use_keyname((char_u *)valuep, &wc))
4933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004934 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4936 return FAIL;
4937 }
4938 else if (fprintf(fd, "%ld", *valuep) < 0)
4939 return FAIL;
4940 if (put_eol(fd) < 0)
4941 return FAIL;
4942 return OK;
4943}
4944
4945 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004946put_setbool(
4947 FILE *fd,
4948 char *cmd,
4949 char *name,
4950 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004953 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4955 || put_eol(fd) < 0)
4956 return FAIL;
4957 return OK;
4958}
4959
4960/*
4961 * Clear all the terminal options.
4962 * If the option has been allocated, free the memory.
4963 * Terminal options are never hidden or indirect.
4964 */
4965 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004966clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 /*
4969 * Reset a few things before clearing the old options. This may cause
4970 * outputting a few things that the terminal doesn't understand, but the
4971 * screen will be cleared later, so this is OK.
4972 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004973 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004975 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#endif
4977#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004978 // When starting the GUI close the display opened for the clipboard.
4979 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (gui.starting)
4981 clear_xterm_clip();
4982#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004985 free_termoptions();
4986}
4987
4988 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004989free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004990{
4991 struct vimoption *p;
4992
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004993 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 if (istermoption(p))
4995 {
4996 if (p->flags & P_ALLOCED)
4997 free_string_option(*(char_u **)(p->var));
4998 if (p->flags & P_DEF_ALLOCED)
4999 free_string_option(p->def_val[VI_DEFAULT]);
5000 *(char_u **)(p->var) = empty_option;
5001 p->def_val[VI_DEFAULT] = empty_option;
5002 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005003#ifdef FEAT_EVAL
5004 // remember where the option was cleared
5005 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 clear_termcodes();
5009}
5010
5011/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005012 * Free the string for one term option, if it was allocated.
5013 * Set the string to empty_option and clear allocated flag.
5014 * "var" points to the option value.
5015 */
5016 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005017free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005018{
5019 struct vimoption *p;
5020
5021 for (p = &options[0]; p->fullname != NULL; p++)
5022 if (p->var == var)
5023 {
5024 if (p->flags & P_ALLOCED)
5025 free_string_option(*(char_u **)(p->var));
5026 *(char_u **)(p->var) = empty_option;
5027 p->flags &= ~P_ALLOCED;
5028 break;
5029 }
5030}
5031
5032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * Set the terminal option defaults to the current value.
5034 * Used after setting the terminal name.
5035 */
5036 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005037set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 struct vimoption *p;
5040
5041 for (p = &options[0]; p->fullname != NULL; p++)
5042 {
5043 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5044 {
5045 if (p->flags & P_DEF_ALLOCED)
5046 {
5047 free_string_option(p->def_val[VI_DEFAULT]);
5048 p->flags &= ~P_DEF_ALLOCED;
5049 }
5050 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5051 if (p->flags & P_ALLOCED)
5052 {
5053 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005054 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 }
5057 }
5058}
5059
5060/*
5061 * return TRUE if 'p' starts with 't_'
5062 */
5063 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005064istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5067}
5068
Bram Moolenaardac13472019-09-16 21:06:21 +02005069/*
5070 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5071 */
5072 int
5073istermoption_idx(int opt_idx)
5074{
5075 return istermoption(&options[opt_idx]);
5076}
5077
Bram Moolenaar113e1072019-01-20 15:30:40 +01005078#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005080 * Unset local option value, similar to ":set opt<".
5081 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005082 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005083unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005084{
5085 struct vimoption *p;
5086 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005087 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005088
5089 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005090 if (opt_idx < 0)
5091 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005092 p = &(options[opt_idx]);
5093
5094 switch ((int)p->indir)
5095 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005096 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005098 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005099 break;
5100 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005101 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102 break;
5103 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005104 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005105 break;
5106 case PV_AR:
5107 buf->b_p_ar = -1;
5108 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005109 case PV_BKC:
5110 clear_string_option(&buf->b_p_bkc);
5111 buf->b_bkc_flags = 0;
5112 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005113 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005114 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005115 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005116 case PV_TC:
5117 clear_string_option(&buf->b_p_tc);
5118 buf->b_tc_flags = 0;
5119 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005120 case PV_SISO:
5121 curwin->w_p_siso = -1;
5122 break;
5123 case PV_SO:
5124 curwin->w_p_so = -1;
5125 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005126#ifdef FEAT_FIND_ID
5127 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005128 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005129 break;
5130 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005131 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005132 break;
5133#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005134 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005135 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005136 break;
5137 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005138 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005139 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005140#ifdef FEAT_COMPL_FUNC
5141 case PV_TSRFU:
5142 clear_string_option(&buf->b_p_tsrfu);
5143 break;
5144#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005145 case PV_FP:
5146 clear_string_option(&buf->b_p_fp);
5147 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005148#ifdef FEAT_QUICKFIX
5149 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005150 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151 break;
5152 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005153 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 break;
5155 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005156 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 break;
5158#endif
5159#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5160 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005161 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005162 break;
5163#endif
5164#if defined(FEAT_CRYPT)
5165 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005166 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005167 break;
5168#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005169#ifdef FEAT_LINEBREAK
5170 case PV_SBR:
5171 clear_string_option(&((win_T *)from)->w_p_sbr);
5172 break;
5173#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005174#ifdef FEAT_STL_OPT
5175 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005176 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005177 break;
5178#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005179 case PV_UL:
5180 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5181 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005182#ifdef FEAT_LISP
5183 case PV_LW:
5184 clear_string_option(&buf->b_p_lw);
5185 break;
5186#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005187 case PV_MENC:
5188 clear_string_option(&buf->b_p_menc);
5189 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005190 case PV_LCS:
5191 clear_string_option(&((win_T *)from)->w_p_lcs);
5192 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5193 redraw_later(NOT_VALID);
5194 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005195 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005196 clear_string_option(&((win_T *)from)->w_p_ve);
5197 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005198 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005199 }
5200}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005201#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005202
5203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 * Get pointer to option variable, depending on local or global scope.
5205 */
5206 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005207get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208{
5209 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5210 {
5211 if (p->var == VAR_WIN)
5212 return (char_u *)GLOBAL_WO(get_varp(p));
5213 return p->var;
5214 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005215 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
5217 switch ((int)p->indir)
5218 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005219 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005221 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5222 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5223 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005225 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5226 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5227 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005228 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005229 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005230 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005231 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5232 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005234 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5235 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005237 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5238 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005239#ifdef FEAT_COMPL_FUNC
5240 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5241#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005242#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5243 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5244#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005245#if defined(FEAT_CRYPT)
5246 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5247#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005248#ifdef FEAT_LINEBREAK
5249 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5250#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005251#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005252 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005253#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005254 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005255#ifdef FEAT_LISP
5256 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5257#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005258 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005259 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005260 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005261 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005264 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 return get_varp(p);
5267}
5268
5269/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005270 * Get pointer to option variable at 'opt_idx', depending on local or global
5271 * scope.
5272 */
5273 char_u *
5274get_option_varp_scope(int opt_idx, int opt_flags)
5275{
5276 return get_varp_scope(&(options[opt_idx]), opt_flags);
5277}
5278
5279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 * Get pointer to option variable.
5281 */
5282 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005283get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005285 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (p->var == NULL)
5287 return NULL;
5288
5289 switch ((int)p->indir)
5290 {
5291 case PV_NONE: return p->var;
5292
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005293 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005294 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005296 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005298 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005300 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005302 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005304 case PV_TC: return *curbuf->b_p_tc != NUL
5305 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005306 case PV_BKC: return *curbuf->b_p_bkc != NUL
5307 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005308 case PV_SISO: return curwin->w_p_siso >= 0
5309 ? (char_u *)&(curwin->w_p_siso) : p->var;
5310 case PV_SO: return curwin->w_p_so >= 0
5311 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005313 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005315 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5317#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005318 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005320 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005322#ifdef FEAT_COMPL_FUNC
5323 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5324 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5325#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005326 case PV_FP: return *curbuf->b_p_fp != NUL
5327 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005329 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005331 case PV_GP: return *curbuf->b_p_gp != NUL
5332 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5333 case PV_MP: return *curbuf->b_p_mp != NUL
5334 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005336#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5337 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5338 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5339#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005340#if defined(FEAT_CRYPT)
5341 case PV_CM: return *curbuf->b_p_cm != NUL
5342 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5343#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005344#ifdef FEAT_LINEBREAK
5345 case PV_SBR: return *curwin->w_p_sbr != NUL
5346 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5347#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005348#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005349 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005350 ? (char_u *)&(curwin->w_p_stl) : p->var;
5351#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005352 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5353 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005354#ifdef FEAT_LISP
5355 case PV_LW: return *curbuf->b_p_lw != NUL
5356 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5357#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005358 case PV_MENC: return *curbuf->b_p_menc != NUL
5359 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#ifdef FEAT_ARABIC
5361 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5362#endif
5363 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005364 case PV_LCS: return *curwin->w_p_lcs != NUL
5365 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005366 case PV_VE: return *curwin->w_p_ve != NUL
5367 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005368#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005369 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5370#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005371#ifdef FEAT_SYN_HL
5372 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5373 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005374 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005375 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377#ifdef FEAT_DIFF
5378 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5379#endif
5380#ifdef FEAT_FOLDING
5381 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5382 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5383 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5384 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5385 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5386 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5387 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5388# ifdef FEAT_EVAL
5389 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5390 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5391# endif
5392 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5393#endif
5394 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005395 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005396#ifdef FEAT_LINEBREAK
5397 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005400 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005401#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5403#endif
5404#ifdef FEAT_RIGHTLEFT
5405 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5406 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5407#endif
5408 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5409 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5410#ifdef FEAT_LINEBREAK
5411 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005412 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5413 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005415 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005417 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005418#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005419 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5420 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5421#endif
5422#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005423 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5424 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5425 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
5428 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5429 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5432 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5434 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5435#ifdef FEAT_CINDENT
5436 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5437 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5438 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5439#endif
5440#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5441 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#ifdef FEAT_FOLDING
5445 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005448#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005449 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005451#ifdef FEAT_COMPL_FUNC
5452 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005453 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005454#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005455#ifdef FEAT_EVAL
5456 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005459 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005465 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5467 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5468 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5469 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5470#ifdef FEAT_FIND_ID
5471# ifdef FEAT_EVAL
5472 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5473# endif
5474#endif
5475#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5476 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5477 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5478#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005479#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005480 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482#ifdef FEAT_CRYPT
5483 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5484#endif
5485#ifdef FEAT_LISP
5486 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5487#endif
5488 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5489 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5490 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5491 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5492 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005494#ifdef FEAT_TEXTOBJ
5495 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5498#ifdef FEAT_SMARTINDENT
5499 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5503#ifdef FEAT_SEARCHPATH
5504 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5505#endif
5506 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5507#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005508 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005509 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5510#endif
5511#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005512 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5513 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5514 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005515 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#endif
5517 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5518 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5519 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5520 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005521#ifdef FEAT_PERSISTENT_UNDO
5522 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5525#ifdef FEAT_KEYMAP
5526 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5527#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005528#ifdef FEAT_SIGNS
5529 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5530#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005531#ifdef FEAT_VARTABS
5532 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5533 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5534#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005535 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005537 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return (char_u *)&(curbuf->b_p_wm);
5539}
5540
5541/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005542 * Return a pointer to the variable for option at 'opt_idx'
5543 */
5544 char_u *
5545get_option_var(int opt_idx)
5546{
5547 return options[opt_idx].var;
5548}
5549
5550/*
5551 * Return the full name of the option at 'opt_idx'
5552 */
5553 char_u *
5554get_option_fullname(int opt_idx)
5555{
5556 return (char_u *)options[opt_idx].fullname;
5557}
5558
5559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 * Get the value of 'equalprg', either the buffer-local one or the global one.
5561 */
5562 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005563get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 if (*curbuf->b_p_ep == NUL)
5566 return p_ep;
5567 return curbuf->b_p_ep;
5568}
5569
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570/*
5571 * Copy options from one window to another.
5572 * Used when splitting a window.
5573 */
5574 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005575win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
5577 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5578 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005579 after_copy_winopt(wp_to);
5580}
5581
5582/*
5583 * After copying window options: update variables depending on options.
5584 */
5585 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005586after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005587{
5588#ifdef FEAT_LINEBREAK
5589 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005590#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005591#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005592 fill_culopt_flags(NULL, wp);
5593 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005594#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005595 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
5598/*
5599 * Copy the options from one winopt_T to another.
5600 * Doesn't free the old option values in "to", use clear_winopt() for that.
5601 * The 'scroll' option is not copied, because it depends on the window height.
5602 * The 'previewwindow' option is reset, there can be only one preview window.
5603 */
5604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005605copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
5607#ifdef FEAT_ARABIC
5608 to->wo_arab = from->wo_arab;
5609#endif
5610 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005611 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005613 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005614 to->wo_ve = vim_strsave(from->wo_ve);
5615 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005616#ifdef FEAT_LINEBREAK
5617 to->wo_nuw = from->wo_nuw;
5618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619#ifdef FEAT_RIGHTLEFT
5620 to->wo_rl = from->wo_rl;
5621 to->wo_rlc = vim_strsave(from->wo_rlc);
5622#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005623#ifdef FEAT_LINEBREAK
5624 to->wo_sbr = vim_strsave(from->wo_sbr);
5625#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005626#ifdef FEAT_STL_OPT
5627 to->wo_stl = vim_strsave(from->wo_stl);
5628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005630#ifdef FEAT_DIFF
5631 to->wo_wrap_save = from->wo_wrap_save;
5632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633#ifdef FEAT_LINEBREAK
5634 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005635 to->wo_bri = from->wo_bri;
5636 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005638 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005640 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005641 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005642 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005643#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005644 to->wo_spell = from->wo_spell;
5645#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005646#ifdef FEAT_SYN_HL
5647 to->wo_cuc = from->wo_cuc;
5648 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005649 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005650 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652#ifdef FEAT_DIFF
5653 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005654 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005656#ifdef FEAT_CONCEAL
5657 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005658 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005659#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005660#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005661 to->wo_twk = vim_strsave(from->wo_twk);
5662 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664#ifdef FEAT_FOLDING
5665 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005666 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005668 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 to->wo_fdi = vim_strsave(from->wo_fdi);
5670 to->wo_fml = from->wo_fml;
5671 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005672 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005674 to->wo_fdm_save = from->wo_diff_saved
5675 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 to->wo_fdn = from->wo_fdn;
5677# ifdef FEAT_EVAL
5678 to->wo_fde = vim_strsave(from->wo_fde);
5679 to->wo_fdt = vim_strsave(from->wo_fdt);
5680# endif
5681 to->wo_fmr = vim_strsave(from->wo_fmr);
5682#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005683#ifdef FEAT_SIGNS
5684 to->wo_scl = vim_strsave(from->wo_scl);
5685#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005686
5687#ifdef FEAT_EVAL
5688 // Copy the script context so that we know where the value was last set.
5689 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5690 sizeof(to->wo_script_ctx));
5691#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005692 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693}
5694
5695/*
5696 * Check string options in a window for a NULL value.
5697 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005699check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 check_winopt(&win->w_onebuf_opt);
5702 check_winopt(&win->w_allbuf_opt);
5703}
5704
5705/*
5706 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5707 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005709check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711#ifdef FEAT_FOLDING
5712 check_string_option(&wop->wo_fdi);
5713 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005714 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715# ifdef FEAT_EVAL
5716 check_string_option(&wop->wo_fde);
5717 check_string_option(&wop->wo_fdt);
5718# endif
5719 check_string_option(&wop->wo_fmr);
5720#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005721#ifdef FEAT_SIGNS
5722 check_string_option(&wop->wo_scl);
5723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724#ifdef FEAT_RIGHTLEFT
5725 check_string_option(&wop->wo_rlc);
5726#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005727#ifdef FEAT_LINEBREAK
5728 check_string_option(&wop->wo_sbr);
5729#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005730#ifdef FEAT_STL_OPT
5731 check_string_option(&wop->wo_stl);
5732#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005733#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005734 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005735 check_string_option(&wop->wo_cc);
5736#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005737#ifdef FEAT_CONCEAL
5738 check_string_option(&wop->wo_cocu);
5739#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005740#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005741 check_string_option(&wop->wo_twk);
5742 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005743#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005744#ifdef FEAT_LINEBREAK
5745 check_string_option(&wop->wo_briopt);
5746#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005747 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005748 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005749 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750}
5751
5752/*
5753 * Free the allocated memory inside a winopt_T.
5754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005756clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758#ifdef FEAT_FOLDING
5759 clear_string_option(&wop->wo_fdi);
5760 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005761 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762# ifdef FEAT_EVAL
5763 clear_string_option(&wop->wo_fde);
5764 clear_string_option(&wop->wo_fdt);
5765# endif
5766 clear_string_option(&wop->wo_fmr);
5767#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005768#ifdef FEAT_SIGNS
5769 clear_string_option(&wop->wo_scl);
5770#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005771#ifdef FEAT_LINEBREAK
5772 clear_string_option(&wop->wo_briopt);
5773#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005774 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#ifdef FEAT_RIGHTLEFT
5776 clear_string_option(&wop->wo_rlc);
5777#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005778#ifdef FEAT_LINEBREAK
5779 clear_string_option(&wop->wo_sbr);
5780#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005781#ifdef FEAT_STL_OPT
5782 clear_string_option(&wop->wo_stl);
5783#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005784#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005785 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005786 clear_string_option(&wop->wo_cc);
5787#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005788#ifdef FEAT_CONCEAL
5789 clear_string_option(&wop->wo_cocu);
5790#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005791#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005792 clear_string_option(&wop->wo_twk);
5793 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005794#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005795 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005796 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797}
5798
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005799#ifdef FEAT_EVAL
5800// Index into the options table for a buffer-local option enum.
5801static int buf_opt_idx[BV_COUNT];
5802# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5803
5804/*
5805 * Initialize buf_opt_idx[] if not done already.
5806 */
5807 static void
5808init_buf_opt_idx(void)
5809{
5810 static int did_init_buf_opt_idx = FALSE;
5811 int i;
5812
5813 if (did_init_buf_opt_idx)
5814 return;
5815 did_init_buf_opt_idx = TRUE;
5816 for (i = 0; !istermoption_idx(i); i++)
5817 if (options[i].indir & PV_BUF)
5818 buf_opt_idx[options[i].indir & PV_MASK] = i;
5819}
5820#else
5821# define COPY_OPT_SCTX(buf, bv)
5822#endif
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824/*
5825 * Copy global option values to local options for one buffer.
5826 * Used when creating a new buffer and sometimes when entering a buffer.
5827 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005828 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5830 * appropriate.
5831 * BCO_NOHELP Don't copy the values to a help buffer.
5832 */
5833 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005834buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005837 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 int dont_do_help;
5839 int did_isk = FALSE;
5840
5841 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 * Skip this when the option defaults have not been set yet. Happens when
5843 * main() allocates the first buffer.
5844 */
5845 if (p_cpo != NULL)
5846 {
5847 /*
5848 * Always copy when entering and 'cpo' contains 'S'.
5849 * Don't copy when already initialized.
5850 * Don't copy when 'cpo' contains 's' and not entering.
5851 * 'S' BCO_ENTER initialized 's' should_copy
5852 * yes yes X X TRUE
5853 * yes no yes X FALSE
5854 * no X yes X FALSE
5855 * X no no yes FALSE
5856 * X no no no TRUE
5857 * no yes no X TRUE
5858 */
5859 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5860 && (buf->b_p_initialized
5861 || (!(flags & BCO_ENTER)
5862 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5863 should_copy = FALSE;
5864
5865 if (should_copy || (flags & BCO_ALWAYS))
5866 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005867#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005868 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005869 init_buf_opt_idx();
5870#endif
5871 // Don't copy the options specific to a help buffer when
5872 // BCO_NOHELP is given or the options were initialized already
5873 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5875 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005876 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
5878 save_p_isk = buf->b_p_isk;
5879 buf->b_p_isk = NULL;
5880 }
5881 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005882 * Always free the allocated strings. If not already initialized,
5883 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 */
5885 if (!buf->b_p_initialized)
5886 {
5887 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005888 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005891 switch (*p_ffs)
5892 {
5893 case 'm':
5894 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5895 case 'd':
5896 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5897 case 'u':
5898 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5899 default:
5900 buf->b_p_ff = vim_strsave(p_ff);
5901 }
5902 if (buf->b_p_ff != NULL)
5903 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 buf->b_p_bh = empty_option;
5905 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
5907 else
5908 free_buf_options(buf, FALSE);
5909
5910 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005911 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 buf->b_p_ai_nopaste = p_ai_nopaste;
5913 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005914 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005916 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 buf->b_p_tw_nopaste = p_tw_nopaste;
5918 buf->b_p_tw_nobin = p_tw_nobin;
5919 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005920 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 buf->b_p_wm_nopaste = p_wm_nopaste;
5922 buf->b_p_wm_nobin = p_wm_nobin;
5923 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005924 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005925 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005926 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005927 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005928 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005930 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005932 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf->b_p_ml_nobin = p_ml_nobin;
5936 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005937 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005938 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005939 buf->b_p_swf = FALSE;
5940 else
5941 {
5942 buf->b_p_swf = p_swf;
5943 COPY_OPT_SCTX(buf, BV_INF);
5944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005946 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005947#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005948 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005951#ifdef FEAT_COMPL_FUNC
5952 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005954 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005956#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005957#ifdef FEAT_EVAL
5958 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00005960 buf_set_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005965#ifdef FEAT_VARTABS
5966 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005968 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005969 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005970 else
5971 buf->b_p_vsts_array = 0;
5972 buf->b_p_vsts_nopaste = p_vsts_nopaste
5973 ? vim_strsave(p_vsts_nopaste) : NULL;
5974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005976 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005978 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#ifdef FEAT_FOLDING
5980 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005981 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982#endif
5983 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005984 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005985 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005986 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005987 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5988 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005992 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#ifdef FEAT_SMARTINDENT
5994 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005995 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996#endif
5997 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005998 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999#ifdef FEAT_CINDENT
6000 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006003 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006005 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006010 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6012 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006013 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014#endif
6015#ifdef FEAT_LISP
6016 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#endif
6019#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006020 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006022 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006023 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006024 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006025#endif
6026#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006027 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006029 (void)compile_cap_prog(&buf->b_s);
6030 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006031 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006032 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006034 buf->b_s.b_p_spo = vim_strsave(p_spo);
6035 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036#endif
6037#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6038 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006039 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006041 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006043 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006044#if defined(FEAT_EVAL)
6045 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006046 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#ifdef FEAT_CRYPT
6049 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006050 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051#endif
6052#ifdef FEAT_SEARCHPATH
6053 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006054 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055#endif
6056#ifdef FEAT_KEYMAP
6057 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006058 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 buf->b_kmap_state |= KEYMAP_INIT;
6060#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006061#ifdef FEAT_TERMINAL
6062 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006063 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006064#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006065 // This isn't really an option, but copying the langmap and IME
6066 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006068 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006070 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006072 // options that are normally global but also have a local value
6073 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006075 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006076 buf->b_p_bkc = empty_option;
6077 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078#ifdef FEAT_QUICKFIX
6079 buf->b_p_gp = empty_option;
6080 buf->b_p_mp = empty_option;
6081 buf->b_p_efm = empty_option;
6082#endif
6083 buf->b_p_ep = empty_option;
6084 buf->b_p_kp = empty_option;
6085 buf->b_p_path = empty_option;
6086 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006087 buf->b_p_tc = empty_option;
6088 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089#ifdef FEAT_FIND_ID
6090 buf->b_p_def = empty_option;
6091 buf->b_p_inc = empty_option;
6092# ifdef FEAT_EVAL
6093 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006094 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095# endif
6096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 buf->b_p_dict = empty_option;
6098 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006099#ifdef FEAT_COMPL_FUNC
6100 buf->b_p_tsrfu = empty_option;
6101#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006102#ifdef FEAT_TEXTOBJ
6103 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006104 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006105#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006106#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6107 buf->b_p_bexpr = empty_option;
6108#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006109#if defined(FEAT_CRYPT)
6110 buf->b_p_cm = empty_option;
6111#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006112#ifdef FEAT_PERSISTENT_UNDO
6113 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006114 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006115#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006116#ifdef FEAT_LISP
6117 buf->b_p_lw = empty_option;
6118#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006119 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 /*
6122 * Don't copy the options set by ex_help(), use the saved values,
6123 * when going from a help buffer to a non-help buffer.
6124 * Don't touch these at all when BCO_NOHELP is used and going from
6125 * or to a help buffer.
6126 */
6127 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006130#ifdef FEAT_VARTABS
6131 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006132 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006133 else
6134 buf->b_p_vts_array = NULL;
6135#endif
6136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 else
6138 {
6139 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006140 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 did_isk = TRUE;
6142 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006143#ifdef FEAT_VARTABS
6144 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006145 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006146 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006147 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006148 else
6149 buf->b_p_vts_array = NULL;
6150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (buf->b_p_bt[0] == 'h')
6153 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006155 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 }
6157 }
6158
6159 /*
6160 * When the options should be copied (ignoring BCO_ALWAYS), set the
6161 * flag that indicates that the options have been initialized.
6162 */
6163 if (should_copy)
6164 buf->b_p_initialized = TRUE;
6165 }
6166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006167 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (did_isk)
6169 (void)buf_init_chartab(buf, FALSE);
6170}
6171
6172/*
6173 * Reset the 'modifiable' option and its default value.
6174 */
6175 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006176reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
6178 int opt_idx;
6179
6180 curbuf->b_p_ma = FALSE;
6181 p_ma = FALSE;
6182 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006183 if (opt_idx >= 0)
6184 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185}
6186
6187/*
6188 * Set the global value for 'iminsert' to the local value.
6189 */
6190 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006191set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
6193 p_iminsert = curbuf->b_p_iminsert;
6194}
6195
6196/*
6197 * Set the global value for 'imsearch' to the local value.
6198 */
6199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006200set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 p_imsearch = curbuf->b_p_imsearch;
6203}
6204
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205static int expand_option_idx = -1;
6206static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6207static int expand_option_flags = 0;
6208
6209 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006210set_context_in_set_cmd(
6211 expand_T *xp,
6212 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006213 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006216 long_u flags = 0; // init for GCC
6217 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 char_u *p;
6219 char_u *s;
6220 int is_term_option = FALSE;
6221 int key;
6222
6223 expand_option_flags = opt_flags;
6224
6225 xp->xp_context = EXPAND_SETTINGS;
6226 if (*arg == NUL)
6227 {
6228 xp->xp_pattern = arg;
6229 return;
6230 }
6231 p = arg + STRLEN(arg) - 1;
6232 if (*p == ' ' && *(p - 1) != '\\')
6233 {
6234 xp->xp_pattern = p + 1;
6235 return;
6236 }
6237 while (p > arg)
6238 {
6239 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006240 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (*p == ' ' || *p == ',')
6242 {
6243 while (s > arg && *(s - 1) == '\\')
6244 --s;
6245 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006246 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (*p == ' ' && ((p - s) & 1) == 0)
6248 {
6249 ++p;
6250 break;
6251 }
6252 --p;
6253 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006254 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
6256 xp->xp_context = EXPAND_BOOL_SETTINGS;
6257 p += 2;
6258 }
6259 if (STRNCMP(p, "inv", 3) == 0)
6260 {
6261 xp->xp_context = EXPAND_BOOL_SETTINGS;
6262 p += 3;
6263 }
6264 xp->xp_pattern = arg = p;
6265 if (*arg == '<')
6266 {
6267 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006268 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 return;
6270 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006271 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
6273 xp->xp_context = EXPAND_NOTHING;
6274 return;
6275 }
6276 nextchar = *++p;
6277 is_term_option = TRUE;
6278 expand_option_name[2] = KEY2TERMCAP0(key);
6279 expand_option_name[3] = KEY2TERMCAP1(key);
6280 }
6281 else
6282 {
6283 if (p[0] == 't' && p[1] == '_')
6284 {
6285 p += 2;
6286 if (*p != NUL)
6287 ++p;
6288 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006289 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 nextchar = *++p;
6291 is_term_option = TRUE;
6292 expand_option_name[2] = p[-2];
6293 expand_option_name[3] = p[-1];
6294 }
6295 else
6296 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006297 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6299 p++;
6300 if (*p == NUL)
6301 return;
6302 nextchar = *p;
6303 *p = NUL;
6304 opt_idx = findoption(arg);
6305 *p = nextchar;
6306 if (opt_idx == -1 || options[opt_idx].var == NULL)
6307 {
6308 xp->xp_context = EXPAND_NOTHING;
6309 return;
6310 }
6311 flags = options[opt_idx].flags;
6312 if (flags & P_BOOL)
6313 {
6314 xp->xp_context = EXPAND_NOTHING;
6315 return;
6316 }
6317 }
6318 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006319 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6321 {
6322 ++p;
6323 nextchar = '=';
6324 }
6325 if ((nextchar != '=' && nextchar != ':')
6326 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6327 {
6328 xp->xp_context = EXPAND_UNSUCCESSFUL;
6329 return;
6330 }
6331 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6332 {
6333 xp->xp_context = EXPAND_OLD_SETTING;
6334 if (is_term_option)
6335 expand_option_idx = -1;
6336 else
6337 expand_option_idx = opt_idx;
6338 xp->xp_pattern = p + 1;
6339 return;
6340 }
6341 xp->xp_context = EXPAND_NOTHING;
6342 if (is_term_option || (flags & P_NUM))
6343 return;
6344
6345 xp->xp_pattern = p + 1;
6346
6347 if (flags & P_EXPAND)
6348 {
6349 p = options[opt_idx].var;
6350 if (p == (char_u *)&p_bdir
6351 || p == (char_u *)&p_dir
6352 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006353 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 || p == (char_u *)&p_rtp
6355#ifdef FEAT_SEARCHPATH
6356 || p == (char_u *)&p_cdpath
6357#endif
6358#ifdef FEAT_SESSION
6359 || p == (char_u *)&p_vdir
6360#endif
6361 )
6362 {
6363 xp->xp_context = EXPAND_DIRECTORIES;
6364 if (p == (char_u *)&p_path
6365#ifdef FEAT_SEARCHPATH
6366 || p == (char_u *)&p_cdpath
6367#endif
6368 )
6369 xp->xp_backslash = XP_BS_THREE;
6370 else
6371 xp->xp_backslash = XP_BS_ONE;
6372 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006373 else if (p == (char_u *)&p_ft)
6374 {
6375 xp->xp_context = EXPAND_FILETYPE;
6376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 else
6378 {
6379 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006380 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (p == (char_u *)&p_tags)
6382 xp->xp_backslash = XP_BS_THREE;
6383 else
6384 xp->xp_backslash = XP_BS_ONE;
6385 }
6386 }
6387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006388 // For an option that is a list of file names, find the start of the
6389 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6391 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006392 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (*p == ' ' || *p == ',')
6394 {
6395 s = p;
6396 while (s > xp->xp_pattern && *(s - 1) == '\\')
6397 --s;
6398 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6399 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6400 {
6401 xp->xp_pattern = p + 1;
6402 break;
6403 }
6404 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006405
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006406#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006407 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006408 if (options[opt_idx].var == (char_u *)&p_sps
6409 && STRNCMP(p, "file:", 5) == 0)
6410 {
6411 xp->xp_pattern = p + 5;
6412 break;
6413 }
6414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416}
6417
6418 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006419ExpandSettings(
6420 expand_T *xp,
6421 regmatch_T *regmatch,
6422 int *num_file,
6423 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006425 int num_normal = 0; // Nr of matching non-term-code settings
6426 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 int opt_idx;
6428 int match;
6429 int count = 0;
6430 char_u *str;
6431 int loop;
6432 int is_term_opt;
6433 char_u name_buf[MAX_KEY_NAME_LEN];
6434 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006435 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006437 // do this loop twice:
6438 // loop == 0: count the number of matching options
6439 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 for (loop = 0; loop <= 1; ++loop)
6441 {
6442 regmatch->rm_ic = ic;
6443 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6444 {
K.Takataeeec2542021-06-02 13:28:16 +02006445 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6447 {
6448 if (loop == 0)
6449 num_normal++;
6450 else
6451 (*file)[count++] = vim_strsave((char_u *)names[match]);
6452 }
6453 }
6454 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6455 opt_idx++)
6456 {
6457 if (options[opt_idx].var == NULL)
6458 continue;
6459 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6460 && !(options[opt_idx].flags & P_BOOL))
6461 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006462 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (is_term_opt && num_normal > 0)
6464 continue;
6465 match = FALSE;
6466 if (vim_regexec(regmatch, str, (colnr_T)0)
6467 || (options[opt_idx].shortname != NULL
6468 && vim_regexec(regmatch,
6469 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6470 match = TRUE;
6471 else if (is_term_opt)
6472 {
6473 name_buf[0] = '<';
6474 name_buf[1] = 't';
6475 name_buf[2] = '_';
6476 name_buf[3] = str[2];
6477 name_buf[4] = str[3];
6478 name_buf[5] = '>';
6479 name_buf[6] = NUL;
6480 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6481 {
6482 match = TRUE;
6483 str = name_buf;
6484 }
6485 }
6486 if (match)
6487 {
6488 if (loop == 0)
6489 {
6490 if (is_term_opt)
6491 num_term++;
6492 else
6493 num_normal++;
6494 }
6495 else
6496 (*file)[count++] = vim_strsave(str);
6497 }
6498 }
6499 /*
6500 * Check terminal key codes, these are not in the option table
6501 */
6502 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6503 {
6504 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6505 {
6506 if (!isprint(str[0]) || !isprint(str[1]))
6507 continue;
6508
6509 name_buf[0] = 't';
6510 name_buf[1] = '_';
6511 name_buf[2] = str[0];
6512 name_buf[3] = str[1];
6513 name_buf[4] = NUL;
6514
6515 match = FALSE;
6516 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6517 match = TRUE;
6518 else
6519 {
6520 name_buf[0] = '<';
6521 name_buf[1] = 't';
6522 name_buf[2] = '_';
6523 name_buf[3] = str[0];
6524 name_buf[4] = str[1];
6525 name_buf[5] = '>';
6526 name_buf[6] = NUL;
6527
6528 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6529 match = TRUE;
6530 }
6531 if (match)
6532 {
6533 if (loop == 0)
6534 num_term++;
6535 else
6536 (*file)[count++] = vim_strsave(name_buf);
6537 }
6538 }
6539
6540 /*
6541 * Check special key names.
6542 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006543 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6545 {
6546 name_buf[0] = '<';
6547 STRCPY(name_buf + 1, str);
6548 STRCAT(name_buf, ">");
6549
6550 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6551 {
6552 if (loop == 0)
6553 num_term++;
6554 else
6555 (*file)[count++] = vim_strsave(name_buf);
6556 }
6557 }
6558 }
6559 if (loop == 0)
6560 {
6561 if (num_normal > 0)
6562 *num_file = num_normal;
6563 else if (num_term > 0)
6564 *num_file = num_term;
6565 else
6566 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006567 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 if (*file == NULL)
6569 {
6570 *file = (char_u **)"";
6571 return FAIL;
6572 }
6573 }
6574 }
6575 return OK;
6576}
6577
6578 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006579ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006581 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 char_u *buf;
6583
6584 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006585 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (*file == NULL)
6587 return FAIL;
6588
6589 /*
6590 * For a terminal key code expand_option_idx is < 0.
6591 */
6592 if (expand_option_idx < 0)
6593 {
6594 var = find_termcode(expand_option_name + 2);
6595 if (var == NULL)
6596 expand_option_idx = findoption(expand_option_name);
6597 }
6598
6599 if (expand_option_idx >= 0)
6600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006601 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 option_value2string(&options[expand_option_idx], expand_option_flags);
6603 var = NameBuff;
6604 }
6605 else if (var == NULL)
6606 var = (char_u *)"";
6607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006608 // A backslash is required before some characters. This is the reverse of
6609 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 buf = vim_strsave_escaped(var, escape_chars);
6611
6612 if (buf == NULL)
6613 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006614 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 return FAIL;
6616 }
6617
6618#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006619 // For MS-Windows et al. we don't double backslashes at the start and
6620 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006621 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 if (var[0] == '\\' && var[1] == '\\'
6623 && expand_option_idx >= 0
6624 && (options[expand_option_idx].flags & P_EXPAND)
6625 && vim_isfilec(var[2])
6626 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006627 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628#endif
6629
6630 *file[0] = buf;
6631 *num_file = 1;
6632 return OK;
6633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635/*
6636 * Get the value for the numeric or string option *opp in a nice format into
6637 * NameBuff[]. Must not be called with a hidden option!
6638 */
6639 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006640option_value2string(
6641 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006642 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 char_u *varp;
6645
6646 varp = get_varp_scope(opp, opt_flags);
6647
6648 if (opp->flags & P_NUM)
6649 {
6650 long wc = 0;
6651
6652 if (wc_use_keyname(varp, &wc))
6653 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6654 else if (wc != 0)
6655 STRCPY(NameBuff, transchar((int)wc));
6656 else
6657 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6658 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006659 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
6661 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006662 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 NameBuff[0] = NUL;
6664#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006665 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006666 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 STRCPY(NameBuff, "*****");
6668#endif
6669 else if (opp->flags & P_EXPAND)
6670 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006671 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 else if ((char_u **)opp->var == &p_pt)
6673 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6674 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006675 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 }
6677}
6678
6679/*
6680 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6681 * printed as a keyname.
6682 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6683 */
6684 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006685wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6688 {
6689 *wcp = *(long *)varp;
6690 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6691 return TRUE;
6692 }
6693 return FALSE;
6694}
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 * Return TRUE if "x" is present in 'shortmess' option, or
6698 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6699 */
6700 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006703 return p_shm != NULL &&
6704 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 || (vim_strchr(p_shm, 'a') != NULL
6706 && vim_strchr((char_u *)SHM_A, x) != NULL));
6707}
6708
6709/*
6710 * paste_option_changed() - Called after p_paste was set or reset.
6711 */
6712 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006713paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 static int old_p_paste = FALSE;
6716 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006717 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718#ifdef FEAT_CMDL_INFO
6719 static int save_ru = 0;
6720#endif
6721#ifdef FEAT_RIGHTLEFT
6722 static int save_ri = 0;
6723 static int save_hkmap = 0;
6724#endif
6725 buf_T *buf;
6726
6727 if (p_paste)
6728 {
6729 /*
6730 * Paste switched from off to on.
6731 * Save the current values, so they can be restored later.
6732 */
6733 if (!old_p_paste)
6734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006735 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006736 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
6738 buf->b_p_tw_nopaste = buf->b_p_tw;
6739 buf->b_p_wm_nopaste = buf->b_p_wm;
6740 buf->b_p_sts_nopaste = buf->b_p_sts;
6741 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006742 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006743#ifdef FEAT_VARTABS
6744 if (buf->b_p_vsts_nopaste)
6745 vim_free(buf->b_p_vsts_nopaste);
6746 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6747 ? vim_strsave(buf->b_p_vsts) : NULL;
6748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
6750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006751 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006753 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754#ifdef FEAT_CMDL_INFO
6755 save_ru = p_ru;
6756#endif
6757#ifdef FEAT_RIGHTLEFT
6758 save_ri = p_ri;
6759 save_hkmap = p_hkmap;
6760#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006761 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006762 p_ai_nopaste = p_ai;
6763 p_et_nopaste = p_et;
6764 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 p_tw_nopaste = p_tw;
6766 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006767#ifdef FEAT_VARTABS
6768 if (p_vsts_nopaste)
6769 vim_free(p_vsts_nopaste);
6770 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 }
6773
6774 /*
6775 * Always set the option values, also when 'paste' is set when it is
6776 * already on.
6777 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006778 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006779 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006781 buf->b_p_tw = 0; // textwidth is 0
6782 buf->b_p_wm = 0; // wrapmargin is 0
6783 buf->b_p_sts = 0; // softtabstop is 0
6784 buf->b_p_ai = 0; // no auto-indent
6785 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006786#ifdef FEAT_VARTABS
6787 if (buf->b_p_vsts)
6788 free_string_option(buf->b_p_vsts);
6789 buf->b_p_vsts = empty_option;
6790 if (buf->b_p_vsts_array)
6791 vim_free(buf->b_p_vsts_array);
6792 buf->b_p_vsts_array = 0;
6793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 }
6795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006796 // set global options
6797 p_sm = 0; // no showmatch
6798 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006801 status_redraw_all(); // redraw to remove the ruler
6802 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803#endif
6804#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 p_ri = 0; // no reverse insert
6806 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006808 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 p_tw = 0;
6810 p_wm = 0;
6811 p_sts = 0;
6812 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006813#ifdef FEAT_VARTABS
6814 if (p_vsts)
6815 free_string_option(p_vsts);
6816 p_vsts = empty_option;
6817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
6819
6820 /*
6821 * Paste switched from on to off: Restore saved values.
6822 */
6823 else if (old_p_paste)
6824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006825 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006826 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
6828 buf->b_p_tw = buf->b_p_tw_nopaste;
6829 buf->b_p_wm = buf->b_p_wm_nopaste;
6830 buf->b_p_sts = buf->b_p_sts_nopaste;
6831 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006832 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006833#ifdef FEAT_VARTABS
6834 if (buf->b_p_vsts)
6835 free_string_option(buf->b_p_vsts);
6836 buf->b_p_vsts = buf->b_p_vsts_nopaste
6837 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6838 if (buf->b_p_vsts_array)
6839 vim_free(buf->b_p_vsts_array);
6840 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006841 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006842 else
6843 buf->b_p_vsts_array = 0;
6844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006847 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006849 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006852 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 p_ru = save_ru;
6854#endif
6855#ifdef FEAT_RIGHTLEFT
6856 p_ri = save_ri;
6857 p_hkmap = save_hkmap;
6858#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006859 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006860 p_ai = p_ai_nopaste;
6861 p_et = p_et_nopaste;
6862 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 p_tw = p_tw_nopaste;
6864 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006865#ifdef FEAT_VARTABS
6866 if (p_vsts)
6867 free_string_option(p_vsts);
6868 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871
6872 old_p_paste = p_paste;
6873}
6874
6875/*
6876 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6877 *
6878 * Reset 'compatible' and set the values for options that didn't get set yet
6879 * to the Vim defaults.
6880 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006881 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 */
6883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006884vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006886 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006887 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006888 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
6890 if (!option_was_set((char_u *)"cp"))
6891 {
6892 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006893 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6895 set_option_default(opt_idx, OPT_FREE, FALSE);
6896 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006897 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006899
6900 if (fname != NULL)
6901 {
6902 p = vim_getenv(envname, &dofree);
6903 if (p == NULL)
6904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006906 p = FullName_save(fname, FALSE);
6907 if (p != NULL)
6908 {
6909 vim_setenv(envname, p);
6910 vim_free(p);
6911 }
6912 }
6913 else if (dofree)
6914 vim_free(p);
6915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916}
6917
6918/*
6919 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6920 */
6921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006922change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006924 int opt_idx;
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 if (p_cp != on)
6927 {
6928 p_cp = on;
6929 compatible_set();
6930 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006931 opt_idx = findoption((char_u *)"cp");
6932 if (opt_idx >= 0)
6933 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934}
6935
6936/*
6937 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006938 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 */
6940 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006941option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 int idx;
6944
6945 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006946 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 return FALSE;
6948 if (options[idx].flags & P_WAS_SET)
6949 return TRUE;
6950 return FALSE;
6951}
6952
6953/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006954 * Reset the flag indicating option "name" was set.
6955 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006956 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006957reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006958{
6959 int idx = findoption(name);
6960
6961 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006962 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006963 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006964 return OK;
6965 }
6966 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006967}
6968
6969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 * compatible_set() - Called when 'compatible' has been set or unset.
6971 *
6972 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6973 * flag) to a Vi compatible value.
6974 * When 'compatible' is unset: Set all options that have a different default
6975 * for Vim (without the P_VI_DEF flag) to that default.
6976 */
6977 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006978compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980 int opt_idx;
6981
Bram Moolenaardac13472019-09-16 21:06:21 +02006982 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6984 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6985 set_option_default(opt_idx, OPT_FREE, p_cp);
6986 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006987 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988}
6989
Bram Moolenaardac13472019-09-16 21:06:21 +02006990#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992/*
6993 * fill_breakat_flags() -- called when 'breakat' changes value.
6994 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006996fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006998 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 int i;
7000
7001 for (i = 0; i < 256; i++)
7002 breakat_flags[i] = FALSE;
7003
7004 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007005 for (p = p_breakat; *p; p++)
7006 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008#endif
7009
7010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 * Check if backspacing over something is allowed.
7012 */
7013 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007014can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007015 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007017#ifdef FEAT_JOB_CHANNEL
7018 if (what == BS_START && bt_prompt(curbuf))
7019 return FALSE;
7020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 switch (*p_bs)
7022 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007023 case '3': return TRUE;
7024 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 case '1': return (what != BS_START);
7026 case '0': return FALSE;
7027 }
7028 return vim_strchr(p_bs, what) != NULL;
7029}
7030
7031/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007032 * Return the effective 'scrolloff' value for the current window, using the
7033 * global value when appropriate.
7034 */
7035 long
7036get_scrolloff_value(void)
7037{
7038 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7039}
7040
7041/*
7042 * Return the effective 'sidescrolloff' value for the current window, using the
7043 * global value when appropriate.
7044 */
7045 long
7046get_sidescrolloff_value(void)
7047{
7048 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7049}
7050
7051/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007052 * Get the local or global value of 'backupcopy'.
7053 */
7054 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007055get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007056{
7057 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7058}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007059
Gary Johnson53ba05b2021-07-26 22:19:10 +02007060/*
7061 * Get the local or global value of the 'virtualedit' flags.
7062 */
7063 unsigned int
7064get_ve_flags(void)
7065{
Gary Johnson51ad8502021-08-03 18:33:08 +02007066 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7067 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007068}
7069
Bram Moolenaaree857022019-11-09 23:26:40 +01007070#if defined(FEAT_LINEBREAK) || defined(PROTO)
7071/*
7072 * Get the local or global value of 'showbreak'.
7073 */
7074 char_u *
7075get_showbreak_value(win_T *win)
7076{
7077 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7078 return p_sbr;
7079 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7080 return empty_option;
7081 return win->w_p_sbr;
7082}
7083#endif
7084
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007085#if defined(FEAT_EVAL) || defined(PROTO)
7086/*
7087 * Get window or buffer local options.
7088 */
7089 dict_T *
7090get_winbuf_options(int bufopt)
7091{
7092 dict_T *d;
7093 int opt_idx;
7094
7095 d = dict_alloc();
7096 if (d == NULL)
7097 return NULL;
7098
Bram Moolenaardac13472019-09-16 21:06:21 +02007099 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007100 {
7101 struct vimoption *opt = &options[opt_idx];
7102
7103 if ((bufopt && (opt->indir & PV_BUF))
7104 || (!bufopt && (opt->indir & PV_WIN)))
7105 {
7106 char_u *varp = get_varp(opt);
7107
7108 if (varp != NULL)
7109 {
7110 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007111 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007112 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007113 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007114 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007115 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007116 }
7117 }
7118 }
7119
7120 return d;
7121}
7122#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007123
Bram Moolenaardac13472019-09-16 21:06:21 +02007124#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007125/*
7126 * This is called when 'culopt' is changed
7127 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007128 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007129fill_culopt_flags(char_u *val, win_T *wp)
7130{
7131 char_u *p;
7132 char_u culopt_flags_new = 0;
7133
7134 if (val == NULL)
7135 p = wp->w_p_culopt;
7136 else
7137 p = val;
7138 while (*p != NUL)
7139 {
7140 if (STRNCMP(p, "line", 4) == 0)
7141 {
7142 p += 4;
7143 culopt_flags_new |= CULOPT_LINE;
7144 }
7145 else if (STRNCMP(p, "both", 4) == 0)
7146 {
7147 p += 4;
7148 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7149 }
7150 else if (STRNCMP(p, "number", 6) == 0)
7151 {
7152 p += 6;
7153 culopt_flags_new |= CULOPT_NBR;
7154 }
7155 else if (STRNCMP(p, "screenline", 10) == 0)
7156 {
7157 p += 10;
7158 culopt_flags_new |= CULOPT_SCRLINE;
7159 }
7160
7161 if (*p != ',' && *p != NUL)
7162 return FAIL;
7163 if (*p == ',')
7164 ++p;
7165 }
7166
7167 // Can't have both "line" and "screenline".
7168 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7169 return FAIL;
7170 wp->w_p_culopt_flags = culopt_flags_new;
7171
7172 return OK;
7173}
7174#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007175
7176/*
7177 * Get the value of 'magic' adjusted for Vim9 script.
7178 */
7179 int
7180magic_isset(void)
7181{
7182 switch (magic_overruled)
7183 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007184 case OPTION_MAGIC_ON: return TRUE;
7185 case OPTION_MAGIC_OFF: return FALSE;
7186 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007187 }
7188#ifdef FEAT_EVAL
7189 if (in_vim9script())
7190 return TRUE;
7191#endif
7192 return p_magic;
7193}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007194
7195/*
7196 * Set the callback function value for an option that accepts a function name,
7197 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7198 * Returns OK if the option is successfully set to a function, otherwise
7199 * returns FAIL.
7200 */
7201 int
7202option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7203{
7204#ifdef FEAT_EVAL
7205 typval_T *tv;
7206 callback_T cb;
7207
7208 if (optval == NULL || *optval == NUL)
7209 {
7210 free_callback(optcb);
7211 return OK;
7212 }
7213
7214 if (*optval == '{'
7215 || (STRNCMP(optval, "function(", 9) == 0)
7216 || (STRNCMP(optval, "funcref(", 8) == 0))
7217 // Lambda expression or a funcref
7218 tv = eval_expr(optval, NULL);
7219 else
7220 // treat everything else as a function name string
7221 tv = alloc_string_tv(vim_strsave(optval));
7222 if (tv == NULL)
7223 return FAIL;
7224
7225 cb = get_callback(tv);
7226 if (cb.cb_name == NULL)
7227 {
7228 free_tv(tv);
7229 return FAIL;
7230 }
7231
7232 free_callback(optcb);
7233 set_callback(optcb, &cb);
7234 free_tv(tv);
7235 return OK;
7236#else
7237 return FAIL;
7238#endif
7239}