blob: 91ea4138d39717df8a4ab42cc0fa9cc6095518ad [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 Moolenaar071d4272004-06-13 20:20:40 +00004482/*
4483 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004484 * When "has_lt" is true there is a '<' before "*arg_arg".
4485 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 */
4487 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004488find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004490 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004492 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
4494 /*
4495 * Don't use get_special_key_code() for t_xx, we don't want it to call
4496 * add_termcap_entry().
4497 */
4498 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4499 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004500 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004502 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004504 key = find_special_key(&arg, &modifiers,
4505 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004506 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 key = 0;
4508 }
4509 return key;
4510}
4511
4512/*
4513 * if 'all' == 0: show changed options
4514 * if 'all' == 1: show all normal options
4515 * if 'all' == 2: show all terminal options
4516 */
4517 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004518showoptions(
4519 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004520 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521{
4522 struct vimoption *p;
4523 int col;
4524 int isterm;
4525 char_u *varp;
4526 struct vimoption **items;
4527 int item_count;
4528 int run;
4529 int row, rows;
4530 int cols;
4531 int i;
4532 int len;
4533
4534#define INC 20
4535#define GAP 3
4536
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004537 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (items == NULL)
4539 return;
4540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004541 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004543 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004545 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004547 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004549 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004552 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 * 1. display the short items
4554 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004555 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 */
4557 for (run = 1; run <= 2 && !got_int; ++run)
4558 {
4559 /*
4560 * collect the items in items[]
4561 */
4562 item_count = 0;
4563 for (p = &options[0]; p->fullname != NULL; p++)
4564 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004565 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004566 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004567 continue;
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 varp = NULL;
4570 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004571 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
4573 if (p->indir != PV_NONE && !isterm)
4574 varp = get_varp_scope(p, opt_flags);
4575 }
4576 else
4577 varp = get_varp(p);
4578 if (varp != NULL
4579 && ((all == 2 && isterm)
4580 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004581 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004583 if (opt_flags & OPT_ONECOLUMN)
4584 len = Columns;
4585 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004586 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 else
4588 {
4589 option_value2string(p, opt_flags);
4590 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4591 }
4592 if ((len <= INC - GAP && run == 1) ||
4593 (len > INC - GAP && run == 2))
4594 items[item_count++] = p;
4595 }
4596 }
4597
4598 /*
4599 * display the items
4600 */
4601 if (run == 1)
4602 {
4603 cols = (Columns + GAP - 3) / INC;
4604 if (cols == 0)
4605 cols = 1;
4606 rows = (item_count + cols - 1) / cols;
4607 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004608 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 rows = item_count;
4610 for (row = 0; row < rows && !got_int; ++row)
4611 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004612 msg_putchar('\n'); // go to next line
4613 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 break;
4615 col = 0;
4616 for (i = row; i < item_count; i += rows)
4617 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 showoneopt(items[i], opt_flags);
4620 col += INC;
4621 }
4622 out_flush();
4623 ui_breakcheck();
4624 }
4625 }
4626 vim_free(items);
4627}
4628
4629/*
4630 * Return TRUE if option "p" has its default value.
4631 */
4632 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004633optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 int dvi;
4636
4637 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004638 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004639 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004641 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004643 // the cast to long is required for Manx C, long_i is
4644 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004645 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004646 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4648}
4649
4650/*
4651 * showoneopt: show the value of one option
4652 * must not be called with a hidden option!
4653 */
4654 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004655showoneopt(
4656 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004657 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004659 char_u *varp;
4660 int save_silent = silent_mode;
4661
4662 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004663 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665 varp = get_varp_scope(p, opt_flags);
4666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004667 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4669 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004670 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004672 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004674 msg_puts(" ");
4675 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (!(p->flags & P_BOOL))
4677 {
4678 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004679 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 option_value2string(p, opt_flags);
4681 msg_outtrans(NameBuff);
4682 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004683
4684 silent_mode = save_silent;
4685 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686}
4687
4688/*
4689 * Write modified options as ":set" commands to a file.
4690 *
4691 * There are three values for "opt_flags":
4692 * OPT_GLOBAL: Write global option values and fresh values of
4693 * buffer-local options (used for start of a session
4694 * file).
4695 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4696 * curwin (used for a vimrc file).
4697 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4698 * and local values for window-local options of
4699 * curwin. Local values are also written when at the
4700 * default value, because a modeline or autocommand
4701 * may have set them when doing ":edit file" and the
4702 * user has set them back at the default or fresh
4703 * value.
4704 * When "local_only" is TRUE, don't write fresh
4705 * values, only local values (for ":mkview").
4706 * (fresh value = value used for a new buffer or window for a local option).
4707 *
4708 * Return FAIL on error, OK otherwise.
4709 */
4710 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004711makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 char_u *varp; // currently used value
4715 char_u *varp_fresh; // local value
4716 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 char *cmd;
4718 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004719 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
4721 /*
4722 * The options that don't have a default (terminal name, columns, lines)
4723 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004724 * Do the loop over "options[]" twice: once for options with the
4725 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004727 for (pri = 1; pri >= 0; --pri)
4728 {
4729 for (p = &options[0]; !istermoption(p); p++)
4730 if (!(p->flags & P_NO_MKRC)
4731 && !istermoption(p)
4732 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004734 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4736 continue;
4737
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004738 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4739 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4741 continue;
4742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004743 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004745 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 continue;
4747
Bram Moolenaard23b7142021-04-17 21:04:34 +02004748 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4749 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004750 continue;
4751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 round = 2;
4753 if (p->indir != PV_NONE)
4754 {
4755 if (p->var == VAR_WIN)
4756 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004757 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (!(opt_flags & OPT_LOCAL))
4759 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004760 // When fresh value of window-local option is not at the
4761 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4763 {
4764 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004765 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 round = 1;
4768 varp_local = varp;
4769 varp = varp_fresh;
4770 }
4771 }
4772 }
4773 }
4774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004775 // Round 1: fresh value for window-local options.
4776 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 for ( ; round <= 2; varp = varp_local, ++round)
4778 {
4779 if (round == 1 || (opt_flags & OPT_GLOBAL))
4780 cmd = "set";
4781 else
4782 cmd = "setlocal";
4783
4784 if (p->flags & P_BOOL)
4785 {
4786 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4787 return FAIL;
4788 }
4789 else if (p->flags & P_NUM)
4790 {
4791 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4792 return FAIL;
4793 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004794 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004796 int do_endif = FALSE;
4797
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 // Don't set 'syntax' and 'filetype' again if the value is
4799 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004800 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004801#if defined(FEAT_SYN_HL)
4802 p->indir == PV_SYN ||
4803#endif
4804 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4807 *(char_u **)(varp)) < 0
4808 || put_eol(fd) < 0)
4809 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004810 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004813 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004815 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 if (put_line(fd, "endif") == FAIL)
4818 return FAIL;
4819 }
4820 }
4821 }
4822 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return OK;
4825}
4826
4827#if defined(FEAT_FOLDING) || defined(PROTO)
4828/*
4829 * Generate set commands for the local fold options only. Used when
4830 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4831 */
4832 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004833makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004835 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004837 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 == FAIL
4839# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004840 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004842 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 == FAIL
4844 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4845 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4846 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4847 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4848 )
4849 return FAIL;
4850
4851 return OK;
4852}
4853#endif
4854
4855 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004856put_setstring(
4857 FILE *fd,
4858 char *cmd,
4859 char *name,
4860 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004861 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004864 char_u *buf = NULL;
4865 char_u *part = NULL;
4866 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4869 return FAIL;
4870 if (*valuep != NULL)
4871 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004872 // Output 'pastetoggle' as key names. For other
4873 // options some characters have to be escaped with
4874 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 if (valuep == &p_pt)
4876 {
4877 s = *valuep;
4878 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004879 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 return FAIL;
4881 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004882 // expand the option value, replace $HOME by ~
4883 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004885 int size = (int)STRLEN(*valuep) + 1;
4886
4887 // replace home directory in the whole option value into "buf"
4888 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004889 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004890 goto fail;
4891 home_replace(NULL, *valuep, buf, size, FALSE);
4892
4893 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004894 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004895 // can be expanded when read back.
4896 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4897 && vim_strchr(*valuep, ',') != NULL)
4898 {
4899 part = alloc(size);
4900 if (part == NULL)
4901 goto fail;
4902
4903 // write line break to clear the option, e.g. ':set rtp='
4904 if (put_eol(fd) == FAIL)
4905 goto fail;
4906
4907 p = buf;
4908 while (*p != NUL)
4909 {
4910 // for each comma separated option part, append value to
4911 // the option, :set rtp+=value
4912 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4913 goto fail;
4914 (void)copy_option_part(&p, part, size, ",");
4915 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4916 goto fail;
4917 }
4918 vim_free(buf);
4919 vim_free(part);
4920 return OK;
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004923 {
4924 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004926 }
4927 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 else if (put_escstr(fd, *valuep, 2) == FAIL)
4930 return FAIL;
4931 }
4932 if (put_eol(fd) < 0)
4933 return FAIL;
4934 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004935fail:
4936 vim_free(buf);
4937 vim_free(part);
4938 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939}
4940
4941 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004942put_setnum(
4943 FILE *fd,
4944 char *cmd,
4945 char *name,
4946 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
4948 long wc;
4949
4950 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4951 return FAIL;
4952 if (wc_use_keyname((char_u *)valuep, &wc))
4953 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4956 return FAIL;
4957 }
4958 else if (fprintf(fd, "%ld", *valuep) < 0)
4959 return FAIL;
4960 if (put_eol(fd) < 0)
4961 return FAIL;
4962 return OK;
4963}
4964
4965 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004966put_setbool(
4967 FILE *fd,
4968 char *cmd,
4969 char *name,
4970 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004972 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004973 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4975 || put_eol(fd) < 0)
4976 return FAIL;
4977 return OK;
4978}
4979
4980/*
4981 * Clear all the terminal options.
4982 * If the option has been allocated, free the memory.
4983 * Terminal options are never hidden or indirect.
4984 */
4985 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004986clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 /*
4989 * Reset a few things before clearing the old options. This may cause
4990 * outputting a few things that the terminal doesn't understand, but the
4991 * screen will be cleared later, so this is OK.
4992 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004993 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004994 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004996 // When starting the GUI close the display opened for the clipboard.
4997 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (gui.starting)
4999 clear_xterm_clip();
5000#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005001 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005003 free_termoptions();
5004}
5005
5006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005007free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005008{
5009 struct vimoption *p;
5010
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005011 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (istermoption(p))
5013 {
5014 if (p->flags & P_ALLOCED)
5015 free_string_option(*(char_u **)(p->var));
5016 if (p->flags & P_DEF_ALLOCED)
5017 free_string_option(p->def_val[VI_DEFAULT]);
5018 *(char_u **)(p->var) = empty_option;
5019 p->def_val[VI_DEFAULT] = empty_option;
5020 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005021#ifdef FEAT_EVAL
5022 // remember where the option was cleared
5023 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
5026 clear_termcodes();
5027}
5028
5029/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005030 * Free the string for one term option, if it was allocated.
5031 * Set the string to empty_option and clear allocated flag.
5032 * "var" points to the option value.
5033 */
5034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005035free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005036{
5037 struct vimoption *p;
5038
5039 for (p = &options[0]; p->fullname != NULL; p++)
5040 if (p->var == var)
5041 {
5042 if (p->flags & P_ALLOCED)
5043 free_string_option(*(char_u **)(p->var));
5044 *(char_u **)(p->var) = empty_option;
5045 p->flags &= ~P_ALLOCED;
5046 break;
5047 }
5048}
5049
5050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * Set the terminal option defaults to the current value.
5052 * Used after setting the terminal name.
5053 */
5054 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005055set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 struct vimoption *p;
5058
5059 for (p = &options[0]; p->fullname != NULL; p++)
5060 {
5061 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5062 {
5063 if (p->flags & P_DEF_ALLOCED)
5064 {
5065 free_string_option(p->def_val[VI_DEFAULT]);
5066 p->flags &= ~P_DEF_ALLOCED;
5067 }
5068 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5069 if (p->flags & P_ALLOCED)
5070 {
5071 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005072 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 }
5075 }
5076}
5077
5078/*
5079 * return TRUE if 'p' starts with 't_'
5080 */
5081 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005082istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
5084 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5085}
5086
Bram Moolenaardac13472019-09-16 21:06:21 +02005087/*
5088 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5089 */
5090 int
5091istermoption_idx(int opt_idx)
5092{
5093 return istermoption(&options[opt_idx]);
5094}
5095
Bram Moolenaar113e1072019-01-20 15:30:40 +01005096#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005098 * Unset local option value, similar to ":set opt<".
5099 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005100 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005101unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102{
5103 struct vimoption *p;
5104 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005105 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106
5107 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005108 if (opt_idx < 0)
5109 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005110 p = &(options[opt_idx]);
5111
5112 switch ((int)p->indir)
5113 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005114 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005115 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005116 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117 break;
5118 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005119 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120 break;
5121 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005122 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005123 break;
5124 case PV_AR:
5125 buf->b_p_ar = -1;
5126 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005127 case PV_BKC:
5128 clear_string_option(&buf->b_p_bkc);
5129 buf->b_bkc_flags = 0;
5130 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005131 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005132 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005133 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005134 case PV_TC:
5135 clear_string_option(&buf->b_p_tc);
5136 buf->b_tc_flags = 0;
5137 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005138 case PV_SISO:
5139 curwin->w_p_siso = -1;
5140 break;
5141 case PV_SO:
5142 curwin->w_p_so = -1;
5143 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005144#ifdef FEAT_FIND_ID
5145 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005146 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005147 break;
5148 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005149 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005150 break;
5151#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005152 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005153 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 break;
5155 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005156 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005157 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005158#ifdef FEAT_COMPL_FUNC
5159 case PV_TSRFU:
5160 clear_string_option(&buf->b_p_tsrfu);
5161 break;
5162#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005163 case PV_FP:
5164 clear_string_option(&buf->b_p_fp);
5165 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005166#ifdef FEAT_QUICKFIX
5167 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005168 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005169 break;
5170 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005171 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005172 break;
5173 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005174 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005175 break;
5176#endif
5177#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5178 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005179 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005180 break;
5181#endif
5182#if defined(FEAT_CRYPT)
5183 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005184 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005185 break;
5186#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005187#ifdef FEAT_LINEBREAK
5188 case PV_SBR:
5189 clear_string_option(&((win_T *)from)->w_p_sbr);
5190 break;
5191#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005192#ifdef FEAT_STL_OPT
5193 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005194 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005195 break;
5196#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005197 case PV_UL:
5198 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5199 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005200#ifdef FEAT_LISP
5201 case PV_LW:
5202 clear_string_option(&buf->b_p_lw);
5203 break;
5204#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005205 case PV_MENC:
5206 clear_string_option(&buf->b_p_menc);
5207 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005208 case PV_LCS:
5209 clear_string_option(&((win_T *)from)->w_p_lcs);
5210 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5211 redraw_later(NOT_VALID);
5212 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005213 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005214 clear_string_option(&((win_T *)from)->w_p_ve);
5215 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005216 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005217 }
5218}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005219#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005220
5221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005223 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
5225 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005226get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005228 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 if (p->var == VAR_WIN)
5231 return (char_u *)GLOBAL_WO(get_varp(p));
5232 return p->var;
5233 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005234 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
5236 switch ((int)p->indir)
5237 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005238 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005240 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5241 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5242 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005244 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5245 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5246 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005247 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005248 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005249 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005250 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5251 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005253 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5254 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005256 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5257 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005258#ifdef FEAT_COMPL_FUNC
5259 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5260#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005261#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5262 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5263#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005264#if defined(FEAT_CRYPT)
5265 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5266#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005267#ifdef FEAT_LINEBREAK
5268 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5269#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005270#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005271 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005272#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005273 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005274#ifdef FEAT_LISP
5275 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5276#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005277 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005278 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005279 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005280 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005283 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
5285 return get_varp(p);
5286}
5287
5288/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005289 * Get pointer to option variable at 'opt_idx', depending on local or global
5290 * scope.
5291 */
5292 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005293get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005294{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005295 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005296}
5297
5298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * Get pointer to option variable.
5300 */
5301 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005302get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005304 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (p->var == NULL)
5306 return NULL;
5307
5308 switch ((int)p->indir)
5309 {
5310 case PV_NONE: return p->var;
5311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005312 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005313 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005315 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005317 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005319 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005321 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005323 case PV_TC: return *curbuf->b_p_tc != NUL
5324 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005325 case PV_BKC: return *curbuf->b_p_bkc != NUL
5326 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005327 case PV_SISO: return curwin->w_p_siso >= 0
5328 ? (char_u *)&(curwin->w_p_siso) : p->var;
5329 case PV_SO: return curwin->w_p_so >= 0
5330 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005332 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005334 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5336#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005337 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005339 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005341#ifdef FEAT_COMPL_FUNC
5342 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5343 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5344#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005345 case PV_FP: return *curbuf->b_p_fp != NUL
5346 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005348 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005350 case PV_GP: return *curbuf->b_p_gp != NUL
5351 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5352 case PV_MP: return *curbuf->b_p_mp != NUL
5353 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005355#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5356 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5357 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5358#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005359#if defined(FEAT_CRYPT)
5360 case PV_CM: return *curbuf->b_p_cm != NUL
5361 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5362#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005363#ifdef FEAT_LINEBREAK
5364 case PV_SBR: return *curwin->w_p_sbr != NUL
5365 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5366#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005367#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005368 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005369 ? (char_u *)&(curwin->w_p_stl) : p->var;
5370#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005371 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5372 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005373#ifdef FEAT_LISP
5374 case PV_LW: return *curbuf->b_p_lw != NUL
5375 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5376#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005377 case PV_MENC: return *curbuf->b_p_menc != NUL
5378 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379#ifdef FEAT_ARABIC
5380 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5381#endif
5382 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005383 case PV_LCS: return *curwin->w_p_lcs != NUL
5384 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005385 case PV_VE: return *curwin->w_p_ve != NUL
5386 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005387#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005388 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5389#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005390#ifdef FEAT_SYN_HL
5391 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5392 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005393 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005394 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#ifdef FEAT_DIFF
5397 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5398#endif
5399#ifdef FEAT_FOLDING
5400 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5401 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5402 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5403 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5404 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5405 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5406 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5407# ifdef FEAT_EVAL
5408 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5409 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5410# endif
5411 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5412#endif
5413 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005414 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005415#ifdef FEAT_LINEBREAK
5416 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005419 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005420#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5422#endif
5423#ifdef FEAT_RIGHTLEFT
5424 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5425 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5426#endif
5427 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5428 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5429#ifdef FEAT_LINEBREAK
5430 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005431 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5432 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005434 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005436 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005437#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005438 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5439 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5440#endif
5441#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005442 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5443 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5444 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
5447 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5448 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5451 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5453 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5454#ifdef FEAT_CINDENT
5455 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5456 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5457 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5458#endif
5459#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5460 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463#ifdef FEAT_FOLDING
5464 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005467#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005468 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005470#ifdef FEAT_COMPL_FUNC
5471 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005472 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005473#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005474#ifdef FEAT_EVAL
5475 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005478 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005484 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5486 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5487 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5488 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5489#ifdef FEAT_FIND_ID
5490# ifdef FEAT_EVAL
5491 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5492# endif
5493#endif
5494#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5495 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5496 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5497#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005498#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005499 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501#ifdef FEAT_CRYPT
5502 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5503#endif
5504#ifdef FEAT_LISP
5505 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5506#endif
5507 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5508 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5509 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5510 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5511 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005513#ifdef FEAT_TEXTOBJ
5514 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5517#ifdef FEAT_SMARTINDENT
5518 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5522#ifdef FEAT_SEARCHPATH
5523 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5524#endif
5525 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5526#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005527 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005528 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5529#endif
5530#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005531 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5532 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5533 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005534 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#endif
5536 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5537 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5538 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5539 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005540#ifdef FEAT_PERSISTENT_UNDO
5541 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5544#ifdef FEAT_KEYMAP
5545 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5546#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005547#ifdef FEAT_SIGNS
5548 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5549#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005550#ifdef FEAT_VARTABS
5551 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5552 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5553#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005554 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005556 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 return (char_u *)&(curbuf->b_p_wm);
5558}
5559
5560/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005561 * Return a pointer to the variable for option at 'opt_idx'
5562 */
5563 char_u *
5564get_option_var(int opt_idx)
5565{
5566 return options[opt_idx].var;
5567}
5568
Dominique Pelle748b3082022-01-08 12:41:16 +00005569#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005570/*
5571 * Return the full name of the option at 'opt_idx'
5572 */
5573 char_u *
5574get_option_fullname(int opt_idx)
5575{
5576 return (char_u *)options[opt_idx].fullname;
5577}
Dominique Pelle748b3082022-01-08 12:41:16 +00005578#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005579
5580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 * Get the value of 'equalprg', either the buffer-local one or the global one.
5582 */
5583 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005584get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
5586 if (*curbuf->b_p_ep == NUL)
5587 return p_ep;
5588 return curbuf->b_p_ep;
5589}
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591/*
5592 * Copy options from one window to another.
5593 * Used when splitting a window.
5594 */
5595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005596win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5599 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005600 after_copy_winopt(wp_to);
5601}
5602
5603/*
5604 * After copying window options: update variables depending on options.
5605 */
5606 void
Sean Dewar0f7ff852022-02-12 11:51:25 +00005607after_copy_winopt(win_T *wp)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005608{
5609#ifdef FEAT_LINEBREAK
5610 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005611#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005612#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005613 fill_culopt_flags(NULL, wp);
5614 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005615#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005616 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
5619/*
5620 * Copy the options from one winopt_T to another.
5621 * Doesn't free the old option values in "to", use clear_winopt() for that.
5622 * The 'scroll' option is not copied, because it depends on the window height.
5623 * The 'previewwindow' option is reset, there can be only one preview window.
5624 */
5625 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005626copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628#ifdef FEAT_ARABIC
5629 to->wo_arab = from->wo_arab;
5630#endif
5631 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005632 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005634 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005635 to->wo_ve = vim_strsave(from->wo_ve);
5636 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005637#ifdef FEAT_LINEBREAK
5638 to->wo_nuw = from->wo_nuw;
5639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640#ifdef FEAT_RIGHTLEFT
5641 to->wo_rl = from->wo_rl;
5642 to->wo_rlc = vim_strsave(from->wo_rlc);
5643#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005644#ifdef FEAT_LINEBREAK
5645 to->wo_sbr = vim_strsave(from->wo_sbr);
5646#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005647#ifdef FEAT_STL_OPT
5648 to->wo_stl = vim_strsave(from->wo_stl);
5649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005651#ifdef FEAT_DIFF
5652 to->wo_wrap_save = from->wo_wrap_save;
5653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654#ifdef FEAT_LINEBREAK
5655 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005656 to->wo_bri = from->wo_bri;
5657 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005659 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005661 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005662 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005663 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005664#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005665 to->wo_spell = from->wo_spell;
5666#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005667#ifdef FEAT_SYN_HL
5668 to->wo_cuc = from->wo_cuc;
5669 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005670 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005671 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#ifdef FEAT_DIFF
5674 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005675 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005677#ifdef FEAT_CONCEAL
5678 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005679 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005680#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005681#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005682 to->wo_twk = vim_strsave(from->wo_twk);
5683 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685#ifdef FEAT_FOLDING
5686 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005687 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005689 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 to->wo_fdi = vim_strsave(from->wo_fdi);
5691 to->wo_fml = from->wo_fml;
5692 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005693 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005695 to->wo_fdm_save = from->wo_diff_saved
5696 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 to->wo_fdn = from->wo_fdn;
5698# ifdef FEAT_EVAL
5699 to->wo_fde = vim_strsave(from->wo_fde);
5700 to->wo_fdt = vim_strsave(from->wo_fdt);
5701# endif
5702 to->wo_fmr = vim_strsave(from->wo_fmr);
5703#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005704#ifdef FEAT_SIGNS
5705 to->wo_scl = vim_strsave(from->wo_scl);
5706#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005707
5708#ifdef FEAT_EVAL
5709 // Copy the script context so that we know where the value was last set.
5710 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5711 sizeof(to->wo_script_ctx));
5712#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005713 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714}
5715
5716/*
5717 * Check string options in a window for a NULL value.
5718 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005719 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005720check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 check_winopt(&win->w_onebuf_opt);
5723 check_winopt(&win->w_allbuf_opt);
5724}
5725
5726/*
5727 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5728 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005730check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732#ifdef FEAT_FOLDING
5733 check_string_option(&wop->wo_fdi);
5734 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005735 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736# ifdef FEAT_EVAL
5737 check_string_option(&wop->wo_fde);
5738 check_string_option(&wop->wo_fdt);
5739# endif
5740 check_string_option(&wop->wo_fmr);
5741#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005742#ifdef FEAT_SIGNS
5743 check_string_option(&wop->wo_scl);
5744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745#ifdef FEAT_RIGHTLEFT
5746 check_string_option(&wop->wo_rlc);
5747#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005748#ifdef FEAT_LINEBREAK
5749 check_string_option(&wop->wo_sbr);
5750#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005751#ifdef FEAT_STL_OPT
5752 check_string_option(&wop->wo_stl);
5753#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005754#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005755 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005756 check_string_option(&wop->wo_cc);
5757#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005758#ifdef FEAT_CONCEAL
5759 check_string_option(&wop->wo_cocu);
5760#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005761#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005762 check_string_option(&wop->wo_twk);
5763 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005764#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005765#ifdef FEAT_LINEBREAK
5766 check_string_option(&wop->wo_briopt);
5767#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005768 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005769 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005770 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771}
5772
5773/*
5774 * Free the allocated memory inside a winopt_T.
5775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005777clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779#ifdef FEAT_FOLDING
5780 clear_string_option(&wop->wo_fdi);
5781 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005782 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783# ifdef FEAT_EVAL
5784 clear_string_option(&wop->wo_fde);
5785 clear_string_option(&wop->wo_fdt);
5786# endif
5787 clear_string_option(&wop->wo_fmr);
5788#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005789#ifdef FEAT_SIGNS
5790 clear_string_option(&wop->wo_scl);
5791#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005792#ifdef FEAT_LINEBREAK
5793 clear_string_option(&wop->wo_briopt);
5794#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005795 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#ifdef FEAT_RIGHTLEFT
5797 clear_string_option(&wop->wo_rlc);
5798#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005799#ifdef FEAT_LINEBREAK
5800 clear_string_option(&wop->wo_sbr);
5801#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005802#ifdef FEAT_STL_OPT
5803 clear_string_option(&wop->wo_stl);
5804#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005805#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005806 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005807 clear_string_option(&wop->wo_cc);
5808#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005809#ifdef FEAT_CONCEAL
5810 clear_string_option(&wop->wo_cocu);
5811#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005812#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005813 clear_string_option(&wop->wo_twk);
5814 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005815#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005816 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005817 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818}
5819
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005820#ifdef FEAT_EVAL
5821// Index into the options table for a buffer-local option enum.
5822static int buf_opt_idx[BV_COUNT];
5823# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5824
5825/*
5826 * Initialize buf_opt_idx[] if not done already.
5827 */
5828 static void
5829init_buf_opt_idx(void)
5830{
5831 static int did_init_buf_opt_idx = FALSE;
5832 int i;
5833
5834 if (did_init_buf_opt_idx)
5835 return;
5836 did_init_buf_opt_idx = TRUE;
5837 for (i = 0; !istermoption_idx(i); i++)
5838 if (options[i].indir & PV_BUF)
5839 buf_opt_idx[options[i].indir & PV_MASK] = i;
5840}
5841#else
5842# define COPY_OPT_SCTX(buf, bv)
5843#endif
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845/*
5846 * Copy global option values to local options for one buffer.
5847 * Used when creating a new buffer and sometimes when entering a buffer.
5848 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005849 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5851 * appropriate.
5852 * BCO_NOHELP Don't copy the values to a help buffer.
5853 */
5854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005855buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005858 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 int dont_do_help;
5860 int did_isk = FALSE;
5861
5862 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 * Skip this when the option defaults have not been set yet. Happens when
5864 * main() allocates the first buffer.
5865 */
5866 if (p_cpo != NULL)
5867 {
5868 /*
5869 * Always copy when entering and 'cpo' contains 'S'.
5870 * Don't copy when already initialized.
5871 * Don't copy when 'cpo' contains 's' and not entering.
5872 * 'S' BCO_ENTER initialized 's' should_copy
5873 * yes yes X X TRUE
5874 * yes no yes X FALSE
5875 * no X yes X FALSE
5876 * X no no yes FALSE
5877 * X no no no TRUE
5878 * no yes no X TRUE
5879 */
5880 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5881 && (buf->b_p_initialized
5882 || (!(flags & BCO_ENTER)
5883 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5884 should_copy = FALSE;
5885
5886 if (should_copy || (flags & BCO_ALWAYS))
5887 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005888#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005889 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005890 init_buf_opt_idx();
5891#endif
5892 // Don't copy the options specific to a help buffer when
5893 // BCO_NOHELP is given or the options were initialized already
5894 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5896 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005897 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
5899 save_p_isk = buf->b_p_isk;
5900 buf->b_p_isk = NULL;
5901 }
5902 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005903 * Always free the allocated strings. If not already initialized,
5904 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 */
5906 if (!buf->b_p_initialized)
5907 {
5908 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005909 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005912 switch (*p_ffs)
5913 {
5914 case 'm':
5915 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5916 case 'd':
5917 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5918 case 'u':
5919 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5920 default:
5921 buf->b_p_ff = vim_strsave(p_ff);
5922 }
5923 if (buf->b_p_ff != NULL)
5924 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 buf->b_p_bh = empty_option;
5926 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 }
5928 else
5929 free_buf_options(buf, FALSE);
5930
5931 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_ai_nopaste = p_ai_nopaste;
5934 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005935 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005937 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 buf->b_p_tw_nopaste = p_tw_nopaste;
5939 buf->b_p_tw_nobin = p_tw_nobin;
5940 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005941 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 buf->b_p_wm_nopaste = p_wm_nopaste;
5943 buf->b_p_wm_nobin = p_wm_nobin;
5944 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005946 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005947 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005948 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005951 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005953 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 buf->b_p_ml_nobin = p_ml_nobin;
5957 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005958 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005959 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005960 buf->b_p_swf = FALSE;
5961 else
5962 {
5963 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005964 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005968#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005969 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005972#ifdef FEAT_COMPL_FUNC
5973 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005974 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005975 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005976 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005978 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005979#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005980#ifdef FEAT_EVAL
5981 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005982 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005983 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005986 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005988#ifdef FEAT_VARTABS
5989 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005991 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005992 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005993 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00005994 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005995 buf->b_p_vsts_nopaste = p_vsts_nopaste
5996 ? vim_strsave(p_vsts_nopaste) : NULL;
5997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005999 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002#ifdef FEAT_FOLDING
6003 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006004 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005#endif
6006 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006008 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006009 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006010 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6011 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006013 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006015 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#ifdef FEAT_SMARTINDENT
6017 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006018 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019#endif
6020 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022#ifdef FEAT_CINDENT
6023 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006024 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006026 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006030 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6035 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037#endif
6038#ifdef FEAT_LISP
6039 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006040 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#endif
6042#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006043 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006045 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006046 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006047 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006048#endif
6049#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006050 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006051 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006052 (void)compile_cap_prog(&buf->b_s);
6053 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006054 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006055 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006056 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006057 buf->b_s.b_p_spo = vim_strsave(p_spo);
6058 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059#endif
6060#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6061 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006062 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006064 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006066 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006067#if defined(FEAT_EVAL)
6068 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006069 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071#ifdef FEAT_CRYPT
6072 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006073 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074#endif
6075#ifdef FEAT_SEARCHPATH
6076 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006077 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078#endif
6079#ifdef FEAT_KEYMAP
6080 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006081 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 buf->b_kmap_state |= KEYMAP_INIT;
6083#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006084#ifdef FEAT_TERMINAL
6085 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006086 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006087#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006088 // This isn't really an option, but copying the langmap and IME
6089 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006091 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006093 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006095 // options that are normally global but also have a local value
6096 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006098 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006099 buf->b_p_bkc = empty_option;
6100 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#ifdef FEAT_QUICKFIX
6102 buf->b_p_gp = empty_option;
6103 buf->b_p_mp = empty_option;
6104 buf->b_p_efm = empty_option;
6105#endif
6106 buf->b_p_ep = empty_option;
6107 buf->b_p_kp = empty_option;
6108 buf->b_p_path = empty_option;
6109 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006110 buf->b_p_tc = empty_option;
6111 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112#ifdef FEAT_FIND_ID
6113 buf->b_p_def = empty_option;
6114 buf->b_p_inc = empty_option;
6115# ifdef FEAT_EVAL
6116 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006117 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118# endif
6119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 buf->b_p_dict = empty_option;
6121 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006122#ifdef FEAT_COMPL_FUNC
6123 buf->b_p_tsrfu = empty_option;
6124#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006125#ifdef FEAT_TEXTOBJ
6126 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006127 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006128#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006129#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6130 buf->b_p_bexpr = empty_option;
6131#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006132#if defined(FEAT_CRYPT)
6133 buf->b_p_cm = empty_option;
6134#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006135#ifdef FEAT_PERSISTENT_UNDO
6136 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006137 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006138#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006139#ifdef FEAT_LISP
6140 buf->b_p_lw = empty_option;
6141#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006142 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 /*
6145 * Don't copy the options set by ex_help(), use the saved values,
6146 * when going from a help buffer to a non-help buffer.
6147 * Don't touch these at all when BCO_NOHELP is used and going from
6148 * or to a help buffer.
6149 */
6150 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006153#ifdef FEAT_VARTABS
6154 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006155 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006156 else
6157 buf->b_p_vts_array = NULL;
6158#endif
6159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 else
6161 {
6162 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006163 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 did_isk = TRUE;
6165 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006166 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006167#ifdef FEAT_VARTABS
6168 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006169 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006170 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006171 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006172 else
6173 buf->b_p_vts_array = NULL;
6174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (buf->b_p_bt[0] == 'h')
6177 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006179 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
6181 }
6182
6183 /*
6184 * When the options should be copied (ignoring BCO_ALWAYS), set the
6185 * flag that indicates that the options have been initialized.
6186 */
6187 if (should_copy)
6188 buf->b_p_initialized = TRUE;
6189 }
6190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006191 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (did_isk)
6193 (void)buf_init_chartab(buf, FALSE);
6194}
6195
6196/*
6197 * Reset the 'modifiable' option and its default value.
6198 */
6199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006200reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 int opt_idx;
6203
6204 curbuf->b_p_ma = FALSE;
6205 p_ma = FALSE;
6206 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006207 if (opt_idx >= 0)
6208 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209}
6210
6211/*
6212 * Set the global value for 'iminsert' to the local value.
6213 */
6214 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006215set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 p_iminsert = curbuf->b_p_iminsert;
6218}
6219
6220/*
6221 * Set the global value for 'imsearch' to the local value.
6222 */
6223 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006224set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 p_imsearch = curbuf->b_p_imsearch;
6227}
6228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229static int expand_option_idx = -1;
6230static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6231static int expand_option_flags = 0;
6232
6233 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234set_context_in_set_cmd(
6235 expand_T *xp,
6236 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006237 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006240 long_u flags = 0; // init for GCC
6241 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 char_u *p;
6243 char_u *s;
6244 int is_term_option = FALSE;
6245 int key;
6246
6247 expand_option_flags = opt_flags;
6248
6249 xp->xp_context = EXPAND_SETTINGS;
6250 if (*arg == NUL)
6251 {
6252 xp->xp_pattern = arg;
6253 return;
6254 }
6255 p = arg + STRLEN(arg) - 1;
6256 if (*p == ' ' && *(p - 1) != '\\')
6257 {
6258 xp->xp_pattern = p + 1;
6259 return;
6260 }
6261 while (p > arg)
6262 {
6263 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006264 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (*p == ' ' || *p == ',')
6266 {
6267 while (s > arg && *(s - 1) == '\\')
6268 --s;
6269 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006270 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if (*p == ' ' && ((p - s) & 1) == 0)
6272 {
6273 ++p;
6274 break;
6275 }
6276 --p;
6277 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006278 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
6280 xp->xp_context = EXPAND_BOOL_SETTINGS;
6281 p += 2;
6282 }
6283 if (STRNCMP(p, "inv", 3) == 0)
6284 {
6285 xp->xp_context = EXPAND_BOOL_SETTINGS;
6286 p += 3;
6287 }
6288 xp->xp_pattern = arg = p;
6289 if (*arg == '<')
6290 {
6291 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006292 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 return;
6294 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006295 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
6297 xp->xp_context = EXPAND_NOTHING;
6298 return;
6299 }
6300 nextchar = *++p;
6301 is_term_option = TRUE;
6302 expand_option_name[2] = KEY2TERMCAP0(key);
6303 expand_option_name[3] = KEY2TERMCAP1(key);
6304 }
6305 else
6306 {
6307 if (p[0] == 't' && p[1] == '_')
6308 {
6309 p += 2;
6310 if (*p != NUL)
6311 ++p;
6312 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006313 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 nextchar = *++p;
6315 is_term_option = TRUE;
6316 expand_option_name[2] = p[-2];
6317 expand_option_name[3] = p[-1];
6318 }
6319 else
6320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006321 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6323 p++;
6324 if (*p == NUL)
6325 return;
6326 nextchar = *p;
6327 *p = NUL;
6328 opt_idx = findoption(arg);
6329 *p = nextchar;
6330 if (opt_idx == -1 || options[opt_idx].var == NULL)
6331 {
6332 xp->xp_context = EXPAND_NOTHING;
6333 return;
6334 }
6335 flags = options[opt_idx].flags;
6336 if (flags & P_BOOL)
6337 {
6338 xp->xp_context = EXPAND_NOTHING;
6339 return;
6340 }
6341 }
6342 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6345 {
6346 ++p;
6347 nextchar = '=';
6348 }
6349 if ((nextchar != '=' && nextchar != ':')
6350 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6351 {
6352 xp->xp_context = EXPAND_UNSUCCESSFUL;
6353 return;
6354 }
6355 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6356 {
6357 xp->xp_context = EXPAND_OLD_SETTING;
6358 if (is_term_option)
6359 expand_option_idx = -1;
6360 else
6361 expand_option_idx = opt_idx;
6362 xp->xp_pattern = p + 1;
6363 return;
6364 }
6365 xp->xp_context = EXPAND_NOTHING;
6366 if (is_term_option || (flags & P_NUM))
6367 return;
6368
6369 xp->xp_pattern = p + 1;
6370
6371 if (flags & P_EXPAND)
6372 {
6373 p = options[opt_idx].var;
6374 if (p == (char_u *)&p_bdir
6375 || p == (char_u *)&p_dir
6376 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006377 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 || p == (char_u *)&p_rtp
6379#ifdef FEAT_SEARCHPATH
6380 || p == (char_u *)&p_cdpath
6381#endif
6382#ifdef FEAT_SESSION
6383 || p == (char_u *)&p_vdir
6384#endif
6385 )
6386 {
6387 xp->xp_context = EXPAND_DIRECTORIES;
6388 if (p == (char_u *)&p_path
6389#ifdef FEAT_SEARCHPATH
6390 || p == (char_u *)&p_cdpath
6391#endif
6392 )
6393 xp->xp_backslash = XP_BS_THREE;
6394 else
6395 xp->xp_backslash = XP_BS_ONE;
6396 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006397 else if (p == (char_u *)&p_ft)
6398 {
6399 xp->xp_context = EXPAND_FILETYPE;
6400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 else
6402 {
6403 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006404 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (p == (char_u *)&p_tags)
6406 xp->xp_backslash = XP_BS_THREE;
6407 else
6408 xp->xp_backslash = XP_BS_ONE;
6409 }
6410 }
6411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006412 // For an option that is a list of file names, find the start of the
6413 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (*p == ' ' || *p == ',')
6418 {
6419 s = p;
6420 while (s > xp->xp_pattern && *(s - 1) == '\\')
6421 --s;
6422 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6423 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6424 {
6425 xp->xp_pattern = p + 1;
6426 break;
6427 }
6428 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006429
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006430#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006431 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006432 if (options[opt_idx].var == (char_u *)&p_sps
6433 && STRNCMP(p, "file:", 5) == 0)
6434 {
6435 xp->xp_pattern = p + 5;
6436 break;
6437 }
6438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441
6442 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006443ExpandSettings(
6444 expand_T *xp,
6445 regmatch_T *regmatch,
6446 int *num_file,
6447 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006449 int num_normal = 0; // Nr of matching non-term-code settings
6450 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 int opt_idx;
6452 int match;
6453 int count = 0;
6454 char_u *str;
6455 int loop;
6456 int is_term_opt;
6457 char_u name_buf[MAX_KEY_NAME_LEN];
6458 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006459 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006461 // do this loop twice:
6462 // loop == 0: count the number of matching options
6463 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 for (loop = 0; loop <= 1; ++loop)
6465 {
6466 regmatch->rm_ic = ic;
6467 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6468 {
K.Takataeeec2542021-06-02 13:28:16 +02006469 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6471 {
6472 if (loop == 0)
6473 num_normal++;
6474 else
6475 (*file)[count++] = vim_strsave((char_u *)names[match]);
6476 }
6477 }
6478 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6479 opt_idx++)
6480 {
6481 if (options[opt_idx].var == NULL)
6482 continue;
6483 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6484 && !(options[opt_idx].flags & P_BOOL))
6485 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006486 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (is_term_opt && num_normal > 0)
6488 continue;
6489 match = FALSE;
6490 if (vim_regexec(regmatch, str, (colnr_T)0)
6491 || (options[opt_idx].shortname != NULL
6492 && vim_regexec(regmatch,
6493 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6494 match = TRUE;
6495 else if (is_term_opt)
6496 {
6497 name_buf[0] = '<';
6498 name_buf[1] = 't';
6499 name_buf[2] = '_';
6500 name_buf[3] = str[2];
6501 name_buf[4] = str[3];
6502 name_buf[5] = '>';
6503 name_buf[6] = NUL;
6504 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6505 {
6506 match = TRUE;
6507 str = name_buf;
6508 }
6509 }
6510 if (match)
6511 {
6512 if (loop == 0)
6513 {
6514 if (is_term_opt)
6515 num_term++;
6516 else
6517 num_normal++;
6518 }
6519 else
6520 (*file)[count++] = vim_strsave(str);
6521 }
6522 }
6523 /*
6524 * Check terminal key codes, these are not in the option table
6525 */
6526 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6527 {
6528 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6529 {
6530 if (!isprint(str[0]) || !isprint(str[1]))
6531 continue;
6532
6533 name_buf[0] = 't';
6534 name_buf[1] = '_';
6535 name_buf[2] = str[0];
6536 name_buf[3] = str[1];
6537 name_buf[4] = NUL;
6538
6539 match = FALSE;
6540 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6541 match = TRUE;
6542 else
6543 {
6544 name_buf[0] = '<';
6545 name_buf[1] = 't';
6546 name_buf[2] = '_';
6547 name_buf[3] = str[0];
6548 name_buf[4] = str[1];
6549 name_buf[5] = '>';
6550 name_buf[6] = NUL;
6551
6552 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6553 match = TRUE;
6554 }
6555 if (match)
6556 {
6557 if (loop == 0)
6558 num_term++;
6559 else
6560 (*file)[count++] = vim_strsave(name_buf);
6561 }
6562 }
6563
6564 /*
6565 * Check special key names.
6566 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006567 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6569 {
6570 name_buf[0] = '<';
6571 STRCPY(name_buf + 1, str);
6572 STRCAT(name_buf, ">");
6573
6574 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6575 {
6576 if (loop == 0)
6577 num_term++;
6578 else
6579 (*file)[count++] = vim_strsave(name_buf);
6580 }
6581 }
6582 }
6583 if (loop == 0)
6584 {
6585 if (num_normal > 0)
6586 *num_file = num_normal;
6587 else if (num_term > 0)
6588 *num_file = num_term;
6589 else
6590 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006591 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 if (*file == NULL)
6593 {
6594 *file = (char_u **)"";
6595 return FAIL;
6596 }
6597 }
6598 }
6599 return OK;
6600}
6601
6602 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006603ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006605 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 char_u *buf;
6607
6608 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006609 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (*file == NULL)
6611 return FAIL;
6612
6613 /*
6614 * For a terminal key code expand_option_idx is < 0.
6615 */
6616 if (expand_option_idx < 0)
6617 {
6618 var = find_termcode(expand_option_name + 2);
6619 if (var == NULL)
6620 expand_option_idx = findoption(expand_option_name);
6621 }
6622
6623 if (expand_option_idx >= 0)
6624 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006625 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 option_value2string(&options[expand_option_idx], expand_option_flags);
6627 var = NameBuff;
6628 }
6629 else if (var == NULL)
6630 var = (char_u *)"";
6631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006632 // A backslash is required before some characters. This is the reverse of
6633 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 buf = vim_strsave_escaped(var, escape_chars);
6635
6636 if (buf == NULL)
6637 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006638 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 return FAIL;
6640 }
6641
6642#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006643 // For MS-Windows et al. we don't double backslashes at the start and
6644 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006645 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (var[0] == '\\' && var[1] == '\\'
6647 && expand_option_idx >= 0
6648 && (options[expand_option_idx].flags & P_EXPAND)
6649 && vim_isfilec(var[2])
6650 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006651 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652#endif
6653
6654 *file[0] = buf;
6655 *num_file = 1;
6656 return OK;
6657}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659/*
6660 * Get the value for the numeric or string option *opp in a nice format into
6661 * NameBuff[]. Must not be called with a hidden option!
6662 */
6663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006664option_value2string(
6665 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006666 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 char_u *varp;
6669
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006670 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 if (opp->flags & P_NUM)
6673 {
6674 long wc = 0;
6675
6676 if (wc_use_keyname(varp, &wc))
6677 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6678 else if (wc != 0)
6679 STRCPY(NameBuff, transchar((int)wc));
6680 else
6681 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6682 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006683 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006686 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 NameBuff[0] = NUL;
6688#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006689 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006690 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 STRCPY(NameBuff, "*****");
6692#endif
6693 else if (opp->flags & P_EXPAND)
6694 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006695 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 else if ((char_u **)opp->var == &p_pt)
6697 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6698 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006699 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701}
6702
6703/*
6704 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6705 * printed as a keyname.
6706 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6707 */
6708 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006709wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6712 {
6713 *wcp = *(long *)varp;
6714 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6715 return TRUE;
6716 }
6717 return FALSE;
6718}
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 * Return TRUE if "x" is present in 'shortmess' option, or
6722 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6723 */
6724 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006725shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006727 return p_shm != NULL &&
6728 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 || (vim_strchr(p_shm, 'a') != NULL
6730 && vim_strchr((char_u *)SHM_A, x) != NULL));
6731}
6732
6733/*
6734 * paste_option_changed() - Called after p_paste was set or reset.
6735 */
6736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006737paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 static int old_p_paste = FALSE;
6740 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006741 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742#ifdef FEAT_CMDL_INFO
6743 static int save_ru = 0;
6744#endif
6745#ifdef FEAT_RIGHTLEFT
6746 static int save_ri = 0;
6747 static int save_hkmap = 0;
6748#endif
6749 buf_T *buf;
6750
6751 if (p_paste)
6752 {
6753 /*
6754 * Paste switched from off to on.
6755 * Save the current values, so they can be restored later.
6756 */
6757 if (!old_p_paste)
6758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006759 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006760 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
6762 buf->b_p_tw_nopaste = buf->b_p_tw;
6763 buf->b_p_wm_nopaste = buf->b_p_wm;
6764 buf->b_p_sts_nopaste = buf->b_p_sts;
6765 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006766 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006767#ifdef FEAT_VARTABS
6768 if (buf->b_p_vsts_nopaste)
6769 vim_free(buf->b_p_vsts_nopaste);
6770 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6771 ? vim_strsave(buf->b_p_vsts) : NULL;
6772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
6774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006775 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006777 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778#ifdef FEAT_CMDL_INFO
6779 save_ru = p_ru;
6780#endif
6781#ifdef FEAT_RIGHTLEFT
6782 save_ri = p_ri;
6783 save_hkmap = p_hkmap;
6784#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006785 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006786 p_ai_nopaste = p_ai;
6787 p_et_nopaste = p_et;
6788 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 p_tw_nopaste = p_tw;
6790 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006791#ifdef FEAT_VARTABS
6792 if (p_vsts_nopaste)
6793 vim_free(p_vsts_nopaste);
6794 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
6797
6798 /*
6799 * Always set the option values, also when 'paste' is set when it is
6800 * already on.
6801 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006803 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006805 buf->b_p_tw = 0; // textwidth is 0
6806 buf->b_p_wm = 0; // wrapmargin is 0
6807 buf->b_p_sts = 0; // softtabstop is 0
6808 buf->b_p_ai = 0; // no auto-indent
6809 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006810#ifdef FEAT_VARTABS
6811 if (buf->b_p_vsts)
6812 free_string_option(buf->b_p_vsts);
6813 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006814 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006818 // set global options
6819 p_sm = 0; // no showmatch
6820 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006823 status_redraw_all(); // redraw to remove the ruler
6824 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825#endif
6826#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006827 p_ri = 0; // no reverse insert
6828 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006830 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 p_tw = 0;
6832 p_wm = 0;
6833 p_sts = 0;
6834 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006835#ifdef FEAT_VARTABS
6836 if (p_vsts)
6837 free_string_option(p_vsts);
6838 p_vsts = empty_option;
6839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
6841
6842 /*
6843 * Paste switched from on to off: Restore saved values.
6844 */
6845 else if (old_p_paste)
6846 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006847 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006848 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
6850 buf->b_p_tw = buf->b_p_tw_nopaste;
6851 buf->b_p_wm = buf->b_p_wm_nopaste;
6852 buf->b_p_sts = buf->b_p_sts_nopaste;
6853 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006854 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006855#ifdef FEAT_VARTABS
6856 if (buf->b_p_vsts)
6857 free_string_option(buf->b_p_vsts);
6858 buf->b_p_vsts = buf->b_p_vsts_nopaste
6859 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006860 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006861 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006862 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006863 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006864 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 }
6867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006870 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006873 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 p_ru = save_ru;
6875#endif
6876#ifdef FEAT_RIGHTLEFT
6877 p_ri = save_ri;
6878 p_hkmap = save_hkmap;
6879#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006880 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006881 p_ai = p_ai_nopaste;
6882 p_et = p_et_nopaste;
6883 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 p_tw = p_tw_nopaste;
6885 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006886#ifdef FEAT_VARTABS
6887 if (p_vsts)
6888 free_string_option(p_vsts);
6889 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892
6893 old_p_paste = p_paste;
6894}
6895
6896/*
6897 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6898 *
6899 * Reset 'compatible' and set the values for options that didn't get set yet
6900 * to the Vim defaults.
6901 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 */
6904 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006905vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006907 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006908 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
6911 if (!option_was_set((char_u *)"cp"))
6912 {
6913 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006914 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6916 set_option_default(opt_idx, OPT_FREE, FALSE);
6917 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006918 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006920
6921 if (fname != NULL)
6922 {
6923 p = vim_getenv(envname, &dofree);
6924 if (p == NULL)
6925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006926 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927 p = FullName_save(fname, FALSE);
6928 if (p != NULL)
6929 {
6930 vim_setenv(envname, p);
6931 vim_free(p);
6932 }
6933 }
6934 else if (dofree)
6935 vim_free(p);
6936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937}
6938
6939/*
6940 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6941 */
6942 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006943change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006945 int opt_idx;
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (p_cp != on)
6948 {
6949 p_cp = on;
6950 compatible_set();
6951 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006952 opt_idx = findoption((char_u *)"cp");
6953 if (opt_idx >= 0)
6954 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955}
6956
6957/*
6958 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006959 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 */
6961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006962option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
6964 int idx;
6965
6966 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 return FALSE;
6969 if (options[idx].flags & P_WAS_SET)
6970 return TRUE;
6971 return FALSE;
6972}
6973
6974/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006975 * Reset the flag indicating option "name" was set.
6976 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006977 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006978reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006979{
6980 int idx = findoption(name);
6981
6982 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006983 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006984 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006985 return OK;
6986 }
6987 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006988}
6989
6990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 * compatible_set() - Called when 'compatible' has been set or unset.
6992 *
6993 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6994 * flag) to a Vi compatible value.
6995 * When 'compatible' is unset: Set all options that have a different default
6996 * for Vim (without the P_VI_DEF flag) to that default.
6997 */
6998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006999compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
7001 int opt_idx;
7002
Bram Moolenaardac13472019-09-16 21:06:21 +02007003 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7005 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7006 set_option_default(opt_idx, OPT_FREE, p_cp);
7007 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007008 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009}
7010
Bram Moolenaardac13472019-09-16 21:06:21 +02007011#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013/*
7014 * fill_breakat_flags() -- called when 'breakat' changes value.
7015 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007016 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007017fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007019 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 int i;
7021
7022 for (i = 0; i < 256; i++)
7023 breakat_flags[i] = FALSE;
7024
7025 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007026 for (p = p_breakat; *p; p++)
7027 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029#endif
7030
7031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 * Check if backspacing over something is allowed.
7033 */
7034 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007035can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007036 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007038#ifdef FEAT_JOB_CHANNEL
7039 if (what == BS_START && bt_prompt(curbuf))
7040 return FALSE;
7041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 switch (*p_bs)
7043 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007044 case '3': return TRUE;
7045 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 case '1': return (what != BS_START);
7047 case '0': return FALSE;
7048 }
7049 return vim_strchr(p_bs, what) != NULL;
7050}
7051
7052/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007053 * Return the effective 'scrolloff' value for the current window, using the
7054 * global value when appropriate.
7055 */
7056 long
7057get_scrolloff_value(void)
7058{
7059 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7060}
7061
7062/*
7063 * Return the effective 'sidescrolloff' value for the current window, using the
7064 * global value when appropriate.
7065 */
7066 long
7067get_sidescrolloff_value(void)
7068{
7069 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7070}
7071
7072/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007073 * Get the local or global value of 'backupcopy'.
7074 */
7075 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007076get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007077{
7078 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7079}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007080
Gary Johnson53ba05b2021-07-26 22:19:10 +02007081/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007082 * Get the local or global value of 'formatlistpat'.
7083 */
7084 char_u *
7085get_flp_value(buf_T *buf)
7086{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007087 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7088 return p_flp;
7089 return buf->b_p_flp;
7090}
7091
7092/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007093 * Get the local or global value of the 'virtualedit' flags.
7094 */
7095 unsigned int
7096get_ve_flags(void)
7097{
Gary Johnson51ad8502021-08-03 18:33:08 +02007098 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7099 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007100}
7101
Bram Moolenaaree857022019-11-09 23:26:40 +01007102#if defined(FEAT_LINEBREAK) || defined(PROTO)
7103/*
7104 * Get the local or global value of 'showbreak'.
7105 */
7106 char_u *
7107get_showbreak_value(win_T *win)
7108{
7109 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7110 return p_sbr;
7111 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7112 return empty_option;
7113 return win->w_p_sbr;
7114}
7115#endif
7116
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007117#if defined(FEAT_EVAL) || defined(PROTO)
7118/*
7119 * Get window or buffer local options.
7120 */
7121 dict_T *
7122get_winbuf_options(int bufopt)
7123{
7124 dict_T *d;
7125 int opt_idx;
7126
7127 d = dict_alloc();
7128 if (d == NULL)
7129 return NULL;
7130
Bram Moolenaardac13472019-09-16 21:06:21 +02007131 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007132 {
7133 struct vimoption *opt = &options[opt_idx];
7134
7135 if ((bufopt && (opt->indir & PV_BUF))
7136 || (!bufopt && (opt->indir & PV_WIN)))
7137 {
7138 char_u *varp = get_varp(opt);
7139
7140 if (varp != NULL)
7141 {
7142 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007143 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007144 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007145 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007146 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007147 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007148 }
7149 }
7150 }
7151
7152 return d;
7153}
7154#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007155
Bram Moolenaardac13472019-09-16 21:06:21 +02007156#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007157/*
7158 * This is called when 'culopt' is changed
7159 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007160 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007161fill_culopt_flags(char_u *val, win_T *wp)
7162{
7163 char_u *p;
7164 char_u culopt_flags_new = 0;
7165
7166 if (val == NULL)
7167 p = wp->w_p_culopt;
7168 else
7169 p = val;
7170 while (*p != NUL)
7171 {
7172 if (STRNCMP(p, "line", 4) == 0)
7173 {
7174 p += 4;
7175 culopt_flags_new |= CULOPT_LINE;
7176 }
7177 else if (STRNCMP(p, "both", 4) == 0)
7178 {
7179 p += 4;
7180 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7181 }
7182 else if (STRNCMP(p, "number", 6) == 0)
7183 {
7184 p += 6;
7185 culopt_flags_new |= CULOPT_NBR;
7186 }
7187 else if (STRNCMP(p, "screenline", 10) == 0)
7188 {
7189 p += 10;
7190 culopt_flags_new |= CULOPT_SCRLINE;
7191 }
7192
7193 if (*p != ',' && *p != NUL)
7194 return FAIL;
7195 if (*p == ',')
7196 ++p;
7197 }
7198
7199 // Can't have both "line" and "screenline".
7200 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7201 return FAIL;
7202 wp->w_p_culopt_flags = culopt_flags_new;
7203
7204 return OK;
7205}
7206#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007207
7208/*
7209 * Get the value of 'magic' adjusted for Vim9 script.
7210 */
7211 int
7212magic_isset(void)
7213{
7214 switch (magic_overruled)
7215 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007216 case OPTION_MAGIC_ON: return TRUE;
7217 case OPTION_MAGIC_OFF: return FALSE;
7218 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007219 }
7220#ifdef FEAT_EVAL
7221 if (in_vim9script())
7222 return TRUE;
7223#endif
7224 return p_magic;
7225}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007226
7227/*
7228 * Set the callback function value for an option that accepts a function name,
7229 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7230 * Returns OK if the option is successfully set to a function, otherwise
7231 * returns FAIL.
7232 */
7233 int
7234option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7235{
7236#ifdef FEAT_EVAL
7237 typval_T *tv;
7238 callback_T cb;
7239
7240 if (optval == NULL || *optval == NUL)
7241 {
7242 free_callback(optcb);
7243 return OK;
7244 }
7245
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007246 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007247 || (STRNCMP(optval, "function(", 9) == 0)
7248 || (STRNCMP(optval, "funcref(", 8) == 0))
7249 // Lambda expression or a funcref
7250 tv = eval_expr(optval, NULL);
7251 else
7252 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007253 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007254 if (tv == NULL)
7255 return FAIL;
7256
7257 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007258 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007259 {
7260 free_tv(tv);
7261 return FAIL;
7262 }
7263
7264 free_callback(optcb);
7265 set_callback(optcb, &cb);
7266 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007267
7268 // when using Vim9 style "import.funcname" it needs to be expanded to
7269 // "import#funcname".
7270 expand_autload_callback(optcb);
7271
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007272 return OK;
7273#else
7274 return FAIL;
7275#endif
7276}