blob: 33d29a1fc43eebbf8a7e4e2f20689889bbf92442 [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
538/*
539 * Set an option to its default value.
540 * This does not take care of side effects!
541 */
542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100543set_option_default(
544 int opt_idx,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100545 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
546 int compatible) // use Vi default value
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100548 char_u *varp; // pointer to variable for current option
549 int dvi; // index in def_val[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 long_u flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000551 long_u *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
553
554 varp = get_varp_scope(&(options[opt_idx]), both ? OPT_LOCAL : opt_flags);
555 flags = options[opt_idx].flags;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 if (varp != NULL) // skip hidden option, nothing to do for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
558 dvi = ((flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
559 if (flags & P_STRING)
560 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // Use set_string_option_direct() for local options to handle
562 // freeing and allocating the value.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200563 if (options[opt_idx].indir != PV_NONE)
564 set_string_option_direct(NULL, opt_idx,
565 options[opt_idx].def_val[dvi], opt_flags, 0);
566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200568 if ((opt_flags & OPT_FREE) && (flags & P_ALLOCED))
569 free_string_option(*(char_u **)(varp));
570 *(char_u **)varp = options[opt_idx].def_val[dvi];
571 options[opt_idx].flags &= ~P_ALLOCED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
573 }
574 else if (flags & P_NUM)
575 {
Bram Moolenaar5fc1a8b2006-10-17 16:34:24 +0000576 if (options[opt_idx].indir == PV_SCROLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 win_comp_scroll(curwin);
578 else
579 {
Bram Moolenaar375e3392019-01-31 18:26:10 +0100580 long def_val = (long)(long_i)options[opt_idx].def_val[dvi];
581
582 if ((long *)varp == &curwin->w_p_so
583 || (long *)varp == &curwin->w_p_siso)
584 // 'scrolloff' and 'sidescrolloff' local values have a
585 // different default value than the global default.
586 *(long *)varp = -1;
587 else
588 *(long *)varp = def_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100589 // May also set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (both)
591 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
Bram Moolenaar375e3392019-01-31 18:26:10 +0100592 def_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
594 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 else // P_BOOL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100597 // the cast to long is required for Manx C, long_i is needed for
598 // MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000599 *(int *)varp = (int)(long)(long_i)options[opt_idx].def_val[dvi];
Bram Moolenaar8243a792007-05-01 17:05:03 +0000600#ifdef UNIX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100601 // 'modeline' defaults to off for root
Bram Moolenaar8243a792007-05-01 17:05:03 +0000602 if (options[opt_idx].indir == PV_ML && getuid() == ROOT_UID)
603 *(int *)varp = FALSE;
604#endif
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 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) =
608 *(int *)varp;
609 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000610
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100611 // The default value is not insecure.
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000612 flagsp = insecure_flag(opt_idx, opt_flags);
613 *flagsp = *flagsp & ~P_INSECURE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 }
615
616#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200617 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#endif
619}
620
621/*
622 * Set all options (except terminal options) to their default value.
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200623 * When "opt_flags" is non-zero skip 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 */
625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100626set_options_default(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 int opt_flags) // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +0000631 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaardac13472019-09-16 21:06:21 +0200633 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarb341dda2015-08-25 12:56:31 +0200634 if (!(options[i].flags & P_NODEFAULT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200635 && (opt_flags == 0
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100636 || (options[i].var != (char_u *)&p_enc
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200637# if defined(FEAT_CRYPT)
Bram Moolenaare68c25c2015-08-25 15:39:55 +0200638 && options[i].var != (char_u *)&p_cm
Bram Moolenaar80606872015-08-25 21:27:35 +0200639 && options[i].var != (char_u *)&p_key
Bram Moolenaar5ea87a02015-08-26 23:24:09 +0200640# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100641 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 set_option_default(i, opt_flags, p_cp);
643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100644 // The 'scroll' option must be computed for all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +0000645 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 win_comp_scroll(wp);
Bram Moolenaar5a4eceb2014-09-09 17:33:07 +0200647#ifdef FEAT_CINDENT
648 parse_cino(curbuf);
649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650}
651
652/*
653 * Set the Vi-default value of a string option.
654 * Used for 'sh', 'backupskip' and 'term'.
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100655 * When "escape" is TRUE escape spaces with a backslash.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 */
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100657 static void
658set_string_default_esc(char *name, char_u *val, int escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 char_u *p;
661 int opt_idx;
662
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100663 if (escape && vim_strchr(val, ' ') != NULL)
664 p = vim_strsave_escaped(val, (char_u *)" ");
665 else
666 p = vim_strsave(val);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 if (p != NULL) // we don't want a NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {
669 opt_idx = findoption((char_u *)name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000670 if (opt_idx >= 0)
671 {
672 if (options[opt_idx].flags & P_DEF_ALLOCED)
673 vim_free(options[opt_idx].def_val[VI_DEFAULT]);
674 options[opt_idx].def_val[VI_DEFAULT] = p;
675 options[opt_idx].flags |= P_DEF_ALLOCED;
676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678}
679
Bram Moolenaar4bfa8af2018-02-03 15:14:46 +0100680 void
681set_string_default(char *name, char_u *val)
682{
683 set_string_default_esc(name, val, FALSE);
684}
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686/*
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200687 * For an option value that contains comma separated items, find "newval" in
688 * "origval". Return NULL if not found.
689 */
690 static char_u *
691find_dup_item(char_u *origval, char_u *newval, long_u flags)
692{
Bram Moolenaar8f13d822020-09-12 21:04:23 +0200693 int bs = 0;
Bram Moolenaarb00ef052020-09-12 14:53:53 +0200694 size_t newlen;
695 char_u *s;
696
697 if (origval == NULL)
698 return NULL;
699
700 newlen = STRLEN(newval);
701 for (s = origval; *s != NUL; ++s)
702 {
703 if ((!(flags & P_COMMA)
704 || s == origval
705 || (s[-1] == ',' && !(bs & 1)))
706 && STRNCMP(s, newval, newlen) == 0
707 && (!(flags & P_COMMA)
708 || s[newlen] == ','
709 || s[newlen] == NUL))
710 return s;
711 // Count backslashes. Only a comma with an even number of backslashes
712 // or a single backslash preceded by a comma before it is recognized as
713 // a separator.
714 if ((s > origval + 1
715 && s[-1] == '\\'
716 && s[-2] != ',')
717 || (s == origval + 1
718 && s[-1] == '\\'))
719 ++bs;
720 else
721 bs = 0;
722 }
723 return NULL;
724}
725
726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 * Set the Vi-default value of a number option.
728 * Used for 'lines' and 'columns'.
729 */
730 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731set_number_default(char *name, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000733 int opt_idx;
734
735 opt_idx = findoption((char_u *)name);
736 if (opt_idx >= 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000737 options[opt_idx].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200740/*
741 * Set all window-local and buffer-local options to the Vim default.
742 * local-global options will use the global value.
Bram Moolenaar46451042019-08-24 15:50:46 +0200743 * When "do_buffer" is FALSE don't set buffer-local options.
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200744 */
745 void
Bram Moolenaar46451042019-08-24 15:50:46 +0200746set_local_options_default(win_T *wp, int do_buffer)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200747{
748 win_T *save_curwin = curwin;
749 int i;
750
751 curwin = wp;
752 curbuf = curwin->w_buffer;
753 block_autocmds();
754
Bram Moolenaardac13472019-09-16 21:06:21 +0200755 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200756 {
757 struct vimoption *p = &(options[i]);
758 char_u *varp = get_varp_scope(p, OPT_LOCAL);
759
760 if (p->indir != PV_NONE
Bram Moolenaar46451042019-08-24 15:50:46 +0200761 && (do_buffer || (p->indir & PV_BUF) == 0)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200762 && !(options[i].flags & P_NODEFAULT)
763 && !optval_default(p, varp, FALSE))
Bram Moolenaar86173482019-10-01 17:02:16 +0200764 set_option_default(i, OPT_FREE|OPT_LOCAL, FALSE);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200765 }
766
767 unblock_autocmds();
768 curwin = save_curwin;
769 curbuf = curwin->w_buffer;
770}
771
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000772#if defined(EXITFREE) || defined(PROTO)
773/*
774 * Free all options.
775 */
776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100777free_all_options(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000778{
779 int i;
780
Bram Moolenaardac13472019-09-16 21:06:21 +0200781 for (i = 0; !istermoption_idx(i); i++)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000782 {
783 if (options[i].indir == PV_NONE)
784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100785 // global option: free value and default value.
Bram Moolenaar67391142017-02-19 21:07:04 +0100786 if ((options[i].flags & P_ALLOCED) && options[i].var != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000787 free_string_option(*(char_u **)options[i].var);
788 if (options[i].flags & P_DEF_ALLOCED)
789 free_string_option(options[i].def_val[VI_DEFAULT]);
790 }
791 else if (options[i].var != VAR_WIN
792 && (options[i].flags & P_STRING))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100793 // buffer-local option: free global value
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000794 free_string_option(*(char_u **)options[i].var);
795 }
796}
797#endif
798
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800/*
801 * Initialize the options, part two: After getting Rows and Columns and
802 * setting 'term'.
803 */
804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100805set_init_2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000807 int idx;
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 /*
Bram Moolenaaraf2d20c2017-10-29 15:26:57 +0100810 * 'scroll' defaults to half the window height. The stored default is zero,
811 * which results in the actual value computed from the window height.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000813 idx = findoption((char_u *)"scroll");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000814 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000815 set_option_default(idx, OPT_LOCAL, p_cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 comp_col();
817
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000818 /*
819 * 'window' is only for backwards compatibility with Vi.
820 * Default is Rows - 1.
821 */
Bram Moolenaard68071d2006-05-02 22:08:30 +0000822 if (!option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000823 p_window = Rows - 1;
824 set_number_default("window", Rows - 1);
825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 // For DOS console the default is always black.
Bram Moolenaar4f974752019-02-17 17:44:42 +0100827#if !((defined(MSWIN)) && !defined(FEAT_GUI))
Bram Moolenaarf740b292006-02-16 22:11:02 +0000828 /*
829 * If 'background' wasn't set by the user, try guessing the value,
830 * depending on the terminal name. Only need to check for terminals
831 * with a dark background, that can handle color.
832 */
833 idx = findoption((char_u *)"bg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000834 if (idx >= 0 && !(options[idx].flags & P_WAS_SET)
835 && *term_bg_default() == 'd')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000837 set_string_option_direct(NULL, idx, (char_u *)"dark", OPT_FREE, 0);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 // don't mark it as set, when starting the GUI it may be
839 // changed again
Bram Moolenaarf740b292006-02-16 22:11:02 +0000840 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000843
844#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 parse_shape_opt(SHAPE_CURSOR); // set cursor shapes from 'guicursor'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000846#endif
847#ifdef FEAT_MOUSESHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100848 parse_shape_opt(SHAPE_MOUSE); // set mouse shapes from 'mouseshape'
Bram Moolenaar58d98232005-07-23 22:25:46 +0000849#endif
850#ifdef FEAT_PRINTER
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100851 (void)parse_printoptions(); // parse 'printoptions' default value
Bram Moolenaar58d98232005-07-23 22:25:46 +0000852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854
855/*
856 * Initialize the options, part three: After reading the .vimrc
857 */
858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100859set_init_3(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100861#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862/*
863 * Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
864 * This is done after other initializations, where 'shell' might have been
865 * set, but only if they have not been set before.
866 */
867 char_u *p;
868 int idx_srr;
869 int do_srr;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100870# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 int idx_sp;
872 int do_sp;
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875 idx_srr = findoption((char_u *)"srr");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000876 if (idx_srr < 0)
877 do_srr = FALSE;
878 else
879 do_srr = !(options[idx_srr].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100880# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 idx_sp = findoption((char_u *)"sp");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000882 if (idx_sp < 0)
883 do_sp = FALSE;
884 else
885 do_sp = !(options[idx_sp].flags & P_WAS_SET);
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100886# endif
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200887 p = get_isolated_shell_name();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (p != NULL)
889 {
890 /*
891 * Default for p_sp is "| tee", for p_srr is ">".
892 * For known shells it is changed here to include stderr.
893 */
894 if ( fnamecmp(p, "csh") == 0
895 || fnamecmp(p, "tcsh") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100896# if defined(MSWIN) // also check with .exe extension
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 || fnamecmp(p, "csh.exe") == 0
898 || fnamecmp(p, "tcsh.exe") == 0
899# endif
900 )
901 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (do_sp)
904 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100905# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 p_sp = (char_u *)">&";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100907# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 p_sp = (char_u *)"|& tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
911 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 if (do_srr)
914 {
915 p_srr = (char_u *)">&";
916 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
917 }
918 }
919 else
Natanael Copa56318362021-05-06 18:46:35 +0200920 // Always use POSIX shell style redirection if we reach this
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if ( fnamecmp(p, "sh") == 0
922 || fnamecmp(p, "ksh") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200923 || fnamecmp(p, "mksh") == 0
924 || fnamecmp(p, "pdksh") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 || fnamecmp(p, "zsh") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000926 || fnamecmp(p, "zsh-beta") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 || fnamecmp(p, "bash") == 0
Bram Moolenaar75a8d742014-05-07 15:10:21 +0200928 || fnamecmp(p, "fish") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200929 || fnamecmp(p, "ash") == 0
930 || fnamecmp(p, "dash") == 0
Bram Moolenaar4f974752019-02-17 17:44:42 +0100931# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 || fnamecmp(p, "cmd") == 0
933 || fnamecmp(p, "sh.exe") == 0
934 || fnamecmp(p, "ksh.exe") == 0
Bram Moolenaarf1fda2d2011-04-28 12:57:36 +0200935 || fnamecmp(p, "mksh.exe") == 0
936 || fnamecmp(p, "pdksh.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 || fnamecmp(p, "zsh.exe") == 0
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000938 || fnamecmp(p, "zsh-beta.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 || fnamecmp(p, "bash.exe") == 0
940 || fnamecmp(p, "cmd.exe") == 0
Natanael Copa56318362021-05-06 18:46:35 +0200941 || fnamecmp(p, "dash.exe") == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942# endif
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100943 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100945# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (do_sp)
947 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100948# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 p_sp = (char_u *)">%s 2>&1";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100950# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 p_sp = (char_u *)"2>&1| tee";
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 options[idx_sp].def_val[VI_DEFAULT] = p_sp;
954 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (do_srr)
957 {
958 p_srr = (char_u *)">%s 2>&1";
959 options[idx_srr].def_val[VI_DEFAULT] = p_srr;
960 }
961 }
962 vim_free(p);
963 }
964#endif
965
Bram Moolenaar4f974752019-02-17 17:44:42 +0100966#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 /*
Bram Moolenaara64ba222012-02-12 23:23:31 +0100968 * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
969 * 'shell' option.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * This is done after other initializations, where 'shell' might have been
971 * set, but only if they have not been set before. Default for p_shcf is
972 * "/c", for p_shq is "". For "sh" like shells it is changed here to
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100973 * "-c" and "\"". And for Win32 we need to set p_sxq instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000975 if (strstr((char *)gettail(p_sh), "sh") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
977 int idx3;
978
979 idx3 = findoption((char_u *)"shcf");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000980 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
982 p_shcf = (char_u *)"-c";
983 options[idx3].def_val[VI_DEFAULT] = p_shcf;
984 }
985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100986 // Somehow Win32 requires the quotes around the redirection too
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 idx3 = findoption((char_u *)"sxq");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +0000988 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
990 p_sxq = (char_u *)"\"";
991 options[idx3].def_val[VI_DEFAULT] = p_sxq;
992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
Bram Moolenaara64ba222012-02-12 23:23:31 +0100994 else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
995 {
996 int idx3;
997
998 /*
999 * cmd.exe on Windows will strip the first and last double quote given
1000 * on the command line, e.g. most of the time things like:
1001 * cmd /c "my path/to/echo" "my args to echo"
1002 * become:
1003 * my path/to/echo" "my args to echo
1004 * when executed.
1005 *
Bram Moolenaar034b1152012-02-19 18:19:30 +01001006 * To avoid this, set shellxquote to surround the command in
1007 * parenthesis. This appears to make most commands work, without
1008 * breaking commands that worked previously, such as
1009 * '"path with spaces/cmd" "a&b"'.
Bram Moolenaara64ba222012-02-12 23:23:31 +01001010 */
Bram Moolenaara64ba222012-02-12 23:23:31 +01001011 idx3 = findoption((char_u *)"sxq");
1012 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1013 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001014 p_sxq = (char_u *)"(";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001015 options[idx3].def_val[VI_DEFAULT] = p_sxq;
1016 }
1017
Bram Moolenaara64ba222012-02-12 23:23:31 +01001018 idx3 = findoption((char_u *)"shcf");
1019 if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
1020 {
Bram Moolenaar034b1152012-02-19 18:19:30 +01001021 p_shcf = (char_u *)"/c";
Bram Moolenaara64ba222012-02-12 23:23:31 +01001022 options[idx3].def_val[VI_DEFAULT] = p_shcf;
1023 }
1024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#endif
1026
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001027 if (BUFEMPTY())
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001028 {
1029 int idx_ffs = findoption((char_u *)"ffs");
1030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001031 // Apply the first entry of 'fileformats' to the initial buffer.
Bram Moolenaar364fa5c2016-03-20 17:53:25 +01001032 if (idx_ffs >= 0 && (options[idx_ffs].flags & P_WAS_SET))
1033 set_fileformat(default_fileformat(), OPT_LOCAL);
1034 }
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036#ifdef FEAT_TITLE
1037 set_title_defaults();
1038#endif
1039}
1040
1041#if defined(FEAT_MULTI_LANG) || defined(PROTO)
1042/*
1043 * When 'helplang' is still at its default value, set it to "lang".
1044 * Only the first two characters of "lang" are used.
1045 */
1046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001047set_helplang_default(char_u *lang)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
1049 int idx;
1050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001051 if (lang == NULL || STRLEN(lang) < 2) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 return;
1053 idx = findoption((char_u *)"hlg");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001054 if (idx >= 0 && !(options[idx].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 {
1056 if (options[idx].flags & P_ALLOCED)
1057 free_string_option(p_hlg);
1058 p_hlg = vim_strsave(lang);
1059 if (p_hlg == NULL)
1060 p_hlg = empty_option;
1061 else
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001062 {
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001063 // zh_CN becomes "cn", zh_TW becomes "tw"
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001064 if (STRNICMP(p_hlg, "zh_", 3) == 0 && STRLEN(p_hlg) >= 5)
1065 {
1066 p_hlg[0] = TOLOWER_ASC(p_hlg[3]);
1067 p_hlg[1] = TOLOWER_ASC(p_hlg[4]);
1068 }
Bram Moolenaardcd71cb2018-11-04 14:40:47 +01001069 // any C like setting, such as C.UTF-8, becomes "en"
1070 else if (STRLEN(p_hlg) >= 1 && *p_hlg == 'C')
1071 {
1072 p_hlg[0] = 'e';
1073 p_hlg[1] = 'n';
1074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 p_hlg[2] = NUL;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 options[idx].flags |= P_ALLOCED;
1078 }
1079}
1080#endif
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082#ifdef FEAT_TITLE
1083/*
1084 * 'title' and 'icon' only default to true if they have not been set or reset
1085 * in .vimrc and we can read the old value.
1086 * When 'title' and 'icon' have been reset in .vimrc, we won't even check if
1087 * they can be reset. This reduces startup time when using X on a remote
1088 * machine.
1089 */
1090 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001091set_title_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
1093 int idx1;
1094 long val;
1095
1096 /*
1097 * If GUI is (going to be) used, we can always set the window title and
1098 * icon name. Saves a bit of time, because the X11 display server does
1099 * not need to be contacted.
1100 */
1101 idx1 = findoption((char_u *)"title");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001102 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
1104#ifdef FEAT_GUI
1105 if (gui.starting || gui.in_use)
1106 val = TRUE;
1107 else
1108#endif
1109 val = mch_can_restore_title();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001110 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 p_title = val;
1112 }
1113 idx1 = findoption((char_u *)"icon");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001114 if (idx1 >= 0 && !(options[idx1].flags & P_WAS_SET))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116#ifdef FEAT_GUI
1117 if (gui.starting || gui.in_use)
1118 val = TRUE;
1119 else
1120#endif
1121 val = mch_can_restore_icon();
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001122 options[idx1].def_val[VI_DEFAULT] = (char_u *)(long_i)val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 p_icon = val;
1124 }
1125}
1126#endif
1127
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001128 void
1129ex_set(exarg_T *eap)
1130{
1131 int flags = 0;
1132
1133 if (eap->cmdidx == CMD_setlocal)
1134 flags = OPT_LOCAL;
1135 else if (eap->cmdidx == CMD_setglobal)
1136 flags = OPT_GLOBAL;
1137#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaare1004402020-10-24 20:49:43 +02001138 if ((cmdmod.cmod_flags & CMOD_BROWSE) && flags == 0)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01001139 ex_options(eap);
1140 else
1141#endif
1142 {
1143 if (eap->forceit)
1144 flags |= OPT_ONECOLUMN;
1145 (void)do_set(eap->arg, flags);
1146 }
1147}
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149/*
1150 * Parse 'arg' for option settings.
1151 *
1152 * 'arg' may be IObuff, but only when no errors can be present and option
1153 * does not need to be expanded with option_expand().
1154 * "opt_flags":
1155 * 0 for ":set"
Bram Moolenaara3227e22006-03-08 21:32:40 +00001156 * OPT_GLOBAL for ":setglobal"
1157 * OPT_LOCAL for ":setlocal" and a modeline
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 * OPT_MODELINE for a modeline
Bram Moolenaara3227e22006-03-08 21:32:40 +00001159 * OPT_WINONLY to only set window-local options
1160 * OPT_NOWIN to skip setting window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 *
1162 * returns FAIL if an error is detected, OK otherwise
1163 */
1164 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001165do_set(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 char_u *arg, // option string (may be written to!)
Bram Moolenaar9b578142016-01-30 19:39:49 +01001167 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
1169 int opt_idx;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001170 char *errmsg;
1171 char errbuf[80];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 char_u *startarg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001173 int prefix; // 1: nothing, 0: "no", 2: "inv" in front of name
1174 int nextchar; // next non-white char after option name
1175 int afterchar; // character just after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int len;
1177 int i;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001178 varnumber_T value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 int key;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001180 long_u flags; // flags for current option
1181 char_u *varp = NULL; // pointer to variable for current option
1182 int did_show = FALSE; // already showed one value
1183 int adding; // "opt+=arg"
1184 int prepending; // "opt^=arg"
1185 int removing; // "opt-=arg"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 int cp_val = 0;
1187 char_u key_name[2];
1188
1189 if (*arg == NUL)
1190 {
1191 showoptions(0, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001192 did_show = TRUE;
1193 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001196 while (*arg != NUL) // loop to process all options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 errmsg = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001199 startarg = arg; // remember for error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001201 if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3])
1202 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
1204 /*
1205 * ":set all" show all options.
1206 * ":set all&" set all options to their default value.
1207 */
1208 arg += 3;
1209 if (*arg == '&')
1210 {
1211 ++arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001212 // Only for :set command set global value of local options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 set_options_default(OPT_FREE | opt_flags);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02001214 didset_options();
1215 didset_options2();
Bram Moolenaarb341dda2015-08-25 12:56:31 +02001216 redraw_all_later(CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 else
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 showoptions(1, opt_flags);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001221 did_show = TRUE;
1222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224 else if (STRNCMP(arg, "termcap", 7) == 0 && !(opt_flags & OPT_MODELINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 showoptions(2, opt_flags);
1227 show_termcodes();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001228 did_show = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 arg += 7;
1230 }
1231 else
1232 {
1233 prefix = 1;
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00001234 if (STRNCMP(arg, "no", 2) == 0 && STRNCMP(arg, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 prefix = 0;
1237 arg += 2;
1238 }
1239 else if (STRNCMP(arg, "inv", 3) == 0)
1240 {
1241 prefix = 2;
1242 arg += 3;
1243 }
1244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 // find end of name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 key = 0;
1247 if (*arg == '<')
1248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 opt_idx = -1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 // look out for <t_>;>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
1252 len = 5;
1253 else
1254 {
1255 len = 1;
1256 while (arg[len] != NUL && arg[len] != '>')
1257 ++len;
1258 }
1259 if (arg[len] != '>')
1260 {
1261 errmsg = e_invarg;
1262 goto skip;
1263 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 arg[len] = NUL; // put NUL after name
1265 if (arg[1] == 't' && arg[2] == '_') // could be term code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 opt_idx = findoption(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001267 arg[len++] = '>'; // restore '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001269 key = find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271 else
1272 {
1273 len = 0;
1274 /*
1275 * The two characters after "t_" may not be alphanumeric.
1276 */
1277 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
1278 len = 4;
1279 else
1280 while (ASCII_ISALNUM(arg[len]) || arg[len] == '_')
1281 ++len;
1282 nextchar = arg[len];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 arg[len] = NUL; // put NUL after name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 opt_idx = findoption(arg);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001285 arg[len] = nextchar; // restore nextchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (opt_idx == -1)
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001287 key = find_key_option(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001290 // remember character after option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 afterchar = arg[len];
1292
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001293 // skip white space, allow ":set ai ?"
Bram Moolenaar1c465442017-03-12 20:10:05 +01001294 while (VIM_ISWHITE(arg[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ++len;
1296
1297 adding = FALSE;
1298 prepending = FALSE;
1299 removing = FALSE;
1300 if (arg[len] != NUL && arg[len + 1] == '=')
1301 {
1302 if (arg[len] == '+')
1303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001304 adding = TRUE; // "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ++len;
1306 }
1307 else if (arg[len] == '^')
1308 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001309 prepending = TRUE; // "^="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 ++len;
1311 }
1312 else if (arg[len] == '-')
1313 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001314 removing = TRUE; // "-="
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 ++len;
1316 }
1317 }
1318 nextchar = arg[len];
1319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 if (opt_idx == -1 && key == 0) // found a mismatch: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001322 errmsg = N_("E518: Unknown option");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 goto skip;
1324 }
1325
1326 if (opt_idx >= 0)
1327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001328 if (options[opt_idx].var == NULL) // hidden option: skip
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001330 // Only give an error message when requesting the value of
1331 // a hidden option, ignore setting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (vim_strchr((char_u *)"=:!&<", nextchar) == NULL
1333 && (!(options[opt_idx].flags & P_BOOL)
1334 || nextchar == '?'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001335 errmsg = N_("E519: Option not supported");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 goto skip;
1337 }
1338
1339 flags = options[opt_idx].flags;
1340 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
1341 }
1342 else
1343 {
1344 flags = P_STRING;
1345 if (key < 0)
1346 {
1347 key_name[0] = KEY2TERMCAP0(key);
1348 key_name[1] = KEY2TERMCAP1(key);
1349 }
1350 else
1351 {
1352 key_name[0] = KS_KEY;
1353 key_name[1] = (key & 0xff);
1354 }
1355 }
1356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001357 // Skip all options that are not window-local (used when showing
1358 // an already loaded buffer in a window).
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001359 if ((opt_flags & OPT_WINONLY)
1360 && (opt_idx < 0 || options[opt_idx].var != VAR_WIN))
1361 goto skip;
1362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001363 // Skip all options that are window-local (used for :vimgrep).
Bram Moolenaara3227e22006-03-08 21:32:40 +00001364 if ((opt_flags & OPT_NOWIN) && opt_idx >= 0
1365 && options[opt_idx].var == VAR_WIN)
1366 goto skip;
1367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001368 // Disallow changing some options from modelines.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001369 if (opt_flags & OPT_MODELINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 {
Bram Moolenaar865242e2010-07-14 21:12:05 +02001371 if (flags & (P_SECURE | P_NO_ML))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001372 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001373 errmsg = N_("E520: Not allowed in a modeline");
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001374 goto skip;
1375 }
Bram Moolenaar110289e2019-05-23 15:38:06 +02001376 if ((flags & P_MLE) && !p_mle)
1377 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001378 errmsg = N_("E992: Not allowed in a modeline when 'modelineexpr' is off");
Bram Moolenaar110289e2019-05-23 15:38:06 +02001379 goto skip;
1380 }
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001381#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001382 // In diff mode some options are overruled. This avoids that
1383 // 'foldmethod' becomes "marker" instead of "diff" and that
1384 // "wrap" gets set.
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001385 if (curwin->w_p_diff
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001386 && opt_idx >= 0 // shut up coverity warning
Bram Moolenaara6c07602017-03-05 21:18:27 +01001387 && (
1388#ifdef FEAT_FOLDING
1389 options[opt_idx].indir == PV_FDM ||
1390#endif
1391 options[opt_idx].indir == PV_WRAP))
Bram Moolenaar1bf0ddc2009-02-11 15:47:05 +00001392 goto skip;
Bram Moolenaarf69d9a32009-02-11 21:48:40 +00001393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001397 // Disallow changing some options in the sandbox
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 if (sandbox != 0 && (flags & P_SECURE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 {
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001400 errmsg = e_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 goto skip;
1402 }
1403#endif
1404
1405 if (vim_strchr((char_u *)"?=:!&<", nextchar) != NULL)
1406 {
1407 arg += len;
1408 cp_val = p_cp;
1409 if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i')
1410 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001411 if (arg[3] == 'm') // "opt&vim": set to Vim default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
1413 cp_val = FALSE;
1414 arg += 3;
1415 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001416 else // "opt&vi": set to Vi default
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
1418 cp_val = TRUE;
1419 arg += 2;
1420 }
1421 }
1422 if (vim_strchr((char_u *)"?!&<", nextchar) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001423 && arg[1] != NUL && !VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
1425 errmsg = e_trailing;
1426 goto skip;
1427 }
1428 }
1429
1430 /*
Bram Moolenaara80faa82020-04-12 19:37:17 +02001431 * allow '=' and ':' for historical reasons (MSDOS command.com
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001432 * allows only one '=' character per "set" command line. grrr. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 */
1434 if (nextchar == '?'
1435 || (prefix == 1
1436 && vim_strchr((char_u *)"=:&<", nextchar) == NULL
1437 && !(flags & P_BOOL)))
1438 {
1439 /*
1440 * print value
1441 */
1442 if (did_show)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001443 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 else
1445 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001446 gotocmdline(TRUE); // cursor at status line
1447 did_show = TRUE; // remember that we did a line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449 if (opt_idx >= 0)
1450 {
1451 showoneopt(&options[opt_idx], opt_flags);
1452#ifdef FEAT_EVAL
1453 if (p_verbose > 0)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001455 // Mention where the option was last set.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001456 if (varp == options[opt_idx].var)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001457 last_set_msg(options[opt_idx].script_ctx);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001458 else if ((int)options[opt_idx].indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001459 last_set_msg(curwin->w_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001460 (int)options[opt_idx].indir & PV_MASK]);
1461 else if ((int)options[opt_idx].indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001462 last_set_msg(curbuf->b_p_script_ctx[
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001463 (int)options[opt_idx].indir & PV_MASK]);
1464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465#endif
1466 }
1467 else
1468 {
1469 char_u *p;
1470
1471 p = find_termcode(key_name);
1472 if (p == NULL)
1473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001474 errmsg = N_("E846: Key code not set");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 goto skip;
1476 }
1477 else
1478 (void)show_one_termcode(key_name, p, TRUE);
1479 }
1480 if (nextchar != '?'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001481 && nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 errmsg = e_trailing;
1483 }
1484 else
1485 {
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001486 int value_is_replaced = !prepending && !adding && !removing;
Bram Moolenaar916a8182018-11-25 02:18:29 +01001487 int value_checked = FALSE;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01001488
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001489 if (flags & P_BOOL) // boolean
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
1491 if (nextchar == '=' || nextchar == ':')
1492 {
1493 errmsg = e_invarg;
1494 goto skip;
1495 }
1496
1497 /*
1498 * ":set opt!": invert
1499 * ":set opt&": reset to default value
1500 * ":set opt<": reset to global value
1501 */
1502 if (nextchar == '!')
1503 value = *(int *)(varp) ^ 1;
1504 else if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001505 value = (int)(long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 ((flags & P_VI_DEF) || cp_val)
1507 ? VI_DEFAULT : VIM_DEFAULT];
1508 else if (nextchar == '<')
1509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001510 // For 'autoread' -1 means to use global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if ((int *)varp == &curbuf->b_p_ar
1512 && opt_flags == OPT_LOCAL)
1513 value = -1;
1514 else
1515 value = *(int *)get_varp_scope(&(options[opt_idx]),
1516 OPT_GLOBAL);
1517 }
1518 else
1519 {
1520 /*
1521 * ":set invopt": invert
1522 * ":set opt" or ":set noopt": set or reset
1523 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001524 if (nextchar != NUL && !VIM_ISWHITE(afterchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 errmsg = e_trailing;
1527 goto skip;
1528 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001529 if (prefix == 2) // inv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 value = *(int *)(varp) ^ 1;
1531 else
1532 value = prefix;
1533 }
1534
1535 errmsg = set_bool_option(opt_idx, varp, (int)value,
1536 opt_flags);
1537 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001538 else // numeric or string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
1540 if (vim_strchr((char_u *)"=:&<", nextchar) == NULL
1541 || prefix != 1)
1542 {
1543 errmsg = e_invarg;
1544 goto skip;
1545 }
1546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001547 if (flags & P_NUM) // numeric
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
1549 /*
1550 * Different ways to set a number option:
1551 * & set to default value
1552 * < set to global value
1553 * <xx> accept special key codes for 'wildchar'
1554 * c accept any non-digit for 'wildchar'
1555 * [-]0-9 set number
1556 * other error
1557 */
1558 ++arg;
1559 if (nextchar == '&')
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001560 value = (long)(long_i)options[opt_idx].def_val[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 ((flags & P_VI_DEF) || cp_val)
1562 ? VI_DEFAULT : VIM_DEFAULT];
1563 else if (nextchar == '<')
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001564 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001565 // For 'undolevels' NO_LOCAL_UNDOLEVEL means to
1566 // use the global value.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001567 if ((long *)varp == &curbuf->b_p_ul
1568 && opt_flags == OPT_LOCAL)
1569 value = NO_LOCAL_UNDOLEVEL;
1570 else
1571 value = *(long *)get_varp_scope(
1572 &(options[opt_idx]), OPT_GLOBAL);
1573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 else if (((long *)varp == &p_wc
1575 || (long *)varp == &p_wcm)
1576 && (*arg == '<'
1577 || *arg == '^'
Bram Moolenaar1c465442017-03-12 20:10:05 +01001578 || (*arg != NUL
1579 && (!arg[1] || VIM_ISWHITE(arg[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 && !VIM_ISDIGIT(*arg))))
1581 {
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001582 value = string_to_key(arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (value == 0 && (long *)varp != &p_wcm)
1584 {
1585 errmsg = e_invarg;
1586 goto skip;
1587 }
1588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 else if (*arg == '-' || VIM_ISDIGIT(*arg))
1590 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591 // Allow negative (for 'undolevels'), octal and
1592 // hex numbers.
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001593 vim_str2nr(arg, NULL, &i, STR2NR_ALL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001594 &value, NULL, 0, TRUE);
Bram Moolenaar06e2c812019-06-12 19:05:48 +02001595 if (i == 0 || (arg[i] != NUL
1596 && !VIM_ISWHITE(arg[i])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001598 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 goto skip;
1600 }
1601 }
1602 else
1603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001604 errmsg = N_("E521: Number required after =");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 goto skip;
1606 }
1607
1608 if (adding)
1609 value = *(long *)varp + value;
1610 if (prepending)
1611 value = *(long *)varp * value;
1612 if (removing)
1613 value = *(long *)varp - value;
1614 errmsg = set_num_option(opt_idx, varp, value,
Bram Moolenaar555b2802005-05-19 21:08:39 +00001615 errbuf, sizeof(errbuf), opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001617 else if (opt_idx >= 0) // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001619 char_u *save_arg = NULL;
1620 char_u *s = NULL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001621 char_u *oldval = NULL; // previous value if *varp
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001622 char_u *newval;
1623 char_u *origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001624 char_u *origval_l = NULL;
1625 char_u *origval_g = NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001626#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001627 char_u *saved_origval = NULL;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001628 char_u *saved_origval_l = NULL;
1629 char_u *saved_origval_g = NULL;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001630 char_u *saved_newval = NULL;
Bram Moolenaar53744302015-07-17 17:38:22 +02001631#endif
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001632 unsigned newlen;
1633 int comma;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 int new_value_alloced; // new string option
1635 // was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001637 // When using ":set opt=val" for a global option
1638 // with a local value the local value will be
1639 // reset, use the global value here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001641 && ((int)options[opt_idx].indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 varp = options[opt_idx].var;
1643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001644 // The old value is kept until we are sure that the
1645 // new value is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 oldval = *(char_u **)varp;
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001647
Bram Moolenaard7c96872019-06-15 17:12:48 +02001648 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1649 {
1650 origval_l = *(char_u **)get_varp_scope(
1651 &(options[opt_idx]), OPT_LOCAL);
1652 origval_g = *(char_u **)get_varp_scope(
1653 &(options[opt_idx]), OPT_GLOBAL);
1654
1655 // A global-local string option might have an empty
1656 // option as value to indicate that the global
1657 // value should be used.
1658 if (((int)options[opt_idx].indir & PV_BOTH)
1659 && origval_l == empty_option)
1660 origval_l = origval_g;
1661 }
1662
1663 // When setting the local value of a global
1664 // option, the old value may be the global value.
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001665 if (((int)options[opt_idx].indir & PV_BOTH)
1666 && (opt_flags & OPT_LOCAL))
1667 origval = *(char_u **)get_varp(
1668 &options[opt_idx]);
1669 else
1670 origval = oldval;
1671
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001672 if (nextchar == '&') // set to default val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 newval = options[opt_idx].def_val[
1675 ((flags & P_VI_DEF) || cp_val)
1676 ? VI_DEFAULT : VIM_DEFAULT];
1677 if ((char_u **)varp == &p_bg)
1678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001679 // guess the value of 'background'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680#ifdef FEAT_GUI
1681 if (gui.in_use)
1682 newval = gui_bg_default();
1683 else
1684#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001685 newval = term_bg_default();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 }
1687
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001688 // expand environment variables and ~ (since the
1689 // default value was already expanded, only
1690 // required when an environment variable was set
1691 // later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (newval == NULL)
1693 newval = empty_option;
1694 else
1695 {
1696 s = option_expand(opt_idx, newval);
1697 if (s == NULL)
1698 s = newval;
1699 newval = vim_strsave(s);
1700 }
1701 new_value_alloced = TRUE;
1702 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001703 else if (nextchar == '<') // set to global val
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 {
1705 newval = vim_strsave(*(char_u **)get_varp_scope(
1706 &(options[opt_idx]), OPT_GLOBAL));
1707 new_value_alloced = TRUE;
1708 }
1709 else
1710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001711 ++arg; // jump to after the '=' or ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
1713 /*
1714 * Set 'keywordprg' to ":help" if an empty
1715 * value was passed to :set by the user.
1716 * Misuse errbuf[] for the resulting string.
1717 */
1718 if (varp == (char_u *)&p_kp
1719 && (*arg == NUL || *arg == ' '))
1720 {
1721 STRCPY(errbuf, ":help");
1722 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001723 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
1725 /*
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001726 * Convert 'backspace' number to string, for
1727 * adding, prepending and removing string.
1728 */
1729 else if (varp == (char_u *)&p_bs
1730 && VIM_ISDIGIT(**(char_u **)varp))
1731 {
1732 i = getdigits((char_u **)varp);
1733 switch (i)
1734 {
1735 case 0:
1736 *(char_u **)varp = empty_option;
1737 break;
1738 case 1:
1739 *(char_u **)varp = vim_strsave(
1740 (char_u *)"indent,eol");
1741 break;
1742 case 2:
1743 *(char_u **)varp = vim_strsave(
1744 (char_u *)"indent,eol,start");
1745 break;
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001746 case 3:
1747 *(char_u **)varp = vim_strsave(
1748 (char_u *)"indent,eol,nostop");
1749 break;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001750 }
1751 vim_free(oldval);
Bram Moolenaaredbc0d42017-08-20 16:11:51 +02001752 if (origval == oldval)
1753 origval = *(char_u **)varp;
Bram Moolenaard7c96872019-06-15 17:12:48 +02001754 if (origval_l == oldval)
1755 origval_l = *(char_u **)varp;
1756 if (origval_g == oldval)
1757 origval_g = *(char_u **)varp;
Bram Moolenaar4e5ccfa2011-11-30 11:15:47 +01001758 oldval = *(char_u **)varp;
1759 }
1760 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 * Convert 'whichwrap' number to string, for
1762 * backwards compatibility with Vim 3.0.
1763 * Misuse errbuf[] for the resulting string.
1764 */
1765 else if (varp == (char_u *)&p_ww
1766 && VIM_ISDIGIT(*arg))
1767 {
1768 *errbuf = NUL;
1769 i = getdigits(&arg);
1770 if (i & 1)
1771 STRCAT(errbuf, "b,");
1772 if (i & 2)
1773 STRCAT(errbuf, "s,");
1774 if (i & 4)
1775 STRCAT(errbuf, "h,l,");
1776 if (i & 8)
1777 STRCAT(errbuf, "<,>,");
1778 if (i & 16)
1779 STRCAT(errbuf, "[,],");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001780 if (*errbuf != NUL) // remove trailing ,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 errbuf[STRLEN(errbuf) - 1] = NUL;
1782 save_arg = arg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001783 arg = (char_u *)errbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785 /*
1786 * Remove '>' before 'dir' and 'bdir', for
1787 * backwards compatibility with version 3.0
1788 */
1789 else if ( *arg == '>'
1790 && (varp == (char_u *)&p_dir
1791 || varp == (char_u *)&p_bdir))
1792 {
1793 ++arg;
1794 }
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 /*
1797 * Copy the new string into allocated memory.
1798 * Can't use set_string_option_direct(), because
1799 * we need to remove the backslashes.
1800 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001801 // get a bit too much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 newlen = (unsigned)STRLEN(arg) + 1;
1803 if (adding || prepending || removing)
1804 newlen += (unsigned)STRLEN(origval) + 1;
1805 newval = alloc(newlen);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001806 if (newval == NULL) // out of mem, don't change
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 break;
1808 s = newval;
1809
1810 /*
1811 * Copy the string, skip over escaped chars.
1812 * For MS-DOS and WIN32 backslashes before normal
1813 * file name characters are not removed, and keep
1814 * backslash at start, for "\\machine\path", but
1815 * do remove it for "\\\\machine\\path".
1816 * The reverse is found in ExpandOldSetting().
1817 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001818 while (*arg && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
1820 if (*arg == '\\' && arg[1] != NUL
1821#ifdef BACKSLASH_IN_FILENAME
1822 && !((flags & P_EXPAND)
1823 && vim_isfilec(arg[1])
Bram Moolenaar994b89d2020-08-07 19:12:41 +02001824 && !VIM_ISWHITE(arg[1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 && (arg[1] != '\\'
1826 || (s == newval
1827 && arg[2] != '\\')))
1828#endif
1829 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 ++arg; // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (has_mbyte
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001832 && (i = (*mb_ptr2len)(arg)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001834 // copy multibyte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 mch_memmove(s, arg, (size_t)i);
1836 arg += i;
1837 s += i;
1838 }
1839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 *s++ = *arg++;
1841 }
1842 *s = NUL;
1843
1844 /*
1845 * Expand environment variables and ~.
1846 * Don't do it when adding without inserting a
1847 * comma.
1848 */
1849 if (!(adding || prepending || removing)
1850 || (flags & P_COMMA))
1851 {
1852 s = option_expand(opt_idx, newval);
1853 if (s != NULL)
1854 {
1855 vim_free(newval);
1856 newlen = (unsigned)STRLEN(s) + 1;
1857 if (adding || prepending || removing)
1858 newlen += (unsigned)STRLEN(origval) + 1;
1859 newval = alloc(newlen);
1860 if (newval == NULL)
1861 break;
1862 STRCPY(newval, s);
1863 }
1864 }
1865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001866 // locate newval[] in origval[] when removing it
1867 // and when adding to avoid duplicates
1868 i = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (removing || (flags & P_NODUP))
1870 {
1871 i = (int)STRLEN(newval);
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001872 s = find_dup_item(origval, newval, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001874 // do not add if already there
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001875 if ((adding || prepending) && s != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
1877 prepending = FALSE;
1878 adding = FALSE;
1879 STRCPY(newval, origval);
1880 }
Bram Moolenaarb00ef052020-09-12 14:53:53 +02001881
1882 // if no duplicate, move pointer to end of
1883 // original value
1884 if (s == NULL)
1885 s = origval + (int)STRLEN(origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 // concatenate the two strings; add a ',' if
1889 // needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (adding || prepending)
1891 {
1892 comma = ((flags & P_COMMA) && *origval != NUL
1893 && *newval != NUL);
1894 if (adding)
1895 {
1896 i = (int)STRLEN(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001897 // strip a trailing comma, would get 2
Bram Moolenaar17467472015-11-10 17:50:24 +01001898 if (comma && i > 1
1899 && (flags & P_ONECOMMA) == P_ONECOMMA
1900 && origval[i - 1] == ','
1901 && origval[i - 2] != '\\')
Bram Moolenaara7b7b1c2015-06-19 14:06:43 +02001902 i--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 mch_memmove(newval + i + comma, newval,
1904 STRLEN(newval) + 1);
1905 mch_memmove(newval, origval, (size_t)i);
1906 }
1907 else
1908 {
1909 i = (int)STRLEN(newval);
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001910 STRMOVE(newval + i + comma, origval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 if (comma)
1913 newval[i] = ',';
1914 }
1915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001916 // Remove newval[] from origval[]. (Note: "i" has
1917 // been set above and is used here).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (removing)
1919 {
1920 STRCPY(newval, origval);
1921 if (*s)
1922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001923 // may need to remove a comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (flags & P_COMMA)
1925 {
1926 if (s == origval)
1927 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001928 // include comma after string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (s[i] == ',')
1930 ++i;
1931 }
1932 else
1933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001934 // include comma before string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 --s;
1936 ++i;
1937 }
1938 }
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001939 STRMOVE(newval + (s - origval), s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941 }
1942
1943 if (flags & P_FLAGLIST)
1944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 // Remove flags that appear twice.
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001946 for (s = newval; *s;)
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001947 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001948 // if options have P_FLAGLIST and
1949 // P_ONECOMMA such as 'whichwrap'
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001950 if (flags & P_ONECOMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001952 if (*s != ',' && *(s + 1) == ','
1953 && vim_strchr(s + 2, *s) != NULL)
1954 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001955 // Remove the duplicated value and
1956 // the next comma.
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001957 STRMOVE(s, s + 2);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001958 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001961 else
1962 {
1963 if ((!(flags & P_COMMA) || *s != ',')
1964 && vim_strchr(s + 1, *s) != NULL)
1965 {
1966 STRMOVE(s, s + 1);
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001967 continue;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001968 }
1969 }
Bram Moolenaaraaaf57d2017-02-05 14:13:20 +01001970 ++s;
Bram Moolenaarc8ce6152016-08-07 13:48:20 +02001971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
1973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001974 if (save_arg != NULL) // number for 'whichwrap'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 arg = save_arg;
1976 new_value_alloced = TRUE;
1977 }
1978
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001979 /*
1980 * Set the new value.
1981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 *(char_u **)(varp) = newval;
1983
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001984#if defined(FEAT_EVAL)
Bram Moolenaar5cbb8db2015-07-17 23:08:29 +02001985 if (!starting
1986# ifdef FEAT_CRYPT
1987 && options[opt_idx].indir != PV_KEY
1988# endif
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001989 && origval != NULL && newval != NULL)
1990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001991 // origval may be freed by
1992 // did_set_string_option(), make a copy.
Bram Moolenaar53744302015-07-17 17:38:22 +02001993 saved_origval = vim_strsave(origval);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001994 // newval (and varp) may become invalid if the
1995 // buffer is closed by autocommands.
Bram Moolenaar182a17b2017-06-25 20:57:18 +02001996 saved_newval = vim_strsave(newval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02001997 if (origval_l != NULL)
1998 saved_origval_l = vim_strsave(origval_l);
1999 if (origval_g != NULL)
2000 saved_origval_g = vim_strsave(origval_g);
Bram Moolenaar182a17b2017-06-25 20:57:18 +02002001 }
Bram Moolenaar53744302015-07-17 17:38:22 +02002002#endif
2003
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002004 {
2005 long_u *p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002006 int secure_saved = secure;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002007
2008 // When an option is set in the sandbox, from a
2009 // modeline or in secure mode, then deal with side
2010 // effects in secure mode. Also when the value was
2011 // set with the P_INSECURE flag and is not
2012 // completely replaced.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002013 if ((opt_flags & OPT_MODELINE)
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002014#ifdef HAVE_SANDBOX
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002015 || sandbox != 0
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002016#endif
Bram Moolenaar82b033e2019-03-24 14:02:04 +01002017 || (!value_is_replaced && (*p & P_INSECURE)))
2018 secure = 1;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002019
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002020 // Handle side effects, and set the global value
2021 // for ":set" on local options. Note: when setting
2022 // 'syntax' or 'filetype' autocommands may be
2023 // triggered that can cause havoc.
2024 errmsg = did_set_string_option(
2025 opt_idx, (char_u **)varp,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002026 new_value_alloced, oldval, errbuf,
2027 opt_flags, &value_checked);
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002028
Bram Moolenaar48f377a2018-12-21 13:03:28 +01002029 secure = secure_saved;
Bram Moolenaar247bb7e2018-11-20 14:27:07 +01002030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002032#if defined(FEAT_EVAL)
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002033 if (errmsg == NULL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002034 trigger_optionsset_string(
2035 opt_idx, opt_flags, saved_origval,
2036 saved_origval_l, saved_origval_g,
2037 saved_newval);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002038 vim_free(saved_origval);
Bram Moolenaard7c96872019-06-15 17:12:48 +02002039 vim_free(saved_origval_l);
2040 vim_free(saved_origval_g);
Bram Moolenaar8efa0262017-08-20 15:47:20 +02002041 vim_free(saved_newval);
2042#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 // If error detected, print the error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (errmsg != NULL)
2045 goto skip;
2046 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002047 else // key code option
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
2049 char_u *p;
2050
2051 if (nextchar == '&')
2052 {
2053 if (add_termcap_entry(key_name, TRUE) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 errmsg = N_("E522: Not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 else
2057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002058 ++arg; // jump to after the '=' or ':'
Bram Moolenaar1c465442017-03-12 20:10:05 +01002059 for (p = arg; *p && !VIM_ISWHITE(*p); ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 if (*p == '\\' && p[1] != NUL)
2061 ++p;
2062 nextchar = *p;
2063 *p = NUL;
2064 add_termcode(key_name, arg, FALSE);
2065 *p = nextchar;
2066 }
2067 if (full_screen)
2068 ttest(FALSE);
2069 redraw_all_later(CLEAR);
2070 }
2071 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (opt_idx >= 0)
Bram Moolenaar916a8182018-11-25 02:18:29 +01002074 did_set_option(
2075 opt_idx, opt_flags, value_is_replaced, value_checked);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
2077
2078skip:
2079 /*
2080 * Advance to next argument.
2081 * - skip until a blank found, taking care of backslashes
2082 * - skip blanks
2083 * - skip one "=val" argument (for hidden options ":set gfn =xx")
2084 */
2085 for (i = 0; i < 2 ; ++i)
2086 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002087 while (*arg != NUL && !VIM_ISWHITE(*arg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (*arg++ == '\\' && *arg != NUL)
2089 ++arg;
2090 arg = skipwhite(arg);
2091 if (*arg != '=')
2092 break;
2093 }
2094 }
2095
2096 if (errmsg != NULL)
2097 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002098 vim_strncpy(IObuff, (char_u *)_(errmsg), IOSIZE - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002099 i = (int)STRLEN(IObuff) + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 if (i + (arg - startarg) < IOSIZE)
2101 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002102 // append the argument with the error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 STRCAT(IObuff, ": ");
2104 mch_memmove(IObuff + i, startarg, (arg - startarg));
2105 IObuff[i + (arg - startarg)] = NUL;
2106 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002107 // make sure all characters are printable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 trans_characters(IObuff, IOSIZE);
2109
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 ++no_wait_return; // wait_return done later
2111 emsg((char *)IObuff); // show error highlighted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 --no_wait_return;
2113
2114 return FAIL;
2115 }
2116
2117 arg = skipwhite(arg);
2118 }
2119
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002120theend:
2121 if (silent_mode && did_show)
2122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002123 // After displaying option values in silent mode.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002124 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002125 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002126 msg_putchar('\n');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002127 cursor_on(); // msg_start() switches it off
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002128 out_flush();
2129 silent_mode = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002130 info_message = FALSE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002131 }
2132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 return OK;
2134}
2135
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002136/*
2137 * Call this when an option has been given a new value through a user command.
2138 * Sets the P_WAS_SET flag and takes care of the P_INSECURE flag.
2139 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002140 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002141did_set_option(
2142 int opt_idx,
Bram Moolenaar916a8182018-11-25 02:18:29 +01002143 int opt_flags, // possibly with OPT_MODELINE
2144 int new_value, // value was replaced completely
2145 int value_checked) // value was checked to be safe, no need to set the
2146 // P_INSECURE flag.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002147{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002148 long_u *p;
2149
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002150 options[opt_idx].flags |= P_WAS_SET;
2151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002152 // When an option is set in the sandbox, from a modeline or in secure mode
2153 // set the P_INSECURE flag. Otherwise, if a new value is stored reset the
2154 // flag.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002155 p = insecure_flag(opt_idx, opt_flags);
Bram Moolenaar916a8182018-11-25 02:18:29 +01002156 if (!value_checked && (secure
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002157#ifdef HAVE_SANDBOX
2158 || sandbox != 0
2159#endif
Bram Moolenaar916a8182018-11-25 02:18:29 +01002160 || (opt_flags & OPT_MODELINE)))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002161 *p = *p | P_INSECURE;
2162 else if (new_value)
2163 *p = *p & ~P_INSECURE;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002164}
2165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166/*
2167 * Convert a key name or string into a key value.
2168 * Used for 'wildchar' and 'cedit' options.
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002169 * When "multi_byte" is TRUE allow for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 */
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002171 int
2172string_to_key(char_u *arg, int multi_byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 if (*arg == '<')
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02002175 return find_key_option(arg + 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (*arg == '^')
2177 return Ctrl_chr(arg[1]);
Bram Moolenaardbe948d2017-07-23 22:50:51 +02002178 if (multi_byte)
2179 return PTR2CHAR(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 return *arg;
2181}
2182
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#ifdef FEAT_TITLE
2184/*
2185 * When changing 'title', 'titlestring', 'icon' or 'iconstring', call
2186 * maketitle() to create and display it.
2187 * When switching the title or icon off, call mch_restore_title() to get
2188 * the old value back.
2189 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002190 void
Bram Moolenaar84a93082018-06-16 22:58:15 +02002191did_set_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 if (starting != NO_SCREEN
2194#ifdef FEAT_GUI
2195 && !gui.starting
2196#endif
2197 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 maketitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199}
2200#endif
2201
2202/*
2203 * set_options_bin - called when 'bin' changes value.
2204 */
2205 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002206set_options_bin(
2207 int oldval,
2208 int newval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211 /*
2212 * The option values that are changed when 'bin' changes are
2213 * copied when 'bin is set and restored when 'bin' is reset.
2214 */
2215 if (newval)
2216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 if (!oldval) // switched on
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 if (!(opt_flags & OPT_GLOBAL))
2220 {
2221 curbuf->b_p_tw_nobin = curbuf->b_p_tw;
2222 curbuf->b_p_wm_nobin = curbuf->b_p_wm;
2223 curbuf->b_p_ml_nobin = curbuf->b_p_ml;
2224 curbuf->b_p_et_nobin = curbuf->b_p_et;
2225 }
2226 if (!(opt_flags & OPT_LOCAL))
2227 {
2228 p_tw_nobin = p_tw;
2229 p_wm_nobin = p_wm;
2230 p_ml_nobin = p_ml;
2231 p_et_nobin = p_et;
2232 }
2233 }
2234
2235 if (!(opt_flags & OPT_GLOBAL))
2236 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 curbuf->b_p_tw = 0; // no automatic line wrap
2238 curbuf->b_p_wm = 0; // no automatic line wrap
2239 curbuf->b_p_ml = 0; // no modelines
2240 curbuf->b_p_et = 0; // no expandtab
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 }
2242 if (!(opt_flags & OPT_LOCAL))
2243 {
2244 p_tw = 0;
2245 p_wm = 0;
2246 p_ml = FALSE;
2247 p_et = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002248 p_bin = TRUE; // needed when called for the "-b" argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 else if (oldval) // switched off
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
2253 if (!(opt_flags & OPT_GLOBAL))
2254 {
2255 curbuf->b_p_tw = curbuf->b_p_tw_nobin;
2256 curbuf->b_p_wm = curbuf->b_p_wm_nobin;
2257 curbuf->b_p_ml = curbuf->b_p_ml_nobin;
2258 curbuf->b_p_et = curbuf->b_p_et_nobin;
2259 }
2260 if (!(opt_flags & OPT_LOCAL))
2261 {
2262 p_tw = p_tw_nobin;
2263 p_wm = p_wm_nobin;
2264 p_ml = p_ml_nobin;
2265 p_et = p_et_nobin;
2266 }
2267 }
2268}
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270/*
2271 * Expand environment variables for some string options.
2272 * These string options cannot be indirect!
2273 * If "val" is NULL expand the current value of the option.
2274 * Return pointer to NameBuff, or NULL when not expanded.
2275 */
2276 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002277option_expand(int opt_idx, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002279 // if option doesn't need expansion nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (!(options[opt_idx].flags & P_EXPAND) || options[opt_idx].var == NULL)
2281 return NULL;
2282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002283 // If val is longer than MAXPATHL no meaningful expansion can be done,
2284 // expand_env() would truncate the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (val != NULL && STRLEN(val) > MAXPATHL)
2286 return NULL;
2287
2288 if (val == NULL)
2289 val = *(char_u **)options[opt_idx].var;
2290
2291 /*
2292 * Expanding this with NameBuff, expand_env() must not be passed IObuff.
2293 * Escape spaces when expanding 'tags', they are used to separate file
2294 * names.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002295 * For 'spellsuggest' expand after "file:".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 */
2297 expand_env_esc(val, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00002298 (char_u **)options[opt_idx].var == &p_tags, FALSE,
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002299#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002300 (char_u **)options[opt_idx].var == &p_sps ? (char_u *)"file:" :
2301#endif
2302 NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 if (STRCMP(NameBuff, val) == 0) // they are the same
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 return NULL;
2305
2306 return NameBuff;
2307}
2308
2309/*
2310 * After setting various option values: recompute variables that depend on
2311 * option values.
2312 */
2313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002314didset_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002316 // initialize the table for 'iskeyword' et.al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 (void)init_chartab();
2318
Bram Moolenaardac13472019-09-16 21:06:21 +02002319 didset_string_options();
2320
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002321#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00002322 (void)spell_check_msm();
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00002323 (void)spell_check_sps();
Bram Moolenaar860cae12010-06-05 23:22:07 +02002324 (void)compile_cap_prog(curwin->w_s);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002325 (void)did_set_spell_option(TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#ifdef FEAT_CMDWIN
Bram Moolenaar010ee962019-09-25 20:37:36 +02002328 // set cedit_key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 (void)check_cedit();
2330#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02002331#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002332 // initialize the table for 'breakat'.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002333 fill_breakat_flags();
2334#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02002335 after_copy_winopt(curwin);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002336}
2337
2338/*
2339 * More side effects of setting options.
2340 */
2341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002342didset_options2(void)
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002343{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002344 // Initialize the highlight_attr[] table.
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002345 (void)highlight_changed();
2346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002347 // Parse default for 'wildmode'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002348 check_opt_wim();
2349
Bram Moolenaareed9d462021-02-15 20:38:25 +01002350 // Parse default for 'listchars'.
2351 (void)set_chars_option(curwin, &curwin->w_p_lcs);
2352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 // Parse default for 'fillchars'.
Bram Moolenaareed9d462021-02-15 20:38:25 +01002354 (void)set_chars_option(curwin, &p_fcs);
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002355
2356#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002357 // Parse default for 'clipboard'
Bram Moolenaare68c25c2015-08-25 15:39:55 +02002358 (void)check_clipboard_option();
2359#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002360#ifdef FEAT_VARTABS
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002361 vim_free(curbuf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002362 tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002363 vim_free(curbuf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002364 tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array);
2365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366}
2367
2368/*
2369 * Check for string options that are NULL (normally only termcap options).
2370 */
2371 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002372check_options(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 int opt_idx;
2375
2376 for (opt_idx = 0; options[opt_idx].fullname != NULL; opt_idx++)
2377 if ((options[opt_idx].flags & P_STRING) && options[opt_idx].var != NULL)
2378 check_string_option((char_u **)get_varp(&(options[opt_idx])));
2379}
2380
2381/*
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002382 * Return the option index found by a pointer into term_strings[].
2383 * Return -1 if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 */
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002385 int
2386get_term_opt_idx(char_u **p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002388 int opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 for (opt_idx = 1; options[opt_idx].fullname != NULL; opt_idx++)
2391 if (options[opt_idx].var == (char_u *)p)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002392 return opt_idx;
2393 return -1; // cannot happen: didn't find it!
2394}
2395
2396/*
2397 * Mark a terminal option as allocated, found by a pointer into term_strings[].
2398 * Return the option index or -1 if not found.
2399 */
2400 int
2401set_term_option_alloced(char_u **p)
2402{
2403 int opt_idx = get_term_opt_idx(p);
2404
2405 if (opt_idx >= 0)
2406 options[opt_idx].flags |= P_ALLOCED;
2407 return opt_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408}
2409
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002410#if defined(FEAT_EVAL) || defined(PROTO)
2411/*
2412 * Return TRUE when option "opt" was set from a modeline or in secure mode.
2413 * Return FALSE when it wasn't.
2414 * Return -1 for an unknown option.
2415 */
2416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417was_set_insecurely(char_u *opt, int opt_flags)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002418{
2419 int idx = findoption(opt);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002420 long_u *flagp;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002421
2422 if (idx >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002423 {
2424 flagp = insecure_flag(idx, opt_flags);
2425 return (*flagp & P_INSECURE) != 0;
2426 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002427 internal_error("was_set_insecurely()");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002428 return -1;
2429}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002430
2431/*
2432 * Get a pointer to the flags used for the P_INSECURE flag of option
2433 * "opt_idx". For some local options a local flags field is used.
Bram Moolenaard5e8c922021-02-02 21:10:01 +01002434 * NOTE: Caller must make sure that "curwin" is set to the window from which
2435 * the option is used.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002436 */
2437 static long_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002438insecure_flag(int opt_idx, int opt_flags)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002439{
2440 if (opt_flags & OPT_LOCAL)
2441 switch ((int)options[opt_idx].indir)
2442 {
2443#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002444 case PV_STL: return &curwin->w_p_stl_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002445#endif
2446#ifdef FEAT_EVAL
Bram Moolenaar2e978902006-05-13 12:37:50 +00002447# ifdef FEAT_FOLDING
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002448 case PV_FDE: return &curwin->w_p_fde_flags;
2449 case PV_FDT: return &curwin->w_p_fdt_flags;
Bram Moolenaar2e978902006-05-13 12:37:50 +00002450# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002451# ifdef FEAT_BEVAL
2452 case PV_BEXPR: return &curbuf->b_p_bexpr_flags;
2453# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002454# if defined(FEAT_CINDENT)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002455 case PV_INDE: return &curbuf->b_p_inde_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002456# endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002457 case PV_FEX: return &curbuf->b_p_fex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002458# ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002459 case PV_INEX: return &curbuf->b_p_inex_flags;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002460# endif
2461#endif
2462 }
2463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002464 // Nothing special, return global flags field.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002465 return &options[opt_idx].flags;
2466}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002467#endif
2468
Bram Moolenaardac13472019-09-16 21:06:21 +02002469#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002470/*
2471 * Redraw the window title and/or tab page text later.
2472 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002473void redraw_titles(void)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002474{
2475 need_maketitle = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002476 redraw_tabline = TRUE;
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002477}
2478#endif
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480/*
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002481 * Return TRUE if "val" is a valid name: only consists of alphanumeric ASCII
2482 * characters or characters in "allowed".
2483 */
Bram Moolenaare677df82019-09-02 22:31:11 +02002484 int
Bram Moolenaar8f130ed2019-04-10 22:15:19 +02002485valid_name(char_u *val, char *allowed)
2486{
2487 char_u *s;
2488
2489 for (s = val; *s != NUL; ++s)
2490 if (!ASCII_ISALNUM(*s) && vim_strchr((char_u *)allowed, *s) == NULL)
2491 return FALSE;
2492 return TRUE;
2493}
2494
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002495#if defined(FEAT_EVAL) || defined(PROTO)
2496/*
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002497 * Set the script_ctx for an option, taking care of setting the buffer- or
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002498 * window-local value.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002499 */
Bram Moolenaardac13472019-09-16 21:06:21 +02002500 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002501set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002502{
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002503 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
2504 int indir = (int)options[opt_idx].indir;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002505 sctx_T new_script_ctx = script_ctx;
2506
Bram Moolenaar51258742020-05-03 17:19:33 +02002507 // Modeline already has the line number set.
2508 if (!(opt_flags & OPT_MODELINE))
2509 new_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002510
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002511 // Remember where the option was set. For local options need to do that
2512 // in the buffer or window structure.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002513 if (both || (opt_flags & OPT_GLOBAL) || (indir & (PV_BUF|PV_WIN)) == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002514 options[opt_idx].script_ctx = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002515 if (both || (opt_flags & OPT_LOCAL))
2516 {
2517 if (indir & PV_BUF)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002518 curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002519 else if (indir & PV_WIN)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002520 curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002521 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002522}
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02002523
2524/*
2525 * Set the script_ctx for a termcap option.
2526 * "name" must be the two character code, e.g. "RV".
2527 * When "name" is NULL use "opt_idx".
2528 */
2529 void
2530set_term_option_sctx_idx(char *name, int opt_idx)
2531{
2532 char_u buf[5];
2533 int idx;
2534
2535 if (name == NULL)
2536 idx = opt_idx;
2537 else
2538 {
2539 buf[0] = 't';
2540 buf[1] = '_';
2541 buf[2] = name[0];
2542 buf[3] = name[1];
2543 buf[4] = 0;
2544 idx = findoption(buf);
2545 }
2546 if (idx >= 0)
2547 set_option_sctx_idx(idx, OPT_GLOBAL, current_sctx);
2548}
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002549#endif
2550
Bram Moolenaarf4140482020-02-15 23:06:45 +01002551#if defined(FEAT_EVAL)
2552/*
2553 * Apply the OptionSet autocommand.
2554 */
2555 static void
2556apply_optionset_autocmd(
2557 int opt_idx,
2558 long opt_flags,
2559 long oldval,
2560 long oldval_g,
2561 long newval,
2562 char *errmsg)
2563{
2564 char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2565
2566 // Don't do this while starting up, failure or recursively.
2567 if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2568 return;
2569
2570 vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2571 vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2572 oldval_g);
2573 vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2574 vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2575 (opt_flags & OPT_LOCAL) ? "local" : "global");
2576 set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2577 set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2578 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2579 if (opt_flags & OPT_LOCAL)
2580 {
2581 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2582 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2583 }
2584 if (opt_flags & OPT_GLOBAL)
2585 {
2586 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2587 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2588 }
2589 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2590 {
2591 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2592 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2593 set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2594 }
2595 if (opt_flags & OPT_MODELINE)
2596 {
2597 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2598 set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2599 }
2600 apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2601 NULL, FALSE, NULL);
2602 reset_v_option_vars();
2603}
2604#endif
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606/*
2607 * Set the value of a boolean option, and take care of side effects.
2608 * Returns NULL for success, or an error message for an error.
2609 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002610 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002611set_bool_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002612 int opt_idx, // index in options[] table
2613 char_u *varp, // pointer to the option variable
2614 int value, // new value
2615 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 int old_value = *(int *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002618#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002619 int old_global_value = 0;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002622 // Disallow changing some options from secure mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if ((secure
2624#ifdef HAVE_SANDBOX
2625 || sandbox != 0
2626#endif
2627 ) && (options[opt_idx].flags & P_SECURE))
2628 return e_secure;
2629
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002630#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02002631 // Save the global value before changing anything. This is needed as for
2632 // a global-only option setting the "local value" in fact sets the global
2633 // value (since there is only one value).
2634 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2635 old_global_value = *(int *)get_varp_scope(&(options[opt_idx]),
2636 OPT_GLOBAL);
Bram Moolenaar983f2f12019-06-16 16:41:41 +02002637#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02002638
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 *(int *)varp = value; // set the new value
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002641 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002642 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643#endif
2644
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002645#ifdef FEAT_GUI
2646 need_mouse_correct = TRUE;
2647#endif
2648
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002649 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2651 *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value;
2652
2653 /*
2654 * Handle side effects of changing a bool option.
2655 */
2656
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002657 // 'compatible'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if ((int *)varp == &p_cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 compatible_set();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaar920694c2016-08-21 17:45:02 +02002661#ifdef FEAT_LANGMAP
2662 if ((int *)varp == &p_lrm)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002663 // 'langremap' -> !'langnoremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002664 p_lnr = !p_lrm;
2665 else if ((int *)varp == &p_lnr)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // 'langnoremap' -> !'langremap'
Bram Moolenaar920694c2016-08-21 17:45:02 +02002667 p_lrm = !p_lnr;
2668#endif
2669
Bram Moolenaar8c63e0e2018-09-25 22:17:54 +02002670#ifdef FEAT_SYN_HL
2671 else if ((int *)varp == &curwin->w_p_cul && !value && old_value)
2672 reset_cursorline();
2673#endif
2674
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002675#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002676 // 'undofile'
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002677 else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf)
2678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002679 // Only take action when the option was set. When reset we do not
2680 // delete the undo file, the option may be set again without making
2681 // any changes in between.
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002682 if (curbuf->b_p_udf || p_udf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002683 {
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002684 char_u hash[UNDO_HASH_SIZE];
2685 buf_T *save_curbuf = curbuf;
2686
Bram Moolenaar29323592016-07-24 22:04:11 +02002687 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // When 'undofile' is set globally: for every buffer, otherwise
2690 // only for the current buffer: Try to read in the undofile,
2691 // if one exists, the buffer wasn't changed and the buffer was
2692 // loaded
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002693 if ((curbuf == save_curbuf
2694 || (opt_flags & OPT_GLOBAL) || opt_flags == 0)
2695 && !curbufIsChanged() && curbuf->b_ml.ml_mfp != NULL)
2696 {
2697 u_compute_hash(hash);
2698 u_read_undo(NULL, hash, curbuf->b_fname);
2699 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002700 }
Bram Moolenaare8d8fd22012-10-21 03:46:05 +02002701 curbuf = save_curbuf;
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002702 }
Bram Moolenaar374d32d2012-01-04 19:34:37 +01002703 }
2704#endif
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 else if ((int *)varp == &curbuf->b_p_ro)
2707 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002708 // when 'readonly' is reset globally, also reset readonlymode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0)
2710 readonlymode = FALSE;
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002712 // when 'readonly' is set may give W10 again
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002713 if (curbuf->b_p_ro)
2714 curbuf->b_did_warn = FALSE;
2715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002717 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718#endif
2719 }
2720
Bram Moolenaar9c449722010-07-20 18:44:27 +02002721#ifdef FEAT_GUI
2722 else if ((int *)varp == &p_mh)
2723 {
2724 if (!p_mh)
2725 gui_mch_mousehide(FALSE);
2726 }
2727#endif
2728
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002729 // when 'modifiable' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else if ((int *)varp == &curbuf->b_p_ma)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002731 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02002732# ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // Cannot set 'modifiable' when in Terminal mode.
Bram Moolenaard7db27b2018-03-07 23:02:33 +01002734 if (curbuf->b_p_ma && (term_in_normal_mode() || (bt_terminal(curbuf)
2735 && curbuf->b_term != NULL && !term_is_finished(curbuf))))
Bram Moolenaar423802d2017-07-30 16:52:24 +02002736 {
2737 curbuf->b_p_ma = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002738 return N_("E946: Cannot make a terminal with running job modifiable");
Bram Moolenaar423802d2017-07-30 16:52:24 +02002739 }
2740# endif
2741# ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002742 redraw_titles();
Bram Moolenaar423802d2017-07-30 16:52:24 +02002743# endif
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002744 }
Bram Moolenaar423802d2017-07-30 16:52:24 +02002745#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // when 'endofline' is changed, redraw the window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 else if ((int *)varp == &curbuf->b_p_eol)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002748 {
2749 redraw_titles();
2750 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 // when 'fixeol' is changed, redraw the window title
Bram Moolenaar34d72d42015-07-17 14:18:08 +02002752 else if ((int *)varp == &curbuf->b_p_fixeol)
2753 {
2754 redraw_titles();
2755 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // when 'bomb' is changed, redraw the window title and tab page text
Bram Moolenaar83eb8852007-08-12 13:51:26 +00002757 else if ((int *)varp == &curbuf->b_p_bomb)
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002758 {
2759 redraw_titles();
2760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761#endif
2762
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 // when 'bin' is set also set some other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else if ((int *)varp == &curbuf->b_p_bin)
2765 {
2766 set_options_bin(old_value, curbuf->b_p_bin, opt_flags);
2767#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002768 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769#endif
2770 }
2771
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // when 'buflisted' changes, trigger autocommands
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl)
2774 {
2775 apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE,
2776 NULL, NULL, TRUE, curbuf);
2777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // when 'swf' is set, create swapfile, when reset remove swapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else if ((int *)varp == &curbuf->b_p_swf)
2781 {
2782 if (curbuf->b_p_swf && p_uc)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 ml_open_file(curbuf); // create the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002785 // no need to reset curbuf->b_may_swap, ml_open_file() will check
2786 // buf->b_p_swf
2787 mf_close_file(curbuf, TRUE); // remove the swap file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // when 'terse' is set change 'shortmess'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else if ((int *)varp == &p_terse)
2792 {
2793 char_u *p;
2794
2795 p = vim_strchr(p_shm, SHM_SEARCH);
2796
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // insert 's' in p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (p_terse && p == NULL)
2799 {
2800 STRCPY(IObuff, p_shm);
2801 STRCAT(IObuff, "s");
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002802 set_string_option_direct((char_u *)"shm", -1, IObuff, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // remove 's' from p_shm
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 else if (!p_terse && p != NULL)
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002806 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002809 // when 'paste' is set or reset also change other options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else if ((int *)varp == &p_paste)
2811 {
2812 paste_option_changed();
2813 }
2814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 // when 'insertmode' is set from an autocommand need to do work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 else if ((int *)varp == &p_im)
2817 {
2818 if (p_im)
2819 {
2820 if ((State & INSERT) == 0)
2821 need_start_insertmode = TRUE;
2822 stop_insert_mode = FALSE;
2823 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // only reset if it was set previously
Bram Moolenaar00672e12016-06-26 18:38:13 +02002825 else if (old_value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
2827 need_start_insertmode = FALSE;
2828 stop_insert_mode = TRUE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002829 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002830 clear_cmdline = TRUE; // remove "(insert)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 restart_edit = 0;
2832 }
2833 }
2834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 else if ((int *)varp == &p_ic && p_hls)
2837 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002838 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840
2841#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // when 'hlsearch' is set or reset: reset no_hlsearch
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 else if ((int *)varp == &p_hls)
2844 {
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002845 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847#endif
2848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // when 'scrollbind' is set: snapshot the current position to avoid a jump
2850 // at the end of normal_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 else if ((int *)varp == &curwin->w_p_scb)
2852 {
2853 if (curwin->w_p_scb)
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 do_check_scrollbind(FALSE);
Bram Moolenaar04c5c9e2013-07-09 13:44:59 +02002856 curwin->w_scbind_pos = curwin->w_topline;
2857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar4033c552017-09-16 20:54:51 +02002860#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // There can be only one window with 'previewwindow' set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 else if ((int *)varp == &curwin->w_p_pvw)
2863 {
2864 if (curwin->w_p_pvw)
2865 {
2866 win_T *win;
2867
Bram Moolenaar29323592016-07-24 22:04:11 +02002868 FOR_ALL_WINDOWS(win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if (win->w_p_pvw && win != curwin)
2870 {
2871 curwin->w_p_pvw = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002872 return N_("E590: A preview window already exists");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 }
2875 }
2876#endif
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // when 'textmode' is set or reset also change 'fileformat'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 else if ((int *)varp == &curbuf->b_p_tx)
2880 {
2881 set_fileformat(curbuf->b_p_tx ? EOL_DOS : EOL_UNIX, opt_flags);
2882 }
2883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // when 'textauto' is set or reset also change 'fileformats'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else if ((int *)varp == &p_ta)
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 set_string_option_direct((char_u *)"ffs", -1,
2888 p_ta ? (char_u *)DFLT_FFS_VIM : (char_u *)"",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002889 OPT_FREE | opt_flags, 0);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891
2892 /*
2893 * When 'lisp' option changes include/exclude '-' in
2894 * keyword characters.
2895 */
2896#ifdef FEAT_LISP
2897 else if (varp == (char_u *)&(curbuf->b_p_lisp))
2898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 (void)buf_init_chartab(curbuf, FALSE); // ignore errors
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
2901#endif
2902
2903#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // when 'title' changed, may need to change the title; same for 'icon'
Bram Moolenaar84a93082018-06-16 22:58:15 +02002905 else if ((int *)varp == &p_title || (int *)varp == &p_icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02002907 did_set_title();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909#endif
2910
2911 else if ((int *)varp == &curbuf->b_changed)
2912 {
2913 if (!value)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 save_file_ff(curbuf); // Buffer is unchanged
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#ifdef FEAT_TITLE
Bram Moolenaar1f26d2f2009-02-11 10:35:36 +00002916 redraw_titles();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 modified_was_set = value;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920
2921#ifdef BACKSLASH_IN_FILENAME
2922 else if ((int *)varp == &p_ssl)
2923 {
2924 if (p_ssl)
2925 {
2926 psepc = '/';
2927 psepcN = '\\';
2928 pseps[0] = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
2930 else
2931 {
2932 psepc = '\\';
2933 psepcN = '/';
2934 pseps[0] = '\\';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 // need to adjust the file name arguments and buffer names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 buflist_slash_adjust();
2939 alist_slash_adjust();
2940# ifdef FEAT_EVAL
2941 scriptnames_slash_adjust();
2942# endif
2943 }
2944#endif
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // If 'wrap' is set, set w_leftcol to zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 else if ((int *)varp == &curwin->w_p_wrap)
2948 {
2949 if (curwin->w_p_wrap)
2950 curwin->w_leftcol = 0;
2951 }
2952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 else if ((int *)varp == &p_ea)
2954 {
2955 if (p_ea && !old_value)
2956 win_equal(curwin, FALSE, 0);
2957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 else if ((int *)varp == &p_wiv)
2960 {
2961 /*
2962 * When 'weirdinvert' changed, set/reset 't_xs'.
2963 * Then set 'weirdinvert' according to value of 't_xs'.
2964 */
2965 if (p_wiv && !old_value)
2966 T_XS = (char_u *)"y";
2967 else if (!p_wiv && old_value)
2968 T_XS = empty_option;
2969 p_wiv = (*T_XS != NUL);
2970 }
2971
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002972#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else if ((int *)varp == &p_beval)
2974 {
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002975 if (!balloonEvalForTerm)
2976 {
2977 if (p_beval && !old_value)
2978 gui_mch_enable_beval_area(balloonEval);
2979 else if (!p_beval && old_value)
2980 gui_mch_disable_beval_area(balloonEval);
2981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002983#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002984#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002985 else if ((int *)varp == &p_bevalterm)
2986 {
2987 mch_bevalterm_changed();
2988 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
Bram Moolenaar8dff8182006-04-06 20:18:50 +00002991#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 else if ((int *)varp == &p_acd)
2993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 // Change directories when the 'acd' option is set now.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002995 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997#endif
2998
2999#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // 'diff'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 else if ((int *)varp == &curwin->w_p_diff)
3002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 // May add or remove the buffer from the list of diff buffers.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003004 diff_buf_adjust(curwin);
3005# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (foldmethodIsDiff(curwin))
3007 foldUpdateAll(curwin);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010#endif
3011
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003012#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003013 // 'imdisable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 else if ((int *)varp == &p_imdisable)
3015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003016 // Only de-activate it here, it will be enabled when changing mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (p_imdisable)
3018 im_set_active(FALSE);
Bram Moolenaar725a9622011-10-12 16:57:13 +02003019 else if (State & INSERT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // When the option is set from an autocommand, it may need to take
3021 // effect right away.
Bram Moolenaar725a9622011-10-12 16:57:13 +02003022 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024#endif
3025
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003026#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003027 // 'spell'
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003028 else if ((int *)varp == &curwin->w_p_spell)
3029 {
3030 if (curwin->w_p_spell)
3031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003032 char *errmsg = did_set_spelllang(curwin);
3033
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003034 if (errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003035 emsg(_(errmsg));
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003036 }
3037 }
3038#endif
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#ifdef FEAT_ARABIC
3041 if ((int *)varp == &curwin->w_p_arab)
3042 {
3043 if (curwin->w_p_arab)
3044 {
3045 /*
3046 * 'arabic' is set, handle various sub-settings.
3047 */
3048 if (!p_tbidi)
3049 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // set rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 if (!curwin->w_p_rl)
3052 {
3053 curwin->w_p_rl = TRUE;
3054 changed_window_setting();
3055 }
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // Enable Arabic shaping (major part of what Arabic requires)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 if (!p_arshape)
3059 {
3060 p_arshape = TRUE;
3061 redraw_later_clear();
3062 }
3063 }
3064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 // Arabic requires a utf-8 encoding, inform the user if its not
3066 // set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (STRCMP(p_enc, "utf-8") != 0)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003068 {
Bram Moolenaar496c5262009-03-18 14:42:00 +00003069 static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'");
3070
Bram Moolenaar8820b482017-03-16 17:23:31 +01003071 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003072 msg_attr(_(w_arabic), HL_ATTR(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003073#ifdef FEAT_EVAL
3074 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_arabic), -1);
3075#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003078 // set 'delcombine'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 p_deco = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // Force-set the necessary keymap for arabic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 set_option_value((char_u *)"keymap", 0L, (char_u *)"arabic",
3084 OPT_LOCAL);
3085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
3087 else
3088 {
3089 /*
3090 * 'arabic' is reset, handle various sub-settings.
3091 */
3092 if (!p_tbidi)
3093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // reset rightleft mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 if (curwin->w_p_rl)
3096 {
3097 curwin->w_p_rl = FALSE;
3098 changed_window_setting();
3099 }
3100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // 'arabicshape' isn't reset, it is a global option and
3102 // another window may still need it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003105 // 'delcombine' isn't reset, it is a global option and another
3106 // window may still want it "on".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108# ifdef FEAT_KEYMAP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003109 // Revert to the default keymap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 curbuf->b_p_iminsert = B_IMODE_NONE;
3111 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3112# endif
3113 }
Bram Moolenaar801f8b82009-07-29 13:42:05 +00003114 }
3115
Bram Moolenaar319bdbd2009-09-11 13:20:33 +00003116#endif
3117
Bram Moolenaar44826212019-08-22 21:23:20 +02003118#if defined(FEAT_SIGNS) && defined(FEAT_GUI)
3119 else if (((int *)varp == &curwin->w_p_nu
3120 || (int *)varp == &curwin->w_p_rnu)
3121 && gui.in_use
3122 && (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u')
3123 && curbuf->b_signlist != NULL)
3124 {
3125 // If the 'number' or 'relativenumber' options are modified and
3126 // 'signcolumn' is set to 'number', then clear the screen for a full
3127 // refresh. Otherwise the sign icons are not displayed properly in the
3128 // number column. If the 'number' option is set and only the
3129 // 'relativenumber' option is toggled, then don't refresh the screen
3130 // (optimization).
3131 if (!(curwin->w_p_nu && ((int *)varp == &curwin->w_p_rnu)))
3132 redraw_all_later(CLEAR);
3133 }
3134#endif
3135
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003136#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003137 // 'termguicolors'
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003138 else if ((int *)varp == &p_tgc)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003139 {
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003140# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003141 // Do not turn on 'tgc' when 24-bit colors are not supported.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003142 if (
3143# ifdef VIMDLL
3144 !gui.in_use && !gui.starting &&
3145# endif
3146 !has_vtp_working())
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003147 {
3148 p_tgc = 0;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003149 return N_("E954: 24-bit colors are not supported on this environment");
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003150 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003151 if (is_term_win32())
3152 swap_tcap();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003153# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003154# ifdef FEAT_GUI
3155 if (!gui.in_use && !gui.starting)
3156# endif
3157 highlight_gui_started();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003158# ifdef FEAT_VTP
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // reset t_Co
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003160 if (is_term_win32())
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003161 {
3162 control_console_color_rgb();
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003163 set_termname(T_NAME);
Bram Moolenaarb0eb14f2018-06-28 15:29:52 +02003164 init_highlight(TRUE, FALSE);
3165 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003166# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003167 }
3168#endif
3169
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 /*
3171 * End of handling side effects for bool options.
3172 */
3173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // after handling side effects, call autocommand
Bram Moolenaar53744302015-07-17 17:38:22 +02003175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 options[opt_idx].flags |= P_WAS_SET;
3177
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003178#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003179 apply_optionset_autocmd(opt_idx, opt_flags,
3180 (long)(old_value ? TRUE : FALSE),
3181 (long)(old_global_value ? TRUE : FALSE),
3182 (long)(value ? TRUE : FALSE), NULL);
Bram Moolenaar53744302015-07-17 17:38:22 +02003183#endif
3184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 comp_col(); // in case 'ruler' or 'showcmd' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003186 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003187 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003188 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003189
3190 if ((opt_flags & OPT_NO_REDRAW) == 0)
3191 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 return NULL;
3194}
3195
3196/*
3197 * Set the value of a number option, and take care of side effects.
3198 * Returns NULL for success, or an error message for an error.
3199 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003200 static char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003201set_num_option(
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003202 int opt_idx, // index in options[] table
3203 char_u *varp, // pointer to the option variable
3204 long value, // new value
3205 char *errbuf, // buffer for error messages
3206 size_t errbuflen, // length of "errbuf"
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003207 int opt_flags) // OPT_LOCAL, OPT_GLOBAL,
3208 // OPT_MODELINE, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003210 char *errmsg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 long old_value = *(long *)varp;
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003212#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003213 long old_global_value = 0; // only used when setting a local and
3214 // global option
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003215#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003216 long old_Rows = Rows; // remember old Rows
3217 long old_Columns = Columns; // remember old Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 long *pp = (long *)varp;
3219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003220 // Disallow changing some options from secure mode.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003221 if ((secure
3222#ifdef HAVE_SANDBOX
3223 || sandbox != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003225 ) && (options[opt_idx].flags & P_SECURE))
3226 return e_secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003228#if defined(FEAT_EVAL)
Bram Moolenaard7c96872019-06-15 17:12:48 +02003229 // Save the global value before changing anything. This is needed as for
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003230 // a global-only option setting the "local value" in fact sets the global
Bram Moolenaard7c96872019-06-15 17:12:48 +02003231 // value (since there is only one value).
3232 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaar983f2f12019-06-16 16:41:41 +02003233 old_global_value = *(long *)get_varp_scope(&(options[opt_idx]),
3234 OPT_GLOBAL);
3235#endif
Bram Moolenaard7c96872019-06-15 17:12:48 +02003236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 *pp = value;
3238#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003239 // Remember where the option was set.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003240 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003242#ifdef FEAT_GUI
3243 need_mouse_correct = TRUE;
3244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
Bram Moolenaar14f24742012-08-08 18:01:05 +02003246 if (curbuf->b_p_sw < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 errmsg = e_positive;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003249#ifdef FEAT_VARTABS
3250 // Use the first 'vartabstop' value, or 'tabstop' if vts isn't in use.
3251 curbuf->b_p_sw = tabstop_count(curbuf->b_p_vts_array) > 0
3252 ? tabstop_first(curbuf->b_p_vts_array)
3253 : curbuf->b_p_ts;
3254#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 curbuf->b_p_sw = curbuf->b_p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258
3259 /*
3260 * Number options that need some action when changed
3261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (pp == &p_wh || pp == &p_hh)
3263 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003264 // 'winheight' and 'helpheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (p_wh < 1)
3266 {
3267 errmsg = e_positive;
3268 p_wh = 1;
3269 }
3270 if (p_wmh > p_wh)
3271 {
3272 errmsg = e_winheight;
3273 p_wh = p_wmh;
3274 }
3275 if (p_hh < 0)
3276 {
3277 errmsg = e_positive;
3278 p_hh = 0;
3279 }
3280
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003281 // Change window height NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003282 if (!ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
3284 if (pp == &p_wh && curwin->w_height < p_wh)
3285 win_setheight((int)p_wh);
3286 if (pp == &p_hh && curbuf->b_help && curwin->w_height < p_hh)
3287 win_setheight((int)p_hh);
3288 }
3289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 else if (pp == &p_wmh)
3291 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003292 // 'winminheight'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (p_wmh < 0)
3294 {
3295 errmsg = e_positive;
3296 p_wmh = 0;
3297 }
3298 if (p_wmh > p_wh)
3299 {
3300 errmsg = e_winheight;
3301 p_wmh = p_wh;
3302 }
3303 win_setminheight();
3304 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003305 else if (pp == &p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003307 // 'winwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (p_wiw < 1)
3309 {
3310 errmsg = e_positive;
3311 p_wiw = 1;
3312 }
3313 if (p_wmw > p_wiw)
3314 {
3315 errmsg = e_winwidth;
3316 p_wiw = p_wmw;
3317 }
3318
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003319 // Change window width NOW
Bram Moolenaar459ca562016-11-10 18:16:33 +01003320 if (!ONE_WINDOW && curwin->w_width < p_wiw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 win_setwidth((int)p_wiw);
3322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 else if (pp == &p_wmw)
3324 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003325 // 'winminwidth'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (p_wmw < 0)
3327 {
3328 errmsg = e_positive;
3329 p_wmw = 0;
3330 }
3331 if (p_wmw > p_wiw)
3332 {
3333 errmsg = e_winwidth;
3334 p_wmw = p_wiw;
3335 }
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02003336 win_setminwidth();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // (re)set last window status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else if (pp == &p_ls)
3341 {
3342 last_status(FALSE);
3343 }
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003344
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003345 // (re)set tab page line
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003346 else if (pp == &p_stal)
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003347 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003348 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00003349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351#ifdef FEAT_GUI
3352 else if (pp == &p_linespace)
3353 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003354 // Recompute gui.char_height and resize the Vim window to keep the
3355 // same number of lines.
Bram Moolenaar02743632005-07-25 20:42:36 +00003356 if (gui.in_use && gui_mch_adjust_charheight() == OK)
Bram Moolenaar3964b7e2006-03-27 20:59:33 +00003357 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359#endif
3360
3361#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003362 // 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else if (pp == &curwin->w_p_fdl)
3364 {
3365 if (curwin->w_p_fdl < 0)
3366 curwin->w_p_fdl = 0;
3367 newFoldLevel();
3368 }
3369
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003370 // 'foldminlines'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else if (pp == &curwin->w_p_fml)
3372 {
3373 foldUpdateAll(curwin);
3374 }
3375
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003376 // 'foldnestmax'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 else if (pp == &curwin->w_p_fdn)
3378 {
3379 if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin))
3380 foldUpdateAll(curwin);
3381 }
3382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003383 // 'foldcolumn'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 else if (pp == &curwin->w_p_fdc)
3385 {
3386 if (curwin->w_p_fdc < 0)
3387 {
3388 errmsg = e_positive;
3389 curwin->w_p_fdc = 0;
3390 }
3391 else if (curwin->w_p_fdc > 12)
3392 {
3393 errmsg = e_invarg;
3394 curwin->w_p_fdc = 12;
3395 }
3396 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003397#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003399#if defined(FEAT_FOLDING) || defined(FEAT_CINDENT)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003400 // 'shiftwidth' or 'tabstop'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts)
3402 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003403# ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (foldmethodIsIndent(curwin))
3405 foldUpdateAll(curwin);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003406# endif
3407# ifdef FEAT_CINDENT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003408 // When 'shiftwidth' changes, or it's zero and 'tabstop' changes:
3409 // parse 'cinoptions'.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003410 if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0)
3411 parse_cino(curbuf);
3412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003416 // 'maxcombine'
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003417 else if (pp == &p_mco)
3418 {
3419 if (p_mco > MAX_MCO)
3420 p_mco = MAX_MCO;
3421 else if (p_mco < 0)
3422 p_mco = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 screenclear(); // will re-allocate the screen
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003424 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 else if (pp == &curbuf->b_p_iminsert)
3427 {
3428 if (curbuf->b_p_iminsert < 0 || curbuf->b_p_iminsert > B_IMODE_LAST)
3429 {
3430 errmsg = e_invarg;
3431 curbuf->b_p_iminsert = B_IMODE_NONE;
3432 }
3433 p_iminsert = curbuf->b_p_iminsert;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003434 if (termcap_active) // don't do this in the alternate screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 showmode();
Bram Moolenaar4033c552017-09-16 20:54:51 +02003436#if defined(FEAT_KEYMAP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003437 // Show/unshow value of 'keymap' in status lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 status_redraw_curbuf();
3439#endif
3440 }
3441
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003442#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 // 'imstyle'
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003444 else if (pp == &p_imst)
3445 {
3446 if (p_imst != IM_ON_THE_SPOT && p_imst != IM_OVER_THE_SPOT)
3447 errmsg = e_invarg;
3448 }
3449#endif
3450
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003451 else if (pp == &p_window)
3452 {
3453 if (p_window < 1)
3454 p_window = 1;
3455 else if (p_window >= Rows)
3456 p_window = Rows - 1;
3457 }
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else if (pp == &curbuf->b_p_imsearch)
3460 {
3461 if (curbuf->b_p_imsearch < -1 || curbuf->b_p_imsearch > B_IMODE_LAST)
3462 {
3463 errmsg = e_invarg;
3464 curbuf->b_p_imsearch = B_IMODE_NONE;
3465 }
3466 p_imsearch = curbuf->b_p_imsearch;
3467 }
3468
3469#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003470 // if 'titlelen' has changed, redraw the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else if (pp == &p_titlelen)
3472 {
3473 if (p_titlelen < 0)
3474 {
3475 errmsg = e_positive;
3476 p_titlelen = 85;
3477 }
3478 if (starting != NO_SCREEN && old_value != p_titlelen)
3479 need_maketitle = TRUE;
3480 }
3481#endif
3482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003483 // if p_ch changed value, change the command line height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else if (pp == &p_ch)
3485 {
3486 if (p_ch < 1)
3487 {
3488 errmsg = e_positive;
3489 p_ch = 1;
3490 }
Bram Moolenaar719939c2007-09-25 12:51:28 +00003491 if (p_ch > Rows - min_rows() + 1)
3492 p_ch = Rows - min_rows() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // Only compute the new window layout when startup has been
3495 // completed. Otherwise the frame sizes may be wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (p_ch != old_value && full_screen
3497#ifdef FEAT_GUI
3498 && !gui.starting
3499#endif
3500 )
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003501 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003504 // when 'updatecount' changes from zero to non-zero, open swap files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 else if (pp == &p_uc)
3506 {
3507 if (p_uc < 0)
3508 {
3509 errmsg = e_positive;
3510 p_uc = 100;
3511 }
3512 if (p_uc && !old_value)
3513 ml_open_files();
3514 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02003515#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003516 else if (pp == &curwin->w_p_cole)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003517 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003518 if (curwin->w_p_cole < 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003519 {
3520 errmsg = e_positive;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003521 curwin->w_p_cole = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003522 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003523 else if (curwin->w_p_cole > 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003524 {
3525 errmsg = e_invarg;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003526 curwin->w_p_cole = 3;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003527 }
3528 }
3529#endif
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003530#ifdef MZSCHEME_GUI_THREADS
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003531 else if (pp == &p_mzq)
3532 mzvim_reset_timer();
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003535#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003536 // 'pyxversion'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003537 else if (pp == &p_pyx)
3538 {
3539 if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3)
3540 errmsg = e_invarg;
3541 }
3542#endif
3543
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003544 // sync undo before 'undolevels' changes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else if (pp == &p_ul)
3546 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003547 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 p_ul = old_value;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003549 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 p_ul = value;
3551 }
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003552 else if (pp == &curbuf->b_p_ul)
3553 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003554 // use the old value, otherwise u_sync() may not work properly
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003555 curbuf->b_p_ul = old_value;
3556 u_sync(TRUE);
3557 curbuf->b_p_ul = value;
3558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003560#ifdef FEAT_LINEBREAK
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003561 // 'numberwidth' must be positive
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003562 else if (pp == &curwin->w_p_nuw)
3563 {
3564 if (curwin->w_p_nuw < 1)
3565 {
3566 errmsg = e_positive;
3567 curwin->w_p_nuw = 1;
3568 }
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003569 if (curwin->w_p_nuw > 20)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003570 {
3571 errmsg = e_invarg;
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003572 curwin->w_p_nuw = 20;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003573 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003574 curwin->w_nrwidth_line_count = 0; // trigger a redraw
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003575 }
3576#endif
3577
Bram Moolenaar1a384422010-07-14 19:53:30 +02003578 else if (pp == &curbuf->b_p_tw)
3579 {
3580 if (curbuf->b_p_tw < 0)
3581 {
3582 errmsg = e_positive;
3583 curbuf->b_p_tw = 0;
3584 }
3585#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02003586 {
3587 win_T *wp;
3588 tabpage_T *tp;
3589
3590 FOR_ALL_TAB_WINDOWS(tp, wp)
3591 check_colorcolumn(wp);
3592 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003593#endif
3594 }
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 /*
3597 * Check the bounds for numeric options here
3598 */
3599 if (Rows < min_rows() && full_screen)
3600 {
3601 if (errbuf != NULL)
3602 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003603 vim_snprintf((char *)errbuf, errbuflen,
3604 _("E593: Need at least %d lines"), min_rows());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 errmsg = errbuf;
3606 }
3607 Rows = min_rows();
3608 }
3609 if (Columns < MIN_COLUMNS && full_screen)
3610 {
3611 if (errbuf != NULL)
3612 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003613 vim_snprintf((char *)errbuf, errbuflen,
3614 _("E594: Need at least %d columns"), MIN_COLUMNS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 errmsg = errbuf;
3616 }
3617 Columns = MIN_COLUMNS;
3618 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003619 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 /*
3622 * If the screen (shell) height has been changed, assume it is the
3623 * physical screenheight.
3624 */
3625 if (old_Rows != Rows || old_Columns != Columns)
3626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003627 // Changing the screen size is not allowed while updating the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (updating_screen)
3629 *pp = old_value;
3630 else if (full_screen
3631#ifdef FEAT_GUI
3632 && !gui.starting
3633#endif
3634 )
3635 set_shellsize((int)Columns, (int)Rows, TRUE);
3636 else
3637 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003638 // Postpone the resizing; check the size and cmdline position for
3639 // messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 check_shellsize();
3641 if (cmdline_row > Rows - p_ch && Rows > p_ch)
3642 cmdline_row = Rows - p_ch;
3643 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003644 if (p_window >= Rows || !option_was_set((char_u *)"window"))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003645 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (curbuf->b_p_ts <= 0)
3649 {
3650 errmsg = e_positive;
3651 curbuf->b_p_ts = 8;
3652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (p_tm < 0)
3654 {
3655 errmsg = e_positive;
3656 p_tm = 0;
3657 }
3658 if ((curwin->w_p_scr <= 0
3659 || (curwin->w_p_scr > curwin->w_height
3660 && curwin->w_height > 0))
3661 && full_screen)
3662 {
3663 if (pp == &(curwin->w_p_scr))
3664 {
3665 if (curwin->w_p_scr != 0)
3666 errmsg = e_scroll;
3667 win_comp_scroll(curwin);
3668 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003669 // If 'scroll' became invalid because of a side effect silently adjust
3670 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 else if (curwin->w_p_scr <= 0)
3672 curwin->w_p_scr = 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003673 else // curwin->w_p_scr > curwin->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 curwin->w_p_scr = curwin->w_height;
3675 }
Bram Moolenaar991e10f2008-10-02 20:48:41 +00003676 if (p_hi < 0)
3677 {
3678 errmsg = e_positive;
3679 p_hi = 0;
3680 }
Bram Moolenaar78159bb2014-06-25 11:48:54 +02003681 else if (p_hi > 10000)
3682 {
3683 errmsg = e_invarg;
3684 p_hi = 10000;
3685 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003686 if (p_re < 0 || p_re > 2)
3687 {
3688 errmsg = e_invarg;
3689 p_re = 0;
3690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 if (p_report < 0)
3692 {
3693 errmsg = e_positive;
3694 p_report = 1;
3695 }
Bram Moolenaar1e015462005-09-25 22:16:38 +00003696 if ((p_sj < -100 || p_sj >= Rows) && full_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003698 if (Rows != old_Rows) // Rows changed, just adjust p_sj
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 p_sj = Rows / 2;
3700 else
3701 {
3702 errmsg = e_scroll;
3703 p_sj = 1;
3704 }
3705 }
3706 if (p_so < 0 && full_screen)
3707 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01003708 errmsg = e_positive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 p_so = 0;
3710 }
3711 if (p_siso < 0 && full_screen)
3712 {
3713 errmsg = e_positive;
3714 p_siso = 0;
3715 }
3716#ifdef FEAT_CMDWIN
3717 if (p_cwh < 1)
3718 {
3719 errmsg = e_positive;
3720 p_cwh = 1;
3721 }
3722#endif
3723 if (p_ut < 0)
3724 {
3725 errmsg = e_positive;
3726 p_ut = 2000;
3727 }
3728 if (p_ss < 0)
3729 {
3730 errmsg = e_positive;
3731 p_ss = 0;
3732 }
3733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003734 // May set global value for local option.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3736 *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp;
3737
3738 options[opt_idx].flags |= P_WAS_SET;
3739
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003740#if defined(FEAT_EVAL)
Bram Moolenaarf4140482020-02-15 23:06:45 +01003741 apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3742 value, errmsg);
Bram Moolenaar53744302015-07-17 17:38:22 +02003743#endif
3744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003745 comp_col(); // in case 'columns' or 'ls' changed
Bram Moolenaar913077c2012-03-28 19:59:04 +02003746 if (curwin->w_curswant != MAXCOL
Bram Moolenaar488eb262015-03-13 11:23:50 +01003747 && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaar913077c2012-03-28 19:59:04 +02003748 curwin->w_set_curswant = TRUE;
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003749 if ((opt_flags & OPT_NO_REDRAW) == 0)
3750 check_redraw(options[opt_idx].flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 return errmsg;
3753}
3754
3755/*
3756 * Called after an option changed: check if something needs to be redrawn.
3757 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003759check_redraw(long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003761 // Careful: P_RCLR and P_RALL are a combination of other P_ flags
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003762 int doclear = (flags & P_RCLR) == P_RCLR;
3763 int all = ((flags & P_RALL) == P_RALL || doclear);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003765 if ((flags & P_RSTAT) || all) // mark all status lines dirty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 status_redraw_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
3768 if ((flags & P_RBUF) || (flags & P_RWIN) || all)
3769 changed_window_setting();
3770 if (flags & P_RBUF)
3771 redraw_curbuf_later(NOT_VALID);
Bram Moolenaara2477fd2016-12-03 15:13:20 +01003772 if (flags & P_RWINONLY)
3773 redraw_later(NOT_VALID);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003774 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 redraw_all_later(CLEAR);
3776 else if (all)
3777 redraw_all_later(NOT_VALID);
3778}
3779
3780/*
3781 * Find index for option 'arg'.
3782 * Return -1 if not found.
3783 */
Bram Moolenaardac13472019-09-16 21:06:21 +02003784 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003785findoption(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 int opt_idx;
3788 char *s, *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003789 static short quick_tab[27] = {0, 0}; // quick access table
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 int is_term_opt;
3791
3792 /*
3793 * For first call: Initialize the quick-access table.
3794 * It contains the index for the first option that starts with a certain
3795 * letter. There are 26 letters, plus the first "t_" option.
3796 */
3797 if (quick_tab[1] == 0)
3798 {
3799 p = options[0].fullname;
3800 for (opt_idx = 1; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3801 {
3802 if (s[0] != p[0])
3803 {
3804 if (s[0] == 't' && s[1] == '_')
3805 quick_tab[26] = opt_idx;
3806 else
3807 quick_tab[CharOrdLow(s[0])] = opt_idx;
3808 }
3809 p = s;
3810 }
3811 }
3812
3813 /*
3814 * Check for name starting with an illegal character.
3815 */
3816#ifdef EBCDIC
3817 if (!islower(arg[0]))
3818#else
3819 if (arg[0] < 'a' || arg[0] > 'z')
3820#endif
3821 return -1;
3822
3823 is_term_opt = (arg[0] == 't' && arg[1] == '_');
3824 if (is_term_opt)
3825 opt_idx = quick_tab[26];
3826 else
3827 opt_idx = quick_tab[CharOrdLow(arg[0])];
3828 for ( ; (s = options[opt_idx].fullname) != NULL; opt_idx++)
3829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003830 if (STRCMP(arg, s) == 0) // match full name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 break;
3832 }
3833 if (s == NULL && !is_term_opt)
3834 {
3835 opt_idx = quick_tab[CharOrdLow(arg[0])];
3836 for ( ; options[opt_idx].fullname != NULL; opt_idx++)
3837 {
3838 s = options[opt_idx].shortname;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 if (s != NULL && STRCMP(arg, s) == 0) // match short name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 break;
3841 s = NULL;
3842 }
3843 }
3844 if (s == NULL)
3845 opt_idx = -1;
3846 return opt_idx;
3847}
3848
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003849#if defined(FEAT_EVAL) || defined(FEAT_TCL) || defined(FEAT_MZSCHEME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850/*
3851 * Get the value for an option.
3852 *
3853 * Returns:
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003854 * Number option: gov_number, *numval gets value.
Bram Moolenaara7251492021-01-02 16:53:13 +01003855 * Toggle option: gov_bool, *numval gets value.
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003856 * String option: gov_string, *stringval gets allocated string.
3857 * Hidden Number option: gov_hidden_number.
3858 * Hidden Toggle option: gov_hidden_bool.
3859 * Hidden String option: gov_hidden_string.
3860 * Unknown option: gov_unknown.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 */
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003862 getoption_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003863get_option_value(
3864 char_u *name,
3865 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003866 char_u **stringval, // NULL when only checking existence
Bram Moolenaar9b578142016-01-30 19:39:49 +01003867 int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869 int opt_idx;
3870 char_u *varp;
3871
3872 opt_idx = findoption(name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003873 if (opt_idx < 0) // option not in the table
Bram Moolenaare353c402017-02-04 19:49:16 +01003874 {
3875 int key;
3876
3877 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003878 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01003879 {
3880 char_u key_name[2];
3881 char_u *p;
3882
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003883 // check for a terminal option
Bram Moolenaare353c402017-02-04 19:49:16 +01003884 if (key < 0)
3885 {
3886 key_name[0] = KEY2TERMCAP0(key);
3887 key_name[1] = KEY2TERMCAP1(key);
3888 }
3889 else
3890 {
3891 key_name[0] = KS_KEY;
3892 key_name[1] = (key & 0xff);
3893 }
3894 p = find_termcode(key_name);
3895 if (p != NULL)
3896 {
3897 if (stringval != NULL)
3898 *stringval = vim_strsave(p);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003899 return gov_string;
Bram Moolenaare353c402017-02-04 19:49:16 +01003900 }
3901 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003902 return gov_unknown;
Bram Moolenaare353c402017-02-04 19:49:16 +01003903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
3906
3907 if (options[opt_idx].flags & P_STRING)
3908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003909 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003910 return gov_hidden_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (stringval != NULL)
3912 {
3913#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003914 // never return the value of the crypt key
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003915 if ((char_u **)varp == &curbuf->b_p_key
3916 && **(char_u **)(varp) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 *stringval = vim_strsave((char_u *)"*****");
3918 else
3919#endif
3920 *stringval = vim_strsave(*(char_u **)(varp));
3921 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003922 return gov_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003925 if (varp == NULL) // hidden option
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003926 return (options[opt_idx].flags & P_NUM)
3927 ? gov_hidden_number : gov_hidden_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if (options[opt_idx].flags & P_NUM)
3929 *numval = *(long *)varp;
3930 else
3931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003932 // Special case: 'modified' is b_changed, but we also want to consider
3933 // it set when 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if ((int *)varp == &curbuf->b_changed)
3935 *numval = curbufIsChanged();
3936 else
Bram Moolenaar2acfbed2016-07-01 23:14:02 +02003937 *numval = (long) *(int *)varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01003939 return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}
3941#endif
3942
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01003943#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003944/*
3945 * Returns the option attributes and its value. Unlike the above function it
3946 * will return either global value or local value of the option depending on
3947 * what was requested, but it will never return global value if it was
3948 * requested to return local one and vice versa. Neither it will return
3949 * buffer-local value if it was requested to return window-local one.
3950 *
3951 * Pretends that option is absent if it is not present in the requested scope
3952 * (i.e. has no global, window-local or buffer-local value depending on
3953 * opt_type). Uses
3954 *
3955 * Returned flags:
Bram Moolenaar75a8d742014-05-07 15:10:21 +02003956 * 0 hidden or unknown option, also option that does not have requested
3957 * type (see SREQ_* in vim.h)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003958 * see SOPT_* in vim.h for other flags
3959 *
3960 * Possible opt_type values: see SREQ_* in vim.h
3961 */
3962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003963get_option_value_strict(
3964 char_u *name,
3965 long *numval,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 char_u **stringval, // NULL when only obtaining attributes
Bram Moolenaar9b578142016-01-30 19:39:49 +01003967 int opt_type,
3968 void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003969{
3970 int opt_idx;
Bram Moolenaar68001862013-05-11 13:45:05 +02003971 char_u *varp = NULL;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003972 struct vimoption *p;
3973 int r = 0;
3974
3975 opt_idx = findoption(name);
3976 if (opt_idx < 0)
3977 return 0;
3978
3979 p = &(options[opt_idx]);
3980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003981 // Hidden option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003982 if (p->var == NULL)
3983 return 0;
3984
3985 if (p->flags & P_BOOL)
3986 r |= SOPT_BOOL;
3987 else if (p->flags & P_NUM)
3988 r |= SOPT_NUM;
3989 else if (p->flags & P_STRING)
3990 r |= SOPT_STRING;
3991
3992 if (p->indir == PV_NONE)
3993 {
3994 if (opt_type == SREQ_GLOBAL)
3995 r |= SOPT_GLOBAL;
3996 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003997 return 0; // Did not request global-only option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02003998 }
3999 else
4000 {
4001 if (p->indir & PV_BOTH)
4002 r |= SOPT_GLOBAL;
4003 else if (opt_type == SREQ_GLOBAL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004004 return 0; // Requested global option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004005
4006 if (p->indir & PV_WIN)
4007 {
4008 if (opt_type == SREQ_BUF)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004009 return 0; // Did not request window-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004010 else
4011 r |= SOPT_WIN;
4012 }
4013 else if (p->indir & PV_BUF)
4014 {
4015 if (opt_type == SREQ_WIN)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004016 return 0; // Did not request buffer-local option
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004017 else
4018 r |= SOPT_BUF;
4019 }
4020 }
4021
4022 if (stringval == NULL)
4023 return r;
4024
4025 if (opt_type == SREQ_GLOBAL)
4026 varp = p->var;
4027 else
4028 {
4029 if (opt_type == SREQ_BUF)
4030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004031 // Special case: 'modified' is b_changed, but we also want to
4032 // consider it set when 'ff' or 'fenc' changed.
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004033 if (p->indir == PV_MOD)
4034 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004035 *numval = bufIsChanged((buf_T *)from);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004036 varp = NULL;
4037 }
4038#ifdef FEAT_CRYPT
4039 else if (p->indir == PV_KEY)
4040 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004041 // never return the value of the crypt key
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004042 *stringval = NULL;
4043 varp = NULL;
4044 }
4045#endif
4046 else
4047 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004048 buf_T *save_curbuf = curbuf;
4049
4050 // only getting a pointer, no need to use aucmd_prepbuf()
4051 curbuf = (buf_T *)from;
4052 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004053 varp = get_varp(p);
Bram Moolenaardefe6422018-06-24 15:14:07 +02004054 curbuf = save_curbuf;
4055 curwin->w_buffer = curbuf;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004056 }
4057 }
4058 else if (opt_type == SREQ_WIN)
4059 {
Bram Moolenaardefe6422018-06-24 15:14:07 +02004060 win_T *save_curwin = curwin;
4061
4062 curwin = (win_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004063 curbuf = curwin->w_buffer;
4064 varp = get_varp(p);
4065 curwin = save_curwin;
4066 curbuf = curwin->w_buffer;
4067 }
4068 if (varp == p->var)
4069 return (r | SOPT_UNSET);
4070 }
4071
4072 if (varp != NULL)
4073 {
4074 if (p->flags & P_STRING)
4075 *stringval = vim_strsave(*(char_u **)(varp));
4076 else if (p->flags & P_NUM)
4077 *numval = *(long *) varp;
4078 else
4079 *numval = *(int *)varp;
4080 }
4081
4082 return r;
4083}
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004084
4085/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004086 * Iterate over options. First argument is a pointer to a pointer to a
4087 * structure inside options[] array, second is option type like in the above
4088 * function.
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004089 *
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004090 * If first argument points to NULL it is assumed that iteration just started
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004091 * and caller needs the very first value.
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02004092 * If first argument points to the end marker function returns NULL and sets
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004093 * first argument to NULL.
4094 *
4095 * Returns full option name for current option on each call.
4096 */
4097 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004098option_iter_next(void **option, int opt_type)
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004099{
4100 struct vimoption *ret = NULL;
4101 do
4102 {
4103 if (*option == NULL)
4104 *option = (void *) options;
4105 else if (((struct vimoption *) (*option))->fullname == NULL)
4106 {
4107 *option = NULL;
4108 return NULL;
4109 }
4110 else
4111 *option = (void *) (((struct vimoption *) (*option)) + 1);
4112
4113 ret = ((struct vimoption *) (*option));
4114
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004115 // Hidden option
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004116 if (ret->var == NULL)
4117 {
4118 ret = NULL;
4119 continue;
4120 }
4121
4122 switch (opt_type)
4123 {
4124 case SREQ_GLOBAL:
4125 if (!(ret->indir == PV_NONE || ret->indir & PV_BOTH))
4126 ret = NULL;
4127 break;
4128 case SREQ_BUF:
4129 if (!(ret->indir & PV_BUF))
4130 ret = NULL;
4131 break;
4132 case SREQ_WIN:
4133 if (!(ret->indir & PV_WIN))
4134 ret = NULL;
4135 break;
4136 default:
Bram Moolenaar95f09602016-11-10 20:01:45 +01004137 internal_error("option_iter_next()");
Bram Moolenaar1028f4d2014-01-14 16:55:00 +01004138 return NULL;
4139 }
4140 }
4141 while (ret == NULL);
4142
4143 return (char_u *)ret->fullname;
4144}
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004145#endif
4146
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004148 * Return the flags for the option at 'opt_idx'.
4149 */
4150 long_u
4151get_option_flags(int opt_idx)
4152{
4153 return options[opt_idx].flags;
4154}
4155
4156/*
4157 * Set a flag for the option at 'opt_idx'.
4158 */
4159 void
4160set_option_flag(int opt_idx, long_u flag)
4161{
4162 options[opt_idx].flags |= flag;
4163}
4164
4165/*
4166 * Clear a flag for the option at 'opt_idx'.
4167 */
4168 void
4169clear_option_flag(int opt_idx, long_u flag)
4170{
4171 options[opt_idx].flags &= ~flag;
4172}
4173
4174/*
4175 * Returns TRUE if the option at 'opt_idx' is a global option
4176 */
4177 int
4178is_global_option(int opt_idx)
4179{
4180 return options[opt_idx].indir == PV_NONE;
4181}
4182
4183/*
4184 * Returns TRUE if the option at 'opt_idx' is a global option which also has a
4185 * local value.
4186 */
4187 int
4188is_global_local_option(int opt_idx)
4189{
4190 return options[opt_idx].indir & PV_BOTH;
4191}
4192
4193/*
4194 * Returns TRUE if the option at 'opt_idx' is a window-local option
4195 */
4196 int
4197is_window_local_option(int opt_idx)
4198{
4199 return options[opt_idx].var == VAR_WIN;
4200}
4201
4202/*
4203 * Returns TRUE if the option at 'opt_idx' is a hidden option
4204 */
4205 int
4206is_hidden_option(int opt_idx)
4207{
4208 return options[opt_idx].var == NULL;
4209}
4210
4211#if defined(FEAT_CRYPT) || defined(PROTO)
4212/*
4213 * Returns TRUE if the option at 'opt_idx' is a crypt key option
4214 */
4215 int
4216is_crypt_key_option(int opt_idx)
4217{
4218 return options[opt_idx].indir == PV_KEY;
4219}
4220#endif
4221
4222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 * Set the value of option "name".
4224 * Use "string" for string options, use "number" for other options.
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004225 *
4226 * Returns NULL on success or error message on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004228 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004229set_option_value(
4230 char_u *name,
4231 long number,
4232 char_u *string,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004233 int opt_flags) // OPT_LOCAL or 0 (both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235 int opt_idx;
4236 char_u *varp;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004237 long_u flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 opt_idx = findoption(name);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004240 if (opt_idx < 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004241 {
4242 int key;
4243
4244 if (STRLEN(name) == 4 && name[0] == 't' && name[1] == '_'
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004245 && (key = find_key_option(name, FALSE)) != 0)
Bram Moolenaare353c402017-02-04 19:49:16 +01004246 {
4247 char_u key_name[2];
4248
4249 if (key < 0)
4250 {
4251 key_name[0] = KEY2TERMCAP0(key);
4252 key_name[1] = KEY2TERMCAP1(key);
4253 }
4254 else
4255 {
4256 key_name[0] = KS_KEY;
4257 key_name[1] = (key & 0xff);
4258 }
4259 add_termcode(key_name, string, FALSE);
4260 if (full_screen)
4261 ttest(FALSE);
4262 redraw_all_later(CLEAR);
4263 return NULL;
4264 }
4265
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004266 semsg(_("E355: Unknown option: %s"), name);
Bram Moolenaare353c402017-02-04 19:49:16 +01004267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 else
4269 {
4270 flags = options[opt_idx].flags;
4271#ifdef HAVE_SANDBOX
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004272 // Disallow changing some options in the sandbox
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (sandbox > 0 && (flags & P_SECURE))
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004275 emsg(_(e_sandbox));
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004276 return NULL;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004279 if (flags & P_STRING)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004280 return set_string_option(opt_idx, string, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 else
4282 {
Bram Moolenaarb3163762008-07-08 15:15:08 +00004283 varp = get_varp_scope(&(options[opt_idx]), opt_flags);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004284 if (varp != NULL) // hidden option is not changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004286 if (number == 0 && string != NULL)
4287 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004288 int idx;
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004289
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004290 // Either we are given a string or we are setting option
4291 // to zero.
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004292 for (idx = 0; string[idx] == '0'; ++idx)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004293 ;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004294 if (string[idx] != NUL || idx == 0)
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004295 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004296 // There's another character after zeros or the string
4297 // is empty. In both cases, we are trying to set a
4298 // num option using a string.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004299 semsg(_("E521: Number required: &%s = '%s'"),
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004300 name, string);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004301 return NULL; // do nothing as we hit an error
Bram Moolenaar96bb6212007-06-19 18:52:53 +00004302
4303 }
4304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (flags & P_NUM)
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004306 return set_num_option(opt_idx, varp, number,
Bram Moolenaar555b2802005-05-19 21:08:39 +00004307 NULL, 0, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 else
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004309 return set_bool_option(opt_idx, varp, (int)number,
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004310 opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312 }
4313 }
Bram Moolenaarc96ebe72013-05-21 22:38:18 +02004314 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315}
4316
4317/*
4318 * Get the terminal code for a terminal option.
4319 * Returns NULL when not found.
4320 */
4321 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004322get_term_code(char_u *tname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
4324 int opt_idx;
4325 char_u *varp;
4326
4327 if (tname[0] != 't' || tname[1] != '_' ||
4328 tname[2] == NUL || tname[3] == NUL)
4329 return NULL;
4330 if ((opt_idx = findoption(tname)) >= 0)
4331 {
4332 varp = get_varp(&(options[opt_idx]));
4333 if (varp != NULL)
4334 varp = *(char_u **)(varp);
4335 return varp;
4336 }
4337 return find_termcode(tname + 2);
4338}
4339
4340 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004341get_highlight_default(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
4343 int i;
4344
4345 i = findoption((char_u *)"hl");
4346 if (i >= 0)
4347 return options[i].def_val[VI_DEFAULT];
4348 return (char_u *)NULL;
4349}
4350
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004351 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004352get_encoding_default(void)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004353{
4354 int i;
4355
4356 i = findoption((char_u *)"enc");
4357 if (i >= 0)
4358 return options[i].def_val[VI_DEFAULT];
4359 return (char_u *)NULL;
4360}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362/*
4363 * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004364 * When "has_lt" is true there is a '<' before "*arg_arg".
4365 * Returns 0 when the key is not recognized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 */
4367 static int
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004368find_key_option(char_u *arg_arg, int has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004370 int key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 int modifiers;
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004372 char_u *arg = arg_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 /*
4375 * Don't use get_special_key_code() for t_xx, we don't want it to call
4376 * add_termcap_entry().
4377 */
4378 if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3])
4379 key = TERMCAP2KEY(arg[2], arg[3]);
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02004380 else if (has_lt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004382 --arg; // put arg at the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 modifiers = 0;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02004384 key = find_special_key(&arg, &modifiers,
4385 FSK_KEYCODE | FSK_KEEP_X_KEY | FSK_SIMPLIFY, NULL);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004386 if (modifiers) // can't handle modifiers here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 key = 0;
4388 }
4389 return key;
4390}
4391
4392/*
4393 * if 'all' == 0: show changed options
4394 * if 'all' == 1: show all normal options
4395 * if 'all' == 2: show all terminal options
4396 */
4397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004398showoptions(
4399 int all,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004400 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
4402 struct vimoption *p;
4403 int col;
4404 int isterm;
4405 char_u *varp;
4406 struct vimoption **items;
4407 int item_count;
4408 int run;
4409 int row, rows;
4410 int cols;
4411 int i;
4412 int len;
4413
4414#define INC 20
4415#define GAP 3
4416
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004417 items = ALLOC_MULT(struct vimoption *, OPTION_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (items == NULL)
4419 return;
4420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004421 // Highlight title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (all == 2)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004423 msg_puts_title(_("\n--- Terminal codes ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 else if (opt_flags & OPT_GLOBAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004425 msg_puts_title(_("\n--- Global option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 else if (opt_flags & OPT_LOCAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004427 msg_puts_title(_("\n--- Local option values ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004429 msg_puts_title(_("\n--- Options ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 /*
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004432 * Do the loop two times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * 1. display the short items
4434 * 2. display the long items (only strings and numbers)
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004435 * When "opt_flags" has OPT_ONECOLUMN do everything in run 2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 */
4437 for (run = 1; run <= 2 && !got_int; ++run)
4438 {
4439 /*
4440 * collect the items in items[]
4441 */
4442 item_count = 0;
4443 for (p = &options[0]; p->fullname != NULL; p++)
4444 {
Bram Moolenaarf86db782018-10-25 13:31:37 +02004445 // apply :filter /pat/
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004446 if (message_filtered((char_u *)p->fullname))
Bram Moolenaarf86db782018-10-25 13:31:37 +02004447 continue;
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 varp = NULL;
4450 isterm = istermoption(p);
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004451 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 if (p->indir != PV_NONE && !isterm)
4454 varp = get_varp_scope(p, opt_flags);
4455 }
4456 else
4457 varp = get_varp(p);
4458 if (varp != NULL
4459 && ((all == 2 && isterm)
4460 || (all == 1 && !isterm)
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004461 || (all == 0 && !optval_default(p, varp, p_cp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar6b915c02020-01-18 15:53:19 +01004463 if (opt_flags & OPT_ONECOLUMN)
4464 len = Columns;
4465 else if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004466 len = 1; // a toggle option fits always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else
4468 {
4469 option_value2string(p, opt_flags);
4470 len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1;
4471 }
4472 if ((len <= INC - GAP && run == 1) ||
4473 (len > INC - GAP && run == 2))
4474 items[item_count++] = p;
4475 }
4476 }
4477
4478 /*
4479 * display the items
4480 */
4481 if (run == 1)
4482 {
4483 cols = (Columns + GAP - 3) / INC;
4484 if (cols == 0)
4485 cols = 1;
4486 rows = (item_count + cols - 1) / cols;
4487 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004488 else // run == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 rows = item_count;
4490 for (row = 0; row < rows && !got_int; ++row)
4491 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004492 msg_putchar('\n'); // go to next line
4493 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
4495 col = 0;
4496 for (i = row; i < item_count; i += rows)
4497 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004498 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 showoneopt(items[i], opt_flags);
4500 col += INC;
4501 }
4502 out_flush();
4503 ui_breakcheck();
4504 }
4505 }
4506 vim_free(items);
4507}
4508
4509/*
4510 * Return TRUE if option "p" has its default value.
4511 */
4512 static int
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004513optval_default(struct vimoption *p, char_u *varp, int compatible)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514{
4515 int dvi;
4516
4517 if (varp == NULL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004518 return TRUE; // hidden option is always at default
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004519 dvi = ((p->flags & P_VI_DEF) || compatible) ? VI_DEFAULT : VIM_DEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (p->flags & P_NUM)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004521 return (*(long *)varp == (long)(long_i)p->def_val[dvi]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (p->flags & P_BOOL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004523 // the cast to long is required for Manx C, long_i is
4524 // needed for MSVC
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004525 return (*(int *)varp == (int)(long)(long_i)p->def_val[dvi]);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004526 // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 return (STRCMP(*(char_u **)varp, p->def_val[dvi]) == 0);
4528}
4529
4530/*
4531 * showoneopt: show the value of one option
4532 * must not be called with a hidden option!
4533 */
4534 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004535showoneopt(
4536 struct vimoption *p,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004537 int opt_flags) // OPT_LOCAL or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004539 char_u *varp;
4540 int save_silent = silent_mode;
4541
4542 silent_mode = FALSE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004543 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 varp = get_varp_scope(p, opt_flags);
4546
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004547 // for 'modified' we also need to check if 'ff' or 'fenc' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed
4549 ? !curbufIsChanged() : !*(int *)varp))
Bram Moolenaar32526b32019-01-19 17:43:09 +01004550 msg_puts("no");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else if ((p->flags & P_BOOL) && *(int *)varp < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552 msg_puts("--");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554 msg_puts(" ");
4555 msg_puts(p->fullname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (!(p->flags & P_BOOL))
4557 {
4558 msg_putchar('=');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004559 // put value string in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 option_value2string(p, opt_flags);
4561 msg_outtrans(NameBuff);
4562 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004563
4564 silent_mode = save_silent;
4565 info_message = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566}
4567
4568/*
4569 * Write modified options as ":set" commands to a file.
4570 *
4571 * There are three values for "opt_flags":
4572 * OPT_GLOBAL: Write global option values and fresh values of
4573 * buffer-local options (used for start of a session
4574 * file).
4575 * OPT_GLOBAL + OPT_LOCAL: Idem, add fresh values of window-local options for
4576 * curwin (used for a vimrc file).
4577 * OPT_LOCAL: Write buffer-local option values for curbuf, fresh
4578 * and local values for window-local options of
4579 * curwin. Local values are also written when at the
4580 * default value, because a modeline or autocommand
4581 * may have set them when doing ":edit file" and the
4582 * user has set them back at the default or fresh
4583 * value.
4584 * When "local_only" is TRUE, don't write fresh
4585 * values, only local values (for ":mkview").
4586 * (fresh value = value used for a new buffer or window for a local option).
4587 *
4588 * Return FAIL on error, OK otherwise.
4589 */
4590 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004591makeset(FILE *fd, int opt_flags, int local_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
4593 struct vimoption *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004594 char_u *varp; // currently used value
4595 char_u *varp_fresh; // local value
4596 char_u *varp_local = NULL; // fresh value
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 char *cmd;
4598 int round;
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004599 int pri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 /*
4602 * The options that don't have a default (terminal name, columns, lines)
4603 * are never written. Terminal options are also not written.
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004604 * Do the loop over "options[]" twice: once for options with the
4605 * P_PRI_MKRC flag and once without.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 */
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004607 for (pri = 1; pri >= 0; --pri)
4608 {
4609 for (p = &options[0]; !istermoption(p); p++)
4610 if (!(p->flags & P_NO_MKRC)
4611 && !istermoption(p)
4612 && ((pri == 1) == ((p->flags & P_PRI_MKRC) != 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004614 // skip global option when only doing locals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (p->indir == PV_NONE && !(opt_flags & OPT_GLOBAL))
4616 continue;
4617
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 // Do not store options like 'bufhidden' and 'syntax' in a vimrc
4619 // file, they are always buffer-specific.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if ((opt_flags & OPT_GLOBAL) && (p->flags & P_NOGLOB))
4621 continue;
4622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004623 // Global values are only written when not at the default value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 varp = get_varp_scope(p, opt_flags);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004625 if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 continue;
4627
Bram Moolenaard23b7142021-04-17 21:04:34 +02004628 if ((opt_flags & OPT_SKIPRTP) && (p->var == (char_u *)&p_rtp
4629 || p->var == (char_u *)&p_pp))
Bram Moolenaar635bd602021-04-16 19:58:22 +02004630 continue;
4631
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 round = 2;
4633 if (p->indir != PV_NONE)
4634 {
4635 if (p->var == VAR_WIN)
4636 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 // skip window-local option when only doing globals
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (!(opt_flags & OPT_LOCAL))
4639 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004640 // When fresh value of window-local option is not at the
4641 // default, need to write it too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (!(opt_flags & OPT_GLOBAL) && !local_only)
4643 {
4644 varp_fresh = get_varp_scope(p, OPT_GLOBAL);
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02004645 if (!optval_default(p, varp_fresh, p_cp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 round = 1;
4648 varp_local = varp;
4649 varp = varp_fresh;
4650 }
4651 }
4652 }
4653 }
4654
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004655 // Round 1: fresh value for window-local options.
4656 // Round 2: other values
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 for ( ; round <= 2; varp = varp_local, ++round)
4658 {
4659 if (round == 1 || (opt_flags & OPT_GLOBAL))
4660 cmd = "set";
4661 else
4662 cmd = "setlocal";
4663
4664 if (p->flags & P_BOOL)
4665 {
4666 if (put_setbool(fd, cmd, p->fullname, *(int *)varp) == FAIL)
4667 return FAIL;
4668 }
4669 else if (p->flags & P_NUM)
4670 {
4671 if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL)
4672 return FAIL;
4673 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004674 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004676 int do_endif = FALSE;
4677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // Don't set 'syntax' and 'filetype' again if the value is
4679 // already right, avoids reloading the syntax file.
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004680 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004681#if defined(FEAT_SYN_HL)
4682 p->indir == PV_SYN ||
4683#endif
4684 p->indir == PV_FT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
4686 if (fprintf(fd, "if &%s != '%s'", p->fullname,
4687 *(char_u **)(varp)) < 0
4688 || put_eol(fd) < 0)
4689 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004690 do_endif = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692 if (put_setstring(fd, cmd, p->fullname, (char_u **)varp,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004693 p->flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 return FAIL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004695 if (do_endif)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
4697 if (put_line(fd, "endif") == FAIL)
4698 return FAIL;
4699 }
4700 }
4701 }
4702 }
Bram Moolenaar7fd16022007-09-06 14:35:35 +00004703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return OK;
4705}
4706
4707#if defined(FEAT_FOLDING) || defined(PROTO)
4708/*
4709 * Generate set commands for the local fold options only. Used when
4710 * 'sessionoptions' or 'viewoptions' contains "folds" but not "options".
4711 */
4712 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004713makefoldset(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004715 if (put_setstring(fd, "setlocal", "fdm", &curwin->w_p_fdm, 0) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716# ifdef FEAT_EVAL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004717 || put_setstring(fd, "setlocal", "fde", &curwin->w_p_fde, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 == FAIL
4719# endif
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004720 || put_setstring(fd, "setlocal", "fmr", &curwin->w_p_fmr, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 == FAIL
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004722 || put_setstring(fd, "setlocal", "fdi", &curwin->w_p_fdi, 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 == FAIL
4724 || put_setnum(fd, "setlocal", "fdl", &curwin->w_p_fdl) == FAIL
4725 || put_setnum(fd, "setlocal", "fml", &curwin->w_p_fml) == FAIL
4726 || put_setnum(fd, "setlocal", "fdn", &curwin->w_p_fdn) == FAIL
4727 || put_setbool(fd, "setlocal", "fen", curwin->w_p_fen) == FAIL
4728 )
4729 return FAIL;
4730
4731 return OK;
4732}
4733#endif
4734
4735 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004736put_setstring(
4737 FILE *fd,
4738 char *cmd,
4739 char *name,
4740 char_u **valuep,
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004741 long_u flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 char_u *s;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004744 char_u *buf = NULL;
4745 char_u *part = NULL;
4746 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4749 return FAIL;
4750 if (*valuep != NULL)
4751 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004752 // Output 'pastetoggle' as key names. For other
4753 // options some characters have to be escaped with
4754 // CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (valuep == &p_pt)
4756 {
4757 s = *valuep;
4758 while (*s != NUL)
Bram Moolenaar7d96acd2008-06-09 15:07:54 +00004759 if (put_escstr(fd, str2special(&s, FALSE), 2) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 return FAIL;
4761 }
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004762 // expand the option value, replace $HOME by ~
4763 else if ((flags & P_EXPAND) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004765 int size = (int)STRLEN(*valuep) + 1;
4766
4767 // replace home directory in the whole option value into "buf"
4768 buf = alloc(size);
Bram Moolenaarf8441472011-04-28 17:24:58 +02004769 if (buf == NULL)
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004770 goto fail;
4771 home_replace(NULL, *valuep, buf, size, FALSE);
4772
4773 // If the option value is longer than MAXPATHL, we need to append
Bram Moolenaar32aa1022019-11-02 22:54:41 +01004774 // each comma separated part of the option separately, so that it
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004775 // can be expanded when read back.
4776 if (size >= MAXPATHL && (flags & P_COMMA) != 0
4777 && vim_strchr(*valuep, ',') != NULL)
4778 {
4779 part = alloc(size);
4780 if (part == NULL)
4781 goto fail;
4782
4783 // write line break to clear the option, e.g. ':set rtp='
4784 if (put_eol(fd) == FAIL)
4785 goto fail;
4786
4787 p = buf;
4788 while (*p != NUL)
4789 {
4790 // for each comma separated option part, append value to
4791 // the option, :set rtp+=value
4792 if (fprintf(fd, "%s %s+=", cmd, name) < 0)
4793 goto fail;
4794 (void)copy_option_part(&p, part, size, ",");
4795 if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL)
4796 goto fail;
4797 }
4798 vim_free(buf);
4799 vim_free(part);
4800 return OK;
4801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 if (put_escstr(fd, buf, 2) == FAIL)
Bram Moolenaarf8441472011-04-28 17:24:58 +02004803 {
4804 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return FAIL;
Bram Moolenaarf8441472011-04-28 17:24:58 +02004806 }
4807 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 else if (put_escstr(fd, *valuep, 2) == FAIL)
4810 return FAIL;
4811 }
4812 if (put_eol(fd) < 0)
4813 return FAIL;
4814 return OK;
Bram Moolenaared18f2c2019-01-24 20:30:52 +01004815fail:
4816 vim_free(buf);
4817 vim_free(part);
4818 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819}
4820
4821 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004822put_setnum(
4823 FILE *fd,
4824 char *cmd,
4825 char *name,
4826 long *valuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 long wc;
4829
4830 if (fprintf(fd, "%s %s=", cmd, name) < 0)
4831 return FAIL;
4832 if (wc_use_keyname((char_u *)valuep, &wc))
4833 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004834 // print 'wildchar' and 'wildcharm' as a key name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0)
4836 return FAIL;
4837 }
4838 else if (fprintf(fd, "%ld", *valuep) < 0)
4839 return FAIL;
4840 if (put_eol(fd) < 0)
4841 return FAIL;
4842 return OK;
4843}
4844
4845 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004846put_setbool(
4847 FILE *fd,
4848 char *cmd,
4849 char *name,
4850 int value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004852 if (value < 0) // global/local option using global value
Bram Moolenaar893de922007-10-02 18:40:57 +00004853 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (fprintf(fd, "%s %s%s", cmd, value ? "" : "no", name) < 0
4855 || put_eol(fd) < 0)
4856 return FAIL;
4857 return OK;
4858}
4859
4860/*
4861 * Clear all the terminal options.
4862 * If the option has been allocated, free the memory.
4863 * Terminal options are never hidden or indirect.
4864 */
4865 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004866clear_termoptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 /*
4869 * Reset a few things before clearing the old options. This may cause
4870 * outputting a few things that the terminal doesn't understand, but the
4871 * screen will be cleared later, so this is OK.
4872 */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004873 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#ifdef FEAT_TITLE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004875 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#endif
4877#if defined(FEAT_XCLIPBOARD) && defined(FEAT_GUI)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004878 // When starting the GUI close the display opened for the clipboard.
4879 // After restoring the title, because that will need the display.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (gui.starting)
4881 clear_xterm_clip();
4882#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004883 stoptermcap(); // stop termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004885 free_termoptions();
4886}
4887
4888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004889free_termoptions(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004890{
4891 struct vimoption *p;
4892
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004893 for (p = options; p->fullname != NULL; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (istermoption(p))
4895 {
4896 if (p->flags & P_ALLOCED)
4897 free_string_option(*(char_u **)(p->var));
4898 if (p->flags & P_DEF_ALLOCED)
4899 free_string_option(p->def_val[VI_DEFAULT]);
4900 *(char_u **)(p->var) = empty_option;
4901 p->def_val[VI_DEFAULT] = empty_option;
4902 p->flags &= ~(P_ALLOCED|P_DEF_ALLOCED);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02004903#ifdef FEAT_EVAL
4904 // remember where the option was cleared
4905 set_option_sctx_idx((int)(p - options), OPT_GLOBAL, current_sctx);
4906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 clear_termcodes();
4909}
4910
4911/*
Bram Moolenaar363cb672009-07-22 12:28:17 +00004912 * Free the string for one term option, if it was allocated.
4913 * Set the string to empty_option and clear allocated flag.
4914 * "var" points to the option value.
4915 */
4916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004917free_one_termoption(char_u *var)
Bram Moolenaar363cb672009-07-22 12:28:17 +00004918{
4919 struct vimoption *p;
4920
4921 for (p = &options[0]; p->fullname != NULL; p++)
4922 if (p->var == var)
4923 {
4924 if (p->flags & P_ALLOCED)
4925 free_string_option(*(char_u **)(p->var));
4926 *(char_u **)(p->var) = empty_option;
4927 p->flags &= ~P_ALLOCED;
4928 break;
4929 }
4930}
4931
4932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 * Set the terminal option defaults to the current value.
4934 * Used after setting the terminal name.
4935 */
4936 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004937set_term_defaults(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 struct vimoption *p;
4940
4941 for (p = &options[0]; p->fullname != NULL; p++)
4942 {
4943 if (istermoption(p) && p->def_val[VI_DEFAULT] != *(char_u **)(p->var))
4944 {
4945 if (p->flags & P_DEF_ALLOCED)
4946 {
4947 free_string_option(p->def_val[VI_DEFAULT]);
4948 p->flags &= ~P_DEF_ALLOCED;
4949 }
4950 p->def_val[VI_DEFAULT] = *(char_u **)(p->var);
4951 if (p->flags & P_ALLOCED)
4952 {
4953 p->flags |= P_DEF_ALLOCED;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004954 p->flags &= ~P_ALLOCED; // don't free the value now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 }
4957 }
4958}
4959
4960/*
4961 * return TRUE if 'p' starts with 't_'
4962 */
4963 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004964istermoption(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 return (p->fullname[0] == 't' && p->fullname[1] == '_');
4967}
4968
Bram Moolenaardac13472019-09-16 21:06:21 +02004969/*
4970 * Returns TRUE if the option at 'opt_idx' starts with 't_'
4971 */
4972 int
4973istermoption_idx(int opt_idx)
4974{
4975 return istermoption(&options[opt_idx]);
4976}
4977
Bram Moolenaar113e1072019-01-20 15:30:40 +01004978#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979/*
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004980 * Unset local option value, similar to ":set opt<".
4981 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004982 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004983unset_global_local_option(char_u *name, void *from)
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004984{
4985 struct vimoption *p;
4986 int opt_idx;
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004987 buf_T *buf = (buf_T *)from;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004988
4989 opt_idx = findoption(name);
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004990 if (opt_idx < 0)
4991 return;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004992 p = &(options[opt_idx]);
4993
4994 switch ((int)p->indir)
4995 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004996 // global option with local value: use local value if it's been set
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004997 case PV_EP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02004998 clear_string_option(&buf->b_p_ep);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02004999 break;
5000 case PV_KP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005001 clear_string_option(&buf->b_p_kp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005002 break;
5003 case PV_PATH:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005004 clear_string_option(&buf->b_p_path);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005005 break;
5006 case PV_AR:
5007 buf->b_p_ar = -1;
5008 break;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005009 case PV_BKC:
5010 clear_string_option(&buf->b_p_bkc);
5011 buf->b_bkc_flags = 0;
5012 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005013 case PV_TAGS:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005014 clear_string_option(&buf->b_p_tags);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005015 break;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005016 case PV_TC:
5017 clear_string_option(&buf->b_p_tc);
5018 buf->b_tc_flags = 0;
5019 break;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005020 case PV_SISO:
5021 curwin->w_p_siso = -1;
5022 break;
5023 case PV_SO:
5024 curwin->w_p_so = -1;
5025 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005026#ifdef FEAT_FIND_ID
5027 case PV_DEF:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005028 clear_string_option(&buf->b_p_def);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005029 break;
5030 case PV_INC:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005031 clear_string_option(&buf->b_p_inc);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005032 break;
5033#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005034 case PV_DICT:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005035 clear_string_option(&buf->b_p_dict);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005036 break;
5037 case PV_TSR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005038 clear_string_option(&buf->b_p_tsr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005039 break;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005040 case PV_FP:
5041 clear_string_option(&buf->b_p_fp);
5042 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005043#ifdef FEAT_QUICKFIX
5044 case PV_EFM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005045 clear_string_option(&buf->b_p_efm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005046 break;
5047 case PV_GP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005048 clear_string_option(&buf->b_p_gp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005049 break;
5050 case PV_MP:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005051 clear_string_option(&buf->b_p_mp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005052 break;
5053#endif
5054#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5055 case PV_BEXPR:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005056 clear_string_option(&buf->b_p_bexpr);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005057 break;
5058#endif
5059#if defined(FEAT_CRYPT)
5060 case PV_CM:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005061 clear_string_option(&buf->b_p_cm);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005062 break;
5063#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005064#ifdef FEAT_LINEBREAK
5065 case PV_SBR:
5066 clear_string_option(&((win_T *)from)->w_p_sbr);
5067 break;
5068#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005069#ifdef FEAT_STL_OPT
5070 case PV_STL:
Bram Moolenaar51ac8a22013-05-06 06:45:47 +02005071 clear_string_option(&((win_T *)from)->w_p_stl);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005072 break;
5073#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005074 case PV_UL:
5075 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
5076 break;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005077#ifdef FEAT_LISP
5078 case PV_LW:
5079 clear_string_option(&buf->b_p_lw);
5080 break;
5081#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005082 case PV_MENC:
5083 clear_string_option(&buf->b_p_menc);
5084 break;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005085 case PV_LCS:
5086 clear_string_option(&((win_T *)from)->w_p_lcs);
5087 set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs);
5088 redraw_later(NOT_VALID);
5089 break;
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005090 }
5091}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005092#endif
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02005093
5094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 * Get pointer to option variable, depending on local or global scope.
5096 */
5097 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005098get_varp_scope(struct vimoption *p, int opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100 if ((opt_flags & OPT_GLOBAL) && p->indir != PV_NONE)
5101 {
5102 if (p->var == VAR_WIN)
5103 return (char_u *)GLOBAL_WO(get_varp(p));
5104 return p->var;
5105 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005106 if ((opt_flags & OPT_LOCAL) && ((int)p->indir & PV_BOTH))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 switch ((int)p->indir)
5109 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005110 case PV_FP: return (char_u *)&(curbuf->b_p_fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005112 case PV_EFM: return (char_u *)&(curbuf->b_p_efm);
5113 case PV_GP: return (char_u *)&(curbuf->b_p_gp);
5114 case PV_MP: return (char_u *)&(curbuf->b_p_mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005116 case PV_EP: return (char_u *)&(curbuf->b_p_ep);
5117 case PV_KP: return (char_u *)&(curbuf->b_p_kp);
5118 case PV_PATH: return (char_u *)&(curbuf->b_p_path);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005119 case PV_AR: return (char_u *)&(curbuf->b_p_ar);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005120 case PV_TAGS: return (char_u *)&(curbuf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005121 case PV_TC: return (char_u *)&(curbuf->b_p_tc);
Bram Moolenaar375e3392019-01-31 18:26:10 +01005122 case PV_SISO: return (char_u *)&(curwin->w_p_siso);
5123 case PV_SO: return (char_u *)&(curwin->w_p_so);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005125 case PV_DEF: return (char_u *)&(curbuf->b_p_def);
5126 case PV_INC: return (char_u *)&(curbuf->b_p_inc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005128 case PV_DICT: return (char_u *)&(curbuf->b_p_dict);
5129 case PV_TSR: return (char_u *)&(curbuf->b_p_tsr);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005130#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5131 case PV_BEXPR: return (char_u *)&(curbuf->b_p_bexpr);
5132#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005133#if defined(FEAT_CRYPT)
5134 case PV_CM: return (char_u *)&(curbuf->b_p_cm);
5135#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005136#ifdef FEAT_LINEBREAK
5137 case PV_SBR: return (char_u *)&(curwin->w_p_sbr);
5138#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005139#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005140 case PV_STL: return (char_u *)&(curwin->w_p_stl);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005141#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005142 case PV_UL: return (char_u *)&(curbuf->b_p_ul);
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005143#ifdef FEAT_LISP
5144 case PV_LW: return (char_u *)&(curbuf->b_p_lw);
5145#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005146 case PV_BKC: return (char_u *)&(curbuf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005147 case PV_MENC: return (char_u *)&(curbuf->b_p_menc);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005148 case PV_LCS: return (char_u *)&(curwin->w_p_lcs);
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005151 return NULL; // "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 return get_varp(p);
5154}
5155
5156/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005157 * Get pointer to option variable at 'opt_idx', depending on local or global
5158 * scope.
5159 */
5160 char_u *
5161get_option_varp_scope(int opt_idx, int opt_flags)
5162{
5163 return get_varp_scope(&(options[opt_idx]), opt_flags);
5164}
5165
5166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 * Get pointer to option variable.
5168 */
5169 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170get_varp(struct vimoption *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005172 // hidden option, always return NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (p->var == NULL)
5174 return NULL;
5175
5176 switch ((int)p->indir)
5177 {
5178 case PV_NONE: return p->var;
5179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005180 // global option with local value: use local value if it's been set
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005181 case PV_EP: return *curbuf->b_p_ep != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 ? (char_u *)&curbuf->b_p_ep : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005183 case PV_KP: return *curbuf->b_p_kp != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 ? (char_u *)&curbuf->b_p_kp : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005185 case PV_PATH: return *curbuf->b_p_path != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 ? (char_u *)&(curbuf->b_p_path) : p->var;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00005187 case PV_AR: return curbuf->b_p_ar >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 ? (char_u *)&(curbuf->b_p_ar) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005189 case PV_TAGS: return *curbuf->b_p_tags != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 ? (char_u *)&(curbuf->b_p_tags) : p->var;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005191 case PV_TC: return *curbuf->b_p_tc != NUL
5192 ? (char_u *)&(curbuf->b_p_tc) : p->var;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005193 case PV_BKC: return *curbuf->b_p_bkc != NUL
5194 ? (char_u *)&(curbuf->b_p_bkc) : p->var;
Bram Moolenaar375e3392019-01-31 18:26:10 +01005195 case PV_SISO: return curwin->w_p_siso >= 0
5196 ? (char_u *)&(curwin->w_p_siso) : p->var;
5197 case PV_SO: return curwin->w_p_so >= 0
5198 ? (char_u *)&(curwin->w_p_so) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199#ifdef FEAT_FIND_ID
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005200 case PV_DEF: return *curbuf->b_p_def != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 ? (char_u *)&(curbuf->b_p_def) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005202 case PV_INC: return *curbuf->b_p_inc != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 ? (char_u *)&(curbuf->b_p_inc) : p->var;
5204#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005205 case PV_DICT: return *curbuf->b_p_dict != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ? (char_u *)&(curbuf->b_p_dict) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005207 case PV_TSR: return *curbuf->b_p_tsr != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 ? (char_u *)&(curbuf->b_p_tsr) : p->var;
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005209 case PV_FP: return *curbuf->b_p_fp != NUL
5210 ? (char_u *)&(curbuf->b_p_fp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#ifdef FEAT_QUICKFIX
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005212 case PV_EFM: return *curbuf->b_p_efm != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 ? (char_u *)&(curbuf->b_p_efm) : p->var;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005214 case PV_GP: return *curbuf->b_p_gp != NUL
5215 ? (char_u *)&(curbuf->b_p_gp) : p->var;
5216 case PV_MP: return *curbuf->b_p_mp != NUL
5217 ? (char_u *)&(curbuf->b_p_mp) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005219#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5220 case PV_BEXPR: return *curbuf->b_p_bexpr != NUL
5221 ? (char_u *)&(curbuf->b_p_bexpr) : p->var;
5222#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005223#if defined(FEAT_CRYPT)
5224 case PV_CM: return *curbuf->b_p_cm != NUL
5225 ? (char_u *)&(curbuf->b_p_cm) : p->var;
5226#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005227#ifdef FEAT_LINEBREAK
5228 case PV_SBR: return *curwin->w_p_sbr != NUL
5229 ? (char_u *)&(curwin->w_p_sbr) : p->var;
5230#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005231#ifdef FEAT_STL_OPT
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005232 case PV_STL: return *curwin->w_p_stl != NUL
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005233 ? (char_u *)&(curwin->w_p_stl) : p->var;
5234#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005235 case PV_UL: return curbuf->b_p_ul != NO_LOCAL_UNDOLEVEL
5236 ? (char_u *)&(curbuf->b_p_ul) : p->var;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005237#ifdef FEAT_LISP
5238 case PV_LW: return *curbuf->b_p_lw != NUL
5239 ? (char_u *)&(curbuf->b_p_lw) : p->var;
5240#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005241 case PV_MENC: return *curbuf->b_p_menc != NUL
5242 ? (char_u *)&(curbuf->b_p_menc) : p->var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#ifdef FEAT_ARABIC
5244 case PV_ARAB: return (char_u *)&(curwin->w_p_arab);
5245#endif
5246 case PV_LIST: return (char_u *)&(curwin->w_p_list);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005247 case PV_LCS: return *curwin->w_p_lcs != NUL
5248 ? (char_u *)&(curwin->w_p_lcs) : p->var;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005249#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005250 case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
5251#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005252#ifdef FEAT_SYN_HL
5253 case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
5254 case PV_CUL: return (char_u *)&(curwin->w_p_cul);
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005255 case PV_CULOPT: return (char_u *)&(curwin->w_p_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005256 case PV_CC: return (char_u *)&(curwin->w_p_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#ifdef FEAT_DIFF
5259 case PV_DIFF: return (char_u *)&(curwin->w_p_diff);
5260#endif
5261#ifdef FEAT_FOLDING
5262 case PV_FDC: return (char_u *)&(curwin->w_p_fdc);
5263 case PV_FEN: return (char_u *)&(curwin->w_p_fen);
5264 case PV_FDI: return (char_u *)&(curwin->w_p_fdi);
5265 case PV_FDL: return (char_u *)&(curwin->w_p_fdl);
5266 case PV_FDM: return (char_u *)&(curwin->w_p_fdm);
5267 case PV_FML: return (char_u *)&(curwin->w_p_fml);
5268 case PV_FDN: return (char_u *)&(curwin->w_p_fdn);
5269# ifdef FEAT_EVAL
5270 case PV_FDE: return (char_u *)&(curwin->w_p_fde);
5271 case PV_FDT: return (char_u *)&(curwin->w_p_fdt);
5272# endif
5273 case PV_FMR: return (char_u *)&(curwin->w_p_fmr);
5274#endif
5275 case PV_NU: return (char_u *)&(curwin->w_p_nu);
Bram Moolenaar64486672010-05-16 15:46:46 +02005276 case PV_RNU: return (char_u *)&(curwin->w_p_rnu);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005277#ifdef FEAT_LINEBREAK
5278 case PV_NUW: return (char_u *)&(curwin->w_p_nuw);
5279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 case PV_WFH: return (char_u *)&(curwin->w_p_wfh);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005281 case PV_WFW: return (char_u *)&(curwin->w_p_wfw);
Bram Moolenaar4033c552017-09-16 20:54:51 +02005282#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 case PV_PVW: return (char_u *)&(curwin->w_p_pvw);
5284#endif
5285#ifdef FEAT_RIGHTLEFT
5286 case PV_RL: return (char_u *)&(curwin->w_p_rl);
5287 case PV_RLC: return (char_u *)&(curwin->w_p_rlc);
5288#endif
5289 case PV_SCROLL: return (char_u *)&(curwin->w_p_scr);
5290 case PV_WRAP: return (char_u *)&(curwin->w_p_wrap);
5291#ifdef FEAT_LINEBREAK
5292 case PV_LBR: return (char_u *)&(curwin->w_p_lbr);
Bram Moolenaar597a4222014-06-25 14:39:50 +02005293 case PV_BRI: return (char_u *)&(curwin->w_p_bri);
5294 case PV_BRIOPT: return (char_u *)&(curwin->w_p_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005296 case PV_WCR: return (char_u *)&(curwin->w_p_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 case PV_SCBIND: return (char_u *)&(curwin->w_p_scb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005298 case PV_CRBIND: return (char_u *)&(curwin->w_p_crb);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005299#ifdef FEAT_CONCEAL
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005300 case PV_COCU: return (char_u *)&(curwin->w_p_cocu);
5301 case PV_COLE: return (char_u *)&(curwin->w_p_cole);
5302#endif
5303#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005304 case PV_TWK: return (char_u *)&(curwin->w_p_twk);
5305 case PV_TWS: return (char_u *)&(curwin->w_p_tws);
5306 case PV_TWSL: return (char_u *)&(curbuf->b_p_twsl);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309 case PV_AI: return (char_u *)&(curbuf->b_p_ai);
5310 case PV_BIN: return (char_u *)&(curbuf->b_p_bin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 case PV_BOMB: return (char_u *)&(curbuf->b_p_bomb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 case PV_BH: return (char_u *)&(curbuf->b_p_bh);
5313 case PV_BT: return (char_u *)&(curbuf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 case PV_BL: return (char_u *)&(curbuf->b_p_bl);
5315 case PV_CI: return (char_u *)&(curbuf->b_p_ci);
5316#ifdef FEAT_CINDENT
5317 case PV_CIN: return (char_u *)&(curbuf->b_p_cin);
5318 case PV_CINK: return (char_u *)&(curbuf->b_p_cink);
5319 case PV_CINO: return (char_u *)&(curbuf->b_p_cino);
5320#endif
5321#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5322 case PV_CINW: return (char_u *)&(curbuf->b_p_cinw);
5323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 case PV_COM: return (char_u *)&(curbuf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#ifdef FEAT_FOLDING
5326 case PV_CMS: return (char_u *)&(curbuf->b_p_cms);
5327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 case PV_CPT: return (char_u *)&(curbuf->b_p_cpt);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005329#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005330 case PV_CSL: return (char_u *)&(curbuf->b_p_csl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005332#ifdef FEAT_COMPL_FUNC
5333 case PV_CFU: return (char_u *)&(curbuf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005334 case PV_OFU: return (char_u *)&(curbuf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005335#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005336#ifdef FEAT_EVAL
5337 case PV_TFU: return (char_u *)&(curbuf->b_p_tfu);
5338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 case PV_EOL: return (char_u *)&(curbuf->b_p_eol);
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005340 case PV_FIXEOL: return (char_u *)&(curbuf->b_p_fixeol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 case PV_ET: return (char_u *)&(curbuf->b_p_et);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 case PV_FENC: return (char_u *)&(curbuf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 case PV_FF: return (char_u *)&(curbuf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 case PV_FT: return (char_u *)&(curbuf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 case PV_FO: return (char_u *)&(curbuf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005346 case PV_FLP: return (char_u *)&(curbuf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 case PV_IMI: return (char_u *)&(curbuf->b_p_iminsert);
5348 case PV_IMS: return (char_u *)&(curbuf->b_p_imsearch);
5349 case PV_INF: return (char_u *)&(curbuf->b_p_inf);
5350 case PV_ISK: return (char_u *)&(curbuf->b_p_isk);
5351#ifdef FEAT_FIND_ID
5352# ifdef FEAT_EVAL
5353 case PV_INEX: return (char_u *)&(curbuf->b_p_inex);
5354# endif
5355#endif
5356#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5357 case PV_INDE: return (char_u *)&(curbuf->b_p_inde);
5358 case PV_INDK: return (char_u *)&(curbuf->b_p_indk);
5359#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005360#ifdef FEAT_EVAL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005361 case PV_FEX: return (char_u *)&(curbuf->b_p_fex);
5362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#ifdef FEAT_CRYPT
5364 case PV_KEY: return (char_u *)&(curbuf->b_p_key);
5365#endif
5366#ifdef FEAT_LISP
5367 case PV_LISP: return (char_u *)&(curbuf->b_p_lisp);
5368#endif
5369 case PV_ML: return (char_u *)&(curbuf->b_p_ml);
5370 case PV_MPS: return (char_u *)&(curbuf->b_p_mps);
5371 case PV_MA: return (char_u *)&(curbuf->b_p_ma);
5372 case PV_MOD: return (char_u *)&(curbuf->b_changed);
5373 case PV_NF: return (char_u *)&(curbuf->b_p_nf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 case PV_PI: return (char_u *)&(curbuf->b_p_pi);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005375#ifdef FEAT_TEXTOBJ
5376 case PV_QE: return (char_u *)&(curbuf->b_p_qe);
5377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 case PV_RO: return (char_u *)&(curbuf->b_p_ro);
5379#ifdef FEAT_SMARTINDENT
5380 case PV_SI: return (char_u *)&(curbuf->b_p_si);
5381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 case PV_SN: return (char_u *)&(curbuf->b_p_sn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 case PV_STS: return (char_u *)&(curbuf->b_p_sts);
5384#ifdef FEAT_SEARCHPATH
5385 case PV_SUA: return (char_u *)&(curbuf->b_p_sua);
5386#endif
5387 case PV_SWF: return (char_u *)&(curbuf->b_p_swf);
5388#ifdef FEAT_SYN_HL
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005389 case PV_SMC: return (char_u *)&(curbuf->b_p_smc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005390 case PV_SYN: return (char_u *)&(curbuf->b_p_syn);
5391#endif
5392#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02005393 case PV_SPC: return (char_u *)&(curwin->w_s->b_p_spc);
5394 case PV_SPF: return (char_u *)&(curwin->w_s->b_p_spf);
5395 case PV_SPL: return (char_u *)&(curwin->w_s->b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005396 case PV_SPO: return (char_u *)&(curwin->w_s->b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif
5398 case PV_SW: return (char_u *)&(curbuf->b_p_sw);
5399 case PV_TS: return (char_u *)&(curbuf->b_p_ts);
5400 case PV_TW: return (char_u *)&(curbuf->b_p_tw);
5401 case PV_TX: return (char_u *)&(curbuf->b_p_tx);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005402#ifdef FEAT_PERSISTENT_UNDO
5403 case PV_UDF: return (char_u *)&(curbuf->b_p_udf);
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 case PV_WM: return (char_u *)&(curbuf->b_p_wm);
5406#ifdef FEAT_KEYMAP
5407 case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap);
5408#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005409#ifdef FEAT_SIGNS
5410 case PV_SCL: return (char_u *)&(curwin->w_p_scl);
5411#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005412#ifdef FEAT_VARTABS
5413 case PV_VSTS: return (char_u *)&(curbuf->b_p_vsts);
5414 case PV_VTS: return (char_u *)&(curbuf->b_p_vts);
5415#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005416 default: iemsg(_("E356: get_varp ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005418 // always return a valid pointer to avoid a crash!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 return (char_u *)&(curbuf->b_p_wm);
5420}
5421
5422/*
Bram Moolenaardac13472019-09-16 21:06:21 +02005423 * Return a pointer to the variable for option at 'opt_idx'
5424 */
5425 char_u *
5426get_option_var(int opt_idx)
5427{
5428 return options[opt_idx].var;
5429}
5430
5431/*
5432 * Return the full name of the option at 'opt_idx'
5433 */
5434 char_u *
5435get_option_fullname(int opt_idx)
5436{
5437 return (char_u *)options[opt_idx].fullname;
5438}
5439
5440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 * Get the value of 'equalprg', either the buffer-local one or the global one.
5442 */
5443 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005444get_equalprg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 if (*curbuf->b_p_ep == NUL)
5447 return p_ep;
5448 return curbuf->b_p_ep;
5449}
5450
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451/*
5452 * Copy options from one window to another.
5453 * Used when splitting a window.
5454 */
5455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005456win_copy_options(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
5458 copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt);
5459 copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt);
Bram Moolenaar010ee962019-09-25 20:37:36 +02005460 after_copy_winopt(wp_to);
5461}
5462
5463/*
5464 * After copying window options: update variables depending on options.
5465 */
5466 void
Bram Moolenaar473952e2019-09-28 16:30:04 +02005467after_copy_winopt(win_T *wp UNUSED)
Bram Moolenaar010ee962019-09-25 20:37:36 +02005468{
5469#ifdef FEAT_LINEBREAK
5470 briopt_check(wp);
Bram Moolenaar285ed7e2014-08-24 21:39:49 +02005471#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02005472#ifdef FEAT_SYN_HL
Bram Moolenaar010ee962019-09-25 20:37:36 +02005473 fill_culopt_flags(NULL, wp);
5474 check_colorcolumn(wp);
Bram Moolenaar017ba072019-09-14 21:01:23 +02005475#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005476 set_chars_option(wp, &wp->w_p_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479/*
5480 * Copy the options from one winopt_T to another.
5481 * Doesn't free the old option values in "to", use clear_winopt() for that.
5482 * The 'scroll' option is not copied, because it depends on the window height.
5483 * The 'previewwindow' option is reset, there can be only one preview window.
5484 */
5485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005486copy_winopt(winopt_T *from, winopt_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487{
5488#ifdef FEAT_ARABIC
5489 to->wo_arab = from->wo_arab;
5490#endif
5491 to->wo_list = from->wo_list;
Bram Moolenaareed9d462021-02-15 20:38:25 +01005492 to->wo_lcs = vim_strsave(from->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 to->wo_nu = from->wo_nu;
Bram Moolenaar64486672010-05-16 15:46:46 +02005494 to->wo_rnu = from->wo_rnu;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005495#ifdef FEAT_LINEBREAK
5496 to->wo_nuw = from->wo_nuw;
5497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498#ifdef FEAT_RIGHTLEFT
5499 to->wo_rl = from->wo_rl;
5500 to->wo_rlc = vim_strsave(from->wo_rlc);
5501#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005502#ifdef FEAT_LINEBREAK
5503 to->wo_sbr = vim_strsave(from->wo_sbr);
5504#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005505#ifdef FEAT_STL_OPT
5506 to->wo_stl = vim_strsave(from->wo_stl);
5507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 to->wo_wrap = from->wo_wrap;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005509#ifdef FEAT_DIFF
5510 to->wo_wrap_save = from->wo_wrap_save;
5511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#ifdef FEAT_LINEBREAK
5513 to->wo_lbr = from->wo_lbr;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005514 to->wo_bri = from->wo_bri;
5515 to->wo_briopt = vim_strsave(from->wo_briopt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005517 to->wo_wcr = vim_strsave(from->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 to->wo_scb = from->wo_scb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005519 to->wo_scb_save = from->wo_scb_save;
Bram Moolenaar4161dcc2010-12-02 15:33:21 +01005520 to->wo_crb = from->wo_crb;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005521 to->wo_crb_save = from->wo_crb_save;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005522#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005523 to->wo_spell = from->wo_spell;
5524#endif
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005525#ifdef FEAT_SYN_HL
5526 to->wo_cuc = from->wo_cuc;
5527 to->wo_cul = from->wo_cul;
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005528 to->wo_culopt = vim_strsave(from->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005529 to->wo_cc = vim_strsave(from->wo_cc);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531#ifdef FEAT_DIFF
5532 to->wo_diff = from->wo_diff;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005533 to->wo_diff_saved = from->wo_diff_saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005535#ifdef FEAT_CONCEAL
5536 to->wo_cocu = vim_strsave(from->wo_cocu);
Bram Moolenaard497a302010-07-23 22:27:03 +02005537 to->wo_cole = from->wo_cole;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005538#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005539#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005540 to->wo_twk = vim_strsave(from->wo_twk);
5541 to->wo_tws = vim_strsave(from->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#ifdef FEAT_FOLDING
5544 to->wo_fdc = from->wo_fdc;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005545 to->wo_fdc_save = from->wo_fdc_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 to->wo_fen = from->wo_fen;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005547 to->wo_fen_save = from->wo_fen_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 to->wo_fdi = vim_strsave(from->wo_fdi);
5549 to->wo_fml = from->wo_fml;
5550 to->wo_fdl = from->wo_fdl;
Bram Moolenaara87aa802013-07-03 15:47:03 +02005551 to->wo_fdl_save = from->wo_fdl_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 to->wo_fdm = vim_strsave(from->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005553 to->wo_fdm_save = from->wo_diff_saved
5554 ? vim_strsave(from->wo_fdm_save) : empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 to->wo_fdn = from->wo_fdn;
5556# ifdef FEAT_EVAL
5557 to->wo_fde = vim_strsave(from->wo_fde);
5558 to->wo_fdt = vim_strsave(from->wo_fdt);
5559# endif
5560 to->wo_fmr = vim_strsave(from->wo_fmr);
5561#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005562#ifdef FEAT_SIGNS
5563 to->wo_scl = vim_strsave(from->wo_scl);
5564#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005565
5566#ifdef FEAT_EVAL
5567 // Copy the script context so that we know where the value was last set.
5568 mch_memmove(to->wo_script_ctx, from->wo_script_ctx,
5569 sizeof(to->wo_script_ctx));
5570#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005571 check_winopt(to); // don't want NULL pointers
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572}
5573
5574/*
5575 * Check string options in a window for a NULL value.
5576 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578check_win_options(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 check_winopt(&win->w_onebuf_opt);
5581 check_winopt(&win->w_allbuf_opt);
5582}
5583
5584/*
5585 * Check for NULL pointers in a winopt_T and replace them with empty_option.
5586 */
Bram Moolenaar8dc907d2014-06-25 14:44:10 +02005587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588check_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590#ifdef FEAT_FOLDING
5591 check_string_option(&wop->wo_fdi);
5592 check_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005593 check_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594# ifdef FEAT_EVAL
5595 check_string_option(&wop->wo_fde);
5596 check_string_option(&wop->wo_fdt);
5597# endif
5598 check_string_option(&wop->wo_fmr);
5599#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005600#ifdef FEAT_SIGNS
5601 check_string_option(&wop->wo_scl);
5602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603#ifdef FEAT_RIGHTLEFT
5604 check_string_option(&wop->wo_rlc);
5605#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005606#ifdef FEAT_LINEBREAK
5607 check_string_option(&wop->wo_sbr);
5608#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005609#ifdef FEAT_STL_OPT
5610 check_string_option(&wop->wo_stl);
5611#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005613 check_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005614 check_string_option(&wop->wo_cc);
5615#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005616#ifdef FEAT_CONCEAL
5617 check_string_option(&wop->wo_cocu);
5618#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005619#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005620 check_string_option(&wop->wo_twk);
5621 check_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005622#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005623#ifdef FEAT_LINEBREAK
5624 check_string_option(&wop->wo_briopt);
5625#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005626 check_string_option(&wop->wo_wcr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01005627 check_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628}
5629
5630/*
5631 * Free the allocated memory inside a winopt_T.
5632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005634clear_winopt(winopt_T *wop UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636#ifdef FEAT_FOLDING
5637 clear_string_option(&wop->wo_fdi);
5638 clear_string_option(&wop->wo_fdm);
Bram Moolenaara87aa802013-07-03 15:47:03 +02005639 clear_string_option(&wop->wo_fdm_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640# ifdef FEAT_EVAL
5641 clear_string_option(&wop->wo_fde);
5642 clear_string_option(&wop->wo_fdt);
5643# endif
5644 clear_string_option(&wop->wo_fmr);
5645#endif
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02005646#ifdef FEAT_SIGNS
5647 clear_string_option(&wop->wo_scl);
5648#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02005649#ifdef FEAT_LINEBREAK
5650 clear_string_option(&wop->wo_briopt);
5651#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005652 clear_string_option(&wop->wo_wcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653#ifdef FEAT_RIGHTLEFT
5654 clear_string_option(&wop->wo_rlc);
5655#endif
Bram Moolenaaree857022019-11-09 23:26:40 +01005656#ifdef FEAT_LINEBREAK
5657 clear_string_option(&wop->wo_sbr);
5658#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005659#ifdef FEAT_STL_OPT
5660 clear_string_option(&wop->wo_stl);
5661#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005662#ifdef FEAT_SYN_HL
Bram Moolenaar410e98a2019-09-09 22:05:49 +02005663 clear_string_option(&wop->wo_culopt);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005664 clear_string_option(&wop->wo_cc);
5665#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005666#ifdef FEAT_CONCEAL
5667 clear_string_option(&wop->wo_cocu);
5668#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005669#ifdef FEAT_TERMINAL
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005670 clear_string_option(&wop->wo_twk);
5671 clear_string_option(&wop->wo_tws);
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005672#endif
Bram Moolenaareed9d462021-02-15 20:38:25 +01005673 clear_string_option(&wop->wo_lcs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674}
5675
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005676#ifdef FEAT_EVAL
5677// Index into the options table for a buffer-local option enum.
5678static int buf_opt_idx[BV_COUNT];
5679# define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].script_ctx
5680
5681/*
5682 * Initialize buf_opt_idx[] if not done already.
5683 */
5684 static void
5685init_buf_opt_idx(void)
5686{
5687 static int did_init_buf_opt_idx = FALSE;
5688 int i;
5689
5690 if (did_init_buf_opt_idx)
5691 return;
5692 did_init_buf_opt_idx = TRUE;
5693 for (i = 0; !istermoption_idx(i); i++)
5694 if (options[i].indir & PV_BUF)
5695 buf_opt_idx[options[i].indir & PV_MASK] = i;
5696}
5697#else
5698# define COPY_OPT_SCTX(buf, bv)
5699#endif
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701/*
5702 * Copy global option values to local options for one buffer.
5703 * Used when creating a new buffer and sometimes when entering a buffer.
5704 * flags:
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005705 * BCO_ENTER We will enter the buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 * BCO_ALWAYS Always copy the options, but only set b_p_initialized when
5707 * appropriate.
5708 * BCO_NOHELP Don't copy the values to a help buffer.
5709 */
5710 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005711buf_copy_options(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 int should_copy = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005714 char_u *save_p_isk = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 int dont_do_help;
5716 int did_isk = FALSE;
5717
5718 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 * Skip this when the option defaults have not been set yet. Happens when
5720 * main() allocates the first buffer.
5721 */
5722 if (p_cpo != NULL)
5723 {
5724 /*
5725 * Always copy when entering and 'cpo' contains 'S'.
5726 * Don't copy when already initialized.
5727 * Don't copy when 'cpo' contains 's' and not entering.
5728 * 'S' BCO_ENTER initialized 's' should_copy
5729 * yes yes X X TRUE
5730 * yes no yes X FALSE
5731 * no X yes X FALSE
5732 * X no no yes FALSE
5733 * X no no no TRUE
5734 * no yes no X TRUE
5735 */
5736 if ((vim_strchr(p_cpo, CPO_BUFOPTGLOB) == NULL || !(flags & BCO_ENTER))
5737 && (buf->b_p_initialized
5738 || (!(flags & BCO_ENTER)
5739 && vim_strchr(p_cpo, CPO_BUFOPT) != NULL)))
5740 should_copy = FALSE;
5741
5742 if (should_copy || (flags & BCO_ALWAYS))
5743 {
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005744#ifdef FEAT_EVAL
Bram Moolenaara80faa82020-04-12 19:37:17 +02005745 CLEAR_FIELD(buf->b_p_script_ctx);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005746 init_buf_opt_idx();
5747#endif
5748 // Don't copy the options specific to a help buffer when
5749 // BCO_NOHELP is given or the options were initialized already
5750 // (jumping back to a help file with CTRL-T or CTRL-O)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 dont_do_help = ((flags & BCO_NOHELP) && buf->b_help)
5752 || buf->b_p_initialized;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005753 if (dont_do_help) // don't free b_p_isk
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 save_p_isk = buf->b_p_isk;
5756 buf->b_p_isk = NULL;
5757 }
5758 /*
Bram Moolenaar40385db2018-08-07 22:31:44 +02005759 * Always free the allocated strings. If not already initialized,
5760 * reset 'readonly' and copy 'fileformat'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 */
5762 if (!buf->b_p_initialized)
5763 {
5764 free_buf_options(buf, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005765 buf->b_p_ro = FALSE; // don't copy readonly
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 buf->b_p_tx = p_tx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 buf->b_p_fenc = vim_strsave(p_fenc);
Bram Moolenaare8ef3a02016-10-12 17:45:29 +02005768 switch (*p_ffs)
5769 {
5770 case 'm':
5771 buf->b_p_ff = vim_strsave((char_u *)FF_MAC); break;
5772 case 'd':
5773 buf->b_p_ff = vim_strsave((char_u *)FF_DOS); break;
5774 case 'u':
5775 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); break;
5776 default:
5777 buf->b_p_ff = vim_strsave(p_ff);
5778 }
5779 if (buf->b_p_ff != NULL)
5780 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 buf->b_p_bh = empty_option;
5782 buf->b_p_bt = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784 else
5785 free_buf_options(buf, FALSE);
5786
5787 buf->b_p_ai = p_ai;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005788 COPY_OPT_SCTX(buf, BV_AI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 buf->b_p_ai_nopaste = p_ai_nopaste;
5790 buf->b_p_sw = p_sw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005791 COPY_OPT_SCTX(buf, BV_SW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 buf->b_p_tw = p_tw;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005793 COPY_OPT_SCTX(buf, BV_TW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 buf->b_p_tw_nopaste = p_tw_nopaste;
5795 buf->b_p_tw_nobin = p_tw_nobin;
5796 buf->b_p_wm = p_wm;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005797 COPY_OPT_SCTX(buf, BV_WM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 buf->b_p_wm_nopaste = p_wm_nopaste;
5799 buf->b_p_wm_nobin = p_wm_nobin;
5800 buf->b_p_bin = p_bin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005801 COPY_OPT_SCTX(buf, BV_BIN);
Bram Moolenaare8bb2552005-07-08 22:26:47 +00005802 buf->b_p_bomb = p_bomb;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005803 COPY_OPT_SCTX(buf, BV_BOMB);
Bram Moolenaarb388be02015-07-22 22:19:38 +02005804 buf->b_p_fixeol = p_fixeol;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005805 COPY_OPT_SCTX(buf, BV_FIXEOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 buf->b_p_et = p_et;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005807 COPY_OPT_SCTX(buf, BV_ET);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 buf->b_p_et_nobin = p_et_nobin;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02005809 buf->b_p_et_nopaste = p_et_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 buf->b_p_ml = p_ml;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005811 COPY_OPT_SCTX(buf, BV_ML);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 buf->b_p_ml_nobin = p_ml_nobin;
5813 buf->b_p_inf = p_inf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005814 COPY_OPT_SCTX(buf, BV_INF);
Bram Moolenaare1004402020-10-24 20:49:43 +02005815 if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005816 buf->b_p_swf = FALSE;
5817 else
5818 {
5819 buf->b_p_swf = p_swf;
5820 COPY_OPT_SCTX(buf, BV_INF);
5821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 buf->b_p_cpt = vim_strsave(p_cpt);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005823 COPY_OPT_SCTX(buf, BV_CPT);
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005824#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarac3150d2019-07-28 16:36:39 +02005825 buf->b_p_csl = vim_strsave(p_csl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005826 COPY_OPT_SCTX(buf, BV_CSL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005828#ifdef FEAT_COMPL_FUNC
5829 buf->b_p_cfu = vim_strsave(p_cfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005830 COPY_OPT_SCTX(buf, BV_CFU);
Bram Moolenaare344bea2005-09-01 20:46:49 +00005831 buf->b_p_ofu = vim_strsave(p_ofu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005832 COPY_OPT_SCTX(buf, BV_OFU);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005833#endif
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005834#ifdef FEAT_EVAL
5835 buf->b_p_tfu = vim_strsave(p_tfu);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005836 COPY_OPT_SCTX(buf, BV_TFU);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 buf->b_p_sts = p_sts;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005839 COPY_OPT_SCTX(buf, BV_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 buf->b_p_sts_nopaste = p_sts_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005841#ifdef FEAT_VARTABS
5842 buf->b_p_vsts = vim_strsave(p_vsts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005843 COPY_OPT_SCTX(buf, BV_VSTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005844 if (p_vsts && p_vsts != empty_option)
5845 tabstop_set(p_vsts, &buf->b_p_vsts_array);
5846 else
5847 buf->b_p_vsts_array = 0;
5848 buf->b_p_vsts_nopaste = p_vsts_nopaste
5849 ? vim_strsave(p_vsts_nopaste) : NULL;
5850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 buf->b_p_sn = p_sn;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005852 COPY_OPT_SCTX(buf, BV_SN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 buf->b_p_com = vim_strsave(p_com);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005854 COPY_OPT_SCTX(buf, BV_COM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#ifdef FEAT_FOLDING
5856 buf->b_p_cms = vim_strsave(p_cms);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005857 COPY_OPT_SCTX(buf, BV_CMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858#endif
5859 buf->b_p_fo = vim_strsave(p_fo);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005860 COPY_OPT_SCTX(buf, BV_FO);
Bram Moolenaar86b68352004-12-27 21:59:20 +00005861 buf->b_p_flp = vim_strsave(p_flp);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005862 COPY_OPT_SCTX(buf, BV_FLP);
Bram Moolenaar473952e2019-09-28 16:30:04 +02005863 // NOTE: Valgrind may report a bogus memory leak for 'nrformats'
5864 // when it is set to 8 bytes in defaults.vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 buf->b_p_nf = vim_strsave(p_nf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005866 COPY_OPT_SCTX(buf, BV_NF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 buf->b_p_mps = vim_strsave(p_mps);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005868 COPY_OPT_SCTX(buf, BV_MPS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#ifdef FEAT_SMARTINDENT
5870 buf->b_p_si = p_si;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005871 COPY_OPT_SCTX(buf, BV_SI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#endif
5873 buf->b_p_ci = p_ci;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005874 COPY_OPT_SCTX(buf, BV_CI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875#ifdef FEAT_CINDENT
5876 buf->b_p_cin = p_cin;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005877 COPY_OPT_SCTX(buf, BV_CIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 buf->b_p_cink = vim_strsave(p_cink);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005879 COPY_OPT_SCTX(buf, BV_CINK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 buf->b_p_cino = vim_strsave(p_cino);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005881 COPY_OPT_SCTX(buf, BV_CINO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882#endif
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005883 // Don't copy 'filetype', it must be detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 buf->b_p_ft = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 buf->b_p_pi = p_pi;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005886 COPY_OPT_SCTX(buf, BV_PI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
5888 buf->b_p_cinw = vim_strsave(p_cinw);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005889 COPY_OPT_SCTX(buf, BV_CINW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
5891#ifdef FEAT_LISP
5892 buf->b_p_lisp = p_lisp;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005893 COPY_OPT_SCTX(buf, BV_LISP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894#endif
5895#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005896 // Don't copy 'syntax', it must be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 buf->b_p_syn = empty_option;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005898 buf->b_p_smc = p_smc;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005899 COPY_OPT_SCTX(buf, BV_SMC);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01005900 buf->b_s.b_syn_isk = empty_option;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005901#endif
5902#ifdef FEAT_SPELL
Bram Moolenaard5784f92010-10-13 14:05:35 +02005903 buf->b_s.b_p_spc = vim_strsave(p_spc);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005904 COPY_OPT_SCTX(buf, BV_SPC);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005905 (void)compile_cap_prog(&buf->b_s);
5906 buf->b_s.b_p_spf = vim_strsave(p_spf);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005907 COPY_OPT_SCTX(buf, BV_SPF);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005908 buf->b_s.b_p_spl = vim_strsave(p_spl);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005909 COPY_OPT_SCTX(buf, BV_SPL);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02005910 buf->b_s.b_p_spo = vim_strsave(p_spo);
5911 COPY_OPT_SCTX(buf, BV_SPO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912#endif
5913#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
5914 buf->b_p_inde = vim_strsave(p_inde);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005915 COPY_OPT_SCTX(buf, BV_INDE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 buf->b_p_indk = vim_strsave(p_indk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005917 COPY_OPT_SCTX(buf, BV_INDK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01005919 buf->b_p_fp = empty_option;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005920#if defined(FEAT_EVAL)
5921 buf->b_p_fex = vim_strsave(p_fex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005922 COPY_OPT_SCTX(buf, BV_FEX);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924#ifdef FEAT_CRYPT
5925 buf->b_p_key = vim_strsave(p_key);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005926 COPY_OPT_SCTX(buf, BV_KEY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#endif
5928#ifdef FEAT_SEARCHPATH
5929 buf->b_p_sua = vim_strsave(p_sua);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005930 COPY_OPT_SCTX(buf, BV_SUA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931#endif
5932#ifdef FEAT_KEYMAP
5933 buf->b_p_keymap = vim_strsave(p_keymap);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005934 COPY_OPT_SCTX(buf, BV_KMAP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf->b_kmap_state |= KEYMAP_INIT;
5936#endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005937#ifdef FEAT_TERMINAL
5938 buf->b_p_twsl = p_twsl;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005939 COPY_OPT_SCTX(buf, BV_TWSL);
Bram Moolenaar6d150f72018-04-21 20:03:20 +02005940#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005941 // This isn't really an option, but copying the langmap and IME
5942 // state from the current buffer is better than resetting it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 buf->b_p_iminsert = p_iminsert;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005944 COPY_OPT_SCTX(buf, BV_IMI);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 buf->b_p_imsearch = p_imsearch;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005946 COPY_OPT_SCTX(buf, BV_IMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005948 // options that are normally global but also have a local value
5949 // are not copied, start using the global value
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01005951 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02005952 buf->b_p_bkc = empty_option;
5953 buf->b_bkc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#ifdef FEAT_QUICKFIX
5955 buf->b_p_gp = empty_option;
5956 buf->b_p_mp = empty_option;
5957 buf->b_p_efm = empty_option;
5958#endif
5959 buf->b_p_ep = empty_option;
5960 buf->b_p_kp = empty_option;
5961 buf->b_p_path = empty_option;
5962 buf->b_p_tags = empty_option;
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01005963 buf->b_p_tc = empty_option;
5964 buf->b_tc_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965#ifdef FEAT_FIND_ID
5966 buf->b_p_def = empty_option;
5967 buf->b_p_inc = empty_option;
5968# ifdef FEAT_EVAL
5969 buf->b_p_inex = vim_strsave(p_inex);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005970 COPY_OPT_SCTX(buf, BV_INEX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971# endif
5972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 buf->b_p_dict = empty_option;
5974 buf->b_p_tsr = empty_option;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005975#ifdef FEAT_TEXTOBJ
5976 buf->b_p_qe = vim_strsave(p_qe);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005977 COPY_OPT_SCTX(buf, BV_QE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005978#endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00005979#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
5980 buf->b_p_bexpr = empty_option;
5981#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02005982#if defined(FEAT_CRYPT)
5983 buf->b_p_cm = empty_option;
5984#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005985#ifdef FEAT_PERSISTENT_UNDO
5986 buf->b_p_udf = p_udf;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02005987 COPY_OPT_SCTX(buf, BV_UDF);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005988#endif
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01005989#ifdef FEAT_LISP
5990 buf->b_p_lw = empty_option;
5991#endif
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01005992 buf->b_p_menc = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993
5994 /*
5995 * Don't copy the options set by ex_help(), use the saved values,
5996 * when going from a help buffer to a non-help buffer.
5997 * Don't touch these at all when BCO_NOHELP is used and going from
5998 * or to a help buffer.
5999 */
6000 if (dont_do_help)
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 buf->b_p_isk = save_p_isk;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006003#ifdef FEAT_VARTABS
6004 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6005 tabstop_set(p_vts, &buf->b_p_vts_array);
6006 else
6007 buf->b_p_vts_array = NULL;
6008#endif
6009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 else
6011 {
6012 buf->b_p_isk = vim_strsave(p_isk);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006013 COPY_OPT_SCTX(buf, BV_ISK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 did_isk = TRUE;
6015 buf->b_p_ts = p_ts;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006016#ifdef FEAT_VARTABS
6017 buf->b_p_vts = vim_strsave(p_vts);
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006018 COPY_OPT_SCTX(buf, BV_VTS);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006019 if (p_vts && p_vts != empty_option && !buf->b_p_vts_array)
6020 tabstop_set(p_vts, &buf->b_p_vts_array);
6021 else
6022 buf->b_p_vts_array = NULL;
6023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 buf->b_help = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 if (buf->b_p_bt[0] == 'h')
6026 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 buf->b_p_ma = p_ma;
Bram Moolenaarcfb38142019-10-19 20:18:47 +02006028 COPY_OPT_SCTX(buf, BV_MA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 }
6030 }
6031
6032 /*
6033 * When the options should be copied (ignoring BCO_ALWAYS), set the
6034 * flag that indicates that the options have been initialized.
6035 */
6036 if (should_copy)
6037 buf->b_p_initialized = TRUE;
6038 }
6039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006040 check_buf_options(buf); // make sure we don't have NULLs
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (did_isk)
6042 (void)buf_init_chartab(buf, FALSE);
6043}
6044
6045/*
6046 * Reset the 'modifiable' option and its default value.
6047 */
6048 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006049reset_modifiable(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 int opt_idx;
6052
6053 curbuf->b_p_ma = FALSE;
6054 p_ma = FALSE;
6055 opt_idx = findoption((char_u *)"ma");
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006056 if (opt_idx >= 0)
6057 options[opt_idx].def_val[VI_DEFAULT] = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058}
6059
6060/*
6061 * Set the global value for 'iminsert' to the local value.
6062 */
6063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006064set_iminsert_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
6066 p_iminsert = curbuf->b_p_iminsert;
6067}
6068
6069/*
6070 * Set the global value for 'imsearch' to the local value.
6071 */
6072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006073set_imsearch_global(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
6075 p_imsearch = curbuf->b_p_imsearch;
6076}
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078static int expand_option_idx = -1;
6079static char_u expand_option_name[5] = {'t', '_', NUL, NUL, NUL};
6080static int expand_option_flags = 0;
6081
6082 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006083set_context_in_set_cmd(
6084 expand_T *xp,
6085 char_u *arg,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006086 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
6088 int nextchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006089 long_u flags = 0; // init for GCC
6090 int opt_idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 char_u *p;
6092 char_u *s;
6093 int is_term_option = FALSE;
6094 int key;
6095
6096 expand_option_flags = opt_flags;
6097
6098 xp->xp_context = EXPAND_SETTINGS;
6099 if (*arg == NUL)
6100 {
6101 xp->xp_pattern = arg;
6102 return;
6103 }
6104 p = arg + STRLEN(arg) - 1;
6105 if (*p == ' ' && *(p - 1) != '\\')
6106 {
6107 xp->xp_pattern = p + 1;
6108 return;
6109 }
6110 while (p > arg)
6111 {
6112 s = p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006113 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 if (*p == ' ' || *p == ',')
6115 {
6116 while (s > arg && *(s - 1) == '\\')
6117 --s;
6118 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006119 // break at a space with an even number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (*p == ' ' && ((p - s) & 1) == 0)
6121 {
6122 ++p;
6123 break;
6124 }
6125 --p;
6126 }
Bram Moolenaar2a7b9ee2009-06-16 15:50:33 +00006127 if (STRNCMP(p, "no", 2) == 0 && STRNCMP(p, "novice", 6) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 xp->xp_context = EXPAND_BOOL_SETTINGS;
6130 p += 2;
6131 }
6132 if (STRNCMP(p, "inv", 3) == 0)
6133 {
6134 xp->xp_context = EXPAND_BOOL_SETTINGS;
6135 p += 3;
6136 }
6137 xp->xp_pattern = arg = p;
6138 if (*arg == '<')
6139 {
6140 while (*p != '>')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006141 if (*p++ == NUL) // expand terminal option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 return;
6143 key = get_special_key_code(arg + 1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006144 if (key == 0) // unknown name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 xp->xp_context = EXPAND_NOTHING;
6147 return;
6148 }
6149 nextchar = *++p;
6150 is_term_option = TRUE;
6151 expand_option_name[2] = KEY2TERMCAP0(key);
6152 expand_option_name[3] = KEY2TERMCAP1(key);
6153 }
6154 else
6155 {
6156 if (p[0] == 't' && p[1] == '_')
6157 {
6158 p += 2;
6159 if (*p != NUL)
6160 ++p;
6161 if (*p == NUL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006162 return; // expand option name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 nextchar = *++p;
6164 is_term_option = TRUE;
6165 expand_option_name[2] = p[-2];
6166 expand_option_name[3] = p[-1];
6167 }
6168 else
6169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006170 // Allow * wildcard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*')
6172 p++;
6173 if (*p == NUL)
6174 return;
6175 nextchar = *p;
6176 *p = NUL;
6177 opt_idx = findoption(arg);
6178 *p = nextchar;
6179 if (opt_idx == -1 || options[opt_idx].var == NULL)
6180 {
6181 xp->xp_context = EXPAND_NOTHING;
6182 return;
6183 }
6184 flags = options[opt_idx].flags;
6185 if (flags & P_BOOL)
6186 {
6187 xp->xp_context = EXPAND_NOTHING;
6188 return;
6189 }
6190 }
6191 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006192 // handle "-=" and "+="
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=')
6194 {
6195 ++p;
6196 nextchar = '=';
6197 }
6198 if ((nextchar != '=' && nextchar != ':')
6199 || xp->xp_context == EXPAND_BOOL_SETTINGS)
6200 {
6201 xp->xp_context = EXPAND_UNSUCCESSFUL;
6202 return;
6203 }
6204 if (xp->xp_context != EXPAND_BOOL_SETTINGS && p[1] == NUL)
6205 {
6206 xp->xp_context = EXPAND_OLD_SETTING;
6207 if (is_term_option)
6208 expand_option_idx = -1;
6209 else
6210 expand_option_idx = opt_idx;
6211 xp->xp_pattern = p + 1;
6212 return;
6213 }
6214 xp->xp_context = EXPAND_NOTHING;
6215 if (is_term_option || (flags & P_NUM))
6216 return;
6217
6218 xp->xp_pattern = p + 1;
6219
6220 if (flags & P_EXPAND)
6221 {
6222 p = options[opt_idx].var;
6223 if (p == (char_u *)&p_bdir
6224 || p == (char_u *)&p_dir
6225 || p == (char_u *)&p_path
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +01006226 || p == (char_u *)&p_pp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 || p == (char_u *)&p_rtp
6228#ifdef FEAT_SEARCHPATH
6229 || p == (char_u *)&p_cdpath
6230#endif
6231#ifdef FEAT_SESSION
6232 || p == (char_u *)&p_vdir
6233#endif
6234 )
6235 {
6236 xp->xp_context = EXPAND_DIRECTORIES;
6237 if (p == (char_u *)&p_path
6238#ifdef FEAT_SEARCHPATH
6239 || p == (char_u *)&p_cdpath
6240#endif
6241 )
6242 xp->xp_backslash = XP_BS_THREE;
6243 else
6244 xp->xp_backslash = XP_BS_ONE;
6245 }
Bram Moolenaard5e8c922021-02-02 21:10:01 +01006246 else if (p == (char_u *)&p_ft)
6247 {
6248 xp->xp_context = EXPAND_FILETYPE;
6249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 else
6251 {
6252 xp->xp_context = EXPAND_FILES;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006253 // for 'tags' need three backslashes for a space
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (p == (char_u *)&p_tags)
6255 xp->xp_backslash = XP_BS_THREE;
6256 else
6257 xp->xp_backslash = XP_BS_ONE;
6258 }
6259 }
6260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006261 // For an option that is a list of file names, find the start of the
6262 // last file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 for (p = arg + STRLEN(arg) - 1; p > xp->xp_pattern; --p)
6264 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006265 // count number of backslashes before ' ' or ','
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 if (*p == ' ' || *p == ',')
6267 {
6268 s = p;
6269 while (s > xp->xp_pattern && *(s - 1) == '\\')
6270 --s;
6271 if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3))
6272 || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0))
6273 {
6274 xp->xp_pattern = p + 1;
6275 break;
6276 }
6277 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006278
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006279#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006280 // for 'spellsuggest' start at "file:"
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006281 if (options[opt_idx].var == (char_u *)&p_sps
6282 && STRNCMP(p, "file:", 5) == 0)
6283 {
6284 xp->xp_pattern = p + 5;
6285 break;
6286 }
6287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289
6290 return;
6291}
6292
6293 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006294ExpandSettings(
6295 expand_T *xp,
6296 regmatch_T *regmatch,
6297 int *num_file,
6298 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006300 int num_normal = 0; // Nr of matching non-term-code settings
6301 int num_term = 0; // Nr of matching terminal code settings
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 int opt_idx;
6303 int match;
6304 int count = 0;
6305 char_u *str;
6306 int loop;
6307 int is_term_opt;
6308 char_u name_buf[MAX_KEY_NAME_LEN];
6309 static char *(names[]) = {"all", "termcap"};
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006310 int ic = regmatch->rm_ic; // remember the ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006312 // do this loop twice:
6313 // loop == 0: count the number of matching options
6314 // loop == 1: copy the matching options into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 for (loop = 0; loop <= 1; ++loop)
6316 {
6317 regmatch->rm_ic = ic;
6318 if (xp->xp_context != EXPAND_BOOL_SETTINGS)
6319 {
K.Takataeeec2542021-06-02 13:28:16 +02006320 for (match = 0; match < (int)ARRAY_LENGTH(names); ++match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (vim_regexec(regmatch, (char_u *)names[match], (colnr_T)0))
6322 {
6323 if (loop == 0)
6324 num_normal++;
6325 else
6326 (*file)[count++] = vim_strsave((char_u *)names[match]);
6327 }
6328 }
6329 for (opt_idx = 0; (str = (char_u *)options[opt_idx].fullname) != NULL;
6330 opt_idx++)
6331 {
6332 if (options[opt_idx].var == NULL)
6333 continue;
6334 if (xp->xp_context == EXPAND_BOOL_SETTINGS
6335 && !(options[opt_idx].flags & P_BOOL))
6336 continue;
Bram Moolenaardac13472019-09-16 21:06:21 +02006337 is_term_opt = istermoption_idx(opt_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 if (is_term_opt && num_normal > 0)
6339 continue;
6340 match = FALSE;
6341 if (vim_regexec(regmatch, str, (colnr_T)0)
6342 || (options[opt_idx].shortname != NULL
6343 && vim_regexec(regmatch,
6344 (char_u *)options[opt_idx].shortname, (colnr_T)0)))
6345 match = TRUE;
6346 else if (is_term_opt)
6347 {
6348 name_buf[0] = '<';
6349 name_buf[1] = 't';
6350 name_buf[2] = '_';
6351 name_buf[3] = str[2];
6352 name_buf[4] = str[3];
6353 name_buf[5] = '>';
6354 name_buf[6] = NUL;
6355 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6356 {
6357 match = TRUE;
6358 str = name_buf;
6359 }
6360 }
6361 if (match)
6362 {
6363 if (loop == 0)
6364 {
6365 if (is_term_opt)
6366 num_term++;
6367 else
6368 num_normal++;
6369 }
6370 else
6371 (*file)[count++] = vim_strsave(str);
6372 }
6373 }
6374 /*
6375 * Check terminal key codes, these are not in the option table
6376 */
6377 if (xp->xp_context != EXPAND_BOOL_SETTINGS && num_normal == 0)
6378 {
6379 for (opt_idx = 0; (str = get_termcode(opt_idx)) != NULL; opt_idx++)
6380 {
6381 if (!isprint(str[0]) || !isprint(str[1]))
6382 continue;
6383
6384 name_buf[0] = 't';
6385 name_buf[1] = '_';
6386 name_buf[2] = str[0];
6387 name_buf[3] = str[1];
6388 name_buf[4] = NUL;
6389
6390 match = FALSE;
6391 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6392 match = TRUE;
6393 else
6394 {
6395 name_buf[0] = '<';
6396 name_buf[1] = 't';
6397 name_buf[2] = '_';
6398 name_buf[3] = str[0];
6399 name_buf[4] = str[1];
6400 name_buf[5] = '>';
6401 name_buf[6] = NUL;
6402
6403 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6404 match = TRUE;
6405 }
6406 if (match)
6407 {
6408 if (loop == 0)
6409 num_term++;
6410 else
6411 (*file)[count++] = vim_strsave(name_buf);
6412 }
6413 }
6414
6415 /*
6416 * Check special key names.
6417 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006418 regmatch->rm_ic = TRUE; // ignore case here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 for (opt_idx = 0; (str = get_key_name(opt_idx)) != NULL; opt_idx++)
6420 {
6421 name_buf[0] = '<';
6422 STRCPY(name_buf + 1, str);
6423 STRCAT(name_buf, ">");
6424
6425 if (vim_regexec(regmatch, name_buf, (colnr_T)0))
6426 {
6427 if (loop == 0)
6428 num_term++;
6429 else
6430 (*file)[count++] = vim_strsave(name_buf);
6431 }
6432 }
6433 }
6434 if (loop == 0)
6435 {
6436 if (num_normal > 0)
6437 *num_file = num_normal;
6438 else if (num_term > 0)
6439 *num_file = num_term;
6440 else
6441 return OK;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006442 *file = ALLOC_MULT(char_u *, *num_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (*file == NULL)
6444 {
6445 *file = (char_u **)"";
6446 return FAIL;
6447 }
6448 }
6449 }
6450 return OK;
6451}
6452
6453 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006454ExpandOldSetting(int *num_file, char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006456 char_u *var = NULL; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 char_u *buf;
6458
6459 *num_file = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006460 *file = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (*file == NULL)
6462 return FAIL;
6463
6464 /*
6465 * For a terminal key code expand_option_idx is < 0.
6466 */
6467 if (expand_option_idx < 0)
6468 {
6469 var = find_termcode(expand_option_name + 2);
6470 if (var == NULL)
6471 expand_option_idx = findoption(expand_option_name);
6472 }
6473
6474 if (expand_option_idx >= 0)
6475 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006476 // put string of option value in NameBuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 option_value2string(&options[expand_option_idx], expand_option_flags);
6478 var = NameBuff;
6479 }
6480 else if (var == NULL)
6481 var = (char_u *)"";
6482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006483 // A backslash is required before some characters. This is the reverse of
6484 // what happens in do_set().
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 buf = vim_strsave_escaped(var, escape_chars);
6486
6487 if (buf == NULL)
6488 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006489 VIM_CLEAR(*file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 return FAIL;
6491 }
6492
6493#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006494 // For MS-Windows et al. we don't double backslashes at the start and
6495 // before a file name character.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006496 for (var = buf; *var != NUL; MB_PTR_ADV(var))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (var[0] == '\\' && var[1] == '\\'
6498 && expand_option_idx >= 0
6499 && (options[expand_option_idx].flags & P_EXPAND)
6500 && vim_isfilec(var[2])
6501 && (var[2] != '\\' || (var == buf && var[4] != '\\')))
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00006502 STRMOVE(var, var + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503#endif
6504
6505 *file[0] = buf;
6506 *num_file = 1;
6507 return OK;
6508}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510/*
6511 * Get the value for the numeric or string option *opp in a nice format into
6512 * NameBuff[]. Must not be called with a hidden option!
6513 */
6514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006515option_value2string(
6516 struct vimoption *opp,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006517 int opt_flags) // OPT_GLOBAL and/or OPT_LOCAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 char_u *varp;
6520
6521 varp = get_varp_scope(opp, opt_flags);
6522
6523 if (opp->flags & P_NUM)
6524 {
6525 long wc = 0;
6526
6527 if (wc_use_keyname(varp, &wc))
6528 STRCPY(NameBuff, get_special_key_name((int)wc, 0));
6529 else if (wc != 0)
6530 STRCPY(NameBuff, transchar((int)wc));
6531 else
6532 sprintf((char *)NameBuff, "%ld", *(long *)varp);
6533 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006534 else // P_STRING
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 varp = *(char_u **)(varp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006537 if (varp == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 NameBuff[0] = NUL;
6539#ifdef FEAT_CRYPT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006540 // don't show the actual value of 'key', only that it's set
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006541 else if (opp->var == (char_u *)&p_key && *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 STRCPY(NameBuff, "*****");
6543#endif
6544 else if (opp->flags & P_EXPAND)
6545 home_replace(NULL, varp, NameBuff, MAXPATHL, FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006546 // Translate 'pastetoggle' into special key names
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 else if ((char_u **)opp->var == &p_pt)
6548 str2specialbuf(p_pt, NameBuff, MAXPATHL);
6549 else
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006550 vim_strncpy(NameBuff, varp, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
6552}
6553
6554/*
6555 * Return TRUE if "varp" points to 'wildchar' or 'wildcharm' and it can be
6556 * printed as a keyname.
6557 * "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'.
6558 */
6559 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006560wc_use_keyname(char_u *varp, long *wcp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm))
6563 {
6564 *wcp = *(long *)varp;
6565 if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)*wcp) >= 0)
6566 return TRUE;
6567 }
6568 return FALSE;
6569}
6570
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 * Return TRUE if "x" is present in 'shortmess' option, or
6573 * 'shortmess' contains 'a' and "x" is present in SHM_A.
6574 */
6575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006576shortmess(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
Bram Moolenaar7f29f7a2012-02-29 13:51:37 +01006578 return p_shm != NULL &&
6579 ( vim_strchr(p_shm, x) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 || (vim_strchr(p_shm, 'a') != NULL
6581 && vim_strchr((char_u *)SHM_A, x) != NULL));
6582}
6583
6584/*
6585 * paste_option_changed() - Called after p_paste was set or reset.
6586 */
6587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006588paste_option_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
6590 static int old_p_paste = FALSE;
6591 static int save_sm = 0;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006592 static int save_sta = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593#ifdef FEAT_CMDL_INFO
6594 static int save_ru = 0;
6595#endif
6596#ifdef FEAT_RIGHTLEFT
6597 static int save_ri = 0;
6598 static int save_hkmap = 0;
6599#endif
6600 buf_T *buf;
6601
6602 if (p_paste)
6603 {
6604 /*
6605 * Paste switched from off to on.
6606 * Save the current values, so they can be restored later.
6607 */
6608 if (!old_p_paste)
6609 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006610 // save options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006611 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 {
6613 buf->b_p_tw_nopaste = buf->b_p_tw;
6614 buf->b_p_wm_nopaste = buf->b_p_wm;
6615 buf->b_p_sts_nopaste = buf->b_p_sts;
6616 buf->b_p_ai_nopaste = buf->b_p_ai;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006617 buf->b_p_et_nopaste = buf->b_p_et;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006618#ifdef FEAT_VARTABS
6619 if (buf->b_p_vsts_nopaste)
6620 vim_free(buf->b_p_vsts_nopaste);
6621 buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option
6622 ? vim_strsave(buf->b_p_vsts) : NULL;
6623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 }
6625
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006626 // save global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 save_sm = p_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006628 save_sta = p_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629#ifdef FEAT_CMDL_INFO
6630 save_ru = p_ru;
6631#endif
6632#ifdef FEAT_RIGHTLEFT
6633 save_ri = p_ri;
6634 save_hkmap = p_hkmap;
6635#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006636 // save global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006637 p_ai_nopaste = p_ai;
6638 p_et_nopaste = p_et;
6639 p_sts_nopaste = p_sts;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 p_tw_nopaste = p_tw;
6641 p_wm_nopaste = p_wm;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006642#ifdef FEAT_VARTABS
6643 if (p_vsts_nopaste)
6644 vim_free(p_vsts_nopaste);
6645 p_vsts_nopaste = p_vsts && p_vsts != empty_option ? vim_strsave(p_vsts) : NULL;
6646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 }
6648
6649 /*
6650 * Always set the option values, also when 'paste' is set when it is
6651 * already on.
6652 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006653 // set options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006654 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006656 buf->b_p_tw = 0; // textwidth is 0
6657 buf->b_p_wm = 0; // wrapmargin is 0
6658 buf->b_p_sts = 0; // softtabstop is 0
6659 buf->b_p_ai = 0; // no auto-indent
6660 buf->b_p_et = 0; // no expandtab
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006661#ifdef FEAT_VARTABS
6662 if (buf->b_p_vsts)
6663 free_string_option(buf->b_p_vsts);
6664 buf->b_p_vsts = empty_option;
6665 if (buf->b_p_vsts_array)
6666 vim_free(buf->b_p_vsts_array);
6667 buf->b_p_vsts_array = 0;
6668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
6670
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006671 // set global options
6672 p_sm = 0; // no showmatch
6673 p_sta = 0; // no smarttab
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (p_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006676 status_redraw_all(); // redraw to remove the ruler
6677 p_ru = 0; // no ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678#endif
6679#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006680 p_ri = 0; // no reverse insert
6681 p_hkmap = 0; // no Hebrew keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006683 // set global values for local buffer options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 p_tw = 0;
6685 p_wm = 0;
6686 p_sts = 0;
6687 p_ai = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006688#ifdef FEAT_VARTABS
6689 if (p_vsts)
6690 free_string_option(p_vsts);
6691 p_vsts = empty_option;
6692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 }
6694
6695 /*
6696 * Paste switched from on to off: Restore saved values.
6697 */
6698 else if (old_p_paste)
6699 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006700 // restore options for each buffer
Bram Moolenaar29323592016-07-24 22:04:11 +02006701 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
6703 buf->b_p_tw = buf->b_p_tw_nopaste;
6704 buf->b_p_wm = buf->b_p_wm_nopaste;
6705 buf->b_p_sts = buf->b_p_sts_nopaste;
6706 buf->b_p_ai = buf->b_p_ai_nopaste;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006707 buf->b_p_et = buf->b_p_et_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006708#ifdef FEAT_VARTABS
6709 if (buf->b_p_vsts)
6710 free_string_option(buf->b_p_vsts);
6711 buf->b_p_vsts = buf->b_p_vsts_nopaste
6712 ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option;
6713 if (buf->b_p_vsts_array)
6714 vim_free(buf->b_p_vsts_array);
6715 if (buf->b_p_vsts && buf->b_p_vsts != empty_option)
6716 tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array);
6717 else
6718 buf->b_p_vsts_array = 0;
6719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006722 // restore global options
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 p_sm = save_sm;
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006724 p_sta = save_sta;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (p_ru != save_ru)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006727 status_redraw_all(); // redraw to draw the ruler
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 p_ru = save_ru;
6729#endif
6730#ifdef FEAT_RIGHTLEFT
6731 p_ri = save_ri;
6732 p_hkmap = save_hkmap;
6733#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006734 // set global values for local buffer options
Bram Moolenaar54f018c2015-09-15 17:30:40 +02006735 p_ai = p_ai_nopaste;
6736 p_et = p_et_nopaste;
6737 p_sts = p_sts_nopaste;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 p_tw = p_tw_nopaste;
6739 p_wm = p_wm_nopaste;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006740#ifdef FEAT_VARTABS
6741 if (p_vsts)
6742 free_string_option(p_vsts);
6743 p_vsts = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : empty_option;
6744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 }
6746
6747 old_p_paste = p_paste;
6748}
6749
6750/*
6751 * vimrc_found() - Called when a ".vimrc" or "VIMINIT" has been found.
6752 *
6753 * Reset 'compatible' and set the values for options that didn't get set yet
6754 * to the Vim defaults.
6755 * Don't do this if the 'compatible' option has been set or reset before.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006756 * When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 */
6758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006759vimrc_found(char_u *fname, char_u *envname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006761 int opt_idx;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006762 int dofree = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006763 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
6765 if (!option_was_set((char_u *)"cp"))
6766 {
6767 p_cp = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02006768 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 if (!(options[opt_idx].flags & (P_WAS_SET|P_VI_DEF)))
6770 set_option_default(opt_idx, OPT_FREE, FALSE);
6771 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006772 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006774
6775 if (fname != NULL)
6776 {
6777 p = vim_getenv(envname, &dofree);
6778 if (p == NULL)
6779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006780 // Set $MYVIMRC to the first vimrc file found.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006781 p = FullName_save(fname, FALSE);
6782 if (p != NULL)
6783 {
6784 vim_setenv(envname, p);
6785 vim_free(p);
6786 }
6787 }
6788 else if (dofree)
6789 vim_free(p);
6790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791}
6792
6793/*
6794 * Set 'compatible' on or off. Called for "-C" and "-N" command line arg.
6795 */
6796 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006797change_compatible(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006799 int opt_idx;
6800
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (p_cp != on)
6802 {
6803 p_cp = on;
6804 compatible_set();
6805 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00006806 opt_idx = findoption((char_u *)"cp");
6807 if (opt_idx >= 0)
6808 options[opt_idx].flags |= P_WAS_SET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809}
6810
6811/*
6812 * Return TRUE when option "name" has been set.
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006813 * Only works correctly for global options.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 */
6815 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006816option_was_set(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817{
6818 int idx;
6819
6820 idx = findoption(name);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006821 if (idx < 0) // unknown option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 return FALSE;
6823 if (options[idx].flags & P_WAS_SET)
6824 return TRUE;
6825 return FALSE;
6826}
6827
6828/*
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006829 * Reset the flag indicating option "name" was set.
6830 */
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006832reset_option_was_set(char_u *name)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006833{
6834 int idx = findoption(name);
6835
6836 if (idx >= 0)
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006837 {
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006838 options[idx].flags &= ~P_WAS_SET;
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02006839 return OK;
6840 }
6841 return FAIL;
Bram Moolenaar15d55de2012-12-05 14:43:02 +01006842}
6843
6844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 * compatible_set() - Called when 'compatible' has been set or unset.
6846 *
6847 * When 'compatible' set: Set all relevant options (those that have the P_VIM)
6848 * flag) to a Vi compatible value.
6849 * When 'compatible' is unset: Set all options that have a different default
6850 * for Vim (without the P_VI_DEF flag) to that default.
6851 */
6852 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006853compatible_set(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 int opt_idx;
6856
Bram Moolenaardac13472019-09-16 21:06:21 +02006857 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 if ( ((options[opt_idx].flags & P_VIM) && p_cp)
6859 || (!(options[opt_idx].flags & P_VI_DEF) && !p_cp))
6860 set_option_default(opt_idx, OPT_FREE, p_cp);
6861 didset_options();
Bram Moolenaare68c25c2015-08-25 15:39:55 +02006862 didset_options2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863}
6864
Bram Moolenaardac13472019-09-16 21:06:21 +02006865#if defined(FEAT_LINEBREAK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867/*
6868 * fill_breakat_flags() -- called when 'breakat' changes value.
6869 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871fill_breakat_flags(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006873 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 int i;
6875
6876 for (i = 0; i < 256; i++)
6877 breakat_flags[i] = FALSE;
6878
6879 if (p_breakat != NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006880 for (p = p_breakat; *p; p++)
6881 breakat_flags[*p] = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883#endif
6884
6885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 * Check if backspacing over something is allowed.
6887 */
6888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889can_bs(
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006890 int what) // BS_INDENT, BS_EOL, BS_START or BS_NOSTOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006892#ifdef FEAT_JOB_CHANNEL
6893 if (what == BS_START && bt_prompt(curbuf))
6894 return FALSE;
6895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 switch (*p_bs)
6897 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02006898 case '3': return TRUE;
6899 case '2': return (what != BS_NOSTOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 case '1': return (what != BS_START);
6901 case '0': return FALSE;
6902 }
6903 return vim_strchr(p_bs, what) != NULL;
6904}
6905
6906/*
Bram Moolenaar375e3392019-01-31 18:26:10 +01006907 * Return the effective 'scrolloff' value for the current window, using the
6908 * global value when appropriate.
6909 */
6910 long
6911get_scrolloff_value(void)
6912{
6913 return curwin->w_p_so < 0 ? p_so : curwin->w_p_so;
6914}
6915
6916/*
6917 * Return the effective 'sidescrolloff' value for the current window, using the
6918 * global value when appropriate.
6919 */
6920 long
6921get_sidescrolloff_value(void)
6922{
6923 return curwin->w_p_siso < 0 ? p_siso : curwin->w_p_siso;
6924}
6925
6926/*
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006927 * Get the local or global value of 'backupcopy'.
6928 */
6929 unsigned int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006930get_bkc_value(buf_T *buf)
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02006931{
6932 return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags;
6933}
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006934
Bram Moolenaaree857022019-11-09 23:26:40 +01006935#if defined(FEAT_LINEBREAK) || defined(PROTO)
6936/*
6937 * Get the local or global value of 'showbreak'.
6938 */
6939 char_u *
6940get_showbreak_value(win_T *win)
6941{
6942 if (win->w_p_sbr == NULL || *win->w_p_sbr == NUL)
6943 return p_sbr;
6944 if (STRCMP(win->w_p_sbr, "NONE") == 0)
6945 return empty_option;
6946 return win->w_p_sbr;
6947}
6948#endif
6949
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006950#if defined(FEAT_EVAL) || defined(PROTO)
6951/*
6952 * Get window or buffer local options.
6953 */
6954 dict_T *
6955get_winbuf_options(int bufopt)
6956{
6957 dict_T *d;
6958 int opt_idx;
6959
6960 d = dict_alloc();
6961 if (d == NULL)
6962 return NULL;
6963
Bram Moolenaardac13472019-09-16 21:06:21 +02006964 for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006965 {
6966 struct vimoption *opt = &options[opt_idx];
6967
6968 if ((bufopt && (opt->indir & PV_BUF))
6969 || (!bufopt && (opt->indir & PV_WIN)))
6970 {
6971 char_u *varp = get_varp(opt);
6972
6973 if (varp != NULL)
6974 {
6975 if (opt->flags & P_STRING)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006976 dict_add_string(d, opt->fullname, *(char_u **)varp);
Bram Moolenaar789a5c02016-09-12 19:51:11 +02006977 else if (opt->flags & P_NUM)
Bram Moolenaare0be1672018-07-08 16:50:37 +02006978 dict_add_number(d, opt->fullname, *(long *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006979 else
Bram Moolenaare0be1672018-07-08 16:50:37 +02006980 dict_add_number(d, opt->fullname, *(int *)varp);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006981 }
6982 }
6983 }
6984
6985 return d;
6986}
6987#endif
Bram Moolenaar017ba072019-09-14 21:01:23 +02006988
Bram Moolenaardac13472019-09-16 21:06:21 +02006989#if defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar017ba072019-09-14 21:01:23 +02006990/*
6991 * This is called when 'culopt' is changed
6992 */
Bram Moolenaardac13472019-09-16 21:06:21 +02006993 int
Bram Moolenaar017ba072019-09-14 21:01:23 +02006994fill_culopt_flags(char_u *val, win_T *wp)
6995{
6996 char_u *p;
6997 char_u culopt_flags_new = 0;
6998
6999 if (val == NULL)
7000 p = wp->w_p_culopt;
7001 else
7002 p = val;
7003 while (*p != NUL)
7004 {
7005 if (STRNCMP(p, "line", 4) == 0)
7006 {
7007 p += 4;
7008 culopt_flags_new |= CULOPT_LINE;
7009 }
7010 else if (STRNCMP(p, "both", 4) == 0)
7011 {
7012 p += 4;
7013 culopt_flags_new |= CULOPT_LINE | CULOPT_NBR;
7014 }
7015 else if (STRNCMP(p, "number", 6) == 0)
7016 {
7017 p += 6;
7018 culopt_flags_new |= CULOPT_NBR;
7019 }
7020 else if (STRNCMP(p, "screenline", 10) == 0)
7021 {
7022 p += 10;
7023 culopt_flags_new |= CULOPT_SCRLINE;
7024 }
7025
7026 if (*p != ',' && *p != NUL)
7027 return FAIL;
7028 if (*p == ',')
7029 ++p;
7030 }
7031
7032 // Can't have both "line" and "screenline".
7033 if ((culopt_flags_new & CULOPT_LINE) && (culopt_flags_new & CULOPT_SCRLINE))
7034 return FAIL;
7035 wp->w_p_culopt_flags = culopt_flags_new;
7036
7037 return OK;
7038}
7039#endif
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007040
7041/*
7042 * Get the value of 'magic' adjusted for Vim9 script.
7043 */
7044 int
7045magic_isset(void)
7046{
7047 switch (magic_overruled)
7048 {
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01007049 case OPTION_MAGIC_ON: return TRUE;
7050 case OPTION_MAGIC_OFF: return FALSE;
7051 case OPTION_MAGIC_NOT_SET: break;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007052 }
7053#ifdef FEAT_EVAL
7054 if (in_vim9script())
7055 return TRUE;
7056#endif
7057 return p_magic;
7058}