blob: 44a0520ad02aee1b00bdfc458afaf790ca27aa0d [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);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000059static char_u *get_varp_scope(struct vimoption *p, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010060static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +000062static void option_value2string(struct vimoption *, int scope);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010063static 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 Moolenaar424bcae2022-01-31 14:59:41 +0000269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || 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
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000278# ifdef MAC
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 (char_u *)"mac-roman"
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000280# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 (char_u *)"hp-roman8"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# endif
283# endif
284# endif
285 );
286#endif
287
288#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100289 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100291# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000292 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293# else
294# ifdef VMS
295 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
296
297# else
298 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
299# endif
300# endif
301 );
302#endif
303
304 /*
305 * Set all the options (except the terminal options) to their default
306 * value. Also set the global value for local options.
307 */
308 set_options_default(0);
309
matveytadbb1bf2022-02-01 17:26:12 +0000310#ifdef UNIX
311 // Force restricted-mode on for "nologin" or "false" $SHELL
312 p = get_isolated_shell_name();
313 if (p != NULL)
314 {
315 if (fnamecmp(p, "nologin") == 0 || fnamecmp(p, "false") == 0)
316 restricted = TRUE;
317 vim_free(p);
318 }
319#endif
320
Bram Moolenaar07268702018-03-01 21:57:32 +0100321#ifdef CLEAN_RUNTIMEPATH
322 if (clean_arg)
323 {
324 opt_idx = findoption((char_u *)"runtimepath");
325 if (opt_idx >= 0)
326 {
327 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
328 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
329 }
330 opt_idx = findoption((char_u *)"packpath");
331 if (opt_idx >= 0)
332 {
333 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
334 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
335 }
336 }
337#endif
338
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#ifdef FEAT_GUI
340 if (found_reverse_arg)
341 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
342#endif
343
344 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100345 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100346 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 check_buf_options(curbuf);
348 check_win_options(curwin);
349 check_options();
350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100351 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 didset_options();
353
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000354#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100355 // Use the current chartab for the generic chartab. This is not in
356 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000357 init_spell_chartab();
358#endif
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 /*
361 * Expand environment variables and things like "~" for the defaults.
362 * If option_expand() returns non-NULL the variable is expanded. This can
363 * only happen for non-indirect options.
364 * Also set the default to the expanded value, so ":set" does not list
365 * them.
366 * Don't set the P_ALLOCED flag, because we don't want to free the
367 * default.
368 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200369 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {
371 if ((options[opt_idx].flags & P_GETTEXT)
372 && options[opt_idx].var != NULL)
373 p = (char_u *)_(*(char **)options[opt_idx].var);
374 else
375 p = option_expand(opt_idx, NULL);
376 if (p != NULL && (p = vim_strsave(p)) != NULL)
377 {
378 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379 // VIMEXP
380 // Defaults for all expanded options are currently the same for Vi
381 // and Vim. When this changes, add some code here! Also need to
382 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 if (options[opt_idx].flags & P_DEF_ALLOCED)
384 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
385 options[opt_idx].def_val[VI_DEFAULT] = p;
386 options[opt_idx].flags |= P_DEF_ALLOCED;
387 }
388 }
389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100390 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100393 // Detect use of mlterm.
394 // Mlterm is a terminal emulator akin to xterm that has some special
395 // abilities (bidi namely).
396 // NOTE: mlterm's author is being asked to 'set' a variable
397 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (mch_getenv((char_u *)"MLTERM") != NULL)
399 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
400#endif
401
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200402 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
Bram Moolenaar4f974752019-02-17 17:44:42 +0100404# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 /*
406 * If $LANG isn't set, try to get a good value for it. This makes the
407 * right language be used automatically. Don't do this for English.
408 */
409 if (mch_getenv((char_u *)"LANG") == NULL)
410 {
411 char buf[20];
412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
414 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
415 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
417 (LPTSTR)buf, 20);
418 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
419 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100420 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
422 STRCPY(buf, "zh_TW");
423 else if (STRNICMP(buf, "chs", 3) == 0
424 || STRNICMP(buf, "zhc", 3) == 0)
425 STRCPY(buf, "zh_CN");
426 else if (STRNICMP(buf, "jp", 2) == 0)
427 STRCPY(buf, "ja");
428 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100429 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100430 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000433# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000434# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100435 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000436 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438# endif
439
K.Takataf883d902021-05-30 18:04:19 +0200440# ifdef MSWIN
441 // MS-Windows has builtin support for conversion to and from Unicode, using
442 // "utf-8" for 'encoding' should work best for most users.
443 p = vim_strsave((char_u *)ENC_DFLT);
444# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100445 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200446 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (p != NULL)
450 {
451 char_u *save_enc;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200454 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 save_enc = p_enc;
456 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000457 if (STRCMP(p_enc, "gb18030") == 0)
458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100459 // We don't support "gb18030", but "cp936" is a good substitute
460 // for practical purposes, thus use that. It's not an alias to
461 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000462 p_enc = vim_strsave((char_u *)"cp936");
463 vim_free(p);
464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (mb_init() == NULL)
466 {
467 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000468 if (opt_idx >= 0)
469 {
470 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
471 options[opt_idx].flags |= P_DEF_ALLOCED;
472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaard0573012017-10-28 21:11:06 +0200474#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100475 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100477 // Adjust the default for 'isprint' and 'iskeyword' to match
478 // latin1. Also set the defaults for when 'nocompatible' is
479 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000480 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000481 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000482 set_string_option_direct((char_u *)"isk", -1,
483 ISK_LATIN1, OPT_FREE, SID_NONE);
484 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000485 if (opt_idx >= 0)
486 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000487 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000488 if (opt_idx >= 0)
489 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000490 (void)init_chartab();
491 }
492#endif
493
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200494#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 // Win32 console: When GetACP() returns a different value from
496 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200497 if (
498# ifdef VIMDLL
499 (!gui.in_use && !gui.starting) &&
500# endif
501 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {
503 char buf[50];
504
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100505 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
506 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100507 if (GetConsoleCP() == 0)
508 sprintf(buf, "cp%ld", (long)GetACP());
509 else
510 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 p_tenc = vim_strsave((char_u *)buf);
512 if (p_tenc != NULL)
513 {
514 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000515 if (opt_idx >= 0)
516 {
517 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
518 options[opt_idx].flags |= P_DEF_ALLOCED;
519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 convert_setup(&input_conv, p_tenc, p_enc);
521 convert_setup(&output_conv, p_enc, p_tenc);
522 }
523 else
524 p_tenc = empty_option;
525 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100526#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100527#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100528 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000529 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 }
532 else
533 {
534 vim_free(p_enc);
535 p_enc = save_enc;
536 }
537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100540 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 set_helplang_default(get_mess_lang());
542#endif
543}
544
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200545static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
546
547/*
548 * Set the "fileencodings" option to the default value for when 'encoding' is
549 * utf-8.
550 */
551 void
552set_fencs_unicode()
553{
554 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
555 OPT_FREE, 0);
556}
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558/*
559 * Set an option to its default value.
560 * This does not take care of side effects!
561 */
562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100563set_option_default(
564 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100565 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
566 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100568 char_u *varp; // pointer to variable for current option
569 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000571 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
573
574 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
575 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100576 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
578 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
579 if (flags & P_STRING)
580 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200581 // 'fencs' default value depends on 'encoding'
582 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
583 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100584 // Use set_string_option_direct() for local options to handle
585 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200586 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200587 set_string_option_direct(NULL, opt_idx,
588 options[opt_idx].def_val[dvi], opt_flags, 0);
589 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200591 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
592 free_string_option(*(char_u **)(varp));
593 *(char_u **)varp = options[opt_idx].def_val[dvi];
594 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 }
596 }
597 else if (flags & P_NUM)
598 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000599 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 win_comp_scroll(curwin);
601 else
602 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100603 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
604
605 if ((long *)varp == &curwin->w_p_so
606 || (long *)varp == &curwin->w_p_siso)
607 // 'scrolloff' and 'sidescrolloff' local values have a
608 // different default value than the global default.
609 *(long *)varp = -1;
610 else
611 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100612 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (both)
614 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100615 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100618 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100620 // the cast to long is required for Manx C, long_i is needed for
621 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000622 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000623#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100624 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000625 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
626 *(int *)varp = FALSE;
627#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (both)
630 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
631 *(int *)varp;
632 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100634 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000635 flagsp = insecure_flag(opt_idx, opt_flags);
636 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638
639#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200640 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#endif
642}
643
644/*
645 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200646 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 */
648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100649set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100650 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000654 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaardac13472019-09-16 21:06:21 +0200656 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200657 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200658 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100659 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200660# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200661 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200662 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200663# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100664 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 set_option_default(i, opt_flags, p_cp);
666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000668 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200670#ifdef FEAT_CINDENT
671 parse_cino(curbuf);
672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673}
674
675/*
676 * Set the Vi-default value of a string option.
677 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100678 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100680 static void
681set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
683 char_u *p;
684 int opt_idx;
685
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100686 if (escape && vim_strchr(val, ' ') != NULL)
687 p = vim_strsave_escaped(val, (char_u *)" ");
688 else
689 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100690 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
692 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000693 if (opt_idx >= 0)
694 {
695 if (options[opt_idx].flags & P_DEF_ALLOCED)
696 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
697 options[opt_idx].def_val[VI_DEFAULT] = p;
698 options[opt_idx].flags |= P_DEF_ALLOCED;
699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
701}
702
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100703 void
704set_string_default(char *name, char_u *val)
705{
706 set_string_default_esc(name, val, FALSE);
707}
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200710 * For an option value that contains comma separated items, find "newval" in
711 * "origval". Return NULL if not found.
712 */
713 static char_u *
714find_dup_item(char_u *origval, char_u *newval, long_u flags)
715{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200716 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200717 size_t newlen;
718 char_u *s;
719
720 if (origval == NULL)
721 return NULL;
722
723 newlen = STRLEN(newval);
724 for (s = origval; *s != NUL; ++s)
725 {
726 if ((!(flags & P_COMMA)
727 || s == origval
728 || (s[-1] == ',' && !(bs & 1)))
729 && STRNCMP(s, newval, newlen) == 0
730 && (!(flags & P_COMMA)
731 || s[newlen] == ','
732 || s[newlen] == NUL))
733 return s;
734 // Count backslashes. Only a comma with an even number of backslashes
735 // or a single backslash preceded by a comma before it is recognized as
736 // a separator.
737 if ((s > origval + 1
738 && s[-1] == '\\'
739 && s[-2] != ',')
740 || (s == origval + 1
741 && s[-1] == '\\'))
742 ++bs;
743 else
744 bs = 0;
745 }
746 return NULL;
747}
748
749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 * Set the Vi-default value of a number option.
751 * Used for 'lines' and 'columns'.
752 */
753 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100754set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000756 int opt_idx;
757
758 opt_idx = findoption((char_u *)name);
759 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000760 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761}
762
Dominique Pelle748b3082022-01-08 12:41:16 +0000763#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200764/*
765 * Set all window-local and buffer-local options to the Vim default.
766 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200767 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200768 */
769 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200770set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200771{
772 win_T *save_curwin = curwin;
773 int i;
774
775 curwin = wp;
776 curbuf = curwin->w_buffer;
777 block_autocmds();
778
Bram Moolenaardac13472019-09-16 21:06:21 +0200779 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200780 {
781 struct vimoption *p = &(options[i]);
782 char_u *varp = get_varp_scope(p, OPT_LOCAL);
783
784 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200785 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200786 && !(options[i].flags & P_NODEFAULT)
787 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200788 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200789 }
790
791 unblock_autocmds();
792 curwin = save_curwin;
793 curbuf = curwin->w_buffer;
794}
Dominique Pelle748b3082022-01-08 12:41:16 +0000795#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200796
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000797#if defined(EXITFREE) || defined(PROTO)
798/*
799 * Free all options.
800 */
801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000803{
804 int i;
805
Bram Moolenaardac13472019-09-16 21:06:21 +0200806 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000807 {
808 if (options[i].indir == PV_NONE)
809 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100810 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100811 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000812 free_string_option(*(char_u **)options[i].var);
813 if (options[i].flags & P_DEF_ALLOCED)
814 free_string_option(options[i].def_val[VI_DEFAULT]);
815 }
816 else if (options[i].var != VAR_WIN
817 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100818 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200819 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000820 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000821 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000822 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000823}
824#endif
825
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827/*
828 * Initialize the options, part two: After getting Rows and Columns and
829 * setting 'term'.
830 */
831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100832set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 int idx;
835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100837 * 'scroll' defaults to half the window height. The stored default is zero,
838 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000840 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000841 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000842 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 comp_col();
844
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000845 /*
846 * 'window' is only for backwards compatibility with Vi.
847 * Default is Rows - 1.
848 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000849 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000850 p_window = Rows - 1;
851 set_number_default("window", Rows - 1);
852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100854#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000855 /*
856 * If 'background' wasn't set by the user, try guessing the value,
857 * depending on the terminal name. Only need to check for terminals
858 * with a dark background, that can handle color.
859 */
860 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000861 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
862 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000864 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 // don't mark it as set, when starting the GUI it may be
866 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000867 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000870
871#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100872 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000873#endif
874#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000876#endif
877#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880}
881
882/*
883 * Initialize the options, part three: After reading the .vimrc
884 */
885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100886set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100888#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889/*
890 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
891 * This is done after other initializations, where 'shell' might have been
892 * set, but only if they have not been set before.
893 */
894 char_u *p;
895 int idx_srr;
896 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100897# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 int idx_sp;
899 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000903 if (idx_srr < 0)
904 do_srr = FALSE;
905 else
906 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100907# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000909 if (idx_sp < 0)
910 do_sp = FALSE;
911 else
912 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100913# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200914 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (p != NULL)
916 {
917 /*
918 * Default for p_sp is "| tee", for p_srr is ">".
919 * For known shells it is changed here to include stderr.
920 */
921 if ( fnamecmp(p, "csh") == 0
922 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100923# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 || fnamecmp(p, "csh.exe") == 0
925 || fnamecmp(p, "tcsh.exe") == 0
926# endif
927 )
928 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100929# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (do_sp)
931 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100932# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100934# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
938 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (do_srr)
941 {
942 p_srr = (char_u *)">&";
943 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
944 }
945 }
Mike Williams12795022021-06-28 20:53:58 +0200946# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200947 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
948 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200949 else if ( fnamecmp(p, "powershell") == 0
950 || fnamecmp(p, "powershell.exe") == 0
951 )
952 {
953# if defined(FEAT_QUICKFIX)
954 if (do_sp)
955 {
956 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
957 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
958 }
959# endif
960 if (do_srr)
961 {
962 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
963 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
964 }
965 }
966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 else
Natanael Copa56318362021-05-06 18:46:35 +0200968 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if ( fnamecmp(p, "sh") == 0
970 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200971 || fnamecmp(p, "mksh") == 0
972 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000974 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200976 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200977 || fnamecmp(p, "ash") == 0
978 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200979 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100980# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 || fnamecmp(p, "cmd") == 0
982 || fnamecmp(p, "sh.exe") == 0
983 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200984 || fnamecmp(p, "mksh.exe") == 0
985 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000987 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 || fnamecmp(p, "bash.exe") == 0
989 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200990 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200991 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100993 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100995# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (do_sp)
997 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100998# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001000# else
Mike Williamsa3d1b292021-06-30 20:56:00 +02001001 if (fnamecmp(p, "pwsh") == 0)
1002 p_sp = (char_u *)">%s 2>&1";
1003 else
1004 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1007 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (do_srr)
1010 {
1011 p_srr = (char_u *)">%s 2>&1";
1012 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1013 }
1014 }
1015 vim_free(p);
1016 }
1017#endif
1018
Bram Moolenaar4f974752019-02-17 17:44:42 +01001019#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001021 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1022 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001024 * set, but only if they have not been set before.
1025 * Default values depend on shell (cmd.exe is default shell):
1026 *
1027 * p_shcf p_sxq
1028 * cmd.exe - "/c" "("
1029 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001030 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001031 * "sh" like shells - "-c" "\""
1032 *
1033 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 */
Mike Williams12795022021-06-28 20:53:58 +02001035 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1036 {
1037 int idx_opt;
1038
1039 idx_opt = findoption((char_u *)"shcf");
1040 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1041 {
1042 p_shcf = (char_u*)"-Command";
1043 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1044 }
1045
1046 idx_opt = findoption((char_u *)"sxq");
1047 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1048 {
1049 p_sxq = (char_u*)"\"";
1050 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1051 }
1052 }
1053 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
1055 int idx3;
1056
1057 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001058 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 p_shcf = (char_u *)"-c";
1061 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1062 }
1063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001066 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
1068 p_sxq = (char_u *)"\"";
1069 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001072 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1073 {
1074 int idx3;
1075
1076 /*
1077 * cmd.exe on Windows will strip the first and last double quote given
1078 * on the command line, e.g. most of the time things like:
1079 * cmd /c "my path/to/echo" "my args to echo"
1080 * become:
1081 * my path/to/echo" "my args to echo
1082 * when executed.
1083 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001084 * To avoid this, set shellxquote to surround the command in
1085 * parenthesis. This appears to make most commands work, without
1086 * breaking commands that worked previously, such as
1087 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001088 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 idx3 = findoption((char_u *)"sxq");
1090 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1091 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001092 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001093 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1094 }
1095
Bram Moolenaara64ba222012-02-12 23:23:31 +01001096 idx3 = findoption((char_u *)"shcf");
1097 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1098 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001099 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001100 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1101 }
1102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103#endif
1104
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001105 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001106 {
1107 int idx_ffs = findoption((char_u *)"ffs");
1108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001109 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001110 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1111 set_fileformat(default_fileformat(), OPT_LOCAL);
1112 }
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
1117#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1118/*
1119 * When 'helplang' is still at its default value, set it to "lang".
1120 * Only the first two characters of "lang" are used.
1121 */
1122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001123set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 int idx;
1126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001127 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 return;
1129 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001130 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
1132 if (options[idx].flags & P_ALLOCED)
1133 free_string_option(p_hlg);
1134 p_hlg = vim_strsave(lang);
1135 if (p_hlg == NULL)
1136 p_hlg = empty_option;
1137 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001138 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001139 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001140 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1141 {
1142 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1143 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1144 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001145 // any C like setting, such as C.UTF-8, becomes "en"
1146 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1147 {
1148 p_hlg[0] = 'e';
1149 p_hlg[1] = 'n';
1150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 options[idx].flags |= P_ALLOCED;
1154 }
1155}
1156#endif
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158/*
1159 * 'title' and 'icon' only default to true if they have not been set or reset
1160 * in .vimrc and we can read the old value.
1161 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1162 * they can be reset. This reduces startup time when using X on a remote
1163 * machine.
1164 */
1165 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001166set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168 int idx1;
1169 long val;
1170
1171 /*
1172 * If GUI is (going to be) used, we can always set the window title and
1173 * icon name. Saves a bit of time, because the X11 display server does
1174 * not need to be contacted.
1175 */
1176 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001177 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179#ifdef FEAT_GUI
1180 if (gui.starting || gui.in_use)
1181 val = TRUE;
1182 else
1183#endif
1184 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001185 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 p_title = val;
1187 }
1188 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001189 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
1191#ifdef FEAT_GUI
1192 if (gui.starting || gui.in_use)
1193 val = TRUE;
1194 else
1195#endif
1196 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001197 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 p_icon = val;
1199 }
1200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001202 void
1203ex_set(exarg_T *eap)
1204{
1205 int flags = 0;
1206
1207 if (eap->cmdidx == CMD_setlocal)
1208 flags = OPT_LOCAL;
1209 else if (eap->cmdidx == CMD_setglobal)
1210 flags = OPT_GLOBAL;
1211#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001212 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001213 ex_options(eap);
1214 else
1215#endif
1216 {
1217 if (eap->forceit)
1218 flags |= OPT_ONECOLUMN;
1219 (void)do_set(eap->arg, flags);
1220 }
1221}
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223/*
1224 * Parse 'arg' for option settings.
1225 *
1226 * 'arg' may be IObuff, but only when no errors can be present and option
1227 * does not need to be expanded with option_expand().
1228 * "opt_flags":
1229 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001230 * OPT_GLOBAL for ":setglobal"
1231 * OPT_LOCAL for ":setlocal" and a modeline
1232 * OPT_MODELINE for a modeline
1233 * OPT_WINONLY to only set window-local options
1234 * OPT_NOWIN to skip setting window-local options
1235 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 *
1237 * returns FAIL if an error is detected, OK otherwise
1238 */
1239 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001240do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001241 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001242 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001244 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001246 char *errmsg;
1247 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001249 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1250 int nextchar; // next non-white char after option name
1251 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 int len;
1253 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001254 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001256 long_u flags; // flags for current option
1257 char_u *varp = NULL; // pointer to variable for current option
1258 int did_show = FALSE; // already showed one value
1259 int adding; // "opt+=arg"
1260 int prepending; // "opt^=arg"
1261 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int cp_val = 0;
1263 char_u key_name[2];
1264
1265 if (*arg == NUL)
1266 {
1267 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001268 did_show = TRUE;
1269 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001272 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
1274 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001277 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1278 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
1280 /*
1281 * ":set all" show all options.
1282 * ":set all&" set all options to their default value.
1283 */
1284 arg += 3;
1285 if (*arg == '&')
1286 {
1287 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001288 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001290 didset_options();
1291 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001292 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001297 did_show = TRUE;
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001300 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
1302 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001303 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001304 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 arg += 7;
1306 }
1307 else
1308 {
1309 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001310 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
1312 prefix = 0;
1313 arg += 2;
1314 }
1315 else if (STRNCMP(arg, "inv", 3) == 0)
1316 {
1317 prefix = 2;
1318 arg += 3;
1319 }
1320
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001321 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 key = 0;
1323 if (*arg == '<')
1324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001326 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1328 len = 5;
1329 else
1330 {
1331 len = 1;
1332 while (arg[len] != NUL && arg[len] != '>')
1333 ++len;
1334 }
1335 if (arg[len] != '>')
1336 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001337 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 goto skip;
1339 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001340 arg[len] = NUL; // put NUL after name
1341 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001343 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001345 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
1347 else
1348 {
1349 len = 0;
1350 /*
1351 * The two characters after "t_" may not be alphanumeric.
1352 */
1353 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1354 len = 4;
1355 else
1356 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1357 ++len;
1358 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001359 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001361 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001363 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001366 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 afterchar = arg[len];
1368
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001369 if (in_vim9script())
1370 {
1371 char_u *p = skipwhite(arg + len);
1372
1373 // disallow white space before =val, +=val, -=val, ^=val
1374 if (p > arg + len && (p[0] == '='
1375 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1376 && p[1] == '=')))
1377 {
1378 errmsg = e_no_white_space_allowed_between_option_and;
1379 arg = p;
1380 startarg = p;
1381 goto skip;
1382 }
1383 }
1384 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001385 // skip white space, allow ":set ai ?", ":set hlsearch !"
1386 while (VIM_ISWHITE(arg[len]))
1387 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 adding = FALSE;
1390 prepending = FALSE;
1391 removing = FALSE;
1392 if (arg[len] != NUL && arg[len + 1] == '=')
1393 {
1394 if (arg[len] == '+')
1395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001396 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ++len;
1398 }
1399 else if (arg[len] == '^')
1400 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 ++len;
1403 }
1404 else if (arg[len] == '-')
1405 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 ++len;
1408 }
1409 }
1410 nextchar = arg[len];
1411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001412 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001414 if (in_vim9script() && arg > arg_start
1415 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1416 errmsg = e_no_white_space_allowed_between_option_and;
1417 else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001418 errmsg = N_(e_unknown_option);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 goto skip;
1420 }
1421
1422 if (opt_idx >= 0)
1423 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001424 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001426 // Only give an error message when requesting the value of
1427 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1429 && (!(options[opt_idx].flags & P_BOOL)
1430 || nextchar == '?'))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001431 errmsg = N_(e_option_not_supported);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 goto skip;
1433 }
1434
1435 flags = options[opt_idx].flags;
1436 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1437 }
1438 else
1439 {
1440 flags = P_STRING;
1441 if (key < 0)
1442 {
1443 key_name[0] = KEY2TERMCAP0(key);
1444 key_name[1] = KEY2TERMCAP1(key);
1445 }
1446 else
1447 {
1448 key_name[0] = KS_KEY;
1449 key_name[1] = (key & 0xff);
1450 }
1451 }
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // Skip all options that are not window-local (used when showing
1454 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001455 if ((opt_flags & OPT_WINONLY)
1456 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1457 goto skip;
1458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001459 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001460 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1461 && options[opt_idx].var == VAR_WIN)
1462 goto skip;
1463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001464 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001465 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001467 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001468 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001469 errmsg = N_(e_not_allowed_in_modeline);
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001470 goto skip;
1471 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001472 if ((flags & P_MLE) && !p_mle)
1473 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001474 errmsg = N_(e_not_allowed_in_modeline_when_modelineexpr_is_off);
Bram Moolenaar110289e2019-05-23 15:38:06 +02001475 goto skip;
1476 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001477#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001478 // In diff mode some options are overruled. This avoids that
1479 // 'foldmethod' becomes "marker" instead of "diff" and that
1480 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001481 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001482 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001483 && (
1484#ifdef FEAT_FOLDING
1485 options[opt_idx].indir == PV_FDM ||
1486#endif
1487 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001488 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491
1492#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001493 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001494 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001496 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 goto skip;
1498 }
1499#endif
1500
1501 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1502 {
1503 arg += len;
1504 cp_val = p_cp;
1505 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001507 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 {
1509 cp_val = FALSE;
1510 arg += 3;
1511 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001512 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 cp_val = TRUE;
1515 arg += 2;
1516 }
1517 }
1518 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001519 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001521 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 goto skip;
1523 }
1524 }
1525
1526 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001527 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001528 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
1530 if (nextchar == '?'
1531 || (prefix == 1
1532 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1533 && !(flags & P_BOOL)))
1534 {
1535 /*
1536 * print value
1537 */
1538 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001539 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 else
1541 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001542 gotocmdline(TRUE); // cursor at status line
1543 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545 if (opt_idx >= 0)
1546 {
1547 showoneopt(&options[opt_idx], opt_flags);
1548#ifdef FEAT_EVAL
1549 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001550 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001551 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001552 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001553 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001554 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001555 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001556 (int)options[opt_idx].indir & PV_MASK]);
1557 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001558 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001559 (int)options[opt_idx].indir & PV_MASK]);
1560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561#endif
1562 }
1563 else
1564 {
1565 char_u *p;
1566
1567 p = find_termcode(key_name);
1568 if (p == NULL)
1569 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001570 errmsg = N_(e_key_code_not_set);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 goto skip;
1572 }
1573 else
1574 (void)show_one_termcode(key_name, p, TRUE);
1575 }
1576 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001577 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001578 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 else
1581 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001582 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001583 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001585 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
1587 if (nextchar == '=' || nextchar == ':')
1588 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001589 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 goto skip;
1591 }
1592
1593 /*
1594 * ":set opt!": invert
1595 * ":set opt&": reset to default value
1596 * ":set opt<": reset to global value
1597 */
1598 if (nextchar == '!')
1599 value = *(int *)(varp) ^ 1;
1600 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001601 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 ((flags & P_VI_DEF) || cp_val)
1603 ? VI_DEFAULT : VIM_DEFAULT];
1604 else if (nextchar == '<')
1605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001606 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if ((int *)varp == &curbuf->b_p_ar
1608 && opt_flags == OPT_LOCAL)
1609 value = -1;
1610 else
1611 value = *(int *)get_varp_scope(&(options[opt_idx]),
1612 OPT_GLOBAL);
1613 }
1614 else
1615 {
1616 /*
1617 * ":set invopt": invert
1618 * ":set opt" or ":set noopt": set or reset
1619 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001620 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001622 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 goto skip;
1624 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001625 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 value = *(int *)(varp) ^ 1;
1627 else
1628 value = prefix;
1629 }
1630
1631 errmsg = set_bool_option(opt_idx, varp, (int)value,
1632 opt_flags);
1633 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1637 || prefix != 1)
1638 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001639 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 goto skip;
1641 }
1642
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001643 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 /*
1646 * Different ways to set a number option:
1647 * & set to default value
1648 * < set to global value
1649 * <xx> accept special key codes for 'wildchar'
1650 * c accept any non-digit for 'wildchar'
1651 * [-]0-9 set number
1652 * other error
1653 */
1654 ++arg;
1655 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001656 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 ((flags & P_VI_DEF) || cp_val)
1658 ? VI_DEFAULT : VIM_DEFAULT];
1659 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001661 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1662 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001663 if ((long *)varp == &curbuf->b_p_ul
1664 && opt_flags == OPT_LOCAL)
1665 value = NO_LOCAL_UNDOLEVEL;
1666 else
1667 value = *(long *)get_varp_scope(
1668 &(options[opt_idx]), OPT_GLOBAL);
1669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 else if (((long *)varp == &p_wc
1671 || (long *)varp == &p_wcm)
1672 && (*arg == '<'
1673 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001674 || (*arg != NUL
1675 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 && !VIM_ISDIGIT(*arg))))
1677 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001678 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (value == 0 && (long *)varp != &p_wcm)
1680 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001681 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 goto skip;
1683 }
1684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1686 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001687 // Allow negative (for 'undolevels'), octal and
1688 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001689 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001690 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001691 if (i == 0 || (arg[i] != NUL
1692 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001694 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 goto skip;
1696 }
1697 }
1698 else
1699 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001700 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 goto skip;
1702 }
1703
1704 if (adding)
1705 value = *(long *)varp + value;
1706 if (prepending)
1707 value = *(long *)varp * value;
1708 if (removing)
1709 value = *(long *)varp - value;
1710 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001711 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001713 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001715 char_u *save_arg = NULL;
1716 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001717 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001718 char_u *newval;
1719 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001720 char_u *origval_l = NULL;
1721 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001722#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001723 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001724 char_u *saved_origval_l = NULL;
1725 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001726 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001727#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001728 unsigned newlen;
1729 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 int new_value_alloced; // new string option
1731 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001733 // When using ":set opt=val" for a global option
1734 // with a local value the local value will be
1735 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001737 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 varp = options[opt_idx].var;
1739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001740 // The old value is kept until we are sure that the
1741 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001743
Bram Moolenaard7c96872019-06-15 17:12:48 +02001744 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1745 {
1746 origval_l = *(char_u **)get_varp_scope(
1747 &(options[opt_idx]), OPT_LOCAL);
1748 origval_g = *(char_u **)get_varp_scope(
1749 &(options[opt_idx]), OPT_GLOBAL);
1750
1751 // A global-local string option might have an empty
1752 // option as value to indicate that the global
1753 // value should be used.
1754 if (((int)options[opt_idx].indir & PV_BOTH)
1755 && origval_l == empty_option)
1756 origval_l = origval_g;
1757 }
1758
1759 // When setting the local value of a global
1760 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001761 if (((int)options[opt_idx].indir & PV_BOTH)
1762 && (opt_flags & OPT_LOCAL))
1763 origval = *(char_u **)get_varp(
1764 &options[opt_idx]);
1765 else
1766 origval = oldval;
1767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001768 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770 newval = options[opt_idx].def_val[
1771 ((flags & P_VI_DEF) || cp_val)
1772 ? VI_DEFAULT : VIM_DEFAULT];
1773 if ((char_u **)varp == &p_bg)
1774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001775 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#ifdef FEAT_GUI
1777 if (gui.in_use)
1778 newval = gui_bg_default();
1779 else
1780#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001781 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001783 else if ((char_u **)varp == &p_fencs && enc_utf8)
1784 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001786 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001787 // default value was already expanded, only
1788 // required when an environment variable was set
1789 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 if (newval == NULL)
1791 newval = empty_option;
1792 else
1793 {
1794 s = option_expand(opt_idx, newval);
1795 if (s == NULL)
1796 s = newval;
1797 newval = vim_strsave(s);
1798 }
1799 new_value_alloced = TRUE;
1800 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001801 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 newval = vim_strsave(*(char_u **)get_varp_scope(
1804 &(options[opt_idx]), OPT_GLOBAL));
1805 new_value_alloced = TRUE;
1806 }
1807 else
1808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001809 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811 /*
1812 * Set 'keywordprg' to ":help" if an empty
1813 * value was passed to :set by the user.
1814 * Misuse errbuf[] for the resulting string.
1815 */
1816 if (varp == (char_u *)&p_kp
1817 && (*arg == NUL || *arg == ' '))
1818 {
1819 STRCPY(errbuf, ":help");
1820 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001821 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001824 * Convert 'backspace' number to string, for
1825 * adding, prepending and removing string.
1826 */
1827 else if (varp == (char_u *)&p_bs
1828 && VIM_ISDIGIT(**(char_u **)varp))
1829 {
1830 i = getdigits((char_u **)varp);
1831 switch (i)
1832 {
1833 case 0:
1834 *(char_u **)varp = empty_option;
1835 break;
1836 case 1:
1837 *(char_u **)varp = vim_strsave(
1838 (char_u *)"indent,eol");
1839 break;
1840 case 2:
1841 *(char_u **)varp = vim_strsave(
1842 (char_u *)"indent,eol,start");
1843 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001844 case 3:
1845 *(char_u **)varp = vim_strsave(
1846 (char_u *)"indent,eol,nostop");
1847 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001848 }
1849 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001850 if (origval == oldval)
1851 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001852 if (origval_l == oldval)
1853 origval_l = *(char_u **)varp;
1854 if (origval_g == oldval)
1855 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001856 oldval = *(char_u **)varp;
1857 }
1858 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 * Convert 'whichwrap' number to string, for
1860 * backwards compatibility with Vim 3.0.
1861 * Misuse errbuf[] for the resulting string.
1862 */
1863 else if (varp == (char_u *)&p_ww
1864 && VIM_ISDIGIT(*arg))
1865 {
1866 *errbuf = NUL;
1867 i = getdigits(&arg);
1868 if (i & 1)
1869 STRCAT(errbuf, "b,");
1870 if (i & 2)
1871 STRCAT(errbuf, "s,");
1872 if (i & 4)
1873 STRCAT(errbuf, "h,l,");
1874 if (i & 8)
1875 STRCAT(errbuf, "<,>,");
1876 if (i & 16)
1877 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001878 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 errbuf[STRLEN(errbuf) - 1] = NUL;
1880 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001881 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 /*
1884 * Remove '>' before 'dir' and 'bdir', for
1885 * backwards compatibility with version 3.0
1886 */
1887 else if ( *arg == '>'
1888 && (varp == (char_u *)&p_dir
1889 || varp == (char_u *)&p_bdir))
1890 {
1891 ++arg;
1892 }
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 /*
1895 * Copy the new string into allocated memory.
1896 * Can't use set_string_option_direct(), because
1897 * we need to remove the backslashes.
1898 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001899 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 newlen = (unsigned)STRLEN(arg) + 1;
1901 if (adding || prepending || removing)
1902 newlen += (unsigned)STRLEN(origval) + 1;
1903 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001904 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 break;
1906 s = newval;
1907
1908 /*
1909 * Copy the string, skip over escaped chars.
1910 * For MS-DOS and WIN32 backslashes before normal
1911 * file name characters are not removed, and keep
1912 * backslash at start, for "\\machine\path", but
1913 * do remove it for "\\\\machine\\path".
1914 * The reverse is found in ExpandOldSetting().
1915 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001916 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 if (*arg == '\\' && arg[1] != NUL
1919#ifdef BACKSLASH_IN_FILENAME
1920 && !((flags & P_EXPAND)
1921 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001922 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 && (arg[1] != '\\'
1924 || (s == newval
1925 && arg[2] != '\\')))
1926#endif
1927 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001928 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001930 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001932 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 mch_memmove(s, arg, (size_t)i);
1934 arg += i;
1935 s += i;
1936 }
1937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 *s++ = *arg++;
1939 }
1940 *s = NUL;
1941
1942 /*
1943 * Expand environment variables and ~.
1944 * Don't do it when adding without inserting a
1945 * comma.
1946 */
1947 if (!(adding || prepending || removing)
1948 || (flags & P_COMMA))
1949 {
1950 s = option_expand(opt_idx, newval);
1951 if (s != NULL)
1952 {
1953 vim_free(newval);
1954 newlen = (unsigned)STRLEN(s) + 1;
1955 if (adding || prepending || removing)
1956 newlen += (unsigned)STRLEN(origval) + 1;
1957 newval = alloc(newlen);
1958 if (newval == NULL)
1959 break;
1960 STRCPY(newval, s);
1961 }
1962 }
1963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001964 // locate newval[] in origval[] when removing it
1965 // and when adding to avoid duplicates
1966 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (removing || (flags & P_NODUP))
1968 {
1969 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001970 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001972 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001973 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 prepending = FALSE;
1976 adding = FALSE;
1977 STRCPY(newval, origval);
1978 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001979
1980 // if no duplicate, move pointer to end of
1981 // original value
1982 if (s == NULL)
1983 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001986 // concatenate the two strings; add a ',' if
1987 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (adding || prepending)
1989 {
1990 comma = ((flags & P_COMMA) && *origval != NUL
1991 && *newval != NUL);
1992 if (adding)
1993 {
1994 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001995 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001996 if (comma && i > 1
1997 && (flags & P_ONECOMMA) == P_ONECOMMA
1998 && origval[i - 1] == ','
1999 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02002000 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 mch_memmove(newval + i + comma, newval,
2002 STRLEN(newval) + 1);
2003 mch_memmove(newval, origval, (size_t)i);
2004 }
2005 else
2006 {
2007 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002008 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 if (comma)
2011 newval[i] = ',';
2012 }
2013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002014 // Remove newval[] from origval[]. (Note: "i" has
2015 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (removing)
2017 {
2018 STRCPY(newval, origval);
2019 if (*s)
2020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002021 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (flags & P_COMMA)
2023 {
2024 if (s == origval)
2025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002026 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (s[i] == ',')
2028 ++i;
2029 }
2030 else
2031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002032 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 --s;
2034 ++i;
2035 }
2036 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002037 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 }
2040
2041 if (flags & P_FLAGLIST)
2042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002044 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002046 // if options have P_FLAGLIST and
2047 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002050 if (*s != ',' && *(s + 1) == ','
2051 && vim_strchr(s + 2, *s) != NULL)
2052 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002053 // Remove the duplicated value and
2054 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002055 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002056 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002059 else
2060 {
2061 if ((!(flags & P_COMMA) || *s != ',')
2062 && vim_strchr(s + 1, *s) != NULL)
2063 {
2064 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002065 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002066 }
2067 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002068 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002072 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 arg = save_arg;
2074 new_value_alloced = TRUE;
2075 }
2076
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002077 /*
2078 * Set the new value.
2079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 *(char_u **)(varp) = newval;
2081
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002082#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002083 if (!starting
2084# ifdef FEAT_CRYPT
2085 && options[opt_idx].indir != PV_KEY
2086# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002087 && origval != NULL && newval != NULL)
2088 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002089 // origval may be freed by
2090 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002091 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 // newval (and varp) may become invalid if the
2093 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002094 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002095 if (origval_l != NULL)
2096 saved_origval_l = vim_strsave(origval_l);
2097 if (origval_g != NULL)
2098 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002099 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002100#endif
2101
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002102 {
2103 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002104 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002105
2106 // When an option is set in the sandbox, from a
2107 // modeline or in secure mode, then deal with side
2108 // effects in secure mode. Also when the value was
2109 // set with the P_INSECURE flag and is not
2110 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002111 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002112#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002113 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002114#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002115 || (!value_is_replaced && (*p & P_INSECURE)))
2116 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002117
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002118 // Handle side effects, and set the global value
2119 // for ":set" on local options. Note: when setting
2120 // 'syntax' or 'filetype' autocommands may be
2121 // triggered that can cause havoc.
2122 errmsg = did_set_string_option(
2123 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002124 new_value_alloced, oldval, errbuf,
2125 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002126
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002127 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002130#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002131 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002132 trigger_optionsset_string(
2133 opt_idx, opt_flags, saved_origval,
2134 saved_origval_l, saved_origval_g,
2135 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002136 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002137 vim_free(saved_origval_l);
2138 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002139 vim_free(saved_newval);
2140#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (errmsg != NULL)
2143 goto skip;
2144 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 char_u *p;
2148
2149 if (nextchar == '&')
2150 {
2151 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002152 errmsg = N_(e_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 else
2155 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002156 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002157 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (*p == '\\' && p[1] != NUL)
2159 ++p;
2160 nextchar = *p;
2161 *p = NUL;
2162 add_termcode(key_name, arg, FALSE);
2163 *p = nextchar;
2164 }
2165 if (full_screen)
2166 ttest(FALSE);
2167 redraw_all_later(CLEAR);
2168 }
2169 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002172 did_set_option(
2173 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175
2176skip:
2177 /*
2178 * Advance to next argument.
2179 * - skip until a blank found, taking care of backslashes
2180 * - skip blanks
2181 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2182 */
2183 for (i = 0; i < 2 ; ++i)
2184 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002185 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (*arg++ == '\\' && *arg != NUL)
2187 ++arg;
2188 arg = skipwhite(arg);
2189 if (*arg != '=')
2190 break;
2191 }
2192 }
2193
2194 if (errmsg != NULL)
2195 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002196 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002197 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (i + (arg - startarg) < IOSIZE)
2199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002200 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 STRCAT(IObuff, ": ");
2202 mch_memmove(IObuff + i, startarg, (arg - startarg));
2203 IObuff[i + (arg - startarg)] = NUL;
2204 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002205 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 trans_characters(IObuff, IOSIZE);
2207
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002208 ++no_wait_return; // wait_return done later
2209 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 --no_wait_return;
2211
2212 return FAIL;
2213 }
2214
2215 arg = skipwhite(arg);
2216 }
2217
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218theend:
2219 if (silent_mode && did_show)
2220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002221 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002223 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002224 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002226 out_flush();
2227 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002228 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002229 }
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return OK;
2232}
2233
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234/*
2235 * Call this when an option has been given a new value through a user command.
2236 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2237 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239did_set_option(
2240 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002241 int opt_flags, // possibly with OPT_MODELINE
2242 int new_value, // value was replaced completely
2243 int value_checked) // value was checked to be safe, no need to set the
2244 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002245{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002246 long_u *p;
2247
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002248 options[opt_idx].flags |= P_WAS_SET;
2249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002250 // When an option is set in the sandbox, from a modeline or in secure mode
2251 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2252 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002253 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002254 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002255#ifdef HAVE_SANDBOX
2256 || sandbox != 0
2257#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002258 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002259 *p = *p | P_INSECURE;
2260 else if (new_value)
2261 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002262}
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264/*
2265 * Convert a key name or string into a key value.
2266 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002267 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002269 int
2270string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002273 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (*arg == '^')
2275 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002276 if (multi_byte)
2277 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return *arg;
2279}
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281/*
2282 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2283 * maketitle() to create and display it.
2284 * When switching the title or icon off, call mch_restore_title() to get
2285 * the old value back.
2286 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002287 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002288did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 if (starting != NO_SCREEN
2291#ifdef FEAT_GUI
2292 && !gui.starting
2293#endif
2294 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298/*
2299 * set_options_bin - called when 'bin' changes value.
2300 */
2301 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002302set_options_bin(
2303 int oldval,
2304 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002305 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 /*
2308 * The option values that are changed when 'bin' changes are
2309 * copied when 'bin is set and restored when 'bin' is reset.
2310 */
2311 if (newval)
2312 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002313 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 if (!(opt_flags & OPT_GLOBAL))
2316 {
2317 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2318 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2319 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2320 curbuf->b_p_et_nobin = curbuf->b_p_et;
2321 }
2322 if (!(opt_flags & OPT_LOCAL))
2323 {
2324 p_tw_nobin = p_tw;
2325 p_wm_nobin = p_wm;
2326 p_ml_nobin = p_ml;
2327 p_et_nobin = p_et;
2328 }
2329 }
2330
2331 if (!(opt_flags & OPT_GLOBAL))
2332 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 curbuf->b_p_tw = 0; // no automatic line wrap
2334 curbuf->b_p_wm = 0; // no automatic line wrap
2335 curbuf->b_p_ml = 0; // no modelines
2336 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338 if (!(opt_flags & OPT_LOCAL))
2339 {
2340 p_tw = 0;
2341 p_wm = 0;
2342 p_ml = FALSE;
2343 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002344 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 if (!(opt_flags & OPT_GLOBAL))
2350 {
2351 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2352 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2353 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2354 curbuf->b_p_et = curbuf->b_p_et_nobin;
2355 }
2356 if (!(opt_flags & OPT_LOCAL))
2357 {
2358 p_tw = p_tw_nobin;
2359 p_wm = p_wm_nobin;
2360 p_ml = p_ml_nobin;
2361 p_et = p_et_nobin;
2362 }
2363 }
2364}
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366/*
2367 * Expand environment variables for some string options.
2368 * These string options cannot be indirect!
2369 * If "val" is NULL expand the current value of the option.
2370 * Return pointer to NameBuff, or NULL when not expanded.
2371 */
2372 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002373option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2377 return NULL;
2378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002379 // If val is longer than MAXPATHL no meaningful expansion can be done,
2380 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (val != NULL && STRLEN(val) > MAXPATHL)
2382 return NULL;
2383
2384 if (val == NULL)
2385 val = *(char_u **)options[opt_idx].var;
2386
2387 /*
2388 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2389 * Escape spaces when expanding 'tags', they are used to separate file
2390 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002391 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002394 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002395#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002396 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2397#endif
2398 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002399 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 return NULL;
2401
2402 return NameBuff;
2403}
2404
2405/*
2406 * After setting various option values: recompute variables that depend on
2407 * option values.
2408 */
2409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002410didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002412 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 (void)init_chartab();
2414
Bram Moolenaardac13472019-09-16 21:06:21 +02002415 didset_string_options();
2416
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002417#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002418 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002419 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002420 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002421 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002424 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 (void)check_cedit();
2426#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002427#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002428 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002429 fill_breakat_flags();
2430#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002431 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002432}
2433
2434/*
2435 * More side effects of setting options.
2436 */
2437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002439{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002440 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002441 (void)highlight_changed();
2442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002443 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002444 check_opt_wim();
2445
Bram Moolenaareed9d462021-02-15 20:38:25 +01002446 // Parse default for 'listchars'.
2447 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002449 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002450 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002451
2452#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002453 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002454 (void)check_clipboard_option();
2455#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002456#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002457 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002458 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002459 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002460 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462}
2463
2464/*
2465 * Check for string options that are NULL (normally only termcap options).
2466 */
2467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002468check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 int opt_idx;
2471
2472 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2473 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2474 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2475}
2476
2477/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002478 * Return the option index found by a pointer into term_strings[].
2479 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002481 int
2482get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002484 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2487 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002488 return opt_idx;
2489 return -1; // cannot happen: didn't find it!
2490}
2491
2492/*
2493 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2494 * Return the option index or -1 if not found.
2495 */
2496 int
2497set_term_option_alloced(char_u **p)
2498{
2499 int opt_idx = get_term_opt_idx(p);
2500
2501 if (opt_idx >= 0)
2502 options[opt_idx].flags |= P_ALLOCED;
2503 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504}
2505
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002506#if defined(FEAT_EVAL) || defined(PROTO)
2507/*
2508 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2509 * Return FALSE when it wasn't.
2510 * Return -1 for an unknown option.
2511 */
2512 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002513was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002514{
2515 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002516 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002517
2518 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002519 {
2520 flagp = insecure_flag(idx, opt_flags);
2521 return (*flagp & P_INSECURE) != 0;
2522 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002523 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002524 return -1;
2525}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002526
2527/*
2528 * Get a pointer to the flags used for the P_INSECURE flag of option
2529 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002530 * NOTE: Caller must make sure that "curwin" is set to the window from which
2531 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002532 */
2533 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002534insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535{
2536 if (opt_flags & OPT_LOCAL)
2537 switch ((int)options[opt_idx].indir)
2538 {
2539#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002540 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541#endif
2542#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002543# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002544 case PV_FDE: return &curwin->w_p_fde_flags;
2545 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002546# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002547# ifdef FEAT_BEVAL
2548 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2549# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002550# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002551 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002552# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002553 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002554# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002555 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002556# endif
2557#endif
2558 }
2559
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002560 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002561 return &options[opt_idx].flags;
2562}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002563#endif
2564
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002565/*
2566 * Redraw the window title and/or tab page text later.
2567 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002568void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002569{
2570 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002571 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002572}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002575 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2576 * characters or characters in "allowed".
2577 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002578 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002579valid_name(char_u *val, char *allowed)
2580{
2581 char_u *s;
2582
2583 for (s = val; *s != NUL; ++s)
2584 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2585 return FALSE;
2586 return TRUE;
2587}
2588
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002589#if defined(FEAT_EVAL) || defined(PROTO)
2590/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002591 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002592 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002593 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002594 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002595set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002596{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002597 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2598 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002599 sctx_T new_script_ctx = script_ctx;
2600
Bram Moolenaar51258742020-05-03 17:19:33 +02002601 // Modeline already has the line number set.
2602 if (!(opt_flags & OPT_MODELINE))
2603 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002604
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002605 // Remember where the option was set. For local options need to do that
2606 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002607 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002608 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002609 if (both || (opt_flags & OPT_LOCAL))
2610 {
2611 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002612 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002613 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002614 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002615 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002616 if (both)
2617 // also setting the "all buffers" value
2618 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2619 new_script_ctx;
2620 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002621 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002622}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002623
2624/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002625 * Get the script context of global option "name".
2626 *
2627 */
2628 sctx_T *
2629get_option_sctx(char *name)
2630{
2631 int idx = findoption((char_u *)name);
2632
2633 if (idx >= 0)
2634 return &options[idx].script_ctx;
2635 siemsg("no such option: %s", name);
2636 return NULL;
2637}
2638
2639/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002640 * Set the script_ctx for a termcap option.
2641 * "name" must be the two character code, e.g. "RV".
2642 * When "name" is NULL use "opt_idx".
2643 */
2644 void
2645set_term_option_sctx_idx(char *name, int opt_idx)
2646{
2647 char_u buf[5];
2648 int idx;
2649
2650 if (name == NULL)
2651 idx = opt_idx;
2652 else
2653 {
2654 buf[0] = 't';
2655 buf[1] = '_';
2656 buf[2] = name[0];
2657 buf[3] = name[1];
2658 buf[4] = 0;
2659 idx = findoption(buf);
2660 }
2661 if (idx >= 0)
2662 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2663}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002664#endif
2665
Bram Moolenaarf4140482020-02-15 23:06:45 +01002666#if defined(FEAT_EVAL)
2667/*
2668 * Apply the OptionSet autocommand.
2669 */
2670 static void
2671apply_optionset_autocmd(
2672 int opt_idx,
2673 long opt_flags,
2674 long oldval,
2675 long oldval_g,
2676 long newval,
2677 char *errmsg)
2678{
2679 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2680
2681 // Don't do this while starting up, failure or recursively.
2682 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2683 return;
2684
2685 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2686 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2687 oldval_g);
2688 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2689 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2690 (opt_flags & OPT_LOCAL) ? "local" : "global");
2691 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2692 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2693 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2694 if (opt_flags & OPT_LOCAL)
2695 {
2696 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2697 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2698 }
2699 if (opt_flags & OPT_GLOBAL)
2700 {
2701 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2702 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2703 }
2704 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2705 {
2706 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2707 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2708 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2709 }
2710 if (opt_flags & OPT_MODELINE)
2711 {
2712 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2713 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2714 }
2715 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2716 NULL, FALSE, NULL);
2717 reset_v_option_vars();
2718}
2719#endif
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721/*
2722 * Set the value of a boolean option, and take care of side effects.
2723 * Returns NULL for success, or an error message for an error.
2724 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002725 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002726set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 int opt_idx, // index in options[] table
2728 char_u *varp, // pointer to the option variable
2729 int value, // new value
2730 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002733#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002734 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if ((secure
2739#ifdef HAVE_SANDBOX
2740 || sandbox != 0
2741#endif
2742 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002743 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002745#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002746 // Save the global value before changing anything. This is needed as for
2747 // a global-only option setting the "local value" in fact sets the global
2748 // value (since there is only one value).
2749 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2750 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2751 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002752#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002754 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002757 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
2759
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002760#ifdef FEAT_GUI
2761 need_mouse_correct = TRUE;
2762#endif
2763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2766 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2767
2768 /*
2769 * Handle side effects of changing a bool option.
2770 */
2771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar920694c2016-08-21 17:45:02 +02002776#ifdef FEAT_LANGMAP
2777 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002779 p_lnr = !p_lrm;
2780 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002782 p_lrm = !p_lnr;
2783#endif
2784
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002785#ifdef FEAT_SYN_HL
2786 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2787 reset_cursorline();
2788#endif
2789
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002790#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002792 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2793 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002794 // Only take action when the option was set. When reset we do not
2795 // delete the undo file, the option may be set again without making
2796 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002797 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002798 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002799 char_u hash[UNDO_HASH_SIZE];
2800 buf_T *save_curbuf = curbuf;
2801
Bram Moolenaar29323592016-07-24 22:04:11 +02002802 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002803 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // When 'undofile' is set globally: for every buffer, otherwise
2805 // only for the current buffer: Try to read in the undofile,
2806 // if one exists, the buffer wasn't changed and the buffer was
2807 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002808 if ((curbuf == save_curbuf
2809 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2810 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2811 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002812#ifdef FEAT_CRYPT
2813 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2814 continue;
2815#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002816 u_compute_hash(hash);
2817 u_read_undo(NULL, hash, curbuf->b_fname);
2818 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002819 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002820 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002821 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002822 }
2823#endif
2824
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 else if ((int *)varp == &curbuf->b_p_ro)
2826 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002827 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2829 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002830
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002832 if (curbuf->b_p_ro)
2833 curbuf->b_did_warn = FALSE;
2834
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002835 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837
Bram Moolenaar9c449722010-07-20 18:44:27 +02002838#ifdef FEAT_GUI
2839 else if ((int *)varp == &p_mh)
2840 {
2841 if (!p_mh)
2842 gui_mch_mousehide(FALSE);
2843 }
2844#endif
2845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002848 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002849# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002851 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2852 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002853 {
2854 curbuf->b_p_ma = FALSE;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002855 return N_(e_cannot_make_terminal_with_running_job_modifiable);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002856 }
2857# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002858 redraw_titles();
2859 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002862 {
2863 redraw_titles();
2864 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002866 else if ((int *)varp == &curbuf->b_p_fixeol)
2867 {
2868 redraw_titles();
2869 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002871 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002872 {
2873 redraw_titles();
2874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 else if ((int *)varp == &curbuf->b_p_bin)
2878 {
2879 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002880 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2885 {
2886 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2887 NULL, NULL, TRUE, curbuf);
2888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 else if ((int *)varp == &curbuf->b_p_swf)
2892 {
2893 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2897 // buf->b_p_swf
2898 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 else if ((int *)varp == &p_terse)
2903 {
2904 char_u *p;
2905
2906 p = vim_strchr(p_shm, SHM_SEARCH);
2907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 if (p_terse && p == NULL)
2910 {
2911 STRCPY(IObuff, p_shm);
2912 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002913 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002917 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 else if ((int *)varp == &p_paste)
2922 {
2923 paste_option_changed();
2924 }
2925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else if ((int *)varp == &p_im)
2928 {
2929 if (p_im)
2930 {
2931 if ((State & INSERT) == 0)
2932 need_start_insertmode = TRUE;
2933 stop_insert_mode = FALSE;
2934 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002936 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
2938 need_start_insertmode = FALSE;
2939 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002940 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 restart_edit = 0;
2943 }
2944 }
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 else if ((int *)varp == &p_ic && p_hls)
2948 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002949 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951
2952#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 else if ((int *)varp == &p_hls)
2955 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002956 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958#endif
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2961 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else if ((int *)varp == &curwin->w_p_scb)
2963 {
2964 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002967 curwin->w_scbind_pos = curwin->w_topline;
2968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaar4033c552017-09-16 20:54:51 +02002971#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else if ((int *)varp == &curwin->w_p_pvw)
2974 {
2975 if (curwin->w_p_pvw)
2976 {
2977 win_T *win;
2978
Bram Moolenaar29323592016-07-24 22:04:11 +02002979 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 if (win->w_p_pvw && win != curwin)
2981 {
2982 curwin->w_p_pvw = FALSE;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002983 return N_(e_preview_window_already_exists);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
2985 }
2986 }
2987#endif
2988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else if ((int *)varp == &curbuf->b_p_tx)
2991 {
2992 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2993 }
2994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 set_string_option_direct((char_u *)"ffs", -1,
2999 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003000 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 /*
3004 * When 'lisp' option changes include/exclude '-' in
3005 * keyword characters.
3006 */
3007#ifdef FEAT_LISP
3008 else if (varp == (char_u *)&(curbuf->b_p_lisp))
3009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012#endif
3013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003014 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003015 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003017 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020 else if ((int *)varp == &curbuf->b_changed)
3021 {
3022 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003024 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
3027
3028#ifdef BACKSLASH_IN_FILENAME
3029 else if ((int *)varp == &p_ssl)
3030 {
3031 if (p_ssl)
3032 {
3033 psepc = '/';
3034 psepcN = '\\';
3035 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
3037 else
3038 {
3039 psepc = '\\';
3040 psepcN = '/';
3041 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 buflist_slash_adjust();
3046 alist_slash_adjust();
3047# ifdef FEAT_EVAL
3048 scriptnames_slash_adjust();
3049# endif
3050 }
3051#endif
3052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003053 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 else if ((int *)varp == &curwin->w_p_wrap)
3055 {
3056 if (curwin->w_p_wrap)
3057 curwin->w_leftcol = 0;
3058 }
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 else if ((int *)varp == &p_ea)
3061 {
3062 if (p_ea && !old_value)
3063 win_equal(curwin, FALSE, 0);
3064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
3066 else if ((int *)varp == &p_wiv)
3067 {
3068 /*
3069 * When 'weirdinvert' changed, set/reset 't_xs'.
3070 * Then set 'weirdinvert' according to value of 't_xs'.
3071 */
3072 if (p_wiv && !old_value)
3073 T_XS = (char_u *)"y";
3074 else if (!p_wiv && old_value)
3075 T_XS = empty_option;
3076 p_wiv = (*T_XS != NUL);
3077 }
3078
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003079#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 else if ((int *)varp == &p_beval)
3081 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003082 if (!balloonEvalForTerm)
3083 {
3084 if (p_beval && !old_value)
3085 gui_mch_enable_beval_area(balloonEval);
3086 else if (!p_beval && old_value)
3087 gui_mch_disable_beval_area(balloonEval);
3088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003090#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003091#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003092 else if ((int *)varp == &p_bevalterm)
3093 {
3094 mch_bevalterm_changed();
3095 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003098#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 else if ((int *)varp == &p_acd)
3100 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003102 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104#endif
3105
3106#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else if ((int *)varp == &curwin->w_p_diff)
3109 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003110 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003111 diff_buf_adjust(curwin);
3112# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (foldmethodIsDiff(curwin))
3114 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117#endif
3118
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003119#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003120 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 else if ((int *)varp == &p_imdisable)
3122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (p_imdisable)
3125 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003126 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // When the option is set from an autocommand, it may need to take
3128 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003129 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131#endif
3132
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003133#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003135 else if ((int *)varp == &curwin->w_p_spell)
3136 {
3137 if (curwin->w_p_spell)
3138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003139 char *errmsg = did_set_spelllang(curwin);
3140
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003141 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003142 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003143 }
3144 }
3145#endif
3146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#ifdef FEAT_ARABIC
3148 if ((int *)varp == &curwin->w_p_arab)
3149 {
3150 if (curwin->w_p_arab)
3151 {
3152 /*
3153 * 'arabic' is set, handle various sub-settings.
3154 */
3155 if (!p_tbidi)
3156 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (!curwin->w_p_rl)
3159 {
3160 curwin->w_p_rl = TRUE;
3161 changed_window_setting();
3162 }
3163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003164 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (!p_arshape)
3166 {
3167 p_arshape = TRUE;
3168 redraw_later_clear();
3169 }
3170 }
3171
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003172 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003175 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003176 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3177
Bram Moolenaar8820b482017-03-16 17:23:31 +01003178 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003179 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003180#ifdef FEAT_EVAL
3181 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3182#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003189 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3191 OPT_LOCAL);
3192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194 else
3195 {
3196 /*
3197 * 'arabic' is reset, handle various sub-settings.
3198 */
3199 if (!p_tbidi)
3200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003201 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (curwin->w_p_rl)
3203 {
3204 curwin->w_p_rl = FALSE;
3205 changed_window_setting();
3206 }
3207
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003208 // 'arabicshape' isn't reset, it is a global option and
3209 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003212 // 'delcombine' isn't reset, it is a global option and another
3213 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003216 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 curbuf->b_p_iminsert = B_IMODE_NONE;
3218 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3219# endif
3220 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003221 }
3222
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003223#endif
3224
Bram Moolenaar44826212019-08-22 21:23:20 +02003225#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3226 else if (((int *)varp == &curwin->w_p_nu
3227 || (int *)varp == &curwin->w_p_rnu)
3228 && gui.in_use
3229 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3230 && curbuf->b_signlist != NULL)
3231 {
3232 // If the 'number' or 'relativenumber' options are modified and
3233 // 'signcolumn' is set to 'number', then clear the screen for a full
3234 // refresh. Otherwise the sign icons are not displayed properly in the
3235 // number column. If the 'number' option is set and only the
3236 // 'relativenumber' option is toggled, then don't refresh the screen
3237 // (optimization).
3238 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3239 redraw_all_later(CLEAR);
3240 }
3241#endif
3242
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003243#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003244 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003245 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003246 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003247# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003248 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003249 if (
3250# ifdef VIMDLL
3251 !gui.in_use && !gui.starting &&
3252# endif
3253 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003254 {
3255 p_tgc = 0;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003256 return N_(e_24_bit_colors_are_not_supported_on_this_environment);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003257 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003258 if (is_term_win32())
3259 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003260# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003261# ifdef FEAT_GUI
3262 if (!gui.in_use && !gui.starting)
3263# endif
3264 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003265# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003266 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003267 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003268 {
3269 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003270 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003271 init_highlight(TRUE, FALSE);
3272 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003273# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003274# ifdef FEAT_TERMINAL
3275 term_update_colors_all();
3276 term_update_wincolor_all();
3277# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003278 }
3279#endif
3280
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 /*
3282 * End of handling side effects for bool options.
3283 */
3284
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003285 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 options[opt_idx].flags |= P_WAS_SET;
3288
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003289#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003290 apply_optionset_autocmd(opt_idx, opt_flags,
3291 (long)(old_value ? TRUE : FALSE),
3292 (long)(old_global_value ? TRUE : FALSE),
3293 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003294#endif
3295
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003296 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003297 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003298 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003299 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003300
3301 if ((opt_flags & OPT_NO_REDRAW) == 0)
3302 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304 return NULL;
3305}
3306
3307/*
3308 * Set the value of a number option, and take care of side effects.
3309 * Returns NULL for success, or an error message for an error.
3310 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003311 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003312set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003313 int opt_idx, // index in options[] table
3314 char_u *varp, // pointer to the option variable
3315 long value, // new value
3316 char *errbuf, // buffer for error messages
3317 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003318 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3319 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003321 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003323#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003324 long old_global_value = 0; // only used when setting a local and
3325 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003326#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003327 long old_Rows = Rows; // remember old Rows
3328 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 long *pp = (long *)varp;
3330
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003331 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003332 if ((secure
3333#ifdef HAVE_SANDBOX
3334 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003336 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003337 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003339#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003340 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003341 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003342 // value (since there is only one value).
3343 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003344 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3345 OPT_GLOBAL);
3346#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 *pp = value;
3349#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003350 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003351 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003353#ifdef FEAT_GUI
3354 need_mouse_correct = TRUE;
3355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaar14f24742012-08-08 18:01:05 +02003357 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003359 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003360#ifdef FEAT_VARTABS
3361 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3362 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3363 ? tabstop_first(curbuf->b_p_vts_array)
3364 : curbuf->b_p_ts;
3365#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369
3370 /*
3371 * Number options that need some action when changed
3372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (pp == &p_wh || pp == &p_hh)
3374 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003375 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (p_wh < 1)
3377 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003378 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 p_wh = 1;
3380 }
3381 if (p_wmh > p_wh)
3382 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003383 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 p_wh = p_wmh;
3385 }
3386 if (p_hh < 0)
3387 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003388 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 p_hh = 0;
3390 }
3391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003392 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003393 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 if (pp == &p_wh && curwin->w_height < p_wh)
3396 win_setheight((int)p_wh);
3397 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3398 win_setheight((int)p_hh);
3399 }
3400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 else if (pp == &p_wmh)
3402 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003403 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (p_wmh < 0)
3405 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003406 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 p_wmh = 0;
3408 }
3409 if (p_wmh > p_wh)
3410 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003411 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 p_wmh = p_wh;
3413 }
3414 win_setminheight();
3415 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003416 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003418 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (p_wiw < 1)
3420 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003421 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 p_wiw = 1;
3423 }
3424 if (p_wmw > p_wiw)
3425 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003426 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 p_wiw = p_wmw;
3428 }
3429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003430 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003431 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 win_setwidth((int)p_wiw);
3433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 else if (pp == &p_wmw)
3435 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003436 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 if (p_wmw < 0)
3438 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003439 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 p_wmw = 0;
3441 }
3442 if (p_wmw > p_wiw)
3443 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003444 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 p_wmw = p_wiw;
3446 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003447 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else if (pp == &p_ls)
3452 {
3453 last_status(FALSE);
3454 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003455
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003457 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
3462#ifdef FEAT_GUI
3463 else if (pp == &p_linespace)
3464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465 // Recompute gui.char_height and resize the Vim window to keep the
3466 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003467 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003468 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470#endif
3471
3472#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003473 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 else if (pp == &curwin->w_p_fdl)
3475 {
3476 if (curwin->w_p_fdl < 0)
3477 curwin->w_p_fdl = 0;
3478 newFoldLevel();
3479 }
3480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003481 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else if (pp == &curwin->w_p_fml)
3483 {
3484 foldUpdateAll(curwin);
3485 }
3486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else if (pp == &curwin->w_p_fdn)
3489 {
3490 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3491 foldUpdateAll(curwin);
3492 }
3493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else if (pp == &curwin->w_p_fdc)
3496 {
3497 if (curwin->w_p_fdc < 0)
3498 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003499 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 curwin->w_p_fdc = 0;
3501 }
3502 else if (curwin->w_p_fdc > 12)
3503 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003504 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 curwin->w_p_fdc = 12;
3506 }
3507 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003508#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003510#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003511 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3513 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003514# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (foldmethodIsIndent(curwin))
3516 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003517# endif
3518# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003519 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3520 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003521 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3522 parse_cino(curbuf);
3523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003527 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003528 else if (pp == &p_mco)
3529 {
3530 if (p_mco > MAX_MCO)
3531 p_mco = MAX_MCO;
3532 else if (p_mco < 0)
3533 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003534 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003535 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else if (pp == &curbuf->b_p_iminsert)
3538 {
3539 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3540 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003541 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 curbuf->b_p_iminsert = B_IMODE_NONE;
3543 }
3544 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003545 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003547#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003548 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 status_redraw_curbuf();
3550#endif
3551 }
3552
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003553#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003554 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003555 else if (pp == &p_imst)
3556 {
3557 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003558 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003559 }
3560#endif
3561
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003562 else if (pp == &p_window)
3563 {
3564 if (p_window < 1)
3565 p_window = 1;
3566 else if (p_window >= Rows)
3567 p_window = Rows - 1;
3568 }
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 else if (pp == &curbuf->b_p_imsearch)
3571 {
3572 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3573 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003574 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 curbuf->b_p_imsearch = B_IMODE_NONE;
3576 }
3577 p_imsearch = curbuf->b_p_imsearch;
3578 }
3579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003580 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 else if (pp == &p_titlelen)
3582 {
3583 if (p_titlelen < 0)
3584 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003585 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 p_titlelen = 85;
3587 }
3588 if (starting != NO_SCREEN && old_value != p_titlelen)
3589 need_maketitle = TRUE;
3590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003592 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 else if (pp == &p_ch)
3594 {
3595 if (p_ch < 1)
3596 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003597 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 p_ch = 1;
3599 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003600 if (p_ch > Rows - min_rows() + 1)
3601 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003603 // Only compute the new window layout when startup has been
3604 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if (p_ch != old_value && full_screen
3606#ifdef FEAT_GUI
3607 && !gui.starting
3608#endif
3609 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003610 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003613 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 else if (pp == &p_uc)
3615 {
3616 if (p_uc < 0)
3617 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003618 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 p_uc = 100;
3620 }
3621 if (p_uc && !old_value)
3622 ml_open_files();
3623 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003624#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003625 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003626 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003627 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003628 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003629 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003630 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003631 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003632 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003633 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003634 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003635 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003636 }
3637 }
3638#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003639#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003640 else if (pp == &p_mzq)
3641 mzvim_reset_timer();
3642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003644#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003645 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003646 else if (pp == &p_pyx)
3647 {
3648 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003649 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003650 }
3651#endif
3652
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003653 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 else if (pp == &p_ul)
3655 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003658 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 p_ul = value;
3660 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003661 else if (pp == &curbuf->b_p_ul)
3662 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003663 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003664 curbuf->b_p_ul = old_value;
3665 u_sync(TRUE);
3666 curbuf->b_p_ul = value;
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003669#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003670 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003671 else if (pp == &curwin->w_p_nuw)
3672 {
3673 if (curwin->w_p_nuw < 1)
3674 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003675 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003676 curwin->w_p_nuw = 1;
3677 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003678 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003679 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003680 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003681 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003682 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003683 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003684 }
3685#endif
3686
Bram Moolenaar1a384422010-07-14 19:53:30 +02003687 else if (pp == &curbuf->b_p_tw)
3688 {
3689 if (curbuf->b_p_tw < 0)
3690 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003691 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003692 curbuf->b_p_tw = 0;
3693 }
3694#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003695 {
3696 win_T *wp;
3697 tabpage_T *tp;
3698
3699 FOR_ALL_TAB_WINDOWS(tp, wp)
3700 check_colorcolumn(wp);
3701 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003702#endif
3703 }
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 /*
3706 * Check the bounds for numeric options here
3707 */
3708 if (Rows < min_rows() && full_screen)
3709 {
3710 if (errbuf != NULL)
3711 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003712 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003713 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 errmsg = errbuf;
3715 }
3716 Rows = min_rows();
3717 }
3718 if (Columns < MIN_COLUMNS && full_screen)
3719 {
3720 if (errbuf != NULL)
3721 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003722 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003723 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 errmsg = errbuf;
3725 }
3726 Columns = MIN_COLUMNS;
3727 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003728 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 /*
3731 * If the screen (shell) height has been changed, assume it is the
3732 * physical screenheight.
3733 */
3734 if (old_Rows != Rows || old_Columns != Columns)
3735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003736 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (updating_screen)
3738 *pp = old_value;
3739 else if (full_screen
3740#ifdef FEAT_GUI
3741 && !gui.starting
3742#endif
3743 )
3744 set_shellsize((int)Columns, (int)Rows, TRUE);
3745 else
3746 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 // Postpone the resizing; check the size and cmdline position for
3748 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 check_shellsize();
3750 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3751 cmdline_row = Rows - p_ch;
3752 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003753 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003754 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (curbuf->b_p_ts <= 0)
3758 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003759 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 curbuf->b_p_ts = 8;
3761 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003762 else if (curbuf->b_p_ts > TABSTOP_MAX)
3763 {
3764 errmsg = e_invalid_argument;
3765 curbuf->b_p_ts = 8;
3766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (p_tm < 0)
3768 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003769 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 p_tm = 0;
3771 }
3772 if ((curwin->w_p_scr <= 0
3773 || (curwin->w_p_scr > curwin->w_height
3774 && curwin->w_height > 0))
3775 && full_screen)
3776 {
3777 if (pp == &(curwin->w_p_scr))
3778 {
3779 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003780 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 win_comp_scroll(curwin);
3782 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003783 // If 'scroll' became invalid because of a side effect silently adjust
3784 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 else if (curwin->w_p_scr <= 0)
3786 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003787 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 curwin->w_p_scr = curwin->w_height;
3789 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003790 if (p_hi < 0)
3791 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003792 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003793 p_hi = 0;
3794 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003795 else if (p_hi > 10000)
3796 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003797 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003798 p_hi = 10000;
3799 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003800 if (p_re < 0 || p_re > 2)
3801 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003802 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003803 p_re = 0;
3804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (p_report < 0)
3806 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003807 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 p_report = 1;
3809 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003810 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003812 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 p_sj = Rows / 2;
3814 else
3815 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003816 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 p_sj = 1;
3818 }
3819 }
3820 if (p_so < 0 && full_screen)
3821 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003822 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 p_so = 0;
3824 }
3825 if (p_siso < 0 && full_screen)
3826 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003827 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 p_siso = 0;
3829 }
3830#ifdef FEAT_CMDWIN
3831 if (p_cwh < 1)
3832 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003833 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 p_cwh = 1;
3835 }
3836#endif
3837 if (p_ut < 0)
3838 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003839 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 p_ut = 2000;
3841 }
3842 if (p_ss < 0)
3843 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003844 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 p_ss = 0;
3846 }
3847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3850 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3851
3852 options[opt_idx].flags |= P_WAS_SET;
3853
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003854#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003855 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3856 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003857#endif
3858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003859 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003860 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003861 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003862 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003863 if ((opt_flags & OPT_NO_REDRAW) == 0)
3864 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 return errmsg;
3867}
3868
3869/*
3870 * Called after an option changed: check if something needs to be redrawn.
3871 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003873check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003875 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003876 int doclear = (flags & P_RCLR) == P_RCLR;
3877 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003879 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
3882 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3883 changed_window_setting();
3884 if (flags & P_RBUF)
3885 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003886 if (flags & P_RWINONLY)
3887 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003888 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 redraw_all_later(CLEAR);
3890 else if (all)
3891 redraw_all_later(NOT_VALID);
3892}
3893
3894/*
3895 * Find index for option 'arg'.
3896 * Return -1 if not found.
3897 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003898 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003899findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 int opt_idx;
3902 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003903 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 int is_term_opt;
3905
3906 /*
3907 * For first call: Initialize the quick-access table.
3908 * It contains the index for the first option that starts with a certain
3909 * letter. There are 26 letters, plus the first "t_" option.
3910 */
3911 if (quick_tab[1] == 0)
3912 {
3913 p = options[0].fullname;
3914 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3915 {
3916 if (s[0] != p[0])
3917 {
3918 if (s[0] == 't' && s[1] == '_')
3919 quick_tab[26] = opt_idx;
3920 else
3921 quick_tab[CharOrdLow(s[0])] = opt_idx;
3922 }
3923 p = s;
3924 }
3925 }
3926
3927 /*
3928 * Check for name starting with an illegal character.
3929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 return -1;
3932
3933 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3934 if (is_term_opt)
3935 opt_idx = quick_tab[26];
3936 else
3937 opt_idx = quick_tab[CharOrdLow(arg[0])];
3938 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3939 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003940 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942 }
3943 if (s == NULL && !is_term_opt)
3944 {
3945 opt_idx = quick_tab[CharOrdLow(arg[0])];
3946 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3947 {
3948 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003949 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 break;
3951 s = NULL;
3952 }
3953 }
3954 if (s == NULL)
3955 opt_idx = -1;
3956 return opt_idx;
3957}
3958
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003959#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960/*
3961 * Get the value for an option.
3962 *
3963 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003964 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003965 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003966 * String option: gov_string, *stringval gets allocated string.
3967 * Hidden Number option: gov_hidden_number.
3968 * Hidden Toggle option: gov_hidden_bool.
3969 * Hidden String option: gov_hidden_string.
3970 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003971 *
3972 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003974 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003975get_option_value(
3976 char_u *name,
3977 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003978 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003979 int *flagsp,
3980 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
3982 int opt_idx;
3983 char_u *varp;
3984
3985 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003986 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003987 {
3988 int key;
3989
3990 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003991 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003992 {
3993 char_u key_name[2];
3994 char_u *p;
3995
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003996 if (flagsp != NULL)
3997 *flagsp = 0; // terminal option has no flags
3998
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003999 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01004000 if (key < 0)
4001 {
4002 key_name[0] = KEY2TERMCAP0(key);
4003 key_name[1] = KEY2TERMCAP1(key);
4004 }
4005 else
4006 {
4007 key_name[0] = KS_KEY;
4008 key_name[1] = (key & 0xff);
4009 }
4010 p = find_termcode(key_name);
4011 if (p != NULL)
4012 {
4013 if (stringval != NULL)
4014 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004015 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004016 }
4017 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004018 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004021 varp = get_varp_scope(&(options[opt_idx]), scope);
4022
4023 if (flagsp != NULL)
4024 // Return the P_xxxx option flags.
4025 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 if (options[opt_idx].flags & P_STRING)
4028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004029 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004030 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 if (stringval != NULL)
4032 {
4033#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004034 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004035 if ((char_u **)varp == &curbuf->b_p_key
4036 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 *stringval = vim_strsave((char_u *)"*****");
4038 else
4039#endif
4040 *stringval = vim_strsave(*(char_u **)(varp));
4041 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004042 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004045 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004046 return (options[opt_idx].flags & P_NUM)
4047 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (options[opt_idx].flags & P_NUM)
4049 *numval = *(long *)varp;
4050 else
4051 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004052 // Special case: 'modified' is b_changed, but we also want to consider
4053 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 if ((int *)varp == &curbuf->b_changed)
4055 *numval = curbufIsChanged();
4056 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004057 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004059 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060}
4061#endif
4062
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004063#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004064/*
4065 * Returns the option attributes and its value. Unlike the above function it
4066 * will return either global value or local value of the option depending on
4067 * what was requested, but it will never return global value if it was
4068 * requested to return local one and vice versa. Neither it will return
4069 * buffer-local value if it was requested to return window-local one.
4070 *
4071 * Pretends that option is absent if it is not present in the requested scope
4072 * (i.e. has no global, window-local or buffer-local value depending on
4073 * opt_type). Uses
4074 *
4075 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004076 * 0 hidden or unknown option, also option that does not have requested
4077 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004078 * see SOPT_* in vim.h for other flags
4079 *
4080 * Possible opt_type values: see SREQ_* in vim.h
4081 */
4082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004083get_option_value_strict(
4084 char_u *name,
4085 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004086 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004087 int opt_type,
4088 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004089{
4090 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004091 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004092 struct vimoption *p;
4093 int r = 0;
4094
4095 opt_idx = findoption(name);
4096 if (opt_idx < 0)
4097 return 0;
4098
4099 p = &(options[opt_idx]);
4100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004102 if (p->var == NULL)
4103 return 0;
4104
4105 if (p->flags & P_BOOL)
4106 r |= SOPT_BOOL;
4107 else if (p->flags & P_NUM)
4108 r |= SOPT_NUM;
4109 else if (p->flags & P_STRING)
4110 r |= SOPT_STRING;
4111
4112 if (p->indir == PV_NONE)
4113 {
4114 if (opt_type == SREQ_GLOBAL)
4115 r |= SOPT_GLOBAL;
4116 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004117 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004118 }
4119 else
4120 {
4121 if (p->indir & PV_BOTH)
4122 r |= SOPT_GLOBAL;
4123 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004124 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004125
4126 if (p->indir & PV_WIN)
4127 {
4128 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004129 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004130 else
4131 r |= SOPT_WIN;
4132 }
4133 else if (p->indir & PV_BUF)
4134 {
4135 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004137 else
4138 r |= SOPT_BUF;
4139 }
4140 }
4141
4142 if (stringval == NULL)
4143 return r;
4144
4145 if (opt_type == SREQ_GLOBAL)
4146 varp = p->var;
4147 else
4148 {
4149 if (opt_type == SREQ_BUF)
4150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004151 // Special case: 'modified' is b_changed, but we also want to
4152 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004153 if (p->indir == PV_MOD)
4154 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004155 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004156 varp = NULL;
4157 }
4158#ifdef FEAT_CRYPT
4159 else if (p->indir == PV_KEY)
4160 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004161 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004162 *stringval = NULL;
4163 varp = NULL;
4164 }
4165#endif
4166 else
4167 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004168 buf_T *save_curbuf = curbuf;
4169
4170 // only getting a pointer, no need to use aucmd_prepbuf()
4171 curbuf = (buf_T *)from;
4172 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004173 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004174 curbuf = save_curbuf;
4175 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004176 }
4177 }
4178 else if (opt_type == SREQ_WIN)
4179 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004180 win_T *save_curwin = curwin;
4181
4182 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004183 curbuf = curwin->w_buffer;
4184 varp = get_varp(p);
4185 curwin = save_curwin;
4186 curbuf = curwin->w_buffer;
4187 }
4188 if (varp == p->var)
4189 return (r | SOPT_UNSET);
4190 }
4191
4192 if (varp != NULL)
4193 {
4194 if (p->flags & P_STRING)
4195 *stringval = vim_strsave(*(char_u **)(varp));
4196 else if (p->flags & P_NUM)
4197 *numval = *(long *) varp;
4198 else
4199 *numval = *(int *)varp;
4200 }
4201
4202 return r;
4203}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004204
4205/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004206 * Iterate over options. First argument is a pointer to a pointer to a
4207 * structure inside options[] array, second is option type like in the above
4208 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004209 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004210 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004211 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004212 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004213 * first argument to NULL.
4214 *
4215 * Returns full option name for current option on each call.
4216 */
4217 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004218option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004219{
4220 struct vimoption *ret = NULL;
4221 do
4222 {
4223 if (*option == NULL)
4224 *option = (void *) options;
4225 else if (((struct vimoption *) (*option))->fullname == NULL)
4226 {
4227 *option = NULL;
4228 return NULL;
4229 }
4230 else
4231 *option = (void *) (((struct vimoption *) (*option)) + 1);
4232
4233 ret = ((struct vimoption *) (*option));
4234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004235 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004236 if (ret->var == NULL)
4237 {
4238 ret = NULL;
4239 continue;
4240 }
4241
4242 switch (opt_type)
4243 {
4244 case SREQ_GLOBAL:
4245 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4246 ret = NULL;
4247 break;
4248 case SREQ_BUF:
4249 if (!(ret->indir & PV_BUF))
4250 ret = NULL;
4251 break;
4252 case SREQ_WIN:
4253 if (!(ret->indir & PV_WIN))
4254 ret = NULL;
4255 break;
4256 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004257 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004258 return NULL;
4259 }
4260 }
4261 while (ret == NULL);
4262
4263 return (char_u *)ret->fullname;
4264}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004265#endif
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004268 * Return the flags for the option at 'opt_idx'.
4269 */
4270 long_u
4271get_option_flags(int opt_idx)
4272{
4273 return options[opt_idx].flags;
4274}
4275
4276/*
4277 * Set a flag for the option at 'opt_idx'.
4278 */
4279 void
4280set_option_flag(int opt_idx, long_u flag)
4281{
4282 options[opt_idx].flags |= flag;
4283}
4284
4285/*
4286 * Clear a flag for the option at 'opt_idx'.
4287 */
4288 void
4289clear_option_flag(int opt_idx, long_u flag)
4290{
4291 options[opt_idx].flags &= ~flag;
4292}
4293
4294/*
4295 * Returns TRUE if the option at 'opt_idx' is a global option
4296 */
4297 int
4298is_global_option(int opt_idx)
4299{
4300 return options[opt_idx].indir == PV_NONE;
4301}
4302
4303/*
4304 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4305 * local value.
4306 */
4307 int
4308is_global_local_option(int opt_idx)
4309{
4310 return options[opt_idx].indir & PV_BOTH;
4311}
4312
4313/*
4314 * Returns TRUE if the option at 'opt_idx' is a window-local option
4315 */
4316 int
4317is_window_local_option(int opt_idx)
4318{
4319 return options[opt_idx].var == VAR_WIN;
4320}
4321
4322/*
4323 * Returns TRUE if the option at 'opt_idx' is a hidden option
4324 */
4325 int
4326is_hidden_option(int opt_idx)
4327{
4328 return options[opt_idx].var == NULL;
4329}
4330
4331#if defined(FEAT_CRYPT) || defined(PROTO)
4332/*
4333 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4334 */
4335 int
4336is_crypt_key_option(int opt_idx)
4337{
4338 return options[opt_idx].indir == PV_KEY;
4339}
4340#endif
4341
4342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 * Set the value of option "name".
4344 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004345 *
4346 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004348 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004349set_option_value(
4350 char_u *name,
4351 long number,
4352 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004353 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354{
4355 int opt_idx;
4356 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004357 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
4359 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004360 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004361 {
4362 int key;
4363
4364 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004365 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004366 {
4367 char_u key_name[2];
4368
4369 if (key < 0)
4370 {
4371 key_name[0] = KEY2TERMCAP0(key);
4372 key_name[1] = KEY2TERMCAP1(key);
4373 }
4374 else
4375 {
4376 key_name[0] = KS_KEY;
4377 key_name[1] = (key & 0xff);
4378 }
4379 add_termcode(key_name, string, FALSE);
4380 if (full_screen)
4381 ttest(FALSE);
4382 redraw_all_later(CLEAR);
4383 return NULL;
4384 }
4385
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004386 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else
4389 {
4390 flags = options[opt_idx].flags;
4391#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004392 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004394 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004395 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004396 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004399 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004400 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 else
4402 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004403 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004404 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004406 if (number == 0 && string != NULL)
4407 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004408 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004410 // Either we are given a string or we are setting option
4411 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004412 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004413 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004414 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004416 // There's another character after zeros or the string
4417 // is empty. In both cases, we are trying to set a
4418 // num option using a string.
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00004419 semsg(_(e_number_required_after_str_equal_str),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004420 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004421 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004422
4423 }
4424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004426 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004427 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004429 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004430 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 }
4433 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004434 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435}
4436
4437/*
4438 * Get the terminal code for a terminal option.
4439 * Returns NULL when not found.
4440 */
4441 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004442get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 int opt_idx;
4445 char_u *varp;
4446
4447 if (tname[0] != 't' || tname[1] != '_' ||
4448 tname[2] == NUL || tname[3] == NUL)
4449 return NULL;
4450 if ((opt_idx = findoption(tname)) >= 0)
4451 {
4452 varp = get_varp(&(options[opt_idx]));
4453 if (varp != NULL)
4454 varp = *(char_u **)(varp);
4455 return varp;
4456 }
4457 return find_termcode(tname + 2);
4458}
4459
4460 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004461get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 int i;
4464
4465 i = findoption((char_u *)"hl");
4466 if (i >= 0)
4467 return options[i].def_val[VI_DEFAULT];
4468 return (char_u *)NULL;
4469}
4470
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004471 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004472get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004473{
4474 int i;
4475
4476 i = findoption((char_u *)"enc");
4477 if (i >= 0)
4478 return options[i].def_val[VI_DEFAULT];
4479 return (char_u *)NULL;
4480}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004481
Bram Moolenaar4791fcd2022-02-23 12:06:00 +00004482 int
4483is_option_allocated(char *name)
4484{
4485 int idx = findoption((char_u *)name);
4486
4487 return idx >= 0 && (options[idx].flags & P_ALLOCED);
4488}
4489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490/*
4491 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004492 * When "has_lt" is true there is a '<' before "*arg_arg".
4493 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 */
4495 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004496find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004498 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004500 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /*
4503 * Don't use get_special_key_code() for t_xx, we don't want it to call
4504 * add_termcap_entry().
4505 */
4506 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4507 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004508 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004510 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004512 key = find_special_key(&arg, &modifiers,
4513 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004514 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 key = 0;
4516 }
4517 return key;
4518}
4519
4520/*
4521 * if 'all' == 0: show changed options
4522 * if 'all' == 1: show all normal options
4523 * if 'all' == 2: show all terminal options
4524 */
4525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004526showoptions(
4527 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004528 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529{
4530 struct vimoption *p;
4531 int col;
4532 int isterm;
4533 char_u *varp;
4534 struct vimoption **items;
4535 int item_count;
4536 int run;
4537 int row, rows;
4538 int cols;
4539 int i;
4540 int len;
4541
4542#define INC 20
4543#define GAP 3
4544
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004545 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (items == NULL)
4547 return;
4548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004549 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004551 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004553 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004555 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004557 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004560 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 * 1. display the short items
4562 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004563 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 */
4565 for (run = 1; run <= 2 && !got_int; ++run)
4566 {
4567 /*
4568 * collect the items in items[]
4569 */
4570 item_count = 0;
4571 for (p = &options[0]; p->fullname != NULL; p++)
4572 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004573 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004574 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004575 continue;
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 varp = NULL;
4578 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004579 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 if (p->indir != PV_NONE && !isterm)
4582 varp = get_varp_scope(p, opt_flags);
4583 }
4584 else
4585 varp = get_varp(p);
4586 if (varp != NULL
4587 && ((all == 2 && isterm)
4588 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004589 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004591 if (opt_flags & OPT_ONECOLUMN)
4592 len = Columns;
4593 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004594 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 else
4596 {
4597 option_value2string(p, opt_flags);
4598 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4599 }
4600 if ((len <= INC - GAP && run == 1) ||
4601 (len > INC - GAP && run == 2))
4602 items[item_count++] = p;
4603 }
4604 }
4605
4606 /*
4607 * display the items
4608 */
4609 if (run == 1)
4610 {
4611 cols = (Columns + GAP - 3) / INC;
4612 if (cols == 0)
4613 cols = 1;
4614 rows = (item_count + cols - 1) / cols;
4615 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004616 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 rows = item_count;
4618 for (row = 0; row < rows && !got_int; ++row)
4619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004620 msg_putchar('\n'); // go to next line
4621 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623 col = 0;
4624 for (i = row; i < item_count; i += rows)
4625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004626 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 showoneopt(items[i], opt_flags);
4628 col += INC;
4629 }
4630 out_flush();
4631 ui_breakcheck();
4632 }
4633 }
4634 vim_free(items);
4635}
4636
4637/*
4638 * Return TRUE if option "p" has its default value.
4639 */
4640 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004641optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 int dvi;
4644
4645 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004646 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004647 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004649 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004651 // the cast to long is required for Manx C, long_i is
4652 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004653 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4656}
4657
4658/*
4659 * showoneopt: show the value of one option
4660 * must not be called with a hidden option!
4661 */
4662 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004663showoneopt(
4664 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004665 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004667 char_u *varp;
4668 int save_silent = silent_mode;
4669
4670 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004671 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 varp = get_varp_scope(p, opt_flags);
4674
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004675 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4677 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004678 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004680 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004682 msg_puts(" ");
4683 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (!(p->flags & P_BOOL))
4685 {
4686 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004687 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 option_value2string(p, opt_flags);
4689 msg_outtrans(NameBuff);
4690 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004691
4692 silent_mode = save_silent;
4693 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694}
4695
4696/*
4697 * Write modified options as ":set" commands to a file.
4698 *
4699 * There are three values for "opt_flags":
4700 * OPT_GLOBAL: Write global option values and fresh values of
4701 * buffer-local options (used for start of a session
4702 * file).
4703 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4704 * curwin (used for a vimrc file).
4705 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4706 * and local values for window-local options of
4707 * curwin. Local values are also written when at the
4708 * default value, because a modeline or autocommand
4709 * may have set them when doing ":edit file" and the
4710 * user has set them back at the default or fresh
4711 * value.
4712 * When "local_only" is TRUE, don't write fresh
4713 * values, only local values (for ":mkview").
4714 * (fresh value = value used for a new buffer or window for a local option).
4715 *
4716 * Return FAIL on error, OK otherwise.
4717 */
4718 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004719makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004722 char_u *varp; // currently used value
4723 char_u *varp_fresh; // local value
4724 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 char *cmd;
4726 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004727 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 /*
4730 * The options that don't have a default (terminal name, columns, lines)
4731 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004732 * Do the loop over "options[]" twice: once for options with the
4733 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004735 for (pri = 1; pri >= 0; --pri)
4736 {
4737 for (p = &options[0]; !istermoption(p); p++)
4738 if (!(p->flags & P_NO_MKRC)
4739 && !istermoption(p)
4740 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004742 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4744 continue;
4745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004746 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4747 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4749 continue;
4750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004751 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004753 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 continue;
4755
Bram Moolenaard23b7142021-04-17 21:04:34 +02004756 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4757 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004758 continue;
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 round = 2;
4761 if (p->indir != PV_NONE)
4762 {
4763 if (p->var == VAR_WIN)
4764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004765 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (!(opt_flags & OPT_LOCAL))
4767 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004768 // When fresh value of window-local option is not at the
4769 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4771 {
4772 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004773 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 round = 1;
4776 varp_local = varp;
4777 varp = varp_fresh;
4778 }
4779 }
4780 }
4781 }
4782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // Round 1: fresh value for window-local options.
4784 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 for ( ; round <= 2; varp = varp_local, ++round)
4786 {
4787 if (round == 1 || (opt_flags & OPT_GLOBAL))
4788 cmd = "set";
4789 else
4790 cmd = "setlocal";
4791
4792 if (p->flags & P_BOOL)
4793 {
4794 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4795 return FAIL;
4796 }
4797 else if (p->flags & P_NUM)
4798 {
4799 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4800 return FAIL;
4801 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004802 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004804 int do_endif = FALSE;
4805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004806 // Don't set 'syntax' and 'filetype' again if the value is
4807 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004808 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004809#if defined(FEAT_SYN_HL)
4810 p->indir == PV_SYN ||
4811#endif
4812 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4815 *(char_u **)(varp)) < 0
4816 || put_eol(fd) < 0)
4817 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004818 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004821 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004823 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
4825 if (put_line(fd, "endif") == FAIL)
4826 return FAIL;
4827 }
4828 }
4829 }
4830 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 return OK;
4833}
4834
4835#if defined(FEAT_FOLDING) || defined(PROTO)
4836/*
4837 * Generate set commands for the local fold options only. Used when
4838 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4839 */
4840 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004841makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004843 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004845 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 == FAIL
4847# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004848 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004850 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 == FAIL
4852 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4853 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4854 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4855 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4856 )
4857 return FAIL;
4858
4859 return OK;
4860}
4861#endif
4862
4863 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004864put_setstring(
4865 FILE *fd,
4866 char *cmd,
4867 char *name,
4868 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004869 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004872 char_u *buf = NULL;
4873 char_u *part = NULL;
4874 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
4876 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4877 return FAIL;
4878 if (*valuep != NULL)
4879 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004880 // Output 'pastetoggle' as key names. For other
4881 // options some characters have to be escaped with
4882 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (valuep == &p_pt)
4884 {
4885 s = *valuep;
4886 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004887 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return FAIL;
4889 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004890 // expand the option value, replace $HOME by ~
4891 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004893 int size = (int)STRLEN(*valuep) + 1;
4894
4895 // replace home directory in the whole option value into "buf"
4896 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004897 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004898 goto fail;
4899 home_replace(NULL, *valuep, buf, size, FALSE);
4900
4901 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004902 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004903 // can be expanded when read back.
4904 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4905 && vim_strchr(*valuep, ',') != NULL)
4906 {
4907 part = alloc(size);
4908 if (part == NULL)
4909 goto fail;
4910
4911 // write line break to clear the option, e.g. ':set rtp='
4912 if (put_eol(fd) == FAIL)
4913 goto fail;
4914
4915 p = buf;
4916 while (*p != NUL)
4917 {
4918 // for each comma separated option part, append value to
4919 // the option, :set rtp+=value
4920 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4921 goto fail;
4922 (void)copy_option_part(&p, part, size, ",");
4923 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4924 goto fail;
4925 }
4926 vim_free(buf);
4927 vim_free(part);
4928 return OK;
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004931 {
4932 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004934 }
4935 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 else if (put_escstr(fd, *valuep, 2) == FAIL)
4938 return FAIL;
4939 }
4940 if (put_eol(fd) < 0)
4941 return FAIL;
4942 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004943fail:
4944 vim_free(buf);
4945 vim_free(part);
4946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947}
4948
4949 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004950put_setnum(
4951 FILE *fd,
4952 char *cmd,
4953 char *name,
4954 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955{
4956 long wc;
4957
4958 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4959 return FAIL;
4960 if (wc_use_keyname((char_u *)valuep, &wc))
4961 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004962 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4964 return FAIL;
4965 }
4966 else if (fprintf(fd, "%ld", *valuep) < 0)
4967 return FAIL;
4968 if (put_eol(fd) < 0)
4969 return FAIL;
4970 return OK;
4971}
4972
4973 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004974put_setbool(
4975 FILE *fd,
4976 char *cmd,
4977 char *name,
4978 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004980 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004981 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4983 || put_eol(fd) < 0)
4984 return FAIL;
4985 return OK;
4986}
4987
4988/*
4989 * Clear all the terminal options.
4990 * If the option has been allocated, free the memory.
4991 * Terminal options are never hidden or indirect.
4992 */
4993 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004994clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /*
4997 * Reset a few things before clearing the old options. This may cause
4998 * outputting a few things that the terminal doesn't understand, but the
4999 * screen will be cleared later, so this is OK.
5000 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005001 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005002 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005004 // When starting the GUI close the display opened for the clipboard.
5005 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (gui.starting)
5007 clear_xterm_clip();
5008#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005009 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005011 free_termoptions();
5012}
5013
5014 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005015free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005016{
5017 struct vimoption *p;
5018
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005019 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (istermoption(p))
5021 {
5022 if (p->flags & P_ALLOCED)
5023 free_string_option(*(char_u **)(p->var));
5024 if (p->flags & P_DEF_ALLOCED)
5025 free_string_option(p->def_val[VI_DEFAULT]);
5026 *(char_u **)(p->var) = empty_option;
5027 p->def_val[VI_DEFAULT] = empty_option;
5028 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005029#ifdef FEAT_EVAL
5030 // remember where the option was cleared
5031 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
5034 clear_termcodes();
5035}
5036
5037/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005038 * Free the string for one term option, if it was allocated.
5039 * Set the string to empty_option and clear allocated flag.
5040 * "var" points to the option value.
5041 */
5042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005043free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005044{
5045 struct vimoption *p;
5046
5047 for (p = &options[0]; p->fullname != NULL; p++)
5048 if (p->var == var)
5049 {
5050 if (p->flags & P_ALLOCED)
5051 free_string_option(*(char_u **)(p->var));
5052 *(char_u **)(p->var) = empty_option;
5053 p->flags &= ~P_ALLOCED;
5054 break;
5055 }
5056}
5057
5058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 * Set the terminal option defaults to the current value.
5060 * Used after setting the terminal name.
5061 */
5062 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005063set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 struct vimoption *p;
5066
5067 for (p = &options[0]; p->fullname != NULL; p++)
5068 {
5069 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5070 {
5071 if (p->flags & P_DEF_ALLOCED)
5072 {
5073 free_string_option(p->def_val[VI_DEFAULT]);
5074 p->flags &= ~P_DEF_ALLOCED;
5075 }
5076 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5077 if (p->flags & P_ALLOCED)
5078 {
5079 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005080 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 }
5083 }
5084}
5085
5086/*
5087 * return TRUE if 'p' starts with 't_'
5088 */
5089 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005090istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
5092 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5093}
5094
Bram Moolenaardac13472019-09-16 21:06:21 +02005095/*
5096 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5097 */
5098 int
5099istermoption_idx(int opt_idx)
5100{
5101 return istermoption(&options[opt_idx]);
5102}
5103
Bram Moolenaar113e1072019-01-20 15:30:40 +01005104#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106 * Unset local option value, similar to ":set opt<".
5107 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005108 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005109unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005110{
5111 struct vimoption *p;
5112 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005113 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005114
5115 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005116 if (opt_idx < 0)
5117 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005118 p = &(options[opt_idx]);
5119
5120 switch ((int)p->indir)
5121 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005122 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005123 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005124 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005125 break;
5126 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005127 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005128 break;
5129 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005130 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 break;
5132 case PV_AR:
5133 buf->b_p_ar = -1;
5134 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005135 case PV_BKC:
5136 clear_string_option(&buf->b_p_bkc);
5137 buf->b_bkc_flags = 0;
5138 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005139 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005140 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005141 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005142 case PV_TC:
5143 clear_string_option(&buf->b_p_tc);
5144 buf->b_tc_flags = 0;
5145 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005146 case PV_SISO:
5147 curwin->w_p_siso = -1;
5148 break;
5149 case PV_SO:
5150 curwin->w_p_so = -1;
5151 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005152#ifdef FEAT_FIND_ID
5153 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005154 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005155 break;
5156 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005157 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 break;
5159#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005160 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005161 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005162 break;
5163 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005164 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005165 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005166#ifdef FEAT_COMPL_FUNC
5167 case PV_TSRFU:
5168 clear_string_option(&buf->b_p_tsrfu);
5169 break;
5170#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005171 case PV_FP:
5172 clear_string_option(&buf->b_p_fp);
5173 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005174#ifdef FEAT_QUICKFIX
5175 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005176 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005177 break;
5178 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005179 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005180 break;
5181 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005182 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005183 break;
5184#endif
5185#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5186 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005187 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005188 break;
5189#endif
5190#if defined(FEAT_CRYPT)
5191 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005192 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005193 break;
5194#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005195#ifdef FEAT_LINEBREAK
5196 case PV_SBR:
5197 clear_string_option(&((win_T *)from)->w_p_sbr);
5198 break;
5199#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005200#ifdef FEAT_STL_OPT
5201 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005202 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005203 break;
5204#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005205 case PV_UL:
5206 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5207 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005208#ifdef FEAT_LISP
5209 case PV_LW:
5210 clear_string_option(&buf->b_p_lw);
5211 break;
5212#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005213 case PV_MENC:
5214 clear_string_option(&buf->b_p_menc);
5215 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005216 case PV_LCS:
5217 clear_string_option(&((win_T *)from)->w_p_lcs);
5218 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5219 redraw_later(NOT_VALID);
5220 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005221 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005222 clear_string_option(&((win_T *)from)->w_p_ve);
5223 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005224 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005225 }
5226}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005227#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005228
5229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005231 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 */
5233 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005234get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005236 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
5238 if (p->var == VAR_WIN)
5239 return (char_u *)GLOBAL_WO(get_varp(p));
5240 return p->var;
5241 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005242 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 switch ((int)p->indir)
5245 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005246 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005248 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5249 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5250 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005252 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5253 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5254 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005255 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005256 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005257 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005258 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5259 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005261 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5262 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005264 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5265 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005266#ifdef FEAT_COMPL_FUNC
5267 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5268#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005269#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5270 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5271#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005272#if defined(FEAT_CRYPT)
5273 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5274#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005275#ifdef FEAT_LINEBREAK
5276 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5277#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005278#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005279 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005280#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005281 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005282#ifdef FEAT_LISP
5283 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5284#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005285 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005286 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005287 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005288 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005291 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
5293 return get_varp(p);
5294}
5295
5296/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005297 * Get pointer to option variable at 'opt_idx', depending on local or global
5298 * scope.
5299 */
5300 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005301get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005302{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005303 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005304}
5305
5306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 * Get pointer to option variable.
5308 */
5309 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005310get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005312 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (p->var == NULL)
5314 return NULL;
5315
5316 switch ((int)p->indir)
5317 {
5318 case PV_NONE: return p->var;
5319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005321 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005323 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005325 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005327 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005329 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005331 case PV_TC: return *curbuf->b_p_tc != NUL
5332 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005333 case PV_BKC: return *curbuf->b_p_bkc != NUL
5334 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005335 case PV_SISO: return curwin->w_p_siso >= 0
5336 ? (char_u *)&(curwin->w_p_siso) : p->var;
5337 case PV_SO: return curwin->w_p_so >= 0
5338 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005340 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005342 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5344#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005345 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005347 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005349#ifdef FEAT_COMPL_FUNC
5350 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5351 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5352#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005353 case PV_FP: return *curbuf->b_p_fp != NUL
5354 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005356 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005358 case PV_GP: return *curbuf->b_p_gp != NUL
5359 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5360 case PV_MP: return *curbuf->b_p_mp != NUL
5361 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005363#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5364 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5365 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5366#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005367#if defined(FEAT_CRYPT)
5368 case PV_CM: return *curbuf->b_p_cm != NUL
5369 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5370#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005371#ifdef FEAT_LINEBREAK
5372 case PV_SBR: return *curwin->w_p_sbr != NUL
5373 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5374#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005375#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005376 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005377 ? (char_u *)&(curwin->w_p_stl) : p->var;
5378#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005379 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5380 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005381#ifdef FEAT_LISP
5382 case PV_LW: return *curbuf->b_p_lw != NUL
5383 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5384#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005385 case PV_MENC: return *curbuf->b_p_menc != NUL
5386 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#ifdef FEAT_ARABIC
5388 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5389#endif
5390 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005391 case PV_LCS: return *curwin->w_p_lcs != NUL
5392 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005393 case PV_VE: return *curwin->w_p_ve != NUL
5394 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005395#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005396 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5397#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005398#ifdef FEAT_SYN_HL
5399 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5400 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005401 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005402 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404#ifdef FEAT_DIFF
5405 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5406#endif
5407#ifdef FEAT_FOLDING
5408 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5409 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5410 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5411 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5412 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5413 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5414 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5415# ifdef FEAT_EVAL
5416 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5417 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5418# endif
5419 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5420#endif
5421 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005422 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005423#ifdef FEAT_LINEBREAK
5424 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005427 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005428#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5430#endif
5431#ifdef FEAT_RIGHTLEFT
5432 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5433 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5434#endif
5435 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5436 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5437#ifdef FEAT_LINEBREAK
5438 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005439 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5440 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005442 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005444 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005445#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005446 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5447 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5448#endif
5449#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005450 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5451 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5452 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5456 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5459 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5461 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5462#ifdef FEAT_CINDENT
5463 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5464 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5465 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5466#endif
5467#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5468 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471#ifdef FEAT_FOLDING
5472 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005475#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005476 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005478#ifdef FEAT_COMPL_FUNC
5479 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005480 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005481#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005482#ifdef FEAT_EVAL
5483 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005486 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005492 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5494 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5495 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5496 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5497#ifdef FEAT_FIND_ID
5498# ifdef FEAT_EVAL
5499 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5500# endif
5501#endif
5502#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5503 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5504 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5505#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005506#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005507 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509#ifdef FEAT_CRYPT
5510 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5511#endif
5512#ifdef FEAT_LISP
5513 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5514#endif
5515 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5516 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5517 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5518 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5519 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005521#ifdef FEAT_TEXTOBJ
5522 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5525#ifdef FEAT_SMARTINDENT
5526 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5530#ifdef FEAT_SEARCHPATH
5531 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5532#endif
5533 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5534#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005535 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005536 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5537#endif
5538#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005539 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5540 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5541 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005542 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#endif
5544 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5545 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5546 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5547 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005548#ifdef FEAT_PERSISTENT_UNDO
5549 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5552#ifdef FEAT_KEYMAP
5553 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5554#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005555#ifdef FEAT_SIGNS
5556 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5557#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005558#ifdef FEAT_VARTABS
5559 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5560 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5561#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005562 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005564 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 return (char_u *)&(curbuf->b_p_wm);
5566}
5567
5568/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005569 * Return a pointer to the variable for option at 'opt_idx'
5570 */
5571 char_u *
5572get_option_var(int opt_idx)
5573{
5574 return options[opt_idx].var;
5575}
5576
Dominique Pelle748b3082022-01-08 12:41:16 +00005577#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005578/*
5579 * Return the full name of the option at 'opt_idx'
5580 */
5581 char_u *
5582get_option_fullname(int opt_idx)
5583{
5584 return (char_u *)options[opt_idx].fullname;
5585}
Dominique Pelle748b3082022-01-08 12:41:16 +00005586#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005587
5588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 * Get the value of 'equalprg', either the buffer-local one or the global one.
5590 */
5591 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005592get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 if (*curbuf->b_p_ep == NUL)
5595 return p_ep;
5596 return curbuf->b_p_ep;
5597}
5598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599/*
5600 * Copy options from one window to another.
5601 * Used when splitting a window.
5602 */
5603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005604win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5607 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005608 after_copy_winopt(wp_to);
5609}
5610
5611/*
5612 * After copying window options: update variables depending on options.
5613 */
5614 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005615after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005616{
5617#ifdef FEAT_LINEBREAK
5618 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005619#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005620#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005621 fill_culopt_flags(NULL, wp);
5622 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005623#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005624 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627/*
5628 * Copy the options from one winopt_T to another.
5629 * Doesn't free the old option values in "to", use clear_winopt() for that.
5630 * The 'scroll' option is not copied, because it depends on the window height.
5631 * The 'previewwindow' option is reset, there can be only one preview window.
5632 */
5633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005634copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636#ifdef FEAT_ARABIC
5637 to->wo_arab = from->wo_arab;
5638#endif
5639 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005640 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005642 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005643 to->wo_ve = vim_strsave(from->wo_ve);
5644 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005645#ifdef FEAT_LINEBREAK
5646 to->wo_nuw = from->wo_nuw;
5647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#ifdef FEAT_RIGHTLEFT
5649 to->wo_rl = from->wo_rl;
5650 to->wo_rlc = vim_strsave(from->wo_rlc);
5651#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005652#ifdef FEAT_LINEBREAK
5653 to->wo_sbr = vim_strsave(from->wo_sbr);
5654#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005655#ifdef FEAT_STL_OPT
5656 to->wo_stl = vim_strsave(from->wo_stl);
5657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005659#ifdef FEAT_DIFF
5660 to->wo_wrap_save = from->wo_wrap_save;
5661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662#ifdef FEAT_LINEBREAK
5663 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005664 to->wo_bri = from->wo_bri;
5665 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005667 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005669 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005670 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005671 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005672#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005673 to->wo_spell = from->wo_spell;
5674#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005675#ifdef FEAT_SYN_HL
5676 to->wo_cuc = from->wo_cuc;
5677 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005678 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005679 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#ifdef FEAT_DIFF
5682 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005683 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005685#ifdef FEAT_CONCEAL
5686 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005687 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005688#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005689#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005690 to->wo_twk = vim_strsave(from->wo_twk);
5691 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693#ifdef FEAT_FOLDING
5694 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005695 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005697 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 to->wo_fdi = vim_strsave(from->wo_fdi);
5699 to->wo_fml = from->wo_fml;
5700 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005701 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005703 to->wo_fdm_save = from->wo_diff_saved
5704 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 to->wo_fdn = from->wo_fdn;
5706# ifdef FEAT_EVAL
5707 to->wo_fde = vim_strsave(from->wo_fde);
5708 to->wo_fdt = vim_strsave(from->wo_fdt);
5709# endif
5710 to->wo_fmr = vim_strsave(from->wo_fmr);
5711#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005712#ifdef FEAT_SIGNS
5713 to->wo_scl = vim_strsave(from->wo_scl);
5714#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005715
5716#ifdef FEAT_EVAL
5717 // Copy the script context so that we know where the value was last set.
5718 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5719 sizeof(to->wo_script_ctx));
5720#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005721 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722}
5723
5724/*
5725 * Check string options in a window for a NULL value.
5726 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005727 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005728check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 check_winopt(&win->w_onebuf_opt);
5731 check_winopt(&win->w_allbuf_opt);
5732}
5733
5734/*
5735 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5736 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005737 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005738check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740#ifdef FEAT_FOLDING
5741 check_string_option(&wop->wo_fdi);
5742 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005743 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744# ifdef FEAT_EVAL
5745 check_string_option(&wop->wo_fde);
5746 check_string_option(&wop->wo_fdt);
5747# endif
5748 check_string_option(&wop->wo_fmr);
5749#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005750#ifdef FEAT_SIGNS
5751 check_string_option(&wop->wo_scl);
5752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#ifdef FEAT_RIGHTLEFT
5754 check_string_option(&wop->wo_rlc);
5755#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005756#ifdef FEAT_LINEBREAK
5757 check_string_option(&wop->wo_sbr);
5758#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005759#ifdef FEAT_STL_OPT
5760 check_string_option(&wop->wo_stl);
5761#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005763 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764 check_string_option(&wop->wo_cc);
5765#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005766#ifdef FEAT_CONCEAL
5767 check_string_option(&wop->wo_cocu);
5768#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005769#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005770 check_string_option(&wop->wo_twk);
5771 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005772#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005773#ifdef FEAT_LINEBREAK
5774 check_string_option(&wop->wo_briopt);
5775#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005776 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005777 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005778 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779}
5780
5781/*
5782 * Free the allocated memory inside a winopt_T.
5783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005785clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787#ifdef FEAT_FOLDING
5788 clear_string_option(&wop->wo_fdi);
5789 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005790 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791# ifdef FEAT_EVAL
5792 clear_string_option(&wop->wo_fde);
5793 clear_string_option(&wop->wo_fdt);
5794# endif
5795 clear_string_option(&wop->wo_fmr);
5796#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005797#ifdef FEAT_SIGNS
5798 clear_string_option(&wop->wo_scl);
5799#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005800#ifdef FEAT_LINEBREAK
5801 clear_string_option(&wop->wo_briopt);
5802#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005803 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804#ifdef FEAT_RIGHTLEFT
5805 clear_string_option(&wop->wo_rlc);
5806#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005807#ifdef FEAT_LINEBREAK
5808 clear_string_option(&wop->wo_sbr);
5809#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005810#ifdef FEAT_STL_OPT
5811 clear_string_option(&wop->wo_stl);
5812#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005813#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005814 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005815 clear_string_option(&wop->wo_cc);
5816#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005817#ifdef FEAT_CONCEAL
5818 clear_string_option(&wop->wo_cocu);
5819#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005820#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005821 clear_string_option(&wop->wo_twk);
5822 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005823#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005824 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005825 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826}
5827
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005828#ifdef FEAT_EVAL
5829// Index into the options table for a buffer-local option enum.
5830static int buf_opt_idx[BV_COUNT];
5831# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5832
5833/*
5834 * Initialize buf_opt_idx[] if not done already.
5835 */
5836 static void
5837init_buf_opt_idx(void)
5838{
5839 static int did_init_buf_opt_idx = FALSE;
5840 int i;
5841
5842 if (did_init_buf_opt_idx)
5843 return;
5844 did_init_buf_opt_idx = TRUE;
5845 for (i = 0; !istermoption_idx(i); i++)
5846 if (options[i].indir & PV_BUF)
5847 buf_opt_idx[options[i].indir & PV_MASK] = i;
5848}
5849#else
5850# define COPY_OPT_SCTX(buf, bv)
5851#endif
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853/*
5854 * Copy global option values to local options for one buffer.
5855 * Used when creating a new buffer and sometimes when entering a buffer.
5856 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005857 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5859 * appropriate.
5860 * BCO_NOHELP Don't copy the values to a help buffer.
5861 */
5862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005863buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005866 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 int dont_do_help;
5868 int did_isk = FALSE;
5869
5870 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 * Skip this when the option defaults have not been set yet. Happens when
5872 * main() allocates the first buffer.
5873 */
5874 if (p_cpo != NULL)
5875 {
5876 /*
5877 * Always copy when entering and 'cpo' contains 'S'.
5878 * Don't copy when already initialized.
5879 * Don't copy when 'cpo' contains 's' and not entering.
5880 * 'S' BCO_ENTER initialized 's' should_copy
5881 * yes yes X X TRUE
5882 * yes no yes X FALSE
5883 * no X yes X FALSE
5884 * X no no yes FALSE
5885 * X no no no TRUE
5886 * no yes no X TRUE
5887 */
5888 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5889 && (buf->b_p_initialized
5890 || (!(flags & BCO_ENTER)
5891 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5892 should_copy = FALSE;
5893
5894 if (should_copy || (flags & BCO_ALWAYS))
5895 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005896#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005897 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005898 init_buf_opt_idx();
5899#endif
5900 // Don't copy the options specific to a help buffer when
5901 // BCO_NOHELP is given or the options were initialized already
5902 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5904 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005905 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
5907 save_p_isk = buf->b_p_isk;
5908 buf->b_p_isk = NULL;
5909 }
5910 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005911 * Always free the allocated strings. If not already initialized,
5912 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 */
5914 if (!buf->b_p_initialized)
5915 {
5916 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005917 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005920 switch (*p_ffs)
5921 {
5922 case 'm':
5923 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5924 case 'd':
5925 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5926 case 'u':
5927 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5928 default:
5929 buf->b_p_ff = vim_strsave(p_ff);
5930 }
5931 if (buf->b_p_ff != NULL)
5932 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_bh = empty_option;
5934 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 }
5936 else
5937 free_buf_options(buf, FALSE);
5938
5939 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 buf->b_p_ai_nopaste = p_ai_nopaste;
5942 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005943 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 buf->b_p_tw_nopaste = p_tw_nopaste;
5947 buf->b_p_tw_nobin = p_tw_nobin;
5948 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 buf->b_p_wm_nopaste = p_wm_nopaste;
5951 buf->b_p_wm_nobin = p_wm_nobin;
5952 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005953 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005954 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005956 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005961 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 buf->b_p_ml_nobin = p_ml_nobin;
5965 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005966 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005967 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005968 buf->b_p_swf = FALSE;
5969 else
5970 {
5971 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005972 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005975 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005976#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005977 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005978 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005980#ifdef FEAT_COMPL_FUNC
5981 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005982 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005983 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005984 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005985 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005986 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005987#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005988#ifdef FEAT_EVAL
5989 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005991 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005994 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005996#ifdef FEAT_VARTABS
5997 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005998 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005999 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006000 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006001 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006002 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006003 buf->b_p_vsts_nopaste = p_vsts_nopaste
6004 ? vim_strsave(p_vsts_nopaste) : NULL;
6005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006009 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010#ifdef FEAT_FOLDING
6011 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006012 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#endif
6014 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006015 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006016 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006018 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6019 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006023 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#ifdef FEAT_SMARTINDENT
6025 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006026 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027#endif
6028 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006029 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030#ifdef FEAT_CINDENT
6031 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006032 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006034 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006038 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006041 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6043 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006044 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045#endif
6046#ifdef FEAT_LISP
6047 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006048 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049#endif
6050#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006051 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006053 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006054 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006055 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006056#endif
6057#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006058 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006059 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006060 (void)compile_cap_prog(&buf->b_s);
6061 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006062 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006063 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006064 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006065 buf->b_s.b_p_spo = vim_strsave(p_spo);
6066 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#endif
6068#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6069 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006070 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006072 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006074 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006075#if defined(FEAT_EVAL)
6076 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006077 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079#ifdef FEAT_CRYPT
6080 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006081 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#endif
6083#ifdef FEAT_SEARCHPATH
6084 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006085 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086#endif
6087#ifdef FEAT_KEYMAP
6088 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006089 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 buf->b_kmap_state |= KEYMAP_INIT;
6091#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006092#ifdef FEAT_TERMINAL
6093 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006094 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006095#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006096 // This isn't really an option, but copying the langmap and IME
6097 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006099 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006101 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006103 // options that are normally global but also have a local value
6104 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006106 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006107 buf->b_p_bkc = empty_option;
6108 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109#ifdef FEAT_QUICKFIX
6110 buf->b_p_gp = empty_option;
6111 buf->b_p_mp = empty_option;
6112 buf->b_p_efm = empty_option;
6113#endif
6114 buf->b_p_ep = empty_option;
6115 buf->b_p_kp = empty_option;
6116 buf->b_p_path = empty_option;
6117 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006118 buf->b_p_tc = empty_option;
6119 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120#ifdef FEAT_FIND_ID
6121 buf->b_p_def = empty_option;
6122 buf->b_p_inc = empty_option;
6123# ifdef FEAT_EVAL
6124 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006125 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126# endif
6127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 buf->b_p_dict = empty_option;
6129 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006130#ifdef FEAT_COMPL_FUNC
6131 buf->b_p_tsrfu = empty_option;
6132#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006133#ifdef FEAT_TEXTOBJ
6134 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006135 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006136#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006137#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6138 buf->b_p_bexpr = empty_option;
6139#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006140#if defined(FEAT_CRYPT)
6141 buf->b_p_cm = empty_option;
6142#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006143#ifdef FEAT_PERSISTENT_UNDO
6144 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006145 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006146#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006147#ifdef FEAT_LISP
6148 buf->b_p_lw = empty_option;
6149#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006150 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 /*
6153 * Don't copy the options set by ex_help(), use the saved values,
6154 * when going from a help buffer to a non-help buffer.
6155 * Don't touch these at all when BCO_NOHELP is used and going from
6156 * or to a help buffer.
6157 */
6158 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006161#ifdef FEAT_VARTABS
6162 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006163 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006164 else
6165 buf->b_p_vts_array = NULL;
6166#endif
6167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 else
6169 {
6170 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006171 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 did_isk = TRUE;
6173 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006174 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006175#ifdef FEAT_VARTABS
6176 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006177 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006178 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006179 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006180 else
6181 buf->b_p_vts_array = NULL;
6182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (buf->b_p_bt[0] == 'h')
6185 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006187 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 }
6189 }
6190
6191 /*
6192 * When the options should be copied (ignoring BCO_ALWAYS), set the
6193 * flag that indicates that the options have been initialized.
6194 */
6195 if (should_copy)
6196 buf->b_p_initialized = TRUE;
6197 }
6198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006199 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 if (did_isk)
6201 (void)buf_init_chartab(buf, FALSE);
6202}
6203
6204/*
6205 * Reset the 'modifiable' option and its default value.
6206 */
6207 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006208reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 int opt_idx;
6211
6212 curbuf->b_p_ma = FALSE;
6213 p_ma = FALSE;
6214 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006215 if (opt_idx >= 0)
6216 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217}
6218
6219/*
6220 * Set the global value for 'iminsert' to the local value.
6221 */
6222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006223set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
6225 p_iminsert = curbuf->b_p_iminsert;
6226}
6227
6228/*
6229 * Set the global value for 'imsearch' to the local value.
6230 */
6231 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006232set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
6234 p_imsearch = curbuf->b_p_imsearch;
6235}
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237static int expand_option_idx = -1;
6238static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6239static int expand_option_flags = 0;
6240
6241 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006242set_context_in_set_cmd(
6243 expand_T *xp,
6244 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006245 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
6247 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006248 long_u flags = 0; // init for GCC
6249 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 char_u *p;
6251 char_u *s;
6252 int is_term_option = FALSE;
6253 int key;
6254
6255 expand_option_flags = opt_flags;
6256
6257 xp->xp_context = EXPAND_SETTINGS;
6258 if (*arg == NUL)
6259 {
6260 xp->xp_pattern = arg;
6261 return;
6262 }
6263 p = arg + STRLEN(arg) - 1;
6264 if (*p == ' ' && *(p - 1) != '\\')
6265 {
6266 xp->xp_pattern = p + 1;
6267 return;
6268 }
6269 while (p > arg)
6270 {
6271 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006272 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (*p == ' ' || *p == ',')
6274 {
6275 while (s > arg && *(s - 1) == '\\')
6276 --s;
6277 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006278 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (*p == ' ' && ((p - s) & 1) == 0)
6280 {
6281 ++p;
6282 break;
6283 }
6284 --p;
6285 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006286 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
6288 xp->xp_context = EXPAND_BOOL_SETTINGS;
6289 p += 2;
6290 }
6291 if (STRNCMP(p, "inv", 3) == 0)
6292 {
6293 xp->xp_context = EXPAND_BOOL_SETTINGS;
6294 p += 3;
6295 }
6296 xp->xp_pattern = arg = p;
6297 if (*arg == '<')
6298 {
6299 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006300 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 return;
6302 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
6305 xp->xp_context = EXPAND_NOTHING;
6306 return;
6307 }
6308 nextchar = *++p;
6309 is_term_option = TRUE;
6310 expand_option_name[2] = KEY2TERMCAP0(key);
6311 expand_option_name[3] = KEY2TERMCAP1(key);
6312 }
6313 else
6314 {
6315 if (p[0] == 't' && p[1] == '_')
6316 {
6317 p += 2;
6318 if (*p != NUL)
6319 ++p;
6320 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006321 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 nextchar = *++p;
6323 is_term_option = TRUE;
6324 expand_option_name[2] = p[-2];
6325 expand_option_name[3] = p[-1];
6326 }
6327 else
6328 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006329 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6331 p++;
6332 if (*p == NUL)
6333 return;
6334 nextchar = *p;
6335 *p = NUL;
6336 opt_idx = findoption(arg);
6337 *p = nextchar;
6338 if (opt_idx == -1 || options[opt_idx].var == NULL)
6339 {
6340 xp->xp_context = EXPAND_NOTHING;
6341 return;
6342 }
6343 flags = options[opt_idx].flags;
6344 if (flags & P_BOOL)
6345 {
6346 xp->xp_context = EXPAND_NOTHING;
6347 return;
6348 }
6349 }
6350 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006351 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6353 {
6354 ++p;
6355 nextchar = '=';
6356 }
6357 if ((nextchar != '=' && nextchar != ':')
6358 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6359 {
6360 xp->xp_context = EXPAND_UNSUCCESSFUL;
6361 return;
6362 }
6363 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6364 {
6365 xp->xp_context = EXPAND_OLD_SETTING;
6366 if (is_term_option)
6367 expand_option_idx = -1;
6368 else
6369 expand_option_idx = opt_idx;
6370 xp->xp_pattern = p + 1;
6371 return;
6372 }
6373 xp->xp_context = EXPAND_NOTHING;
6374 if (is_term_option || (flags & P_NUM))
6375 return;
6376
6377 xp->xp_pattern = p + 1;
6378
6379 if (flags & P_EXPAND)
6380 {
6381 p = options[opt_idx].var;
6382 if (p == (char_u *)&p_bdir
6383 || p == (char_u *)&p_dir
6384 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006385 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 || p == (char_u *)&p_rtp
6387#ifdef FEAT_SEARCHPATH
6388 || p == (char_u *)&p_cdpath
6389#endif
6390#ifdef FEAT_SESSION
6391 || p == (char_u *)&p_vdir
6392#endif
6393 )
6394 {
6395 xp->xp_context = EXPAND_DIRECTORIES;
6396 if (p == (char_u *)&p_path
6397#ifdef FEAT_SEARCHPATH
6398 || p == (char_u *)&p_cdpath
6399#endif
6400 )
6401 xp->xp_backslash = XP_BS_THREE;
6402 else
6403 xp->xp_backslash = XP_BS_ONE;
6404 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006405 else if (p == (char_u *)&p_ft)
6406 {
6407 xp->xp_context = EXPAND_FILETYPE;
6408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 else
6410 {
6411 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006412 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (p == (char_u *)&p_tags)
6414 xp->xp_backslash = XP_BS_THREE;
6415 else
6416 xp->xp_backslash = XP_BS_ONE;
6417 }
6418 }
6419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006420 // For an option that is a list of file names, find the start of the
6421 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6423 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006424 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 if (*p == ' ' || *p == ',')
6426 {
6427 s = p;
6428 while (s > xp->xp_pattern && *(s - 1) == '\\')
6429 --s;
6430 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6431 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6432 {
6433 xp->xp_pattern = p + 1;
6434 break;
6435 }
6436 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006437
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006438#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006439 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006440 if (options[opt_idx].var == (char_u *)&p_sps
6441 && STRNCMP(p, "file:", 5) == 0)
6442 {
6443 xp->xp_pattern = p + 5;
6444 break;
6445 }
6446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448}
6449
6450 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006451ExpandSettings(
6452 expand_T *xp,
6453 regmatch_T *regmatch,
6454 int *num_file,
6455 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006457 int num_normal = 0; // Nr of matching non-term-code settings
6458 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 int opt_idx;
6460 int match;
6461 int count = 0;
6462 char_u *str;
6463 int loop;
6464 int is_term_opt;
6465 char_u name_buf[MAX_KEY_NAME_LEN];
6466 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006469 // do this loop twice:
6470 // loop == 0: count the number of matching options
6471 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 for (loop = 0; loop <= 1; ++loop)
6473 {
6474 regmatch->rm_ic = ic;
6475 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6476 {
K.Takataeeec2542021-06-02 13:28:16 +02006477 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6479 {
6480 if (loop == 0)
6481 num_normal++;
6482 else
6483 (*file)[count++] = vim_strsave((char_u *)names[match]);
6484 }
6485 }
6486 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6487 opt_idx++)
6488 {
6489 if (options[opt_idx].var == NULL)
6490 continue;
6491 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6492 && !(options[opt_idx].flags & P_BOOL))
6493 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006494 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 if (is_term_opt && num_normal > 0)
6496 continue;
6497 match = FALSE;
6498 if (vim_regexec(regmatch, str, (colnr_T)0)
6499 || (options[opt_idx].shortname != NULL
6500 && vim_regexec(regmatch,
6501 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6502 match = TRUE;
6503 else if (is_term_opt)
6504 {
6505 name_buf[0] = '<';
6506 name_buf[1] = 't';
6507 name_buf[2] = '_';
6508 name_buf[3] = str[2];
6509 name_buf[4] = str[3];
6510 name_buf[5] = '>';
6511 name_buf[6] = NUL;
6512 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6513 {
6514 match = TRUE;
6515 str = name_buf;
6516 }
6517 }
6518 if (match)
6519 {
6520 if (loop == 0)
6521 {
6522 if (is_term_opt)
6523 num_term++;
6524 else
6525 num_normal++;
6526 }
6527 else
6528 (*file)[count++] = vim_strsave(str);
6529 }
6530 }
6531 /*
6532 * Check terminal key codes, these are not in the option table
6533 */
6534 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6535 {
6536 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6537 {
6538 if (!isprint(str[0]) || !isprint(str[1]))
6539 continue;
6540
6541 name_buf[0] = 't';
6542 name_buf[1] = '_';
6543 name_buf[2] = str[0];
6544 name_buf[3] = str[1];
6545 name_buf[4] = NUL;
6546
6547 match = FALSE;
6548 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6549 match = TRUE;
6550 else
6551 {
6552 name_buf[0] = '<';
6553 name_buf[1] = 't';
6554 name_buf[2] = '_';
6555 name_buf[3] = str[0];
6556 name_buf[4] = str[1];
6557 name_buf[5] = '>';
6558 name_buf[6] = NUL;
6559
6560 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6561 match = TRUE;
6562 }
6563 if (match)
6564 {
6565 if (loop == 0)
6566 num_term++;
6567 else
6568 (*file)[count++] = vim_strsave(name_buf);
6569 }
6570 }
6571
6572 /*
6573 * Check special key names.
6574 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006575 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6577 {
6578 name_buf[0] = '<';
6579 STRCPY(name_buf + 1, str);
6580 STRCAT(name_buf, ">");
6581
6582 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6583 {
6584 if (loop == 0)
6585 num_term++;
6586 else
6587 (*file)[count++] = vim_strsave(name_buf);
6588 }
6589 }
6590 }
6591 if (loop == 0)
6592 {
6593 if (num_normal > 0)
6594 *num_file = num_normal;
6595 else if (num_term > 0)
6596 *num_file = num_term;
6597 else
6598 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006599 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 if (*file == NULL)
6601 {
6602 *file = (char_u **)"";
6603 return FAIL;
6604 }
6605 }
6606 }
6607 return OK;
6608}
6609
6610 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006613 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 char_u *buf;
6615
6616 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006617 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 if (*file == NULL)
6619 return FAIL;
6620
6621 /*
6622 * For a terminal key code expand_option_idx is < 0.
6623 */
6624 if (expand_option_idx < 0)
6625 {
6626 var = find_termcode(expand_option_name + 2);
6627 if (var == NULL)
6628 expand_option_idx = findoption(expand_option_name);
6629 }
6630
6631 if (expand_option_idx >= 0)
6632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006633 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 option_value2string(&options[expand_option_idx], expand_option_flags);
6635 var = NameBuff;
6636 }
6637 else if (var == NULL)
6638 var = (char_u *)"";
6639
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006640 // A backslash is required before some characters. This is the reverse of
6641 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 buf = vim_strsave_escaped(var, escape_chars);
6643
6644 if (buf == NULL)
6645 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006646 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 return FAIL;
6648 }
6649
6650#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006651 // For MS-Windows et al. we don't double backslashes at the start and
6652 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006653 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (var[0] == '\\' && var[1] == '\\'
6655 && expand_option_idx >= 0
6656 && (options[expand_option_idx].flags & P_EXPAND)
6657 && vim_isfilec(var[2])
6658 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006659 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660#endif
6661
6662 *file[0] = buf;
6663 *num_file = 1;
6664 return OK;
6665}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667/*
6668 * Get the value for the numeric or string option *opp in a nice format into
6669 * NameBuff[]. Must not be called with a hidden option!
6670 */
6671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006672option_value2string(
6673 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006674 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 char_u *varp;
6677
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006678 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 if (opp->flags & P_NUM)
6681 {
6682 long wc = 0;
6683
6684 if (wc_use_keyname(varp, &wc))
6685 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6686 else if (wc != 0)
6687 STRCPY(NameBuff, transchar((int)wc));
6688 else
6689 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6690 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006691 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {
6693 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006694 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 NameBuff[0] = NUL;
6696#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006697 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006698 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 STRCPY(NameBuff, "*****");
6700#endif
6701 else if (opp->flags & P_EXPAND)
6702 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006703 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 else if ((char_u **)opp->var == &p_pt)
6705 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6706 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006707 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
6709}
6710
6711/*
6712 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6713 * printed as a keyname.
6714 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6715 */
6716 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006717wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6720 {
6721 *wcp = *(long *)varp;
6722 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6723 return TRUE;
6724 }
6725 return FALSE;
6726}
6727
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 * Return TRUE if "x" is present in 'shortmess' option, or
6730 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6731 */
6732 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006733shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006735 return p_shm != NULL &&
6736 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 || (vim_strchr(p_shm, 'a') != NULL
6738 && vim_strchr((char_u *)SHM_A, x) != NULL));
6739}
6740
6741/*
6742 * paste_option_changed() - Called after p_paste was set or reset.
6743 */
6744 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006745paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 static int old_p_paste = FALSE;
6748 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006749 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750#ifdef FEAT_CMDL_INFO
6751 static int save_ru = 0;
6752#endif
6753#ifdef FEAT_RIGHTLEFT
6754 static int save_ri = 0;
6755 static int save_hkmap = 0;
6756#endif
6757 buf_T *buf;
6758
6759 if (p_paste)
6760 {
6761 /*
6762 * Paste switched from off to on.
6763 * Save the current values, so they can be restored later.
6764 */
6765 if (!old_p_paste)
6766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006767 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006768 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
6770 buf->b_p_tw_nopaste = buf->b_p_tw;
6771 buf->b_p_wm_nopaste = buf->b_p_wm;
6772 buf->b_p_sts_nopaste = buf->b_p_sts;
6773 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006774 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006775#ifdef FEAT_VARTABS
6776 if (buf->b_p_vsts_nopaste)
6777 vim_free(buf->b_p_vsts_nopaste);
6778 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6779 ? vim_strsave(buf->b_p_vsts) : NULL;
6780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
6782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006783 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006785 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786#ifdef FEAT_CMDL_INFO
6787 save_ru = p_ru;
6788#endif
6789#ifdef FEAT_RIGHTLEFT
6790 save_ri = p_ri;
6791 save_hkmap = p_hkmap;
6792#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006793 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006794 p_ai_nopaste = p_ai;
6795 p_et_nopaste = p_et;
6796 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 p_tw_nopaste = p_tw;
6798 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006799#ifdef FEAT_VARTABS
6800 if (p_vsts_nopaste)
6801 vim_free(p_vsts_nopaste);
6802 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 }
6805
6806 /*
6807 * Always set the option values, also when 'paste' is set when it is
6808 * already on.
6809 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006810 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006811 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006813 buf->b_p_tw = 0; // textwidth is 0
6814 buf->b_p_wm = 0; // wrapmargin is 0
6815 buf->b_p_sts = 0; // softtabstop is 0
6816 buf->b_p_ai = 0; // no auto-indent
6817 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006818#ifdef FEAT_VARTABS
6819 if (buf->b_p_vsts)
6820 free_string_option(buf->b_p_vsts);
6821 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006822 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
6825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006826 // set global options
6827 p_sm = 0; // no showmatch
6828 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006831 status_redraw_all(); // redraw to remove the ruler
6832 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833#endif
6834#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006835 p_ri = 0; // no reverse insert
6836 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006838 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 p_tw = 0;
6840 p_wm = 0;
6841 p_sts = 0;
6842 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006843#ifdef FEAT_VARTABS
6844 if (p_vsts)
6845 free_string_option(p_vsts);
6846 p_vsts = empty_option;
6847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849
6850 /*
6851 * Paste switched from on to off: Restore saved values.
6852 */
6853 else if (old_p_paste)
6854 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006855 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006856 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {
6858 buf->b_p_tw = buf->b_p_tw_nopaste;
6859 buf->b_p_wm = buf->b_p_wm_nopaste;
6860 buf->b_p_sts = buf->b_p_sts_nopaste;
6861 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006862 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006863#ifdef FEAT_VARTABS
6864 if (buf->b_p_vsts)
6865 free_string_option(buf->b_p_vsts);
6866 buf->b_p_vsts = buf->b_p_vsts_nopaste
6867 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006868 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006869 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006870 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006871 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006872 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006876 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006878 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006881 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 p_ru = save_ru;
6883#endif
6884#ifdef FEAT_RIGHTLEFT
6885 p_ri = save_ri;
6886 p_hkmap = save_hkmap;
6887#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006888 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006889 p_ai = p_ai_nopaste;
6890 p_et = p_et_nopaste;
6891 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 p_tw = p_tw_nopaste;
6893 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006894#ifdef FEAT_VARTABS
6895 if (p_vsts)
6896 free_string_option(p_vsts);
6897 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900
6901 old_p_paste = p_paste;
6902}
6903
6904/*
6905 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6906 *
6907 * Reset 'compatible' and set the values for options that didn't get set yet
6908 * to the Vim defaults.
6909 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 */
6912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006913vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006916 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006917 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
6919 if (!option_was_set((char_u *)"cp"))
6920 {
6921 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006922 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6924 set_option_default(opt_idx, OPT_FREE, FALSE);
6925 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006926 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006928
6929 if (fname != NULL)
6930 {
6931 p = vim_getenv(envname, &dofree);
6932 if (p == NULL)
6933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006934 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006935 p = FullName_save(fname, FALSE);
6936 if (p != NULL)
6937 {
6938 vim_setenv(envname, p);
6939 vim_free(p);
6940 }
6941 }
6942 else if (dofree)
6943 vim_free(p);
6944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945}
6946
6947/*
6948 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6949 */
6950 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006951change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006953 int opt_idx;
6954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if (p_cp != on)
6956 {
6957 p_cp = on;
6958 compatible_set();
6959 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006960 opt_idx = findoption((char_u *)"cp");
6961 if (opt_idx >= 0)
6962 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963}
6964
6965/*
6966 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006967 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 */
6969 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006970option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
6972 int idx;
6973
6974 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006975 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 return FALSE;
6977 if (options[idx].flags & P_WAS_SET)
6978 return TRUE;
6979 return FALSE;
6980}
6981
6982/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006983 * Reset the flag indicating option "name" was set.
6984 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006985 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006986reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006987{
6988 int idx = findoption(name);
6989
6990 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006991 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006992 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006993 return OK;
6994 }
6995 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006996}
6997
6998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 * compatible_set() - Called when 'compatible' has been set or unset.
7000 *
7001 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
7002 * flag) to a Vi compatible value.
7003 * When 'compatible' is unset: Set all options that have a different default
7004 * for Vim (without the P_VI_DEF flag) to that default.
7005 */
7006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007007compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
7009 int opt_idx;
7010
Bram Moolenaardac13472019-09-16 21:06:21 +02007011 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7013 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7014 set_option_default(opt_idx, OPT_FREE, p_cp);
7015 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007016 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017}
7018
Bram Moolenaardac13472019-09-16 21:06:21 +02007019#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021/*
7022 * fill_breakat_flags() -- called when 'breakat' changes value.
7023 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007024 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007025fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007027 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 int i;
7029
7030 for (i = 0; i < 256; i++)
7031 breakat_flags[i] = FALSE;
7032
7033 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007034 for (p = p_breakat; *p; p++)
7035 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#endif
7038
7039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 * Check if backspacing over something is allowed.
7041 */
7042 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007043can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007044 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007046#ifdef FEAT_JOB_CHANNEL
7047 if (what == BS_START && bt_prompt(curbuf))
7048 return FALSE;
7049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 switch (*p_bs)
7051 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007052 case '3': return TRUE;
7053 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 case '1': return (what != BS_START);
7055 case '0': return FALSE;
7056 }
7057 return vim_strchr(p_bs, what) != NULL;
7058}
7059
7060/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007061 * Return the effective 'scrolloff' value for the current window, using the
7062 * global value when appropriate.
7063 */
7064 long
7065get_scrolloff_value(void)
7066{
7067 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7068}
7069
7070/*
7071 * Return the effective 'sidescrolloff' value for the current window, using the
7072 * global value when appropriate.
7073 */
7074 long
7075get_sidescrolloff_value(void)
7076{
7077 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7078}
7079
7080/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007081 * Get the local or global value of 'backupcopy'.
7082 */
7083 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007084get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007085{
7086 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7087}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007088
Gary Johnson53ba05b2021-07-26 22:19:10 +02007089/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007090 * Get the local or global value of 'formatlistpat'.
7091 */
7092 char_u *
7093get_flp_value(buf_T *buf)
7094{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007095 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7096 return p_flp;
7097 return buf->b_p_flp;
7098}
7099
7100/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007101 * Get the local or global value of the 'virtualedit' flags.
7102 */
7103 unsigned int
7104get_ve_flags(void)
7105{
Gary Johnson51ad8502021-08-03 18:33:08 +02007106 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7107 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007108}
7109
Bram Moolenaaree857022019-11-09 23:26:40 +01007110#if defined(FEAT_LINEBREAK) || defined(PROTO)
7111/*
7112 * Get the local or global value of 'showbreak'.
7113 */
7114 char_u *
7115get_showbreak_value(win_T *win)
7116{
7117 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7118 return p_sbr;
7119 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7120 return empty_option;
7121 return win->w_p_sbr;
7122}
7123#endif
7124
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007125#if defined(FEAT_EVAL) || defined(PROTO)
7126/*
7127 * Get window or buffer local options.
7128 */
7129 dict_T *
7130get_winbuf_options(int bufopt)
7131{
7132 dict_T *d;
7133 int opt_idx;
7134
7135 d = dict_alloc();
7136 if (d == NULL)
7137 return NULL;
7138
Bram Moolenaardac13472019-09-16 21:06:21 +02007139 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007140 {
7141 struct vimoption *opt = &options[opt_idx];
7142
7143 if ((bufopt && (opt->indir & PV_BUF))
7144 || (!bufopt && (opt->indir & PV_WIN)))
7145 {
7146 char_u *varp = get_varp(opt);
7147
7148 if (varp != NULL)
7149 {
7150 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007151 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007152 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007153 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007154 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007155 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007156 }
7157 }
7158 }
7159
7160 return d;
7161}
7162#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007163
Bram Moolenaardac13472019-09-16 21:06:21 +02007164#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007165/*
7166 * This is called when 'culopt' is changed
7167 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007168 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007169fill_culopt_flags(char_u *val, win_T *wp)
7170{
7171 char_u *p;
7172 char_u culopt_flags_new = 0;
7173
7174 if (val == NULL)
7175 p = wp->w_p_culopt;
7176 else
7177 p = val;
7178 while (*p != NUL)
7179 {
7180 if (STRNCMP(p, "line", 4) == 0)
7181 {
7182 p += 4;
7183 culopt_flags_new |= CULOPT_LINE;
7184 }
7185 else if (STRNCMP(p, "both", 4) == 0)
7186 {
7187 p += 4;
7188 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7189 }
7190 else if (STRNCMP(p, "number", 6) == 0)
7191 {
7192 p += 6;
7193 culopt_flags_new |= CULOPT_NBR;
7194 }
7195 else if (STRNCMP(p, "screenline", 10) == 0)
7196 {
7197 p += 10;
7198 culopt_flags_new |= CULOPT_SCRLINE;
7199 }
7200
7201 if (*p != ',' && *p != NUL)
7202 return FAIL;
7203 if (*p == ',')
7204 ++p;
7205 }
7206
7207 // Can't have both "line" and "screenline".
7208 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7209 return FAIL;
7210 wp->w_p_culopt_flags = culopt_flags_new;
7211
7212 return OK;
7213}
7214#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007215
7216/*
7217 * Get the value of 'magic' adjusted for Vim9 script.
7218 */
7219 int
7220magic_isset(void)
7221{
7222 switch (magic_overruled)
7223 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007224 case OPTION_MAGIC_ON: return TRUE;
7225 case OPTION_MAGIC_OFF: return FALSE;
7226 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007227 }
7228#ifdef FEAT_EVAL
7229 if (in_vim9script())
7230 return TRUE;
7231#endif
7232 return p_magic;
7233}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007234
7235/*
7236 * Set the callback function value for an option that accepts a function name,
7237 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7238 * Returns OK if the option is successfully set to a function, otherwise
7239 * returns FAIL.
7240 */
7241 int
7242option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7243{
7244#ifdef FEAT_EVAL
7245 typval_T *tv;
7246 callback_T cb;
7247
7248 if (optval == NULL || *optval == NUL)
7249 {
7250 free_callback(optcb);
7251 return OK;
7252 }
7253
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007254 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007255 || (STRNCMP(optval, "function(", 9) == 0)
7256 || (STRNCMP(optval, "funcref(", 8) == 0))
7257 // Lambda expression or a funcref
7258 tv = eval_expr(optval, NULL);
7259 else
7260 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007261 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007262 if (tv == NULL)
7263 return FAIL;
7264
7265 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007266 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007267 {
7268 free_tv(tv);
7269 return FAIL;
7270 }
7271
7272 free_callback(optcb);
7273 set_callback(optcb, &cb);
7274 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007275
7276 // when using Vim9 style "import.funcname" it needs to be expanded to
7277 // "import#funcname".
7278 expand_autload_callback(optcb);
7279
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007280 return OK;
7281#else
7282 return FAIL;
7283#endif
7284}