blob: 339ea4299692c8646dd2c5b31b3e97bf69d3c509 [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
Bram Moolenaar07268702018-03-01 21:57:32 +0100310#ifdef CLEAN_RUNTIMEPATH
311 if (clean_arg)
312 {
313 opt_idx = findoption((char_u *)"runtimepath");
314 if (opt_idx >= 0)
315 {
316 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
317 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
318 }
319 opt_idx = findoption((char_u *)"packpath");
320 if (opt_idx >= 0)
321 {
322 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
323 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
324 }
325 }
326#endif
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_GUI
329 if (found_reverse_arg)
330 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
331#endif
332
333 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100334 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100335 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 check_buf_options(curbuf);
337 check_win_options(curwin);
338 check_options();
339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100340 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 didset_options();
342
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000343#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Use the current chartab for the generic chartab. This is not in
345 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000346 init_spell_chartab();
347#endif
348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 /*
350 * Expand environment variables and things like "~" for the defaults.
351 * If option_expand() returns non-NULL the variable is expanded. This can
352 * only happen for non-indirect options.
353 * Also set the default to the expanded value, so ":set" does not list
354 * them.
355 * Don't set the P_ALLOCED flag, because we don't want to free the
356 * default.
357 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200358 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 {
360 if ((options[opt_idx].flags & P_GETTEXT)
361 && options[opt_idx].var != NULL)
362 p = (char_u *)_(*(char **)options[opt_idx].var);
363 else
364 p = option_expand(opt_idx, NULL);
365 if (p != NULL && (p = vim_strsave(p)) != NULL)
366 {
367 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100368 // VIMEXP
369 // Defaults for all expanded options are currently the same for Vi
370 // and Vim. When this changes, add some code here! Also need to
371 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 if (options[opt_idx].flags & P_DEF_ALLOCED)
373 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
374 options[opt_idx].def_val[VI_DEFAULT] = p;
375 options[opt_idx].flags |= P_DEF_ALLOCED;
376 }
377 }
378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100382 // Detect use of mlterm.
383 // Mlterm is a terminal emulator akin to xterm that has some special
384 // abilities (bidi namely).
385 // NOTE: mlterm's author is being asked to 'set' a variable
386 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (mch_getenv((char_u *)"MLTERM") != NULL)
388 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
389#endif
390
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200391 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Bram Moolenaar4f974752019-02-17 17:44:42 +0100393# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 /*
395 * If $LANG isn't set, try to get a good value for it. This makes the
396 * right language be used automatically. Don't do this for English.
397 */
398 if (mch_getenv((char_u *)"LANG") == NULL)
399 {
400 char buf[20];
401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100402 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
403 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
404 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
406 (LPTSTR)buf, 20);
407 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
408 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100409 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
411 STRCPY(buf, "zh_TW");
412 else if (STRNICMP(buf, "chs", 3) == 0
413 || STRNICMP(buf, "zhc", 3) == 0)
414 STRCPY(buf, "zh_CN");
415 else if (STRNICMP(buf, "jp", 2) == 0)
416 STRCPY(buf, "ja");
417 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100419 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000422# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000423# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000425 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427# endif
428
K.Takataf883d902021-05-30 18:04:19 +0200429# ifdef MSWIN
430 // MS-Windows has builtin support for conversion to and from Unicode, using
431 // "utf-8" for 'encoding' should work best for most users.
432 p = vim_strsave((char_u *)ENC_DFLT);
433# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100434 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200435 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if (p != NULL)
439 {
440 char_u *save_enc;
441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100442 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200443 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 save_enc = p_enc;
445 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000446 if (STRCMP(p_enc, "gb18030") == 0)
447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We don't support "gb18030", but "cp936" is a good substitute
449 // for practical purposes, thus use that. It's not an alias to
450 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000451 p_enc = vim_strsave((char_u *)"cp936");
452 vim_free(p);
453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (mb_init() == NULL)
455 {
456 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000457 if (opt_idx >= 0)
458 {
459 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
460 options[opt_idx].flags |= P_DEF_ALLOCED;
461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaard0573012017-10-28 21:11:06 +0200463#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100464 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100466 // Adjust the default for 'isprint' and 'iskeyword' to match
467 // latin1. Also set the defaults for when 'nocompatible' is
468 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000469 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000470 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000471 set_string_option_direct((char_u *)"isk", -1,
472 ISK_LATIN1, OPT_FREE, SID_NONE);
473 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000474 if (opt_idx >= 0)
475 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000476 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000477 if (opt_idx >= 0)
478 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000479 (void)init_chartab();
480 }
481#endif
482
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200483#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100484 // Win32 console: When GetACP() returns a different value from
485 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200486 if (
487# ifdef VIMDLL
488 (!gui.in_use && !gui.starting) &&
489# endif
490 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 {
492 char buf[50];
493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
495 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100496 if (GetConsoleCP() == 0)
497 sprintf(buf, "cp%ld", (long)GetACP());
498 else
499 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 p_tenc = vim_strsave((char_u *)buf);
501 if (p_tenc != NULL)
502 {
503 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000504 if (opt_idx >= 0)
505 {
506 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
507 options[opt_idx].flags |= P_DEF_ALLOCED;
508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 convert_setup(&input_conv, p_tenc, p_enc);
510 convert_setup(&output_conv, p_enc, p_tenc);
511 }
512 else
513 p_tenc = empty_option;
514 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100515#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100516#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100517 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000518 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521 else
522 {
523 vim_free(p_enc);
524 p_enc = save_enc;
525 }
526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
528#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100529 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 set_helplang_default(get_mess_lang());
531#endif
532}
533
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200534static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
535
536/*
537 * Set the "fileencodings" option to the default value for when 'encoding' is
538 * utf-8.
539 */
540 void
541set_fencs_unicode()
542{
543 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
544 OPT_FREE, 0);
545}
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547/*
548 * Set an option to its default value.
549 * This does not take care of side effects!
550 */
551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552set_option_default(
553 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100554 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
555 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 char_u *varp; // pointer to variable for current option
558 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000560 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
562
563 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
564 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100565 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
567 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
568 if (flags & P_STRING)
569 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200570 // 'fencs' default value depends on 'encoding'
571 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
572 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100573 // Use set_string_option_direct() for local options to handle
574 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200575 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200576 set_string_option_direct(NULL, opt_idx,
577 options[opt_idx].def_val[dvi], opt_flags, 0);
578 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200580 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
581 free_string_option(*(char_u **)(varp));
582 *(char_u **)varp = options[opt_idx].def_val[dvi];
583 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585 }
586 else if (flags & P_NUM)
587 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000588 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 win_comp_scroll(curwin);
590 else
591 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100592 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
593
594 if ((long *)varp == &curwin->w_p_so
595 || (long *)varp == &curwin->w_p_siso)
596 // 'scrolloff' and 'sidescrolloff' local values have a
597 // different default value than the global default.
598 *(long *)varp = -1;
599 else
600 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100601 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (both)
603 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100604 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100607 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100609 // the cast to long is required for Manx C, long_i is needed for
610 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000611 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000612#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000614 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
615 *(int *)varp = FALSE;
616#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 if (both)
619 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
620 *(int *)varp;
621 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100623 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000624 flagsp = insecure_flag(opt_idx, opt_flags);
625 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
627
628#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200629 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#endif
631}
632
633/*
634 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200635 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 */
637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100638set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100639 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000643 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaardac13472019-09-16 21:06:21 +0200645 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200646 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200647 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100648 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200649# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200650 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200651 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200652# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100653 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 set_option_default(i, opt_flags, p_cp);
655
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100656 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000657 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200659#ifdef FEAT_CINDENT
660 parse_cino(curbuf);
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662}
663
664/*
665 * Set the Vi-default value of a string option.
666 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100667 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100669 static void
670set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 char_u *p;
673 int opt_idx;
674
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100675 if (escape && vim_strchr(val, ' ') != NULL)
676 p = vim_strsave_escaped(val, (char_u *)" ");
677 else
678 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
681 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000682 if (opt_idx >= 0)
683 {
684 if (options[opt_idx].flags & P_DEF_ALLOCED)
685 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
686 options[opt_idx].def_val[VI_DEFAULT] = p;
687 options[opt_idx].flags |= P_DEF_ALLOCED;
688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690}
691
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100692 void
693set_string_default(char *name, char_u *val)
694{
695 set_string_default_esc(name, val, FALSE);
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200699 * For an option value that contains comma separated items, find "newval" in
700 * "origval". Return NULL if not found.
701 */
702 static char_u *
703find_dup_item(char_u *origval, char_u *newval, long_u flags)
704{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200705 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200706 size_t newlen;
707 char_u *s;
708
709 if (origval == NULL)
710 return NULL;
711
712 newlen = STRLEN(newval);
713 for (s = origval; *s != NUL; ++s)
714 {
715 if ((!(flags & P_COMMA)
716 || s == origval
717 || (s[-1] == ',' && !(bs & 1)))
718 && STRNCMP(s, newval, newlen) == 0
719 && (!(flags & P_COMMA)
720 || s[newlen] == ','
721 || s[newlen] == NUL))
722 return s;
723 // Count backslashes. Only a comma with an even number of backslashes
724 // or a single backslash preceded by a comma before it is recognized as
725 // a separator.
726 if ((s > origval + 1
727 && s[-1] == '\\'
728 && s[-2] != ',')
729 || (s == origval + 1
730 && s[-1] == '\\'))
731 ++bs;
732 else
733 bs = 0;
734 }
735 return NULL;
736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Set the Vi-default value of a number option.
740 * Used for 'lines' and 'columns'.
741 */
742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100743set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000745 int opt_idx;
746
747 opt_idx = findoption((char_u *)name);
748 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000749 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
Dominique Pelle748b3082022-01-08 12:41:16 +0000752#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200753/*
754 * Set all window-local and buffer-local options to the Vim default.
755 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200756 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200757 */
758 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200759set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760{
761 win_T *save_curwin = curwin;
762 int i;
763
764 curwin = wp;
765 curbuf = curwin->w_buffer;
766 block_autocmds();
767
Bram Moolenaardac13472019-09-16 21:06:21 +0200768 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200769 {
770 struct vimoption *p = &(options[i]);
771 char_u *varp = get_varp_scope(p, OPT_LOCAL);
772
773 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200774 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200775 && !(options[i].flags & P_NODEFAULT)
776 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200777 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 }
779
780 unblock_autocmds();
781 curwin = save_curwin;
782 curbuf = curwin->w_buffer;
783}
Dominique Pelle748b3082022-01-08 12:41:16 +0000784#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200785
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000786#if defined(EXITFREE) || defined(PROTO)
787/*
788 * Free all options.
789 */
790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000792{
793 int i;
794
Bram Moolenaardac13472019-09-16 21:06:21 +0200795 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000796 {
797 if (options[i].indir == PV_NONE)
798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100799 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100800 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000801 free_string_option(*(char_u **)options[i].var);
802 if (options[i].flags & P_DEF_ALLOCED)
803 free_string_option(options[i].def_val[VI_DEFAULT]);
804 }
805 else if (options[i].var != VAR_WIN
806 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100807 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200808 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000809 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000810 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000811 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000812}
813#endif
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Initialize the options, part two: After getting Rows and Columns and
818 * setting 'term'.
819 */
820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100821set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 int idx;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100826 * 'scroll' defaults to half the window height. The stored default is zero,
827 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000829 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000830 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 comp_col();
833
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 /*
835 * 'window' is only for backwards compatibility with Vi.
836 * Default is Rows - 1.
837 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000838 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 p_window = Rows - 1;
840 set_number_default("window", Rows - 1);
841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100842 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000844 /*
845 * If 'background' wasn't set by the user, try guessing the value,
846 * depending on the terminal name. Only need to check for terminals
847 * with a dark background, that can handle color.
848 */
849 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000850 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
851 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000853 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // don't mark it as set, when starting the GUI it may be
855 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000856 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000859
860#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000862#endif
863#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000865#endif
866#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
871/*
872 * Initialize the options, part three: After reading the .vimrc
873 */
874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100875set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878/*
879 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
880 * This is done after other initializations, where 'shell' might have been
881 * set, but only if they have not been set before.
882 */
883 char_u *p;
884 int idx_srr;
885 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int idx_sp;
888 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000892 if (idx_srr < 0)
893 do_srr = FALSE;
894 else
895 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100896# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000898 if (idx_sp < 0)
899 do_sp = FALSE;
900 else
901 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200903 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (p != NULL)
905 {
906 /*
907 * Default for p_sp is "| tee", for p_srr is ">".
908 * For known shells it is changed here to include stderr.
909 */
910 if ( fnamecmp(p, "csh") == 0
911 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100912# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 || fnamecmp(p, "csh.exe") == 0
914 || fnamecmp(p, "tcsh.exe") == 0
915# endif
916 )
917 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100918# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (do_sp)
920 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100923# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
927 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (do_srr)
930 {
931 p_srr = (char_u *)">&";
932 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
933 }
934 }
Mike Williams12795022021-06-28 20:53:58 +0200935# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200936 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
937 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200938 else if ( fnamecmp(p, "powershell") == 0
939 || fnamecmp(p, "powershell.exe") == 0
940 )
941 {
942# if defined(FEAT_QUICKFIX)
943 if (do_sp)
944 {
945 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
946 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
947 }
948# endif
949 if (do_srr)
950 {
951 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
952 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
953 }
954 }
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Natanael Copa56318362021-05-06 18:46:35 +0200957 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ( fnamecmp(p, "sh") == 0
959 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200960 || fnamecmp(p, "mksh") == 0
961 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000963 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200965 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200966 || fnamecmp(p, "ash") == 0
967 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200968 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 || fnamecmp(p, "cmd") == 0
971 || fnamecmp(p, "sh.exe") == 0
972 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200973 || fnamecmp(p, "mksh.exe") == 0
974 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000976 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "bash.exe") == 0
978 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200979 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200980 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (do_sp)
986 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100987# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100989# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200990 if (fnamecmp(p, "pwsh") == 0)
991 p_sp = (char_u *)">%s 2>&1";
992 else
993 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
996 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (do_srr)
999 {
1000 p_srr = (char_u *)">%s 2>&1";
1001 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1002 }
1003 }
1004 vim_free(p);
1005 }
1006#endif
1007
Bram Moolenaar4f974752019-02-17 17:44:42 +01001008#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1011 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001013 * set, but only if they have not been set before.
1014 * Default values depend on shell (cmd.exe is default shell):
1015 *
1016 * p_shcf p_sxq
1017 * cmd.exe - "/c" "("
1018 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001019 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001020 * "sh" like shells - "-c" "\""
1021 *
1022 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 */
Mike Williams12795022021-06-28 20:53:58 +02001024 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1025 {
1026 int idx_opt;
1027
1028 idx_opt = findoption((char_u *)"shcf");
1029 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1030 {
1031 p_shcf = (char_u*)"-Command";
1032 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1033 }
1034
1035 idx_opt = findoption((char_u *)"sxq");
1036 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1037 {
1038 p_sxq = (char_u*)"\"";
1039 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1040 }
1041 }
1042 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 int idx3;
1045
1046 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001047 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 p_shcf = (char_u *)"-c";
1050 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1051 }
1052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001053 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001055 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 p_sxq = (char_u *)"\"";
1058 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001061 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1062 {
1063 int idx3;
1064
1065 /*
1066 * cmd.exe on Windows will strip the first and last double quote given
1067 * on the command line, e.g. most of the time things like:
1068 * cmd /c "my path/to/echo" "my args to echo"
1069 * become:
1070 * my path/to/echo" "my args to echo
1071 * when executed.
1072 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001073 * To avoid this, set shellxquote to surround the command in
1074 * parenthesis. This appears to make most commands work, without
1075 * breaking commands that worked previously, such as
1076 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001077 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001078 idx3 = findoption((char_u *)"sxq");
1079 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1080 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001081 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001082 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1083 }
1084
Bram Moolenaara64ba222012-02-12 23:23:31 +01001085 idx3 = findoption((char_u *)"shcf");
1086 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1087 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001088 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001094 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001095 {
1096 int idx_ffs = findoption((char_u *)"ffs");
1097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001098 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001099 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1100 set_fileformat(default_fileformat(), OPT_LOCAL);
1101 }
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104}
1105
1106#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1107/*
1108 * When 'helplang' is still at its default value, set it to "lang".
1109 * Only the first two characters of "lang" are used.
1110 */
1111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001112set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int idx;
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 return;
1118 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001119 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 {
1121 if (options[idx].flags & P_ALLOCED)
1122 free_string_option(p_hlg);
1123 p_hlg = vim_strsave(lang);
1124 if (p_hlg == NULL)
1125 p_hlg = empty_option;
1126 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001127 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001128 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001129 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1130 {
1131 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1132 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1133 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001134 // any C like setting, such as C.UTF-8, becomes "en"
1135 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1136 {
1137 p_hlg[0] = 'e';
1138 p_hlg[1] = 'n';
1139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 options[idx].flags |= P_ALLOCED;
1143 }
1144}
1145#endif
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147/*
1148 * 'title' and 'icon' only default to true if they have not been set or reset
1149 * in .vimrc and we can read the old value.
1150 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1151 * they can be reset. This reduces startup time when using X on a remote
1152 * machine.
1153 */
1154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001155set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 int idx1;
1158 long val;
1159
1160 /*
1161 * If GUI is (going to be) used, we can always set the window title and
1162 * icon name. Saves a bit of time, because the X11 display server does
1163 * not need to be contacted.
1164 */
1165 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001166 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168#ifdef FEAT_GUI
1169 if (gui.starting || gui.in_use)
1170 val = TRUE;
1171 else
1172#endif
1173 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001174 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 p_title = val;
1176 }
1177 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001178 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180#ifdef FEAT_GUI
1181 if (gui.starting || gui.in_use)
1182 val = TRUE;
1183 else
1184#endif
1185 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001186 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 p_icon = val;
1188 }
1189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001191 void
1192ex_set(exarg_T *eap)
1193{
1194 int flags = 0;
1195
1196 if (eap->cmdidx == CMD_setlocal)
1197 flags = OPT_LOCAL;
1198 else if (eap->cmdidx == CMD_setglobal)
1199 flags = OPT_GLOBAL;
1200#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001201 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001202 ex_options(eap);
1203 else
1204#endif
1205 {
1206 if (eap->forceit)
1207 flags |= OPT_ONECOLUMN;
1208 (void)do_set(eap->arg, flags);
1209 }
1210}
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212/*
1213 * Parse 'arg' for option settings.
1214 *
1215 * 'arg' may be IObuff, but only when no errors can be present and option
1216 * does not need to be expanded with option_expand().
1217 * "opt_flags":
1218 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001219 * OPT_GLOBAL for ":setglobal"
1220 * OPT_LOCAL for ":setlocal" and a modeline
1221 * OPT_MODELINE for a modeline
1222 * OPT_WINONLY to only set window-local options
1223 * OPT_NOWIN to skip setting window-local options
1224 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 *
1226 * returns FAIL if an error is detected, OK otherwise
1227 */
1228 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001229do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001230 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001231 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001233 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 char *errmsg;
1236 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001238 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1239 int nextchar; // next non-white char after option name
1240 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 int len;
1242 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001243 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 long_u flags; // flags for current option
1246 char_u *varp = NULL; // pointer to variable for current option
1247 int did_show = FALSE; // already showed one value
1248 int adding; // "opt+=arg"
1249 int prepending; // "opt^=arg"
1250 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 int cp_val = 0;
1252 char_u key_name[2];
1253
1254 if (*arg == NUL)
1255 {
1256 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001257 did_show = TRUE;
1258 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001266 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1267 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269 /*
1270 * ":set all" show all options.
1271 * ":set all&" set all options to their default value.
1272 */
1273 arg += 3;
1274 if (*arg == '&')
1275 {
1276 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001277 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001279 didset_options();
1280 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001281 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001286 did_show = TRUE;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001289 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
1291 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001292 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001293 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 arg += 7;
1295 }
1296 else
1297 {
1298 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001299 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
1301 prefix = 0;
1302 arg += 2;
1303 }
1304 else if (STRNCMP(arg, "inv", 3) == 0)
1305 {
1306 prefix = 2;
1307 arg += 3;
1308 }
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 key = 0;
1312 if (*arg == '<')
1313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001315 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1317 len = 5;
1318 else
1319 {
1320 len = 1;
1321 while (arg[len] != NUL && arg[len] != '>')
1322 ++len;
1323 }
1324 if (arg[len] != '>')
1325 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001326 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 goto skip;
1328 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001329 arg[len] = NUL; // put NUL after name
1330 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001332 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001334 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 else
1337 {
1338 len = 0;
1339 /*
1340 * The two characters after "t_" may not be alphanumeric.
1341 */
1342 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1343 len = 4;
1344 else
1345 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1346 ++len;
1347 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001348 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001352 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001355 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 afterchar = arg[len];
1357
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001358 if (in_vim9script())
1359 {
1360 char_u *p = skipwhite(arg + len);
1361
1362 // disallow white space before =val, +=val, -=val, ^=val
1363 if (p > arg + len && (p[0] == '='
1364 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1365 && p[1] == '=')))
1366 {
1367 errmsg = e_no_white_space_allowed_between_option_and;
1368 arg = p;
1369 startarg = p;
1370 goto skip;
1371 }
1372 }
1373 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001374 // skip white space, allow ":set ai ?", ":set hlsearch !"
1375 while (VIM_ISWHITE(arg[len]))
1376 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 adding = FALSE;
1379 prepending = FALSE;
1380 removing = FALSE;
1381 if (arg[len] != NUL && arg[len + 1] == '=')
1382 {
1383 if (arg[len] == '+')
1384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001385 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 ++len;
1387 }
1388 else if (arg[len] == '^')
1389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001390 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 ++len;
1392 }
1393 else if (arg[len] == '-')
1394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001395 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 ++len;
1397 }
1398 }
1399 nextchar = arg[len];
1400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001403 if (in_vim9script() && arg > arg_start
1404 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1405 errmsg = e_no_white_space_allowed_between_option_and;
1406 else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001407 errmsg = N_(e_unknown_option);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 goto skip;
1409 }
1410
1411 if (opt_idx >= 0)
1412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001413 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 // Only give an error message when requesting the value of
1416 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1418 && (!(options[opt_idx].flags & P_BOOL)
1419 || nextchar == '?'))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001420 errmsg = N_(e_option_not_supported);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 goto skip;
1422 }
1423
1424 flags = options[opt_idx].flags;
1425 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1426 }
1427 else
1428 {
1429 flags = P_STRING;
1430 if (key < 0)
1431 {
1432 key_name[0] = KEY2TERMCAP0(key);
1433 key_name[1] = KEY2TERMCAP1(key);
1434 }
1435 else
1436 {
1437 key_name[0] = KS_KEY;
1438 key_name[1] = (key & 0xff);
1439 }
1440 }
1441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001442 // Skip all options that are not window-local (used when showing
1443 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001444 if ((opt_flags & OPT_WINONLY)
1445 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1446 goto skip;
1447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001448 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001449 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1450 && options[opt_idx].var == VAR_WIN)
1451 goto skip;
1452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001454 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001456 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001457 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001458 errmsg = N_(e_not_allowed_in_modeline);
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001459 goto skip;
1460 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001461 if ((flags & P_MLE) && !p_mle)
1462 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001463 errmsg = N_(e_not_allowed_in_modeline_when_modelineexpr_is_off);
Bram Moolenaar110289e2019-05-23 15:38:06 +02001464 goto skip;
1465 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001466#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 // In diff mode some options are overruled. This avoids that
1468 // 'foldmethod' becomes "marker" instead of "diff" and that
1469 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001470 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001472 && (
1473#ifdef FEAT_FOLDING
1474 options[opt_idx].indir == PV_FDM ||
1475#endif
1476 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001477 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480
1481#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001482 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001483 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001485 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 goto skip;
1487 }
1488#endif
1489
1490 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1491 {
1492 arg += len;
1493 cp_val = p_cp;
1494 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001496 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 cp_val = FALSE;
1499 arg += 3;
1500 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001501 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 cp_val = TRUE;
1504 arg += 2;
1505 }
1506 }
1507 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001508 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001510 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 goto skip;
1512 }
1513 }
1514
1515 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001516 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001517 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 if (nextchar == '?'
1520 || (prefix == 1
1521 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1522 && !(flags & P_BOOL)))
1523 {
1524 /*
1525 * print value
1526 */
1527 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001528 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 else
1530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 gotocmdline(TRUE); // cursor at status line
1532 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 if (opt_idx >= 0)
1535 {
1536 showoneopt(&options[opt_idx], opt_flags);
1537#ifdef FEAT_EVAL
1538 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001539 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001540 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001541 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001542 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001543 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001544 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001545 (int)options[opt_idx].indir & PV_MASK]);
1546 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001547 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001548 (int)options[opt_idx].indir & PV_MASK]);
1549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550#endif
1551 }
1552 else
1553 {
1554 char_u *p;
1555
1556 p = find_termcode(key_name);
1557 if (p == NULL)
1558 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001559 errmsg = N_(e_key_code_not_set);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 goto skip;
1561 }
1562 else
1563 (void)show_one_termcode(key_name, p, TRUE);
1564 }
1565 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001566 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001567 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 else
1570 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001571 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001572 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001574 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 if (nextchar == '=' || nextchar == ':')
1577 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001578 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 goto skip;
1580 }
1581
1582 /*
1583 * ":set opt!": invert
1584 * ":set opt&": reset to default value
1585 * ":set opt<": reset to global value
1586 */
1587 if (nextchar == '!')
1588 value = *(int *)(varp) ^ 1;
1589 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 ((flags & P_VI_DEF) || cp_val)
1592 ? VI_DEFAULT : VIM_DEFAULT];
1593 else if (nextchar == '<')
1594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001595 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if ((int *)varp == &curbuf->b_p_ar
1597 && opt_flags == OPT_LOCAL)
1598 value = -1;
1599 else
1600 value = *(int *)get_varp_scope(&(options[opt_idx]),
1601 OPT_GLOBAL);
1602 }
1603 else
1604 {
1605 /*
1606 * ":set invopt": invert
1607 * ":set opt" or ":set noopt": set or reset
1608 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001609 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001611 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 goto skip;
1613 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 value = *(int *)(varp) ^ 1;
1616 else
1617 value = prefix;
1618 }
1619
1620 errmsg = set_bool_option(opt_idx, varp, (int)value,
1621 opt_flags);
1622 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001623 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1626 || prefix != 1)
1627 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001628 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 goto skip;
1630 }
1631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001632 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 {
1634 /*
1635 * Different ways to set a number option:
1636 * & set to default value
1637 * < set to global value
1638 * <xx> accept special key codes for 'wildchar'
1639 * c accept any non-digit for 'wildchar'
1640 * [-]0-9 set number
1641 * other error
1642 */
1643 ++arg;
1644 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001645 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 ((flags & P_VI_DEF) || cp_val)
1647 ? VI_DEFAULT : VIM_DEFAULT];
1648 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001650 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1651 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001652 if ((long *)varp == &curbuf->b_p_ul
1653 && opt_flags == OPT_LOCAL)
1654 value = NO_LOCAL_UNDOLEVEL;
1655 else
1656 value = *(long *)get_varp_scope(
1657 &(options[opt_idx]), OPT_GLOBAL);
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 else if (((long *)varp == &p_wc
1660 || (long *)varp == &p_wcm)
1661 && (*arg == '<'
1662 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001663 || (*arg != NUL
1664 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 && !VIM_ISDIGIT(*arg))))
1666 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001667 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (value == 0 && (long *)varp != &p_wcm)
1669 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001670 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 goto skip;
1672 }
1673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1675 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001676 // Allow negative (for 'undolevels'), octal and
1677 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001678 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001679 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001680 if (i == 0 || (arg[i] != NUL
1681 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001683 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 goto skip;
1685 }
1686 }
1687 else
1688 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001689 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 goto skip;
1691 }
1692
1693 if (adding)
1694 value = *(long *)varp + value;
1695 if (prepending)
1696 value = *(long *)varp * value;
1697 if (removing)
1698 value = *(long *)varp - value;
1699 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001700 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001702 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001704 char_u *save_arg = NULL;
1705 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001706 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001707 char_u *newval;
1708 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001709 char_u *origval_l = NULL;
1710 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001711#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001712 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001713 char_u *saved_origval_l = NULL;
1714 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001715 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001716#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001717 unsigned newlen;
1718 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001719 int new_value_alloced; // new string option
1720 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001722 // When using ":set opt=val" for a global option
1723 // with a local value the local value will be
1724 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001726 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 varp = options[opt_idx].var;
1728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001729 // The old value is kept until we are sure that the
1730 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001732
Bram Moolenaard7c96872019-06-15 17:12:48 +02001733 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1734 {
1735 origval_l = *(char_u **)get_varp_scope(
1736 &(options[opt_idx]), OPT_LOCAL);
1737 origval_g = *(char_u **)get_varp_scope(
1738 &(options[opt_idx]), OPT_GLOBAL);
1739
1740 // A global-local string option might have an empty
1741 // option as value to indicate that the global
1742 // value should be used.
1743 if (((int)options[opt_idx].indir & PV_BOTH)
1744 && origval_l == empty_option)
1745 origval_l = origval_g;
1746 }
1747
1748 // When setting the local value of a global
1749 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001750 if (((int)options[opt_idx].indir & PV_BOTH)
1751 && (opt_flags & OPT_LOCAL))
1752 origval = *(char_u **)get_varp(
1753 &options[opt_idx]);
1754 else
1755 origval = oldval;
1756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001757 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 newval = options[opt_idx].def_val[
1760 ((flags & P_VI_DEF) || cp_val)
1761 ? VI_DEFAULT : VIM_DEFAULT];
1762 if ((char_u **)varp == &p_bg)
1763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001764 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#ifdef FEAT_GUI
1766 if (gui.in_use)
1767 newval = gui_bg_default();
1768 else
1769#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001770 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001772 else if ((char_u **)varp == &p_fencs && enc_utf8)
1773 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001775 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001776 // default value was already expanded, only
1777 // required when an environment variable was set
1778 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (newval == NULL)
1780 newval = empty_option;
1781 else
1782 {
1783 s = option_expand(opt_idx, newval);
1784 if (s == NULL)
1785 s = newval;
1786 newval = vim_strsave(s);
1787 }
1788 new_value_alloced = TRUE;
1789 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001790 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 newval = vim_strsave(*(char_u **)get_varp_scope(
1793 &(options[opt_idx]), OPT_GLOBAL));
1794 new_value_alloced = TRUE;
1795 }
1796 else
1797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001798 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 /*
1801 * Set 'keywordprg' to ":help" if an empty
1802 * value was passed to :set by the user.
1803 * Misuse errbuf[] for the resulting string.
1804 */
1805 if (varp == (char_u *)&p_kp
1806 && (*arg == NUL || *arg == ' '))
1807 {
1808 STRCPY(errbuf, ":help");
1809 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001810 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001813 * Convert 'backspace' number to string, for
1814 * adding, prepending and removing string.
1815 */
1816 else if (varp == (char_u *)&p_bs
1817 && VIM_ISDIGIT(**(char_u **)varp))
1818 {
1819 i = getdigits((char_u **)varp);
1820 switch (i)
1821 {
1822 case 0:
1823 *(char_u **)varp = empty_option;
1824 break;
1825 case 1:
1826 *(char_u **)varp = vim_strsave(
1827 (char_u *)"indent,eol");
1828 break;
1829 case 2:
1830 *(char_u **)varp = vim_strsave(
1831 (char_u *)"indent,eol,start");
1832 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001833 case 3:
1834 *(char_u **)varp = vim_strsave(
1835 (char_u *)"indent,eol,nostop");
1836 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001837 }
1838 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001839 if (origval == oldval)
1840 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001841 if (origval_l == oldval)
1842 origval_l = *(char_u **)varp;
1843 if (origval_g == oldval)
1844 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001845 oldval = *(char_u **)varp;
1846 }
1847 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 * Convert 'whichwrap' number to string, for
1849 * backwards compatibility with Vim 3.0.
1850 * Misuse errbuf[] for the resulting string.
1851 */
1852 else if (varp == (char_u *)&p_ww
1853 && VIM_ISDIGIT(*arg))
1854 {
1855 *errbuf = NUL;
1856 i = getdigits(&arg);
1857 if (i & 1)
1858 STRCAT(errbuf, "b,");
1859 if (i & 2)
1860 STRCAT(errbuf, "s,");
1861 if (i & 4)
1862 STRCAT(errbuf, "h,l,");
1863 if (i & 8)
1864 STRCAT(errbuf, "<,>,");
1865 if (i & 16)
1866 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001867 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 errbuf[STRLEN(errbuf) - 1] = NUL;
1869 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001870 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 /*
1873 * Remove '>' before 'dir' and 'bdir', for
1874 * backwards compatibility with version 3.0
1875 */
1876 else if ( *arg == '>'
1877 && (varp == (char_u *)&p_dir
1878 || varp == (char_u *)&p_bdir))
1879 {
1880 ++arg;
1881 }
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 /*
1884 * Copy the new string into allocated memory.
1885 * Can't use set_string_option_direct(), because
1886 * we need to remove the backslashes.
1887 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 newlen = (unsigned)STRLEN(arg) + 1;
1890 if (adding || prepending || removing)
1891 newlen += (unsigned)STRLEN(origval) + 1;
1892 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 break;
1895 s = newval;
1896
1897 /*
1898 * Copy the string, skip over escaped chars.
1899 * For MS-DOS and WIN32 backslashes before normal
1900 * file name characters are not removed, and keep
1901 * backslash at start, for "\\machine\path", but
1902 * do remove it for "\\\\machine\\path".
1903 * The reverse is found in ExpandOldSetting().
1904 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001905 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 if (*arg == '\\' && arg[1] != NUL
1908#ifdef BACKSLASH_IN_FILENAME
1909 && !((flags & P_EXPAND)
1910 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001911 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 && (arg[1] != '\\'
1913 || (s == newval
1914 && arg[2] != '\\')))
1915#endif
1916 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001917 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001919 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001921 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 mch_memmove(s, arg, (size_t)i);
1923 arg += i;
1924 s += i;
1925 }
1926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 *s++ = *arg++;
1928 }
1929 *s = NUL;
1930
1931 /*
1932 * Expand environment variables and ~.
1933 * Don't do it when adding without inserting a
1934 * comma.
1935 */
1936 if (!(adding || prepending || removing)
1937 || (flags & P_COMMA))
1938 {
1939 s = option_expand(opt_idx, newval);
1940 if (s != NULL)
1941 {
1942 vim_free(newval);
1943 newlen = (unsigned)STRLEN(s) + 1;
1944 if (adding || prepending || removing)
1945 newlen += (unsigned)STRLEN(origval) + 1;
1946 newval = alloc(newlen);
1947 if (newval == NULL)
1948 break;
1949 STRCPY(newval, s);
1950 }
1951 }
1952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001953 // locate newval[] in origval[] when removing it
1954 // and when adding to avoid duplicates
1955 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (removing || (flags & P_NODUP))
1957 {
1958 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001959 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001961 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001962 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 prepending = FALSE;
1965 adding = FALSE;
1966 STRCPY(newval, origval);
1967 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001968
1969 // if no duplicate, move pointer to end of
1970 // original value
1971 if (s == NULL)
1972 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001975 // concatenate the two strings; add a ',' if
1976 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (adding || prepending)
1978 {
1979 comma = ((flags & P_COMMA) && *origval != NUL
1980 && *newval != NUL);
1981 if (adding)
1982 {
1983 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001984 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001985 if (comma && i > 1
1986 && (flags & P_ONECOMMA) == P_ONECOMMA
1987 && origval[i - 1] == ','
1988 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001989 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 mch_memmove(newval + i + comma, newval,
1991 STRLEN(newval) + 1);
1992 mch_memmove(newval, origval, (size_t)i);
1993 }
1994 else
1995 {
1996 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001997 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (comma)
2000 newval[i] = ',';
2001 }
2002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002003 // Remove newval[] from origval[]. (Note: "i" has
2004 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (removing)
2006 {
2007 STRCPY(newval, origval);
2008 if (*s)
2009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002010 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 if (flags & P_COMMA)
2012 {
2013 if (s == origval)
2014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002015 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (s[i] == ',')
2017 ++i;
2018 }
2019 else
2020 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002021 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 --s;
2023 ++i;
2024 }
2025 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002026 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 }
2029
2030 if (flags & P_FLAGLIST)
2031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002032 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002033 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002035 // if options have P_FLAGLIST and
2036 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002037 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002039 if (*s != ',' && *(s + 1) == ','
2040 && vim_strchr(s + 2, *s) != NULL)
2041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002042 // Remove the duplicated value and
2043 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002044 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002045 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 else
2049 {
2050 if ((!(flags & P_COMMA) || *s != ',')
2051 && vim_strchr(s + 1, *s) != NULL)
2052 {
2053 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002054 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002055 }
2056 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002057 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002061 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 arg = save_arg;
2063 new_value_alloced = TRUE;
2064 }
2065
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002066 /*
2067 * Set the new value.
2068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 *(char_u **)(varp) = newval;
2070
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002071#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002072 if (!starting
2073# ifdef FEAT_CRYPT
2074 && options[opt_idx].indir != PV_KEY
2075# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002076 && origval != NULL && newval != NULL)
2077 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002078 // origval may be freed by
2079 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002080 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002081 // newval (and varp) may become invalid if the
2082 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002083 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002084 if (origval_l != NULL)
2085 saved_origval_l = vim_strsave(origval_l);
2086 if (origval_g != NULL)
2087 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002088 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002089#endif
2090
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002091 {
2092 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002093 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002094
2095 // When an option is set in the sandbox, from a
2096 // modeline or in secure mode, then deal with side
2097 // effects in secure mode. Also when the value was
2098 // set with the P_INSECURE flag and is not
2099 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002100 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002101#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002102 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002103#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002104 || (!value_is_replaced && (*p & P_INSECURE)))
2105 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002106
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002107 // Handle side effects, and set the global value
2108 // for ":set" on local options. Note: when setting
2109 // 'syntax' or 'filetype' autocommands may be
2110 // triggered that can cause havoc.
2111 errmsg = did_set_string_option(
2112 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002113 new_value_alloced, oldval, errbuf,
2114 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002115
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002116 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002119#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002120 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002121 trigger_optionsset_string(
2122 opt_idx, opt_flags, saved_origval,
2123 saved_origval_l, saved_origval_g,
2124 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002125 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002126 vim_free(saved_origval_l);
2127 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002128 vim_free(saved_newval);
2129#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002130 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 if (errmsg != NULL)
2132 goto skip;
2133 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002134 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 char_u *p;
2137
2138 if (nextchar == '&')
2139 {
2140 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002141 errmsg = N_(e_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 else
2144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002146 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (*p == '\\' && p[1] != NUL)
2148 ++p;
2149 nextchar = *p;
2150 *p = NUL;
2151 add_termcode(key_name, arg, FALSE);
2152 *p = nextchar;
2153 }
2154 if (full_screen)
2155 ttest(FALSE);
2156 redraw_all_later(CLEAR);
2157 }
2158 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002159
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002161 did_set_option(
2162 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164
2165skip:
2166 /*
2167 * Advance to next argument.
2168 * - skip until a blank found, taking care of backslashes
2169 * - skip blanks
2170 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2171 */
2172 for (i = 0; i < 2 ; ++i)
2173 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002174 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (*arg++ == '\\' && *arg != NUL)
2176 ++arg;
2177 arg = skipwhite(arg);
2178 if (*arg != '=')
2179 break;
2180 }
2181 }
2182
2183 if (errmsg != NULL)
2184 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002185 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (i + (arg - startarg) < IOSIZE)
2188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002189 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 STRCAT(IObuff, ": ");
2191 mch_memmove(IObuff + i, startarg, (arg - startarg));
2192 IObuff[i + (arg - startarg)] = NUL;
2193 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 trans_characters(IObuff, IOSIZE);
2196
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 ++no_wait_return; // wait_return done later
2198 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 --no_wait_return;
2200
2201 return FAIL;
2202 }
2203
2204 arg = skipwhite(arg);
2205 }
2206
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207theend:
2208 if (silent_mode && did_show)
2209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002210 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002212 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 out_flush();
2216 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218 }
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 return OK;
2221}
2222
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002223/*
2224 * Call this when an option has been given a new value through a user command.
2225 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2226 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002228did_set_option(
2229 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002230 int opt_flags, // possibly with OPT_MODELINE
2231 int new_value, // value was replaced completely
2232 int value_checked) // value was checked to be safe, no need to set the
2233 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002234{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002235 long_u *p;
2236
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002237 options[opt_idx].flags |= P_WAS_SET;
2238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002239 // When an option is set in the sandbox, from a modeline or in secure mode
2240 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2241 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002242 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002243 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002244#ifdef HAVE_SANDBOX
2245 || sandbox != 0
2246#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002247 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002248 *p = *p | P_INSECURE;
2249 else if (new_value)
2250 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002251}
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253/*
2254 * Convert a key name or string into a key value.
2255 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002256 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002258 int
2259string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002262 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (*arg == '^')
2264 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002265 if (multi_byte)
2266 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 return *arg;
2268}
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270/*
2271 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2272 * maketitle() to create and display it.
2273 * When switching the title or icon off, call mch_restore_title() to get
2274 * the old value back.
2275 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002276 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002277did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
2279 if (starting != NO_SCREEN
2280#ifdef FEAT_GUI
2281 && !gui.starting
2282#endif
2283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287/*
2288 * set_options_bin - called when 'bin' changes value.
2289 */
2290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291set_options_bin(
2292 int oldval,
2293 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002294 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 /*
2297 * The option values that are changed when 'bin' changes are
2298 * copied when 'bin is set and restored when 'bin' is reset.
2299 */
2300 if (newval)
2301 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 if (!(opt_flags & OPT_GLOBAL))
2305 {
2306 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2307 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2308 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2309 curbuf->b_p_et_nobin = curbuf->b_p_et;
2310 }
2311 if (!(opt_flags & OPT_LOCAL))
2312 {
2313 p_tw_nobin = p_tw;
2314 p_wm_nobin = p_wm;
2315 p_ml_nobin = p_ml;
2316 p_et_nobin = p_et;
2317 }
2318 }
2319
2320 if (!(opt_flags & OPT_GLOBAL))
2321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 curbuf->b_p_tw = 0; // no automatic line wrap
2323 curbuf->b_p_wm = 0; // no automatic line wrap
2324 curbuf->b_p_ml = 0; // no modelines
2325 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 if (!(opt_flags & OPT_LOCAL))
2328 {
2329 p_tw = 0;
2330 p_wm = 0;
2331 p_ml = FALSE;
2332 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002333 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002336 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 if (!(opt_flags & OPT_GLOBAL))
2339 {
2340 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2341 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2342 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2343 curbuf->b_p_et = curbuf->b_p_et_nobin;
2344 }
2345 if (!(opt_flags & OPT_LOCAL))
2346 {
2347 p_tw = p_tw_nobin;
2348 p_wm = p_wm_nobin;
2349 p_ml = p_ml_nobin;
2350 p_et = p_et_nobin;
2351 }
2352 }
2353}
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355/*
2356 * Expand environment variables for some string options.
2357 * These string options cannot be indirect!
2358 * If "val" is NULL expand the current value of the option.
2359 * Return pointer to NameBuff, or NULL when not expanded.
2360 */
2361 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002362option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002364 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2366 return NULL;
2367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002368 // If val is longer than MAXPATHL no meaningful expansion can be done,
2369 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 if (val != NULL && STRLEN(val) > MAXPATHL)
2371 return NULL;
2372
2373 if (val == NULL)
2374 val = *(char_u **)options[opt_idx].var;
2375
2376 /*
2377 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2378 * Escape spaces when expanding 'tags', they are used to separate file
2379 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002380 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 */
2382 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002383 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002384#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002385 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2386#endif
2387 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return NULL;
2390
2391 return NameBuff;
2392}
2393
2394/*
2395 * After setting various option values: recompute variables that depend on
2396 * option values.
2397 */
2398 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002399didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002401 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 (void)init_chartab();
2403
Bram Moolenaardac13472019-09-16 21:06:21 +02002404 didset_string_options();
2405
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002406#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002407 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002408 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002409 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002410 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002413 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 (void)check_cedit();
2415#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002416#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002418 fill_breakat_flags();
2419#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002420 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002421}
2422
2423/*
2424 * More side effects of setting options.
2425 */
2426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002427didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002428{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002429 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002430 (void)highlight_changed();
2431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002432 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002433 check_opt_wim();
2434
Bram Moolenaareed9d462021-02-15 20:38:25 +01002435 // Parse default for 'listchars'.
2436 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002439 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002440
2441#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002442 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002443 (void)check_clipboard_option();
2444#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002445#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002446 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002447 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002448 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002449 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451}
2452
2453/*
2454 * Check for string options that are NULL (normally only termcap options).
2455 */
2456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002457check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 int opt_idx;
2460
2461 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2462 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2463 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2464}
2465
2466/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002467 * Return the option index found by a pointer into term_strings[].
2468 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002470 int
2471get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002473 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2476 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002477 return opt_idx;
2478 return -1; // cannot happen: didn't find it!
2479}
2480
2481/*
2482 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2483 * Return the option index or -1 if not found.
2484 */
2485 int
2486set_term_option_alloced(char_u **p)
2487{
2488 int opt_idx = get_term_opt_idx(p);
2489
2490 if (opt_idx >= 0)
2491 options[opt_idx].flags |= P_ALLOCED;
2492 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493}
2494
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002495#if defined(FEAT_EVAL) || defined(PROTO)
2496/*
2497 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2498 * Return FALSE when it wasn't.
2499 * Return -1 for an unknown option.
2500 */
2501 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002502was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002503{
2504 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002505 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002506
2507 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002508 {
2509 flagp = insecure_flag(idx, opt_flags);
2510 return (*flagp & P_INSECURE) != 0;
2511 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002512 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002513 return -1;
2514}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515
2516/*
2517 * Get a pointer to the flags used for the P_INSECURE flag of option
2518 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002519 * NOTE: Caller must make sure that "curwin" is set to the window from which
2520 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002521 */
2522 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002523insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002524{
2525 if (opt_flags & OPT_LOCAL)
2526 switch ((int)options[opt_idx].indir)
2527 {
2528#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002529 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002530#endif
2531#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002532# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 case PV_FDE: return &curwin->w_p_fde_flags;
2534 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002535# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002536# ifdef FEAT_BEVAL
2537 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2538# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002539# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002540 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002542 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002543# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002544 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545# endif
2546#endif
2547 }
2548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002549 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002550 return &options[opt_idx].flags;
2551}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002552#endif
2553
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002554/*
2555 * Redraw the window title and/or tab page text later.
2556 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002557void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002558{
2559 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002560 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002561}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002564 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2565 * characters or characters in "allowed".
2566 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002567 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002568valid_name(char_u *val, char *allowed)
2569{
2570 char_u *s;
2571
2572 for (s = val; *s != NUL; ++s)
2573 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2574 return FALSE;
2575 return TRUE;
2576}
2577
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002578#if defined(FEAT_EVAL) || defined(PROTO)
2579/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002580 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002581 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002582 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002583 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002584set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002585{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002586 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2587 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002588 sctx_T new_script_ctx = script_ctx;
2589
Bram Moolenaar51258742020-05-03 17:19:33 +02002590 // Modeline already has the line number set.
2591 if (!(opt_flags & OPT_MODELINE))
2592 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002594 // Remember where the option was set. For local options need to do that
2595 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002596 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002597 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002598 if (both || (opt_flags & OPT_LOCAL))
2599 {
2600 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002601 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002602 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002603 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002604 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002605 if (both)
2606 // also setting the "all buffers" value
2607 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2608 new_script_ctx;
2609 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002610 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002611}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002612
2613/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002614 * Get the script context of global option "name".
2615 *
2616 */
2617 sctx_T *
2618get_option_sctx(char *name)
2619{
2620 int idx = findoption((char_u *)name);
2621
2622 if (idx >= 0)
2623 return &options[idx].script_ctx;
2624 siemsg("no such option: %s", name);
2625 return NULL;
2626}
2627
2628/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002629 * Set the script_ctx for a termcap option.
2630 * "name" must be the two character code, e.g. "RV".
2631 * When "name" is NULL use "opt_idx".
2632 */
2633 void
2634set_term_option_sctx_idx(char *name, int opt_idx)
2635{
2636 char_u buf[5];
2637 int idx;
2638
2639 if (name == NULL)
2640 idx = opt_idx;
2641 else
2642 {
2643 buf[0] = 't';
2644 buf[1] = '_';
2645 buf[2] = name[0];
2646 buf[3] = name[1];
2647 buf[4] = 0;
2648 idx = findoption(buf);
2649 }
2650 if (idx >= 0)
2651 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2652}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002653#endif
2654
Bram Moolenaarf4140482020-02-15 23:06:45 +01002655#if defined(FEAT_EVAL)
2656/*
2657 * Apply the OptionSet autocommand.
2658 */
2659 static void
2660apply_optionset_autocmd(
2661 int opt_idx,
2662 long opt_flags,
2663 long oldval,
2664 long oldval_g,
2665 long newval,
2666 char *errmsg)
2667{
2668 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2669
2670 // Don't do this while starting up, failure or recursively.
2671 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2672 return;
2673
2674 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2675 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2676 oldval_g);
2677 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2678 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2679 (opt_flags & OPT_LOCAL) ? "local" : "global");
2680 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2681 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2682 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2683 if (opt_flags & OPT_LOCAL)
2684 {
2685 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2686 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2687 }
2688 if (opt_flags & OPT_GLOBAL)
2689 {
2690 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2691 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2692 }
2693 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2694 {
2695 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2696 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2697 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2698 }
2699 if (opt_flags & OPT_MODELINE)
2700 {
2701 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2702 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2703 }
2704 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2705 NULL, FALSE, NULL);
2706 reset_v_option_vars();
2707}
2708#endif
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710/*
2711 * Set the value of a boolean option, and take care of side effects.
2712 * Returns NULL for success, or an error message for an error.
2713 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002714 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002715set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 int opt_idx, // index in options[] table
2717 char_u *varp, // pointer to the option variable
2718 int value, // new value
2719 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
2721 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002722#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002723 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if ((secure
2728#ifdef HAVE_SANDBOX
2729 || sandbox != 0
2730#endif
2731 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002732 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002734#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002735 // Save the global value before changing anything. This is needed as for
2736 // a global-only option setting the "local value" in fact sets the global
2737 // value (since there is only one value).
2738 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2739 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2740 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002741#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002746 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#endif
2748
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002749#ifdef FEAT_GUI
2750 need_mouse_correct = TRUE;
2751#endif
2752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002753 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2755 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2756
2757 /*
2758 * Handle side effects of changing a bool option.
2759 */
2760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaar920694c2016-08-21 17:45:02 +02002765#ifdef FEAT_LANGMAP
2766 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002768 p_lnr = !p_lrm;
2769 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002771 p_lrm = !p_lnr;
2772#endif
2773
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002774#ifdef FEAT_SYN_HL
2775 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2776 reset_cursorline();
2777#endif
2778
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002779#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002781 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2782 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 // Only take action when the option was set. When reset we do not
2784 // delete the undo file, the option may be set again without making
2785 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002786 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002787 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002788 char_u hash[UNDO_HASH_SIZE];
2789 buf_T *save_curbuf = curbuf;
2790
Bram Moolenaar29323592016-07-24 22:04:11 +02002791 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 // When 'undofile' is set globally: for every buffer, otherwise
2794 // only for the current buffer: Try to read in the undofile,
2795 // if one exists, the buffer wasn't changed and the buffer was
2796 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002797 if ((curbuf == save_curbuf
2798 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2799 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2800 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002801#ifdef FEAT_CRYPT
2802 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2803 continue;
2804#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002805 u_compute_hash(hash);
2806 u_read_undo(NULL, hash, curbuf->b_fname);
2807 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002808 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002809 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002810 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002811 }
2812#endif
2813
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 else if ((int *)varp == &curbuf->b_p_ro)
2815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2818 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002819
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002821 if (curbuf->b_p_ro)
2822 curbuf->b_did_warn = FALSE;
2823
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002824 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
2826
Bram Moolenaar9c449722010-07-20 18:44:27 +02002827#ifdef FEAT_GUI
2828 else if ((int *)varp == &p_mh)
2829 {
2830 if (!p_mh)
2831 gui_mch_mousehide(FALSE);
2832 }
2833#endif
2834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002837 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002838# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002840 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2841 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002842 {
2843 curbuf->b_p_ma = FALSE;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002844 return N_(e_cannot_make_terminal_with_running_job_modifiable);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002845 }
2846# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002847 redraw_titles();
2848 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002851 {
2852 redraw_titles();
2853 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002855 else if ((int *)varp == &curbuf->b_p_fixeol)
2856 {
2857 redraw_titles();
2858 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002860 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002861 {
2862 redraw_titles();
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 else if ((int *)varp == &curbuf->b_p_bin)
2867 {
2868 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002869 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2874 {
2875 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2876 NULL, NULL, TRUE, curbuf);
2877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 else if ((int *)varp == &curbuf->b_p_swf)
2881 {
2882 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2886 // buf->b_p_swf
2887 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002890 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 else if ((int *)varp == &p_terse)
2892 {
2893 char_u *p;
2894
2895 p = vim_strchr(p_shm, SHM_SEARCH);
2896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 if (p_terse && p == NULL)
2899 {
2900 STRCPY(IObuff, p_shm);
2901 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002902 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002906 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 else if ((int *)varp == &p_paste)
2911 {
2912 paste_option_changed();
2913 }
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 else if ((int *)varp == &p_im)
2917 {
2918 if (p_im)
2919 {
2920 if ((State & INSERT) == 0)
2921 need_start_insertmode = TRUE;
2922 stop_insert_mode = FALSE;
2923 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002925 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
2927 need_start_insertmode = FALSE;
2928 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002929 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 restart_edit = 0;
2932 }
2933 }
2934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 else if ((int *)varp == &p_ic && p_hls)
2937 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002938 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940
2941#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 else if ((int *)varp == &p_hls)
2944 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002945 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947#endif
2948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2950 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 else if ((int *)varp == &curwin->w_p_scb)
2952 {
2953 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002956 curwin->w_scbind_pos = curwin->w_topline;
2957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
Bram Moolenaar4033c552017-09-16 20:54:51 +02002960#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else if ((int *)varp == &curwin->w_p_pvw)
2963 {
2964 if (curwin->w_p_pvw)
2965 {
2966 win_T *win;
2967
Bram Moolenaar29323592016-07-24 22:04:11 +02002968 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 if (win->w_p_pvw && win != curwin)
2970 {
2971 curwin->w_p_pvw = FALSE;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002972 return N_(e_preview_window_already_exists);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974 }
2975 }
2976#endif
2977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 else if ((int *)varp == &curbuf->b_p_tx)
2980 {
2981 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2982 }
2983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 set_string_option_direct((char_u *)"ffs", -1,
2988 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002989 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
2992 /*
2993 * When 'lisp' option changes include/exclude '-' in
2994 * keyword characters.
2995 */
2996#ifdef FEAT_LISP
2997 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2998 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
3001#endif
3002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003004 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003006 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 else if ((int *)varp == &curbuf->b_changed)
3010 {
3011 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003013 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016
3017#ifdef BACKSLASH_IN_FILENAME
3018 else if ((int *)varp == &p_ssl)
3019 {
3020 if (p_ssl)
3021 {
3022 psepc = '/';
3023 psepcN = '\\';
3024 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026 else
3027 {
3028 psepc = '\\';
3029 psepcN = '/';
3030 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
3032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 buflist_slash_adjust();
3035 alist_slash_adjust();
3036# ifdef FEAT_EVAL
3037 scriptnames_slash_adjust();
3038# endif
3039 }
3040#endif
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else if ((int *)varp == &curwin->w_p_wrap)
3044 {
3045 if (curwin->w_p_wrap)
3046 curwin->w_leftcol = 0;
3047 }
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 else if ((int *)varp == &p_ea)
3050 {
3051 if (p_ea && !old_value)
3052 win_equal(curwin, FALSE, 0);
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 else if ((int *)varp == &p_wiv)
3056 {
3057 /*
3058 * When 'weirdinvert' changed, set/reset 't_xs'.
3059 * Then set 'weirdinvert' according to value of 't_xs'.
3060 */
3061 if (p_wiv && !old_value)
3062 T_XS = (char_u *)"y";
3063 else if (!p_wiv && old_value)
3064 T_XS = empty_option;
3065 p_wiv = (*T_XS != NUL);
3066 }
3067
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003068#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 else if ((int *)varp == &p_beval)
3070 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003071 if (!balloonEvalForTerm)
3072 {
3073 if (p_beval && !old_value)
3074 gui_mch_enable_beval_area(balloonEval);
3075 else if (!p_beval && old_value)
3076 gui_mch_disable_beval_area(balloonEval);
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003079#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003080#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003081 else if ((int *)varp == &p_bevalterm)
3082 {
3083 mch_bevalterm_changed();
3084 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003087#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else if ((int *)varp == &p_acd)
3089 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003091 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
3093#endif
3094
3095#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else if ((int *)varp == &curwin->w_p_diff)
3098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003100 diff_buf_adjust(curwin);
3101# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (foldmethodIsDiff(curwin))
3103 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106#endif
3107
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003108#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else if ((int *)varp == &p_imdisable)
3111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003112 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (p_imdisable)
3114 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003115 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003116 // When the option is set from an autocommand, it may need to take
3117 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003118 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
3120#endif
3121
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003122#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003124 else if ((int *)varp == &curwin->w_p_spell)
3125 {
3126 if (curwin->w_p_spell)
3127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 char *errmsg = did_set_spelllang(curwin);
3129
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003130 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003131 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003132 }
3133 }
3134#endif
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136#ifdef FEAT_ARABIC
3137 if ((int *)varp == &curwin->w_p_arab)
3138 {
3139 if (curwin->w_p_arab)
3140 {
3141 /*
3142 * 'arabic' is set, handle various sub-settings.
3143 */
3144 if (!p_tbidi)
3145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if (!curwin->w_p_rl)
3148 {
3149 curwin->w_p_rl = TRUE;
3150 changed_window_setting();
3151 }
3152
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003153 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (!p_arshape)
3155 {
3156 p_arshape = TRUE;
3157 redraw_later_clear();
3158 }
3159 }
3160
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003161 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003162 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003164 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003165 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3166
Bram Moolenaar8820b482017-03-16 17:23:31 +01003167 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003168 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003169#ifdef FEAT_EVAL
3170 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3171#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
3177# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3180 OPT_LOCAL);
3181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 else
3184 {
3185 /*
3186 * 'arabic' is reset, handle various sub-settings.
3187 */
3188 if (!p_tbidi)
3189 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (curwin->w_p_rl)
3192 {
3193 curwin->w_p_rl = FALSE;
3194 changed_window_setting();
3195 }
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // 'arabicshape' isn't reset, it is a global option and
3198 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003201 // 'delcombine' isn't reset, it is a global option and another
3202 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 curbuf->b_p_iminsert = B_IMODE_NONE;
3207 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3208# endif
3209 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003210 }
3211
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003212#endif
3213
Bram Moolenaar44826212019-08-22 21:23:20 +02003214#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3215 else if (((int *)varp == &curwin->w_p_nu
3216 || (int *)varp == &curwin->w_p_rnu)
3217 && gui.in_use
3218 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3219 && curbuf->b_signlist != NULL)
3220 {
3221 // If the 'number' or 'relativenumber' options are modified and
3222 // 'signcolumn' is set to 'number', then clear the screen for a full
3223 // refresh. Otherwise the sign icons are not displayed properly in the
3224 // number column. If the 'number' option is set and only the
3225 // 'relativenumber' option is toggled, then don't refresh the screen
3226 // (optimization).
3227 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3228 redraw_all_later(CLEAR);
3229 }
3230#endif
3231
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003232#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003234 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003235 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003236# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003237 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003238 if (
3239# ifdef VIMDLL
3240 !gui.in_use && !gui.starting &&
3241# endif
3242 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003243 {
3244 p_tgc = 0;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003245 return N_(e_24_bit_colors_are_not_supported_on_this_environment);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003246 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003247 if (is_term_win32())
3248 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003249# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003250# ifdef FEAT_GUI
3251 if (!gui.in_use && !gui.starting)
3252# endif
3253 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003254# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003255 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003256 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003257 {
3258 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003259 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003260 init_highlight(TRUE, FALSE);
3261 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003262# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003263# ifdef FEAT_TERMINAL
3264 term_update_colors_all();
3265 term_update_wincolor_all();
3266# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003267 }
3268#endif
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 /*
3271 * End of handling side effects for bool options.
3272 */
3273
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003274 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 options[opt_idx].flags |= P_WAS_SET;
3277
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003278#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003279 apply_optionset_autocmd(opt_idx, opt_flags,
3280 (long)(old_value ? TRUE : FALSE),
3281 (long)(old_global_value ? TRUE : FALSE),
3282 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003283#endif
3284
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003285 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003286 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003287 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003288 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003289
3290 if ((opt_flags & OPT_NO_REDRAW) == 0)
3291 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 return NULL;
3294}
3295
3296/*
3297 * Set the value of a number option, and take care of side effects.
3298 * Returns NULL for success, or an error message for an error.
3299 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003300 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003301set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003302 int opt_idx, // index in options[] table
3303 char_u *varp, // pointer to the option variable
3304 long value, // new value
3305 char *errbuf, // buffer for error messages
3306 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003307 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3308 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003310 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003312#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003313 long old_global_value = 0; // only used when setting a local and
3314 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003315#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003316 long old_Rows = Rows; // remember old Rows
3317 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 long *pp = (long *)varp;
3319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003320 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003321 if ((secure
3322#ifdef HAVE_SANDBOX
3323 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003325 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003326 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003328#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003329 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003330 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003331 // value (since there is only one value).
3332 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003333 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3334 OPT_GLOBAL);
3335#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 *pp = value;
3338#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003340 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003342#ifdef FEAT_GUI
3343 need_mouse_correct = TRUE;
3344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
Bram Moolenaar14f24742012-08-08 18:01:05 +02003346 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003348 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003349#ifdef FEAT_VARTABS
3350 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3351 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3352 ? tabstop_first(curbuf->b_p_vts_array)
3353 : curbuf->b_p_ts;
3354#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358
3359 /*
3360 * Number options that need some action when changed
3361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (pp == &p_wh || pp == &p_hh)
3363 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003364 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (p_wh < 1)
3366 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003367 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 p_wh = 1;
3369 }
3370 if (p_wmh > p_wh)
3371 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003372 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 p_wh = p_wmh;
3374 }
3375 if (p_hh < 0)
3376 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003377 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 p_hh = 0;
3379 }
3380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003381 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003382 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 if (pp == &p_wh && curwin->w_height < p_wh)
3385 win_setheight((int)p_wh);
3386 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3387 win_setheight((int)p_hh);
3388 }
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 else if (pp == &p_wmh)
3391 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003392 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if (p_wmh < 0)
3394 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003395 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 p_wmh = 0;
3397 }
3398 if (p_wmh > p_wh)
3399 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003400 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 p_wmh = p_wh;
3402 }
3403 win_setminheight();
3404 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003405 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003407 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (p_wiw < 1)
3409 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003410 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 p_wiw = 1;
3412 }
3413 if (p_wmw > p_wiw)
3414 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003415 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 p_wiw = p_wmw;
3417 }
3418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003420 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 win_setwidth((int)p_wiw);
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else if (pp == &p_wmw)
3424 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003425 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (p_wmw < 0)
3427 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003428 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 p_wmw = 0;
3430 }
3431 if (p_wmw > p_wiw)
3432 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003433 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 p_wmw = p_wiw;
3435 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003436 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 else if (pp == &p_ls)
3441 {
3442 last_status(FALSE);
3443 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003445 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003446 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451#ifdef FEAT_GUI
3452 else if (pp == &p_linespace)
3453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // Recompute gui.char_height and resize the Vim window to keep the
3455 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003456 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003457 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459#endif
3460
3461#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003462 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 else if (pp == &curwin->w_p_fdl)
3464 {
3465 if (curwin->w_p_fdl < 0)
3466 curwin->w_p_fdl = 0;
3467 newFoldLevel();
3468 }
3469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003470 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else if (pp == &curwin->w_p_fml)
3472 {
3473 foldUpdateAll(curwin);
3474 }
3475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003476 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else if (pp == &curwin->w_p_fdn)
3478 {
3479 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3480 foldUpdateAll(curwin);
3481 }
3482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003483 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else if (pp == &curwin->w_p_fdc)
3485 {
3486 if (curwin->w_p_fdc < 0)
3487 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003488 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 curwin->w_p_fdc = 0;
3490 }
3491 else if (curwin->w_p_fdc > 12)
3492 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003493 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 curwin->w_p_fdc = 12;
3495 }
3496 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003497#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003499#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3502 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003503# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (foldmethodIsIndent(curwin))
3505 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003506# endif
3507# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003508 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3509 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003510 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3511 parse_cino(curbuf);
3512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003516 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003517 else if (pp == &p_mco)
3518 {
3519 if (p_mco > MAX_MCO)
3520 p_mco = MAX_MCO;
3521 else if (p_mco < 0)
3522 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003523 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003524 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 else if (pp == &curbuf->b_p_iminsert)
3527 {
3528 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3529 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003530 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 curbuf->b_p_iminsert = B_IMODE_NONE;
3532 }
3533 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003534 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003536#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003537 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 status_redraw_curbuf();
3539#endif
3540 }
3541
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003542#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003543 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003544 else if (pp == &p_imst)
3545 {
3546 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003547 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003548 }
3549#endif
3550
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003551 else if (pp == &p_window)
3552 {
3553 if (p_window < 1)
3554 p_window = 1;
3555 else if (p_window >= Rows)
3556 p_window = Rows - 1;
3557 }
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 else if (pp == &curbuf->b_p_imsearch)
3560 {
3561 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3562 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003563 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 curbuf->b_p_imsearch = B_IMODE_NONE;
3565 }
3566 p_imsearch = curbuf->b_p_imsearch;
3567 }
3568
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003569 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 else if (pp == &p_titlelen)
3571 {
3572 if (p_titlelen < 0)
3573 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003574 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 p_titlelen = 85;
3576 }
3577 if (starting != NO_SCREEN && old_value != p_titlelen)
3578 need_maketitle = TRUE;
3579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003581 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 else if (pp == &p_ch)
3583 {
3584 if (p_ch < 1)
3585 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003586 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 p_ch = 1;
3588 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003589 if (p_ch > Rows - min_rows() + 1)
3590 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003592 // Only compute the new window layout when startup has been
3593 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (p_ch != old_value && full_screen
3595#ifdef FEAT_GUI
3596 && !gui.starting
3597#endif
3598 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003599 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003602 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 else if (pp == &p_uc)
3604 {
3605 if (p_uc < 0)
3606 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003607 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 p_uc = 100;
3609 }
3610 if (p_uc && !old_value)
3611 ml_open_files();
3612 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003613#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003614 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003615 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003616 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003618 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003619 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003620 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003621 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003622 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003623 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003624 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003625 }
3626 }
3627#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003628#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003629 else if (pp == &p_mzq)
3630 mzvim_reset_timer();
3631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003633#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003634 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003635 else if (pp == &p_pyx)
3636 {
3637 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003638 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003639 }
3640#endif
3641
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003642 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 else if (pp == &p_ul)
3644 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003645 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003647 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 p_ul = value;
3649 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003650 else if (pp == &curbuf->b_p_ul)
3651 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003652 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003653 curbuf->b_p_ul = old_value;
3654 u_sync(TRUE);
3655 curbuf->b_p_ul = value;
3656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003658#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003659 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003660 else if (pp == &curwin->w_p_nuw)
3661 {
3662 if (curwin->w_p_nuw < 1)
3663 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003664 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003665 curwin->w_p_nuw = 1;
3666 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003667 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003668 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003669 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003670 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003671 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003672 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003673 }
3674#endif
3675
Bram Moolenaar1a384422010-07-14 19:53:30 +02003676 else if (pp == &curbuf->b_p_tw)
3677 {
3678 if (curbuf->b_p_tw < 0)
3679 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003680 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003681 curbuf->b_p_tw = 0;
3682 }
3683#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003684 {
3685 win_T *wp;
3686 tabpage_T *tp;
3687
3688 FOR_ALL_TAB_WINDOWS(tp, wp)
3689 check_colorcolumn(wp);
3690 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003691#endif
3692 }
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 /*
3695 * Check the bounds for numeric options here
3696 */
3697 if (Rows < min_rows() && full_screen)
3698 {
3699 if (errbuf != NULL)
3700 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003701 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003702 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 errmsg = errbuf;
3704 }
3705 Rows = min_rows();
3706 }
3707 if (Columns < MIN_COLUMNS && full_screen)
3708 {
3709 if (errbuf != NULL)
3710 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003711 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003712 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 errmsg = errbuf;
3714 }
3715 Columns = MIN_COLUMNS;
3716 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003717 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 /*
3720 * If the screen (shell) height has been changed, assume it is the
3721 * physical screenheight.
3722 */
3723 if (old_Rows != Rows || old_Columns != Columns)
3724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003725 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (updating_screen)
3727 *pp = old_value;
3728 else if (full_screen
3729#ifdef FEAT_GUI
3730 && !gui.starting
3731#endif
3732 )
3733 set_shellsize((int)Columns, (int)Rows, TRUE);
3734 else
3735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003736 // Postpone the resizing; check the size and cmdline position for
3737 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 check_shellsize();
3739 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3740 cmdline_row = Rows - p_ch;
3741 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003742 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003743 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (curbuf->b_p_ts <= 0)
3747 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003748 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 curbuf->b_p_ts = 8;
3750 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003751 else if (curbuf->b_p_ts > TABSTOP_MAX)
3752 {
3753 errmsg = e_invalid_argument;
3754 curbuf->b_p_ts = 8;
3755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (p_tm < 0)
3757 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003758 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 p_tm = 0;
3760 }
3761 if ((curwin->w_p_scr <= 0
3762 || (curwin->w_p_scr > curwin->w_height
3763 && curwin->w_height > 0))
3764 && full_screen)
3765 {
3766 if (pp == &(curwin->w_p_scr))
3767 {
3768 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003769 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 win_comp_scroll(curwin);
3771 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003772 // If 'scroll' became invalid because of a side effect silently adjust
3773 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 else if (curwin->w_p_scr <= 0)
3775 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003776 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 curwin->w_p_scr = curwin->w_height;
3778 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003779 if (p_hi < 0)
3780 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003781 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003782 p_hi = 0;
3783 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003784 else if (p_hi > 10000)
3785 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003786 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003787 p_hi = 10000;
3788 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003789 if (p_re < 0 || p_re > 2)
3790 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003791 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003792 p_re = 0;
3793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (p_report < 0)
3795 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003796 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 p_report = 1;
3798 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003799 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003801 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 p_sj = Rows / 2;
3803 else
3804 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003805 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 p_sj = 1;
3807 }
3808 }
3809 if (p_so < 0 && full_screen)
3810 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003811 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 p_so = 0;
3813 }
3814 if (p_siso < 0 && full_screen)
3815 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003816 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 p_siso = 0;
3818 }
3819#ifdef FEAT_CMDWIN
3820 if (p_cwh < 1)
3821 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003822 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 p_cwh = 1;
3824 }
3825#endif
3826 if (p_ut < 0)
3827 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003828 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 p_ut = 2000;
3830 }
3831 if (p_ss < 0)
3832 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003833 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 p_ss = 0;
3835 }
3836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003837 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3839 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3840
3841 options[opt_idx].flags |= P_WAS_SET;
3842
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003843#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003844 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3845 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003846#endif
3847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003849 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003850 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003851 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003852 if ((opt_flags & OPT_NO_REDRAW) == 0)
3853 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 return errmsg;
3856}
3857
3858/*
3859 * Called after an option changed: check if something needs to be redrawn.
3860 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003862check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003864 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003865 int doclear = (flags & P_RCLR) == P_RCLR;
3866 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003868 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
3871 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3872 changed_window_setting();
3873 if (flags & P_RBUF)
3874 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003875 if (flags & P_RWINONLY)
3876 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003877 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 redraw_all_later(CLEAR);
3879 else if (all)
3880 redraw_all_later(NOT_VALID);
3881}
3882
3883/*
3884 * Find index for option 'arg'.
3885 * Return -1 if not found.
3886 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003887 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003888findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890 int opt_idx;
3891 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003892 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 int is_term_opt;
3894
3895 /*
3896 * For first call: Initialize the quick-access table.
3897 * It contains the index for the first option that starts with a certain
3898 * letter. There are 26 letters, plus the first "t_" option.
3899 */
3900 if (quick_tab[1] == 0)
3901 {
3902 p = options[0].fullname;
3903 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3904 {
3905 if (s[0] != p[0])
3906 {
3907 if (s[0] == 't' && s[1] == '_')
3908 quick_tab[26] = opt_idx;
3909 else
3910 quick_tab[CharOrdLow(s[0])] = opt_idx;
3911 }
3912 p = s;
3913 }
3914 }
3915
3916 /*
3917 * Check for name starting with an illegal character.
3918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (arg[0] < 'a' || arg[0] > 'z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 return -1;
3921
3922 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3923 if (is_term_opt)
3924 opt_idx = quick_tab[26];
3925 else
3926 opt_idx = quick_tab[CharOrdLow(arg[0])];
3927 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3928 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003929 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 break;
3931 }
3932 if (s == NULL && !is_term_opt)
3933 {
3934 opt_idx = quick_tab[CharOrdLow(arg[0])];
3935 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3936 {
3937 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003938 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 break;
3940 s = NULL;
3941 }
3942 }
3943 if (s == NULL)
3944 opt_idx = -1;
3945 return opt_idx;
3946}
3947
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003948#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949/*
3950 * Get the value for an option.
3951 *
3952 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003953 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003954 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003955 * String option: gov_string, *stringval gets allocated string.
3956 * Hidden Number option: gov_hidden_number.
3957 * Hidden Toggle option: gov_hidden_bool.
3958 * Hidden String option: gov_hidden_string.
3959 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003960 *
3961 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003963 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003964get_option_value(
3965 char_u *name,
3966 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003967 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003968 int *flagsp,
3969 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
3971 int opt_idx;
3972 char_u *varp;
3973
3974 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003975 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003976 {
3977 int key;
3978
3979 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003980 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003981 {
3982 char_u key_name[2];
3983 char_u *p;
3984
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003985 if (flagsp != NULL)
3986 *flagsp = 0; // terminal option has no flags
3987
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003988 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003989 if (key < 0)
3990 {
3991 key_name[0] = KEY2TERMCAP0(key);
3992 key_name[1] = KEY2TERMCAP1(key);
3993 }
3994 else
3995 {
3996 key_name[0] = KS_KEY;
3997 key_name[1] = (key & 0xff);
3998 }
3999 p = find_termcode(key_name);
4000 if (p != NULL)
4001 {
4002 if (stringval != NULL)
4003 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004004 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004005 }
4006 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004007 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004010 varp = get_varp_scope(&(options[opt_idx]), scope);
4011
4012 if (flagsp != NULL)
4013 // Return the P_xxxx option flags.
4014 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 if (options[opt_idx].flags & P_STRING)
4017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004019 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 if (stringval != NULL)
4021 {
4022#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004023 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004024 if ((char_u **)varp == &curbuf->b_p_key
4025 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 *stringval = vim_strsave((char_u *)"*****");
4027 else
4028#endif
4029 *stringval = vim_strsave(*(char_u **)(varp));
4030 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004031 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
4033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004034 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004035 return (options[opt_idx].flags & P_NUM)
4036 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 if (options[opt_idx].flags & P_NUM)
4038 *numval = *(long *)varp;
4039 else
4040 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004041 // Special case: 'modified' is b_changed, but we also want to consider
4042 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if ((int *)varp == &curbuf->b_changed)
4044 *numval = curbufIsChanged();
4045 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004046 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004048 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049}
4050#endif
4051
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004052#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004053/*
4054 * Returns the option attributes and its value. Unlike the above function it
4055 * will return either global value or local value of the option depending on
4056 * what was requested, but it will never return global value if it was
4057 * requested to return local one and vice versa. Neither it will return
4058 * buffer-local value if it was requested to return window-local one.
4059 *
4060 * Pretends that option is absent if it is not present in the requested scope
4061 * (i.e. has no global, window-local or buffer-local value depending on
4062 * opt_type). Uses
4063 *
4064 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004065 * 0 hidden or unknown option, also option that does not have requested
4066 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004067 * see SOPT_* in vim.h for other flags
4068 *
4069 * Possible opt_type values: see SREQ_* in vim.h
4070 */
4071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004072get_option_value_strict(
4073 char_u *name,
4074 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004075 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004076 int opt_type,
4077 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004078{
4079 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004080 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004081 struct vimoption *p;
4082 int r = 0;
4083
4084 opt_idx = findoption(name);
4085 if (opt_idx < 0)
4086 return 0;
4087
4088 p = &(options[opt_idx]);
4089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004090 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004091 if (p->var == NULL)
4092 return 0;
4093
4094 if (p->flags & P_BOOL)
4095 r |= SOPT_BOOL;
4096 else if (p->flags & P_NUM)
4097 r |= SOPT_NUM;
4098 else if (p->flags & P_STRING)
4099 r |= SOPT_STRING;
4100
4101 if (p->indir == PV_NONE)
4102 {
4103 if (opt_type == SREQ_GLOBAL)
4104 r |= SOPT_GLOBAL;
4105 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004106 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004107 }
4108 else
4109 {
4110 if (p->indir & PV_BOTH)
4111 r |= SOPT_GLOBAL;
4112 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004113 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004114
4115 if (p->indir & PV_WIN)
4116 {
4117 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004118 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004119 else
4120 r |= SOPT_WIN;
4121 }
4122 else if (p->indir & PV_BUF)
4123 {
4124 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004125 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004126 else
4127 r |= SOPT_BUF;
4128 }
4129 }
4130
4131 if (stringval == NULL)
4132 return r;
4133
4134 if (opt_type == SREQ_GLOBAL)
4135 varp = p->var;
4136 else
4137 {
4138 if (opt_type == SREQ_BUF)
4139 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004140 // Special case: 'modified' is b_changed, but we also want to
4141 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004142 if (p->indir == PV_MOD)
4143 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004144 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004145 varp = NULL;
4146 }
4147#ifdef FEAT_CRYPT
4148 else if (p->indir == PV_KEY)
4149 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004150 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004151 *stringval = NULL;
4152 varp = NULL;
4153 }
4154#endif
4155 else
4156 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004157 buf_T *save_curbuf = curbuf;
4158
4159 // only getting a pointer, no need to use aucmd_prepbuf()
4160 curbuf = (buf_T *)from;
4161 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004162 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004163 curbuf = save_curbuf;
4164 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004165 }
4166 }
4167 else if (opt_type == SREQ_WIN)
4168 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004169 win_T *save_curwin = curwin;
4170
4171 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004172 curbuf = curwin->w_buffer;
4173 varp = get_varp(p);
4174 curwin = save_curwin;
4175 curbuf = curwin->w_buffer;
4176 }
4177 if (varp == p->var)
4178 return (r | SOPT_UNSET);
4179 }
4180
4181 if (varp != NULL)
4182 {
4183 if (p->flags & P_STRING)
4184 *stringval = vim_strsave(*(char_u **)(varp));
4185 else if (p->flags & P_NUM)
4186 *numval = *(long *) varp;
4187 else
4188 *numval = *(int *)varp;
4189 }
4190
4191 return r;
4192}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004193
4194/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004195 * Iterate over options. First argument is a pointer to a pointer to a
4196 * structure inside options[] array, second is option type like in the above
4197 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004198 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004199 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004200 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004201 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004202 * first argument to NULL.
4203 *
4204 * Returns full option name for current option on each call.
4205 */
4206 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004207option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004208{
4209 struct vimoption *ret = NULL;
4210 do
4211 {
4212 if (*option == NULL)
4213 *option = (void *) options;
4214 else if (((struct vimoption *) (*option))->fullname == NULL)
4215 {
4216 *option = NULL;
4217 return NULL;
4218 }
4219 else
4220 *option = (void *) (((struct vimoption *) (*option)) + 1);
4221
4222 ret = ((struct vimoption *) (*option));
4223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004224 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004225 if (ret->var == NULL)
4226 {
4227 ret = NULL;
4228 continue;
4229 }
4230
4231 switch (opt_type)
4232 {
4233 case SREQ_GLOBAL:
4234 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4235 ret = NULL;
4236 break;
4237 case SREQ_BUF:
4238 if (!(ret->indir & PV_BUF))
4239 ret = NULL;
4240 break;
4241 case SREQ_WIN:
4242 if (!(ret->indir & PV_WIN))
4243 ret = NULL;
4244 break;
4245 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004246 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004247 return NULL;
4248 }
4249 }
4250 while (ret == NULL);
4251
4252 return (char_u *)ret->fullname;
4253}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004254#endif
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004257 * Return the flags for the option at 'opt_idx'.
4258 */
4259 long_u
4260get_option_flags(int opt_idx)
4261{
4262 return options[opt_idx].flags;
4263}
4264
4265/*
4266 * Set a flag for the option at 'opt_idx'.
4267 */
4268 void
4269set_option_flag(int opt_idx, long_u flag)
4270{
4271 options[opt_idx].flags |= flag;
4272}
4273
4274/*
4275 * Clear a flag for the option at 'opt_idx'.
4276 */
4277 void
4278clear_option_flag(int opt_idx, long_u flag)
4279{
4280 options[opt_idx].flags &= ~flag;
4281}
4282
4283/*
4284 * Returns TRUE if the option at 'opt_idx' is a global option
4285 */
4286 int
4287is_global_option(int opt_idx)
4288{
4289 return options[opt_idx].indir == PV_NONE;
4290}
4291
4292/*
4293 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4294 * local value.
4295 */
4296 int
4297is_global_local_option(int opt_idx)
4298{
4299 return options[opt_idx].indir & PV_BOTH;
4300}
4301
4302/*
4303 * Returns TRUE if the option at 'opt_idx' is a window-local option
4304 */
4305 int
4306is_window_local_option(int opt_idx)
4307{
4308 return options[opt_idx].var == VAR_WIN;
4309}
4310
4311/*
4312 * Returns TRUE if the option at 'opt_idx' is a hidden option
4313 */
4314 int
4315is_hidden_option(int opt_idx)
4316{
4317 return options[opt_idx].var == NULL;
4318}
4319
4320#if defined(FEAT_CRYPT) || defined(PROTO)
4321/*
4322 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4323 */
4324 int
4325is_crypt_key_option(int opt_idx)
4326{
4327 return options[opt_idx].indir == PV_KEY;
4328}
4329#endif
4330
4331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 * Set the value of option "name".
4333 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004334 *
4335 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004337 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004338set_option_value(
4339 char_u *name,
4340 long number,
4341 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004342 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343{
4344 int opt_idx;
4345 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004346 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004349 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004350 {
4351 int key;
4352
4353 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004354 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004355 {
4356 char_u key_name[2];
4357
4358 if (key < 0)
4359 {
4360 key_name[0] = KEY2TERMCAP0(key);
4361 key_name[1] = KEY2TERMCAP1(key);
4362 }
4363 else
4364 {
4365 key_name[0] = KS_KEY;
4366 key_name[1] = (key & 0xff);
4367 }
4368 add_termcode(key_name, string, FALSE);
4369 if (full_screen)
4370 ttest(FALSE);
4371 redraw_all_later(CLEAR);
4372 return NULL;
4373 }
4374
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004375 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
4378 {
4379 flags = options[opt_idx].flags;
4380#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004381 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004383 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004384 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004385 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004388 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004389 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 else
4391 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004392 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004393 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004395 if (number == 0 && string != NULL)
4396 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004397 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004399 // Either we are given a string or we are setting option
4400 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004401 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004402 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004403 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004405 // There's another character after zeros or the string
4406 // is empty. In both cases, we are trying to set a
4407 // num option using a string.
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00004408 semsg(_(e_number_required_after_str_equal_str),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004409 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004410 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004411
4412 }
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004415 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004416 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004418 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004419 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421 }
4422 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004423 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424}
4425
4426/*
4427 * Get the terminal code for a terminal option.
4428 * Returns NULL when not found.
4429 */
4430 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004431get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432{
4433 int opt_idx;
4434 char_u *varp;
4435
4436 if (tname[0] != 't' || tname[1] != '_' ||
4437 tname[2] == NUL || tname[3] == NUL)
4438 return NULL;
4439 if ((opt_idx = findoption(tname)) >= 0)
4440 {
4441 varp = get_varp(&(options[opt_idx]));
4442 if (varp != NULL)
4443 varp = *(char_u **)(varp);
4444 return varp;
4445 }
4446 return find_termcode(tname + 2);
4447}
4448
4449 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004450get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int i;
4453
4454 i = findoption((char_u *)"hl");
4455 if (i >= 0)
4456 return options[i].def_val[VI_DEFAULT];
4457 return (char_u *)NULL;
4458}
4459
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004460 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004461get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004462{
4463 int i;
4464
4465 i = findoption((char_u *)"enc");
4466 if (i >= 0)
4467 return options[i].def_val[VI_DEFAULT];
4468 return (char_u *)NULL;
4469}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471/*
4472 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004473 * When "has_lt" is true there is a '<' before "*arg_arg".
4474 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 */
4476 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004477find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004479 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004481 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
4483 /*
4484 * Don't use get_special_key_code() for t_xx, we don't want it to call
4485 * add_termcap_entry().
4486 */
4487 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4488 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004489 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004491 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004493 key = find_special_key(&arg, &modifiers,
4494 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004495 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 key = 0;
4497 }
4498 return key;
4499}
4500
4501/*
4502 * if 'all' == 0: show changed options
4503 * if 'all' == 1: show all normal options
4504 * if 'all' == 2: show all terminal options
4505 */
4506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004507showoptions(
4508 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004509 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511 struct vimoption *p;
4512 int col;
4513 int isterm;
4514 char_u *varp;
4515 struct vimoption **items;
4516 int item_count;
4517 int run;
4518 int row, rows;
4519 int cols;
4520 int i;
4521 int len;
4522
4523#define INC 20
4524#define GAP 3
4525
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004526 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (items == NULL)
4528 return;
4529
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004530 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004532 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004534 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004536 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004538 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004541 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 * 1. display the short items
4543 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004544 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
4546 for (run = 1; run <= 2 && !got_int; ++run)
4547 {
4548 /*
4549 * collect the items in items[]
4550 */
4551 item_count = 0;
4552 for (p = &options[0]; p->fullname != NULL; p++)
4553 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004554 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004555 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004556 continue;
4557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 varp = NULL;
4559 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004560 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
4562 if (p->indir != PV_NONE && !isterm)
4563 varp = get_varp_scope(p, opt_flags);
4564 }
4565 else
4566 varp = get_varp(p);
4567 if (varp != NULL
4568 && ((all == 2 && isterm)
4569 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004570 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004572 if (opt_flags & OPT_ONECOLUMN)
4573 len = Columns;
4574 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004575 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
4577 {
4578 option_value2string(p, opt_flags);
4579 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4580 }
4581 if ((len <= INC - GAP && run == 1) ||
4582 (len > INC - GAP && run == 2))
4583 items[item_count++] = p;
4584 }
4585 }
4586
4587 /*
4588 * display the items
4589 */
4590 if (run == 1)
4591 {
4592 cols = (Columns + GAP - 3) / INC;
4593 if (cols == 0)
4594 cols = 1;
4595 rows = (item_count + cols - 1) / cols;
4596 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004597 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 rows = item_count;
4599 for (row = 0; row < rows && !got_int; ++row)
4600 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004601 msg_putchar('\n'); // go to next line
4602 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 break;
4604 col = 0;
4605 for (i = row; i < item_count; i += rows)
4606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004607 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 showoneopt(items[i], opt_flags);
4609 col += INC;
4610 }
4611 out_flush();
4612 ui_breakcheck();
4613 }
4614 }
4615 vim_free(items);
4616}
4617
4618/*
4619 * Return TRUE if option "p" has its default value.
4620 */
4621 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004622optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 int dvi;
4625
4626 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004627 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004628 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004630 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004632 // the cast to long is required for Manx C, long_i is
4633 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004634 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004635 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4637}
4638
4639/*
4640 * showoneopt: show the value of one option
4641 * must not be called with a hidden option!
4642 */
4643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004644showoneopt(
4645 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004646 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004648 char_u *varp;
4649 int save_silent = silent_mode;
4650
4651 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004652 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 varp = get_varp_scope(p, opt_flags);
4655
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004656 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4658 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004659 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004661 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004663 msg_puts(" ");
4664 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (!(p->flags & P_BOOL))
4666 {
4667 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004668 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 option_value2string(p, opt_flags);
4670 msg_outtrans(NameBuff);
4671 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004672
4673 silent_mode = save_silent;
4674 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675}
4676
4677/*
4678 * Write modified options as ":set" commands to a file.
4679 *
4680 * There are three values for "opt_flags":
4681 * OPT_GLOBAL: Write global option values and fresh values of
4682 * buffer-local options (used for start of a session
4683 * file).
4684 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4685 * curwin (used for a vimrc file).
4686 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4687 * and local values for window-local options of
4688 * curwin. Local values are also written when at the
4689 * default value, because a modeline or autocommand
4690 * may have set them when doing ":edit file" and the
4691 * user has set them back at the default or fresh
4692 * value.
4693 * When "local_only" is TRUE, don't write fresh
4694 * values, only local values (for ":mkview").
4695 * (fresh value = value used for a new buffer or window for a local option).
4696 *
4697 * Return FAIL on error, OK otherwise.
4698 */
4699 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004700makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004703 char_u *varp; // currently used value
4704 char_u *varp_fresh; // local value
4705 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 char *cmd;
4707 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004708 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
4710 /*
4711 * The options that don't have a default (terminal name, columns, lines)
4712 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004713 * Do the loop over "options[]" twice: once for options with the
4714 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004716 for (pri = 1; pri >= 0; --pri)
4717 {
4718 for (p = &options[0]; !istermoption(p); p++)
4719 if (!(p->flags & P_NO_MKRC)
4720 && !istermoption(p)
4721 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004723 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4725 continue;
4726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004727 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4728 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4730 continue;
4731
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004732 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004734 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 continue;
4736
Bram Moolenaard23b7142021-04-17 21:04:34 +02004737 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4738 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004739 continue;
4740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 round = 2;
4742 if (p->indir != PV_NONE)
4743 {
4744 if (p->var == VAR_WIN)
4745 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004746 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (!(opt_flags & OPT_LOCAL))
4748 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004749 // When fresh value of window-local option is not at the
4750 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4752 {
4753 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004754 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 round = 1;
4757 varp_local = varp;
4758 varp = varp_fresh;
4759 }
4760 }
4761 }
4762 }
4763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004764 // Round 1: fresh value for window-local options.
4765 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 for ( ; round <= 2; varp = varp_local, ++round)
4767 {
4768 if (round == 1 || (opt_flags & OPT_GLOBAL))
4769 cmd = "set";
4770 else
4771 cmd = "setlocal";
4772
4773 if (p->flags & P_BOOL)
4774 {
4775 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4776 return FAIL;
4777 }
4778 else if (p->flags & P_NUM)
4779 {
4780 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4781 return FAIL;
4782 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004785 int do_endif = FALSE;
4786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Don't set 'syntax' and 'filetype' again if the value is
4788 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004789 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004790#if defined(FEAT_SYN_HL)
4791 p->indir == PV_SYN ||
4792#endif
4793 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
4795 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4796 *(char_u **)(varp)) < 0
4797 || put_eol(fd) < 0)
4798 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004799 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004802 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004804 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 if (put_line(fd, "endif") == FAIL)
4807 return FAIL;
4808 }
4809 }
4810 }
4811 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return OK;
4814}
4815
4816#if defined(FEAT_FOLDING) || defined(PROTO)
4817/*
4818 * Generate set commands for the local fold options only. Used when
4819 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4820 */
4821 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004822makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004824 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004826 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 == FAIL
4828# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004829 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004831 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 == FAIL
4833 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4834 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4835 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4836 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4837 )
4838 return FAIL;
4839
4840 return OK;
4841}
4842#endif
4843
4844 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004845put_setstring(
4846 FILE *fd,
4847 char *cmd,
4848 char *name,
4849 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004850 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
4852 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004853 char_u *buf = NULL;
4854 char_u *part = NULL;
4855 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
4857 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4858 return FAIL;
4859 if (*valuep != NULL)
4860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 // Output 'pastetoggle' as key names. For other
4862 // options some characters have to be escaped with
4863 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 if (valuep == &p_pt)
4865 {
4866 s = *valuep;
4867 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004868 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 return FAIL;
4870 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004871 // expand the option value, replace $HOME by ~
4872 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004874 int size = (int)STRLEN(*valuep) + 1;
4875
4876 // replace home directory in the whole option value into "buf"
4877 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004878 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004879 goto fail;
4880 home_replace(NULL, *valuep, buf, size, FALSE);
4881
4882 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004883 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004884 // can be expanded when read back.
4885 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4886 && vim_strchr(*valuep, ',') != NULL)
4887 {
4888 part = alloc(size);
4889 if (part == NULL)
4890 goto fail;
4891
4892 // write line break to clear the option, e.g. ':set rtp='
4893 if (put_eol(fd) == FAIL)
4894 goto fail;
4895
4896 p = buf;
4897 while (*p != NUL)
4898 {
4899 // for each comma separated option part, append value to
4900 // the option, :set rtp+=value
4901 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4902 goto fail;
4903 (void)copy_option_part(&p, part, size, ",");
4904 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4905 goto fail;
4906 }
4907 vim_free(buf);
4908 vim_free(part);
4909 return OK;
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004912 {
4913 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004915 }
4916 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else if (put_escstr(fd, *valuep, 2) == FAIL)
4919 return FAIL;
4920 }
4921 if (put_eol(fd) < 0)
4922 return FAIL;
4923 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004924fail:
4925 vim_free(buf);
4926 vim_free(part);
4927 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928}
4929
4930 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004931put_setnum(
4932 FILE *fd,
4933 char *cmd,
4934 char *name,
4935 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 long wc;
4938
4939 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4940 return FAIL;
4941 if (wc_use_keyname((char_u *)valuep, &wc))
4942 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004943 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4945 return FAIL;
4946 }
4947 else if (fprintf(fd, "%ld", *valuep) < 0)
4948 return FAIL;
4949 if (put_eol(fd) < 0)
4950 return FAIL;
4951 return OK;
4952}
4953
4954 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004955put_setbool(
4956 FILE *fd,
4957 char *cmd,
4958 char *name,
4959 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004962 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4964 || put_eol(fd) < 0)
4965 return FAIL;
4966 return OK;
4967}
4968
4969/*
4970 * Clear all the terminal options.
4971 * If the option has been allocated, free the memory.
4972 * Terminal options are never hidden or indirect.
4973 */
4974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004975clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 /*
4978 * Reset a few things before clearing the old options. This may cause
4979 * outputting a few things that the terminal doesn't understand, but the
4980 * screen will be cleared later, so this is OK.
4981 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004982 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004985 // When starting the GUI close the display opened for the clipboard.
4986 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (gui.starting)
4988 clear_xterm_clip();
4989#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004990 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004992 free_termoptions();
4993}
4994
4995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004996free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004997{
4998 struct vimoption *p;
4999
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005000 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (istermoption(p))
5002 {
5003 if (p->flags & P_ALLOCED)
5004 free_string_option(*(char_u **)(p->var));
5005 if (p->flags & P_DEF_ALLOCED)
5006 free_string_option(p->def_val[VI_DEFAULT]);
5007 *(char_u **)(p->var) = empty_option;
5008 p->def_val[VI_DEFAULT] = empty_option;
5009 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005010#ifdef FEAT_EVAL
5011 // remember where the option was cleared
5012 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 clear_termcodes();
5016}
5017
5018/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005019 * Free the string for one term option, if it was allocated.
5020 * Set the string to empty_option and clear allocated flag.
5021 * "var" points to the option value.
5022 */
5023 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005024free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005025{
5026 struct vimoption *p;
5027
5028 for (p = &options[0]; p->fullname != NULL; p++)
5029 if (p->var == var)
5030 {
5031 if (p->flags & P_ALLOCED)
5032 free_string_option(*(char_u **)(p->var));
5033 *(char_u **)(p->var) = empty_option;
5034 p->flags &= ~P_ALLOCED;
5035 break;
5036 }
5037}
5038
5039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 * Set the terminal option defaults to the current value.
5041 * Used after setting the terminal name.
5042 */
5043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005044set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
5046 struct vimoption *p;
5047
5048 for (p = &options[0]; p->fullname != NULL; p++)
5049 {
5050 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5051 {
5052 if (p->flags & P_DEF_ALLOCED)
5053 {
5054 free_string_option(p->def_val[VI_DEFAULT]);
5055 p->flags &= ~P_DEF_ALLOCED;
5056 }
5057 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5058 if (p->flags & P_ALLOCED)
5059 {
5060 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005061 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063 }
5064 }
5065}
5066
5067/*
5068 * return TRUE if 'p' starts with 't_'
5069 */
5070 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005071istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
5073 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5074}
5075
Bram Moolenaardac13472019-09-16 21:06:21 +02005076/*
5077 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5078 */
5079 int
5080istermoption_idx(int opt_idx)
5081{
5082 return istermoption(&options[opt_idx]);
5083}
5084
Bram Moolenaar113e1072019-01-20 15:30:40 +01005085#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005087 * Unset local option value, similar to ":set opt<".
5088 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005089 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005090unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005091{
5092 struct vimoption *p;
5093 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005094 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005095
5096 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005097 if (opt_idx < 0)
5098 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005099 p = &(options[opt_idx]);
5100
5101 switch ((int)p->indir)
5102 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005103 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005104 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005105 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005106 break;
5107 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005108 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005109 break;
5110 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005111 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005112 break;
5113 case PV_AR:
5114 buf->b_p_ar = -1;
5115 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005116 case PV_BKC:
5117 clear_string_option(&buf->b_p_bkc);
5118 buf->b_bkc_flags = 0;
5119 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005121 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005122 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005123 case PV_TC:
5124 clear_string_option(&buf->b_p_tc);
5125 buf->b_tc_flags = 0;
5126 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005127 case PV_SISO:
5128 curwin->w_p_siso = -1;
5129 break;
5130 case PV_SO:
5131 curwin->w_p_so = -1;
5132 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005133#ifdef FEAT_FIND_ID
5134 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005135 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005136 break;
5137 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005138 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005139 break;
5140#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005141 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005142 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005143 break;
5144 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005145 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005146 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005147#ifdef FEAT_COMPL_FUNC
5148 case PV_TSRFU:
5149 clear_string_option(&buf->b_p_tsrfu);
5150 break;
5151#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005152 case PV_FP:
5153 clear_string_option(&buf->b_p_fp);
5154 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005155#ifdef FEAT_QUICKFIX
5156 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005157 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005158 break;
5159 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005160 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005161 break;
5162 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005163 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005164 break;
5165#endif
5166#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5167 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005168 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005169 break;
5170#endif
5171#if defined(FEAT_CRYPT)
5172 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005173 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005174 break;
5175#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005176#ifdef FEAT_LINEBREAK
5177 case PV_SBR:
5178 clear_string_option(&((win_T *)from)->w_p_sbr);
5179 break;
5180#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005181#ifdef FEAT_STL_OPT
5182 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005183 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005184 break;
5185#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005186 case PV_UL:
5187 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5188 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005189#ifdef FEAT_LISP
5190 case PV_LW:
5191 clear_string_option(&buf->b_p_lw);
5192 break;
5193#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005194 case PV_MENC:
5195 clear_string_option(&buf->b_p_menc);
5196 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005197 case PV_LCS:
5198 clear_string_option(&((win_T *)from)->w_p_lcs);
5199 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5200 redraw_later(NOT_VALID);
5201 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005202 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005203 clear_string_option(&((win_T *)from)->w_p_ve);
5204 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005205 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005206 }
5207}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005208#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005209
5210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005212 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 */
5214 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005215get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005217 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 if (p->var == VAR_WIN)
5220 return (char_u *)GLOBAL_WO(get_varp(p));
5221 return p->var;
5222 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005223 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 switch ((int)p->indir)
5226 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005227 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005229 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5230 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5231 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005233 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5234 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5235 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005236 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005237 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005238 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005239 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5240 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005242 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5243 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005245 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5246 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005247#ifdef FEAT_COMPL_FUNC
5248 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5249#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005250#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5251 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5252#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005253#if defined(FEAT_CRYPT)
5254 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5255#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005256#ifdef FEAT_LINEBREAK
5257 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5258#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005259#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005260 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005261#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005262 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005263#ifdef FEAT_LISP
5264 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5265#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005266 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005267 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005268 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005269 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005272 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274 return get_varp(p);
5275}
5276
5277/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005278 * Get pointer to option variable at 'opt_idx', depending on local or global
5279 * scope.
5280 */
5281 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005282get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005283{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005284 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005285}
5286
5287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 * Get pointer to option variable.
5289 */
5290 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005291get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005293 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (p->var == NULL)
5295 return NULL;
5296
5297 switch ((int)p->indir)
5298 {
5299 case PV_NONE: return p->var;
5300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005301 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005302 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005304 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005306 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005308 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005310 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005312 case PV_TC: return *curbuf->b_p_tc != NUL
5313 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005314 case PV_BKC: return *curbuf->b_p_bkc != NUL
5315 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005316 case PV_SISO: return curwin->w_p_siso >= 0
5317 ? (char_u *)&(curwin->w_p_siso) : p->var;
5318 case PV_SO: return curwin->w_p_so >= 0
5319 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005321 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005323 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5325#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005326 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005328 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005330#ifdef FEAT_COMPL_FUNC
5331 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5332 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5333#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005334 case PV_FP: return *curbuf->b_p_fp != NUL
5335 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005337 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005339 case PV_GP: return *curbuf->b_p_gp != NUL
5340 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5341 case PV_MP: return *curbuf->b_p_mp != NUL
5342 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005344#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5345 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5346 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5347#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005348#if defined(FEAT_CRYPT)
5349 case PV_CM: return *curbuf->b_p_cm != NUL
5350 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5351#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005352#ifdef FEAT_LINEBREAK
5353 case PV_SBR: return *curwin->w_p_sbr != NUL
5354 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5355#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005356#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005357 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005358 ? (char_u *)&(curwin->w_p_stl) : p->var;
5359#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005360 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5361 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005362#ifdef FEAT_LISP
5363 case PV_LW: return *curbuf->b_p_lw != NUL
5364 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5365#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005366 case PV_MENC: return *curbuf->b_p_menc != NUL
5367 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#ifdef FEAT_ARABIC
5369 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5370#endif
5371 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005372 case PV_LCS: return *curwin->w_p_lcs != NUL
5373 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005374 case PV_VE: return *curwin->w_p_ve != NUL
5375 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005376#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005377 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5378#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005379#ifdef FEAT_SYN_HL
5380 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5381 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005382 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005383 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#ifdef FEAT_DIFF
5386 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5387#endif
5388#ifdef FEAT_FOLDING
5389 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5390 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5391 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5392 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5393 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5394 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5395 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5396# ifdef FEAT_EVAL
5397 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5398 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5399# endif
5400 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5401#endif
5402 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005403 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005404#ifdef FEAT_LINEBREAK
5405 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005408 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005409#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5411#endif
5412#ifdef FEAT_RIGHTLEFT
5413 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5414 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5415#endif
5416 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5417 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5418#ifdef FEAT_LINEBREAK
5419 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005420 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5421 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005423 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005425 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005426#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005427 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5428 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5429#endif
5430#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005431 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5432 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5433 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5437 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5440 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5442 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5443#ifdef FEAT_CINDENT
5444 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5445 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5446 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5447#endif
5448#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5449 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452#ifdef FEAT_FOLDING
5453 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005456#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005457 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005459#ifdef FEAT_COMPL_FUNC
5460 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005461 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005462#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005463#ifdef FEAT_EVAL
5464 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005467 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005473 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5475 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5476 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5477 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5478#ifdef FEAT_FIND_ID
5479# ifdef FEAT_EVAL
5480 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5481# endif
5482#endif
5483#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5484 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5485 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5486#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005487#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005488 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490#ifdef FEAT_CRYPT
5491 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5492#endif
5493#ifdef FEAT_LISP
5494 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5495#endif
5496 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5497 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5498 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5499 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5500 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005502#ifdef FEAT_TEXTOBJ
5503 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5506#ifdef FEAT_SMARTINDENT
5507 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5511#ifdef FEAT_SEARCHPATH
5512 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5513#endif
5514 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5515#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005516 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005517 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5518#endif
5519#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005520 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5521 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5522 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005523 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#endif
5525 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5526 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5527 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5528 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005529#ifdef FEAT_PERSISTENT_UNDO
5530 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5533#ifdef FEAT_KEYMAP
5534 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5535#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005536#ifdef FEAT_SIGNS
5537 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5538#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005539#ifdef FEAT_VARTABS
5540 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5541 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5542#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005543 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005545 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 return (char_u *)&(curbuf->b_p_wm);
5547}
5548
5549/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005550 * Return a pointer to the variable for option at 'opt_idx'
5551 */
5552 char_u *
5553get_option_var(int opt_idx)
5554{
5555 return options[opt_idx].var;
5556}
5557
Dominique Pelle748b3082022-01-08 12:41:16 +00005558#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005559/*
5560 * Return the full name of the option at 'opt_idx'
5561 */
5562 char_u *
5563get_option_fullname(int opt_idx)
5564{
5565 return (char_u *)options[opt_idx].fullname;
5566}
Dominique Pelle748b3082022-01-08 12:41:16 +00005567#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005568
5569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 * Get the value of 'equalprg', either the buffer-local one or the global one.
5571 */
5572 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005573get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575 if (*curbuf->b_p_ep == NUL)
5576 return p_ep;
5577 return curbuf->b_p_ep;
5578}
5579
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580/*
5581 * Copy options from one window to another.
5582 * Used when splitting a window.
5583 */
5584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005585win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5588 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005589 after_copy_winopt(wp_to);
5590}
5591
5592/*
5593 * After copying window options: update variables depending on options.
5594 */
5595 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005596after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005597{
5598#ifdef FEAT_LINEBREAK
5599 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005600#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005601#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005602 fill_culopt_flags(NULL, wp);
5603 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005604#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005605 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
5608/*
5609 * Copy the options from one winopt_T to another.
5610 * Doesn't free the old option values in "to", use clear_winopt() for that.
5611 * The 'scroll' option is not copied, because it depends on the window height.
5612 * The 'previewwindow' option is reset, there can be only one preview window.
5613 */
5614 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005615copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617#ifdef FEAT_ARABIC
5618 to->wo_arab = from->wo_arab;
5619#endif
5620 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005621 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005623 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005624 to->wo_ve = vim_strsave(from->wo_ve);
5625 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005626#ifdef FEAT_LINEBREAK
5627 to->wo_nuw = from->wo_nuw;
5628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629#ifdef FEAT_RIGHTLEFT
5630 to->wo_rl = from->wo_rl;
5631 to->wo_rlc = vim_strsave(from->wo_rlc);
5632#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005633#ifdef FEAT_LINEBREAK
5634 to->wo_sbr = vim_strsave(from->wo_sbr);
5635#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005636#ifdef FEAT_STL_OPT
5637 to->wo_stl = vim_strsave(from->wo_stl);
5638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005640#ifdef FEAT_DIFF
5641 to->wo_wrap_save = from->wo_wrap_save;
5642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643#ifdef FEAT_LINEBREAK
5644 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005645 to->wo_bri = from->wo_bri;
5646 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005648 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005650 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005651 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005652 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005653#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005654 to->wo_spell = from->wo_spell;
5655#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005656#ifdef FEAT_SYN_HL
5657 to->wo_cuc = from->wo_cuc;
5658 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005659 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005660 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662#ifdef FEAT_DIFF
5663 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005664 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005666#ifdef FEAT_CONCEAL
5667 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005668 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005669#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005670#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005671 to->wo_twk = vim_strsave(from->wo_twk);
5672 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674#ifdef FEAT_FOLDING
5675 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005676 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005678 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 to->wo_fdi = vim_strsave(from->wo_fdi);
5680 to->wo_fml = from->wo_fml;
5681 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005682 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005684 to->wo_fdm_save = from->wo_diff_saved
5685 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 to->wo_fdn = from->wo_fdn;
5687# ifdef FEAT_EVAL
5688 to->wo_fde = vim_strsave(from->wo_fde);
5689 to->wo_fdt = vim_strsave(from->wo_fdt);
5690# endif
5691 to->wo_fmr = vim_strsave(from->wo_fmr);
5692#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005693#ifdef FEAT_SIGNS
5694 to->wo_scl = vim_strsave(from->wo_scl);
5695#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005696
5697#ifdef FEAT_EVAL
5698 // Copy the script context so that we know where the value was last set.
5699 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5700 sizeof(to->wo_script_ctx));
5701#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005702 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703}
5704
5705/*
5706 * Check string options in a window for a NULL value.
5707 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005709check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 check_winopt(&win->w_onebuf_opt);
5712 check_winopt(&win->w_allbuf_opt);
5713}
5714
5715/*
5716 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5717 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005719check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721#ifdef FEAT_FOLDING
5722 check_string_option(&wop->wo_fdi);
5723 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005724 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725# ifdef FEAT_EVAL
5726 check_string_option(&wop->wo_fde);
5727 check_string_option(&wop->wo_fdt);
5728# endif
5729 check_string_option(&wop->wo_fmr);
5730#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005731#ifdef FEAT_SIGNS
5732 check_string_option(&wop->wo_scl);
5733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#ifdef FEAT_RIGHTLEFT
5735 check_string_option(&wop->wo_rlc);
5736#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005737#ifdef FEAT_LINEBREAK
5738 check_string_option(&wop->wo_sbr);
5739#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005740#ifdef FEAT_STL_OPT
5741 check_string_option(&wop->wo_stl);
5742#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005743#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005744 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005745 check_string_option(&wop->wo_cc);
5746#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005747#ifdef FEAT_CONCEAL
5748 check_string_option(&wop->wo_cocu);
5749#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005750#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005751 check_string_option(&wop->wo_twk);
5752 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005753#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005754#ifdef FEAT_LINEBREAK
5755 check_string_option(&wop->wo_briopt);
5756#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005757 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005758 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005759 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760}
5761
5762/*
5763 * Free the allocated memory inside a winopt_T.
5764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005766clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768#ifdef FEAT_FOLDING
5769 clear_string_option(&wop->wo_fdi);
5770 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005771 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772# ifdef FEAT_EVAL
5773 clear_string_option(&wop->wo_fde);
5774 clear_string_option(&wop->wo_fdt);
5775# endif
5776 clear_string_option(&wop->wo_fmr);
5777#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005778#ifdef FEAT_SIGNS
5779 clear_string_option(&wop->wo_scl);
5780#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005781#ifdef FEAT_LINEBREAK
5782 clear_string_option(&wop->wo_briopt);
5783#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005784 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef FEAT_RIGHTLEFT
5786 clear_string_option(&wop->wo_rlc);
5787#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005788#ifdef FEAT_LINEBREAK
5789 clear_string_option(&wop->wo_sbr);
5790#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005791#ifdef FEAT_STL_OPT
5792 clear_string_option(&wop->wo_stl);
5793#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005794#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005795 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005796 clear_string_option(&wop->wo_cc);
5797#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005798#ifdef FEAT_CONCEAL
5799 clear_string_option(&wop->wo_cocu);
5800#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005801#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005802 clear_string_option(&wop->wo_twk);
5803 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005804#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005805 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005806 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807}
5808
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005809#ifdef FEAT_EVAL
5810// Index into the options table for a buffer-local option enum.
5811static int buf_opt_idx[BV_COUNT];
5812# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5813
5814/*
5815 * Initialize buf_opt_idx[] if not done already.
5816 */
5817 static void
5818init_buf_opt_idx(void)
5819{
5820 static int did_init_buf_opt_idx = FALSE;
5821 int i;
5822
5823 if (did_init_buf_opt_idx)
5824 return;
5825 did_init_buf_opt_idx = TRUE;
5826 for (i = 0; !istermoption_idx(i); i++)
5827 if (options[i].indir & PV_BUF)
5828 buf_opt_idx[options[i].indir & PV_MASK] = i;
5829}
5830#else
5831# define COPY_OPT_SCTX(buf, bv)
5832#endif
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834/*
5835 * Copy global option values to local options for one buffer.
5836 * Used when creating a new buffer and sometimes when entering a buffer.
5837 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005838 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5840 * appropriate.
5841 * BCO_NOHELP Don't copy the values to a help buffer.
5842 */
5843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005844buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005847 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 int dont_do_help;
5849 int did_isk = FALSE;
5850
5851 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 * Skip this when the option defaults have not been set yet. Happens when
5853 * main() allocates the first buffer.
5854 */
5855 if (p_cpo != NULL)
5856 {
5857 /*
5858 * Always copy when entering and 'cpo' contains 'S'.
5859 * Don't copy when already initialized.
5860 * Don't copy when 'cpo' contains 's' and not entering.
5861 * 'S' BCO_ENTER initialized 's' should_copy
5862 * yes yes X X TRUE
5863 * yes no yes X FALSE
5864 * no X yes X FALSE
5865 * X no no yes FALSE
5866 * X no no no TRUE
5867 * no yes no X TRUE
5868 */
5869 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5870 && (buf->b_p_initialized
5871 || (!(flags & BCO_ENTER)
5872 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5873 should_copy = FALSE;
5874
5875 if (should_copy || (flags & BCO_ALWAYS))
5876 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005877#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005878 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005879 init_buf_opt_idx();
5880#endif
5881 // Don't copy the options specific to a help buffer when
5882 // BCO_NOHELP is given or the options were initialized already
5883 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5885 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005886 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
5888 save_p_isk = buf->b_p_isk;
5889 buf->b_p_isk = NULL;
5890 }
5891 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005892 * Always free the allocated strings. If not already initialized,
5893 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 */
5895 if (!buf->b_p_initialized)
5896 {
5897 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005898 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005901 switch (*p_ffs)
5902 {
5903 case 'm':
5904 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5905 case 'd':
5906 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5907 case 'u':
5908 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5909 default:
5910 buf->b_p_ff = vim_strsave(p_ff);
5911 }
5912 if (buf->b_p_ff != NULL)
5913 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 buf->b_p_bh = empty_option;
5915 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917 else
5918 free_buf_options(buf, FALSE);
5919
5920 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005921 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 buf->b_p_ai_nopaste = p_ai_nopaste;
5923 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005924 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005926 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 buf->b_p_tw_nopaste = p_tw_nopaste;
5928 buf->b_p_tw_nobin = p_tw_nobin;
5929 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005930 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 buf->b_p_wm_nopaste = p_wm_nopaste;
5932 buf->b_p_wm_nobin = p_wm_nobin;
5933 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005935 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005936 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005937 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005938 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005940 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005942 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 buf->b_p_ml_nobin = p_ml_nobin;
5946 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005947 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005948 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005949 buf->b_p_swf = FALSE;
5950 else
5951 {
5952 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005953 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005956 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005957#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005958 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005961#ifdef FEAT_COMPL_FUNC
5962 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005963 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005964 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005965 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005966 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005967 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005968#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005969#ifdef FEAT_EVAL
5970 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005971 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005972 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005975 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005977#ifdef FEAT_VARTABS
5978 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005979 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005980 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005981 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005982 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00005983 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005984 buf->b_p_vsts_nopaste = p_vsts_nopaste
5985 ? vim_strsave(p_vsts_nopaste) : NULL;
5986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005988 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005990 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#ifdef FEAT_FOLDING
5992 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005993 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994#endif
5995 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005996 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005997 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005998 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005999 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6000 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006002 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006004 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005#ifdef FEAT_SMARTINDENT
6006 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006007 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#endif
6009 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006010 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011#ifdef FEAT_CINDENT
6012 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006013 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006015 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006022 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6024 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026#endif
6027#ifdef FEAT_LISP
6028 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006029 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030#endif
6031#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006032 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006034 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006035 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006036 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006037#endif
6038#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006039 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006040 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006041 (void)compile_cap_prog(&buf->b_s);
6042 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006043 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006044 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006045 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006046 buf->b_s.b_p_spo = vim_strsave(p_spo);
6047 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#endif
6049#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6050 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006051 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006053 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006055 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006056#if defined(FEAT_EVAL)
6057 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006058 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#ifdef FEAT_CRYPT
6061 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006062 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063#endif
6064#ifdef FEAT_SEARCHPATH
6065 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006066 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#endif
6068#ifdef FEAT_KEYMAP
6069 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006070 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 buf->b_kmap_state |= KEYMAP_INIT;
6072#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006073#ifdef FEAT_TERMINAL
6074 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006075 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006076#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006077 // This isn't really an option, but copying the langmap and IME
6078 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006080 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006082 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006084 // options that are normally global but also have a local value
6085 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006087 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006088 buf->b_p_bkc = empty_option;
6089 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090#ifdef FEAT_QUICKFIX
6091 buf->b_p_gp = empty_option;
6092 buf->b_p_mp = empty_option;
6093 buf->b_p_efm = empty_option;
6094#endif
6095 buf->b_p_ep = empty_option;
6096 buf->b_p_kp = empty_option;
6097 buf->b_p_path = empty_option;
6098 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006099 buf->b_p_tc = empty_option;
6100 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#ifdef FEAT_FIND_ID
6102 buf->b_p_def = empty_option;
6103 buf->b_p_inc = empty_option;
6104# ifdef FEAT_EVAL
6105 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006106 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107# endif
6108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 buf->b_p_dict = empty_option;
6110 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006111#ifdef FEAT_COMPL_FUNC
6112 buf->b_p_tsrfu = empty_option;
6113#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006114#ifdef FEAT_TEXTOBJ
6115 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006116 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006117#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006118#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6119 buf->b_p_bexpr = empty_option;
6120#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006121#if defined(FEAT_CRYPT)
6122 buf->b_p_cm = empty_option;
6123#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006124#ifdef FEAT_PERSISTENT_UNDO
6125 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006126 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006127#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006128#ifdef FEAT_LISP
6129 buf->b_p_lw = empty_option;
6130#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006131 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 /*
6134 * Don't copy the options set by ex_help(), use the saved values,
6135 * when going from a help buffer to a non-help buffer.
6136 * Don't touch these at all when BCO_NOHELP is used and going from
6137 * or to a help buffer.
6138 */
6139 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006142#ifdef FEAT_VARTABS
6143 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006144 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006145 else
6146 buf->b_p_vts_array = NULL;
6147#endif
6148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 else
6150 {
6151 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006152 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 did_isk = TRUE;
6154 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006155 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006156#ifdef FEAT_VARTABS
6157 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006158 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006159 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006160 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006161 else
6162 buf->b_p_vts_array = NULL;
6163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (buf->b_p_bt[0] == 'h')
6166 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006168 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 }
6170 }
6171
6172 /*
6173 * When the options should be copied (ignoring BCO_ALWAYS), set the
6174 * flag that indicates that the options have been initialized.
6175 */
6176 if (should_copy)
6177 buf->b_p_initialized = TRUE;
6178 }
6179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006180 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (did_isk)
6182 (void)buf_init_chartab(buf, FALSE);
6183}
6184
6185/*
6186 * Reset the 'modifiable' option and its default value.
6187 */
6188 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006189reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 int opt_idx;
6192
6193 curbuf->b_p_ma = FALSE;
6194 p_ma = FALSE;
6195 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006196 if (opt_idx >= 0)
6197 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198}
6199
6200/*
6201 * Set the global value for 'iminsert' to the local value.
6202 */
6203 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006204set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 p_iminsert = curbuf->b_p_iminsert;
6207}
6208
6209/*
6210 * Set the global value for 'imsearch' to the local value.
6211 */
6212 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006213set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 p_imsearch = curbuf->b_p_imsearch;
6216}
6217
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218static int expand_option_idx = -1;
6219static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6220static int expand_option_flags = 0;
6221
6222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006223set_context_in_set_cmd(
6224 expand_T *xp,
6225 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006226 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006229 long_u flags = 0; // init for GCC
6230 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 char_u *p;
6232 char_u *s;
6233 int is_term_option = FALSE;
6234 int key;
6235
6236 expand_option_flags = opt_flags;
6237
6238 xp->xp_context = EXPAND_SETTINGS;
6239 if (*arg == NUL)
6240 {
6241 xp->xp_pattern = arg;
6242 return;
6243 }
6244 p = arg + STRLEN(arg) - 1;
6245 if (*p == ' ' && *(p - 1) != '\\')
6246 {
6247 xp->xp_pattern = p + 1;
6248 return;
6249 }
6250 while (p > arg)
6251 {
6252 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006253 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (*p == ' ' || *p == ',')
6255 {
6256 while (s > arg && *(s - 1) == '\\')
6257 --s;
6258 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006259 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (*p == ' ' && ((p - s) & 1) == 0)
6261 {
6262 ++p;
6263 break;
6264 }
6265 --p;
6266 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006267 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
6269 xp->xp_context = EXPAND_BOOL_SETTINGS;
6270 p += 2;
6271 }
6272 if (STRNCMP(p, "inv", 3) == 0)
6273 {
6274 xp->xp_context = EXPAND_BOOL_SETTINGS;
6275 p += 3;
6276 }
6277 xp->xp_pattern = arg = p;
6278 if (*arg == '<')
6279 {
6280 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006281 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 return;
6283 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006284 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
6286 xp->xp_context = EXPAND_NOTHING;
6287 return;
6288 }
6289 nextchar = *++p;
6290 is_term_option = TRUE;
6291 expand_option_name[2] = KEY2TERMCAP0(key);
6292 expand_option_name[3] = KEY2TERMCAP1(key);
6293 }
6294 else
6295 {
6296 if (p[0] == 't' && p[1] == '_')
6297 {
6298 p += 2;
6299 if (*p != NUL)
6300 ++p;
6301 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006302 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 nextchar = *++p;
6304 is_term_option = TRUE;
6305 expand_option_name[2] = p[-2];
6306 expand_option_name[3] = p[-1];
6307 }
6308 else
6309 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006310 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6312 p++;
6313 if (*p == NUL)
6314 return;
6315 nextchar = *p;
6316 *p = NUL;
6317 opt_idx = findoption(arg);
6318 *p = nextchar;
6319 if (opt_idx == -1 || options[opt_idx].var == NULL)
6320 {
6321 xp->xp_context = EXPAND_NOTHING;
6322 return;
6323 }
6324 flags = options[opt_idx].flags;
6325 if (flags & P_BOOL)
6326 {
6327 xp->xp_context = EXPAND_NOTHING;
6328 return;
6329 }
6330 }
6331 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006332 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6334 {
6335 ++p;
6336 nextchar = '=';
6337 }
6338 if ((nextchar != '=' && nextchar != ':')
6339 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6340 {
6341 xp->xp_context = EXPAND_UNSUCCESSFUL;
6342 return;
6343 }
6344 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6345 {
6346 xp->xp_context = EXPAND_OLD_SETTING;
6347 if (is_term_option)
6348 expand_option_idx = -1;
6349 else
6350 expand_option_idx = opt_idx;
6351 xp->xp_pattern = p + 1;
6352 return;
6353 }
6354 xp->xp_context = EXPAND_NOTHING;
6355 if (is_term_option || (flags & P_NUM))
6356 return;
6357
6358 xp->xp_pattern = p + 1;
6359
6360 if (flags & P_EXPAND)
6361 {
6362 p = options[opt_idx].var;
6363 if (p == (char_u *)&p_bdir
6364 || p == (char_u *)&p_dir
6365 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006366 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 || p == (char_u *)&p_rtp
6368#ifdef FEAT_SEARCHPATH
6369 || p == (char_u *)&p_cdpath
6370#endif
6371#ifdef FEAT_SESSION
6372 || p == (char_u *)&p_vdir
6373#endif
6374 )
6375 {
6376 xp->xp_context = EXPAND_DIRECTORIES;
6377 if (p == (char_u *)&p_path
6378#ifdef FEAT_SEARCHPATH
6379 || p == (char_u *)&p_cdpath
6380#endif
6381 )
6382 xp->xp_backslash = XP_BS_THREE;
6383 else
6384 xp->xp_backslash = XP_BS_ONE;
6385 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006386 else if (p == (char_u *)&p_ft)
6387 {
6388 xp->xp_context = EXPAND_FILETYPE;
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 else
6391 {
6392 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006393 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (p == (char_u *)&p_tags)
6395 xp->xp_backslash = XP_BS_THREE;
6396 else
6397 xp->xp_backslash = XP_BS_ONE;
6398 }
6399 }
6400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006401 // For an option that is a list of file names, find the start of the
6402 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006405 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (*p == ' ' || *p == ',')
6407 {
6408 s = p;
6409 while (s > xp->xp_pattern && *(s - 1) == '\\')
6410 --s;
6411 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6412 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6413 {
6414 xp->xp_pattern = p + 1;
6415 break;
6416 }
6417 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006418
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006419#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006420 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006421 if (options[opt_idx].var == (char_u *)&p_sps
6422 && STRNCMP(p, "file:", 5) == 0)
6423 {
6424 xp->xp_pattern = p + 5;
6425 break;
6426 }
6427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429}
6430
6431 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432ExpandSettings(
6433 expand_T *xp,
6434 regmatch_T *regmatch,
6435 int *num_file,
6436 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006438 int num_normal = 0; // Nr of matching non-term-code settings
6439 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 int opt_idx;
6441 int match;
6442 int count = 0;
6443 char_u *str;
6444 int loop;
6445 int is_term_opt;
6446 char_u name_buf[MAX_KEY_NAME_LEN];
6447 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006448 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006450 // do this loop twice:
6451 // loop == 0: count the number of matching options
6452 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 for (loop = 0; loop <= 1; ++loop)
6454 {
6455 regmatch->rm_ic = ic;
6456 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6457 {
K.Takataeeec2542021-06-02 13:28:16 +02006458 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6460 {
6461 if (loop == 0)
6462 num_normal++;
6463 else
6464 (*file)[count++] = vim_strsave((char_u *)names[match]);
6465 }
6466 }
6467 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6468 opt_idx++)
6469 {
6470 if (options[opt_idx].var == NULL)
6471 continue;
6472 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6473 && !(options[opt_idx].flags & P_BOOL))
6474 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006475 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (is_term_opt && num_normal > 0)
6477 continue;
6478 match = FALSE;
6479 if (vim_regexec(regmatch, str, (colnr_T)0)
6480 || (options[opt_idx].shortname != NULL
6481 && vim_regexec(regmatch,
6482 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6483 match = TRUE;
6484 else if (is_term_opt)
6485 {
6486 name_buf[0] = '<';
6487 name_buf[1] = 't';
6488 name_buf[2] = '_';
6489 name_buf[3] = str[2];
6490 name_buf[4] = str[3];
6491 name_buf[5] = '>';
6492 name_buf[6] = NUL;
6493 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6494 {
6495 match = TRUE;
6496 str = name_buf;
6497 }
6498 }
6499 if (match)
6500 {
6501 if (loop == 0)
6502 {
6503 if (is_term_opt)
6504 num_term++;
6505 else
6506 num_normal++;
6507 }
6508 else
6509 (*file)[count++] = vim_strsave(str);
6510 }
6511 }
6512 /*
6513 * Check terminal key codes, these are not in the option table
6514 */
6515 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6516 {
6517 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6518 {
6519 if (!isprint(str[0]) || !isprint(str[1]))
6520 continue;
6521
6522 name_buf[0] = 't';
6523 name_buf[1] = '_';
6524 name_buf[2] = str[0];
6525 name_buf[3] = str[1];
6526 name_buf[4] = NUL;
6527
6528 match = FALSE;
6529 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6530 match = TRUE;
6531 else
6532 {
6533 name_buf[0] = '<';
6534 name_buf[1] = 't';
6535 name_buf[2] = '_';
6536 name_buf[3] = str[0];
6537 name_buf[4] = str[1];
6538 name_buf[5] = '>';
6539 name_buf[6] = NUL;
6540
6541 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6542 match = TRUE;
6543 }
6544 if (match)
6545 {
6546 if (loop == 0)
6547 num_term++;
6548 else
6549 (*file)[count++] = vim_strsave(name_buf);
6550 }
6551 }
6552
6553 /*
6554 * Check special key names.
6555 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006556 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6558 {
6559 name_buf[0] = '<';
6560 STRCPY(name_buf + 1, str);
6561 STRCAT(name_buf, ">");
6562
6563 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6564 {
6565 if (loop == 0)
6566 num_term++;
6567 else
6568 (*file)[count++] = vim_strsave(name_buf);
6569 }
6570 }
6571 }
6572 if (loop == 0)
6573 {
6574 if (num_normal > 0)
6575 *num_file = num_normal;
6576 else if (num_term > 0)
6577 *num_file = num_term;
6578 else
6579 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006580 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 if (*file == NULL)
6582 {
6583 *file = (char_u **)"";
6584 return FAIL;
6585 }
6586 }
6587 }
6588 return OK;
6589}
6590
6591 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006592ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006594 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 char_u *buf;
6596
6597 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006598 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 if (*file == NULL)
6600 return FAIL;
6601
6602 /*
6603 * For a terminal key code expand_option_idx is < 0.
6604 */
6605 if (expand_option_idx < 0)
6606 {
6607 var = find_termcode(expand_option_name + 2);
6608 if (var == NULL)
6609 expand_option_idx = findoption(expand_option_name);
6610 }
6611
6612 if (expand_option_idx >= 0)
6613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006614 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 option_value2string(&options[expand_option_idx], expand_option_flags);
6616 var = NameBuff;
6617 }
6618 else if (var == NULL)
6619 var = (char_u *)"";
6620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006621 // A backslash is required before some characters. This is the reverse of
6622 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 buf = vim_strsave_escaped(var, escape_chars);
6624
6625 if (buf == NULL)
6626 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006627 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 return FAIL;
6629 }
6630
6631#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006632 // For MS-Windows et al. we don't double backslashes at the start and
6633 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006634 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (var[0] == '\\' && var[1] == '\\'
6636 && expand_option_idx >= 0
6637 && (options[expand_option_idx].flags & P_EXPAND)
6638 && vim_isfilec(var[2])
6639 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006640 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#endif
6642
6643 *file[0] = buf;
6644 *num_file = 1;
6645 return OK;
6646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
6648/*
6649 * Get the value for the numeric or string option *opp in a nice format into
6650 * NameBuff[]. Must not be called with a hidden option!
6651 */
6652 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006653option_value2string(
6654 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006655 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 char_u *varp;
6658
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006659 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
6661 if (opp->flags & P_NUM)
6662 {
6663 long wc = 0;
6664
6665 if (wc_use_keyname(varp, &wc))
6666 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6667 else if (wc != 0)
6668 STRCPY(NameBuff, transchar((int)wc));
6669 else
6670 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6671 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006672 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
6674 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006675 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 NameBuff[0] = NUL;
6677#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006678 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006679 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 STRCPY(NameBuff, "*****");
6681#endif
6682 else if (opp->flags & P_EXPAND)
6683 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006684 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 else if ((char_u **)opp->var == &p_pt)
6686 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6687 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006688 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690}
6691
6692/*
6693 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6694 * printed as a keyname.
6695 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6696 */
6697 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006698wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6701 {
6702 *wcp = *(long *)varp;
6703 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6704 return TRUE;
6705 }
6706 return FALSE;
6707}
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 * Return TRUE if "x" is present in 'shortmess' option, or
6711 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6712 */
6713 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006714shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006716 return p_shm != NULL &&
6717 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 || (vim_strchr(p_shm, 'a') != NULL
6719 && vim_strchr((char_u *)SHM_A, x) != NULL));
6720}
6721
6722/*
6723 * paste_option_changed() - Called after p_paste was set or reset.
6724 */
6725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006726paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 static int old_p_paste = FALSE;
6729 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006730 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731#ifdef FEAT_CMDL_INFO
6732 static int save_ru = 0;
6733#endif
6734#ifdef FEAT_RIGHTLEFT
6735 static int save_ri = 0;
6736 static int save_hkmap = 0;
6737#endif
6738 buf_T *buf;
6739
6740 if (p_paste)
6741 {
6742 /*
6743 * Paste switched from off to on.
6744 * Save the current values, so they can be restored later.
6745 */
6746 if (!old_p_paste)
6747 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006748 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006749 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 buf->b_p_tw_nopaste = buf->b_p_tw;
6752 buf->b_p_wm_nopaste = buf->b_p_wm;
6753 buf->b_p_sts_nopaste = buf->b_p_sts;
6754 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006755 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006756#ifdef FEAT_VARTABS
6757 if (buf->b_p_vsts_nopaste)
6758 vim_free(buf->b_p_vsts_nopaste);
6759 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6760 ? vim_strsave(buf->b_p_vsts) : NULL;
6761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
6763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006764 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006766 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767#ifdef FEAT_CMDL_INFO
6768 save_ru = p_ru;
6769#endif
6770#ifdef FEAT_RIGHTLEFT
6771 save_ri = p_ri;
6772 save_hkmap = p_hkmap;
6773#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006774 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006775 p_ai_nopaste = p_ai;
6776 p_et_nopaste = p_et;
6777 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 p_tw_nopaste = p_tw;
6779 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006780#ifdef FEAT_VARTABS
6781 if (p_vsts_nopaste)
6782 vim_free(p_vsts_nopaste);
6783 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
6786
6787 /*
6788 * Always set the option values, also when 'paste' is set when it is
6789 * already on.
6790 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006791 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006792 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006794 buf->b_p_tw = 0; // textwidth is 0
6795 buf->b_p_wm = 0; // wrapmargin is 0
6796 buf->b_p_sts = 0; // softtabstop is 0
6797 buf->b_p_ai = 0; // no auto-indent
6798 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006799#ifdef FEAT_VARTABS
6800 if (buf->b_p_vsts)
6801 free_string_option(buf->b_p_vsts);
6802 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006803 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
6806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006807 // set global options
6808 p_sm = 0; // no showmatch
6809 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006812 status_redraw_all(); // redraw to remove the ruler
6813 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814#endif
6815#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006816 p_ri = 0; // no reverse insert
6817 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006819 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 p_tw = 0;
6821 p_wm = 0;
6822 p_sts = 0;
6823 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006824#ifdef FEAT_VARTABS
6825 if (p_vsts)
6826 free_string_option(p_vsts);
6827 p_vsts = empty_option;
6828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830
6831 /*
6832 * Paste switched from on to off: Restore saved values.
6833 */
6834 else if (old_p_paste)
6835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006836 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006837 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {
6839 buf->b_p_tw = buf->b_p_tw_nopaste;
6840 buf->b_p_wm = buf->b_p_wm_nopaste;
6841 buf->b_p_sts = buf->b_p_sts_nopaste;
6842 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006843 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006844#ifdef FEAT_VARTABS
6845 if (buf->b_p_vsts)
6846 free_string_option(buf->b_p_vsts);
6847 buf->b_p_vsts = buf->b_p_vsts_nopaste
6848 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006849 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006850 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006851 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006852 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006853 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
6856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006857 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006859 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006862 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 p_ru = save_ru;
6864#endif
6865#ifdef FEAT_RIGHTLEFT
6866 p_ri = save_ri;
6867 p_hkmap = save_hkmap;
6868#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006869 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006870 p_ai = p_ai_nopaste;
6871 p_et = p_et_nopaste;
6872 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 p_tw = p_tw_nopaste;
6874 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006875#ifdef FEAT_VARTABS
6876 if (p_vsts)
6877 free_string_option(p_vsts);
6878 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881
6882 old_p_paste = p_paste;
6883}
6884
6885/*
6886 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6887 *
6888 * Reset 'compatible' and set the values for options that didn't get set yet
6889 * to the Vim defaults.
6890 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006891 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 */
6893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006894vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006897 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006898 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
6900 if (!option_was_set((char_u *)"cp"))
6901 {
6902 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006903 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6905 set_option_default(opt_idx, OPT_FREE, FALSE);
6906 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006907 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909
6910 if (fname != NULL)
6911 {
6912 p = vim_getenv(envname, &dofree);
6913 if (p == NULL)
6914 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006915 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006916 p = FullName_save(fname, FALSE);
6917 if (p != NULL)
6918 {
6919 vim_setenv(envname, p);
6920 vim_free(p);
6921 }
6922 }
6923 else if (dofree)
6924 vim_free(p);
6925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926}
6927
6928/*
6929 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6930 */
6931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006932change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006934 int opt_idx;
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (p_cp != on)
6937 {
6938 p_cp = on;
6939 compatible_set();
6940 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006941 opt_idx = findoption((char_u *)"cp");
6942 if (opt_idx >= 0)
6943 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944}
6945
6946/*
6947 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006948 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 */
6950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006951option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
6953 int idx;
6954
6955 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006956 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 return FALSE;
6958 if (options[idx].flags & P_WAS_SET)
6959 return TRUE;
6960 return FALSE;
6961}
6962
6963/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006964 * Reset the flag indicating option "name" was set.
6965 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006966 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006967reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006968{
6969 int idx = findoption(name);
6970
6971 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006972 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006973 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006974 return OK;
6975 }
6976 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006977}
6978
6979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 * compatible_set() - Called when 'compatible' has been set or unset.
6981 *
6982 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6983 * flag) to a Vi compatible value.
6984 * When 'compatible' is unset: Set all options that have a different default
6985 * for Vim (without the P_VI_DEF flag) to that default.
6986 */
6987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006988compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989{
6990 int opt_idx;
6991
Bram Moolenaardac13472019-09-16 21:06:21 +02006992 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6994 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6995 set_option_default(opt_idx, OPT_FREE, p_cp);
6996 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006997 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998}
6999
Bram Moolenaardac13472019-09-16 21:06:21 +02007000#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002/*
7003 * fill_breakat_flags() -- called when 'breakat' changes value.
7004 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007006fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007008 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 int i;
7010
7011 for (i = 0; i < 256; i++)
7012 breakat_flags[i] = FALSE;
7013
7014 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007015 for (p = p_breakat; *p; p++)
7016 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018#endif
7019
7020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 * Check if backspacing over something is allowed.
7022 */
7023 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007024can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007025 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007027#ifdef FEAT_JOB_CHANNEL
7028 if (what == BS_START && bt_prompt(curbuf))
7029 return FALSE;
7030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 switch (*p_bs)
7032 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007033 case '3': return TRUE;
7034 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 case '1': return (what != BS_START);
7036 case '0': return FALSE;
7037 }
7038 return vim_strchr(p_bs, what) != NULL;
7039}
7040
7041/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007042 * Return the effective 'scrolloff' value for the current window, using the
7043 * global value when appropriate.
7044 */
7045 long
7046get_scrolloff_value(void)
7047{
7048 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7049}
7050
7051/*
7052 * Return the effective 'sidescrolloff' value for the current window, using the
7053 * global value when appropriate.
7054 */
7055 long
7056get_sidescrolloff_value(void)
7057{
7058 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7059}
7060
7061/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007062 * Get the local or global value of 'backupcopy'.
7063 */
7064 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007065get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007066{
7067 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7068}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007069
Gary Johnson53ba05b2021-07-26 22:19:10 +02007070/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007071 * Get the local or global value of 'formatlistpat'.
7072 */
7073 char_u *
7074get_flp_value(buf_T *buf)
7075{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007076 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7077 return p_flp;
7078 return buf->b_p_flp;
7079}
7080
7081/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007082 * Get the local or global value of the 'virtualedit' flags.
7083 */
7084 unsigned int
7085get_ve_flags(void)
7086{
Gary Johnson51ad8502021-08-03 18:33:08 +02007087 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7088 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007089}
7090
Bram Moolenaaree857022019-11-09 23:26:40 +01007091#if defined(FEAT_LINEBREAK) || defined(PROTO)
7092/*
7093 * Get the local or global value of 'showbreak'.
7094 */
7095 char_u *
7096get_showbreak_value(win_T *win)
7097{
7098 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7099 return p_sbr;
7100 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7101 return empty_option;
7102 return win->w_p_sbr;
7103}
7104#endif
7105
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007106#if defined(FEAT_EVAL) || defined(PROTO)
7107/*
7108 * Get window or buffer local options.
7109 */
7110 dict_T *
7111get_winbuf_options(int bufopt)
7112{
7113 dict_T *d;
7114 int opt_idx;
7115
7116 d = dict_alloc();
7117 if (d == NULL)
7118 return NULL;
7119
Bram Moolenaardac13472019-09-16 21:06:21 +02007120 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007121 {
7122 struct vimoption *opt = &options[opt_idx];
7123
7124 if ((bufopt && (opt->indir & PV_BUF))
7125 || (!bufopt && (opt->indir & PV_WIN)))
7126 {
7127 char_u *varp = get_varp(opt);
7128
7129 if (varp != NULL)
7130 {
7131 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007132 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007133 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007134 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007135 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007136 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007137 }
7138 }
7139 }
7140
7141 return d;
7142}
7143#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007144
Bram Moolenaardac13472019-09-16 21:06:21 +02007145#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007146/*
7147 * This is called when 'culopt' is changed
7148 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007149 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007150fill_culopt_flags(char_u *val, win_T *wp)
7151{
7152 char_u *p;
7153 char_u culopt_flags_new = 0;
7154
7155 if (val == NULL)
7156 p = wp->w_p_culopt;
7157 else
7158 p = val;
7159 while (*p != NUL)
7160 {
7161 if (STRNCMP(p, "line", 4) == 0)
7162 {
7163 p += 4;
7164 culopt_flags_new |= CULOPT_LINE;
7165 }
7166 else if (STRNCMP(p, "both", 4) == 0)
7167 {
7168 p += 4;
7169 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7170 }
7171 else if (STRNCMP(p, "number", 6) == 0)
7172 {
7173 p += 6;
7174 culopt_flags_new |= CULOPT_NBR;
7175 }
7176 else if (STRNCMP(p, "screenline", 10) == 0)
7177 {
7178 p += 10;
7179 culopt_flags_new |= CULOPT_SCRLINE;
7180 }
7181
7182 if (*p != ',' && *p != NUL)
7183 return FAIL;
7184 if (*p == ',')
7185 ++p;
7186 }
7187
7188 // Can't have both "line" and "screenline".
7189 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7190 return FAIL;
7191 wp->w_p_culopt_flags = culopt_flags_new;
7192
7193 return OK;
7194}
7195#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007196
7197/*
7198 * Get the value of 'magic' adjusted for Vim9 script.
7199 */
7200 int
7201magic_isset(void)
7202{
7203 switch (magic_overruled)
7204 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007205 case OPTION_MAGIC_ON: return TRUE;
7206 case OPTION_MAGIC_OFF: return FALSE;
7207 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007208 }
7209#ifdef FEAT_EVAL
7210 if (in_vim9script())
7211 return TRUE;
7212#endif
7213 return p_magic;
7214}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007215
7216/*
7217 * Set the callback function value for an option that accepts a function name,
7218 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7219 * Returns OK if the option is successfully set to a function, otherwise
7220 * returns FAIL.
7221 */
7222 int
7223option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7224{
7225#ifdef FEAT_EVAL
7226 typval_T *tv;
7227 callback_T cb;
7228
7229 if (optval == NULL || *optval == NUL)
7230 {
7231 free_callback(optcb);
7232 return OK;
7233 }
7234
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007235 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007236 || (STRNCMP(optval, "function(", 9) == 0)
7237 || (STRNCMP(optval, "funcref(", 8) == 0))
7238 // Lambda expression or a funcref
7239 tv = eval_expr(optval, NULL);
7240 else
7241 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007242 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007243 if (tv == NULL)
7244 return FAIL;
7245
7246 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007247 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007248 {
7249 free_tv(tv);
7250 return FAIL;
7251 }
7252
7253 free_callback(optcb);
7254 set_callback(optcb, &cb);
7255 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007256
7257 // when using Vim9 style "import.funcname" it needs to be expanded to
7258 // "import#funcname".
7259 expand_autload_callback(optcb);
7260
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007261 return OK;
7262#else
7263 return FAIL;
7264#endif
7265}