blob: 482e960b254748a293f00a698951035cfff3428c [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 Moolenaare7fedb62015-12-31 19:07:19 +0100269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100270 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100272# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 (char_u *)"cp1252"
274# else
275# ifdef VMS
276 (char_u *)"dec-mcs"
277# else
278# ifdef EBCDIC
279 (char_u *)"ebcdic-uk"
280# else
281# ifdef MAC
282 (char_u *)"mac-roman"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100283# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 (char_u *)"hp-roman8"
285# endif
286# endif
287# endif
288# endif
289 );
290#endif
291
292#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100293 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100295# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000296 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297# else
298# ifdef VMS
299 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
300
301# else
302 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
303# endif
304# endif
305 );
306#endif
307
308 /*
309 * Set all the options (except the terminal options) to their default
310 * value. Also set the global value for local options.
311 */
312 set_options_default(0);
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
334 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
392 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000427# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100428 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000429 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# endif
432
K.Takataf883d902021-05-30 18:04:19 +0200433# ifdef MSWIN
434 // MS-Windows has builtin support for conversion to and from Unicode, using
435 // "utf-8" for 'encoding' should work best for most users.
436 p = vim_strsave((char_u *)ENC_DFLT);
437# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200439 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (p != NULL)
443 {
444 char_u *save_enc;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200447 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 save_enc = p_enc;
449 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000450 if (STRCMP(p_enc, "gb18030") == 0)
451 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // We don't support "gb18030", but "cp936" is a good substitute
453 // for practical purposes, thus use that. It's not an alias to
454 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000455 p_enc = vim_strsave((char_u *)"cp936");
456 vim_free(p);
457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 if (mb_init() == NULL)
459 {
460 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000461 if (opt_idx >= 0)
462 {
463 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
464 options[opt_idx].flags |= P_DEF_ALLOCED;
465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
Bram Moolenaard0573012017-10-28 21:11:06 +0200467#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100468 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100470 // Adjust the default for 'isprint' and 'iskeyword' to match
471 // latin1. Also set the defaults for when 'nocompatible' is
472 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000473 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000474 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000475 set_string_option_direct((char_u *)"isk", -1,
476 ISK_LATIN1, OPT_FREE, SID_NONE);
477 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000478 if (opt_idx >= 0)
479 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000480 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000483 (void)init_chartab();
484 }
485#endif
486
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200487#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 // Win32 console: When GetACP() returns a different value from
489 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200490 if (
491# ifdef VIMDLL
492 (!gui.in_use && !gui.starting) &&
493# endif
494 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 char buf[50];
497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100498 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
499 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100500 if (GetConsoleCP() == 0)
501 sprintf(buf, "cp%ld", (long)GetACP());
502 else
503 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 p_tenc = vim_strsave((char_u *)buf);
505 if (p_tenc != NULL)
506 {
507 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000508 if (opt_idx >= 0)
509 {
510 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
511 options[opt_idx].flags |= P_DEF_ALLOCED;
512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 convert_setup(&input_conv, p_tenc, p_enc);
514 convert_setup(&output_conv, p_enc, p_tenc);
515 }
516 else
517 p_tenc = empty_option;
518 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100519#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100520#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000522 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525 else
526 {
527 vim_free(p_enc);
528 p_enc = save_enc;
529 }
530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 set_helplang_default(get_mess_lang());
535#endif
536}
537
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200538static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
539
540/*
541 * Set the "fileencodings" option to the default value for when 'encoding' is
542 * utf-8.
543 */
544 void
545set_fencs_unicode()
546{
547 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
548 OPT_FREE, 0);
549}
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/*
552 * Set an option to its default value.
553 * This does not take care of side effects!
554 */
555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100556set_option_default(
557 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100558 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
559 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 char_u *varp; // pointer to variable for current option
562 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000564 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
566
567 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
568 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
572 if (flags & P_STRING)
573 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200574 // 'fencs' default value depends on 'encoding'
575 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
576 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100577 // Use set_string_option_direct() for local options to handle
578 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200579 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200580 set_string_option_direct(NULL, opt_idx,
581 options[opt_idx].def_val[dvi], opt_flags, 0);
582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200584 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
585 free_string_option(*(char_u **)(varp));
586 *(char_u **)varp = options[opt_idx].def_val[dvi];
587 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589 }
590 else if (flags & P_NUM)
591 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000592 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 win_comp_scroll(curwin);
594 else
595 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100596 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
597
598 if ((long *)varp == &curwin->w_p_so
599 || (long *)varp == &curwin->w_p_siso)
600 // 'scrolloff' and 'sidescrolloff' local values have a
601 // different default value than the global default.
602 *(long *)varp = -1;
603 else
604 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (both)
607 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100608 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 // the cast to long is required for Manx C, long_i is needed for
614 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000615 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000616#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000618 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
619 *(int *)varp = FALSE;
620#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100621 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (both)
623 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
624 *(int *)varp;
625 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000628 flagsp = insecure_flag(opt_idx, opt_flags);
629 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631
632#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200633 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635}
636
637/*
638 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200639 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 */
641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100642set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100643 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000647 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaardac13472019-09-16 21:06:21 +0200649 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200650 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200651 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100652 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200653# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200654 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200655 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200656# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100657 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 set_option_default(i, opt_flags, p_cp);
659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000661 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200663#ifdef FEAT_CINDENT
664 parse_cino(curbuf);
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
668/*
669 * Set the Vi-default value of a string option.
670 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100671 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 static void
674set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 char_u *p;
677 int opt_idx;
678
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100679 if (escape && vim_strchr(val, ' ') != NULL)
680 p = vim_strsave_escaped(val, (char_u *)" ");
681 else
682 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000686 if (opt_idx >= 0)
687 {
688 if (options[opt_idx].flags & P_DEF_ALLOCED)
689 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
690 options[opt_idx].def_val[VI_DEFAULT] = p;
691 options[opt_idx].flags |= P_DEF_ALLOCED;
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694}
695
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100696 void
697set_string_default(char *name, char_u *val)
698{
699 set_string_default_esc(name, val, FALSE);
700}
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200703 * For an option value that contains comma separated items, find "newval" in
704 * "origval". Return NULL if not found.
705 */
706 static char_u *
707find_dup_item(char_u *origval, char_u *newval, long_u flags)
708{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200709 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200710 size_t newlen;
711 char_u *s;
712
713 if (origval == NULL)
714 return NULL;
715
716 newlen = STRLEN(newval);
717 for (s = origval; *s != NUL; ++s)
718 {
719 if ((!(flags & P_COMMA)
720 || s == origval
721 || (s[-1] == ',' && !(bs & 1)))
722 && STRNCMP(s, newval, newlen) == 0
723 && (!(flags & P_COMMA)
724 || s[newlen] == ','
725 || s[newlen] == NUL))
726 return s;
727 // Count backslashes. Only a comma with an even number of backslashes
728 // or a single backslash preceded by a comma before it is recognized as
729 // a separator.
730 if ((s > origval + 1
731 && s[-1] == '\\'
732 && s[-2] != ',')
733 || (s == origval + 1
734 && s[-1] == '\\'))
735 ++bs;
736 else
737 bs = 0;
738 }
739 return NULL;
740}
741
742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * Set the Vi-default value of a number option.
744 * Used for 'lines' and 'columns'.
745 */
746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100747set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000749 int opt_idx;
750
751 opt_idx = findoption((char_u *)name);
752 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000753 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754}
755
Dominique Pelle748b3082022-01-08 12:41:16 +0000756#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200757/*
758 * Set all window-local and buffer-local options to the Vim default.
759 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200760 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200761 */
762 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200763set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200764{
765 win_T *save_curwin = curwin;
766 int i;
767
768 curwin = wp;
769 curbuf = curwin->w_buffer;
770 block_autocmds();
771
Bram Moolenaardac13472019-09-16 21:06:21 +0200772 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200773 {
774 struct vimoption *p = &(options[i]);
775 char_u *varp = get_varp_scope(p, OPT_LOCAL);
776
777 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200778 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200779 && !(options[i].flags & P_NODEFAULT)
780 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200781 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200782 }
783
784 unblock_autocmds();
785 curwin = save_curwin;
786 curbuf = curwin->w_buffer;
787}
Dominique Pelle748b3082022-01-08 12:41:16 +0000788#endif
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200789
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000790#if defined(EXITFREE) || defined(PROTO)
791/*
792 * Free all options.
793 */
794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100795free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000796{
797 int i;
798
Bram Moolenaardac13472019-09-16 21:06:21 +0200799 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000800 {
801 if (options[i].indir == PV_NONE)
802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100803 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100804 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000805 free_string_option(*(char_u **)options[i].var);
806 if (options[i].flags & P_DEF_ALLOCED)
807 free_string_option(options[i].def_val[VI_DEFAULT]);
808 }
809 else if (options[i].var != VAR_WIN
810 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100811 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200812 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000813 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000814 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000815 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000816}
817#endif
818
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820/*
821 * Initialize the options, part two: After getting Rows and Columns and
822 * setting 'term'.
823 */
824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100825set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000827 int idx;
828
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100830 * 'scroll' defaults to half the window height. The stored default is zero,
831 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000833 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000834 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000835 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 comp_col();
837
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000838 /*
839 * 'window' is only for backwards compatibility with Vi.
840 * Default is Rows - 1.
841 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000842 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000843 p_window = Rows - 1;
844 set_number_default("window", Rows - 1);
845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100847#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000848 /*
849 * If 'background' wasn't set by the user, try guessing the value,
850 * depending on the terminal name. Only need to check for terminals
851 * with a dark background, that can handle color.
852 */
853 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000854 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
855 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000857 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 // don't mark it as set, when starting the GUI it may be
859 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000860 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000863
864#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000866#endif
867#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100868 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000869#endif
870#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873}
874
875/*
876 * Initialize the options, part three: After reading the .vimrc
877 */
878 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100879set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100881#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882/*
883 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
884 * This is done after other initializations, where 'shell' might have been
885 * set, but only if they have not been set before.
886 */
887 char_u *p;
888 int idx_srr;
889 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100890# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 int idx_sp;
892 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
895 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000896 if (idx_srr < 0)
897 do_srr = FALSE;
898 else
899 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100900# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000902 if (idx_sp < 0)
903 do_sp = FALSE;
904 else
905 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100906# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200907 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (p != NULL)
909 {
910 /*
911 * Default for p_sp is "| tee", for p_srr is ">".
912 * For known shells it is changed here to include stderr.
913 */
914 if ( fnamecmp(p, "csh") == 0
915 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100916# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 || fnamecmp(p, "csh.exe") == 0
918 || fnamecmp(p, "tcsh.exe") == 0
919# endif
920 )
921 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100922# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (do_sp)
924 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100925# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100927# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
931 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (do_srr)
934 {
935 p_srr = (char_u *)">&";
936 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
937 }
938 }
Mike Williams12795022021-06-28 20:53:58 +0200939# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200940 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
941 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200942 else if ( fnamecmp(p, "powershell") == 0
943 || fnamecmp(p, "powershell.exe") == 0
944 )
945 {
946# if defined(FEAT_QUICKFIX)
947 if (do_sp)
948 {
949 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
950 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
951 }
952# endif
953 if (do_srr)
954 {
955 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
956 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
957 }
958 }
959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 else
Natanael Copa56318362021-05-06 18:46:35 +0200961 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if ( fnamecmp(p, "sh") == 0
963 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200964 || fnamecmp(p, "mksh") == 0
965 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000967 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200969 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200970 || fnamecmp(p, "ash") == 0
971 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200972 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100973# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 || fnamecmp(p, "cmd") == 0
975 || fnamecmp(p, "sh.exe") == 0
976 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200977 || fnamecmp(p, "mksh.exe") == 0
978 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000980 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 || fnamecmp(p, "bash.exe") == 0
982 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200983 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200984 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100986 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100988# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (do_sp)
990 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100991# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100993# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200994 if (fnamecmp(p, "pwsh") == 0)
995 p_sp = (char_u *)">%s 2>&1";
996 else
997 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
1000 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (do_srr)
1003 {
1004 p_srr = (char_u *)">%s 2>&1";
1005 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1006 }
1007 }
1008 vim_free(p);
1009 }
1010#endif
1011
Bram Moolenaar4f974752019-02-17 17:44:42 +01001012#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001014 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1015 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001017 * set, but only if they have not been set before.
1018 * Default values depend on shell (cmd.exe is default shell):
1019 *
1020 * p_shcf p_sxq
1021 * cmd.exe - "/c" "("
1022 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001023 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001024 * "sh" like shells - "-c" "\""
1025 *
1026 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 */
Mike Williams12795022021-06-28 20:53:58 +02001028 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1029 {
1030 int idx_opt;
1031
1032 idx_opt = findoption((char_u *)"shcf");
1033 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1034 {
1035 p_shcf = (char_u*)"-Command";
1036 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1037 }
1038
1039 idx_opt = findoption((char_u *)"sxq");
1040 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1041 {
1042 p_sxq = (char_u*)"\"";
1043 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1044 }
1045 }
1046 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
1048 int idx3;
1049
1050 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001051 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
1053 p_shcf = (char_u *)"-c";
1054 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1055 }
1056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001057 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001059 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
1061 p_sxq = (char_u *)"\"";
1062 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001065 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1066 {
1067 int idx3;
1068
1069 /*
1070 * cmd.exe on Windows will strip the first and last double quote given
1071 * on the command line, e.g. most of the time things like:
1072 * cmd /c "my path/to/echo" "my args to echo"
1073 * become:
1074 * my path/to/echo" "my args to echo
1075 * when executed.
1076 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001077 * To avoid this, set shellxquote to surround the command in
1078 * parenthesis. This appears to make most commands work, without
1079 * breaking commands that worked previously, such as
1080 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001081 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001082 idx3 = findoption((char_u *)"sxq");
1083 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1084 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001085 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001086 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1087 }
1088
Bram Moolenaara64ba222012-02-12 23:23:31 +01001089 idx3 = findoption((char_u *)"shcf");
1090 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1091 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001092 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001093 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1094 }
1095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096#endif
1097
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001098 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001099 {
1100 int idx_ffs = findoption((char_u *)"ffs");
1101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001102 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001103 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1104 set_fileformat(default_fileformat(), OPT_LOCAL);
1105 }
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108}
1109
1110#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1111/*
1112 * When 'helplang' is still at its default value, set it to "lang".
1113 * Only the first two characters of "lang" are used.
1114 */
1115 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001116set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 int idx;
1119
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001120 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 return;
1122 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001123 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
1125 if (options[idx].flags & P_ALLOCED)
1126 free_string_option(p_hlg);
1127 p_hlg = vim_strsave(lang);
1128 if (p_hlg == NULL)
1129 p_hlg = empty_option;
1130 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001131 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001132 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001133 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1134 {
1135 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1136 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1137 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001138 // any C like setting, such as C.UTF-8, becomes "en"
1139 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1140 {
1141 p_hlg[0] = 'e';
1142 p_hlg[1] = 'n';
1143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 options[idx].flags |= P_ALLOCED;
1147 }
1148}
1149#endif
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151/*
1152 * 'title' and 'icon' only default to true if they have not been set or reset
1153 * in .vimrc and we can read the old value.
1154 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1155 * they can be reset. This reduces startup time when using X on a remote
1156 * machine.
1157 */
1158 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001159set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160{
1161 int idx1;
1162 long val;
1163
1164 /*
1165 * If GUI is (going to be) used, we can always set the window title and
1166 * icon name. Saves a bit of time, because the X11 display server does
1167 * not need to be contacted.
1168 */
1169 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001170 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172#ifdef FEAT_GUI
1173 if (gui.starting || gui.in_use)
1174 val = TRUE;
1175 else
1176#endif
1177 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001178 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 p_title = val;
1180 }
1181 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001182 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184#ifdef FEAT_GUI
1185 if (gui.starting || gui.in_use)
1186 val = TRUE;
1187 else
1188#endif
1189 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001190 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 p_icon = val;
1192 }
1193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001195 void
1196ex_set(exarg_T *eap)
1197{
1198 int flags = 0;
1199
1200 if (eap->cmdidx == CMD_setlocal)
1201 flags = OPT_LOCAL;
1202 else if (eap->cmdidx == CMD_setglobal)
1203 flags = OPT_GLOBAL;
1204#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001205 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001206 ex_options(eap);
1207 else
1208#endif
1209 {
1210 if (eap->forceit)
1211 flags |= OPT_ONECOLUMN;
1212 (void)do_set(eap->arg, flags);
1213 }
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
1217 * Parse 'arg' for option settings.
1218 *
1219 * 'arg' may be IObuff, but only when no errors can be present and option
1220 * does not need to be expanded with option_expand().
1221 * "opt_flags":
1222 * 0 for ":set"
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001223 * OPT_GLOBAL for ":setglobal"
1224 * OPT_LOCAL for ":setlocal" and a modeline
1225 * OPT_MODELINE for a modeline
1226 * OPT_WINONLY to only set window-local options
1227 * OPT_NOWIN to skip setting window-local options
1228 * OPT_ONECOLUMN do not use multiple columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 *
1230 * returns FAIL if an error is detected, OK otherwise
1231 */
1232 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001233do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001234 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001235 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001237 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001239 char *errmsg;
1240 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001242 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1243 int nextchar; // next non-white char after option name
1244 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 int len;
1246 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001247 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001249 long_u flags; // flags for current option
1250 char_u *varp = NULL; // pointer to variable for current option
1251 int did_show = FALSE; // already showed one value
1252 int adding; // "opt+=arg"
1253 int prepending; // "opt^=arg"
1254 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 int cp_val = 0;
1256 char_u key_name[2];
1257
1258 if (*arg == NUL)
1259 {
1260 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001261 did_show = TRUE;
1262 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001265 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
1267 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001268 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001270 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1271 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 {
1273 /*
1274 * ":set all" show all options.
1275 * ":set all&" set all options to their default value.
1276 */
1277 arg += 3;
1278 if (*arg == '&')
1279 {
1280 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001281 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001283 didset_options();
1284 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001285 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001290 did_show = TRUE;
1291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001293 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {
1295 showoptions(2, opt_flags);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00001296 show_termcodes(opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001297 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 arg += 7;
1299 }
1300 else
1301 {
1302 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001303 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
1305 prefix = 0;
1306 arg += 2;
1307 }
1308 else if (STRNCMP(arg, "inv", 3) == 0)
1309 {
1310 prefix = 2;
1311 arg += 3;
1312 }
1313
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001314 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 key = 0;
1316 if (*arg == '<')
1317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001319 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1321 len = 5;
1322 else
1323 {
1324 len = 1;
1325 while (arg[len] != NUL && arg[len] != '>')
1326 ++len;
1327 }
1328 if (arg[len] != '>')
1329 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001330 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 goto skip;
1332 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001333 arg[len] = NUL; // put NUL after name
1334 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001336 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001338 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340 else
1341 {
1342 len = 0;
1343 /*
1344 * The two characters after "t_" may not be alphanumeric.
1345 */
1346 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1347 len = 4;
1348 else
1349 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1350 ++len;
1351 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001352 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001354 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001356 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001359 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 afterchar = arg[len];
1361
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001362 if (in_vim9script())
1363 {
1364 char_u *p = skipwhite(arg + len);
1365
1366 // disallow white space before =val, +=val, -=val, ^=val
1367 if (p > arg + len && (p[0] == '='
1368 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1369 && p[1] == '=')))
1370 {
1371 errmsg = e_no_white_space_allowed_between_option_and;
1372 arg = p;
1373 startarg = p;
1374 goto skip;
1375 }
1376 }
1377 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001378 // skip white space, allow ":set ai ?", ":set hlsearch !"
1379 while (VIM_ISWHITE(arg[len]))
1380 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 adding = FALSE;
1383 prepending = FALSE;
1384 removing = FALSE;
1385 if (arg[len] != NUL && arg[len + 1] == '=')
1386 {
1387 if (arg[len] == '+')
1388 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001389 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 ++len;
1391 }
1392 else if (arg[len] == '^')
1393 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001394 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 ++len;
1396 }
1397 else if (arg[len] == '-')
1398 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001399 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 ++len;
1401 }
1402 }
1403 nextchar = arg[len];
1404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001405 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001407 if (in_vim9script() && arg > arg_start
1408 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1409 errmsg = e_no_white_space_allowed_between_option_and;
1410 else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001411 errmsg = N_(e_unknown_option);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 goto skip;
1413 }
1414
1415 if (opt_idx >= 0)
1416 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001417 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001419 // Only give an error message when requesting the value of
1420 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1422 && (!(options[opt_idx].flags & P_BOOL)
1423 || nextchar == '?'))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001424 errmsg = N_(e_option_not_supported);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 goto skip;
1426 }
1427
1428 flags = options[opt_idx].flags;
1429 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1430 }
1431 else
1432 {
1433 flags = P_STRING;
1434 if (key < 0)
1435 {
1436 key_name[0] = KEY2TERMCAP0(key);
1437 key_name[1] = KEY2TERMCAP1(key);
1438 }
1439 else
1440 {
1441 key_name[0] = KS_KEY;
1442 key_name[1] = (key & 0xff);
1443 }
1444 }
1445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001446 // Skip all options that are not window-local (used when showing
1447 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001448 if ((opt_flags & OPT_WINONLY)
1449 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1450 goto skip;
1451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001452 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001453 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1454 && options[opt_idx].var == VAR_WIN)
1455 goto skip;
1456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001457 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001458 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001460 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001461 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001462 errmsg = N_(e_not_allowed_in_modeline);
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001463 goto skip;
1464 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001465 if ((flags & P_MLE) && !p_mle)
1466 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001467 errmsg = N_(e_not_allowed_in_modeline_when_modelineexpr_is_off);
Bram Moolenaar110289e2019-05-23 15:38:06 +02001468 goto skip;
1469 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001470#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 // In diff mode some options are overruled. This avoids that
1472 // 'foldmethod' becomes "marker" instead of "diff" and that
1473 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001474 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001476 && (
1477#ifdef FEAT_FOLDING
1478 options[opt_idx].indir == PV_FDM ||
1479#endif
1480 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001481 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484
1485#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001486 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001487 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001489 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 goto skip;
1491 }
1492#endif
1493
1494 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1495 {
1496 arg += len;
1497 cp_val = p_cp;
1498 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1499 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001500 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 cp_val = FALSE;
1503 arg += 3;
1504 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001505 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
1507 cp_val = TRUE;
1508 arg += 2;
1509 }
1510 }
1511 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001512 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001514 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 goto skip;
1516 }
1517 }
1518
1519 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001520 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001521 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
1523 if (nextchar == '?'
1524 || (prefix == 1
1525 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1526 && !(flags & P_BOOL)))
1527 {
1528 /*
1529 * print value
1530 */
1531 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001532 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 else
1534 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001535 gotocmdline(TRUE); // cursor at status line
1536 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 if (opt_idx >= 0)
1539 {
1540 showoneopt(&options[opt_idx], opt_flags);
1541#ifdef FEAT_EVAL
1542 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001544 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001545 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001546 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001547 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001548 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001549 (int)options[opt_idx].indir & PV_MASK]);
1550 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001551 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001552 (int)options[opt_idx].indir & PV_MASK]);
1553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554#endif
1555 }
1556 else
1557 {
1558 char_u *p;
1559
1560 p = find_termcode(key_name);
1561 if (p == NULL)
1562 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001563 errmsg = N_(e_key_code_not_set);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 goto skip;
1565 }
1566 else
1567 (void)show_one_termcode(key_name, p, TRUE);
1568 }
1569 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001570 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001571 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
1573 else
1574 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001575 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001576 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001578 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 if (nextchar == '=' || nextchar == ':')
1581 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001582 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 goto skip;
1584 }
1585
1586 /*
1587 * ":set opt!": invert
1588 * ":set opt&": reset to default value
1589 * ":set opt<": reset to global value
1590 */
1591 if (nextchar == '!')
1592 value = *(int *)(varp) ^ 1;
1593 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001594 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 ((flags & P_VI_DEF) || cp_val)
1596 ? VI_DEFAULT : VIM_DEFAULT];
1597 else if (nextchar == '<')
1598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001599 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if ((int *)varp == &curbuf->b_p_ar
1601 && opt_flags == OPT_LOCAL)
1602 value = -1;
1603 else
1604 value = *(int *)get_varp_scope(&(options[opt_idx]),
1605 OPT_GLOBAL);
1606 }
1607 else
1608 {
1609 /*
1610 * ":set invopt": invert
1611 * ":set opt" or ":set noopt": set or reset
1612 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001613 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001615 errmsg = e_trailing_characters;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 goto skip;
1617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001618 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 value = *(int *)(varp) ^ 1;
1620 else
1621 value = prefix;
1622 }
1623
1624 errmsg = set_bool_option(opt_idx, varp, (int)value,
1625 opt_flags);
1626 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001627 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {
1629 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1630 || prefix != 1)
1631 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001632 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 goto skip;
1634 }
1635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001636 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 /*
1639 * Different ways to set a number option:
1640 * & set to default value
1641 * < set to global value
1642 * <xx> accept special key codes for 'wildchar'
1643 * c accept any non-digit for 'wildchar'
1644 * [-]0-9 set number
1645 * other error
1646 */
1647 ++arg;
1648 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001649 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 ((flags & P_VI_DEF) || cp_val)
1651 ? VI_DEFAULT : VIM_DEFAULT];
1652 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001653 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001654 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1655 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001656 if ((long *)varp == &curbuf->b_p_ul
1657 && opt_flags == OPT_LOCAL)
1658 value = NO_LOCAL_UNDOLEVEL;
1659 else
1660 value = *(long *)get_varp_scope(
1661 &(options[opt_idx]), OPT_GLOBAL);
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 else if (((long *)varp == &p_wc
1664 || (long *)varp == &p_wcm)
1665 && (*arg == '<'
1666 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001667 || (*arg != NUL
1668 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 && !VIM_ISDIGIT(*arg))))
1670 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001671 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (value == 0 && (long *)varp != &p_wcm)
1673 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001674 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 goto skip;
1676 }
1677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001680 // Allow negative (for 'undolevels'), octal and
1681 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001682 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001683 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001684 if (i == 0 || (arg[i] != NUL
1685 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001687 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 goto skip;
1689 }
1690 }
1691 else
1692 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001693 errmsg = N_(e_number_required_after_equal);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 goto skip;
1695 }
1696
1697 if (adding)
1698 value = *(long *)varp + value;
1699 if (prepending)
1700 value = *(long *)varp * value;
1701 if (removing)
1702 value = *(long *)varp - value;
1703 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001704 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001706 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001708 char_u *save_arg = NULL;
1709 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001710 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001711 char_u *newval;
1712 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001713 char_u *origval_l = NULL;
1714 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001715#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001716 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001717 char_u *saved_origval_l = NULL;
1718 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001719 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001720#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001721 unsigned newlen;
1722 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001723 int new_value_alloced; // new string option
1724 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001726 // When using ":set opt=val" for a global option
1727 // with a local value the local value will be
1728 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001730 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 varp = options[opt_idx].var;
1732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001733 // The old value is kept until we are sure that the
1734 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001736
Bram Moolenaard7c96872019-06-15 17:12:48 +02001737 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1738 {
1739 origval_l = *(char_u **)get_varp_scope(
1740 &(options[opt_idx]), OPT_LOCAL);
1741 origval_g = *(char_u **)get_varp_scope(
1742 &(options[opt_idx]), OPT_GLOBAL);
1743
1744 // A global-local string option might have an empty
1745 // option as value to indicate that the global
1746 // value should be used.
1747 if (((int)options[opt_idx].indir & PV_BOTH)
1748 && origval_l == empty_option)
1749 origval_l = origval_g;
1750 }
1751
1752 // When setting the local value of a global
1753 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001754 if (((int)options[opt_idx].indir & PV_BOTH)
1755 && (opt_flags & OPT_LOCAL))
1756 origval = *(char_u **)get_varp(
1757 &options[opt_idx]);
1758 else
1759 origval = oldval;
1760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001761 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 {
1763 newval = options[opt_idx].def_val[
1764 ((flags & P_VI_DEF) || cp_val)
1765 ? VI_DEFAULT : VIM_DEFAULT];
1766 if ((char_u **)varp == &p_bg)
1767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001768 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#ifdef FEAT_GUI
1770 if (gui.in_use)
1771 newval = gui_bg_default();
1772 else
1773#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001774 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001776 else if ((char_u **)varp == &p_fencs && enc_utf8)
1777 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001779 // expand environment variables and ~ since the
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001780 // default value was already expanded, only
1781 // required when an environment variable was set
1782 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (newval == NULL)
1784 newval = empty_option;
1785 else
1786 {
1787 s = option_expand(opt_idx, newval);
1788 if (s == NULL)
1789 s = newval;
1790 newval = vim_strsave(s);
1791 }
1792 new_value_alloced = TRUE;
1793 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001794 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796 newval = vim_strsave(*(char_u **)get_varp_scope(
1797 &(options[opt_idx]), OPT_GLOBAL));
1798 new_value_alloced = TRUE;
1799 }
1800 else
1801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001802 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
1804 /*
1805 * Set 'keywordprg' to ":help" if an empty
1806 * value was passed to :set by the user.
1807 * Misuse errbuf[] for the resulting string.
1808 */
1809 if (varp == (char_u *)&p_kp
1810 && (*arg == NUL || *arg == ' '))
1811 {
1812 STRCPY(errbuf, ":help");
1813 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001814 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001817 * Convert 'backspace' number to string, for
1818 * adding, prepending and removing string.
1819 */
1820 else if (varp == (char_u *)&p_bs
1821 && VIM_ISDIGIT(**(char_u **)varp))
1822 {
1823 i = getdigits((char_u **)varp);
1824 switch (i)
1825 {
1826 case 0:
1827 *(char_u **)varp = empty_option;
1828 break;
1829 case 1:
1830 *(char_u **)varp = vim_strsave(
1831 (char_u *)"indent,eol");
1832 break;
1833 case 2:
1834 *(char_u **)varp = vim_strsave(
1835 (char_u *)"indent,eol,start");
1836 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001837 case 3:
1838 *(char_u **)varp = vim_strsave(
1839 (char_u *)"indent,eol,nostop");
1840 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001841 }
1842 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001843 if (origval == oldval)
1844 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001845 if (origval_l == oldval)
1846 origval_l = *(char_u **)varp;
1847 if (origval_g == oldval)
1848 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001849 oldval = *(char_u **)varp;
1850 }
1851 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 * Convert 'whichwrap' number to string, for
1853 * backwards compatibility with Vim 3.0.
1854 * Misuse errbuf[] for the resulting string.
1855 */
1856 else if (varp == (char_u *)&p_ww
1857 && VIM_ISDIGIT(*arg))
1858 {
1859 *errbuf = NUL;
1860 i = getdigits(&arg);
1861 if (i & 1)
1862 STRCAT(errbuf, "b,");
1863 if (i & 2)
1864 STRCAT(errbuf, "s,");
1865 if (i & 4)
1866 STRCAT(errbuf, "h,l,");
1867 if (i & 8)
1868 STRCAT(errbuf, "<,>,");
1869 if (i & 16)
1870 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001871 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 errbuf[STRLEN(errbuf) - 1] = NUL;
1873 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001874 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 /*
1877 * Remove '>' before 'dir' and 'bdir', for
1878 * backwards compatibility with version 3.0
1879 */
1880 else if ( *arg == '>'
1881 && (varp == (char_u *)&p_dir
1882 || varp == (char_u *)&p_bdir))
1883 {
1884 ++arg;
1885 }
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 /*
1888 * Copy the new string into allocated memory.
1889 * Can't use set_string_option_direct(), because
1890 * we need to remove the backslashes.
1891 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001892 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 newlen = (unsigned)STRLEN(arg) + 1;
1894 if (adding || prepending || removing)
1895 newlen += (unsigned)STRLEN(origval) + 1;
1896 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001897 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 break;
1899 s = newval;
1900
1901 /*
1902 * Copy the string, skip over escaped chars.
1903 * For MS-DOS and WIN32 backslashes before normal
1904 * file name characters are not removed, and keep
1905 * backslash at start, for "\\machine\path", but
1906 * do remove it for "\\\\machine\\path".
1907 * The reverse is found in ExpandOldSetting().
1908 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001909 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 if (*arg == '\\' && arg[1] != NUL
1912#ifdef BACKSLASH_IN_FILENAME
1913 && !((flags & P_EXPAND)
1914 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001915 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 && (arg[1] != '\\'
1917 || (s == newval
1918 && arg[2] != '\\')))
1919#endif
1920 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001921 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001923 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001925 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 mch_memmove(s, arg, (size_t)i);
1927 arg += i;
1928 s += i;
1929 }
1930 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 *s++ = *arg++;
1932 }
1933 *s = NUL;
1934
1935 /*
1936 * Expand environment variables and ~.
1937 * Don't do it when adding without inserting a
1938 * comma.
1939 */
1940 if (!(adding || prepending || removing)
1941 || (flags & P_COMMA))
1942 {
1943 s = option_expand(opt_idx, newval);
1944 if (s != NULL)
1945 {
1946 vim_free(newval);
1947 newlen = (unsigned)STRLEN(s) + 1;
1948 if (adding || prepending || removing)
1949 newlen += (unsigned)STRLEN(origval) + 1;
1950 newval = alloc(newlen);
1951 if (newval == NULL)
1952 break;
1953 STRCPY(newval, s);
1954 }
1955 }
1956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001957 // locate newval[] in origval[] when removing it
1958 // and when adding to avoid duplicates
1959 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (removing || (flags & P_NODUP))
1961 {
1962 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001963 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001965 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001966 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 prepending = FALSE;
1969 adding = FALSE;
1970 STRCPY(newval, origval);
1971 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001972
1973 // if no duplicate, move pointer to end of
1974 // original value
1975 if (s == NULL)
1976 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001979 // concatenate the two strings; add a ',' if
1980 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (adding || prepending)
1982 {
1983 comma = ((flags & P_COMMA) && *origval != NUL
1984 && *newval != NUL);
1985 if (adding)
1986 {
1987 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001988 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001989 if (comma && i > 1
1990 && (flags & P_ONECOMMA) == P_ONECOMMA
1991 && origval[i - 1] == ','
1992 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001993 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 mch_memmove(newval + i + comma, newval,
1995 STRLEN(newval) + 1);
1996 mch_memmove(newval, origval, (size_t)i);
1997 }
1998 else
1999 {
2000 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002001 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 if (comma)
2004 newval[i] = ',';
2005 }
2006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002007 // Remove newval[] from origval[]. (Note: "i" has
2008 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (removing)
2010 {
2011 STRCPY(newval, origval);
2012 if (*s)
2013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002014 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (flags & P_COMMA)
2016 {
2017 if (s == origval)
2018 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002019 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 if (s[i] == ',')
2021 ++i;
2022 }
2023 else
2024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002025 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 --s;
2027 ++i;
2028 }
2029 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002030 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 }
2033
2034 if (flags & P_FLAGLIST)
2035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002037 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002038 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002039 // if options have P_FLAGLIST and
2040 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002041 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002043 if (*s != ',' && *(s + 1) == ','
2044 && vim_strchr(s + 2, *s) != NULL)
2045 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002046 // Remove the duplicated value and
2047 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002048 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002049 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002052 else
2053 {
2054 if ((!(flags & P_COMMA) || *s != ',')
2055 && vim_strchr(s + 1, *s) != NULL)
2056 {
2057 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002058 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002059 }
2060 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002061 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002065 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 arg = save_arg;
2067 new_value_alloced = TRUE;
2068 }
2069
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002070 /*
2071 * Set the new value.
2072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 *(char_u **)(varp) = newval;
2074
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002075#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002076 if (!starting
2077# ifdef FEAT_CRYPT
2078 && options[opt_idx].indir != PV_KEY
2079# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002080 && origval != NULL && newval != NULL)
2081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002082 // origval may be freed by
2083 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002084 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002085 // newval (and varp) may become invalid if the
2086 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002087 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002088 if (origval_l != NULL)
2089 saved_origval_l = vim_strsave(origval_l);
2090 if (origval_g != NULL)
2091 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002092 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002093#endif
2094
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002095 {
2096 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002097 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002098
2099 // When an option is set in the sandbox, from a
2100 // modeline or in secure mode, then deal with side
2101 // effects in secure mode. Also when the value was
2102 // set with the P_INSECURE flag and is not
2103 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002104 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002105#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002106 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002107#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002108 || (!value_is_replaced && (*p & P_INSECURE)))
2109 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002110
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002111 // Handle side effects, and set the global value
2112 // for ":set" on local options. Note: when setting
2113 // 'syntax' or 'filetype' autocommands may be
2114 // triggered that can cause havoc.
2115 errmsg = did_set_string_option(
2116 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002117 new_value_alloced, oldval, errbuf,
2118 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002119
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002120 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002123#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002124 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002125 trigger_optionsset_string(
2126 opt_idx, opt_flags, saved_origval,
2127 saved_origval_l, saved_origval_g,
2128 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002129 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002130 vim_free(saved_origval_l);
2131 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002132 vim_free(saved_newval);
2133#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002134 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (errmsg != NULL)
2136 goto skip;
2137 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002138 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 char_u *p;
2141
2142 if (nextchar == '&')
2143 {
2144 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002145 errmsg = N_(e_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147 else
2148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002149 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002150 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (*p == '\\' && p[1] != NUL)
2152 ++p;
2153 nextchar = *p;
2154 *p = NUL;
2155 add_termcode(key_name, arg, FALSE);
2156 *p = nextchar;
2157 }
2158 if (full_screen)
2159 ttest(FALSE);
2160 redraw_all_later(CLEAR);
2161 }
2162 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002163
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002165 did_set_option(
2166 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 }
2168
2169skip:
2170 /*
2171 * Advance to next argument.
2172 * - skip until a blank found, taking care of backslashes
2173 * - skip blanks
2174 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2175 */
2176 for (i = 0; i < 2 ; ++i)
2177 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002178 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (*arg++ == '\\' && *arg != NUL)
2180 ++arg;
2181 arg = skipwhite(arg);
2182 if (*arg != '=')
2183 break;
2184 }
2185 }
2186
2187 if (errmsg != NULL)
2188 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002189 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002190 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (i + (arg - startarg) < IOSIZE)
2192 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 STRCAT(IObuff, ": ");
2195 mch_memmove(IObuff + i, startarg, (arg - startarg));
2196 IObuff[i + (arg - startarg)] = NUL;
2197 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002198 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 trans_characters(IObuff, IOSIZE);
2200
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 ++no_wait_return; // wait_return done later
2202 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 --no_wait_return;
2204
2205 return FAIL;
2206 }
2207
2208 arg = skipwhite(arg);
2209 }
2210
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211theend:
2212 if (silent_mode && did_show)
2213 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002216 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002217 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 out_flush();
2220 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002221 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 }
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 return OK;
2225}
2226
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002227/*
2228 * Call this when an option has been given a new value through a user command.
2229 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2230 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002231 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002232did_set_option(
2233 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002234 int opt_flags, // possibly with OPT_MODELINE
2235 int new_value, // value was replaced completely
2236 int value_checked) // value was checked to be safe, no need to set the
2237 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002238{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002239 long_u *p;
2240
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002241 options[opt_idx].flags |= P_WAS_SET;
2242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002243 // When an option is set in the sandbox, from a modeline or in secure mode
2244 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2245 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002246 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002247 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002248#ifdef HAVE_SANDBOX
2249 || sandbox != 0
2250#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002251 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002252 *p = *p | P_INSECURE;
2253 else if (new_value)
2254 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002255}
2256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257/*
2258 * Convert a key name or string into a key value.
2259 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002260 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002262 int
2263string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002266 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (*arg == '^')
2268 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002269 if (multi_byte)
2270 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 return *arg;
2272}
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274/*
2275 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2276 * maketitle() to create and display it.
2277 * When switching the title or icon off, call mch_restore_title() to get
2278 * the old value back.
2279 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002280 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002281did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 if (starting != NO_SCREEN
2284#ifdef FEAT_GUI
2285 && !gui.starting
2286#endif
2287 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291/*
2292 * set_options_bin - called when 'bin' changes value.
2293 */
2294 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002295set_options_bin(
2296 int oldval,
2297 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002298 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
2300 /*
2301 * The option values that are changed when 'bin' changes are
2302 * copied when 'bin is set and restored when 'bin' is reset.
2303 */
2304 if (newval)
2305 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002306 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 if (!(opt_flags & OPT_GLOBAL))
2309 {
2310 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2311 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2312 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2313 curbuf->b_p_et_nobin = curbuf->b_p_et;
2314 }
2315 if (!(opt_flags & OPT_LOCAL))
2316 {
2317 p_tw_nobin = p_tw;
2318 p_wm_nobin = p_wm;
2319 p_ml_nobin = p_ml;
2320 p_et_nobin = p_et;
2321 }
2322 }
2323
2324 if (!(opt_flags & OPT_GLOBAL))
2325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002326 curbuf->b_p_tw = 0; // no automatic line wrap
2327 curbuf->b_p_wm = 0; // no automatic line wrap
2328 curbuf->b_p_ml = 0; // no modelines
2329 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331 if (!(opt_flags & OPT_LOCAL))
2332 {
2333 p_tw = 0;
2334 p_wm = 0;
2335 p_ml = FALSE;
2336 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 if (!(opt_flags & OPT_GLOBAL))
2343 {
2344 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2345 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2346 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2347 curbuf->b_p_et = curbuf->b_p_et_nobin;
2348 }
2349 if (!(opt_flags & OPT_LOCAL))
2350 {
2351 p_tw = p_tw_nobin;
2352 p_wm = p_wm_nobin;
2353 p_ml = p_ml_nobin;
2354 p_et = p_et_nobin;
2355 }
2356 }
2357}
2358
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359/*
2360 * Expand environment variables for some string options.
2361 * These string options cannot be indirect!
2362 * If "val" is NULL expand the current value of the option.
2363 * Return pointer to NameBuff, or NULL when not expanded.
2364 */
2365 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002366option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002368 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2370 return NULL;
2371
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002372 // If val is longer than MAXPATHL no meaningful expansion can be done,
2373 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (val != NULL && STRLEN(val) > MAXPATHL)
2375 return NULL;
2376
2377 if (val == NULL)
2378 val = *(char_u **)options[opt_idx].var;
2379
2380 /*
2381 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2382 * Escape spaces when expanding 'tags', they are used to separate file
2383 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002384 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 */
2386 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002387 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002388#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002389 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2390#endif
2391 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002392 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 return NULL;
2394
2395 return NameBuff;
2396}
2397
2398/*
2399 * After setting various option values: recompute variables that depend on
2400 * option values.
2401 */
2402 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002403didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002405 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 (void)init_chartab();
2407
Bram Moolenaardac13472019-09-16 21:06:21 +02002408 didset_string_options();
2409
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002410#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002411 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002412 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002413 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002414 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002417 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 (void)check_cedit();
2419#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002420#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002421 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002422 fill_breakat_flags();
2423#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002424 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002425}
2426
2427/*
2428 * More side effects of setting options.
2429 */
2430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002431didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002432{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002433 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002434 (void)highlight_changed();
2435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002436 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002437 check_opt_wim();
2438
Bram Moolenaareed9d462021-02-15 20:38:25 +01002439 // Parse default for 'listchars'.
2440 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2441
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002442 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002443 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002444
2445#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002446 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002447 (void)check_clipboard_option();
2448#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002449#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002450 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002451 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002452 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002453 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455}
2456
2457/*
2458 * Check for string options that are NULL (normally only termcap options).
2459 */
2460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 int opt_idx;
2464
2465 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2466 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2467 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2468}
2469
2470/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002471 * Return the option index found by a pointer into term_strings[].
2472 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002474 int
2475get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002477 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2480 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002481 return opt_idx;
2482 return -1; // cannot happen: didn't find it!
2483}
2484
2485/*
2486 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2487 * Return the option index or -1 if not found.
2488 */
2489 int
2490set_term_option_alloced(char_u **p)
2491{
2492 int opt_idx = get_term_opt_idx(p);
2493
2494 if (opt_idx >= 0)
2495 options[opt_idx].flags |= P_ALLOCED;
2496 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497}
2498
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002499#if defined(FEAT_EVAL) || defined(PROTO)
2500/*
2501 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2502 * Return FALSE when it wasn't.
2503 * Return -1 for an unknown option.
2504 */
2505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002506was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002507{
2508 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002509 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002510
2511 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002512 {
2513 flagp = insecure_flag(idx, opt_flags);
2514 return (*flagp & P_INSECURE) != 0;
2515 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002516 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002517 return -1;
2518}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002519
2520/*
2521 * Get a pointer to the flags used for the P_INSECURE flag of option
2522 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002523 * NOTE: Caller must make sure that "curwin" is set to the window from which
2524 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002525 */
2526 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002527insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002528{
2529 if (opt_flags & OPT_LOCAL)
2530 switch ((int)options[opt_idx].indir)
2531 {
2532#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534#endif
2535#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002536# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002537 case PV_FDE: return &curwin->w_p_fde_flags;
2538 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002539# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002540# ifdef FEAT_BEVAL
2541 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2542# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002543# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002544 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002546 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002547# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002548 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002549# endif
2550#endif
2551 }
2552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002553 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002554 return &options[opt_idx].flags;
2555}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002556#endif
2557
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002558/*
2559 * Redraw the window title and/or tab page text later.
2560 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002561void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002562{
2563 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002564 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002565}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002568 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2569 * characters or characters in "allowed".
2570 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002571 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002572valid_name(char_u *val, char *allowed)
2573{
2574 char_u *s;
2575
2576 for (s = val; *s != NUL; ++s)
2577 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2578 return FALSE;
2579 return TRUE;
2580}
2581
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002582#if defined(FEAT_EVAL) || defined(PROTO)
2583/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002584 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002585 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002586 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002587 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002588set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002589{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002590 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2591 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002592 sctx_T new_script_ctx = script_ctx;
2593
Bram Moolenaar51258742020-05-03 17:19:33 +02002594 // Modeline already has the line number set.
2595 if (!(opt_flags & OPT_MODELINE))
2596 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002598 // Remember where the option was set. For local options need to do that
2599 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002600 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002601 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002602 if (both || (opt_flags & OPT_LOCAL))
2603 {
2604 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002605 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002606 else if (indir & PV_WIN)
Bram Moolenaare70dd112022-01-21 16:31:11 +00002607 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002608 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaare70dd112022-01-21 16:31:11 +00002609 if (both)
2610 // also setting the "all buffers" value
2611 curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
2612 new_script_ctx;
2613 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002614 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002615}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002616
2617/*
Bram Moolenaar5600a702022-01-22 15:09:36 +00002618 * Get the script context of global option "name".
2619 *
2620 */
2621 sctx_T *
2622get_option_sctx(char *name)
2623{
2624 int idx = findoption((char_u *)name);
2625
2626 if (idx >= 0)
2627 return &options[idx].script_ctx;
2628 siemsg("no such option: %s", name);
2629 return NULL;
2630}
2631
2632/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002633 * Set the script_ctx for a termcap option.
2634 * "name" must be the two character code, e.g. "RV".
2635 * When "name" is NULL use "opt_idx".
2636 */
2637 void
2638set_term_option_sctx_idx(char *name, int opt_idx)
2639{
2640 char_u buf[5];
2641 int idx;
2642
2643 if (name == NULL)
2644 idx = opt_idx;
2645 else
2646 {
2647 buf[0] = 't';
2648 buf[1] = '_';
2649 buf[2] = name[0];
2650 buf[3] = name[1];
2651 buf[4] = 0;
2652 idx = findoption(buf);
2653 }
2654 if (idx >= 0)
2655 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2656}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657#endif
2658
Bram Moolenaarf4140482020-02-15 23:06:45 +01002659#if defined(FEAT_EVAL)
2660/*
2661 * Apply the OptionSet autocommand.
2662 */
2663 static void
2664apply_optionset_autocmd(
2665 int opt_idx,
2666 long opt_flags,
2667 long oldval,
2668 long oldval_g,
2669 long newval,
2670 char *errmsg)
2671{
2672 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2673
2674 // Don't do this while starting up, failure or recursively.
2675 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2676 return;
2677
2678 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2679 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2680 oldval_g);
2681 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2682 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2683 (opt_flags & OPT_LOCAL) ? "local" : "global");
2684 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2685 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2686 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2687 if (opt_flags & OPT_LOCAL)
2688 {
2689 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2690 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2691 }
2692 if (opt_flags & OPT_GLOBAL)
2693 {
2694 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2695 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2696 }
2697 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2698 {
2699 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2700 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2701 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2702 }
2703 if (opt_flags & OPT_MODELINE)
2704 {
2705 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2706 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2707 }
2708 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2709 NULL, FALSE, NULL);
2710 reset_v_option_vars();
2711}
2712#endif
2713
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714/*
2715 * Set the value of a boolean option, and take care of side effects.
2716 * Returns NULL for success, or an error message for an error.
2717 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002718 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002719set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 int opt_idx, // index in options[] table
2721 char_u *varp, // pointer to the option variable
2722 int value, // new value
2723 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002726#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002727 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 if ((secure
2732#ifdef HAVE_SANDBOX
2733 || sandbox != 0
2734#endif
2735 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002736 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002738#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002739 // Save the global value before changing anything. This is needed as for
2740 // a global-only option setting the "local value" in fact sets the global
2741 // value (since there is only one value).
2742 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2743 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2744 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002745#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002747 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002750 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002753#ifdef FEAT_GUI
2754 need_mouse_correct = TRUE;
2755#endif
2756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002757 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2759 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2760
2761 /*
2762 * Handle side effects of changing a bool option.
2763 */
2764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
Bram Moolenaar920694c2016-08-21 17:45:02 +02002769#ifdef FEAT_LANGMAP
2770 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002772 p_lnr = !p_lrm;
2773 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002775 p_lrm = !p_lnr;
2776#endif
2777
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002778#ifdef FEAT_SYN_HL
2779 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2780 reset_cursorline();
2781#endif
2782
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002783#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002784 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002785 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002787 // Only take action when the option was set. When reset we do not
2788 // delete the undo file, the option may be set again without making
2789 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002790 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002791 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002792 char_u hash[UNDO_HASH_SIZE];
2793 buf_T *save_curbuf = curbuf;
2794
Bram Moolenaar29323592016-07-24 22:04:11 +02002795 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002796 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // When 'undofile' is set globally: for every buffer, otherwise
2798 // only for the current buffer: Try to read in the undofile,
2799 // if one exists, the buffer wasn't changed and the buffer was
2800 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002801 if ((curbuf == save_curbuf
2802 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2803 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2804 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002805#ifdef FEAT_CRYPT
2806 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2807 continue;
2808#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002809 u_compute_hash(hash);
2810 u_read_undo(NULL, hash, curbuf->b_fname);
2811 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002812 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002813 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002814 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002815 }
2816#endif
2817
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 else if ((int *)varp == &curbuf->b_p_ro)
2819 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002820 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2822 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002825 if (curbuf->b_p_ro)
2826 curbuf->b_did_warn = FALSE;
2827
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002828 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830
Bram Moolenaar9c449722010-07-20 18:44:27 +02002831#ifdef FEAT_GUI
2832 else if ((int *)varp == &p_mh)
2833 {
2834 if (!p_mh)
2835 gui_mch_mousehide(FALSE);
2836 }
2837#endif
2838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002841 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002842# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002844 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2845 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002846 {
2847 curbuf->b_p_ma = FALSE;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002848 return N_(e_cannot_make_terminal_with_running_job_modifiable);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002849 }
2850# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002851 redraw_titles();
2852 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002855 {
2856 redraw_titles();
2857 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002859 else if ((int *)varp == &curbuf->b_p_fixeol)
2860 {
2861 redraw_titles();
2862 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002864 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002865 {
2866 redraw_titles();
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 else if ((int *)varp == &curbuf->b_p_bin)
2871 {
2872 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002873 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2878 {
2879 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2880 NULL, NULL, TRUE, curbuf);
2881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else if ((int *)varp == &curbuf->b_p_swf)
2885 {
2886 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002887 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2890 // buf->b_p_swf
2891 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 else if ((int *)varp == &p_terse)
2896 {
2897 char_u *p;
2898
2899 p = vim_strchr(p_shm, SHM_SEARCH);
2900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 if (p_terse && p == NULL)
2903 {
2904 STRCPY(IObuff, p_shm);
2905 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002906 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002910 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 }
2912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002913 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 else if ((int *)varp == &p_paste)
2915 {
2916 paste_option_changed();
2917 }
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 else if ((int *)varp == &p_im)
2921 {
2922 if (p_im)
2923 {
2924 if ((State & INSERT) == 0)
2925 need_start_insertmode = TRUE;
2926 stop_insert_mode = FALSE;
2927 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002929 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
2931 need_start_insertmode = FALSE;
2932 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002933 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 restart_edit = 0;
2936 }
2937 }
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 else if ((int *)varp == &p_ic && p_hls)
2941 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002942 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944
2945#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 else if ((int *)varp == &p_hls)
2948 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002949 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951#endif
2952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2954 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 else if ((int *)varp == &curwin->w_p_scb)
2956 {
2957 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002960 curwin->w_scbind_pos = curwin->w_topline;
2961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
Bram Moolenaar4033c552017-09-16 20:54:51 +02002964#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else if ((int *)varp == &curwin->w_p_pvw)
2967 {
2968 if (curwin->w_p_pvw)
2969 {
2970 win_T *win;
2971
Bram Moolenaar29323592016-07-24 22:04:11 +02002972 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (win->w_p_pvw && win != curwin)
2974 {
2975 curwin->w_p_pvw = FALSE;
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002976 return N_(e_preview_window_already_exists);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
2978 }
2979 }
2980#endif
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 else if ((int *)varp == &curbuf->b_p_tx)
2984 {
2985 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2986 }
2987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 set_string_option_direct((char_u *)"ffs", -1,
2992 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002993 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
2996 /*
2997 * When 'lisp' option changes include/exclude '-' in
2998 * keyword characters.
2999 */
3000#ifdef FEAT_LISP
3001 else if (varp == (char_u *)&(curbuf->b_p_lisp))
3002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005#endif
3006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003007 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02003008 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003010 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 else if ((int *)varp == &curbuf->b_changed)
3014 {
3015 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003016 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00003017 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
3020
3021#ifdef BACKSLASH_IN_FILENAME
3022 else if ((int *)varp == &p_ssl)
3023 {
3024 if (p_ssl)
3025 {
3026 psepc = '/';
3027 psepcN = '\\';
3028 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030 else
3031 {
3032 psepc = '\\';
3033 psepcN = '/';
3034 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003037 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 buflist_slash_adjust();
3039 alist_slash_adjust();
3040# ifdef FEAT_EVAL
3041 scriptnames_slash_adjust();
3042# endif
3043 }
3044#endif
3045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003046 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 else if ((int *)varp == &curwin->w_p_wrap)
3048 {
3049 if (curwin->w_p_wrap)
3050 curwin->w_leftcol = 0;
3051 }
3052
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 else if ((int *)varp == &p_ea)
3054 {
3055 if (p_ea && !old_value)
3056 win_equal(curwin, FALSE, 0);
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 else if ((int *)varp == &p_wiv)
3060 {
3061 /*
3062 * When 'weirdinvert' changed, set/reset 't_xs'.
3063 * Then set 'weirdinvert' according to value of 't_xs'.
3064 */
3065 if (p_wiv && !old_value)
3066 T_XS = (char_u *)"y";
3067 else if (!p_wiv && old_value)
3068 T_XS = empty_option;
3069 p_wiv = (*T_XS != NUL);
3070 }
3071
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003072#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 else if ((int *)varp == &p_beval)
3074 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003075 if (!balloonEvalForTerm)
3076 {
3077 if (p_beval && !old_value)
3078 gui_mch_enable_beval_area(balloonEval);
3079 else if (!p_beval && old_value)
3080 gui_mch_disable_beval_area(balloonEval);
3081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003083#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003084#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003085 else if ((int *)varp == &p_bevalterm)
3086 {
3087 mch_bevalterm_changed();
3088 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003091#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else if ((int *)varp == &p_acd)
3093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003095 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
3097#endif
3098
3099#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003100 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else if ((int *)varp == &curwin->w_p_diff)
3102 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003103 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003104 diff_buf_adjust(curwin);
3105# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 if (foldmethodIsDiff(curwin))
3107 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110#endif
3111
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003112#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003113 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 else if ((int *)varp == &p_imdisable)
3115 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003116 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 if (p_imdisable)
3118 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003119 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003120 // When the option is set from an autocommand, it may need to take
3121 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003122 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
3124#endif
3125
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003126#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003128 else if ((int *)varp == &curwin->w_p_spell)
3129 {
3130 if (curwin->w_p_spell)
3131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003132 char *errmsg = did_set_spelllang(curwin);
3133
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003134 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003135 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003136 }
3137 }
3138#endif
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#ifdef FEAT_ARABIC
3141 if ((int *)varp == &curwin->w_p_arab)
3142 {
3143 if (curwin->w_p_arab)
3144 {
3145 /*
3146 * 'arabic' is set, handle various sub-settings.
3147 */
3148 if (!p_tbidi)
3149 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003150 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (!curwin->w_p_rl)
3152 {
3153 curwin->w_p_rl = TRUE;
3154 changed_window_setting();
3155 }
3156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (!p_arshape)
3159 {
3160 p_arshape = TRUE;
3161 redraw_later_clear();
3162 }
3163 }
3164
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003165 // Arabic requires a utf-8 encoding, inform the user if it's not
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003166 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003168 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003169 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3170
Bram Moolenaar8820b482017-03-16 17:23:31 +01003171 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003172 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003173#ifdef FEAT_EVAL
3174 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3175#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3184 OPT_LOCAL);
3185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 else
3188 {
3189 /*
3190 * 'arabic' is reset, handle various sub-settings.
3191 */
3192 if (!p_tbidi)
3193 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003194 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (curwin->w_p_rl)
3196 {
3197 curwin->w_p_rl = FALSE;
3198 changed_window_setting();
3199 }
3200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003201 // 'arabicshape' isn't reset, it is a global option and
3202 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // 'delcombine' isn't reset, it is a global option and another
3206 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207
3208# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003209 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 curbuf->b_p_iminsert = B_IMODE_NONE;
3211 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3212# endif
3213 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003214 }
3215
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003216#endif
3217
Bram Moolenaar44826212019-08-22 21:23:20 +02003218#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3219 else if (((int *)varp == &curwin->w_p_nu
3220 || (int *)varp == &curwin->w_p_rnu)
3221 && gui.in_use
3222 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3223 && curbuf->b_signlist != NULL)
3224 {
3225 // If the 'number' or 'relativenumber' options are modified and
3226 // 'signcolumn' is set to 'number', then clear the screen for a full
3227 // refresh. Otherwise the sign icons are not displayed properly in the
3228 // number column. If the 'number' option is set and only the
3229 // 'relativenumber' option is toggled, then don't refresh the screen
3230 // (optimization).
3231 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3232 redraw_all_later(CLEAR);
3233 }
3234#endif
3235
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003236#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003237 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003238 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003239 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003240# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003241 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003242 if (
3243# ifdef VIMDLL
3244 !gui.in_use && !gui.starting &&
3245# endif
3246 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003247 {
3248 p_tgc = 0;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003249 return N_(e_24_bit_colors_are_not_supported_on_this_environment);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003250 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003251 if (is_term_win32())
3252 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003253# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003254# ifdef FEAT_GUI
3255 if (!gui.in_use && !gui.starting)
3256# endif
3257 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003258# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003259 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003260 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003261 {
3262 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003263 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003264 init_highlight(TRUE, FALSE);
3265 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003266# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003267# ifdef FEAT_TERMINAL
3268 term_update_colors_all();
3269 term_update_wincolor_all();
3270# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003271 }
3272#endif
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 /*
3275 * End of handling side effects for bool options.
3276 */
3277
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003278 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 options[opt_idx].flags |= P_WAS_SET;
3281
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003282#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003283 apply_optionset_autocmd(opt_idx, opt_flags,
3284 (long)(old_value ? TRUE : FALSE),
3285 (long)(old_global_value ? TRUE : FALSE),
3286 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003287#endif
3288
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003289 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003290 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003291 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003292 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003293
3294 if ((opt_flags & OPT_NO_REDRAW) == 0)
3295 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297 return NULL;
3298}
3299
3300/*
3301 * Set the value of a number option, and take care of side effects.
3302 * Returns NULL for success, or an error message for an error.
3303 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003304 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003305set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003306 int opt_idx, // index in options[] table
3307 char_u *varp, // pointer to the option variable
3308 long value, // new value
3309 char *errbuf, // buffer for error messages
3310 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003311 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3312 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003314 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003316#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003317 long old_global_value = 0; // only used when setting a local and
3318 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003319#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003320 long old_Rows = Rows; // remember old Rows
3321 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 long *pp = (long *)varp;
3323
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003324 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003325 if ((secure
3326#ifdef HAVE_SANDBOX
3327 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003329 ) && (options[opt_idx].flags & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00003330 return e_not_allowed_here;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003332#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003333 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003334 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003335 // value (since there is only one value).
3336 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003337 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3338 OPT_GLOBAL);
3339#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 *pp = value;
3342#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003343 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003344 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003346#ifdef FEAT_GUI
3347 need_mouse_correct = TRUE;
3348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar14f24742012-08-08 18:01:05 +02003350 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003352 errmsg = e_argument_must_be_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003353#ifdef FEAT_VARTABS
3354 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3355 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3356 ? tabstop_first(curbuf->b_p_vts_array)
3357 : curbuf->b_p_ts;
3358#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362
3363 /*
3364 * Number options that need some action when changed
3365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (pp == &p_wh || pp == &p_hh)
3367 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003368 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (p_wh < 1)
3370 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003371 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 p_wh = 1;
3373 }
3374 if (p_wmh > p_wh)
3375 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003376 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 p_wh = p_wmh;
3378 }
3379 if (p_hh < 0)
3380 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003381 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 p_hh = 0;
3383 }
3384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003385 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003386 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 if (pp == &p_wh && curwin->w_height < p_wh)
3389 win_setheight((int)p_wh);
3390 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3391 win_setheight((int)p_hh);
3392 }
3393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 else if (pp == &p_wmh)
3395 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003396 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (p_wmh < 0)
3398 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003399 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 p_wmh = 0;
3401 }
3402 if (p_wmh > p_wh)
3403 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003404 errmsg = e_winheight_cannot_be_smaller_than_winminheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 p_wmh = p_wh;
3406 }
3407 win_setminheight();
3408 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003409 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003411 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (p_wiw < 1)
3413 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003414 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 p_wiw = 1;
3416 }
3417 if (p_wmw > p_wiw)
3418 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003419 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 p_wiw = p_wmw;
3421 }
3422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003424 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 win_setwidth((int)p_wiw);
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 else if (pp == &p_wmw)
3428 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003429 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (p_wmw < 0)
3431 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003432 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 p_wmw = 0;
3434 }
3435 if (p_wmw > p_wiw)
3436 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003437 errmsg = e_winwidth_cannot_be_smaller_than_winminwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 p_wmw = p_wiw;
3439 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003440 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 else if (pp == &p_ls)
3445 {
3446 last_status(FALSE);
3447 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003449 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003450 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003451 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003452 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455#ifdef FEAT_GUI
3456 else if (pp == &p_linespace)
3457 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // Recompute gui.char_height and resize the Vim window to keep the
3459 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003460 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003461 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
3463#endif
3464
3465#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003466 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 else if (pp == &curwin->w_p_fdl)
3468 {
3469 if (curwin->w_p_fdl < 0)
3470 curwin->w_p_fdl = 0;
3471 newFoldLevel();
3472 }
3473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003474 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 else if (pp == &curwin->w_p_fml)
3476 {
3477 foldUpdateAll(curwin);
3478 }
3479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003480 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 else if (pp == &curwin->w_p_fdn)
3482 {
3483 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3484 foldUpdateAll(curwin);
3485 }
3486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else if (pp == &curwin->w_p_fdc)
3489 {
3490 if (curwin->w_p_fdc < 0)
3491 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003492 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 curwin->w_p_fdc = 0;
3494 }
3495 else if (curwin->w_p_fdc > 12)
3496 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003497 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 curwin->w_p_fdc = 12;
3499 }
3500 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003501#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003503#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003504 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3506 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003507# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (foldmethodIsIndent(curwin))
3509 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003510# endif
3511# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003512 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3513 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003514 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3515 parse_cino(curbuf);
3516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003520 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003521 else if (pp == &p_mco)
3522 {
3523 if (p_mco > MAX_MCO)
3524 p_mco = MAX_MCO;
3525 else if (p_mco < 0)
3526 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003527 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003528 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 else if (pp == &curbuf->b_p_iminsert)
3531 {
3532 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3533 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003534 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 curbuf->b_p_iminsert = B_IMODE_NONE;
3536 }
3537 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003538 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003540#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003541 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 status_redraw_curbuf();
3543#endif
3544 }
3545
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003546#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003547 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003548 else if (pp == &p_imst)
3549 {
3550 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003551 errmsg = e_invalid_argument;
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003552 }
3553#endif
3554
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003555 else if (pp == &p_window)
3556 {
3557 if (p_window < 1)
3558 p_window = 1;
3559 else if (p_window >= Rows)
3560 p_window = Rows - 1;
3561 }
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 else if (pp == &curbuf->b_p_imsearch)
3564 {
3565 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3566 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003567 errmsg = e_invalid_argument;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 curbuf->b_p_imsearch = B_IMODE_NONE;
3569 }
3570 p_imsearch = curbuf->b_p_imsearch;
3571 }
3572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003573 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 else if (pp == &p_titlelen)
3575 {
3576 if (p_titlelen < 0)
3577 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003578 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 p_titlelen = 85;
3580 }
3581 if (starting != NO_SCREEN && old_value != p_titlelen)
3582 need_maketitle = TRUE;
3583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003585 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else if (pp == &p_ch)
3587 {
3588 if (p_ch < 1)
3589 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003590 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 p_ch = 1;
3592 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003593 if (p_ch > Rows - min_rows() + 1)
3594 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003596 // Only compute the new window layout when startup has been
3597 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (p_ch != old_value && full_screen
3599#ifdef FEAT_GUI
3600 && !gui.starting
3601#endif
3602 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003603 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003606 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 else if (pp == &p_uc)
3608 {
3609 if (p_uc < 0)
3610 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003611 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 p_uc = 100;
3613 }
3614 if (p_uc && !old_value)
3615 ml_open_files();
3616 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003617#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003618 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003619 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003620 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003621 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003622 errmsg = e_argument_must_be_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003623 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003624 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003625 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003626 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003627 errmsg = e_invalid_argument;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003628 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003629 }
3630 }
3631#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003632#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003633 else if (pp == &p_mzq)
3634 mzvim_reset_timer();
3635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003637#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003638 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003639 else if (pp == &p_pyx)
3640 {
3641 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003642 errmsg = e_invalid_argument;
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003643 }
3644#endif
3645
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003646 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 else if (pp == &p_ul)
3648 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003649 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003651 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 p_ul = value;
3653 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003654 else if (pp == &curbuf->b_p_ul)
3655 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003657 curbuf->b_p_ul = old_value;
3658 u_sync(TRUE);
3659 curbuf->b_p_ul = value;
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003662#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003663 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003664 else if (pp == &curwin->w_p_nuw)
3665 {
3666 if (curwin->w_p_nuw < 1)
3667 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003668 errmsg = e_argument_must_be_positive;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003669 curwin->w_p_nuw = 1;
3670 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003671 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003672 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003673 errmsg = e_invalid_argument;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003674 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003675 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003676 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003677 }
3678#endif
3679
Bram Moolenaar1a384422010-07-14 19:53:30 +02003680 else if (pp == &curbuf->b_p_tw)
3681 {
3682 if (curbuf->b_p_tw < 0)
3683 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003684 errmsg = e_argument_must_be_positive;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003685 curbuf->b_p_tw = 0;
3686 }
3687#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003688 {
3689 win_T *wp;
3690 tabpage_T *tp;
3691
3692 FOR_ALL_TAB_WINDOWS(tp, wp)
3693 check_colorcolumn(wp);
3694 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003695#endif
3696 }
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 /*
3699 * Check the bounds for numeric options here
3700 */
3701 if (Rows < min_rows() && full_screen)
3702 {
3703 if (errbuf != NULL)
3704 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003705 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003706 _(e_need_at_least_nr_lines), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 errmsg = errbuf;
3708 }
3709 Rows = min_rows();
3710 }
3711 if (Columns < MIN_COLUMNS && full_screen)
3712 {
3713 if (errbuf != NULL)
3714 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003715 vim_snprintf(errbuf, errbuflen,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00003716 _(e_need_at_least_nr_columns), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 errmsg = errbuf;
3718 }
3719 Columns = MIN_COLUMNS;
3720 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003721 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 /*
3724 * If the screen (shell) height has been changed, assume it is the
3725 * physical screenheight.
3726 */
3727 if (old_Rows != Rows || old_Columns != Columns)
3728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (updating_screen)
3731 *pp = old_value;
3732 else if (full_screen
3733#ifdef FEAT_GUI
3734 && !gui.starting
3735#endif
3736 )
3737 set_shellsize((int)Columns, (int)Rows, TRUE);
3738 else
3739 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003740 // Postpone the resizing; check the size and cmdline position for
3741 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 check_shellsize();
3743 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3744 cmdline_row = Rows - p_ch;
3745 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003746 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003747 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (curbuf->b_p_ts <= 0)
3751 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003752 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 curbuf->b_p_ts = 8;
3754 }
Bram Moolenaar652dee42022-01-28 20:47:49 +00003755 else if (curbuf->b_p_ts > TABSTOP_MAX)
3756 {
3757 errmsg = e_invalid_argument;
3758 curbuf->b_p_ts = 8;
3759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (p_tm < 0)
3761 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003762 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 p_tm = 0;
3764 }
3765 if ((curwin->w_p_scr <= 0
3766 || (curwin->w_p_scr > curwin->w_height
3767 && curwin->w_height > 0))
3768 && full_screen)
3769 {
3770 if (pp == &(curwin->w_p_scr))
3771 {
3772 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003773 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 win_comp_scroll(curwin);
3775 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003776 // If 'scroll' became invalid because of a side effect silently adjust
3777 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 else if (curwin->w_p_scr <= 0)
3779 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003780 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 curwin->w_p_scr = curwin->w_height;
3782 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003783 if (p_hi < 0)
3784 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003785 errmsg = e_argument_must_be_positive;
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003786 p_hi = 0;
3787 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003788 else if (p_hi > 10000)
3789 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003790 errmsg = e_invalid_argument;
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003791 p_hi = 10000;
3792 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003793 if (p_re < 0 || p_re > 2)
3794 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003795 errmsg = e_invalid_argument;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003796 p_re = 0;
3797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (p_report < 0)
3799 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003800 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 p_report = 1;
3802 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003803 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003805 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 p_sj = Rows / 2;
3807 else
3808 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003809 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 p_sj = 1;
3811 }
3812 }
3813 if (p_so < 0 && full_screen)
3814 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003815 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 p_so = 0;
3817 }
3818 if (p_siso < 0 && full_screen)
3819 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003820 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 p_siso = 0;
3822 }
3823#ifdef FEAT_CMDWIN
3824 if (p_cwh < 1)
3825 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003826 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 p_cwh = 1;
3828 }
3829#endif
3830 if (p_ut < 0)
3831 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003832 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 p_ut = 2000;
3834 }
3835 if (p_ss < 0)
3836 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003837 errmsg = e_argument_must_be_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 p_ss = 0;
3839 }
3840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003841 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3843 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3844
3845 options[opt_idx].flags |= P_WAS_SET;
3846
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003847#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003848 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3849 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003850#endif
3851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003852 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003853 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003854 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003855 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003856 if ((opt_flags & OPT_NO_REDRAW) == 0)
3857 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
3859 return errmsg;
3860}
3861
3862/*
3863 * Called after an option changed: check if something needs to be redrawn.
3864 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003865 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003866check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003868 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003869 int doclear = (flags & P_RCLR) == P_RCLR;
3870 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003872 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
3875 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3876 changed_window_setting();
3877 if (flags & P_RBUF)
3878 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003879 if (flags & P_RWINONLY)
3880 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003881 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 redraw_all_later(CLEAR);
3883 else if (all)
3884 redraw_all_later(NOT_VALID);
3885}
3886
3887/*
3888 * Find index for option 'arg'.
3889 * Return -1 if not found.
3890 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003891 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003892findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 int opt_idx;
3895 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003896 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 int is_term_opt;
3898
3899 /*
3900 * For first call: Initialize the quick-access table.
3901 * It contains the index for the first option that starts with a certain
3902 * letter. There are 26 letters, plus the first "t_" option.
3903 */
3904 if (quick_tab[1] == 0)
3905 {
3906 p = options[0].fullname;
3907 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3908 {
3909 if (s[0] != p[0])
3910 {
3911 if (s[0] == 't' && s[1] == '_')
3912 quick_tab[26] = opt_idx;
3913 else
3914 quick_tab[CharOrdLow(s[0])] = opt_idx;
3915 }
3916 p = s;
3917 }
3918 }
3919
3920 /*
3921 * Check for name starting with an illegal character.
3922 */
3923#ifdef EBCDIC
3924 if (!islower(arg[0]))
3925#else
3926 if (arg[0] < 'a' || arg[0] > 'z')
3927#endif
3928 return -1;
3929
3930 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3931 if (is_term_opt)
3932 opt_idx = quick_tab[26];
3933 else
3934 opt_idx = quick_tab[CharOrdLow(arg[0])];
3935 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3936 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939 }
3940 if (s == NULL && !is_term_opt)
3941 {
3942 opt_idx = quick_tab[CharOrdLow(arg[0])];
3943 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3944 {
3945 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003946 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948 s = NULL;
3949 }
3950 }
3951 if (s == NULL)
3952 opt_idx = -1;
3953 return opt_idx;
3954}
3955
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003956#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957/*
3958 * Get the value for an option.
3959 *
3960 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003961 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003962 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003963 * String option: gov_string, *stringval gets allocated string.
3964 * Hidden Number option: gov_hidden_number.
3965 * Hidden Toggle option: gov_hidden_bool.
3966 * Hidden String option: gov_hidden_string.
3967 * Unknown option: gov_unknown.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003968 *
3969 * "flagsp" (if not NULL) is set to the option flags (P_xxxx).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003971 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003972get_option_value(
3973 char_u *name,
3974 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003975 char_u **stringval, // NULL when only checking existence
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00003976 int *flagsp,
3977 int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int opt_idx;
3980 char_u *varp;
3981
3982 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003983 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003984 {
3985 int key;
3986
3987 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003988 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003989 {
3990 char_u key_name[2];
3991 char_u *p;
3992
Bram Moolenaar10c75c42021-12-28 20:53:30 +00003993 if (flagsp != NULL)
3994 *flagsp = 0; // terminal option has no flags
3995
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003996 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003997 if (key < 0)
3998 {
3999 key_name[0] = KEY2TERMCAP0(key);
4000 key_name[1] = KEY2TERMCAP1(key);
4001 }
4002 else
4003 {
4004 key_name[0] = KS_KEY;
4005 key_name[1] = (key & 0xff);
4006 }
4007 p = find_termcode(key_name);
4008 if (p != NULL)
4009 {
4010 if (stringval != NULL)
4011 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004012 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01004013 }
4014 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004015 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01004016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00004018 varp = get_varp_scope(&(options[opt_idx]), scope);
4019
4020 if (flagsp != NULL)
4021 // Return the P_xxxx option flags.
4022 *flagsp = options[opt_idx].flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 if (options[opt_idx].flags & P_STRING)
4025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004026 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004027 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (stringval != NULL)
4029 {
4030#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004031 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00004032 if ((char_u **)varp == &curbuf->b_p_key
4033 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 *stringval = vim_strsave((char_u *)"*****");
4035 else
4036#endif
4037 *stringval = vim_strsave(*(char_u **)(varp));
4038 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004039 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004042 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004043 return (options[opt_idx].flags & P_NUM)
4044 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (options[opt_idx].flags & P_NUM)
4046 *numval = *(long *)varp;
4047 else
4048 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004049 // Special case: 'modified' is b_changed, but we also want to consider
4050 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if ((int *)varp == &curbuf->b_changed)
4052 *numval = curbufIsChanged();
4053 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004054 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004056 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057}
4058#endif
4059
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004060#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004061/*
4062 * Returns the option attributes and its value. Unlike the above function it
4063 * will return either global value or local value of the option depending on
4064 * what was requested, but it will never return global value if it was
4065 * requested to return local one and vice versa. Neither it will return
4066 * buffer-local value if it was requested to return window-local one.
4067 *
4068 * Pretends that option is absent if it is not present in the requested scope
4069 * (i.e. has no global, window-local or buffer-local value depending on
4070 * opt_type). Uses
4071 *
4072 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004073 * 0 hidden or unknown option, also option that does not have requested
4074 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004075 * see SOPT_* in vim.h for other flags
4076 *
4077 * Possible opt_type values: see SREQ_* in vim.h
4078 */
4079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004080get_option_value_strict(
4081 char_u *name,
4082 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004083 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004084 int opt_type,
4085 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004086{
4087 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004088 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004089 struct vimoption *p;
4090 int r = 0;
4091
4092 opt_idx = findoption(name);
4093 if (opt_idx < 0)
4094 return 0;
4095
4096 p = &(options[opt_idx]);
4097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004098 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004099 if (p->var == NULL)
4100 return 0;
4101
4102 if (p->flags & P_BOOL)
4103 r |= SOPT_BOOL;
4104 else if (p->flags & P_NUM)
4105 r |= SOPT_NUM;
4106 else if (p->flags & P_STRING)
4107 r |= SOPT_STRING;
4108
4109 if (p->indir == PV_NONE)
4110 {
4111 if (opt_type == SREQ_GLOBAL)
4112 r |= SOPT_GLOBAL;
4113 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004114 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004115 }
4116 else
4117 {
4118 if (p->indir & PV_BOTH)
4119 r |= SOPT_GLOBAL;
4120 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004121 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004122
4123 if (p->indir & PV_WIN)
4124 {
4125 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004127 else
4128 r |= SOPT_WIN;
4129 }
4130 else if (p->indir & PV_BUF)
4131 {
4132 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004133 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004134 else
4135 r |= SOPT_BUF;
4136 }
4137 }
4138
4139 if (stringval == NULL)
4140 return r;
4141
4142 if (opt_type == SREQ_GLOBAL)
4143 varp = p->var;
4144 else
4145 {
4146 if (opt_type == SREQ_BUF)
4147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004148 // Special case: 'modified' is b_changed, but we also want to
4149 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004150 if (p->indir == PV_MOD)
4151 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004152 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004153 varp = NULL;
4154 }
4155#ifdef FEAT_CRYPT
4156 else if (p->indir == PV_KEY)
4157 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004158 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004159 *stringval = NULL;
4160 varp = NULL;
4161 }
4162#endif
4163 else
4164 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004165 buf_T *save_curbuf = curbuf;
4166
4167 // only getting a pointer, no need to use aucmd_prepbuf()
4168 curbuf = (buf_T *)from;
4169 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004170 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004171 curbuf = save_curbuf;
4172 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004173 }
4174 }
4175 else if (opt_type == SREQ_WIN)
4176 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004177 win_T *save_curwin = curwin;
4178
4179 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004180 curbuf = curwin->w_buffer;
4181 varp = get_varp(p);
4182 curwin = save_curwin;
4183 curbuf = curwin->w_buffer;
4184 }
4185 if (varp == p->var)
4186 return (r | SOPT_UNSET);
4187 }
4188
4189 if (varp != NULL)
4190 {
4191 if (p->flags & P_STRING)
4192 *stringval = vim_strsave(*(char_u **)(varp));
4193 else if (p->flags & P_NUM)
4194 *numval = *(long *) varp;
4195 else
4196 *numval = *(int *)varp;
4197 }
4198
4199 return r;
4200}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004201
4202/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004203 * Iterate over options. First argument is a pointer to a pointer to a
4204 * structure inside options[] array, second is option type like in the above
4205 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004206 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004207 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004208 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004209 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004210 * first argument to NULL.
4211 *
4212 * Returns full option name for current option on each call.
4213 */
4214 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004215option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004216{
4217 struct vimoption *ret = NULL;
4218 do
4219 {
4220 if (*option == NULL)
4221 *option = (void *) options;
4222 else if (((struct vimoption *) (*option))->fullname == NULL)
4223 {
4224 *option = NULL;
4225 return NULL;
4226 }
4227 else
4228 *option = (void *) (((struct vimoption *) (*option)) + 1);
4229
4230 ret = ((struct vimoption *) (*option));
4231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004232 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004233 if (ret->var == NULL)
4234 {
4235 ret = NULL;
4236 continue;
4237 }
4238
4239 switch (opt_type)
4240 {
4241 case SREQ_GLOBAL:
4242 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4243 ret = NULL;
4244 break;
4245 case SREQ_BUF:
4246 if (!(ret->indir & PV_BUF))
4247 ret = NULL;
4248 break;
4249 case SREQ_WIN:
4250 if (!(ret->indir & PV_WIN))
4251 ret = NULL;
4252 break;
4253 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004254 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004255 return NULL;
4256 }
4257 }
4258 while (ret == NULL);
4259
4260 return (char_u *)ret->fullname;
4261}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004262#endif
4263
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004265 * Return the flags for the option at 'opt_idx'.
4266 */
4267 long_u
4268get_option_flags(int opt_idx)
4269{
4270 return options[opt_idx].flags;
4271}
4272
4273/*
4274 * Set a flag for the option at 'opt_idx'.
4275 */
4276 void
4277set_option_flag(int opt_idx, long_u flag)
4278{
4279 options[opt_idx].flags |= flag;
4280}
4281
4282/*
4283 * Clear a flag for the option at 'opt_idx'.
4284 */
4285 void
4286clear_option_flag(int opt_idx, long_u flag)
4287{
4288 options[opt_idx].flags &= ~flag;
4289}
4290
4291/*
4292 * Returns TRUE if the option at 'opt_idx' is a global option
4293 */
4294 int
4295is_global_option(int opt_idx)
4296{
4297 return options[opt_idx].indir == PV_NONE;
4298}
4299
4300/*
4301 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4302 * local value.
4303 */
4304 int
4305is_global_local_option(int opt_idx)
4306{
4307 return options[opt_idx].indir & PV_BOTH;
4308}
4309
4310/*
4311 * Returns TRUE if the option at 'opt_idx' is a window-local option
4312 */
4313 int
4314is_window_local_option(int opt_idx)
4315{
4316 return options[opt_idx].var == VAR_WIN;
4317}
4318
4319/*
4320 * Returns TRUE if the option at 'opt_idx' is a hidden option
4321 */
4322 int
4323is_hidden_option(int opt_idx)
4324{
4325 return options[opt_idx].var == NULL;
4326}
4327
4328#if defined(FEAT_CRYPT) || defined(PROTO)
4329/*
4330 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4331 */
4332 int
4333is_crypt_key_option(int opt_idx)
4334{
4335 return options[opt_idx].indir == PV_KEY;
4336}
4337#endif
4338
4339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 * Set the value of option "name".
4341 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004342 *
4343 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004345 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004346set_option_value(
4347 char_u *name,
4348 long number,
4349 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004350 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
4352 int opt_idx;
4353 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004354 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004357 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004358 {
4359 int key;
4360
4361 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004362 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004363 {
4364 char_u key_name[2];
4365
4366 if (key < 0)
4367 {
4368 key_name[0] = KEY2TERMCAP0(key);
4369 key_name[1] = KEY2TERMCAP1(key);
4370 }
4371 else
4372 {
4373 key_name[0] = KS_KEY;
4374 key_name[1] = (key & 0xff);
4375 }
4376 add_termcode(key_name, string, FALSE);
4377 if (full_screen)
4378 ttest(FALSE);
4379 redraw_all_later(CLEAR);
4380 return NULL;
4381 }
4382
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004383 semsg(_(e_unknown_option_str_2), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 else
4386 {
4387 flags = options[opt_idx].flags;
4388#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004389 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004391 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004392 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004393 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004396 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004397 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else
4399 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004400 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004401 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004403 if (number == 0 && string != NULL)
4404 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004405 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004407 // Either we are given a string or we are setting option
4408 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004409 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004410 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004411 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004413 // There's another character after zeros or the string
4414 // is empty. In both cases, we are trying to set a
4415 // num option using a string.
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00004416 semsg(_(e_number_required_after_str_equal_str),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004417 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004418 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004419
4420 }
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004423 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004424 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004426 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004427 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 }
4430 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004431 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432}
4433
4434/*
4435 * Get the terminal code for a terminal option.
4436 * Returns NULL when not found.
4437 */
4438 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004439get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 int opt_idx;
4442 char_u *varp;
4443
4444 if (tname[0] != 't' || tname[1] != '_' ||
4445 tname[2] == NUL || tname[3] == NUL)
4446 return NULL;
4447 if ((opt_idx = findoption(tname)) >= 0)
4448 {
4449 varp = get_varp(&(options[opt_idx]));
4450 if (varp != NULL)
4451 varp = *(char_u **)(varp);
4452 return varp;
4453 }
4454 return find_termcode(tname + 2);
4455}
4456
4457 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004458get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
4460 int i;
4461
4462 i = findoption((char_u *)"hl");
4463 if (i >= 0)
4464 return options[i].def_val[VI_DEFAULT];
4465 return (char_u *)NULL;
4466}
4467
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004468 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004469get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004470{
4471 int i;
4472
4473 i = findoption((char_u *)"enc");
4474 if (i >= 0)
4475 return options[i].def_val[VI_DEFAULT];
4476 return (char_u *)NULL;
4477}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479/*
4480 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004481 * When "has_lt" is true there is a '<' before "*arg_arg".
4482 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 */
4484 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004485find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004487 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004489 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 /*
4492 * Don't use get_special_key_code() for t_xx, we don't want it to call
4493 * add_termcap_entry().
4494 */
4495 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4496 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004497 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004499 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004501 key = find_special_key(&arg, &modifiers,
4502 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004503 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 key = 0;
4505 }
4506 return key;
4507}
4508
4509/*
4510 * if 'all' == 0: show changed options
4511 * if 'all' == 1: show all normal options
4512 * if 'all' == 2: show all terminal options
4513 */
4514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004515showoptions(
4516 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004517 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
4519 struct vimoption *p;
4520 int col;
4521 int isterm;
4522 char_u *varp;
4523 struct vimoption **items;
4524 int item_count;
4525 int run;
4526 int row, rows;
4527 int cols;
4528 int i;
4529 int len;
4530
4531#define INC 20
4532#define GAP 3
4533
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004534 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (items == NULL)
4536 return;
4537
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004538 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004540 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004542 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004544 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004546 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004549 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 * 1. display the short items
4551 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004552 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 */
4554 for (run = 1; run <= 2 && !got_int; ++run)
4555 {
4556 /*
4557 * collect the items in items[]
4558 */
4559 item_count = 0;
4560 for (p = &options[0]; p->fullname != NULL; p++)
4561 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004562 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004563 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004564 continue;
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 varp = NULL;
4567 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004568 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
4570 if (p->indir != PV_NONE && !isterm)
4571 varp = get_varp_scope(p, opt_flags);
4572 }
4573 else
4574 varp = get_varp(p);
4575 if (varp != NULL
4576 && ((all == 2 && isterm)
4577 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004578 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004580 if (opt_flags & OPT_ONECOLUMN)
4581 len = Columns;
4582 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004583 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 else
4585 {
4586 option_value2string(p, opt_flags);
4587 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4588 }
4589 if ((len <= INC - GAP && run == 1) ||
4590 (len > INC - GAP && run == 2))
4591 items[item_count++] = p;
4592 }
4593 }
4594
4595 /*
4596 * display the items
4597 */
4598 if (run == 1)
4599 {
4600 cols = (Columns + GAP - 3) / INC;
4601 if (cols == 0)
4602 cols = 1;
4603 rows = (item_count + cols - 1) / cols;
4604 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004605 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 rows = item_count;
4607 for (row = 0; row < rows && !got_int; ++row)
4608 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004609 msg_putchar('\n'); // go to next line
4610 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612 col = 0;
4613 for (i = row; i < item_count; i += rows)
4614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 showoneopt(items[i], opt_flags);
4617 col += INC;
4618 }
4619 out_flush();
4620 ui_breakcheck();
4621 }
4622 }
4623 vim_free(items);
4624}
4625
4626/*
4627 * Return TRUE if option "p" has its default value.
4628 */
4629 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004630optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 int dvi;
4633
4634 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004635 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004636 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004638 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004640 // the cast to long is required for Manx C, long_i is
4641 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004642 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004643 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4645}
4646
4647/*
4648 * showoneopt: show the value of one option
4649 * must not be called with a hidden option!
4650 */
4651 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004652showoneopt(
4653 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004656 char_u *varp;
4657 int save_silent = silent_mode;
4658
4659 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004660 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 varp = get_varp_scope(p, opt_flags);
4663
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004664 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4666 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004667 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004669 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004671 msg_puts(" ");
4672 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (!(p->flags & P_BOOL))
4674 {
4675 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004676 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 option_value2string(p, opt_flags);
4678 msg_outtrans(NameBuff);
4679 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004680
4681 silent_mode = save_silent;
4682 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683}
4684
4685/*
4686 * Write modified options as ":set" commands to a file.
4687 *
4688 * There are three values for "opt_flags":
4689 * OPT_GLOBAL: Write global option values and fresh values of
4690 * buffer-local options (used for start of a session
4691 * file).
4692 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4693 * curwin (used for a vimrc file).
4694 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4695 * and local values for window-local options of
4696 * curwin. Local values are also written when at the
4697 * default value, because a modeline or autocommand
4698 * may have set them when doing ":edit file" and the
4699 * user has set them back at the default or fresh
4700 * value.
4701 * When "local_only" is TRUE, don't write fresh
4702 * values, only local values (for ":mkview").
4703 * (fresh value = value used for a new buffer or window for a local option).
4704 *
4705 * Return FAIL on error, OK otherwise.
4706 */
4707 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004708makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004711 char_u *varp; // currently used value
4712 char_u *varp_fresh; // local value
4713 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 char *cmd;
4715 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004716 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
4718 /*
4719 * The options that don't have a default (terminal name, columns, lines)
4720 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004721 * Do the loop over "options[]" twice: once for options with the
4722 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004724 for (pri = 1; pri >= 0; --pri)
4725 {
4726 for (p = &options[0]; !istermoption(p); p++)
4727 if (!(p->flags & P_NO_MKRC)
4728 && !istermoption(p)
4729 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004731 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4733 continue;
4734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4736 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4738 continue;
4739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004740 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004742 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 continue;
4744
Bram Moolenaard23b7142021-04-17 21:04:34 +02004745 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4746 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004747 continue;
4748
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 round = 2;
4750 if (p->indir != PV_NONE)
4751 {
4752 if (p->var == VAR_WIN)
4753 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004754 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (!(opt_flags & OPT_LOCAL))
4756 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004757 // When fresh value of window-local option is not at the
4758 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4760 {
4761 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004762 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 round = 1;
4765 varp_local = varp;
4766 varp = varp_fresh;
4767 }
4768 }
4769 }
4770 }
4771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004772 // Round 1: fresh value for window-local options.
4773 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 for ( ; round <= 2; varp = varp_local, ++round)
4775 {
4776 if (round == 1 || (opt_flags & OPT_GLOBAL))
4777 cmd = "set";
4778 else
4779 cmd = "setlocal";
4780
4781 if (p->flags & P_BOOL)
4782 {
4783 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4784 return FAIL;
4785 }
4786 else if (p->flags & P_NUM)
4787 {
4788 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4789 return FAIL;
4790 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004791 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004793 int do_endif = FALSE;
4794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004795 // Don't set 'syntax' and 'filetype' again if the value is
4796 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004797 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004798#if defined(FEAT_SYN_HL)
4799 p->indir == PV_SYN ||
4800#endif
4801 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
4803 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4804 *(char_u **)(varp)) < 0
4805 || put_eol(fd) < 0)
4806 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004807 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004810 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004812 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 if (put_line(fd, "endif") == FAIL)
4815 return FAIL;
4816 }
4817 }
4818 }
4819 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 return OK;
4822}
4823
4824#if defined(FEAT_FOLDING) || defined(PROTO)
4825/*
4826 * Generate set commands for the local fold options only. Used when
4827 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4828 */
4829 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004830makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004832 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004834 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 == FAIL
4836# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004837 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004839 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 == FAIL
4841 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4842 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4843 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4844 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4845 )
4846 return FAIL;
4847
4848 return OK;
4849}
4850#endif
4851
4852 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004853put_setstring(
4854 FILE *fd,
4855 char *cmd,
4856 char *name,
4857 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004858 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004861 char_u *buf = NULL;
4862 char_u *part = NULL;
4863 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4866 return FAIL;
4867 if (*valuep != NULL)
4868 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 // Output 'pastetoggle' as key names. For other
4870 // options some characters have to be escaped with
4871 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (valuep == &p_pt)
4873 {
4874 s = *valuep;
4875 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004876 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return FAIL;
4878 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004879 // expand the option value, replace $HOME by ~
4880 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004882 int size = (int)STRLEN(*valuep) + 1;
4883
4884 // replace home directory in the whole option value into "buf"
4885 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004886 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004887 goto fail;
4888 home_replace(NULL, *valuep, buf, size, FALSE);
4889
4890 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004891 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004892 // can be expanded when read back.
4893 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4894 && vim_strchr(*valuep, ',') != NULL)
4895 {
4896 part = alloc(size);
4897 if (part == NULL)
4898 goto fail;
4899
4900 // write line break to clear the option, e.g. ':set rtp='
4901 if (put_eol(fd) == FAIL)
4902 goto fail;
4903
4904 p = buf;
4905 while (*p != NUL)
4906 {
4907 // for each comma separated option part, append value to
4908 // the option, :set rtp+=value
4909 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4910 goto fail;
4911 (void)copy_option_part(&p, part, size, ",");
4912 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4913 goto fail;
4914 }
4915 vim_free(buf);
4916 vim_free(part);
4917 return OK;
4918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004920 {
4921 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004923 }
4924 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 else if (put_escstr(fd, *valuep, 2) == FAIL)
4927 return FAIL;
4928 }
4929 if (put_eol(fd) < 0)
4930 return FAIL;
4931 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004932fail:
4933 vim_free(buf);
4934 vim_free(part);
4935 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936}
4937
4938 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004939put_setnum(
4940 FILE *fd,
4941 char *cmd,
4942 char *name,
4943 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 long wc;
4946
4947 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4948 return FAIL;
4949 if (wc_use_keyname((char_u *)valuep, &wc))
4950 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004951 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4953 return FAIL;
4954 }
4955 else if (fprintf(fd, "%ld", *valuep) < 0)
4956 return FAIL;
4957 if (put_eol(fd) < 0)
4958 return FAIL;
4959 return OK;
4960}
4961
4962 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004963put_setbool(
4964 FILE *fd,
4965 char *cmd,
4966 char *name,
4967 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004969 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004970 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4972 || put_eol(fd) < 0)
4973 return FAIL;
4974 return OK;
4975}
4976
4977/*
4978 * Clear all the terminal options.
4979 * If the option has been allocated, free the memory.
4980 * Terminal options are never hidden or indirect.
4981 */
4982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004983clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 /*
4986 * Reset a few things before clearing the old options. This may cause
4987 * outputting a few things that the terminal doesn't understand, but the
4988 * screen will be cleared later, so this is OK.
4989 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004990 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004991 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004993 // When starting the GUI close the display opened for the clipboard.
4994 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (gui.starting)
4996 clear_xterm_clip();
4997#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004998 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005000 free_termoptions();
5001}
5002
5003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005004free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005005{
5006 struct vimoption *p;
5007
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005008 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 if (istermoption(p))
5010 {
5011 if (p->flags & P_ALLOCED)
5012 free_string_option(*(char_u **)(p->var));
5013 if (p->flags & P_DEF_ALLOCED)
5014 free_string_option(p->def_val[VI_DEFAULT]);
5015 *(char_u **)(p->var) = empty_option;
5016 p->def_val[VI_DEFAULT] = empty_option;
5017 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02005018#ifdef FEAT_EVAL
5019 // remember where the option was cleared
5020 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
5021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 clear_termcodes();
5024}
5025
5026/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00005027 * Free the string for one term option, if it was allocated.
5028 * Set the string to empty_option and clear allocated flag.
5029 * "var" points to the option value.
5030 */
5031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00005033{
5034 struct vimoption *p;
5035
5036 for (p = &options[0]; p->fullname != NULL; p++)
5037 if (p->var == var)
5038 {
5039 if (p->flags & P_ALLOCED)
5040 free_string_option(*(char_u **)(p->var));
5041 *(char_u **)(p->var) = empty_option;
5042 p->flags &= ~P_ALLOCED;
5043 break;
5044 }
5045}
5046
5047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * Set the terminal option defaults to the current value.
5049 * Used after setting the terminal name.
5050 */
5051 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005052set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
5054 struct vimoption *p;
5055
5056 for (p = &options[0]; p->fullname != NULL; p++)
5057 {
5058 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5059 {
5060 if (p->flags & P_DEF_ALLOCED)
5061 {
5062 free_string_option(p->def_val[VI_DEFAULT]);
5063 p->flags &= ~P_DEF_ALLOCED;
5064 }
5065 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5066 if (p->flags & P_ALLOCED)
5067 {
5068 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005069 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 }
5072 }
5073}
5074
5075/*
5076 * return TRUE if 'p' starts with 't_'
5077 */
5078 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005079istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5082}
5083
Bram Moolenaardac13472019-09-16 21:06:21 +02005084/*
5085 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5086 */
5087 int
5088istermoption_idx(int opt_idx)
5089{
5090 return istermoption(&options[opt_idx]);
5091}
5092
Bram Moolenaar113e1072019-01-20 15:30:40 +01005093#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005095 * Unset local option value, similar to ":set opt<".
5096 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005097 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005098unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005099{
5100 struct vimoption *p;
5101 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005102 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005103
5104 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005105 if (opt_idx < 0)
5106 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005107 p = &(options[opt_idx]);
5108
5109 switch ((int)p->indir)
5110 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005111 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005112 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005113 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005114 break;
5115 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005116 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005117 break;
5118 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005119 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005120 break;
5121 case PV_AR:
5122 buf->b_p_ar = -1;
5123 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005124 case PV_BKC:
5125 clear_string_option(&buf->b_p_bkc);
5126 buf->b_bkc_flags = 0;
5127 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005128 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005129 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005131 case PV_TC:
5132 clear_string_option(&buf->b_p_tc);
5133 buf->b_tc_flags = 0;
5134 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005135 case PV_SISO:
5136 curwin->w_p_siso = -1;
5137 break;
5138 case PV_SO:
5139 curwin->w_p_so = -1;
5140 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005141#ifdef FEAT_FIND_ID
5142 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005143 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005144 break;
5145 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005146 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005147 break;
5148#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005149 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005150 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005151 break;
5152 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005153 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005154 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005155#ifdef FEAT_COMPL_FUNC
5156 case PV_TSRFU:
5157 clear_string_option(&buf->b_p_tsrfu);
5158 break;
5159#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005160 case PV_FP:
5161 clear_string_option(&buf->b_p_fp);
5162 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005163#ifdef FEAT_QUICKFIX
5164 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005165 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005166 break;
5167 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005168 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005169 break;
5170 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005171 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005172 break;
5173#endif
5174#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5175 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005176 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005177 break;
5178#endif
5179#if defined(FEAT_CRYPT)
5180 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005181 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005182 break;
5183#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005184#ifdef FEAT_LINEBREAK
5185 case PV_SBR:
5186 clear_string_option(&((win_T *)from)->w_p_sbr);
5187 break;
5188#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005189#ifdef FEAT_STL_OPT
5190 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005191 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005192 break;
5193#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005194 case PV_UL:
5195 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5196 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005197#ifdef FEAT_LISP
5198 case PV_LW:
5199 clear_string_option(&buf->b_p_lw);
5200 break;
5201#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005202 case PV_MENC:
5203 clear_string_option(&buf->b_p_menc);
5204 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005205 case PV_LCS:
5206 clear_string_option(&((win_T *)from)->w_p_lcs);
5207 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5208 redraw_later(NOT_VALID);
5209 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005210 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005211 clear_string_option(&((win_T *)from)->w_p_ve);
5212 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005213 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005214 }
5215}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005216#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005217
5218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 * Get pointer to option variable, depending on local or global scope.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005220 * "scope" can be OPT_LOCAL, OPT_GLOBAL or a combination.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 */
5222 static char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005223get_varp_scope(struct vimoption *p, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005225 if ((scope & OPT_GLOBAL) && p->indir != PV_NONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 if (p->var == VAR_WIN)
5228 return (char_u *)GLOBAL_WO(get_varp(p));
5229 return p->var;
5230 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005231 if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 switch ((int)p->indir)
5234 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005235 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005237 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5238 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5239 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005241 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5242 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5243 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005244 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005245 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005246 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005247 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5248 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005250 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5251 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005253 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5254 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005255#ifdef FEAT_COMPL_FUNC
5256 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5257#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005258#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5259 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5260#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005261#if defined(FEAT_CRYPT)
5262 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5263#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005264#ifdef FEAT_LINEBREAK
5265 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5266#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005267#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005268 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005269#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005270 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005271#ifdef FEAT_LISP
5272 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5273#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005274 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005275 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005276 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005277 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005280 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 return get_varp(p);
5283}
5284
5285/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005286 * Get pointer to option variable at 'opt_idx', depending on local or global
5287 * scope.
5288 */
5289 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005290get_option_varp_scope(int opt_idx, int scope)
Bram Moolenaardac13472019-09-16 21:06:21 +02005291{
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00005292 return get_varp_scope(&(options[opt_idx]), scope);
Bram Moolenaardac13472019-09-16 21:06:21 +02005293}
5294
5295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 * Get pointer to option variable.
5297 */
5298 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005299get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005301 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (p->var == NULL)
5303 return NULL;
5304
5305 switch ((int)p->indir)
5306 {
5307 case PV_NONE: return p->var;
5308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005309 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005310 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005312 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005314 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005316 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005318 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005320 case PV_TC: return *curbuf->b_p_tc != NUL
5321 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005322 case PV_BKC: return *curbuf->b_p_bkc != NUL
5323 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005324 case PV_SISO: return curwin->w_p_siso >= 0
5325 ? (char_u *)&(curwin->w_p_siso) : p->var;
5326 case PV_SO: return curwin->w_p_so >= 0
5327 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005329 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005331 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5333#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005334 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005336 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005338#ifdef FEAT_COMPL_FUNC
5339 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5340 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5341#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005342 case PV_FP: return *curbuf->b_p_fp != NUL
5343 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005345 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005347 case PV_GP: return *curbuf->b_p_gp != NUL
5348 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5349 case PV_MP: return *curbuf->b_p_mp != NUL
5350 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005352#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5353 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5354 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5355#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005356#if defined(FEAT_CRYPT)
5357 case PV_CM: return *curbuf->b_p_cm != NUL
5358 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5359#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005360#ifdef FEAT_LINEBREAK
5361 case PV_SBR: return *curwin->w_p_sbr != NUL
5362 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5363#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005364#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005365 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005366 ? (char_u *)&(curwin->w_p_stl) : p->var;
5367#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005368 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5369 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005370#ifdef FEAT_LISP
5371 case PV_LW: return *curbuf->b_p_lw != NUL
5372 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5373#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005374 case PV_MENC: return *curbuf->b_p_menc != NUL
5375 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376#ifdef FEAT_ARABIC
5377 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5378#endif
5379 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005380 case PV_LCS: return *curwin->w_p_lcs != NUL
5381 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005382 case PV_VE: return *curwin->w_p_ve != NUL
5383 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005384#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005385 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5386#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005387#ifdef FEAT_SYN_HL
5388 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5389 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005390 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005391 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_DIFF
5394 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5395#endif
5396#ifdef FEAT_FOLDING
5397 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5398 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5399 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5400 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5401 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5402 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5403 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5404# ifdef FEAT_EVAL
5405 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5406 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5407# endif
5408 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5409#endif
5410 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005411 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005412#ifdef FEAT_LINEBREAK
5413 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005416 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005417#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5419#endif
5420#ifdef FEAT_RIGHTLEFT
5421 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5422 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5423#endif
5424 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5425 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5426#ifdef FEAT_LINEBREAK
5427 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005428 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5429 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005431 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005433 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005434#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005435 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5436 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5437#endif
5438#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005439 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5440 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5441 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
5444 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5445 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5448 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5450 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5451#ifdef FEAT_CINDENT
5452 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5453 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5454 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5455#endif
5456#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5457 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460#ifdef FEAT_FOLDING
5461 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005464#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005465 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005467#ifdef FEAT_COMPL_FUNC
5468 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005469 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005470#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005471#ifdef FEAT_EVAL
5472 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005475 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005481 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5483 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5484 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5485 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5486#ifdef FEAT_FIND_ID
5487# ifdef FEAT_EVAL
5488 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5489# endif
5490#endif
5491#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5492 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5493 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5494#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005495#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005496 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498#ifdef FEAT_CRYPT
5499 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5500#endif
5501#ifdef FEAT_LISP
5502 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5503#endif
5504 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5505 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5506 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5507 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5508 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005510#ifdef FEAT_TEXTOBJ
5511 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5514#ifdef FEAT_SMARTINDENT
5515 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5519#ifdef FEAT_SEARCHPATH
5520 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5521#endif
5522 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5523#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005524 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005525 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5526#endif
5527#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005528 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5529 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5530 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005531 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532#endif
5533 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5534 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5535 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5536 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005537#ifdef FEAT_PERSISTENT_UNDO
5538 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5541#ifdef FEAT_KEYMAP
5542 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5543#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005544#ifdef FEAT_SIGNS
5545 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5546#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005547#ifdef FEAT_VARTABS
5548 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5549 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5550#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00005551 default: iemsg(_(e_get_varp_error));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005553 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 return (char_u *)&(curbuf->b_p_wm);
5555}
5556
5557/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005558 * Return a pointer to the variable for option at 'opt_idx'
5559 */
5560 char_u *
5561get_option_var(int opt_idx)
5562{
5563 return options[opt_idx].var;
5564}
5565
Dominique Pelle748b3082022-01-08 12:41:16 +00005566#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +02005567/*
5568 * Return the full name of the option at 'opt_idx'
5569 */
5570 char_u *
5571get_option_fullname(int opt_idx)
5572{
5573 return (char_u *)options[opt_idx].fullname;
5574}
Dominique Pelle748b3082022-01-08 12:41:16 +00005575#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02005576
5577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 * Get the value of 'equalprg', either the buffer-local one or the global one.
5579 */
5580 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 if (*curbuf->b_p_ep == NUL)
5584 return p_ep;
5585 return curbuf->b_p_ep;
5586}
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588/*
5589 * Copy options from one window to another.
5590 * Used when splitting a window.
5591 */
5592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005593win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5596 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005597 after_copy_winopt(wp_to);
5598}
5599
5600/*
5601 * After copying window options: update variables depending on options.
5602 */
5603 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005604after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005605{
5606#ifdef FEAT_LINEBREAK
5607 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005608#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005609#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005610 fill_culopt_flags(NULL, wp);
5611 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005612#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005613 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616/*
5617 * Copy the options from one winopt_T to another.
5618 * Doesn't free the old option values in "to", use clear_winopt() for that.
5619 * The 'scroll' option is not copied, because it depends on the window height.
5620 * The 'previewwindow' option is reset, there can be only one preview window.
5621 */
5622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005623copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625#ifdef FEAT_ARABIC
5626 to->wo_arab = from->wo_arab;
5627#endif
5628 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005629 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005631 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005632 to->wo_ve = vim_strsave(from->wo_ve);
5633 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005634#ifdef FEAT_LINEBREAK
5635 to->wo_nuw = from->wo_nuw;
5636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637#ifdef FEAT_RIGHTLEFT
5638 to->wo_rl = from->wo_rl;
5639 to->wo_rlc = vim_strsave(from->wo_rlc);
5640#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005641#ifdef FEAT_LINEBREAK
5642 to->wo_sbr = vim_strsave(from->wo_sbr);
5643#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005644#ifdef FEAT_STL_OPT
5645 to->wo_stl = vim_strsave(from->wo_stl);
5646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005648#ifdef FEAT_DIFF
5649 to->wo_wrap_save = from->wo_wrap_save;
5650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651#ifdef FEAT_LINEBREAK
5652 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005653 to->wo_bri = from->wo_bri;
5654 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005656 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005658 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005659 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005660 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005661#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005662 to->wo_spell = from->wo_spell;
5663#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005664#ifdef FEAT_SYN_HL
5665 to->wo_cuc = from->wo_cuc;
5666 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005667 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005668 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670#ifdef FEAT_DIFF
5671 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005672 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005674#ifdef FEAT_CONCEAL
5675 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005676 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005677#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005678#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005679 to->wo_twk = vim_strsave(from->wo_twk);
5680 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682#ifdef FEAT_FOLDING
5683 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005684 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005686 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 to->wo_fdi = vim_strsave(from->wo_fdi);
5688 to->wo_fml = from->wo_fml;
5689 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005690 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005692 to->wo_fdm_save = from->wo_diff_saved
5693 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 to->wo_fdn = from->wo_fdn;
5695# ifdef FEAT_EVAL
5696 to->wo_fde = vim_strsave(from->wo_fde);
5697 to->wo_fdt = vim_strsave(from->wo_fdt);
5698# endif
5699 to->wo_fmr = vim_strsave(from->wo_fmr);
5700#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005701#ifdef FEAT_SIGNS
5702 to->wo_scl = vim_strsave(from->wo_scl);
5703#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005704
5705#ifdef FEAT_EVAL
5706 // Copy the script context so that we know where the value was last set.
5707 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5708 sizeof(to->wo_script_ctx));
5709#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711}
5712
5713/*
5714 * Check string options in a window for a NULL value.
5715 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005717check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 check_winopt(&win->w_onebuf_opt);
5720 check_winopt(&win->w_allbuf_opt);
5721}
5722
5723/*
5724 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5725 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005726 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005727check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729#ifdef FEAT_FOLDING
5730 check_string_option(&wop->wo_fdi);
5731 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005732 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733# ifdef FEAT_EVAL
5734 check_string_option(&wop->wo_fde);
5735 check_string_option(&wop->wo_fdt);
5736# endif
5737 check_string_option(&wop->wo_fmr);
5738#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005739#ifdef FEAT_SIGNS
5740 check_string_option(&wop->wo_scl);
5741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742#ifdef FEAT_RIGHTLEFT
5743 check_string_option(&wop->wo_rlc);
5744#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005745#ifdef FEAT_LINEBREAK
5746 check_string_option(&wop->wo_sbr);
5747#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005748#ifdef FEAT_STL_OPT
5749 check_string_option(&wop->wo_stl);
5750#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005751#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005752 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005753 check_string_option(&wop->wo_cc);
5754#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005755#ifdef FEAT_CONCEAL
5756 check_string_option(&wop->wo_cocu);
5757#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005758#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005759 check_string_option(&wop->wo_twk);
5760 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005761#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005762#ifdef FEAT_LINEBREAK
5763 check_string_option(&wop->wo_briopt);
5764#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005765 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005766 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005767 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768}
5769
5770/*
5771 * Free the allocated memory inside a winopt_T.
5772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776#ifdef FEAT_FOLDING
5777 clear_string_option(&wop->wo_fdi);
5778 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005779 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780# ifdef FEAT_EVAL
5781 clear_string_option(&wop->wo_fde);
5782 clear_string_option(&wop->wo_fdt);
5783# endif
5784 clear_string_option(&wop->wo_fmr);
5785#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005786#ifdef FEAT_SIGNS
5787 clear_string_option(&wop->wo_scl);
5788#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005789#ifdef FEAT_LINEBREAK
5790 clear_string_option(&wop->wo_briopt);
5791#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005792 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793#ifdef FEAT_RIGHTLEFT
5794 clear_string_option(&wop->wo_rlc);
5795#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005796#ifdef FEAT_LINEBREAK
5797 clear_string_option(&wop->wo_sbr);
5798#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005799#ifdef FEAT_STL_OPT
5800 clear_string_option(&wop->wo_stl);
5801#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005802#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005803 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005804 clear_string_option(&wop->wo_cc);
5805#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005806#ifdef FEAT_CONCEAL
5807 clear_string_option(&wop->wo_cocu);
5808#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005809#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005810 clear_string_option(&wop->wo_twk);
5811 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005812#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005813 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005814 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815}
5816
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005817#ifdef FEAT_EVAL
5818// Index into the options table for a buffer-local option enum.
5819static int buf_opt_idx[BV_COUNT];
5820# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5821
5822/*
5823 * Initialize buf_opt_idx[] if not done already.
5824 */
5825 static void
5826init_buf_opt_idx(void)
5827{
5828 static int did_init_buf_opt_idx = FALSE;
5829 int i;
5830
5831 if (did_init_buf_opt_idx)
5832 return;
5833 did_init_buf_opt_idx = TRUE;
5834 for (i = 0; !istermoption_idx(i); i++)
5835 if (options[i].indir & PV_BUF)
5836 buf_opt_idx[options[i].indir & PV_MASK] = i;
5837}
5838#else
5839# define COPY_OPT_SCTX(buf, bv)
5840#endif
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842/*
5843 * Copy global option values to local options for one buffer.
5844 * Used when creating a new buffer and sometimes when entering a buffer.
5845 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005846 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5848 * appropriate.
5849 * BCO_NOHELP Don't copy the values to a help buffer.
5850 */
5851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005852buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
5854 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005855 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 int dont_do_help;
5857 int did_isk = FALSE;
5858
5859 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 * Skip this when the option defaults have not been set yet. Happens when
5861 * main() allocates the first buffer.
5862 */
5863 if (p_cpo != NULL)
5864 {
5865 /*
5866 * Always copy when entering and 'cpo' contains 'S'.
5867 * Don't copy when already initialized.
5868 * Don't copy when 'cpo' contains 's' and not entering.
5869 * 'S' BCO_ENTER initialized 's' should_copy
5870 * yes yes X X TRUE
5871 * yes no yes X FALSE
5872 * no X yes X FALSE
5873 * X no no yes FALSE
5874 * X no no no TRUE
5875 * no yes no X TRUE
5876 */
5877 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5878 && (buf->b_p_initialized
5879 || (!(flags & BCO_ENTER)
5880 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5881 should_copy = FALSE;
5882
5883 if (should_copy || (flags & BCO_ALWAYS))
5884 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005885#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005886 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005887 init_buf_opt_idx();
5888#endif
5889 // Don't copy the options specific to a help buffer when
5890 // BCO_NOHELP is given or the options were initialized already
5891 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5893 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005894 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 {
5896 save_p_isk = buf->b_p_isk;
5897 buf->b_p_isk = NULL;
5898 }
5899 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005900 * Always free the allocated strings. If not already initialized,
5901 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 */
5903 if (!buf->b_p_initialized)
5904 {
5905 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005906 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005909 switch (*p_ffs)
5910 {
5911 case 'm':
5912 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5913 case 'd':
5914 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5915 case 'u':
5916 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5917 default:
5918 buf->b_p_ff = vim_strsave(p_ff);
5919 }
5920 if (buf->b_p_ff != NULL)
5921 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 buf->b_p_bh = empty_option;
5923 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 }
5925 else
5926 free_buf_options(buf, FALSE);
5927
5928 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005929 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 buf->b_p_ai_nopaste = p_ai_nopaste;
5931 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf->b_p_tw_nopaste = p_tw_nopaste;
5936 buf->b_p_tw_nobin = p_tw_nobin;
5937 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005938 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 buf->b_p_wm_nopaste = p_wm_nopaste;
5940 buf->b_p_wm_nobin = p_wm_nobin;
5941 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005942 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005943 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005945 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005946 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005948 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005950 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005952 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 buf->b_p_ml_nobin = p_ml_nobin;
5954 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005955 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005956 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005957 buf->b_p_swf = FALSE;
5958 else
5959 {
5960 buf->b_p_swf = p_swf;
Bram Moolenaar62068772021-12-14 09:01:38 +00005961 COPY_OPT_SCTX(buf, BV_SWF);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005964 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005965#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005966 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005967 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005969#ifdef FEAT_COMPL_FUNC
5970 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005971 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005972 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005973 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005974 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005975 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005976#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005977#ifdef FEAT_EVAL
5978 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005979 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005980 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005983 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005985#ifdef FEAT_VARTABS
5986 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005987 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005988 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005989 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005990 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00005991 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005992 buf->b_p_vsts_nopaste = p_vsts_nopaste
5993 ? vim_strsave(p_vsts_nopaste) : NULL;
5994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005996 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005998 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999#ifdef FEAT_FOLDING
6000 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002#endif
6003 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006004 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00006005 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006006 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02006007 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
6008 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006010 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006012 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#ifdef FEAT_SMARTINDENT
6014 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006015 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016#endif
6017 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006018 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019#ifdef FEAT_CINDENT
6020 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006021 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006023 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006025 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006027 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006030 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
6032 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006033 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034#endif
6035#ifdef FEAT_LISP
6036 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006037 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038#endif
6039#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006040 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006042 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006043 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006044 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006045#endif
6046#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006047 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006048 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006049 (void)compile_cap_prog(&buf->b_s);
6050 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006051 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006052 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006053 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006054 buf->b_s.b_p_spo = vim_strsave(p_spo);
6055 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#endif
6057#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6058 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006059 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006061 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006063 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006064#if defined(FEAT_EVAL)
6065 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006066 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068#ifdef FEAT_CRYPT
6069 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006070 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071#endif
6072#ifdef FEAT_SEARCHPATH
6073 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006074 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075#endif
6076#ifdef FEAT_KEYMAP
6077 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006078 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 buf->b_kmap_state |= KEYMAP_INIT;
6080#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006081#ifdef FEAT_TERMINAL
6082 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006083 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006084#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006085 // This isn't really an option, but copying the langmap and IME
6086 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006088 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006090 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006092 // options that are normally global but also have a local value
6093 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006095 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006096 buf->b_p_bkc = empty_option;
6097 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#ifdef FEAT_QUICKFIX
6099 buf->b_p_gp = empty_option;
6100 buf->b_p_mp = empty_option;
6101 buf->b_p_efm = empty_option;
6102#endif
6103 buf->b_p_ep = empty_option;
6104 buf->b_p_kp = empty_option;
6105 buf->b_p_path = empty_option;
6106 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006107 buf->b_p_tc = empty_option;
6108 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109#ifdef FEAT_FIND_ID
6110 buf->b_p_def = empty_option;
6111 buf->b_p_inc = empty_option;
6112# ifdef FEAT_EVAL
6113 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006114 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115# endif
6116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 buf->b_p_dict = empty_option;
6118 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006119#ifdef FEAT_COMPL_FUNC
6120 buf->b_p_tsrfu = empty_option;
6121#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006122#ifdef FEAT_TEXTOBJ
6123 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006124 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006125#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006126#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6127 buf->b_p_bexpr = empty_option;
6128#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006129#if defined(FEAT_CRYPT)
6130 buf->b_p_cm = empty_option;
6131#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006132#ifdef FEAT_PERSISTENT_UNDO
6133 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006134 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006135#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006136#ifdef FEAT_LISP
6137 buf->b_p_lw = empty_option;
6138#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006139 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 /*
6142 * Don't copy the options set by ex_help(), use the saved values,
6143 * when going from a help buffer to a non-help buffer.
6144 * Don't touch these at all when BCO_NOHELP is used and going from
6145 * or to a help buffer.
6146 */
6147 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006150#ifdef FEAT_VARTABS
6151 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006152 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006153 else
6154 buf->b_p_vts_array = NULL;
6155#endif
6156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 else
6158 {
6159 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006160 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 did_isk = TRUE;
6162 buf->b_p_ts = p_ts;
Bram Moolenaar62068772021-12-14 09:01:38 +00006163 COPY_OPT_SCTX(buf, BV_TS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006164#ifdef FEAT_VARTABS
6165 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006166 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006167 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006168 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006169 else
6170 buf->b_p_vts_array = NULL;
6171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (buf->b_p_bt[0] == 'h')
6174 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006176 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178 }
6179
6180 /*
6181 * When the options should be copied (ignoring BCO_ALWAYS), set the
6182 * flag that indicates that the options have been initialized.
6183 */
6184 if (should_copy)
6185 buf->b_p_initialized = TRUE;
6186 }
6187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (did_isk)
6190 (void)buf_init_chartab(buf, FALSE);
6191}
6192
6193/*
6194 * Reset the 'modifiable' option and its default value.
6195 */
6196 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006197reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 int opt_idx;
6200
6201 curbuf->b_p_ma = FALSE;
6202 p_ma = FALSE;
6203 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006204 if (opt_idx >= 0)
6205 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206}
6207
6208/*
6209 * Set the global value for 'iminsert' to the local value.
6210 */
6211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006212set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 p_iminsert = curbuf->b_p_iminsert;
6215}
6216
6217/*
6218 * Set the global value for 'imsearch' to the local value.
6219 */
6220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006221set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 p_imsearch = curbuf->b_p_imsearch;
6224}
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226static int expand_option_idx = -1;
6227static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6228static int expand_option_flags = 0;
6229
6230 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006231set_context_in_set_cmd(
6232 expand_T *xp,
6233 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006234 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006237 long_u flags = 0; // init for GCC
6238 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 char_u *p;
6240 char_u *s;
6241 int is_term_option = FALSE;
6242 int key;
6243
6244 expand_option_flags = opt_flags;
6245
6246 xp->xp_context = EXPAND_SETTINGS;
6247 if (*arg == NUL)
6248 {
6249 xp->xp_pattern = arg;
6250 return;
6251 }
6252 p = arg + STRLEN(arg) - 1;
6253 if (*p == ' ' && *(p - 1) != '\\')
6254 {
6255 xp->xp_pattern = p + 1;
6256 return;
6257 }
6258 while (p > arg)
6259 {
6260 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006261 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (*p == ' ' || *p == ',')
6263 {
6264 while (s > arg && *(s - 1) == '\\')
6265 --s;
6266 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006267 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (*p == ' ' && ((p - s) & 1) == 0)
6269 {
6270 ++p;
6271 break;
6272 }
6273 --p;
6274 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006275 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
6277 xp->xp_context = EXPAND_BOOL_SETTINGS;
6278 p += 2;
6279 }
6280 if (STRNCMP(p, "inv", 3) == 0)
6281 {
6282 xp->xp_context = EXPAND_BOOL_SETTINGS;
6283 p += 3;
6284 }
6285 xp->xp_pattern = arg = p;
6286 if (*arg == '<')
6287 {
6288 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006289 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 return;
6291 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006292 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
6294 xp->xp_context = EXPAND_NOTHING;
6295 return;
6296 }
6297 nextchar = *++p;
6298 is_term_option = TRUE;
6299 expand_option_name[2] = KEY2TERMCAP0(key);
6300 expand_option_name[3] = KEY2TERMCAP1(key);
6301 }
6302 else
6303 {
6304 if (p[0] == 't' && p[1] == '_')
6305 {
6306 p += 2;
6307 if (*p != NUL)
6308 ++p;
6309 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006310 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 nextchar = *++p;
6312 is_term_option = TRUE;
6313 expand_option_name[2] = p[-2];
6314 expand_option_name[3] = p[-1];
6315 }
6316 else
6317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006318 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6320 p++;
6321 if (*p == NUL)
6322 return;
6323 nextchar = *p;
6324 *p = NUL;
6325 opt_idx = findoption(arg);
6326 *p = nextchar;
6327 if (opt_idx == -1 || options[opt_idx].var == NULL)
6328 {
6329 xp->xp_context = EXPAND_NOTHING;
6330 return;
6331 }
6332 flags = options[opt_idx].flags;
6333 if (flags & P_BOOL)
6334 {
6335 xp->xp_context = EXPAND_NOTHING;
6336 return;
6337 }
6338 }
6339 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006340 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6342 {
6343 ++p;
6344 nextchar = '=';
6345 }
6346 if ((nextchar != '=' && nextchar != ':')
6347 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6348 {
6349 xp->xp_context = EXPAND_UNSUCCESSFUL;
6350 return;
6351 }
6352 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6353 {
6354 xp->xp_context = EXPAND_OLD_SETTING;
6355 if (is_term_option)
6356 expand_option_idx = -1;
6357 else
6358 expand_option_idx = opt_idx;
6359 xp->xp_pattern = p + 1;
6360 return;
6361 }
6362 xp->xp_context = EXPAND_NOTHING;
6363 if (is_term_option || (flags & P_NUM))
6364 return;
6365
6366 xp->xp_pattern = p + 1;
6367
6368 if (flags & P_EXPAND)
6369 {
6370 p = options[opt_idx].var;
6371 if (p == (char_u *)&p_bdir
6372 || p == (char_u *)&p_dir
6373 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006374 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 || p == (char_u *)&p_rtp
6376#ifdef FEAT_SEARCHPATH
6377 || p == (char_u *)&p_cdpath
6378#endif
6379#ifdef FEAT_SESSION
6380 || p == (char_u *)&p_vdir
6381#endif
6382 )
6383 {
6384 xp->xp_context = EXPAND_DIRECTORIES;
6385 if (p == (char_u *)&p_path
6386#ifdef FEAT_SEARCHPATH
6387 || p == (char_u *)&p_cdpath
6388#endif
6389 )
6390 xp->xp_backslash = XP_BS_THREE;
6391 else
6392 xp->xp_backslash = XP_BS_ONE;
6393 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006394 else if (p == (char_u *)&p_ft)
6395 {
6396 xp->xp_context = EXPAND_FILETYPE;
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 else
6399 {
6400 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006401 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (p == (char_u *)&p_tags)
6403 xp->xp_backslash = XP_BS_THREE;
6404 else
6405 xp->xp_backslash = XP_BS_ONE;
6406 }
6407 }
6408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006409 // For an option that is a list of file names, find the start of the
6410 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006413 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (*p == ' ' || *p == ',')
6415 {
6416 s = p;
6417 while (s > xp->xp_pattern && *(s - 1) == '\\')
6418 --s;
6419 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6420 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6421 {
6422 xp->xp_pattern = p + 1;
6423 break;
6424 }
6425 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006426
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006427#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006428 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006429 if (options[opt_idx].var == (char_u *)&p_sps
6430 && STRNCMP(p, "file:", 5) == 0)
6431 {
6432 xp->xp_pattern = p + 5;
6433 break;
6434 }
6435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437}
6438
6439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006440ExpandSettings(
6441 expand_T *xp,
6442 regmatch_T *regmatch,
6443 int *num_file,
6444 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006446 int num_normal = 0; // Nr of matching non-term-code settings
6447 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 int opt_idx;
6449 int match;
6450 int count = 0;
6451 char_u *str;
6452 int loop;
6453 int is_term_opt;
6454 char_u name_buf[MAX_KEY_NAME_LEN];
6455 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006456 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006458 // do this loop twice:
6459 // loop == 0: count the number of matching options
6460 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 for (loop = 0; loop <= 1; ++loop)
6462 {
6463 regmatch->rm_ic = ic;
6464 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6465 {
K.Takataeeec2542021-06-02 13:28:16 +02006466 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6468 {
6469 if (loop == 0)
6470 num_normal++;
6471 else
6472 (*file)[count++] = vim_strsave((char_u *)names[match]);
6473 }
6474 }
6475 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6476 opt_idx++)
6477 {
6478 if (options[opt_idx].var == NULL)
6479 continue;
6480 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6481 && !(options[opt_idx].flags & P_BOOL))
6482 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006483 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (is_term_opt && num_normal > 0)
6485 continue;
6486 match = FALSE;
6487 if (vim_regexec(regmatch, str, (colnr_T)0)
6488 || (options[opt_idx].shortname != NULL
6489 && vim_regexec(regmatch,
6490 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6491 match = TRUE;
6492 else if (is_term_opt)
6493 {
6494 name_buf[0] = '<';
6495 name_buf[1] = 't';
6496 name_buf[2] = '_';
6497 name_buf[3] = str[2];
6498 name_buf[4] = str[3];
6499 name_buf[5] = '>';
6500 name_buf[6] = NUL;
6501 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6502 {
6503 match = TRUE;
6504 str = name_buf;
6505 }
6506 }
6507 if (match)
6508 {
6509 if (loop == 0)
6510 {
6511 if (is_term_opt)
6512 num_term++;
6513 else
6514 num_normal++;
6515 }
6516 else
6517 (*file)[count++] = vim_strsave(str);
6518 }
6519 }
6520 /*
6521 * Check terminal key codes, these are not in the option table
6522 */
6523 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6524 {
6525 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6526 {
6527 if (!isprint(str[0]) || !isprint(str[1]))
6528 continue;
6529
6530 name_buf[0] = 't';
6531 name_buf[1] = '_';
6532 name_buf[2] = str[0];
6533 name_buf[3] = str[1];
6534 name_buf[4] = NUL;
6535
6536 match = FALSE;
6537 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6538 match = TRUE;
6539 else
6540 {
6541 name_buf[0] = '<';
6542 name_buf[1] = 't';
6543 name_buf[2] = '_';
6544 name_buf[3] = str[0];
6545 name_buf[4] = str[1];
6546 name_buf[5] = '>';
6547 name_buf[6] = NUL;
6548
6549 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6550 match = TRUE;
6551 }
6552 if (match)
6553 {
6554 if (loop == 0)
6555 num_term++;
6556 else
6557 (*file)[count++] = vim_strsave(name_buf);
6558 }
6559 }
6560
6561 /*
6562 * Check special key names.
6563 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006564 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6566 {
6567 name_buf[0] = '<';
6568 STRCPY(name_buf + 1, str);
6569 STRCAT(name_buf, ">");
6570
6571 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6572 {
6573 if (loop == 0)
6574 num_term++;
6575 else
6576 (*file)[count++] = vim_strsave(name_buf);
6577 }
6578 }
6579 }
6580 if (loop == 0)
6581 {
6582 if (num_normal > 0)
6583 *num_file = num_normal;
6584 else if (num_term > 0)
6585 *num_file = num_term;
6586 else
6587 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006588 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (*file == NULL)
6590 {
6591 *file = (char_u **)"";
6592 return FAIL;
6593 }
6594 }
6595 }
6596 return OK;
6597}
6598
6599 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006600ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006602 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 char_u *buf;
6604
6605 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006606 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (*file == NULL)
6608 return FAIL;
6609
6610 /*
6611 * For a terminal key code expand_option_idx is < 0.
6612 */
6613 if (expand_option_idx < 0)
6614 {
6615 var = find_termcode(expand_option_name + 2);
6616 if (var == NULL)
6617 expand_option_idx = findoption(expand_option_name);
6618 }
6619
6620 if (expand_option_idx >= 0)
6621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006622 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 option_value2string(&options[expand_option_idx], expand_option_flags);
6624 var = NameBuff;
6625 }
6626 else if (var == NULL)
6627 var = (char_u *)"";
6628
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006629 // A backslash is required before some characters. This is the reverse of
6630 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 buf = vim_strsave_escaped(var, escape_chars);
6632
6633 if (buf == NULL)
6634 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006635 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 return FAIL;
6637 }
6638
6639#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006640 // For MS-Windows et al. we don't double backslashes at the start and
6641 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006642 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 if (var[0] == '\\' && var[1] == '\\'
6644 && expand_option_idx >= 0
6645 && (options[expand_option_idx].flags & P_EXPAND)
6646 && vim_isfilec(var[2])
6647 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006648 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649#endif
6650
6651 *file[0] = buf;
6652 *num_file = 1;
6653 return OK;
6654}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655
6656/*
6657 * Get the value for the numeric or string option *opp in a nice format into
6658 * NameBuff[]. Must not be called with a hidden option!
6659 */
6660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006661option_value2string(
6662 struct vimoption *opp,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006663 int scope) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 char_u *varp;
6666
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006667 varp = get_varp_scope(opp, scope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669 if (opp->flags & P_NUM)
6670 {
6671 long wc = 0;
6672
6673 if (wc_use_keyname(varp, &wc))
6674 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6675 else if (wc != 0)
6676 STRCPY(NameBuff, transchar((int)wc));
6677 else
6678 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6679 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006680 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
6682 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006683 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 NameBuff[0] = NUL;
6685#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006686 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006687 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 STRCPY(NameBuff, "*****");
6689#endif
6690 else if (opp->flags & P_EXPAND)
6691 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006692 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 else if ((char_u **)opp->var == &p_pt)
6694 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6695 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006696 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 }
6698}
6699
6700/*
6701 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6702 * printed as a keyname.
6703 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6704 */
6705 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006706wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6709 {
6710 *wcp = *(long *)varp;
6711 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6712 return TRUE;
6713 }
6714 return FALSE;
6715}
6716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 * Return TRUE if "x" is present in 'shortmess' option, or
6719 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6720 */
6721 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006722shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006724 return p_shm != NULL &&
6725 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 || (vim_strchr(p_shm, 'a') != NULL
6727 && vim_strchr((char_u *)SHM_A, x) != NULL));
6728}
6729
6730/*
6731 * paste_option_changed() - Called after p_paste was set or reset.
6732 */
6733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006734paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 static int old_p_paste = FALSE;
6737 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006738 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739#ifdef FEAT_CMDL_INFO
6740 static int save_ru = 0;
6741#endif
6742#ifdef FEAT_RIGHTLEFT
6743 static int save_ri = 0;
6744 static int save_hkmap = 0;
6745#endif
6746 buf_T *buf;
6747
6748 if (p_paste)
6749 {
6750 /*
6751 * Paste switched from off to on.
6752 * Save the current values, so they can be restored later.
6753 */
6754 if (!old_p_paste)
6755 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006756 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006757 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
6759 buf->b_p_tw_nopaste = buf->b_p_tw;
6760 buf->b_p_wm_nopaste = buf->b_p_wm;
6761 buf->b_p_sts_nopaste = buf->b_p_sts;
6762 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006763 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006764#ifdef FEAT_VARTABS
6765 if (buf->b_p_vsts_nopaste)
6766 vim_free(buf->b_p_vsts_nopaste);
6767 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6768 ? vim_strsave(buf->b_p_vsts) : NULL;
6769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006772 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006774 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775#ifdef FEAT_CMDL_INFO
6776 save_ru = p_ru;
6777#endif
6778#ifdef FEAT_RIGHTLEFT
6779 save_ri = p_ri;
6780 save_hkmap = p_hkmap;
6781#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006782 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006783 p_ai_nopaste = p_ai;
6784 p_et_nopaste = p_et;
6785 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 p_tw_nopaste = p_tw;
6787 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006788#ifdef FEAT_VARTABS
6789 if (p_vsts_nopaste)
6790 vim_free(p_vsts_nopaste);
6791 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
6794
6795 /*
6796 * Always set the option values, also when 'paste' is set when it is
6797 * already on.
6798 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006800 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 buf->b_p_tw = 0; // textwidth is 0
6803 buf->b_p_wm = 0; // wrapmargin is 0
6804 buf->b_p_sts = 0; // softtabstop is 0
6805 buf->b_p_ai = 0; // no auto-indent
6806 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006807#ifdef FEAT_VARTABS
6808 if (buf->b_p_vsts)
6809 free_string_option(buf->b_p_vsts);
6810 buf->b_p_vsts = empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006811 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006815 // set global options
6816 p_sm = 0; // no showmatch
6817 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006820 status_redraw_all(); // redraw to remove the ruler
6821 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822#endif
6823#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006824 p_ri = 0; // no reverse insert
6825 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006827 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 p_tw = 0;
6829 p_wm = 0;
6830 p_sts = 0;
6831 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006832#ifdef FEAT_VARTABS
6833 if (p_vsts)
6834 free_string_option(p_vsts);
6835 p_vsts = empty_option;
6836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838
6839 /*
6840 * Paste switched from on to off: Restore saved values.
6841 */
6842 else if (old_p_paste)
6843 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006844 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006845 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
6847 buf->b_p_tw = buf->b_p_tw_nopaste;
6848 buf->b_p_wm = buf->b_p_wm_nopaste;
6849 buf->b_p_sts = buf->b_p_sts_nopaste;
6850 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006851 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006852#ifdef FEAT_VARTABS
6853 if (buf->b_p_vsts)
6854 free_string_option(buf->b_p_vsts);
6855 buf->b_p_vsts = buf->b_p_vsts_nopaste
6856 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
Bram Moolenaar652dee42022-01-28 20:47:49 +00006857 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006858 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006859 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006860 else
Bram Moolenaar652dee42022-01-28 20:47:49 +00006861 buf->b_p_vsts_array = NULL;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006865 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006867 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006870 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 p_ru = save_ru;
6872#endif
6873#ifdef FEAT_RIGHTLEFT
6874 p_ri = save_ri;
6875 p_hkmap = save_hkmap;
6876#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006877 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006878 p_ai = p_ai_nopaste;
6879 p_et = p_et_nopaste;
6880 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 p_tw = p_tw_nopaste;
6882 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006883#ifdef FEAT_VARTABS
6884 if (p_vsts)
6885 free_string_option(p_vsts);
6886 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
6889
6890 old_p_paste = p_paste;
6891}
6892
6893/*
6894 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6895 *
6896 * Reset 'compatible' and set the values for options that didn't get set yet
6897 * to the Vim defaults.
6898 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006899 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 */
6901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006902vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006904 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006905 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006906 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 if (!option_was_set((char_u *)"cp"))
6909 {
6910 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006911 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6913 set_option_default(opt_idx, OPT_FREE, FALSE);
6914 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006915 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006917
6918 if (fname != NULL)
6919 {
6920 p = vim_getenv(envname, &dofree);
6921 if (p == NULL)
6922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006923 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006924 p = FullName_save(fname, FALSE);
6925 if (p != NULL)
6926 {
6927 vim_setenv(envname, p);
6928 vim_free(p);
6929 }
6930 }
6931 else if (dofree)
6932 vim_free(p);
6933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934}
6935
6936/*
6937 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6938 */
6939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006940change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006942 int opt_idx;
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (p_cp != on)
6945 {
6946 p_cp = on;
6947 compatible_set();
6948 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006949 opt_idx = findoption((char_u *)"cp");
6950 if (opt_idx >= 0)
6951 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952}
6953
6954/*
6955 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006956 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 */
6958 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006959option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 int idx;
6962
6963 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006964 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 return FALSE;
6966 if (options[idx].flags & P_WAS_SET)
6967 return TRUE;
6968 return FALSE;
6969}
6970
6971/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006972 * Reset the flag indicating option "name" was set.
6973 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006975reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006976{
6977 int idx = findoption(name);
6978
6979 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006980 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006981 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006982 return OK;
6983 }
6984 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006985}
6986
6987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 * compatible_set() - Called when 'compatible' has been set or unset.
6989 *
6990 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6991 * flag) to a Vi compatible value.
6992 * When 'compatible' is unset: Set all options that have a different default
6993 * for Vim (without the P_VI_DEF flag) to that default.
6994 */
6995 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006996compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 int opt_idx;
6999
Bram Moolenaardac13472019-09-16 21:06:21 +02007000 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
7002 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
7003 set_option_default(opt_idx, OPT_FREE, p_cp);
7004 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02007005 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006}
7007
Bram Moolenaardac13472019-09-16 21:06:21 +02007008#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010/*
7011 * fill_breakat_flags() -- called when 'breakat' changes value.
7012 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007013 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007014fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007016 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 int i;
7018
7019 for (i = 0; i < 256; i++)
7020 breakat_flags[i] = FALSE;
7021
7022 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007023 for (p = p_breakat; *p; p++)
7024 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026#endif
7027
7028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 * Check if backspacing over something is allowed.
7030 */
7031 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007032can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007033 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02007035#ifdef FEAT_JOB_CHANNEL
7036 if (what == BS_START && bt_prompt(curbuf))
7037 return FALSE;
7038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 switch (*p_bs)
7040 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007041 case '3': return TRUE;
7042 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 case '1': return (what != BS_START);
7044 case '0': return FALSE;
7045 }
7046 return vim_strchr(p_bs, what) != NULL;
7047}
7048
7049/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007050 * Return the effective 'scrolloff' value for the current window, using the
7051 * global value when appropriate.
7052 */
7053 long
7054get_scrolloff_value(void)
7055{
7056 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7057}
7058
7059/*
7060 * Return the effective 'sidescrolloff' value for the current window, using the
7061 * global value when appropriate.
7062 */
7063 long
7064get_sidescrolloff_value(void)
7065{
7066 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7067}
7068
7069/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007070 * Get the local or global value of 'backupcopy'.
7071 */
7072 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007073get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007074{
7075 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7076}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007077
Gary Johnson53ba05b2021-07-26 22:19:10 +02007078/*
Christian Brabandtc53b4672022-01-15 10:01:05 +00007079 * Get the local or global value of 'formatlistpat'.
7080 */
7081 char_u *
7082get_flp_value(buf_T *buf)
7083{
Christian Brabandtc53b4672022-01-15 10:01:05 +00007084 if (buf->b_p_flp == NULL || *buf->b_p_flp == NUL)
7085 return p_flp;
7086 return buf->b_p_flp;
7087}
7088
7089/*
Gary Johnson53ba05b2021-07-26 22:19:10 +02007090 * Get the local or global value of the 'virtualedit' flags.
7091 */
7092 unsigned int
7093get_ve_flags(void)
7094{
Gary Johnson51ad8502021-08-03 18:33:08 +02007095 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7096 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007097}
7098
Bram Moolenaaree857022019-11-09 23:26:40 +01007099#if defined(FEAT_LINEBREAK) || defined(PROTO)
7100/*
7101 * Get the local or global value of 'showbreak'.
7102 */
7103 char_u *
7104get_showbreak_value(win_T *win)
7105{
7106 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7107 return p_sbr;
7108 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7109 return empty_option;
7110 return win->w_p_sbr;
7111}
7112#endif
7113
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007114#if defined(FEAT_EVAL) || defined(PROTO)
7115/*
7116 * Get window or buffer local options.
7117 */
7118 dict_T *
7119get_winbuf_options(int bufopt)
7120{
7121 dict_T *d;
7122 int opt_idx;
7123
7124 d = dict_alloc();
7125 if (d == NULL)
7126 return NULL;
7127
Bram Moolenaardac13472019-09-16 21:06:21 +02007128 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007129 {
7130 struct vimoption *opt = &options[opt_idx];
7131
7132 if ((bufopt && (opt->indir & PV_BUF))
7133 || (!bufopt && (opt->indir & PV_WIN)))
7134 {
7135 char_u *varp = get_varp(opt);
7136
7137 if (varp != NULL)
7138 {
7139 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007140 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007141 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007142 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007143 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007144 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007145 }
7146 }
7147 }
7148
7149 return d;
7150}
7151#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007152
Bram Moolenaardac13472019-09-16 21:06:21 +02007153#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007154/*
7155 * This is called when 'culopt' is changed
7156 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007157 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007158fill_culopt_flags(char_u *val, win_T *wp)
7159{
7160 char_u *p;
7161 char_u culopt_flags_new = 0;
7162
7163 if (val == NULL)
7164 p = wp->w_p_culopt;
7165 else
7166 p = val;
7167 while (*p != NUL)
7168 {
7169 if (STRNCMP(p, "line", 4) == 0)
7170 {
7171 p += 4;
7172 culopt_flags_new |= CULOPT_LINE;
7173 }
7174 else if (STRNCMP(p, "both", 4) == 0)
7175 {
7176 p += 4;
7177 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7178 }
7179 else if (STRNCMP(p, "number", 6) == 0)
7180 {
7181 p += 6;
7182 culopt_flags_new |= CULOPT_NBR;
7183 }
7184 else if (STRNCMP(p, "screenline", 10) == 0)
7185 {
7186 p += 10;
7187 culopt_flags_new |= CULOPT_SCRLINE;
7188 }
7189
7190 if (*p != ',' && *p != NUL)
7191 return FAIL;
7192 if (*p == ',')
7193 ++p;
7194 }
7195
7196 // Can't have both "line" and "screenline".
7197 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7198 return FAIL;
7199 wp->w_p_culopt_flags = culopt_flags_new;
7200
7201 return OK;
7202}
7203#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007204
7205/*
7206 * Get the value of 'magic' adjusted for Vim9 script.
7207 */
7208 int
7209magic_isset(void)
7210{
7211 switch (magic_overruled)
7212 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007213 case OPTION_MAGIC_ON: return TRUE;
7214 case OPTION_MAGIC_OFF: return FALSE;
7215 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007216 }
7217#ifdef FEAT_EVAL
7218 if (in_vim9script())
7219 return TRUE;
7220#endif
7221 return p_magic;
7222}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007223
7224/*
7225 * Set the callback function value for an option that accepts a function name,
7226 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7227 * Returns OK if the option is successfully set to a function, otherwise
7228 * returns FAIL.
7229 */
7230 int
7231option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7232{
7233#ifdef FEAT_EVAL
7234 typval_T *tv;
7235 callback_T cb;
7236
7237 if (optval == NULL || *optval == NUL)
7238 {
7239 free_callback(optcb);
7240 return OK;
7241 }
7242
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007243 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007244 || (STRNCMP(optval, "function(", 9) == 0)
7245 || (STRNCMP(optval, "funcref(", 8) == 0))
7246 // Lambda expression or a funcref
7247 tv = eval_expr(optval, NULL);
7248 else
7249 // treat everything else as a function name string
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00007250 tv = alloc_string_tv(vim_strsave(optval));
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007251 if (tv == NULL)
7252 return FAIL;
7253
7254 cb = get_callback(tv);
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00007255 if (cb.cb_name == NULL || *cb.cb_name == NUL)
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007256 {
7257 free_tv(tv);
7258 return FAIL;
7259 }
7260
7261 free_callback(optcb);
7262 set_callback(optcb, &cb);
7263 free_tv(tv);
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00007264
7265 // when using Vim9 style "import.funcname" it needs to be expanded to
7266 // "import#funcname".
7267 expand_autload_callback(optcb);
7268
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007269 return OK;
7270#else
7271 return FAIL;
7272#endif
7273}