blob: 22dcfc5d1f1b5cf441b2f67a1dba561a6570f4d3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Code to handle user-settable options. This is all pretty much table-
12 * driven. Checklist for adding a new option:
13 * - Put it in the options array below (copy an existing entry).
14 * - For a global option: Add a variable for it in option.h.
15 * - For a buffer or window local option:
16 * - Add a PV_XX entry to the enum below.
17 * - Add a variable to the window or buffer struct in structs.h.
18 * - For a window option, add some code to copy_winopt().
19 * - For a buffer option, add some code to buf_copy_options().
20 * - For a buffer string option, add code to check_buf_options().
21 * - If it's a numeric option, add any necessary bounds checks to do_set().
22 * - If it's a list of flags, add some code in do_set(), search for WW_ALL.
23 * - When adding an option with expansion (P_EXPAND), but with a different
24 * default for Vi and Vim (no P_VI_DEF), add some code at VIMEXP.
Bram Moolenaarcea912a2016-10-12 14:20:24 +020025 * - Add documentation! One line in doc/quickref.txt, full description in
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 * options.txt, and any other related places.
27 * - Add an entry in runtime/optwin.vim.
28 * When making changes:
29 * - Adjust the help for the option in doc/option.txt.
30 * - When an entry has the P_VIM flag, or is lacking the P_VI_DEF flag, add a
31 * comment at the help for the 'compatible' option.
32 */
33
34#define IN_OPTION_C
35#include "vim.h"
Bram Moolenaar0eddca42019-09-12 22:26:43 +020036#include "optiondefs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static void set_options_default(int opt_flags);
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +010039static void set_string_default_esc(char *name, char_u *val, int escape);
Bram Moolenaarb00ef052020-09-12 14:53:53 +020040static char_u *find_dup_item(char_u *origval, char_u *newval, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010041static char_u *option_expand(int opt_idx, char_u *val);
42static void didset_options(void);
43static void didset_options2(void);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010045static long_u *insecure_flag(int opt_idx, int opt_flags);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000046#else
47# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
48#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010049static char *set_bool_option(int opt_idx, char_u *varp, int value, int opt_flags);
50static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, int opt_flags);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020051static int find_key_option(char_u *arg_arg, int has_lt);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void showoptions(int all, int opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +020053static int optval_default(struct vimoption *, char_u *varp, int compatible);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010054static void showoneopt(struct vimoption *, int opt_flags);
Bram Moolenaared18f2c2019-01-24 20:30:52 +010055static int put_setstring(FILE *fd, char *cmd, char *name, char_u **valuep, long_u flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep);
57static int put_setbool(FILE *fd, char *cmd, char *name, int value);
Bram Moolenaardac13472019-09-16 21:06:21 +020058static int istermoption(struct vimoption *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static char_u *get_varp_scope(struct vimoption *p, int opt_flags);
60static char_u *get_varp(struct vimoption *);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061static void check_win_options(win_T *win);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void option_value2string(struct vimoption *, int opt_flags);
63static void check_winopt(winopt_T *wop);
64static int wc_use_keyname(char_u *varp, long *wcp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void paste_option_changed(void);
66static void compatible_set(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
69 * Initialize the options, first part.
70 *
71 * Called only once from main(), just after creating the first buffer.
Bram Moolenaar07268702018-03-01 21:57:32 +010072 * If "clean_arg" is TRUE Vim was started with --clean.
Bram Moolenaar071d4272004-06-13 20:20:40 +000073 */
74 void
Bram Moolenaar07268702018-03-01 21:57:32 +010075set_init_1(int clean_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000076{
77 char_u *p;
78 int opt_idx;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000079 long_u n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81#ifdef FEAT_LANGMAP
82 langmap_init();
83#endif
84
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010085 // Be Vi compatible by default
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 p_cp = TRUE;
87
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010088 // Use POSIX compatibility when $VIM_POSIX is set.
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +000090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 set_string_default("cpo", (char_u *)CPO_ALL);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +020092 set_string_default("shm", (char_u *)SHM_POSIX);
Bram Moolenaar26a60b42005-02-22 08:49:11 +000093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000094
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 /*
96 * Find default value for 'shell' option.
Bram Moolenaar7c626922005-02-07 22:01:03 +000097 * Don't use it if it is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
Bram Moolenaar7c626922005-02-07 22:01:03 +000099 if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100100#if defined(MSWIN)
Bram Moolenaar7c626922005-02-07 22:01:03 +0000101 || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100102 || ((p = (char_u *)default_shell()) != NULL && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +0000104 )
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200105#if defined(MSWIN)
106 {
107 // For MS-Windows put the path in quotes instead of escaping spaces.
108 char_u *cmd;
109 size_t len;
110
111 if (vim_strchr(p, ' ') != NULL)
112 {
113 len = STRLEN(p) + 3; // two quotes and a trailing NUL
114 cmd = alloc(len);
Bram Moolenaar1671de32019-10-05 21:35:16 +0200115 if (cmd != NULL)
116 {
117 vim_snprintf((char *)cmd, len, "\"%s\"", p);
118 set_string_default("sh", cmd);
119 vim_free(cmd);
120 }
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200121 }
122 else
123 set_string_default("sh", p);
124 }
125#else
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100126 set_string_default_esc("sh", p, TRUE);
Bram Moolenaar2efc44b2019-10-05 12:09:32 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_WILDIGN
130 /*
131 * Set the default for 'backupskip' to include environment variables for
132 * temp files.
133 */
134 {
135# ifdef UNIX
136 static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
137# else
138 static char *(names[3]) = {"TMPDIR", "TEMP", "TMP"};
139# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000140 int len;
141 garray_T ga;
142 int mustfree;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200143 char_u *item;
144
145 opt_idx = findoption((char_u *)"backupskip");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
147 ga_init2(&ga, 1, 100);
K.Takataeeec2542021-06-02 13:28:16 +0200148 for (n = 0; n < (long)ARRAY_LENGTH(names); ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000150 mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# ifdef UNIX
152 if (*names[n] == NUL)
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200153# ifdef MACOS_X
154 p = (char_u *)"/private/tmp";
155# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 p = (char_u *)"/tmp";
Bram Moolenaarb8e22a02018-04-12 21:37:34 +0200157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000160 p = vim_getenv((char_u *)names[n], &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (p != NULL && *p != NUL)
162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100163 // First time count the NUL, otherwise count the ','.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000164 len = (int)STRLEN(p) + 3;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200165 item = alloc(len);
166 STRCPY(item, p);
167 add_pathsep(item);
168 STRCAT(item, "*");
169 if (find_dup_item(ga.ga_data, item, options[opt_idx].flags)
170 == NULL
171 && ga_grow(&ga, len) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (ga.ga_len > 0)
174 STRCAT(ga.ga_data, ",");
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200175 STRCAT(ga.ga_data, item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 ga.ga_len += len;
177 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200178 vim_free(item);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 if (mustfree)
181 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
183 if (ga.ga_data != NULL)
184 {
185 set_string_default("bsk", ga.ga_data);
186 vim_free(ga.ga_data);
187 }
188 }
189#endif
190
191 /*
192 * 'maxmemtot' and 'maxmem' may have to be adjusted for available memory
193 */
194 opt_idx = findoption((char_u *)"maxmemtot");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000195 if (opt_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000197#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
198 if (options[opt_idx].def_val[VI_DEFAULT] == (char_u *)0L)
199#endif
200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef HAVE_AVAIL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100202 // Use amount of memory available at this moment.
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200203 n = (mch_avail_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#else
205# ifdef HAVE_TOTAL_MEM
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100206 // Use amount of memory available to Vim.
Bram Moolenaar914572a2007-05-01 11:37:47 +0000207 n = (mch_total_mem(FALSE) >> 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# else
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000209 n = (0x7fffffff >> 11);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# endif
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000213 opt_idx = findoption((char_u *)"maxmem");
214 if (opt_idx >= 0)
215 {
216#if !defined(HAVE_AVAIL_MEM) && !defined(HAVE_TOTAL_MEM)
Bram Moolenaar35be4532015-12-11 22:38:36 +0100217 if ((long)(long_i)options[opt_idx].def_val[VI_DEFAULT] > (long)n
218 || (long)(long_i)options[opt_idx].def_val[VI_DEFAULT] == 0L)
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000219#endif
220 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)n;
221 }
222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 }
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_SEARCHPATH
226 {
227 char_u *cdpath;
228 char_u *buf;
229 int i;
230 int j;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000231 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100233 // Initialize the 'cdpath' option's default value.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000234 cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (cdpath != NULL)
236 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200237 buf = alloc((STRLEN(cdpath) << 1) + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (buf != NULL)
239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100240 buf[0] = ','; // start with ",", current dir first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 j = 1;
242 for (i = 0; cdpath[i] != NUL; ++i)
243 {
244 if (vim_ispathlistsep(cdpath[i]))
245 buf[j++] = ',';
246 else
247 {
248 if (cdpath[i] == ' ' || cdpath[i] == ',')
249 buf[j++] = '\\';
250 buf[j++] = cdpath[i];
251 }
252 }
253 buf[j] = NUL;
254 opt_idx = findoption((char_u *)"cdpath");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000255 if (opt_idx >= 0)
256 {
257 options[opt_idx].def_val[VI_DEFAULT] = buf;
258 options[opt_idx].flags |= P_DEF_ALLOCED;
259 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +0200260 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100261 vim_free(buf); // cannot happen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000263 if (mustfree)
264 vim_free(cdpath);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 }
266 }
267#endif
268
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100269#if defined(FEAT_POSTSCRIPT) && (defined(MSWIN) || defined(VMS) || defined(EBCDIC) || defined(MAC) || defined(hpux))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100270 // Set print encoding on platforms that don't default to latin1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 set_string_default("penc",
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100272# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 (char_u *)"cp1252"
274# else
275# ifdef VMS
276 (char_u *)"dec-mcs"
277# else
278# ifdef EBCDIC
279 (char_u *)"ebcdic-uk"
280# else
281# ifdef MAC
282 (char_u *)"mac-roman"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100283# else // HPUX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 (char_u *)"hp-roman8"
285# endif
286# endif
287# endif
288# endif
289 );
290#endif
291
292#ifdef FEAT_POSTSCRIPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100293 // 'printexpr' must be allocated to be able to evaluate it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 set_string_default("pexpr",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100295# if defined(MSWIN)
Bram Moolenaared203462004-06-16 11:19:22 +0000296 (char_u *)"system('copy' . ' ' . v:fname_in . (&printdevice == '' ? ' LPT1:' : (' \"' . &printdevice . '\"'))) . delete(v:fname_in)"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297# else
298# ifdef VMS
299 (char_u *)"system('print/delete' . (&printdevice == '' ? '' : ' /queue=' . &printdevice) . ' ' . v:fname_in)"
300
301# else
302 (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
303# endif
304# endif
305 );
306#endif
307
308 /*
309 * Set all the options (except the terminal options) to their default
310 * value. Also set the global value for local options.
311 */
312 set_options_default(0);
313
Bram Moolenaar07268702018-03-01 21:57:32 +0100314#ifdef CLEAN_RUNTIMEPATH
315 if (clean_arg)
316 {
317 opt_idx = findoption((char_u *)"runtimepath");
318 if (opt_idx >= 0)
319 {
320 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
321 p_rtp = (char_u *)CLEAN_RUNTIMEPATH;
322 }
323 opt_idx = findoption((char_u *)"packpath");
324 if (opt_idx >= 0)
325 {
326 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)CLEAN_RUNTIMEPATH;
327 p_pp = (char_u *)CLEAN_RUNTIMEPATH;
328 }
329 }
330#endif
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef FEAT_GUI
333 if (found_reverse_arg)
334 set_option_value((char_u *)"bg", 0L, (char_u *)"dark", 0);
335#endif
336
337 curbuf->b_p_initialized = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100338 curbuf->b_p_ar = -1; // no local 'autoread' value
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100339 curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 check_buf_options(curbuf);
341 check_win_options(curwin);
342 check_options();
343
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100344 // Must be before option_expand(), because that one needs vim_isIDc()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 didset_options();
346
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000347#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100348 // Use the current chartab for the generic chartab. This is not in
349 // didset_options() because it only depends on 'encoding'.
Bram Moolenaar6bb68362005-03-22 23:03:44 +0000350 init_spell_chartab();
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 /*
354 * Expand environment variables and things like "~" for the defaults.
355 * If option_expand() returns non-NULL the variable is expanded. This can
356 * only happen for non-indirect options.
357 * Also set the default to the expanded value, so ":set" does not list
358 * them.
359 * Don't set the P_ALLOCED flag, because we don't want to free the
360 * default.
361 */
Bram Moolenaardac13472019-09-16 21:06:21 +0200362 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
364 if ((options[opt_idx].flags & P_GETTEXT)
365 && options[opt_idx].var != NULL)
366 p = (char_u *)_(*(char **)options[opt_idx].var);
367 else
368 p = option_expand(opt_idx, NULL);
369 if (p != NULL && (p = vim_strsave(p)) != NULL)
370 {
371 *(char_u **)options[opt_idx].var = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100372 // VIMEXP
373 // Defaults for all expanded options are currently the same for Vi
374 // and Vim. When this changes, add some code here! Also need to
375 // split P_DEF_ALLOCED in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (options[opt_idx].flags & P_DEF_ALLOCED)
377 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
378 options[opt_idx].def_val[VI_DEFAULT] = p;
379 options[opt_idx].flags |= P_DEF_ALLOCED;
380 }
381 }
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385#if defined(FEAT_ARABIC)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386 // Detect use of mlterm.
387 // Mlterm is a terminal emulator akin to xterm that has some special
388 // abilities (bidi namely).
389 // NOTE: mlterm's author is being asked to 'set' a variable
390 // instead of an environment variable due to inheritance.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (mch_getenv((char_u *)"MLTERM") != NULL)
392 set_option_value((char_u *)"tbidi", 1L, NULL, 0);
393#endif
394
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200395 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Bram Moolenaar4f974752019-02-17 17:44:42 +0100397# if defined(MSWIN) && defined(FEAT_GETTEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 /*
399 * If $LANG isn't set, try to get a good value for it. This makes the
400 * right language be used automatically. Don't do this for English.
401 */
402 if (mch_getenv((char_u *)"LANG") == NULL)
403 {
404 char buf[20];
405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100406 // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
407 // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
408 // only the first two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 n = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVLANGNAME,
410 (LPTSTR)buf, 20);
411 if (n >= 2 && STRNICMP(buf, "en", 2) != 0)
412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100413 // There are a few exceptions (probably more)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (STRNICMP(buf, "cht", 3) == 0 || STRNICMP(buf, "zht", 3) == 0)
415 STRCPY(buf, "zh_TW");
416 else if (STRNICMP(buf, "chs", 3) == 0
417 || STRNICMP(buf, "zhc", 3) == 0)
418 STRCPY(buf, "zh_CN");
419 else if (STRNICMP(buf, "jp", 2) == 0)
420 STRCPY(buf, "ja");
421 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 buf[2] = NUL; // truncate to two-letter code
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100423 vim_setenv((char_u *)"LANG", (char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000426# else
Bram Moolenaar9d47f172006-03-15 23:03:01 +0000427# ifdef MACOS_CONVERT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100428 // Moved to os_mac_conv.c to avoid dependency problems.
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000429 mac_lang_init();
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# endif
432
K.Takataf883d902021-05-30 18:04:19 +0200433# ifdef MSWIN
434 // MS-Windows has builtin support for conversion to and from Unicode, using
435 // "utf-8" for 'encoding' should work best for most users.
436 p = vim_strsave((char_u *)ENC_DFLT);
437# else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100438 // enc_locale() will try to find the encoding of the current locale.
K.Takataf883d902021-05-30 18:04:19 +0200439 // This works best for properly configured systems, old and new.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 p = enc_locale();
K.Takataf883d902021-05-30 18:04:19 +0200441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (p != NULL)
443 {
444 char_u *save_enc;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // Try setting 'encoding' and check if the value is valid.
K.Takataf883d902021-05-30 18:04:19 +0200447 // If not, go back to the default encoding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 save_enc = p_enc;
449 p_enc = p;
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000450 if (STRCMP(p_enc, "gb18030") == 0)
451 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // We don't support "gb18030", but "cp936" is a good substitute
453 // for practical purposes, thus use that. It's not an alias to
454 // still support conversion between gb18030 and utf-8.
Bram Moolenaar733f0a22007-03-02 18:56:27 +0000455 p_enc = vim_strsave((char_u *)"cp936");
456 vim_free(p);
457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 if (mb_init() == NULL)
459 {
460 opt_idx = findoption((char_u *)"encoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000461 if (opt_idx >= 0)
462 {
463 options[opt_idx].def_val[VI_DEFAULT] = p_enc;
464 options[opt_idx].flags |= P_DEF_ALLOCED;
465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
Bram Moolenaard0573012017-10-28 21:11:06 +0200467#if defined(MSWIN) || defined(MACOS_X) || defined(VMS)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100468 if (STRCMP(p_enc, "latin1") == 0 || enc_utf8)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100470 // Adjust the default for 'isprint' and 'iskeyword' to match
471 // latin1. Also set the defaults for when 'nocompatible' is
472 // set.
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000473 set_string_option_direct((char_u *)"isp", -1,
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +0000474 ISP_LATIN1, OPT_FREE, SID_NONE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000475 set_string_option_direct((char_u *)"isk", -1,
476 ISK_LATIN1, OPT_FREE, SID_NONE);
477 opt_idx = findoption((char_u *)"isp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000478 if (opt_idx >= 0)
479 options[opt_idx].def_val[VIM_DEFAULT] = ISP_LATIN1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000480 opt_idx = findoption((char_u *)"isk");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000481 if (opt_idx >= 0)
482 options[opt_idx].def_val[VIM_DEFAULT] = ISK_LATIN1;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000483 (void)init_chartab();
484 }
485#endif
486
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200487#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 // Win32 console: When GetACP() returns a different value from
489 // GetConsoleCP() set 'termencoding'.
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200490 if (
491# ifdef VIMDLL
492 (!gui.in_use && !gui.starting) &&
493# endif
494 GetACP() != GetConsoleCP())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 char buf[50];
497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100498 // Win32 console: In ConPTY, GetConsoleCP() returns zero.
499 // Use an alternative value.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100500 if (GetConsoleCP() == 0)
501 sprintf(buf, "cp%ld", (long)GetACP());
502 else
503 sprintf(buf, "cp%ld", (long)GetConsoleCP());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 p_tenc = vim_strsave((char_u *)buf);
505 if (p_tenc != NULL)
506 {
507 opt_idx = findoption((char_u *)"termencoding");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000508 if (opt_idx >= 0)
509 {
510 options[opt_idx].def_val[VI_DEFAULT] = p_tenc;
511 options[opt_idx].flags |= P_DEF_ALLOCED;
512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 convert_setup(&input_conv, p_tenc, p_enc);
514 convert_setup(&output_conv, p_enc, p_tenc);
515 }
516 else
517 p_tenc = empty_option;
518 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100519#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100520#if defined(MSWIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 // $HOME may have characters in active code page.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000522 init_homedir();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525 else
526 {
527 vim_free(p_enc);
528 p_enc = save_enc;
529 }
530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532#ifdef FEAT_MULTI_LANG
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 // Set the default for 'helplang'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 set_helplang_default(get_mess_lang());
535#endif
536}
537
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200538static char_u *fencs_utf8_default = (char_u *)"ucs-bom,utf-8,default,latin1";
539
540/*
541 * Set the "fileencodings" option to the default value for when 'encoding' is
542 * utf-8.
543 */
544 void
545set_fencs_unicode()
546{
547 set_string_option_direct((char_u *)"fencs", -1, fencs_utf8_default,
548 OPT_FREE, 0);
549}
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/*
552 * Set an option to its default value.
553 * This does not take care of side effects!
554 */
555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100556set_option_default(
557 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100558 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
559 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 char_u *varp; // pointer to variable for current option
562 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000564 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
566
567 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
568 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100569 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
572 if (flags & P_STRING)
573 {
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200574 // 'fencs' default value depends on 'encoding'
575 if (options[opt_idx].var == (char_u *)&p_fencs && enc_utf8)
576 set_fencs_unicode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100577 // Use set_string_option_direct() for local options to handle
578 // freeing and allocating the value.
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +0200579 else if (options[opt_idx].indir != PV_NONE)
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200580 set_string_option_direct(NULL, opt_idx,
581 options[opt_idx].def_val[dvi], opt_flags, 0);
582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200584 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
585 free_string_option(*(char_u **)(varp));
586 *(char_u **)varp = options[opt_idx].def_val[dvi];
587 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589 }
590 else if (flags & P_NUM)
591 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000592 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 win_comp_scroll(curwin);
594 else
595 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100596 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
597
598 if ((long *)varp == &curwin->w_p_so
599 || (long *)varp == &curwin->w_p_siso)
600 // 'scrolloff' and 'sidescrolloff' local values have a
601 // different default value than the global default.
602 *(long *)varp = -1;
603 else
604 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (both)
607 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100608 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 // the cast to long is required for Manx C, long_i is needed for
614 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000615 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000616#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000618 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
619 *(int *)varp = FALSE;
620#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100621 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 if (both)
623 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
624 *(int *)varp;
625 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000628 flagsp = insecure_flag(opt_idx, opt_flags);
629 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631
632#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200633 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635}
636
637/*
638 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200639 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 */
641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100642set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100643 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000647 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaardac13472019-09-16 21:06:21 +0200649 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200650 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200651 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100652 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200653# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200654 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200655 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200656# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100657 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 set_option_default(i, opt_flags, p_cp);
659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100660 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000661 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200663#ifdef FEAT_CINDENT
664 parse_cino(curbuf);
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666}
667
668/*
669 * Set the Vi-default value of a string option.
670 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100671 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100673 static void
674set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 char_u *p;
677 int opt_idx;
678
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100679 if (escape && vim_strchr(val, ' ') != NULL)
680 p = vim_strsave_escaped(val, (char_u *)" ");
681 else
682 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000686 if (opt_idx >= 0)
687 {
688 if (options[opt_idx].flags & P_DEF_ALLOCED)
689 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
690 options[opt_idx].def_val[VI_DEFAULT] = p;
691 options[opt_idx].flags |= P_DEF_ALLOCED;
692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694}
695
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100696 void
697set_string_default(char *name, char_u *val)
698{
699 set_string_default_esc(name, val, FALSE);
700}
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200703 * For an option value that contains comma separated items, find "newval" in
704 * "origval". Return NULL if not found.
705 */
706 static char_u *
707find_dup_item(char_u *origval, char_u *newval, long_u flags)
708{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200709 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200710 size_t newlen;
711 char_u *s;
712
713 if (origval == NULL)
714 return NULL;
715
716 newlen = STRLEN(newval);
717 for (s = origval; *s != NUL; ++s)
718 {
719 if ((!(flags & P_COMMA)
720 || s == origval
721 || (s[-1] == ',' && !(bs & 1)))
722 && STRNCMP(s, newval, newlen) == 0
723 && (!(flags & P_COMMA)
724 || s[newlen] == ','
725 || s[newlen] == NUL))
726 return s;
727 // Count backslashes. Only a comma with an even number of backslashes
728 // or a single backslash preceded by a comma before it is recognized as
729 // a separator.
730 if ((s > origval + 1
731 && s[-1] == '\\'
732 && s[-2] != ',')
733 || (s == origval + 1
734 && s[-1] == '\\'))
735 ++bs;
736 else
737 bs = 0;
738 }
739 return NULL;
740}
741
742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * Set the Vi-default value of a number option.
744 * Used for 'lines' and 'columns'.
745 */
746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100747set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000749 int opt_idx;
750
751 opt_idx = findoption((char_u *)name);
752 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000753 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754}
755
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200756/*
757 * Set all window-local and buffer-local options to the Vim default.
758 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200759 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200760 */
761 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200762set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200763{
764 win_T *save_curwin = curwin;
765 int i;
766
767 curwin = wp;
768 curbuf = curwin->w_buffer;
769 block_autocmds();
770
Bram Moolenaardac13472019-09-16 21:06:21 +0200771 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200772 {
773 struct vimoption *p = &(options[i]);
774 char_u *varp = get_varp_scope(p, OPT_LOCAL);
775
776 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200777 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200778 && !(options[i].flags & P_NODEFAULT)
779 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200780 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200781 }
782
783 unblock_autocmds();
784 curwin = save_curwin;
785 curbuf = curwin->w_buffer;
786}
787
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000788#if defined(EXITFREE) || defined(PROTO)
789/*
790 * Free all options.
791 */
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000794{
795 int i;
796
Bram Moolenaardac13472019-09-16 21:06:21 +0200797 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000798 {
799 if (options[i].indir == PV_NONE)
800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100801 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100802 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000803 free_string_option(*(char_u **)options[i].var);
804 if (options[i].flags & P_DEF_ALLOCED)
805 free_string_option(options[i].def_val[VI_DEFAULT]);
806 }
807 else if (options[i].var != VAR_WIN
808 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100809 // buffer-local option: free global value
Bram Moolenaar77111e22021-07-29 21:11:30 +0200810 clear_string_option((char_u **)options[i].var);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000811 }
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +0000812 free_operatorfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +0000813 free_tagfunc_option();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000814}
815#endif
816
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818/*
819 * Initialize the options, part two: After getting Rows and Columns and
820 * setting 'term'.
821 */
822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000825 int idx;
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100828 * 'scroll' defaults to half the window height. The stored default is zero,
829 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000832 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000833 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 comp_col();
835
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000836 /*
837 * 'window' is only for backwards compatibility with Vi.
838 * Default is Rows - 1.
839 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000840 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000841 p_window = Rows - 1;
842 set_number_default("window", Rows - 1);
843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100844 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100845#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000846 /*
847 * If 'background' wasn't set by the user, try guessing the value,
848 * depending on the terminal name. Only need to check for terminals
849 * with a dark background, that can handle color.
850 */
851 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000852 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
853 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000855 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 // don't mark it as set, when starting the GUI it may be
857 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000858 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000861
862#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000864#endif
865#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100866 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000867#endif
868#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871}
872
873/*
874 * Initialize the options, part three: After reading the .vimrc
875 */
876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100879#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880/*
881 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
882 * This is done after other initializations, where 'shell' might have been
883 * set, but only if they have not been set before.
884 */
885 char_u *p;
886 int idx_srr;
887 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100888# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 int idx_sp;
890 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000894 if (idx_srr < 0)
895 do_srr = FALSE;
896 else
897 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100898# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000900 if (idx_sp < 0)
901 do_sp = FALSE;
902 else
903 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100904# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200905 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (p != NULL)
907 {
908 /*
909 * Default for p_sp is "| tee", for p_srr is ">".
910 * For known shells it is changed here to include stderr.
911 */
912 if ( fnamecmp(p, "csh") == 0
913 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100914# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 || fnamecmp(p, "csh.exe") == 0
916 || fnamecmp(p, "tcsh.exe") == 0
917# endif
918 )
919 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100920# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (do_sp)
922 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100923# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100925# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
929 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (do_srr)
932 {
933 p_srr = (char_u *)">&";
934 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
935 }
936 }
Mike Williams12795022021-06-28 20:53:58 +0200937# ifdef MSWIN
Mike Williamsa3d1b292021-06-30 20:56:00 +0200938 // Windows PowerShell output is UTF-16 with BOM so re-encode to the
939 // current codepage.
Mike Williams12795022021-06-28 20:53:58 +0200940 else if ( fnamecmp(p, "powershell") == 0
941 || fnamecmp(p, "powershell.exe") == 0
942 )
943 {
944# if defined(FEAT_QUICKFIX)
945 if (do_sp)
946 {
947 p_sp = (char_u *)"2>&1 | Out-File -Encoding default";
948 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
949 }
950# endif
951 if (do_srr)
952 {
953 p_srr = (char_u *)"2>&1 | Out-File -Encoding default";
954 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
955 }
956 }
957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 else
Natanael Copa56318362021-05-06 18:46:35 +0200959 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if ( fnamecmp(p, "sh") == 0
961 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200962 || fnamecmp(p, "mksh") == 0
963 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000965 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200967 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200968 || fnamecmp(p, "ash") == 0
969 || fnamecmp(p, "dash") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200970 || fnamecmp(p, "pwsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100971# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 || fnamecmp(p, "cmd") == 0
973 || fnamecmp(p, "sh.exe") == 0
974 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200975 || fnamecmp(p, "mksh.exe") == 0
976 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000978 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 || fnamecmp(p, "bash.exe") == 0
980 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200981 || fnamecmp(p, "dash.exe") == 0
Mike Williamsa3d1b292021-06-30 20:56:00 +0200982 || fnamecmp(p, "pwsh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100986# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (do_sp)
988 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100989# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991# else
Mike Williamsa3d1b292021-06-30 20:56:00 +0200992 if (fnamecmp(p, "pwsh") == 0)
993 p_sp = (char_u *)">%s 2>&1";
994 else
995 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
998 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (do_srr)
1001 {
1002 p_srr = (char_u *)">%s 2>&1";
1003 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
1004 }
1005 }
1006 vim_free(p);
1007 }
1008#endif
1009
Bram Moolenaar4f974752019-02-17 17:44:42 +01001010#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +01001012 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
1013 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * This is done after other initializations, where 'shell' might have been
Mike Williams12795022021-06-28 20:53:58 +02001015 * set, but only if they have not been set before.
1016 * Default values depend on shell (cmd.exe is default shell):
1017 *
1018 * p_shcf p_sxq
1019 * cmd.exe - "/c" "("
1020 * powershell.exe - "-Command" "\""
Mike Williamsa3d1b292021-06-30 20:56:00 +02001021 * pwsh.exe - "-c" "\""
Mike Williams12795022021-06-28 20:53:58 +02001022 * "sh" like shells - "-c" "\""
1023 *
1024 * For Win32 p_sxq is set instead of p_shq to include shell redirection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 */
Mike Williams12795022021-06-28 20:53:58 +02001026 if (strstr((char *)gettail(p_sh), "powershell") != NULL)
1027 {
1028 int idx_opt;
1029
1030 idx_opt = findoption((char_u *)"shcf");
1031 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1032 {
1033 p_shcf = (char_u*)"-Command";
1034 options[idx_opt].def_val[VI_DEFAULT] = p_shcf;
1035 }
1036
1037 idx_opt = findoption((char_u *)"sxq");
1038 if (idx_opt >= 0 && !(options[idx_opt].flags & P_WAS_SET))
1039 {
1040 p_sxq = (char_u*)"\"";
1041 options[idx_opt].def_val[VI_DEFAULT] = p_sxq;
1042 }
1043 }
1044 else if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 int idx3;
1047
1048 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001049 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 p_shcf = (char_u *)"-c";
1052 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1053 }
1054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001055 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001057 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 {
1059 p_sxq = (char_u *)"\"";
1060 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
Bram Moolenaara64ba222012-02-12 23:23:31 +01001063 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
1064 {
1065 int idx3;
1066
1067 /*
1068 * cmd.exe on Windows will strip the first and last double quote given
1069 * on the command line, e.g. most of the time things like:
1070 * cmd /c "my path/to/echo" "my args to echo"
1071 * become:
1072 * my path/to/echo" "my args to echo
1073 * when executed.
1074 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001075 * To avoid this, set shellxquote to surround the command in
1076 * parenthesis. This appears to make most commands work, without
1077 * breaking commands that worked previously, such as
1078 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001079 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001080 idx3 = findoption((char_u *)"sxq");
1081 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1082 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001083 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001084 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1085 }
1086
Bram Moolenaara64ba222012-02-12 23:23:31 +01001087 idx3 = findoption((char_u *)"shcf");
1088 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1089 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001090 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001091 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1092 }
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094#endif
1095
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001096 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001097 {
1098 int idx_ffs = findoption((char_u *)"ffs");
1099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001100 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001101 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1102 set_fileformat(default_fileformat(), OPT_LOCAL);
1103 }
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 set_title_defaults();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106}
1107
1108#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1109/*
1110 * When 'helplang' is still at its default value, set it to "lang".
1111 * Only the first two characters of "lang" are used.
1112 */
1113 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001114set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 int idx;
1117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001118 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 return;
1120 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001121 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (options[idx].flags & P_ALLOCED)
1124 free_string_option(p_hlg);
1125 p_hlg = vim_strsave(lang);
1126 if (p_hlg == NULL)
1127 p_hlg = empty_option;
1128 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001129 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001130 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001131 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1132 {
1133 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1134 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1135 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001136 // any C like setting, such as C.UTF-8, becomes "en"
1137 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1138 {
1139 p_hlg[0] = 'e';
1140 p_hlg[1] = 'n';
1141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 options[idx].flags |= P_ALLOCED;
1145 }
1146}
1147#endif
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149/*
1150 * 'title' and 'icon' only default to true if they have not been set or reset
1151 * in .vimrc and we can read the old value.
1152 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1153 * they can be reset. This reduces startup time when using X on a remote
1154 * machine.
1155 */
1156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001157set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 int idx1;
1160 long val;
1161
1162 /*
1163 * If GUI is (going to be) used, we can always set the window title and
1164 * icon name. Saves a bit of time, because the X11 display server does
1165 * not need to be contacted.
1166 */
1167 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001168 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170#ifdef FEAT_GUI
1171 if (gui.starting || gui.in_use)
1172 val = TRUE;
1173 else
1174#endif
1175 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001176 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 p_title = val;
1178 }
1179 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001180 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182#ifdef FEAT_GUI
1183 if (gui.starting || gui.in_use)
1184 val = TRUE;
1185 else
1186#endif
1187 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001188 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 p_icon = val;
1190 }
1191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001193 void
1194ex_set(exarg_T *eap)
1195{
1196 int flags = 0;
1197
1198 if (eap->cmdidx == CMD_setlocal)
1199 flags = OPT_LOCAL;
1200 else if (eap->cmdidx == CMD_setglobal)
1201 flags = OPT_GLOBAL;
1202#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001203 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001204 ex_options(eap);
1205 else
1206#endif
1207 {
1208 if (eap->forceit)
1209 flags |= OPT_ONECOLUMN;
1210 (void)do_set(eap->arg, flags);
1211 }
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214/*
1215 * Parse 'arg' for option settings.
1216 *
1217 * 'arg' may be IObuff, but only when no errors can be present and option
1218 * does not need to be expanded with option_expand().
1219 * "opt_flags":
1220 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001221 * OPT_GLOBAL for ":setglobal"
1222 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001224 * OPT_WINONLY to only set window-local options
1225 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 *
1227 * returns FAIL if an error is detected, OK otherwise
1228 */
1229 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001230do_set(
Bram Moolenaar1594f312021-07-08 16:40:13 +02001231 char_u *arg_start, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001232 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233{
Bram Moolenaar1594f312021-07-08 16:40:13 +02001234 char_u *arg = arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 char *errmsg;
1237 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001239 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1240 int nextchar; // next non-white char after option name
1241 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 int len;
1243 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001244 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001246 long_u flags; // flags for current option
1247 char_u *varp = NULL; // pointer to variable for current option
1248 int did_show = FALSE; // already showed one value
1249 int adding; // "opt+=arg"
1250 int prepending; // "opt^=arg"
1251 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 int cp_val = 0;
1253 char_u key_name[2];
1254
1255 if (*arg == NUL)
1256 {
1257 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001258 did_show = TRUE;
1259 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001262 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001265 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001267 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1268 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
1270 /*
1271 * ":set all" show all options.
1272 * ":set all&" set all options to their default value.
1273 */
1274 arg += 3;
1275 if (*arg == '&')
1276 {
1277 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001278 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001280 didset_options();
1281 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001282 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001287 did_show = TRUE;
1288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001290 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 {
1292 showoptions(2, opt_flags);
1293 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001294 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 arg += 7;
1296 }
1297 else
1298 {
1299 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001300 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
1302 prefix = 0;
1303 arg += 2;
1304 }
1305 else if (STRNCMP(arg, "inv", 3) == 0)
1306 {
1307 prefix = 2;
1308 arg += 3;
1309 }
1310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001311 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 key = 0;
1313 if (*arg == '<')
1314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001316 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1318 len = 5;
1319 else
1320 {
1321 len = 1;
1322 while (arg[len] != NUL && arg[len] != '>')
1323 ++len;
1324 }
1325 if (arg[len] != '>')
1326 {
1327 errmsg = e_invarg;
1328 goto skip;
1329 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001330 arg[len] = NUL; // put NUL after name
1331 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001333 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001335 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 else
1338 {
1339 len = 0;
1340 /*
1341 * The two characters after "t_" may not be alphanumeric.
1342 */
1343 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1344 len = 4;
1345 else
1346 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1347 ++len;
1348 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001349 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001351 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001353 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001356 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 afterchar = arg[len];
1358
Bram Moolenaardeb108b2021-07-08 17:35:36 +02001359 if (in_vim9script())
1360 {
1361 char_u *p = skipwhite(arg + len);
1362
1363 // disallow white space before =val, +=val, -=val, ^=val
1364 if (p > arg + len && (p[0] == '='
1365 || (vim_strchr((char_u *)"+-^", p[0]) != NULL
1366 && p[1] == '=')))
1367 {
1368 errmsg = e_no_white_space_allowed_between_option_and;
1369 arg = p;
1370 startarg = p;
1371 goto skip;
1372 }
1373 }
1374 else
Bram Moolenaar208f0b42021-06-20 12:40:08 +02001375 // skip white space, allow ":set ai ?", ":set hlsearch !"
1376 while (VIM_ISWHITE(arg[len]))
1377 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 adding = FALSE;
1380 prepending = FALSE;
1381 removing = FALSE;
1382 if (arg[len] != NUL && arg[len + 1] == '=')
1383 {
1384 if (arg[len] == '+')
1385 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001386 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 ++len;
1388 }
1389 else if (arg[len] == '^')
1390 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001391 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 ++len;
1393 }
1394 else if (arg[len] == '-')
1395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001396 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ++len;
1398 }
1399 }
1400 nextchar = arg[len];
1401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001402 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 {
Bram Moolenaar1594f312021-07-08 16:40:13 +02001404 if (in_vim9script() && arg > arg_start
1405 && vim_strchr((char_u *)"!&<", *arg) != NULL)
1406 errmsg = e_no_white_space_allowed_between_option_and;
1407 else
1408 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 goto skip;
1410 }
1411
1412 if (opt_idx >= 0)
1413 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001414 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001416 // Only give an error message when requesting the value of
1417 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1419 && (!(options[opt_idx].flags & P_BOOL)
1420 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001421 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 goto skip;
1423 }
1424
1425 flags = options[opt_idx].flags;
1426 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1427 }
1428 else
1429 {
1430 flags = P_STRING;
1431 if (key < 0)
1432 {
1433 key_name[0] = KEY2TERMCAP0(key);
1434 key_name[1] = KEY2TERMCAP1(key);
1435 }
1436 else
1437 {
1438 key_name[0] = KS_KEY;
1439 key_name[1] = (key & 0xff);
1440 }
1441 }
1442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001443 // Skip all options that are not window-local (used when showing
1444 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001445 if ((opt_flags & OPT_WINONLY)
1446 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1447 goto skip;
1448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001449 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001450 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1451 && options[opt_idx].var == VAR_WIN)
1452 goto skip;
1453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001454 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001455 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001457 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001458 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001459 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001460 goto skip;
1461 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001462 if ((flags & P_MLE) && !p_mle)
1463 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001464 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001465 goto skip;
1466 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001467#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001468 // In diff mode some options are overruled. This avoids that
1469 // 'foldmethod' becomes "marker" instead of "diff" and that
1470 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001471 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001472 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001473 && (
1474#ifdef FEAT_FOLDING
1475 options[opt_idx].indir == PV_FDM ||
1476#endif
1477 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001478 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481
1482#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001483 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001484 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02001486 errmsg = e_not_allowed_in_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 goto skip;
1488 }
1489#endif
1490
1491 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1492 {
1493 arg += len;
1494 cp_val = p_cp;
1495 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1496 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001497 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
1499 cp_val = FALSE;
1500 arg += 3;
1501 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001502 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 cp_val = TRUE;
1505 arg += 2;
1506 }
1507 }
1508 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001509 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
1511 errmsg = e_trailing;
1512 goto skip;
1513 }
1514 }
1515
1516 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001517 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001518 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 */
1520 if (nextchar == '?'
1521 || (prefix == 1
1522 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1523 && !(flags & P_BOOL)))
1524 {
1525 /*
1526 * print value
1527 */
1528 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 else
1531 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001532 gotocmdline(TRUE); // cursor at status line
1533 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 if (opt_idx >= 0)
1536 {
1537 showoneopt(&options[opt_idx], opt_flags);
1538#ifdef FEAT_EVAL
1539 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001540 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001541 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001542 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001543 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001544 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001545 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001546 (int)options[opt_idx].indir & PV_MASK]);
1547 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001548 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001549 (int)options[opt_idx].indir & PV_MASK]);
1550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551#endif
1552 }
1553 else
1554 {
1555 char_u *p;
1556
1557 p = find_termcode(key_name);
1558 if (p == NULL)
1559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001560 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 goto skip;
1562 }
1563 else
1564 (void)show_one_termcode(key_name, p, TRUE);
1565 }
1566 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001567 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 errmsg = e_trailing;
1569 }
1570 else
1571 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001572 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001573 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001575 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 if (nextchar == '=' || nextchar == ':')
1578 {
1579 errmsg = e_invarg;
1580 goto skip;
1581 }
1582
1583 /*
1584 * ":set opt!": invert
1585 * ":set opt&": reset to default value
1586 * ":set opt<": reset to global value
1587 */
1588 if (nextchar == '!')
1589 value = *(int *)(varp) ^ 1;
1590 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001591 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 ((flags & P_VI_DEF) || cp_val)
1593 ? VI_DEFAULT : VIM_DEFAULT];
1594 else if (nextchar == '<')
1595 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if ((int *)varp == &curbuf->b_p_ar
1598 && opt_flags == OPT_LOCAL)
1599 value = -1;
1600 else
1601 value = *(int *)get_varp_scope(&(options[opt_idx]),
1602 OPT_GLOBAL);
1603 }
1604 else
1605 {
1606 /*
1607 * ":set invopt": invert
1608 * ":set opt" or ":set noopt": set or reset
1609 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001610 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
1612 errmsg = e_trailing;
1613 goto skip;
1614 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001615 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 value = *(int *)(varp) ^ 1;
1617 else
1618 value = prefix;
1619 }
1620
1621 errmsg = set_bool_option(opt_idx, varp, (int)value,
1622 opt_flags);
1623 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001624 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
1626 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1627 || prefix != 1)
1628 {
1629 errmsg = e_invarg;
1630 goto skip;
1631 }
1632
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001633 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {
1635 /*
1636 * Different ways to set a number option:
1637 * & set to default value
1638 * < set to global value
1639 * <xx> accept special key codes for 'wildchar'
1640 * c accept any non-digit for 'wildchar'
1641 * [-]0-9 set number
1642 * other error
1643 */
1644 ++arg;
1645 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001646 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 ((flags & P_VI_DEF) || cp_val)
1648 ? VI_DEFAULT : VIM_DEFAULT];
1649 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001650 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001651 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1652 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001653 if ((long *)varp == &curbuf->b_p_ul
1654 && opt_flags == OPT_LOCAL)
1655 value = NO_LOCAL_UNDOLEVEL;
1656 else
1657 value = *(long *)get_varp_scope(
1658 &(options[opt_idx]), OPT_GLOBAL);
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 else if (((long *)varp == &p_wc
1661 || (long *)varp == &p_wcm)
1662 && (*arg == '<'
1663 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001664 || (*arg != NUL
1665 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 && !VIM_ISDIGIT(*arg))))
1667 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001668 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (value == 0 && (long *)varp != &p_wcm)
1670 {
1671 errmsg = e_invarg;
1672 goto skip;
1673 }
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1676 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001677 // Allow negative (for 'undolevels'), octal and
1678 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001679 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001680 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001681 if (i == 0 || (arg[i] != NUL
1682 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001684 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 goto skip;
1686 }
1687 }
1688 else
1689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001690 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 goto skip;
1692 }
1693
1694 if (adding)
1695 value = *(long *)varp + value;
1696 if (prepending)
1697 value = *(long *)varp * value;
1698 if (removing)
1699 value = *(long *)varp - value;
1700 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001701 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001703 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001705 char_u *save_arg = NULL;
1706 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001707 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001708 char_u *newval;
1709 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001710 char_u *origval_l = NULL;
1711 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001712#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001713 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001714 char_u *saved_origval_l = NULL;
1715 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001716 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001717#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001718 unsigned newlen;
1719 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001720 int new_value_alloced; // new string option
1721 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001723 // When using ":set opt=val" for a global option
1724 // with a local value the local value will be
1725 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001727 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 varp = options[opt_idx].var;
1729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 // The old value is kept until we are sure that the
1731 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001733
Bram Moolenaard7c96872019-06-15 17:12:48 +02001734 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1735 {
1736 origval_l = *(char_u **)get_varp_scope(
1737 &(options[opt_idx]), OPT_LOCAL);
1738 origval_g = *(char_u **)get_varp_scope(
1739 &(options[opt_idx]), OPT_GLOBAL);
1740
1741 // A global-local string option might have an empty
1742 // option as value to indicate that the global
1743 // value should be used.
1744 if (((int)options[opt_idx].indir & PV_BOTH)
1745 && origval_l == empty_option)
1746 origval_l = origval_g;
1747 }
1748
1749 // When setting the local value of a global
1750 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001751 if (((int)options[opt_idx].indir & PV_BOTH)
1752 && (opt_flags & OPT_LOCAL))
1753 origval = *(char_u **)get_varp(
1754 &options[opt_idx]);
1755 else
1756 origval = oldval;
1757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001758 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 newval = options[opt_idx].def_val[
1761 ((flags & P_VI_DEF) || cp_val)
1762 ? VI_DEFAULT : VIM_DEFAULT];
1763 if ((char_u **)varp == &p_bg)
1764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001765 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_GUI
1767 if (gui.in_use)
1768 newval = gui_bg_default();
1769 else
1770#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001771 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
Bram Moolenaar5ffefbb2021-06-13 20:27:36 +02001773 else if ((char_u **)varp == &p_fencs && enc_utf8)
1774 newval = fencs_utf8_default;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001776 // expand environment variables and ~ (since the
1777 // default value was already expanded, only
1778 // required when an environment variable was set
1779 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (newval == NULL)
1781 newval = empty_option;
1782 else
1783 {
1784 s = option_expand(opt_idx, newval);
1785 if (s == NULL)
1786 s = newval;
1787 newval = vim_strsave(s);
1788 }
1789 new_value_alloced = TRUE;
1790 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001791 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 {
1793 newval = vim_strsave(*(char_u **)get_varp_scope(
1794 &(options[opt_idx]), OPT_GLOBAL));
1795 new_value_alloced = TRUE;
1796 }
1797 else
1798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001799 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801 /*
1802 * Set 'keywordprg' to ":help" if an empty
1803 * value was passed to :set by the user.
1804 * Misuse errbuf[] for the resulting string.
1805 */
1806 if (varp == (char_u *)&p_kp
1807 && (*arg == NUL || *arg == ' '))
1808 {
1809 STRCPY(errbuf, ":help");
1810 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001811 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001814 * Convert 'backspace' number to string, for
1815 * adding, prepending and removing string.
1816 */
1817 else if (varp == (char_u *)&p_bs
1818 && VIM_ISDIGIT(**(char_u **)varp))
1819 {
1820 i = getdigits((char_u **)varp);
1821 switch (i)
1822 {
1823 case 0:
1824 *(char_u **)varp = empty_option;
1825 break;
1826 case 1:
1827 *(char_u **)varp = vim_strsave(
1828 (char_u *)"indent,eol");
1829 break;
1830 case 2:
1831 *(char_u **)varp = vim_strsave(
1832 (char_u *)"indent,eol,start");
1833 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001834 case 3:
1835 *(char_u **)varp = vim_strsave(
1836 (char_u *)"indent,eol,nostop");
1837 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001838 }
1839 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001840 if (origval == oldval)
1841 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001842 if (origval_l == oldval)
1843 origval_l = *(char_u **)varp;
1844 if (origval_g == oldval)
1845 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001846 oldval = *(char_u **)varp;
1847 }
1848 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 * Convert 'whichwrap' number to string, for
1850 * backwards compatibility with Vim 3.0.
1851 * Misuse errbuf[] for the resulting string.
1852 */
1853 else if (varp == (char_u *)&p_ww
1854 && VIM_ISDIGIT(*arg))
1855 {
1856 *errbuf = NUL;
1857 i = getdigits(&arg);
1858 if (i & 1)
1859 STRCAT(errbuf, "b,");
1860 if (i & 2)
1861 STRCAT(errbuf, "s,");
1862 if (i & 4)
1863 STRCAT(errbuf, "h,l,");
1864 if (i & 8)
1865 STRCAT(errbuf, "<,>,");
1866 if (i & 16)
1867 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001868 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 errbuf[STRLEN(errbuf) - 1] = NUL;
1870 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001871 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 /*
1874 * Remove '>' before 'dir' and 'bdir', for
1875 * backwards compatibility with version 3.0
1876 */
1877 else if ( *arg == '>'
1878 && (varp == (char_u *)&p_dir
1879 || varp == (char_u *)&p_bdir))
1880 {
1881 ++arg;
1882 }
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 /*
1885 * Copy the new string into allocated memory.
1886 * Can't use set_string_option_direct(), because
1887 * we need to remove the backslashes.
1888 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001889 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 newlen = (unsigned)STRLEN(arg) + 1;
1891 if (adding || prepending || removing)
1892 newlen += (unsigned)STRLEN(origval) + 1;
1893 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001894 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 break;
1896 s = newval;
1897
1898 /*
1899 * Copy the string, skip over escaped chars.
1900 * For MS-DOS and WIN32 backslashes before normal
1901 * file name characters are not removed, and keep
1902 * backslash at start, for "\\machine\path", but
1903 * do remove it for "\\\\machine\\path".
1904 * The reverse is found in ExpandOldSetting().
1905 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001906 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 if (*arg == '\\' && arg[1] != NUL
1909#ifdef BACKSLASH_IN_FILENAME
1910 && !((flags & P_EXPAND)
1911 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001912 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 && (arg[1] != '\\'
1914 || (s == newval
1915 && arg[2] != '\\')))
1916#endif
1917 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001920 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001922 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 mch_memmove(s, arg, (size_t)i);
1924 arg += i;
1925 s += i;
1926 }
1927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 *s++ = *arg++;
1929 }
1930 *s = NUL;
1931
1932 /*
1933 * Expand environment variables and ~.
1934 * Don't do it when adding without inserting a
1935 * comma.
1936 */
1937 if (!(adding || prepending || removing)
1938 || (flags & P_COMMA))
1939 {
1940 s = option_expand(opt_idx, newval);
1941 if (s != NULL)
1942 {
1943 vim_free(newval);
1944 newlen = (unsigned)STRLEN(s) + 1;
1945 if (adding || prepending || removing)
1946 newlen += (unsigned)STRLEN(origval) + 1;
1947 newval = alloc(newlen);
1948 if (newval == NULL)
1949 break;
1950 STRCPY(newval, s);
1951 }
1952 }
1953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001954 // locate newval[] in origval[] when removing it
1955 // and when adding to avoid duplicates
1956 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (removing || (flags & P_NODUP))
1958 {
1959 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001960 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001962 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001963 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 prepending = FALSE;
1966 adding = FALSE;
1967 STRCPY(newval, origval);
1968 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001969
1970 // if no duplicate, move pointer to end of
1971 // original value
1972 if (s == NULL)
1973 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001976 // concatenate the two strings; add a ',' if
1977 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (adding || prepending)
1979 {
1980 comma = ((flags & P_COMMA) && *origval != NUL
1981 && *newval != NUL);
1982 if (adding)
1983 {
1984 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001985 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001986 if (comma && i > 1
1987 && (flags & P_ONECOMMA) == P_ONECOMMA
1988 && origval[i - 1] == ','
1989 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001990 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 mch_memmove(newval + i + comma, newval,
1992 STRLEN(newval) + 1);
1993 mch_memmove(newval, origval, (size_t)i);
1994 }
1995 else
1996 {
1997 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001998 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 if (comma)
2001 newval[i] = ',';
2002 }
2003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002004 // Remove newval[] from origval[]. (Note: "i" has
2005 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (removing)
2007 {
2008 STRCPY(newval, origval);
2009 if (*s)
2010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (flags & P_COMMA)
2013 {
2014 if (s == origval)
2015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002016 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (s[i] == ',')
2018 ++i;
2019 }
2020 else
2021 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002022 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 --s;
2024 ++i;
2025 }
2026 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002027 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 }
2030
2031 if (flags & P_FLAGLIST)
2032 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002033 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002034 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002036 // if options have P_FLAGLIST and
2037 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002038 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002040 if (*s != ',' && *(s + 1) == ','
2041 && vim_strchr(s + 2, *s) != NULL)
2042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // Remove the duplicated value and
2044 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002045 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002046 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002049 else
2050 {
2051 if ((!(flags & P_COMMA) || *s != ',')
2052 && vim_strchr(s + 1, *s) != NULL)
2053 {
2054 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002055 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002056 }
2057 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01002058 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02002059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002062 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 arg = save_arg;
2064 new_value_alloced = TRUE;
2065 }
2066
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002067 /*
2068 * Set the new value.
2069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 *(char_u **)(varp) = newval;
2071
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002072#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02002073 if (!starting
2074# ifdef FEAT_CRYPT
2075 && options[opt_idx].indir != PV_KEY
2076# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002077 && origval != NULL && newval != NULL)
2078 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002079 // origval may be freed by
2080 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02002081 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002082 // newval (and varp) may become invalid if the
2083 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002084 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002085 if (origval_l != NULL)
2086 saved_origval_l = vim_strsave(origval_l);
2087 if (origval_g != NULL)
2088 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002089 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002090#endif
2091
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002092 {
2093 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002094 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002095
2096 // When an option is set in the sandbox, from a
2097 // modeline or in secure mode, then deal with side
2098 // effects in secure mode. Also when the value was
2099 // set with the P_INSECURE flag and is not
2100 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002101 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002102#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002103 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002104#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002105 || (!value_is_replaced && (*p & P_INSECURE)))
2106 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002107
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002108 // Handle side effects, and set the global value
2109 // for ":set" on local options. Note: when setting
2110 // 'syntax' or 'filetype' autocommands may be
2111 // triggered that can cause havoc.
2112 errmsg = did_set_string_option(
2113 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002114 new_value_alloced, oldval, errbuf,
2115 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002116
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002117 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002120#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002121 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002122 trigger_optionsset_string(
2123 opt_idx, opt_flags, saved_origval,
2124 saved_origval_l, saved_origval_g,
2125 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002126 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002127 vim_free(saved_origval_l);
2128 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002129 vim_free(saved_newval);
2130#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002131 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (errmsg != NULL)
2133 goto skip;
2134 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002135 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 char_u *p;
2138
2139 if (nextchar == '&')
2140 {
2141 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002142 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 else
2145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002146 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002147 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (*p == '\\' && p[1] != NUL)
2149 ++p;
2150 nextchar = *p;
2151 *p = NUL;
2152 add_termcode(key_name, arg, FALSE);
2153 *p = nextchar;
2154 }
2155 if (full_screen)
2156 ttest(FALSE);
2157 redraw_all_later(CLEAR);
2158 }
2159 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002160
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002162 did_set_option(
2163 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165
2166skip:
2167 /*
2168 * Advance to next argument.
2169 * - skip until a blank found, taking care of backslashes
2170 * - skip blanks
2171 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2172 */
2173 for (i = 0; i < 2 ; ++i)
2174 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002175 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (*arg++ == '\\' && *arg != NUL)
2177 ++arg;
2178 arg = skipwhite(arg);
2179 if (*arg != '=')
2180 break;
2181 }
2182 }
2183
2184 if (errmsg != NULL)
2185 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002186 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002187 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (i + (arg - startarg) < IOSIZE)
2189 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002190 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 STRCAT(IObuff, ": ");
2192 mch_memmove(IObuff + i, startarg, (arg - startarg));
2193 IObuff[i + (arg - startarg)] = NUL;
2194 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002195 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 trans_characters(IObuff, IOSIZE);
2197
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002198 ++no_wait_return; // wait_return done later
2199 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 --no_wait_return;
2201
2202 return FAIL;
2203 }
2204
2205 arg = skipwhite(arg);
2206 }
2207
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002208theend:
2209 if (silent_mode && did_show)
2210 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002211 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002212 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002213 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002214 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002215 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002216 out_flush();
2217 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 }
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 return OK;
2222}
2223
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002224/*
2225 * Call this when an option has been given a new value through a user command.
2226 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2227 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002228 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002229did_set_option(
2230 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002231 int opt_flags, // possibly with OPT_MODELINE
2232 int new_value, // value was replaced completely
2233 int value_checked) // value was checked to be safe, no need to set the
2234 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002235{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002236 long_u *p;
2237
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002238 options[opt_idx].flags |= P_WAS_SET;
2239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002240 // When an option is set in the sandbox, from a modeline or in secure mode
2241 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2242 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002243 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002244 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002245#ifdef HAVE_SANDBOX
2246 || sandbox != 0
2247#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002248 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002249 *p = *p | P_INSECURE;
2250 else if (new_value)
2251 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002252}
2253
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254/*
2255 * Convert a key name or string into a key value.
2256 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002257 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002259 int
2260string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
2262 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002263 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (*arg == '^')
2265 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002266 if (multi_byte)
2267 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 return *arg;
2269}
2270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271/*
2272 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2273 * maketitle() to create and display it.
2274 * When switching the title or icon off, call mch_restore_title() to get
2275 * the old value back.
2276 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002277 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002278did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 if (starting != NO_SCREEN
2281#ifdef FEAT_GUI
2282 && !gui.starting
2283#endif
2284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288/*
2289 * set_options_bin - called when 'bin' changes value.
2290 */
2291 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002292set_options_bin(
2293 int oldval,
2294 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002295 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
2297 /*
2298 * The option values that are changed when 'bin' changes are
2299 * copied when 'bin is set and restored when 'bin' is reset.
2300 */
2301 if (newval)
2302 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 if (!(opt_flags & OPT_GLOBAL))
2306 {
2307 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2308 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2309 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2310 curbuf->b_p_et_nobin = curbuf->b_p_et;
2311 }
2312 if (!(opt_flags & OPT_LOCAL))
2313 {
2314 p_tw_nobin = p_tw;
2315 p_wm_nobin = p_wm;
2316 p_ml_nobin = p_ml;
2317 p_et_nobin = p_et;
2318 }
2319 }
2320
2321 if (!(opt_flags & OPT_GLOBAL))
2322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002323 curbuf->b_p_tw = 0; // no automatic line wrap
2324 curbuf->b_p_wm = 0; // no automatic line wrap
2325 curbuf->b_p_ml = 0; // no modelines
2326 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
2328 if (!(opt_flags & OPT_LOCAL))
2329 {
2330 p_tw = 0;
2331 p_wm = 0;
2332 p_ml = FALSE;
2333 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002334 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 if (!(opt_flags & OPT_GLOBAL))
2340 {
2341 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2342 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2343 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2344 curbuf->b_p_et = curbuf->b_p_et_nobin;
2345 }
2346 if (!(opt_flags & OPT_LOCAL))
2347 {
2348 p_tw = p_tw_nobin;
2349 p_wm = p_wm_nobin;
2350 p_ml = p_ml_nobin;
2351 p_et = p_et_nobin;
2352 }
2353 }
2354}
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356/*
2357 * Expand environment variables for some string options.
2358 * These string options cannot be indirect!
2359 * If "val" is NULL expand the current value of the option.
2360 * Return pointer to NameBuff, or NULL when not expanded.
2361 */
2362 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002363option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002365 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2367 return NULL;
2368
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002369 // If val is longer than MAXPATHL no meaningful expansion can be done,
2370 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (val != NULL && STRLEN(val) > MAXPATHL)
2372 return NULL;
2373
2374 if (val == NULL)
2375 val = *(char_u **)options[opt_idx].var;
2376
2377 /*
2378 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2379 * Escape spaces when expanding 'tags', they are used to separate file
2380 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002381 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 */
2383 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002384 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002385#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002386 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2387#endif
2388 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002389 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 return NULL;
2391
2392 return NameBuff;
2393}
2394
2395/*
2396 * After setting various option values: recompute variables that depend on
2397 * option values.
2398 */
2399 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002400didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 (void)init_chartab();
2404
Bram Moolenaardac13472019-09-16 21:06:21 +02002405 didset_string_options();
2406
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002407#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002408 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002409 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002410 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002411 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002414 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 (void)check_cedit();
2416#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002417#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002418 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002419 fill_breakat_flags();
2420#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002421 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002422}
2423
2424/*
2425 * More side effects of setting options.
2426 */
2427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002428didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002429{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002430 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002431 (void)highlight_changed();
2432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002433 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002434 check_opt_wim();
2435
Bram Moolenaareed9d462021-02-15 20:38:25 +01002436 // Parse default for 'listchars'.
2437 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002439 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002440 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002441
2442#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002443 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002444 (void)check_clipboard_option();
2445#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002446#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002447 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002448 (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002449 vim_free(curbuf->b_p_vts_array);
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002450 (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
2453
2454/*
2455 * Check for string options that are NULL (normally only termcap options).
2456 */
2457 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002458check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460 int opt_idx;
2461
2462 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2463 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2464 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2465}
2466
2467/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002468 * Return the option index found by a pointer into term_strings[].
2469 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002471 int
2472get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002474 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2477 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002478 return opt_idx;
2479 return -1; // cannot happen: didn't find it!
2480}
2481
2482/*
2483 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2484 * Return the option index or -1 if not found.
2485 */
2486 int
2487set_term_option_alloced(char_u **p)
2488{
2489 int opt_idx = get_term_opt_idx(p);
2490
2491 if (opt_idx >= 0)
2492 options[opt_idx].flags |= P_ALLOCED;
2493 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494}
2495
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002496#if defined(FEAT_EVAL) || defined(PROTO)
2497/*
2498 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2499 * Return FALSE when it wasn't.
2500 * Return -1 for an unknown option.
2501 */
2502 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002503was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002504{
2505 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002506 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002507
2508 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002509 {
2510 flagp = insecure_flag(idx, opt_flags);
2511 return (*flagp & P_INSECURE) != 0;
2512 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002513 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002514 return -1;
2515}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002516
2517/*
2518 * Get a pointer to the flags used for the P_INSECURE flag of option
2519 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002520 * NOTE: Caller must make sure that "curwin" is set to the window from which
2521 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002522 */
2523 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002524insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002525{
2526 if (opt_flags & OPT_LOCAL)
2527 switch ((int)options[opt_idx].indir)
2528 {
2529#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002530 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002531#endif
2532#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002533# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002534 case PV_FDE: return &curwin->w_p_fde_flags;
2535 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002536# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002537# ifdef FEAT_BEVAL
2538 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2539# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002541 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002543 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002544# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002545 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002546# endif
2547#endif
2548 }
2549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002550 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002551 return &options[opt_idx].flags;
2552}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002553#endif
2554
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002555/*
2556 * Redraw the window title and/or tab page text later.
2557 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002558void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002559{
2560 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002561 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002562}
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002565 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2566 * characters or characters in "allowed".
2567 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002568 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002569valid_name(char_u *val, char *allowed)
2570{
2571 char_u *s;
2572
2573 for (s = val; *s != NUL; ++s)
2574 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2575 return FALSE;
2576 return TRUE;
2577}
2578
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002579#if defined(FEAT_EVAL) || defined(PROTO)
2580/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002581 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002582 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002584 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002585set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002586{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002587 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2588 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002589 sctx_T new_script_ctx = script_ctx;
2590
Bram Moolenaar51258742020-05-03 17:19:33 +02002591 // Modeline already has the line number set.
2592 if (!(opt_flags & OPT_MODELINE))
2593 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002595 // Remember where the option was set. For local options need to do that
2596 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002597 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002598 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002599 if (both || (opt_flags & OPT_LOCAL))
2600 {
2601 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002602 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002603 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002604 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002605 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002606}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002607
2608/*
2609 * Set the script_ctx for a termcap option.
2610 * "name" must be the two character code, e.g. "RV".
2611 * When "name" is NULL use "opt_idx".
2612 */
2613 void
2614set_term_option_sctx_idx(char *name, int opt_idx)
2615{
2616 char_u buf[5];
2617 int idx;
2618
2619 if (name == NULL)
2620 idx = opt_idx;
2621 else
2622 {
2623 buf[0] = 't';
2624 buf[1] = '_';
2625 buf[2] = name[0];
2626 buf[3] = name[1];
2627 buf[4] = 0;
2628 idx = findoption(buf);
2629 }
2630 if (idx >= 0)
2631 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2632}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002633#endif
2634
Bram Moolenaarf4140482020-02-15 23:06:45 +01002635#if defined(FEAT_EVAL)
2636/*
2637 * Apply the OptionSet autocommand.
2638 */
2639 static void
2640apply_optionset_autocmd(
2641 int opt_idx,
2642 long opt_flags,
2643 long oldval,
2644 long oldval_g,
2645 long newval,
2646 char *errmsg)
2647{
2648 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2649
2650 // Don't do this while starting up, failure or recursively.
2651 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2652 return;
2653
2654 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2655 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2656 oldval_g);
2657 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2658 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2659 (opt_flags & OPT_LOCAL) ? "local" : "global");
2660 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2661 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2662 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2663 if (opt_flags & OPT_LOCAL)
2664 {
2665 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2666 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2667 }
2668 if (opt_flags & OPT_GLOBAL)
2669 {
2670 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2671 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2672 }
2673 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2674 {
2675 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2676 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2677 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2678 }
2679 if (opt_flags & OPT_MODELINE)
2680 {
2681 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2682 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2683 }
2684 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2685 NULL, FALSE, NULL);
2686 reset_v_option_vars();
2687}
2688#endif
2689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690/*
2691 * Set the value of a boolean option, and take care of side effects.
2692 * Returns NULL for success, or an error message for an error.
2693 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002694 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002695set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 int opt_idx, // index in options[] table
2697 char_u *varp, // pointer to the option variable
2698 int value, // new value
2699 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002702#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002703 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if ((secure
2708#ifdef HAVE_SANDBOX
2709 || sandbox != 0
2710#endif
2711 ) && (options[opt_idx].flags & P_SECURE))
2712 return e_secure;
2713
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002714#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002715 // Save the global value before changing anything. This is needed as for
2716 // a global-only option setting the "local value" in fact sets the global
2717 // value (since there is only one value).
2718 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2719 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2720 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002721#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002726 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727#endif
2728
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002729#ifdef FEAT_GUI
2730 need_mouse_correct = TRUE;
2731#endif
2732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2735 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2736
2737 /*
2738 * Handle side effects of changing a bool option.
2739 */
2740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002741 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar920694c2016-08-21 17:45:02 +02002745#ifdef FEAT_LANGMAP
2746 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002747 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002748 p_lnr = !p_lrm;
2749 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002751 p_lrm = !p_lnr;
2752#endif
2753
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002754#ifdef FEAT_SYN_HL
2755 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2756 reset_cursorline();
2757#endif
2758
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002759#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002761 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2762 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // Only take action when the option was set. When reset we do not
2764 // delete the undo file, the option may be set again without making
2765 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002766 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002767 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002768 char_u hash[UNDO_HASH_SIZE];
2769 buf_T *save_curbuf = curbuf;
2770
Bram Moolenaar29323592016-07-24 22:04:11 +02002771 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 // When 'undofile' is set globally: for every buffer, otherwise
2774 // only for the current buffer: Try to read in the undofile,
2775 // if one exists, the buffer wasn't changed and the buffer was
2776 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002777 if ((curbuf == save_curbuf
2778 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2779 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2780 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +02002781#ifdef FEAT_CRYPT
2782 if (crypt_get_method_nr(curbuf) == CRYPT_M_SOD)
2783 continue;
2784#endif
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002785 u_compute_hash(hash);
2786 u_read_undo(NULL, hash, curbuf->b_fname);
2787 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002788 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002789 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002790 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002791 }
2792#endif
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 else if ((int *)varp == &curbuf->b_p_ro)
2795 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002796 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2798 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002801 if (curbuf->b_p_ro)
2802 curbuf->b_did_warn = FALSE;
2803
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002804 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806
Bram Moolenaar9c449722010-07-20 18:44:27 +02002807#ifdef FEAT_GUI
2808 else if ((int *)varp == &p_mh)
2809 {
2810 if (!p_mh)
2811 gui_mch_mousehide(FALSE);
2812 }
2813#endif
2814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002817 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002818# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002819 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002820 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2821 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002822 {
2823 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002824 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002825 }
2826# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002827 redraw_titles();
2828 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002831 {
2832 redraw_titles();
2833 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002834 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002835 else if ((int *)varp == &curbuf->b_p_fixeol)
2836 {
2837 redraw_titles();
2838 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002840 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002841 {
2842 redraw_titles();
2843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 else if ((int *)varp == &curbuf->b_p_bin)
2847 {
2848 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002849 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2854 {
2855 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2856 NULL, NULL, TRUE, curbuf);
2857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 else if ((int *)varp == &curbuf->b_p_swf)
2861 {
2862 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2866 // buf->b_p_swf
2867 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
2869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 else if ((int *)varp == &p_terse)
2872 {
2873 char_u *p;
2874
2875 p = vim_strchr(p_shm, SHM_SEARCH);
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 if (p_terse && p == NULL)
2879 {
2880 STRCPY(IObuff, p_shm);
2881 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002882 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002886 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 else if ((int *)varp == &p_paste)
2891 {
2892 paste_option_changed();
2893 }
2894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 else if ((int *)varp == &p_im)
2897 {
2898 if (p_im)
2899 {
2900 if ((State & INSERT) == 0)
2901 need_start_insertmode = TRUE;
2902 stop_insert_mode = FALSE;
2903 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002905 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
2907 need_start_insertmode = FALSE;
2908 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002909 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 restart_edit = 0;
2912 }
2913 }
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 else if ((int *)varp == &p_ic && p_hls)
2917 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002918 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920
2921#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 else if ((int *)varp == &p_hls)
2924 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002925 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927#endif
2928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2930 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 else if ((int *)varp == &curwin->w_p_scb)
2932 {
2933 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002936 curwin->w_scbind_pos = curwin->w_topline;
2937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939
Bram Moolenaar4033c552017-09-16 20:54:51 +02002940#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 else if ((int *)varp == &curwin->w_p_pvw)
2943 {
2944 if (curwin->w_p_pvw)
2945 {
2946 win_T *win;
2947
Bram Moolenaar29323592016-07-24 22:04:11 +02002948 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (win->w_p_pvw && win != curwin)
2950 {
2951 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002952 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 }
2955 }
2956#endif
2957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 else if ((int *)varp == &curbuf->b_p_tx)
2960 {
2961 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2962 }
2963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 set_string_option_direct((char_u *)"ffs", -1,
2968 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002969 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 /*
2973 * When 'lisp' option changes include/exclude '-' in
2974 * keyword characters.
2975 */
2976#ifdef FEAT_LISP
2977 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2978 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981#endif
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002984 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002986 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
2989 else if ((int *)varp == &curbuf->b_changed)
2990 {
2991 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002993 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996
2997#ifdef BACKSLASH_IN_FILENAME
2998 else if ((int *)varp == &p_ssl)
2999 {
3000 if (p_ssl)
3001 {
3002 psepc = '/';
3003 psepcN = '\\';
3004 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
3006 else
3007 {
3008 psepc = '\\';
3009 psepcN = '/';
3010 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 buflist_slash_adjust();
3015 alist_slash_adjust();
3016# ifdef FEAT_EVAL
3017 scriptnames_slash_adjust();
3018# endif
3019 }
3020#endif
3021
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003022 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else if ((int *)varp == &curwin->w_p_wrap)
3024 {
3025 if (curwin->w_p_wrap)
3026 curwin->w_leftcol = 0;
3027 }
3028
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 else if ((int *)varp == &p_ea)
3030 {
3031 if (p_ea && !old_value)
3032 win_equal(curwin, FALSE, 0);
3033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
3035 else if ((int *)varp == &p_wiv)
3036 {
3037 /*
3038 * When 'weirdinvert' changed, set/reset 't_xs'.
3039 * Then set 'weirdinvert' according to value of 't_xs'.
3040 */
3041 if (p_wiv && !old_value)
3042 T_XS = (char_u *)"y";
3043 else if (!p_wiv && old_value)
3044 T_XS = empty_option;
3045 p_wiv = (*T_XS != NUL);
3046 }
3047
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003048#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 else if ((int *)varp == &p_beval)
3050 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003051 if (!balloonEvalForTerm)
3052 {
3053 if (p_beval && !old_value)
3054 gui_mch_enable_beval_area(balloonEval);
3055 else if (!p_beval && old_value)
3056 gui_mch_disable_beval_area(balloonEval);
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003059#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003060#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003061 else if ((int *)varp == &p_bevalterm)
3062 {
3063 mch_bevalterm_changed();
3064 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003067#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 else if ((int *)varp == &p_acd)
3069 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02003071 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073#endif
3074
3075#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003076 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 else if ((int *)varp == &curwin->w_p_diff)
3078 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003080 diff_buf_adjust(curwin);
3081# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (foldmethodIsDiff(curwin))
3083 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086#endif
3087
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003088#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 else if ((int *)varp == &p_imdisable)
3091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003092 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (p_imdisable)
3094 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003095 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // When the option is set from an autocommand, it may need to take
3097 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003098 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100#endif
3101
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003102#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003103 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003104 else if ((int *)varp == &curwin->w_p_spell)
3105 {
3106 if (curwin->w_p_spell)
3107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003108 char *errmsg = did_set_spelllang(curwin);
3109
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003110 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003111 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003112 }
3113 }
3114#endif
3115
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#ifdef FEAT_ARABIC
3117 if ((int *)varp == &curwin->w_p_arab)
3118 {
3119 if (curwin->w_p_arab)
3120 {
3121 /*
3122 * 'arabic' is set, handle various sub-settings.
3123 */
3124 if (!p_tbidi)
3125 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003126 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (!curwin->w_p_rl)
3128 {
3129 curwin->w_p_rl = TRUE;
3130 changed_window_setting();
3131 }
3132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (!p_arshape)
3135 {
3136 p_arshape = TRUE;
3137 redraw_later_clear();
3138 }
3139 }
3140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003141 // Arabic requires a utf-8 encoding, inform the user if its not
3142 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003144 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003145 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3146
Bram Moolenaar8820b482017-03-16 17:23:31 +01003147 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003148 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003149#ifdef FEAT_EVAL
3150 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3151#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003154 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003158 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3160 OPT_LOCAL);
3161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163 else
3164 {
3165 /*
3166 * 'arabic' is reset, handle various sub-settings.
3167 */
3168 if (!p_tbidi)
3169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003170 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (curwin->w_p_rl)
3172 {
3173 curwin->w_p_rl = FALSE;
3174 changed_window_setting();
3175 }
3176
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 // 'arabicshape' isn't reset, it is a global option and
3178 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // 'delcombine' isn't reset, it is a global option and another
3182 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
3184# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 curbuf->b_p_iminsert = B_IMODE_NONE;
3187 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3188# endif
3189 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003190 }
3191
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003192#endif
3193
Bram Moolenaar44826212019-08-22 21:23:20 +02003194#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3195 else if (((int *)varp == &curwin->w_p_nu
3196 || (int *)varp == &curwin->w_p_rnu)
3197 && gui.in_use
3198 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3199 && curbuf->b_signlist != NULL)
3200 {
3201 // If the 'number' or 'relativenumber' options are modified and
3202 // 'signcolumn' is set to 'number', then clear the screen for a full
3203 // refresh. Otherwise the sign icons are not displayed properly in the
3204 // number column. If the 'number' option is set and only the
3205 // 'relativenumber' option is toggled, then don't refresh the screen
3206 // (optimization).
3207 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3208 redraw_all_later(CLEAR);
3209 }
3210#endif
3211
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003212#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003214 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003215 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003216# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003217 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003218 if (
3219# ifdef VIMDLL
3220 !gui.in_use && !gui.starting &&
3221# endif
3222 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003223 {
3224 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003225 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003226 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003227 if (is_term_win32())
3228 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003229# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003230# ifdef FEAT_GUI
3231 if (!gui.in_use && !gui.starting)
3232# endif
3233 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003234# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003235 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003236 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003237 {
3238 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003239 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003240 init_highlight(TRUE, FALSE);
3241 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003242# endif
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003243# ifdef FEAT_TERMINAL
3244 term_update_colors_all();
3245 term_update_wincolor_all();
3246# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003247 }
3248#endif
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 /*
3251 * End of handling side effects for bool options.
3252 */
3253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003254 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 options[opt_idx].flags |= P_WAS_SET;
3257
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003258#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003259 apply_optionset_autocmd(opt_idx, opt_flags,
3260 (long)(old_value ? TRUE : FALSE),
3261 (long)(old_global_value ? TRUE : FALSE),
3262 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003263#endif
3264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003265 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003266 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003267 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003268 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003269
3270 if ((opt_flags & OPT_NO_REDRAW) == 0)
3271 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
3273 return NULL;
3274}
3275
3276/*
3277 * Set the value of a number option, and take care of side effects.
3278 * Returns NULL for success, or an error message for an error.
3279 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003280 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003281set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 int opt_idx, // index in options[] table
3283 char_u *varp, // pointer to the option variable
3284 long value, // new value
3285 char *errbuf, // buffer for error messages
3286 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003287 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3288 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003290 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003292#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003293 long old_global_value = 0; // only used when setting a local and
3294 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003295#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003296 long old_Rows = Rows; // remember old Rows
3297 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 long *pp = (long *)varp;
3299
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003300 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003301 if ((secure
3302#ifdef HAVE_SANDBOX
3303 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003305 ) && (options[opt_idx].flags & P_SECURE))
3306 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003308#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003309 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003310 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003311 // value (since there is only one value).
3312 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003313 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3314 OPT_GLOBAL);
3315#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003316
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 *pp = value;
3318#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003319 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003320 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003322#ifdef FEAT_GUI
3323 need_mouse_correct = TRUE;
3324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar14f24742012-08-08 18:01:05 +02003326 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003329#ifdef FEAT_VARTABS
3330 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3331 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3332 ? tabstop_first(curbuf->b_p_vts_array)
3333 : curbuf->b_p_ts;
3334#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338
3339 /*
3340 * Number options that need some action when changed
3341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (pp == &p_wh || pp == &p_hh)
3343 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003344 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (p_wh < 1)
3346 {
3347 errmsg = e_positive;
3348 p_wh = 1;
3349 }
3350 if (p_wmh > p_wh)
3351 {
3352 errmsg = e_winheight;
3353 p_wh = p_wmh;
3354 }
3355 if (p_hh < 0)
3356 {
3357 errmsg = e_positive;
3358 p_hh = 0;
3359 }
3360
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003361 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003362 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
3364 if (pp == &p_wh && curwin->w_height < p_wh)
3365 win_setheight((int)p_wh);
3366 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3367 win_setheight((int)p_hh);
3368 }
3369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else if (pp == &p_wmh)
3371 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003372 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (p_wmh < 0)
3374 {
3375 errmsg = e_positive;
3376 p_wmh = 0;
3377 }
3378 if (p_wmh > p_wh)
3379 {
3380 errmsg = e_winheight;
3381 p_wmh = p_wh;
3382 }
3383 win_setminheight();
3384 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003385 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003387 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (p_wiw < 1)
3389 {
3390 errmsg = e_positive;
3391 p_wiw = 1;
3392 }
3393 if (p_wmw > p_wiw)
3394 {
3395 errmsg = e_winwidth;
3396 p_wiw = p_wmw;
3397 }
3398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003399 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003400 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 win_setwidth((int)p_wiw);
3402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 else if (pp == &p_wmw)
3404 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003405 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (p_wmw < 0)
3407 {
3408 errmsg = e_positive;
3409 p_wmw = 0;
3410 }
3411 if (p_wmw > p_wiw)
3412 {
3413 errmsg = e_winwidth;
3414 p_wmw = p_wiw;
3415 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003416 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 else if (pp == &p_ls)
3421 {
3422 last_status(FALSE);
3423 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003425 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003426 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431#ifdef FEAT_GUI
3432 else if (pp == &p_linespace)
3433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003434 // Recompute gui.char_height and resize the Vim window to keep the
3435 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003436 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003437 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439#endif
3440
3441#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003442 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 else if (pp == &curwin->w_p_fdl)
3444 {
3445 if (curwin->w_p_fdl < 0)
3446 curwin->w_p_fdl = 0;
3447 newFoldLevel();
3448 }
3449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else if (pp == &curwin->w_p_fml)
3452 {
3453 foldUpdateAll(curwin);
3454 }
3455
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 else if (pp == &curwin->w_p_fdn)
3458 {
3459 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3460 foldUpdateAll(curwin);
3461 }
3462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 else if (pp == &curwin->w_p_fdc)
3465 {
3466 if (curwin->w_p_fdc < 0)
3467 {
3468 errmsg = e_positive;
3469 curwin->w_p_fdc = 0;
3470 }
3471 else if (curwin->w_p_fdc > 12)
3472 {
3473 errmsg = e_invarg;
3474 curwin->w_p_fdc = 12;
3475 }
3476 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003477#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003479#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003480 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3482 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003483# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (foldmethodIsIndent(curwin))
3485 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003486# endif
3487# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003488 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3489 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003490 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3491 parse_cino(curbuf);
3492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003496 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003497 else if (pp == &p_mco)
3498 {
3499 if (p_mco > MAX_MCO)
3500 p_mco = MAX_MCO;
3501 else if (p_mco < 0)
3502 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003503 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003504 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 else if (pp == &curbuf->b_p_iminsert)
3507 {
3508 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3509 {
3510 errmsg = e_invarg;
3511 curbuf->b_p_iminsert = B_IMODE_NONE;
3512 }
3513 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003516#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 status_redraw_curbuf();
3519#endif
3520 }
3521
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003522#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003523 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003524 else if (pp == &p_imst)
3525 {
3526 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3527 errmsg = e_invarg;
3528 }
3529#endif
3530
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003531 else if (pp == &p_window)
3532 {
3533 if (p_window < 1)
3534 p_window = 1;
3535 else if (p_window >= Rows)
3536 p_window = Rows - 1;
3537 }
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 else if (pp == &curbuf->b_p_imsearch)
3540 {
3541 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3542 {
3543 errmsg = e_invarg;
3544 curbuf->b_p_imsearch = B_IMODE_NONE;
3545 }
3546 p_imsearch = curbuf->b_p_imsearch;
3547 }
3548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003549 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 else if (pp == &p_titlelen)
3551 {
3552 if (p_titlelen < 0)
3553 {
3554 errmsg = e_positive;
3555 p_titlelen = 85;
3556 }
3557 if (starting != NO_SCREEN && old_value != p_titlelen)
3558 need_maketitle = TRUE;
3559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003561 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else if (pp == &p_ch)
3563 {
3564 if (p_ch < 1)
3565 {
3566 errmsg = e_positive;
3567 p_ch = 1;
3568 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003569 if (p_ch > Rows - min_rows() + 1)
3570 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003572 // Only compute the new window layout when startup has been
3573 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (p_ch != old_value && full_screen
3575#ifdef FEAT_GUI
3576 && !gui.starting
3577#endif
3578 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003579 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003582 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 else if (pp == &p_uc)
3584 {
3585 if (p_uc < 0)
3586 {
3587 errmsg = e_positive;
3588 p_uc = 100;
3589 }
3590 if (p_uc && !old_value)
3591 ml_open_files();
3592 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003593#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003594 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003595 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003596 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003597 {
3598 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003599 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003600 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003601 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003602 {
3603 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003604 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003605 }
3606 }
3607#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003608#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003609 else if (pp == &p_mzq)
3610 mzvim_reset_timer();
3611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003613#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003614 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003615 else if (pp == &p_pyx)
3616 {
3617 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3618 errmsg = e_invarg;
3619 }
3620#endif
3621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003622 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 else if (pp == &p_ul)
3624 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003625 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003627 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 p_ul = value;
3629 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003630 else if (pp == &curbuf->b_p_ul)
3631 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003632 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003633 curbuf->b_p_ul = old_value;
3634 u_sync(TRUE);
3635 curbuf->b_p_ul = value;
3636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003638#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003639 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003640 else if (pp == &curwin->w_p_nuw)
3641 {
3642 if (curwin->w_p_nuw < 1)
3643 {
3644 errmsg = e_positive;
3645 curwin->w_p_nuw = 1;
3646 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003647 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003648 {
3649 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003650 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003651 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003652 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003653 }
3654#endif
3655
Bram Moolenaar1a384422010-07-14 19:53:30 +02003656 else if (pp == &curbuf->b_p_tw)
3657 {
3658 if (curbuf->b_p_tw < 0)
3659 {
3660 errmsg = e_positive;
3661 curbuf->b_p_tw = 0;
3662 }
3663#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003664 {
3665 win_T *wp;
3666 tabpage_T *tp;
3667
3668 FOR_ALL_TAB_WINDOWS(tp, wp)
3669 check_colorcolumn(wp);
3670 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003671#endif
3672 }
3673
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 /*
3675 * Check the bounds for numeric options here
3676 */
3677 if (Rows < min_rows() && full_screen)
3678 {
3679 if (errbuf != NULL)
3680 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003681 vim_snprintf((char *)errbuf, errbuflen,
3682 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 errmsg = errbuf;
3684 }
3685 Rows = min_rows();
3686 }
3687 if (Columns < MIN_COLUMNS && full_screen)
3688 {
3689 if (errbuf != NULL)
3690 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003691 vim_snprintf((char *)errbuf, errbuflen,
3692 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 errmsg = errbuf;
3694 }
3695 Columns = MIN_COLUMNS;
3696 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003697 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 /*
3700 * If the screen (shell) height has been changed, assume it is the
3701 * physical screenheight.
3702 */
3703 if (old_Rows != Rows || old_Columns != Columns)
3704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003705 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (updating_screen)
3707 *pp = old_value;
3708 else if (full_screen
3709#ifdef FEAT_GUI
3710 && !gui.starting
3711#endif
3712 )
3713 set_shellsize((int)Columns, (int)Rows, TRUE);
3714 else
3715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003716 // Postpone the resizing; check the size and cmdline position for
3717 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 check_shellsize();
3719 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3720 cmdline_row = Rows - p_ch;
3721 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003722 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003723 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (curbuf->b_p_ts <= 0)
3727 {
3728 errmsg = e_positive;
3729 curbuf->b_p_ts = 8;
3730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (p_tm < 0)
3732 {
3733 errmsg = e_positive;
3734 p_tm = 0;
3735 }
3736 if ((curwin->w_p_scr <= 0
3737 || (curwin->w_p_scr > curwin->w_height
3738 && curwin->w_height > 0))
3739 && full_screen)
3740 {
3741 if (pp == &(curwin->w_p_scr))
3742 {
3743 if (curwin->w_p_scr != 0)
Bram Moolenaard8e44472021-07-21 22:20:33 +02003744 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 win_comp_scroll(curwin);
3746 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 // If 'scroll' became invalid because of a side effect silently adjust
3748 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 else if (curwin->w_p_scr <= 0)
3750 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003751 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 curwin->w_p_scr = curwin->w_height;
3753 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003754 if (p_hi < 0)
3755 {
3756 errmsg = e_positive;
3757 p_hi = 0;
3758 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003759 else if (p_hi > 10000)
3760 {
3761 errmsg = e_invarg;
3762 p_hi = 10000;
3763 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764 if (p_re < 0 || p_re > 2)
3765 {
3766 errmsg = e_invarg;
3767 p_re = 0;
3768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (p_report < 0)
3770 {
3771 errmsg = e_positive;
3772 p_report = 1;
3773 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003774 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003776 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 p_sj = Rows / 2;
3778 else
3779 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02003780 errmsg = e_invalid_scroll_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 p_sj = 1;
3782 }
3783 }
3784 if (p_so < 0 && full_screen)
3785 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003786 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 p_so = 0;
3788 }
3789 if (p_siso < 0 && full_screen)
3790 {
3791 errmsg = e_positive;
3792 p_siso = 0;
3793 }
3794#ifdef FEAT_CMDWIN
3795 if (p_cwh < 1)
3796 {
3797 errmsg = e_positive;
3798 p_cwh = 1;
3799 }
3800#endif
3801 if (p_ut < 0)
3802 {
3803 errmsg = e_positive;
3804 p_ut = 2000;
3805 }
3806 if (p_ss < 0)
3807 {
3808 errmsg = e_positive;
3809 p_ss = 0;
3810 }
3811
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003812 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3814 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3815
3816 options[opt_idx].flags |= P_WAS_SET;
3817
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003818#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003819 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3820 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003821#endif
3822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003823 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003824 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003825 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003826 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003827 if ((opt_flags & OPT_NO_REDRAW) == 0)
3828 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 return errmsg;
3831}
3832
3833/*
3834 * Called after an option changed: check if something needs to be redrawn.
3835 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003837check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003840 int doclear = (flags & P_RCLR) == P_RCLR;
3841 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003843 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
3846 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3847 changed_window_setting();
3848 if (flags & P_RBUF)
3849 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003850 if (flags & P_RWINONLY)
3851 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003852 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 redraw_all_later(CLEAR);
3854 else if (all)
3855 redraw_all_later(NOT_VALID);
3856}
3857
3858/*
3859 * Find index for option 'arg'.
3860 * Return -1 if not found.
3861 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003862 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003863findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 int opt_idx;
3866 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003867 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 int is_term_opt;
3869
3870 /*
3871 * For first call: Initialize the quick-access table.
3872 * It contains the index for the first option that starts with a certain
3873 * letter. There are 26 letters, plus the first "t_" option.
3874 */
3875 if (quick_tab[1] == 0)
3876 {
3877 p = options[0].fullname;
3878 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3879 {
3880 if (s[0] != p[0])
3881 {
3882 if (s[0] == 't' && s[1] == '_')
3883 quick_tab[26] = opt_idx;
3884 else
3885 quick_tab[CharOrdLow(s[0])] = opt_idx;
3886 }
3887 p = s;
3888 }
3889 }
3890
3891 /*
3892 * Check for name starting with an illegal character.
3893 */
3894#ifdef EBCDIC
3895 if (!islower(arg[0]))
3896#else
3897 if (arg[0] < 'a' || arg[0] > 'z')
3898#endif
3899 return -1;
3900
3901 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3902 if (is_term_opt)
3903 opt_idx = quick_tab[26];
3904 else
3905 opt_idx = quick_tab[CharOrdLow(arg[0])];
3906 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003908 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 break;
3910 }
3911 if (s == NULL && !is_term_opt)
3912 {
3913 opt_idx = quick_tab[CharOrdLow(arg[0])];
3914 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3915 {
3916 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003917 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 break;
3919 s = NULL;
3920 }
3921 }
3922 if (s == NULL)
3923 opt_idx = -1;
3924 return opt_idx;
3925}
3926
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003927#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928/*
3929 * Get the value for an option.
3930 *
3931 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003932 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003933 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003934 * String option: gov_string, *stringval gets allocated string.
3935 * Hidden Number option: gov_hidden_number.
3936 * Hidden Toggle option: gov_hidden_bool.
3937 * Hidden String option: gov_hidden_string.
3938 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003940 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003941get_option_value(
3942 char_u *name,
3943 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003944 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003945 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
3947 int opt_idx;
3948 char_u *varp;
3949
3950 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003951 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003952 {
3953 int key;
3954
3955 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003956 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003957 {
3958 char_u key_name[2];
3959 char_u *p;
3960
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003961 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003962 if (key < 0)
3963 {
3964 key_name[0] = KEY2TERMCAP0(key);
3965 key_name[1] = KEY2TERMCAP1(key);
3966 }
3967 else
3968 {
3969 key_name[0] = KS_KEY;
3970 key_name[1] = (key & 0xff);
3971 }
3972 p = find_termcode(key_name);
3973 if (p != NULL)
3974 {
3975 if (stringval != NULL)
3976 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003977 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003978 }
3979 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003980 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982
3983 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3984
3985 if (options[opt_idx].flags & P_STRING)
3986 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003987 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003988 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (stringval != NULL)
3990 {
3991#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003992 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003993 if ((char_u **)varp == &curbuf->b_p_key
3994 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 *stringval = vim_strsave((char_u *)"*****");
3996 else
3997#endif
3998 *stringval = vim_strsave(*(char_u **)(varp));
3999 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004000 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
4002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004004 return (options[opt_idx].flags & P_NUM)
4005 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (options[opt_idx].flags & P_NUM)
4007 *numval = *(long *)varp;
4008 else
4009 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004010 // Special case: 'modified' is b_changed, but we also want to consider
4011 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if ((int *)varp == &curbuf->b_changed)
4013 *numval = curbufIsChanged();
4014 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02004015 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01004017 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018}
4019#endif
4020
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004021#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004022/*
4023 * Returns the option attributes and its value. Unlike the above function it
4024 * will return either global value or local value of the option depending on
4025 * what was requested, but it will never return global value if it was
4026 * requested to return local one and vice versa. Neither it will return
4027 * buffer-local value if it was requested to return window-local one.
4028 *
4029 * Pretends that option is absent if it is not present in the requested scope
4030 * (i.e. has no global, window-local or buffer-local value depending on
4031 * opt_type). Uses
4032 *
4033 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02004034 * 0 hidden or unknown option, also option that does not have requested
4035 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004036 * see SOPT_* in vim.h for other flags
4037 *
4038 * Possible opt_type values: see SREQ_* in vim.h
4039 */
4040 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004041get_option_value_strict(
4042 char_u *name,
4043 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004044 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01004045 int opt_type,
4046 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004047{
4048 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02004049 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004050 struct vimoption *p;
4051 int r = 0;
4052
4053 opt_idx = findoption(name);
4054 if (opt_idx < 0)
4055 return 0;
4056
4057 p = &(options[opt_idx]);
4058
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004059 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004060 if (p->var == NULL)
4061 return 0;
4062
4063 if (p->flags & P_BOOL)
4064 r |= SOPT_BOOL;
4065 else if (p->flags & P_NUM)
4066 r |= SOPT_NUM;
4067 else if (p->flags & P_STRING)
4068 r |= SOPT_STRING;
4069
4070 if (p->indir == PV_NONE)
4071 {
4072 if (opt_type == SREQ_GLOBAL)
4073 r |= SOPT_GLOBAL;
4074 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004075 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004076 }
4077 else
4078 {
4079 if (p->indir & PV_BOTH)
4080 r |= SOPT_GLOBAL;
4081 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004082 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004083
4084 if (p->indir & PV_WIN)
4085 {
4086 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004087 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004088 else
4089 r |= SOPT_WIN;
4090 }
4091 else if (p->indir & PV_BUF)
4092 {
4093 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004094 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004095 else
4096 r |= SOPT_BUF;
4097 }
4098 }
4099
4100 if (stringval == NULL)
4101 return r;
4102
4103 if (opt_type == SREQ_GLOBAL)
4104 varp = p->var;
4105 else
4106 {
4107 if (opt_type == SREQ_BUF)
4108 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 // Special case: 'modified' is b_changed, but we also want to
4110 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004111 if (p->indir == PV_MOD)
4112 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004113 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004114 varp = NULL;
4115 }
4116#ifdef FEAT_CRYPT
4117 else if (p->indir == PV_KEY)
4118 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004119 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004120 *stringval = NULL;
4121 varp = NULL;
4122 }
4123#endif
4124 else
4125 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004126 buf_T *save_curbuf = curbuf;
4127
4128 // only getting a pointer, no need to use aucmd_prepbuf()
4129 curbuf = (buf_T *)from;
4130 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004131 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004132 curbuf = save_curbuf;
4133 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004134 }
4135 }
4136 else if (opt_type == SREQ_WIN)
4137 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004138 win_T *save_curwin = curwin;
4139
4140 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004141 curbuf = curwin->w_buffer;
4142 varp = get_varp(p);
4143 curwin = save_curwin;
4144 curbuf = curwin->w_buffer;
4145 }
4146 if (varp == p->var)
4147 return (r | SOPT_UNSET);
4148 }
4149
4150 if (varp != NULL)
4151 {
4152 if (p->flags & P_STRING)
4153 *stringval = vim_strsave(*(char_u **)(varp));
4154 else if (p->flags & P_NUM)
4155 *numval = *(long *) varp;
4156 else
4157 *numval = *(int *)varp;
4158 }
4159
4160 return r;
4161}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004162
4163/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004164 * Iterate over options. First argument is a pointer to a pointer to a
4165 * structure inside options[] array, second is option type like in the above
4166 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004167 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004168 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004169 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004170 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004171 * first argument to NULL.
4172 *
4173 * Returns full option name for current option on each call.
4174 */
4175 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004176option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004177{
4178 struct vimoption *ret = NULL;
4179 do
4180 {
4181 if (*option == NULL)
4182 *option = (void *) options;
4183 else if (((struct vimoption *) (*option))->fullname == NULL)
4184 {
4185 *option = NULL;
4186 return NULL;
4187 }
4188 else
4189 *option = (void *) (((struct vimoption *) (*option)) + 1);
4190
4191 ret = ((struct vimoption *) (*option));
4192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004193 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004194 if (ret->var == NULL)
4195 {
4196 ret = NULL;
4197 continue;
4198 }
4199
4200 switch (opt_type)
4201 {
4202 case SREQ_GLOBAL:
4203 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4204 ret = NULL;
4205 break;
4206 case SREQ_BUF:
4207 if (!(ret->indir & PV_BUF))
4208 ret = NULL;
4209 break;
4210 case SREQ_WIN:
4211 if (!(ret->indir & PV_WIN))
4212 ret = NULL;
4213 break;
4214 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004215 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004216 return NULL;
4217 }
4218 }
4219 while (ret == NULL);
4220
4221 return (char_u *)ret->fullname;
4222}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004223#endif
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004226 * Return the flags for the option at 'opt_idx'.
4227 */
4228 long_u
4229get_option_flags(int opt_idx)
4230{
4231 return options[opt_idx].flags;
4232}
4233
4234/*
4235 * Set a flag for the option at 'opt_idx'.
4236 */
4237 void
4238set_option_flag(int opt_idx, long_u flag)
4239{
4240 options[opt_idx].flags |= flag;
4241}
4242
4243/*
4244 * Clear a flag for the option at 'opt_idx'.
4245 */
4246 void
4247clear_option_flag(int opt_idx, long_u flag)
4248{
4249 options[opt_idx].flags &= ~flag;
4250}
4251
4252/*
4253 * Returns TRUE if the option at 'opt_idx' is a global option
4254 */
4255 int
4256is_global_option(int opt_idx)
4257{
4258 return options[opt_idx].indir == PV_NONE;
4259}
4260
4261/*
4262 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4263 * local value.
4264 */
4265 int
4266is_global_local_option(int opt_idx)
4267{
4268 return options[opt_idx].indir & PV_BOTH;
4269}
4270
4271/*
4272 * Returns TRUE if the option at 'opt_idx' is a window-local option
4273 */
4274 int
4275is_window_local_option(int opt_idx)
4276{
4277 return options[opt_idx].var == VAR_WIN;
4278}
4279
4280/*
4281 * Returns TRUE if the option at 'opt_idx' is a hidden option
4282 */
4283 int
4284is_hidden_option(int opt_idx)
4285{
4286 return options[opt_idx].var == NULL;
4287}
4288
4289#if defined(FEAT_CRYPT) || defined(PROTO)
4290/*
4291 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4292 */
4293 int
4294is_crypt_key_option(int opt_idx)
4295{
4296 return options[opt_idx].indir == PV_KEY;
4297}
4298#endif
4299
4300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 * Set the value of option "name".
4302 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004303 *
4304 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004306 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004307set_option_value(
4308 char_u *name,
4309 long number,
4310 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004311 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312{
4313 int opt_idx;
4314 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004315 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004318 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004319 {
4320 int key;
4321
4322 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004323 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004324 {
4325 char_u key_name[2];
4326
4327 if (key < 0)
4328 {
4329 key_name[0] = KEY2TERMCAP0(key);
4330 key_name[1] = KEY2TERMCAP1(key);
4331 }
4332 else
4333 {
4334 key_name[0] = KS_KEY;
4335 key_name[1] = (key & 0xff);
4336 }
4337 add_termcode(key_name, string, FALSE);
4338 if (full_screen)
4339 ttest(FALSE);
4340 redraw_all_later(CLEAR);
4341 return NULL;
4342 }
4343
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004344 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 else
4347 {
4348 flags = options[opt_idx].flags;
4349#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004350 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004352 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02004353 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004354 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004357 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004358 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 else
4360 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004361 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004362 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004364 if (number == 0 && string != NULL)
4365 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004366 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004368 // Either we are given a string or we are setting option
4369 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004370 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004371 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004372 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004373 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004374 // There's another character after zeros or the string
4375 // is empty. In both cases, we are trying to set a
4376 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004377 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004378 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004379 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004380
4381 }
4382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004384 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004385 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004387 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004388 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 }
4391 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004392 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393}
4394
4395/*
4396 * Get the terminal code for a terminal option.
4397 * Returns NULL when not found.
4398 */
4399 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004400get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
4402 int opt_idx;
4403 char_u *varp;
4404
4405 if (tname[0] != 't' || tname[1] != '_' ||
4406 tname[2] == NUL || tname[3] == NUL)
4407 return NULL;
4408 if ((opt_idx = findoption(tname)) >= 0)
4409 {
4410 varp = get_varp(&(options[opt_idx]));
4411 if (varp != NULL)
4412 varp = *(char_u **)(varp);
4413 return varp;
4414 }
4415 return find_termcode(tname + 2);
4416}
4417
4418 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004419get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420{
4421 int i;
4422
4423 i = findoption((char_u *)"hl");
4424 if (i >= 0)
4425 return options[i].def_val[VI_DEFAULT];
4426 return (char_u *)NULL;
4427}
4428
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004429 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004430get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004431{
4432 int i;
4433
4434 i = findoption((char_u *)"enc");
4435 if (i >= 0)
4436 return options[i].def_val[VI_DEFAULT];
4437 return (char_u *)NULL;
4438}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440/*
4441 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004442 * When "has_lt" is true there is a '<' before "*arg_arg".
4443 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 */
4445 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004446find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004448 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004450 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452 /*
4453 * Don't use get_special_key_code() for t_xx, we don't want it to call
4454 * add_termcap_entry().
4455 */
4456 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4457 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004458 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004460 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004462 key = find_special_key(&arg, &modifiers,
4463 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004464 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 key = 0;
4466 }
4467 return key;
4468}
4469
4470/*
4471 * if 'all' == 0: show changed options
4472 * if 'all' == 1: show all normal options
4473 * if 'all' == 2: show all terminal options
4474 */
4475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004476showoptions(
4477 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004478 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479{
4480 struct vimoption *p;
4481 int col;
4482 int isterm;
4483 char_u *varp;
4484 struct vimoption **items;
4485 int item_count;
4486 int run;
4487 int row, rows;
4488 int cols;
4489 int i;
4490 int len;
4491
4492#define INC 20
4493#define GAP 3
4494
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004495 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (items == NULL)
4497 return;
4498
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004499 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004501 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004503 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004505 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004507 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
4509 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004510 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 * 1. display the short items
4512 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004513 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 */
4515 for (run = 1; run <= 2 && !got_int; ++run)
4516 {
4517 /*
4518 * collect the items in items[]
4519 */
4520 item_count = 0;
4521 for (p = &options[0]; p->fullname != NULL; p++)
4522 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004523 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004524 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004525 continue;
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 varp = NULL;
4528 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004529 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 if (p->indir != PV_NONE && !isterm)
4532 varp = get_varp_scope(p, opt_flags);
4533 }
4534 else
4535 varp = get_varp(p);
4536 if (varp != NULL
4537 && ((all == 2 && isterm)
4538 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004539 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004541 if (opt_flags & OPT_ONECOLUMN)
4542 len = Columns;
4543 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004544 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 else
4546 {
4547 option_value2string(p, opt_flags);
4548 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4549 }
4550 if ((len <= INC - GAP && run == 1) ||
4551 (len > INC - GAP && run == 2))
4552 items[item_count++] = p;
4553 }
4554 }
4555
4556 /*
4557 * display the items
4558 */
4559 if (run == 1)
4560 {
4561 cols = (Columns + GAP - 3) / INC;
4562 if (cols == 0)
4563 cols = 1;
4564 rows = (item_count + cols - 1) / cols;
4565 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 rows = item_count;
4568 for (row = 0; row < rows && !got_int; ++row)
4569 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004570 msg_putchar('\n'); // go to next line
4571 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 break;
4573 col = 0;
4574 for (i = row; i < item_count; i += rows)
4575 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004576 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 showoneopt(items[i], opt_flags);
4578 col += INC;
4579 }
4580 out_flush();
4581 ui_breakcheck();
4582 }
4583 }
4584 vim_free(items);
4585}
4586
4587/*
4588 * Return TRUE if option "p" has its default value.
4589 */
4590 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004591optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
4593 int dvi;
4594
4595 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004596 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004597 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004599 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004601 // the cast to long is required for Manx C, long_i is
4602 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004603 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004604 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4606}
4607
4608/*
4609 * showoneopt: show the value of one option
4610 * must not be called with a hidden option!
4611 */
4612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004613showoneopt(
4614 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004617 char_u *varp;
4618 int save_silent = silent_mode;
4619
4620 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004621 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 varp = get_varp_scope(p, opt_flags);
4624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004625 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4627 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004628 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004630 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004632 msg_puts(" ");
4633 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (!(p->flags & P_BOOL))
4635 {
4636 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 option_value2string(p, opt_flags);
4639 msg_outtrans(NameBuff);
4640 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004641
4642 silent_mode = save_silent;
4643 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644}
4645
4646/*
4647 * Write modified options as ":set" commands to a file.
4648 *
4649 * There are three values for "opt_flags":
4650 * OPT_GLOBAL: Write global option values and fresh values of
4651 * buffer-local options (used for start of a session
4652 * file).
4653 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4654 * curwin (used for a vimrc file).
4655 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4656 * and local values for window-local options of
4657 * curwin. Local values are also written when at the
4658 * default value, because a modeline or autocommand
4659 * may have set them when doing ":edit file" and the
4660 * user has set them back at the default or fresh
4661 * value.
4662 * When "local_only" is TRUE, don't write fresh
4663 * values, only local values (for ":mkview").
4664 * (fresh value = value used for a new buffer or window for a local option).
4665 *
4666 * Return FAIL on error, OK otherwise.
4667 */
4668 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004669makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004672 char_u *varp; // currently used value
4673 char_u *varp_fresh; // local value
4674 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 char *cmd;
4676 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004677 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /*
4680 * The options that don't have a default (terminal name, columns, lines)
4681 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004682 * Do the loop over "options[]" twice: once for options with the
4683 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004685 for (pri = 1; pri >= 0; --pri)
4686 {
4687 for (p = &options[0]; !istermoption(p); p++)
4688 if (!(p->flags & P_NO_MKRC)
4689 && !istermoption(p)
4690 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004692 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4694 continue;
4695
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004696 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4697 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4699 continue;
4700
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004701 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004703 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 continue;
4705
Bram Moolenaard23b7142021-04-17 21:04:34 +02004706 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4707 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004708 continue;
4709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 round = 2;
4711 if (p->indir != PV_NONE)
4712 {
4713 if (p->var == VAR_WIN)
4714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004715 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (!(opt_flags & OPT_LOCAL))
4717 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004718 // When fresh value of window-local option is not at the
4719 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4721 {
4722 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004723 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 round = 1;
4726 varp_local = varp;
4727 varp = varp_fresh;
4728 }
4729 }
4730 }
4731 }
4732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004733 // Round 1: fresh value for window-local options.
4734 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 for ( ; round <= 2; varp = varp_local, ++round)
4736 {
4737 if (round == 1 || (opt_flags & OPT_GLOBAL))
4738 cmd = "set";
4739 else
4740 cmd = "setlocal";
4741
4742 if (p->flags & P_BOOL)
4743 {
4744 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4745 return FAIL;
4746 }
4747 else if (p->flags & P_NUM)
4748 {
4749 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4750 return FAIL;
4751 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004752 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004754 int do_endif = FALSE;
4755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004756 // Don't set 'syntax' and 'filetype' again if the value is
4757 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004758 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004759#if defined(FEAT_SYN_HL)
4760 p->indir == PV_SYN ||
4761#endif
4762 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4765 *(char_u **)(varp)) < 0
4766 || put_eol(fd) < 0)
4767 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004768 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004771 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004773 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 if (put_line(fd, "endif") == FAIL)
4776 return FAIL;
4777 }
4778 }
4779 }
4780 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 return OK;
4783}
4784
4785#if defined(FEAT_FOLDING) || defined(PROTO)
4786/*
4787 * Generate set commands for the local fold options only. Used when
4788 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4789 */
4790 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004791makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004793 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004795 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 == FAIL
4797# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004798 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004800 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 == FAIL
4802 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4803 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4804 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4805 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4806 )
4807 return FAIL;
4808
4809 return OK;
4810}
4811#endif
4812
4813 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004814put_setstring(
4815 FILE *fd,
4816 char *cmd,
4817 char *name,
4818 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004819 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
4821 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004822 char_u *buf = NULL;
4823 char_u *part = NULL;
4824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825
4826 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4827 return FAIL;
4828 if (*valuep != NULL)
4829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 // Output 'pastetoggle' as key names. For other
4831 // options some characters have to be escaped with
4832 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (valuep == &p_pt)
4834 {
4835 s = *valuep;
4836 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004837 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 return FAIL;
4839 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004840 // expand the option value, replace $HOME by ~
4841 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004843 int size = (int)STRLEN(*valuep) + 1;
4844
4845 // replace home directory in the whole option value into "buf"
4846 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004847 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004848 goto fail;
4849 home_replace(NULL, *valuep, buf, size, FALSE);
4850
4851 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004852 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004853 // can be expanded when read back.
4854 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4855 && vim_strchr(*valuep, ',') != NULL)
4856 {
4857 part = alloc(size);
4858 if (part == NULL)
4859 goto fail;
4860
4861 // write line break to clear the option, e.g. ':set rtp='
4862 if (put_eol(fd) == FAIL)
4863 goto fail;
4864
4865 p = buf;
4866 while (*p != NUL)
4867 {
4868 // for each comma separated option part, append value to
4869 // the option, :set rtp+=value
4870 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4871 goto fail;
4872 (void)copy_option_part(&p, part, size, ",");
4873 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4874 goto fail;
4875 }
4876 vim_free(buf);
4877 vim_free(part);
4878 return OK;
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004881 {
4882 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004884 }
4885 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
4887 else if (put_escstr(fd, *valuep, 2) == FAIL)
4888 return FAIL;
4889 }
4890 if (put_eol(fd) < 0)
4891 return FAIL;
4892 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004893fail:
4894 vim_free(buf);
4895 vim_free(part);
4896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897}
4898
4899 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004900put_setnum(
4901 FILE *fd,
4902 char *cmd,
4903 char *name,
4904 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905{
4906 long wc;
4907
4908 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4909 return FAIL;
4910 if (wc_use_keyname((char_u *)valuep, &wc))
4911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004912 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4914 return FAIL;
4915 }
4916 else if (fprintf(fd, "%ld", *valuep) < 0)
4917 return FAIL;
4918 if (put_eol(fd) < 0)
4919 return FAIL;
4920 return OK;
4921}
4922
4923 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004924put_setbool(
4925 FILE *fd,
4926 char *cmd,
4927 char *name,
4928 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004930 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004931 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4933 || put_eol(fd) < 0)
4934 return FAIL;
4935 return OK;
4936}
4937
4938/*
4939 * Clear all the terminal options.
4940 * If the option has been allocated, free the memory.
4941 * Terminal options are never hidden or indirect.
4942 */
4943 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004944clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 /*
4947 * Reset a few things before clearing the old options. This may cause
4948 * outputting a few things that the terminal doesn't understand, but the
4949 * screen will be cleared later, so this is OK.
4950 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004951 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 // When starting the GUI close the display opened for the clipboard.
4955 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 if (gui.starting)
4957 clear_xterm_clip();
4958#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004961 free_termoptions();
4962}
4963
4964 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004965free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004966{
4967 struct vimoption *p;
4968
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004969 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (istermoption(p))
4971 {
4972 if (p->flags & P_ALLOCED)
4973 free_string_option(*(char_u **)(p->var));
4974 if (p->flags & P_DEF_ALLOCED)
4975 free_string_option(p->def_val[VI_DEFAULT]);
4976 *(char_u **)(p->var) = empty_option;
4977 p->def_val[VI_DEFAULT] = empty_option;
4978 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004979#ifdef FEAT_EVAL
4980 // remember where the option was cleared
4981 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984 clear_termcodes();
4985}
4986
4987/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004988 * Free the string for one term option, if it was allocated.
4989 * Set the string to empty_option and clear allocated flag.
4990 * "var" points to the option value.
4991 */
4992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004993free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004994{
4995 struct vimoption *p;
4996
4997 for (p = &options[0]; p->fullname != NULL; p++)
4998 if (p->var == var)
4999 {
5000 if (p->flags & P_ALLOCED)
5001 free_string_option(*(char_u **)(p->var));
5002 *(char_u **)(p->var) = empty_option;
5003 p->flags &= ~P_ALLOCED;
5004 break;
5005 }
5006}
5007
5008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 * Set the terminal option defaults to the current value.
5010 * Used after setting the terminal name.
5011 */
5012 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005013set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 struct vimoption *p;
5016
5017 for (p = &options[0]; p->fullname != NULL; p++)
5018 {
5019 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
5020 {
5021 if (p->flags & P_DEF_ALLOCED)
5022 {
5023 free_string_option(p->def_val[VI_DEFAULT]);
5024 p->flags &= ~P_DEF_ALLOCED;
5025 }
5026 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
5027 if (p->flags & P_ALLOCED)
5028 {
5029 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005030 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032 }
5033 }
5034}
5035
5036/*
5037 * return TRUE if 'p' starts with 't_'
5038 */
5039 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005040istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 return (p->fullname[0] == 't' && p->fullname[1] == '_');
5043}
5044
Bram Moolenaardac13472019-09-16 21:06:21 +02005045/*
5046 * Returns TRUE if the option at 'opt_idx' starts with 't_'
5047 */
5048 int
5049istermoption_idx(int opt_idx)
5050{
5051 return istermoption(&options[opt_idx]);
5052}
5053
Bram Moolenaar113e1072019-01-20 15:30:40 +01005054#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005056 * Unset local option value, similar to ":set opt<".
5057 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005058 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005059unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005060{
5061 struct vimoption *p;
5062 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005063 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005064
5065 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02005066 if (opt_idx < 0)
5067 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005068 p = &(options[opt_idx]);
5069
5070 switch ((int)p->indir)
5071 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005072 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005073 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005074 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005075 break;
5076 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005077 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005078 break;
5079 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005080 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005081 break;
5082 case PV_AR:
5083 buf->b_p_ar = -1;
5084 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005085 case PV_BKC:
5086 clear_string_option(&buf->b_p_bkc);
5087 buf->b_bkc_flags = 0;
5088 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005089 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005090 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005091 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005092 case PV_TC:
5093 clear_string_option(&buf->b_p_tc);
5094 buf->b_tc_flags = 0;
5095 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005096 case PV_SISO:
5097 curwin->w_p_siso = -1;
5098 break;
5099 case PV_SO:
5100 curwin->w_p_so = -1;
5101 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005102#ifdef FEAT_FIND_ID
5103 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005104 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005105 break;
5106 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005107 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005108 break;
5109#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005110 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005111 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005112 break;
5113 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005114 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005115 break;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005116#ifdef FEAT_COMPL_FUNC
5117 case PV_TSRFU:
5118 clear_string_option(&buf->b_p_tsrfu);
5119 break;
5120#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005121 case PV_FP:
5122 clear_string_option(&buf->b_p_fp);
5123 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005124#ifdef FEAT_QUICKFIX
5125 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005126 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005127 break;
5128 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005129 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005130 break;
5131 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005132 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005133 break;
5134#endif
5135#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5136 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005137 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005138 break;
5139#endif
5140#if defined(FEAT_CRYPT)
5141 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005142 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005143 break;
5144#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005145#ifdef FEAT_LINEBREAK
5146 case PV_SBR:
5147 clear_string_option(&((win_T *)from)->w_p_sbr);
5148 break;
5149#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005150#ifdef FEAT_STL_OPT
5151 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005152 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005153 break;
5154#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005155 case PV_UL:
5156 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5157 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005158#ifdef FEAT_LISP
5159 case PV_LW:
5160 clear_string_option(&buf->b_p_lw);
5161 break;
5162#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005163 case PV_MENC:
5164 clear_string_option(&buf->b_p_menc);
5165 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005166 case PV_LCS:
5167 clear_string_option(&((win_T *)from)->w_p_lcs);
5168 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5169 redraw_later(NOT_VALID);
5170 break;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005171 case PV_VE:
Gary Johnson51ad8502021-08-03 18:33:08 +02005172 clear_string_option(&((win_T *)from)->w_p_ve);
5173 ((win_T *)from)->w_ve_flags = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +02005174 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005175 }
5176}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005177#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005178
5179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * Get pointer to option variable, depending on local or global scope.
5181 */
5182 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005183get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
5185 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5186 {
5187 if (p->var == VAR_WIN)
5188 return (char_u *)GLOBAL_WO(get_varp(p));
5189 return p->var;
5190 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005191 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
5193 switch ((int)p->indir)
5194 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005195 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005197 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5198 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5199 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005201 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5202 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5203 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005204 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005205 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005206 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005207 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5208 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005210 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5211 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005213 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5214 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005215#ifdef FEAT_COMPL_FUNC
5216 case PV_TSRFU: return (char_u *)&(curbuf->b_p_tsrfu);
5217#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005218#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5219 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5220#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005221#if defined(FEAT_CRYPT)
5222 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5223#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005224#ifdef FEAT_LINEBREAK
5225 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5226#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005227#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005228 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005229#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005230 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005231#ifdef FEAT_LISP
5232 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5233#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005234 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005235 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Gary Johnson53ba05b2021-07-26 22:19:10 +02005236 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005237 case PV_VE: return (char_u *)&(curwin->w_p_ve);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 return get_varp(p);
5243}
5244
5245/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005246 * Get pointer to option variable at 'opt_idx', depending on local or global
5247 * scope.
5248 */
5249 char_u *
5250get_option_varp_scope(int opt_idx, int opt_flags)
5251{
5252 return get_varp_scope(&(options[opt_idx]), opt_flags);
5253}
5254
5255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 * Get pointer to option variable.
5257 */
5258 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005259get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005261 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (p->var == NULL)
5263 return NULL;
5264
5265 switch ((int)p->indir)
5266 {
5267 case PV_NONE: return p->var;
5268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005269 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005270 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005272 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005274 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005276 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005278 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005280 case PV_TC: return *curbuf->b_p_tc != NUL
5281 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005282 case PV_BKC: return *curbuf->b_p_bkc != NUL
5283 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005284 case PV_SISO: return curwin->w_p_siso >= 0
5285 ? (char_u *)&(curwin->w_p_siso) : p->var;
5286 case PV_SO: return curwin->w_p_so >= 0
5287 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005289 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005291 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5293#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005294 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005296 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01005298#ifdef FEAT_COMPL_FUNC
5299 case PV_TSRFU: return *curbuf->b_p_tsrfu != NUL
5300 ? (char_u *)&(curbuf->b_p_tsrfu) : p->var;
5301#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005302 case PV_FP: return *curbuf->b_p_fp != NUL
5303 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005305 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005307 case PV_GP: return *curbuf->b_p_gp != NUL
5308 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5309 case PV_MP: return *curbuf->b_p_mp != NUL
5310 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005312#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5313 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5314 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5315#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005316#if defined(FEAT_CRYPT)
5317 case PV_CM: return *curbuf->b_p_cm != NUL
5318 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5319#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005320#ifdef FEAT_LINEBREAK
5321 case PV_SBR: return *curwin->w_p_sbr != NUL
5322 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5323#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005324#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005325 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005326 ? (char_u *)&(curwin->w_p_stl) : p->var;
5327#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005328 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5329 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005330#ifdef FEAT_LISP
5331 case PV_LW: return *curbuf->b_p_lw != NUL
5332 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5333#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005334 case PV_MENC: return *curbuf->b_p_menc != NUL
5335 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336#ifdef FEAT_ARABIC
5337 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5338#endif
5339 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005340 case PV_LCS: return *curwin->w_p_lcs != NUL
5341 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Gary Johnson51ad8502021-08-03 18:33:08 +02005342 case PV_VE: return *curwin->w_p_ve != NUL
5343 ? (char_u *)&(curwin->w_p_ve) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005344#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005345 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5346#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005347#ifdef FEAT_SYN_HL
5348 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5349 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005350 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005351 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#ifdef FEAT_DIFF
5354 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5355#endif
5356#ifdef FEAT_FOLDING
5357 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5358 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5359 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5360 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5361 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5362 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5363 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5364# ifdef FEAT_EVAL
5365 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5366 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5367# endif
5368 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5369#endif
5370 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005371 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005372#ifdef FEAT_LINEBREAK
5373 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005376 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005377#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5379#endif
5380#ifdef FEAT_RIGHTLEFT
5381 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5382 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5383#endif
5384 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5385 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5386#ifdef FEAT_LINEBREAK
5387 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005388 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5389 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005391 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005393 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005394#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005395 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5396 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5397#endif
5398#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005399 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5400 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5401 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
5404 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5405 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5408 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5410 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5411#ifdef FEAT_CINDENT
5412 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5413 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5414 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5415#endif
5416#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5417 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420#ifdef FEAT_FOLDING
5421 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005424#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005425 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005427#ifdef FEAT_COMPL_FUNC
5428 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005429 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005430#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005431#ifdef FEAT_EVAL
5432 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005435 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005441 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5443 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5444 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5445 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5446#ifdef FEAT_FIND_ID
5447# ifdef FEAT_EVAL
5448 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5449# endif
5450#endif
5451#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5452 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5453 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5454#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005455#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005456 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458#ifdef FEAT_CRYPT
5459 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5460#endif
5461#ifdef FEAT_LISP
5462 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5463#endif
5464 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5465 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5466 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5467 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5468 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005470#ifdef FEAT_TEXTOBJ
5471 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5474#ifdef FEAT_SMARTINDENT
5475 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5479#ifdef FEAT_SEARCHPATH
5480 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5481#endif
5482 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5483#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005484 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005485 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5486#endif
5487#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005488 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5489 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5490 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005491 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492#endif
5493 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5494 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5495 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5496 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005497#ifdef FEAT_PERSISTENT_UNDO
5498 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5501#ifdef FEAT_KEYMAP
5502 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5503#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005504#ifdef FEAT_SIGNS
5505 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5506#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005507#ifdef FEAT_VARTABS
5508 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5509 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5510#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005511 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005513 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 return (char_u *)&(curbuf->b_p_wm);
5515}
5516
5517/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005518 * Return a pointer to the variable for option at 'opt_idx'
5519 */
5520 char_u *
5521get_option_var(int opt_idx)
5522{
5523 return options[opt_idx].var;
5524}
5525
5526/*
5527 * Return the full name of the option at 'opt_idx'
5528 */
5529 char_u *
5530get_option_fullname(int opt_idx)
5531{
5532 return (char_u *)options[opt_idx].fullname;
5533}
5534
5535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 * Get the value of 'equalprg', either the buffer-local one or the global one.
5537 */
5538 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 if (*curbuf->b_p_ep == NUL)
5542 return p_ep;
5543 return curbuf->b_p_ep;
5544}
5545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546/*
5547 * Copy options from one window to another.
5548 * Used when splitting a window.
5549 */
5550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005551win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5554 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005555 after_copy_winopt(wp_to);
5556}
5557
5558/*
5559 * After copying window options: update variables depending on options.
5560 */
5561 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005562after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005563{
5564#ifdef FEAT_LINEBREAK
5565 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005566#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005567#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005568 fill_culopt_flags(NULL, wp);
5569 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005570#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005571 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574/*
5575 * Copy the options from one winopt_T to another.
5576 * Doesn't free the old option values in "to", use clear_winopt() for that.
5577 * The 'scroll' option is not copied, because it depends on the window height.
5578 * The 'previewwindow' option is reset, there can be only one preview window.
5579 */
5580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583#ifdef FEAT_ARABIC
5584 to->wo_arab = from->wo_arab;
5585#endif
5586 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005587 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005589 to->wo_rnu = from->wo_rnu;
Gary Johnson51ad8502021-08-03 18:33:08 +02005590 to->wo_ve = vim_strsave(from->wo_ve);
5591 to->wo_ve_flags = from->wo_ve_flags;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005592#ifdef FEAT_LINEBREAK
5593 to->wo_nuw = from->wo_nuw;
5594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595#ifdef FEAT_RIGHTLEFT
5596 to->wo_rl = from->wo_rl;
5597 to->wo_rlc = vim_strsave(from->wo_rlc);
5598#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005599#ifdef FEAT_LINEBREAK
5600 to->wo_sbr = vim_strsave(from->wo_sbr);
5601#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005602#ifdef FEAT_STL_OPT
5603 to->wo_stl = vim_strsave(from->wo_stl);
5604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005606#ifdef FEAT_DIFF
5607 to->wo_wrap_save = from->wo_wrap_save;
5608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609#ifdef FEAT_LINEBREAK
5610 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005611 to->wo_bri = from->wo_bri;
5612 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005614 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005616 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005617 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005618 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005619#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005620 to->wo_spell = from->wo_spell;
5621#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005622#ifdef FEAT_SYN_HL
5623 to->wo_cuc = from->wo_cuc;
5624 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005625 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005626 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628#ifdef FEAT_DIFF
5629 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005630 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005632#ifdef FEAT_CONCEAL
5633 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005634 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005635#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005636#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005637 to->wo_twk = vim_strsave(from->wo_twk);
5638 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640#ifdef FEAT_FOLDING
5641 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005642 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005644 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 to->wo_fdi = vim_strsave(from->wo_fdi);
5646 to->wo_fml = from->wo_fml;
5647 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005648 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005650 to->wo_fdm_save = from->wo_diff_saved
5651 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 to->wo_fdn = from->wo_fdn;
5653# ifdef FEAT_EVAL
5654 to->wo_fde = vim_strsave(from->wo_fde);
5655 to->wo_fdt = vim_strsave(from->wo_fdt);
5656# endif
5657 to->wo_fmr = vim_strsave(from->wo_fmr);
5658#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005659#ifdef FEAT_SIGNS
5660 to->wo_scl = vim_strsave(from->wo_scl);
5661#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005662
5663#ifdef FEAT_EVAL
5664 // Copy the script context so that we know where the value was last set.
5665 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5666 sizeof(to->wo_script_ctx));
5667#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005668 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669}
5670
5671/*
5672 * Check string options in a window for a NULL value.
5673 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005675check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 check_winopt(&win->w_onebuf_opt);
5678 check_winopt(&win->w_allbuf_opt);
5679}
5680
5681/*
5682 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5683 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005685check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687#ifdef FEAT_FOLDING
5688 check_string_option(&wop->wo_fdi);
5689 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005690 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691# ifdef FEAT_EVAL
5692 check_string_option(&wop->wo_fde);
5693 check_string_option(&wop->wo_fdt);
5694# endif
5695 check_string_option(&wop->wo_fmr);
5696#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005697#ifdef FEAT_SIGNS
5698 check_string_option(&wop->wo_scl);
5699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700#ifdef FEAT_RIGHTLEFT
5701 check_string_option(&wop->wo_rlc);
5702#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005703#ifdef FEAT_LINEBREAK
5704 check_string_option(&wop->wo_sbr);
5705#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005706#ifdef FEAT_STL_OPT
5707 check_string_option(&wop->wo_stl);
5708#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005709#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005710 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005711 check_string_option(&wop->wo_cc);
5712#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005713#ifdef FEAT_CONCEAL
5714 check_string_option(&wop->wo_cocu);
5715#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005716#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005717 check_string_option(&wop->wo_twk);
5718 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005719#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005720#ifdef FEAT_LINEBREAK
5721 check_string_option(&wop->wo_briopt);
5722#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005723 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005724 check_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005725 check_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726}
5727
5728/*
5729 * Free the allocated memory inside a winopt_T.
5730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005732clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734#ifdef FEAT_FOLDING
5735 clear_string_option(&wop->wo_fdi);
5736 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005737 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738# ifdef FEAT_EVAL
5739 clear_string_option(&wop->wo_fde);
5740 clear_string_option(&wop->wo_fdt);
5741# endif
5742 clear_string_option(&wop->wo_fmr);
5743#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005744#ifdef FEAT_SIGNS
5745 clear_string_option(&wop->wo_scl);
5746#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005747#ifdef FEAT_LINEBREAK
5748 clear_string_option(&wop->wo_briopt);
5749#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005750 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#ifdef FEAT_RIGHTLEFT
5752 clear_string_option(&wop->wo_rlc);
5753#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005754#ifdef FEAT_LINEBREAK
5755 clear_string_option(&wop->wo_sbr);
5756#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005757#ifdef FEAT_STL_OPT
5758 clear_string_option(&wop->wo_stl);
5759#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005760#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005761 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762 clear_string_option(&wop->wo_cc);
5763#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005764#ifdef FEAT_CONCEAL
5765 clear_string_option(&wop->wo_cocu);
5766#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005767#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005768 clear_string_option(&wop->wo_twk);
5769 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005770#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005771 clear_string_option(&wop->wo_lcs);
Gary Johnson51ad8502021-08-03 18:33:08 +02005772 clear_string_option(&wop->wo_ve);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773}
5774
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005775#ifdef FEAT_EVAL
5776// Index into the options table for a buffer-local option enum.
5777static int buf_opt_idx[BV_COUNT];
5778# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5779
5780/*
5781 * Initialize buf_opt_idx[] if not done already.
5782 */
5783 static void
5784init_buf_opt_idx(void)
5785{
5786 static int did_init_buf_opt_idx = FALSE;
5787 int i;
5788
5789 if (did_init_buf_opt_idx)
5790 return;
5791 did_init_buf_opt_idx = TRUE;
5792 for (i = 0; !istermoption_idx(i); i++)
5793 if (options[i].indir & PV_BUF)
5794 buf_opt_idx[options[i].indir & PV_MASK] = i;
5795}
5796#else
5797# define COPY_OPT_SCTX(buf, bv)
5798#endif
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800/*
5801 * Copy global option values to local options for one buffer.
5802 * Used when creating a new buffer and sometimes when entering a buffer.
5803 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005804 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5806 * appropriate.
5807 * BCO_NOHELP Don't copy the values to a help buffer.
5808 */
5809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005810buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005813 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 int dont_do_help;
5815 int did_isk = FALSE;
5816
5817 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 * Skip this when the option defaults have not been set yet. Happens when
5819 * main() allocates the first buffer.
5820 */
5821 if (p_cpo != NULL)
5822 {
5823 /*
5824 * Always copy when entering and 'cpo' contains 'S'.
5825 * Don't copy when already initialized.
5826 * Don't copy when 'cpo' contains 's' and not entering.
5827 * 'S' BCO_ENTER initialized 's' should_copy
5828 * yes yes X X TRUE
5829 * yes no yes X FALSE
5830 * no X yes X FALSE
5831 * X no no yes FALSE
5832 * X no no no TRUE
5833 * no yes no X TRUE
5834 */
5835 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5836 && (buf->b_p_initialized
5837 || (!(flags & BCO_ENTER)
5838 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5839 should_copy = FALSE;
5840
5841 if (should_copy || (flags & BCO_ALWAYS))
5842 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005843#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005844 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005845 init_buf_opt_idx();
5846#endif
5847 // Don't copy the options specific to a help buffer when
5848 // BCO_NOHELP is given or the options were initialized already
5849 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5851 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005852 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
5854 save_p_isk = buf->b_p_isk;
5855 buf->b_p_isk = NULL;
5856 }
5857 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005858 * Always free the allocated strings. If not already initialized,
5859 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 */
5861 if (!buf->b_p_initialized)
5862 {
5863 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005864 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005867 switch (*p_ffs)
5868 {
5869 case 'm':
5870 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5871 case 'd':
5872 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5873 case 'u':
5874 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5875 default:
5876 buf->b_p_ff = vim_strsave(p_ff);
5877 }
5878 if (buf->b_p_ff != NULL)
5879 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 buf->b_p_bh = empty_option;
5881 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
5883 else
5884 free_buf_options(buf, FALSE);
5885
5886 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005887 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 buf->b_p_ai_nopaste = p_ai_nopaste;
5889 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005890 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005892 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 buf->b_p_tw_nopaste = p_tw_nopaste;
5894 buf->b_p_tw_nobin = p_tw_nobin;
5895 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005896 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 buf->b_p_wm_nopaste = p_wm_nopaste;
5898 buf->b_p_wm_nobin = p_wm_nobin;
5899 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005900 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005901 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005902 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005903 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005904 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005906 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005908 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005910 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 buf->b_p_ml_nobin = p_ml_nobin;
5912 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005913 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005914 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005915 buf->b_p_swf = FALSE;
5916 else
5917 {
5918 buf->b_p_swf = p_swf;
5919 COPY_OPT_SCTX(buf, BV_INF);
5920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005922 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005923#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005924 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005925 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005927#ifdef FEAT_COMPL_FUNC
5928 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005929 COPY_OPT_SCTX(buf, BV_CFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005930 set_buflocal_cfu_callback(buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005931 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005932 COPY_OPT_SCTX(buf, BV_OFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005933 set_buflocal_ofu_callback(buf);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005934#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005935#ifdef FEAT_EVAL
5936 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005937 COPY_OPT_SCTX(buf, BV_TFU);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00005938 set_buflocal_tfu_callback(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005941 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005943#ifdef FEAT_VARTABS
5944 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005945 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005946 if (p_vsts && p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02005947 (void)tabstop_set(p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005948 else
5949 buf->b_p_vsts_array = 0;
5950 buf->b_p_vsts_nopaste = p_vsts_nopaste
5951 ? vim_strsave(p_vsts_nopaste) : NULL;
5952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005954 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005956 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#ifdef FEAT_FOLDING
5958 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005959 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#endif
5961 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005962 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005963 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005964 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005965 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5966 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005968 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971#ifdef FEAT_SMARTINDENT
5972 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005973 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#endif
5975 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005976 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#ifdef FEAT_CINDENT
5978 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005979 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005981 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005983 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005985 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005988 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5990 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005991 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992#endif
5993#ifdef FEAT_LISP
5994 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005995 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996#endif
5997#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005998 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00006000 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006001 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01006002 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006003#endif
6004#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02006005 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006006 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006007 (void)compile_cap_prog(&buf->b_s);
6008 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006009 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006010 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006011 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02006012 buf->b_s.b_p_spo = vim_strsave(p_spo);
6013 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014#endif
6015#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
6016 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006017 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006019 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01006021 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006022#if defined(FEAT_EVAL)
6023 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006024 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026#ifdef FEAT_CRYPT
6027 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029#endif
6030#ifdef FEAT_SEARCHPATH
6031 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006032 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033#endif
6034#ifdef FEAT_KEYMAP
6035 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006036 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 buf->b_kmap_state |= KEYMAP_INIT;
6038#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006039#ifdef FEAT_TERMINAL
6040 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006041 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02006042#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006043 // This isn't really an option, but copying the langmap and IME
6044 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006046 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006048 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006050 // options that are normally global but also have a local value
6051 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01006053 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006054 buf->b_p_bkc = empty_option;
6055 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056#ifdef FEAT_QUICKFIX
6057 buf->b_p_gp = empty_option;
6058 buf->b_p_mp = empty_option;
6059 buf->b_p_efm = empty_option;
6060#endif
6061 buf->b_p_ep = empty_option;
6062 buf->b_p_kp = empty_option;
6063 buf->b_p_path = empty_option;
6064 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01006065 buf->b_p_tc = empty_option;
6066 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#ifdef FEAT_FIND_ID
6068 buf->b_p_def = empty_option;
6069 buf->b_p_inc = empty_option;
6070# ifdef FEAT_EVAL
6071 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006072 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073# endif
6074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 buf->b_p_dict = empty_option;
6076 buf->b_p_tsr = empty_option;
Bram Moolenaarf4d8b762021-10-17 14:13:09 +01006077#ifdef FEAT_COMPL_FUNC
6078 buf->b_p_tsrfu = empty_option;
6079#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006080#ifdef FEAT_TEXTOBJ
6081 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006082 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00006083#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00006084#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
6085 buf->b_p_bexpr = empty_option;
6086#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02006087#if defined(FEAT_CRYPT)
6088 buf->b_p_cm = empty_option;
6089#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006090#ifdef FEAT_PERSISTENT_UNDO
6091 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006092 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02006093#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01006094#ifdef FEAT_LISP
6095 buf->b_p_lw = empty_option;
6096#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01006097 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 /*
6100 * Don't copy the options set by ex_help(), use the saved values,
6101 * when going from a help buffer to a non-help buffer.
6102 * Don't touch these at all when BCO_NOHELP is used and going from
6103 * or to a help buffer.
6104 */
6105 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006108#ifdef FEAT_VARTABS
6109 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006110 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006111 else
6112 buf->b_p_vts_array = NULL;
6113#endif
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 else
6116 {
6117 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006118 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 did_isk = TRUE;
6120 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006121#ifdef FEAT_VARTABS
6122 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006123 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006124 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006125 (void)tabstop_set(p_vts, &buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006126 else
6127 buf->b_p_vts_array = NULL;
6128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (buf->b_p_bt[0] == 'h')
6131 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006133 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135 }
6136
6137 /*
6138 * When the options should be copied (ignoring BCO_ALWAYS), set the
6139 * flag that indicates that the options have been initialized.
6140 */
6141 if (should_copy)
6142 buf->b_p_initialized = TRUE;
6143 }
6144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006145 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (did_isk)
6147 (void)buf_init_chartab(buf, FALSE);
6148}
6149
6150/*
6151 * Reset the 'modifiable' option and its default value.
6152 */
6153 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006154reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 int opt_idx;
6157
6158 curbuf->b_p_ma = FALSE;
6159 p_ma = FALSE;
6160 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006161 if (opt_idx >= 0)
6162 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163}
6164
6165/*
6166 * Set the global value for 'iminsert' to the local value.
6167 */
6168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006169set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 p_iminsert = curbuf->b_p_iminsert;
6172}
6173
6174/*
6175 * Set the global value for 'imsearch' to the local value.
6176 */
6177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006178set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 p_imsearch = curbuf->b_p_imsearch;
6181}
6182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183static int expand_option_idx = -1;
6184static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6185static int expand_option_flags = 0;
6186
6187 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006188set_context_in_set_cmd(
6189 expand_T *xp,
6190 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006191 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
6193 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006194 long_u flags = 0; // init for GCC
6195 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 char_u *p;
6197 char_u *s;
6198 int is_term_option = FALSE;
6199 int key;
6200
6201 expand_option_flags = opt_flags;
6202
6203 xp->xp_context = EXPAND_SETTINGS;
6204 if (*arg == NUL)
6205 {
6206 xp->xp_pattern = arg;
6207 return;
6208 }
6209 p = arg + STRLEN(arg) - 1;
6210 if (*p == ' ' && *(p - 1) != '\\')
6211 {
6212 xp->xp_pattern = p + 1;
6213 return;
6214 }
6215 while (p > arg)
6216 {
6217 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006218 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (*p == ' ' || *p == ',')
6220 {
6221 while (s > arg && *(s - 1) == '\\')
6222 --s;
6223 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006224 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (*p == ' ' && ((p - s) & 1) == 0)
6226 {
6227 ++p;
6228 break;
6229 }
6230 --p;
6231 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006232 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 xp->xp_context = EXPAND_BOOL_SETTINGS;
6235 p += 2;
6236 }
6237 if (STRNCMP(p, "inv", 3) == 0)
6238 {
6239 xp->xp_context = EXPAND_BOOL_SETTINGS;
6240 p += 3;
6241 }
6242 xp->xp_pattern = arg = p;
6243 if (*arg == '<')
6244 {
6245 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006246 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 return;
6248 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006249 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 xp->xp_context = EXPAND_NOTHING;
6252 return;
6253 }
6254 nextchar = *++p;
6255 is_term_option = TRUE;
6256 expand_option_name[2] = KEY2TERMCAP0(key);
6257 expand_option_name[3] = KEY2TERMCAP1(key);
6258 }
6259 else
6260 {
6261 if (p[0] == 't' && p[1] == '_')
6262 {
6263 p += 2;
6264 if (*p != NUL)
6265 ++p;
6266 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006267 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 nextchar = *++p;
6269 is_term_option = TRUE;
6270 expand_option_name[2] = p[-2];
6271 expand_option_name[3] = p[-1];
6272 }
6273 else
6274 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006275 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6277 p++;
6278 if (*p == NUL)
6279 return;
6280 nextchar = *p;
6281 *p = NUL;
6282 opt_idx = findoption(arg);
6283 *p = nextchar;
6284 if (opt_idx == -1 || options[opt_idx].var == NULL)
6285 {
6286 xp->xp_context = EXPAND_NOTHING;
6287 return;
6288 }
6289 flags = options[opt_idx].flags;
6290 if (flags & P_BOOL)
6291 {
6292 xp->xp_context = EXPAND_NOTHING;
6293 return;
6294 }
6295 }
6296 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006297 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6299 {
6300 ++p;
6301 nextchar = '=';
6302 }
6303 if ((nextchar != '=' && nextchar != ':')
6304 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6305 {
6306 xp->xp_context = EXPAND_UNSUCCESSFUL;
6307 return;
6308 }
6309 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6310 {
6311 xp->xp_context = EXPAND_OLD_SETTING;
6312 if (is_term_option)
6313 expand_option_idx = -1;
6314 else
6315 expand_option_idx = opt_idx;
6316 xp->xp_pattern = p + 1;
6317 return;
6318 }
6319 xp->xp_context = EXPAND_NOTHING;
6320 if (is_term_option || (flags & P_NUM))
6321 return;
6322
6323 xp->xp_pattern = p + 1;
6324
6325 if (flags & P_EXPAND)
6326 {
6327 p = options[opt_idx].var;
6328 if (p == (char_u *)&p_bdir
6329 || p == (char_u *)&p_dir
6330 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006331 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 || p == (char_u *)&p_rtp
6333#ifdef FEAT_SEARCHPATH
6334 || p == (char_u *)&p_cdpath
6335#endif
6336#ifdef FEAT_SESSION
6337 || p == (char_u *)&p_vdir
6338#endif
6339 )
6340 {
6341 xp->xp_context = EXPAND_DIRECTORIES;
6342 if (p == (char_u *)&p_path
6343#ifdef FEAT_SEARCHPATH
6344 || p == (char_u *)&p_cdpath
6345#endif
6346 )
6347 xp->xp_backslash = XP_BS_THREE;
6348 else
6349 xp->xp_backslash = XP_BS_ONE;
6350 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006351 else if (p == (char_u *)&p_ft)
6352 {
6353 xp->xp_context = EXPAND_FILETYPE;
6354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 else
6356 {
6357 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006358 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (p == (char_u *)&p_tags)
6360 xp->xp_backslash = XP_BS_THREE;
6361 else
6362 xp->xp_backslash = XP_BS_ONE;
6363 }
6364 }
6365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006366 // For an option that is a list of file names, find the start of the
6367 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6369 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006370 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (*p == ' ' || *p == ',')
6372 {
6373 s = p;
6374 while (s > xp->xp_pattern && *(s - 1) == '\\')
6375 --s;
6376 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6377 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6378 {
6379 xp->xp_pattern = p + 1;
6380 break;
6381 }
6382 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006383
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006384#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006385 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006386 if (options[opt_idx].var == (char_u *)&p_sps
6387 && STRNCMP(p, "file:", 5) == 0)
6388 {
6389 xp->xp_pattern = p + 5;
6390 break;
6391 }
6392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394}
6395
6396 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006397ExpandSettings(
6398 expand_T *xp,
6399 regmatch_T *regmatch,
6400 int *num_file,
6401 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006403 int num_normal = 0; // Nr of matching non-term-code settings
6404 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 int opt_idx;
6406 int match;
6407 int count = 0;
6408 char_u *str;
6409 int loop;
6410 int is_term_opt;
6411 char_u name_buf[MAX_KEY_NAME_LEN];
6412 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006413 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006415 // do this loop twice:
6416 // loop == 0: count the number of matching options
6417 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 for (loop = 0; loop <= 1; ++loop)
6419 {
6420 regmatch->rm_ic = ic;
6421 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6422 {
K.Takataeeec2542021-06-02 13:28:16 +02006423 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6425 {
6426 if (loop == 0)
6427 num_normal++;
6428 else
6429 (*file)[count++] = vim_strsave((char_u *)names[match]);
6430 }
6431 }
6432 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6433 opt_idx++)
6434 {
6435 if (options[opt_idx].var == NULL)
6436 continue;
6437 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6438 && !(options[opt_idx].flags & P_BOOL))
6439 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006440 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (is_term_opt && num_normal > 0)
6442 continue;
6443 match = FALSE;
6444 if (vim_regexec(regmatch, str, (colnr_T)0)
6445 || (options[opt_idx].shortname != NULL
6446 && vim_regexec(regmatch,
6447 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6448 match = TRUE;
6449 else if (is_term_opt)
6450 {
6451 name_buf[0] = '<';
6452 name_buf[1] = 't';
6453 name_buf[2] = '_';
6454 name_buf[3] = str[2];
6455 name_buf[4] = str[3];
6456 name_buf[5] = '>';
6457 name_buf[6] = NUL;
6458 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6459 {
6460 match = TRUE;
6461 str = name_buf;
6462 }
6463 }
6464 if (match)
6465 {
6466 if (loop == 0)
6467 {
6468 if (is_term_opt)
6469 num_term++;
6470 else
6471 num_normal++;
6472 }
6473 else
6474 (*file)[count++] = vim_strsave(str);
6475 }
6476 }
6477 /*
6478 * Check terminal key codes, these are not in the option table
6479 */
6480 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6481 {
6482 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6483 {
6484 if (!isprint(str[0]) || !isprint(str[1]))
6485 continue;
6486
6487 name_buf[0] = 't';
6488 name_buf[1] = '_';
6489 name_buf[2] = str[0];
6490 name_buf[3] = str[1];
6491 name_buf[4] = NUL;
6492
6493 match = FALSE;
6494 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6495 match = TRUE;
6496 else
6497 {
6498 name_buf[0] = '<';
6499 name_buf[1] = 't';
6500 name_buf[2] = '_';
6501 name_buf[3] = str[0];
6502 name_buf[4] = str[1];
6503 name_buf[5] = '>';
6504 name_buf[6] = NUL;
6505
6506 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6507 match = TRUE;
6508 }
6509 if (match)
6510 {
6511 if (loop == 0)
6512 num_term++;
6513 else
6514 (*file)[count++] = vim_strsave(name_buf);
6515 }
6516 }
6517
6518 /*
6519 * Check special key names.
6520 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006521 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6523 {
6524 name_buf[0] = '<';
6525 STRCPY(name_buf + 1, str);
6526 STRCAT(name_buf, ">");
6527
6528 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6529 {
6530 if (loop == 0)
6531 num_term++;
6532 else
6533 (*file)[count++] = vim_strsave(name_buf);
6534 }
6535 }
6536 }
6537 if (loop == 0)
6538 {
6539 if (num_normal > 0)
6540 *num_file = num_normal;
6541 else if (num_term > 0)
6542 *num_file = num_term;
6543 else
6544 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006545 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (*file == NULL)
6547 {
6548 *file = (char_u **)"";
6549 return FAIL;
6550 }
6551 }
6552 }
6553 return OK;
6554}
6555
6556 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006557ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006559 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 char_u *buf;
6561
6562 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006563 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 if (*file == NULL)
6565 return FAIL;
6566
6567 /*
6568 * For a terminal key code expand_option_idx is < 0.
6569 */
6570 if (expand_option_idx < 0)
6571 {
6572 var = find_termcode(expand_option_name + 2);
6573 if (var == NULL)
6574 expand_option_idx = findoption(expand_option_name);
6575 }
6576
6577 if (expand_option_idx >= 0)
6578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006579 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 option_value2string(&options[expand_option_idx], expand_option_flags);
6581 var = NameBuff;
6582 }
6583 else if (var == NULL)
6584 var = (char_u *)"";
6585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006586 // A backslash is required before some characters. This is the reverse of
6587 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 buf = vim_strsave_escaped(var, escape_chars);
6589
6590 if (buf == NULL)
6591 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006592 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 return FAIL;
6594 }
6595
6596#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006597 // For MS-Windows et al. we don't double backslashes at the start and
6598 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006599 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 if (var[0] == '\\' && var[1] == '\\'
6601 && expand_option_idx >= 0
6602 && (options[expand_option_idx].flags & P_EXPAND)
6603 && vim_isfilec(var[2])
6604 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006605 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606#endif
6607
6608 *file[0] = buf;
6609 *num_file = 1;
6610 return OK;
6611}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
6613/*
6614 * Get the value for the numeric or string option *opp in a nice format into
6615 * NameBuff[]. Must not be called with a hidden option!
6616 */
6617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006618option_value2string(
6619 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006620 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 char_u *varp;
6623
6624 varp = get_varp_scope(opp, opt_flags);
6625
6626 if (opp->flags & P_NUM)
6627 {
6628 long wc = 0;
6629
6630 if (wc_use_keyname(varp, &wc))
6631 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6632 else if (wc != 0)
6633 STRCPY(NameBuff, transchar((int)wc));
6634 else
6635 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6636 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006637 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 {
6639 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006640 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 NameBuff[0] = NUL;
6642#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006643 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006644 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 STRCPY(NameBuff, "*****");
6646#endif
6647 else if (opp->flags & P_EXPAND)
6648 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006649 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 else if ((char_u **)opp->var == &p_pt)
6651 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6652 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006653 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655}
6656
6657/*
6658 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6659 * printed as a keyname.
6660 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6661 */
6662 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006663wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6666 {
6667 *wcp = *(long *)varp;
6668 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6669 return TRUE;
6670 }
6671 return FALSE;
6672}
6673
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 * Return TRUE if "x" is present in 'shortmess' option, or
6676 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6677 */
6678 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006679shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006681 return p_shm != NULL &&
6682 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 || (vim_strchr(p_shm, 'a') != NULL
6684 && vim_strchr((char_u *)SHM_A, x) != NULL));
6685}
6686
6687/*
6688 * paste_option_changed() - Called after p_paste was set or reset.
6689 */
6690 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006691paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 static int old_p_paste = FALSE;
6694 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006695 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696#ifdef FEAT_CMDL_INFO
6697 static int save_ru = 0;
6698#endif
6699#ifdef FEAT_RIGHTLEFT
6700 static int save_ri = 0;
6701 static int save_hkmap = 0;
6702#endif
6703 buf_T *buf;
6704
6705 if (p_paste)
6706 {
6707 /*
6708 * Paste switched from off to on.
6709 * Save the current values, so they can be restored later.
6710 */
6711 if (!old_p_paste)
6712 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006714 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
6716 buf->b_p_tw_nopaste = buf->b_p_tw;
6717 buf->b_p_wm_nopaste = buf->b_p_wm;
6718 buf->b_p_sts_nopaste = buf->b_p_sts;
6719 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006720 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006721#ifdef FEAT_VARTABS
6722 if (buf->b_p_vsts_nopaste)
6723 vim_free(buf->b_p_vsts_nopaste);
6724 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6725 ? vim_strsave(buf->b_p_vsts) : NULL;
6726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006729 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006731 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732#ifdef FEAT_CMDL_INFO
6733 save_ru = p_ru;
6734#endif
6735#ifdef FEAT_RIGHTLEFT
6736 save_ri = p_ri;
6737 save_hkmap = p_hkmap;
6738#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006739 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006740 p_ai_nopaste = p_ai;
6741 p_et_nopaste = p_et;
6742 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 p_tw_nopaste = p_tw;
6744 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006745#ifdef FEAT_VARTABS
6746 if (p_vsts_nopaste)
6747 vim_free(p_vsts_nopaste);
6748 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 }
6751
6752 /*
6753 * Always set the option values, also when 'paste' is set when it is
6754 * already on.
6755 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006756 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006757 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006759 buf->b_p_tw = 0; // textwidth is 0
6760 buf->b_p_wm = 0; // wrapmargin is 0
6761 buf->b_p_sts = 0; // softtabstop is 0
6762 buf->b_p_ai = 0; // no auto-indent
6763 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006764#ifdef FEAT_VARTABS
6765 if (buf->b_p_vsts)
6766 free_string_option(buf->b_p_vsts);
6767 buf->b_p_vsts = empty_option;
6768 if (buf->b_p_vsts_array)
6769 vim_free(buf->b_p_vsts_array);
6770 buf->b_p_vsts_array = 0;
6771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 }
6773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006774 // set global options
6775 p_sm = 0; // no showmatch
6776 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006779 status_redraw_all(); // redraw to remove the ruler
6780 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781#endif
6782#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006783 p_ri = 0; // no reverse insert
6784 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006786 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 p_tw = 0;
6788 p_wm = 0;
6789 p_sts = 0;
6790 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006791#ifdef FEAT_VARTABS
6792 if (p_vsts)
6793 free_string_option(p_vsts);
6794 p_vsts = empty_option;
6795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
6797
6798 /*
6799 * Paste switched from on to off: Restore saved values.
6800 */
6801 else if (old_p_paste)
6802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006803 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006804 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
6806 buf->b_p_tw = buf->b_p_tw_nopaste;
6807 buf->b_p_wm = buf->b_p_wm_nopaste;
6808 buf->b_p_sts = buf->b_p_sts_nopaste;
6809 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006810 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006811#ifdef FEAT_VARTABS
6812 if (buf->b_p_vsts)
6813 free_string_option(buf->b_p_vsts);
6814 buf->b_p_vsts = buf->b_p_vsts_nopaste
6815 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6816 if (buf->b_p_vsts_array)
6817 vim_free(buf->b_p_vsts_array);
6818 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
Bram Moolenaarb7081e12021-09-04 18:47:28 +02006819 (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006820 else
6821 buf->b_p_vsts_array = 0;
6822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
6824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006825 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006827 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006830 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 p_ru = save_ru;
6832#endif
6833#ifdef FEAT_RIGHTLEFT
6834 p_ri = save_ri;
6835 p_hkmap = save_hkmap;
6836#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006837 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006838 p_ai = p_ai_nopaste;
6839 p_et = p_et_nopaste;
6840 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 p_tw = p_tw_nopaste;
6842 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006843#ifdef FEAT_VARTABS
6844 if (p_vsts)
6845 free_string_option(p_vsts);
6846 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849
6850 old_p_paste = p_paste;
6851}
6852
6853/*
6854 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6855 *
6856 * Reset 'compatible' and set the values for options that didn't get set yet
6857 * to the Vim defaults.
6858 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006859 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 */
6861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006864 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006865 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006866 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868 if (!option_was_set((char_u *)"cp"))
6869 {
6870 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006871 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6873 set_option_default(opt_idx, OPT_FREE, FALSE);
6874 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006875 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006877
6878 if (fname != NULL)
6879 {
6880 p = vim_getenv(envname, &dofree);
6881 if (p == NULL)
6882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006883 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006884 p = FullName_save(fname, FALSE);
6885 if (p != NULL)
6886 {
6887 vim_setenv(envname, p);
6888 vim_free(p);
6889 }
6890 }
6891 else if (dofree)
6892 vim_free(p);
6893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894}
6895
6896/*
6897 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6898 */
6899 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006900change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006902 int opt_idx;
6903
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (p_cp != on)
6905 {
6906 p_cp = on;
6907 compatible_set();
6908 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006909 opt_idx = findoption((char_u *)"cp");
6910 if (opt_idx >= 0)
6911 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912}
6913
6914/*
6915 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006916 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 */
6918 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006919option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 int idx;
6922
6923 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006924 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 return FALSE;
6926 if (options[idx].flags & P_WAS_SET)
6927 return TRUE;
6928 return FALSE;
6929}
6930
6931/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006932 * Reset the flag indicating option "name" was set.
6933 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006934 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006935reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006936{
6937 int idx = findoption(name);
6938
6939 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006940 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006941 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006942 return OK;
6943 }
6944 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006945}
6946
6947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 * compatible_set() - Called when 'compatible' has been set or unset.
6949 *
6950 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6951 * flag) to a Vi compatible value.
6952 * When 'compatible' is unset: Set all options that have a different default
6953 * for Vim (without the P_VI_DEF flag) to that default.
6954 */
6955 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006956compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 int opt_idx;
6959
Bram Moolenaardac13472019-09-16 21:06:21 +02006960 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6962 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6963 set_option_default(opt_idx, OPT_FREE, p_cp);
6964 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006965 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966}
6967
Bram Moolenaardac13472019-09-16 21:06:21 +02006968#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970/*
6971 * fill_breakat_flags() -- called when 'breakat' changes value.
6972 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006973 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006974fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006976 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 int i;
6978
6979 for (i = 0; i < 256; i++)
6980 breakat_flags[i] = FALSE;
6981
6982 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006983 for (p = p_breakat; *p; p++)
6984 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986#endif
6987
6988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 * Check if backspacing over something is allowed.
6990 */
6991 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006992can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006993 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006995#ifdef FEAT_JOB_CHANNEL
6996 if (what == BS_START && bt_prompt(curbuf))
6997 return FALSE;
6998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 switch (*p_bs)
7000 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02007001 case '3': return TRUE;
7002 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 case '1': return (what != BS_START);
7004 case '0': return FALSE;
7005 }
7006 return vim_strchr(p_bs, what) != NULL;
7007}
7008
7009/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01007010 * Return the effective 'scrolloff' value for the current window, using the
7011 * global value when appropriate.
7012 */
7013 long
7014get_scrolloff_value(void)
7015{
7016 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
7017}
7018
7019/*
7020 * Return the effective 'sidescrolloff' value for the current window, using the
7021 * global value when appropriate.
7022 */
7023 long
7024get_sidescrolloff_value(void)
7025{
7026 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
7027}
7028
7029/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007030 * Get the local or global value of 'backupcopy'.
7031 */
7032 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007033get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02007034{
7035 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
7036}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02007037
Gary Johnson53ba05b2021-07-26 22:19:10 +02007038/*
7039 * Get the local or global value of the 'virtualedit' flags.
7040 */
7041 unsigned int
7042get_ve_flags(void)
7043{
Gary Johnson51ad8502021-08-03 18:33:08 +02007044 return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags)
7045 & ~(VE_NONE | VE_NONEU);
Gary Johnson53ba05b2021-07-26 22:19:10 +02007046}
7047
Bram Moolenaaree857022019-11-09 23:26:40 +01007048#if defined(FEAT_LINEBREAK) || defined(PROTO)
7049/*
7050 * Get the local or global value of 'showbreak'.
7051 */
7052 char_u *
7053get_showbreak_value(win_T *win)
7054{
7055 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
7056 return p_sbr;
7057 if (STRCMP(win->w_p_sbr, "NONE") == 0)
7058 return empty_option;
7059 return win->w_p_sbr;
7060}
7061#endif
7062
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007063#if defined(FEAT_EVAL) || defined(PROTO)
7064/*
7065 * Get window or buffer local options.
7066 */
7067 dict_T *
7068get_winbuf_options(int bufopt)
7069{
7070 dict_T *d;
7071 int opt_idx;
7072
7073 d = dict_alloc();
7074 if (d == NULL)
7075 return NULL;
7076
Bram Moolenaardac13472019-09-16 21:06:21 +02007077 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007078 {
7079 struct vimoption *opt = &options[opt_idx];
7080
7081 if ((bufopt && (opt->indir & PV_BUF))
7082 || (!bufopt && (opt->indir & PV_WIN)))
7083 {
7084 char_u *varp = get_varp(opt);
7085
7086 if (varp != NULL)
7087 {
7088 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007089 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02007090 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02007091 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007092 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02007093 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007094 }
7095 }
7096 }
7097
7098 return d;
7099}
7100#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02007101
Bram Moolenaardac13472019-09-16 21:06:21 +02007102#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02007103/*
7104 * This is called when 'culopt' is changed
7105 */
Bram Moolenaardac13472019-09-16 21:06:21 +02007106 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02007107fill_culopt_flags(char_u *val, win_T *wp)
7108{
7109 char_u *p;
7110 char_u culopt_flags_new = 0;
7111
7112 if (val == NULL)
7113 p = wp->w_p_culopt;
7114 else
7115 p = val;
7116 while (*p != NUL)
7117 {
7118 if (STRNCMP(p, "line", 4) == 0)
7119 {
7120 p += 4;
7121 culopt_flags_new |= CULOPT_LINE;
7122 }
7123 else if (STRNCMP(p, "both", 4) == 0)
7124 {
7125 p += 4;
7126 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7127 }
7128 else if (STRNCMP(p, "number", 6) == 0)
7129 {
7130 p += 6;
7131 culopt_flags_new |= CULOPT_NBR;
7132 }
7133 else if (STRNCMP(p, "screenline", 10) == 0)
7134 {
7135 p += 10;
7136 culopt_flags_new |= CULOPT_SCRLINE;
7137 }
7138
7139 if (*p != ',' && *p != NUL)
7140 return FAIL;
7141 if (*p == ',')
7142 ++p;
7143 }
7144
7145 // Can't have both "line" and "screenline".
7146 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7147 return FAIL;
7148 wp->w_p_culopt_flags = culopt_flags_new;
7149
7150 return OK;
7151}
7152#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007153
7154/*
7155 * Get the value of 'magic' adjusted for Vim9 script.
7156 */
7157 int
7158magic_isset(void)
7159{
7160 switch (magic_overruled)
7161 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007162 case OPTION_MAGIC_ON: return TRUE;
7163 case OPTION_MAGIC_OFF: return FALSE;
7164 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007165 }
7166#ifdef FEAT_EVAL
7167 if (in_vim9script())
7168 return TRUE;
7169#endif
7170 return p_magic;
7171}
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007172
7173/*
7174 * Set the callback function value for an option that accepts a function name,
7175 * lambda, et al. (e.g. 'operatorfunc', 'tagfunc', etc.)
7176 * Returns OK if the option is successfully set to a function, otherwise
7177 * returns FAIL.
7178 */
7179 int
7180option_set_callback_func(char_u *optval UNUSED, callback_T *optcb UNUSED)
7181{
7182#ifdef FEAT_EVAL
7183 typval_T *tv;
7184 callback_T cb;
7185
7186 if (optval == NULL || *optval == NUL)
7187 {
7188 free_callback(optcb);
7189 return OK;
7190 }
7191
Yegappan Lakshmanan05e59e32021-12-01 10:30:07 +00007192 if (*optval == '{' || (in_vim9script() && *optval == '(')
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00007193 || (STRNCMP(optval, "function(", 9) == 0)
7194 || (STRNCMP(optval, "funcref(", 8) == 0))
7195 // Lambda expression or a funcref
7196 tv = eval_expr(optval, NULL);
7197 else
7198 // treat everything else as a function name string
7199 tv = alloc_string_tv(vim_strsave(optval));
7200 if (tv == NULL)
7201 return FAIL;
7202
7203 cb = get_callback(tv);
7204 if (cb.cb_name == NULL)
7205 {
7206 free_tv(tv);
7207 return FAIL;
7208 }
7209
7210 free_callback(optcb);
7211 set_callback(optcb, &cb);
7212 free_tv(tv);
7213 return OK;
7214#else
7215 return FAIL;
7216#endif
7217}